Skip to content

Commit 7043b59

Browse files
authored
Merge pull request #219 from PatKamin/basic-example
Basic example with documentation
2 parents 4ae8c9d + 7600f95 commit 7043b59

File tree

14 files changed

+441
-94
lines changed

14 files changed

+441
-94
lines changed

.github/workflows/basic.yml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ jobs:
5555
-DUSE_ASAN=${{matrix.sanitizers.asan}}
5656
-DUSE_UBSAN=${{matrix.sanitizers.ubsan}}
5757
-DUSE_TSAN=${{matrix.sanitizers.tsan}}
58+
-DUMF_BUILD_EXAMPLES=ON
5859
5960
- name: Build UMF
6061
run: |
@@ -63,14 +64,18 @@ jobs:
6364
- name: Run tests
6465
working-directory: ${{github.workspace}}/build
6566
run: |
66-
ctest --output-on-failure
67+
ctest --output-on-failure --test-dir test
6768
6869
- name: Test make install
70+
# Run only when the example is built
71+
if: matrix.os_provider == 'ON' && matrix.pool_tracking == 'ON'
6972
working-directory: ${{env.BUILD_DIR}}
7073
run: ${{github.workspace}}/test/test_make_install.sh \
7174
${{github.workspace}} ${{env.BUILD_DIR}} ${{env.INSTL_DIR}} ${{matrix.build_type}} ${{matrix.shared_library}}
7275

7376
- name: Test make uninstall
77+
# Run only when the example is built
78+
if: matrix.os_provider == 'ON' && matrix.pool_tracking == 'ON'
7479
working-directory: ${{env.BUILD_DIR}}
7580
run: ${{github.workspace}}/test/test_make_uninstall.sh ${{github.workspace}} ${{env.BUILD_DIR}} ${{env.INSTL_DIR}}
7681

@@ -194,11 +199,17 @@ jobs:
194199
ctest --output-on-failure
195200
196201
- name: Test make install
202+
# Run only when the example is built
203+
# TODO: Modify installation test to accept output varying with build options
204+
if: matrix.os_provider == 'ON' && matrix.pool_tracking == 'ON'
197205
working-directory: ${{env.BUILD_DIR}}
198206
run: ${{github.workspace}}/test/test_make_install.sh \
199207
${{github.workspace}} ${{env.BUILD_DIR}} ${{env.INSTL_DIR}} ${{matrix.build_type}} ${{matrix.shared_library}}
200208

201209
- name: Test make uninstall
210+
# Run only when the example is built
211+
# TODO: Modify installation test to accept output varying with build options
212+
if: matrix.os_provider == 'ON' && matrix.pool_tracking == 'ON'
202213
working-directory: ${{env.BUILD_DIR}}
203214
run: ${{github.workspace}}/test/test_make_uninstall.sh ${{github.workspace}} ${{env.BUILD_DIR}} ${{env.INSTL_DIR}}
204215

.github/workflows/pr_push.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,17 @@ jobs:
4949
-DUMF_BUILD_LIBUMF_POOL_DISJOINT=${{matrix.disjoint}}
5050
-DUMF_BUILD_LIBUMF_POOL_JEMALLOC=${{matrix.jemalloc}}
5151
-DUMF_BUILD_TESTS=OFF
52+
-DUMF_BUILD_EXAMPLES=ON
5253
5354
- name: Build
5455
run: >
5556
cmake --build ${{github.workspace}}/build --config Release -j ${{matrix.nproc}}
5657
58+
- name: Run examples
59+
working-directory: ${{github.workspace}}/build
60+
run: |
61+
ctest --output-on-failure --test-dir examples
62+
5763
CodeStyle:
5864
name: Coding style
5965
runs-on: ubuntu-latest

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ option(UMF_BUILD_LIBUMF_POOL_SCALABLE "Build the libumf_pool_scalable static lib
1414
option(UMF_BUILD_TESTS "Build UMF tests" ON)
1515
option(UMF_BUILD_BENCHMARKS "Build UMF benchmarks" OFF)
1616
option(UMF_BUILD_BENCHMARKS_MT "Build UMF multithreaded benchmarks" OFF)
17+
option(UMF_BUILD_EXAMPLES "Build UMF examples" ON)
1718
option(UMF_ENABLE_POOL_TRACKING "Build UMF with pool tracking" ON)
1819
option(UMF_DEVELOPER_MODE "Enable developer checks, treats warnings as errors" OFF)
1920
option(UMF_FORMAT_CODE_STYLE "Format UMF code with clang-format" OFF)
@@ -123,6 +124,10 @@ if(UMF_BUILD_BENCHMARKS)
123124
endif()
124125
endif()
125126

127+
if(UMF_BUILD_EXAMPLES)
128+
add_subdirectory(examples)
129+
endif()
130+
126131
# Check if clang-format (in correct version) is available for code formatting.
127132
if(UMF_FORMAT_CODE_STYLE)
128133
find_program(CLANG_FORMAT NAMES clang-format-15 clang-format-15.0 clang-format)

README.md

Lines changed: 100 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,97 @@
88
[![Nightly](https://github.com/oneapi-src/unified-memory-framework/actions/workflows/nightly.yml/badge.svg?branch=main)](https://github.com/oneapi-src/unified-memory-framework/actions/workflows/nightly.yml)
99
[![OpenSSF Scorecard](https://api.securityscorecards.dev/projects/github.com/oneapi-src/unified-memory-framework/badge)](https://securityscorecards.dev/viewer/?uri=github.com/oneapi-src/unified-memory-framework)
1010

11+
## Introduction
12+
1113
The Unified Memory Framework (UMF) is a library for constructing allocators and memory pools. It also contains broadly useful abstractions and utilities for memory management. UMF allows users to manage multiple memory pools characterized by different attributes, allowing certain allocation types to be isolated from others and allocated using different hardware resources as required.
1214

13-
# Architecture: memory pools and providers
15+
## Usage
16+
17+
For a quick introduction to UMF usage, see [Example usage](https://oneapi-src.github.io/unified-memory-framework/example-usage.html),
18+
which includes the code of the basic [example](https://github.com/oneapi-src/unified-memory-framework/blob/main/examples/basic/basic.c).
19+
20+
## Build
21+
22+
### Requirements
23+
24+
Required packages:
25+
- C compiler
26+
- [CMake](https://cmake.org/) >= 3.14.0
27+
28+
For development and contributions:
29+
- clang-format-15.0 (can be installed with `python -m pip install clang-format==15.0.7`)
30+
31+
For building tests, multithreaded benchmarks and Disjoint Pool:
32+
- C++ compiler with C++17 support
33+
34+
### Linux
35+
36+
Executable and binaries will be in **build/bin**
37+
38+
```bash
39+
$ mkdir build
40+
$ cd build
41+
$ cmake {path_to_source_dir}
42+
$ make
43+
```
44+
45+
### Windows
46+
47+
Generating Visual Studio Project. EXE and binaries will be in **build/bin/{build_config}**
48+
49+
```bash
50+
$ mkdir build
51+
$ cd build
52+
$ cmake {path_to_source_dir} -G "Visual Studio 15 2017 Win64"
53+
```
54+
55+
### Benchmark
56+
57+
UMF comes with a single-threaded micro benchmark based on [ubench](https://github.com/sheredom/ubench.h).
58+
In order to build the benchmark, the `UMF_BUILD_BENCHMARKS` CMake configuration flag has to be turned `ON`.
59+
60+
UMF also provides multithreaded benchmarks that can be enabled by setting both
61+
`UMF_BUILD_BENCHMARKS` and `UMF_BUILD_BENCHMARKS_MT` CMake
62+
configuration flags to `ON`. Multithreaded benchmarks require a C++ support.
63+
64+
### Sanitizers
65+
66+
List of sanitizers available on Linux:
67+
- AddressSanitizer
68+
- UndefinedBehaviorSanitizer
69+
- ThreadSanitizer
70+
- Is mutually exclusive with other sanitizers.
71+
- MemorySanitizer
72+
- Requires linking against MSan-instrumented libraries to prevent false positive reports. More information [here](https://github.com/google/sanitizers/wiki/MemorySanitizerLibcxxHowTo).
73+
74+
List of sanitizers available on Windows:
75+
- AddressSanitizer
76+
77+
Listed sanitizers can be enabled with appropriate [CMake options](#cmake-standard-options).
78+
79+
### CMake standard options
80+
81+
List of options provided by CMake:
82+
83+
| Name | Description | Values | Default |
84+
| - | - | - | - |
85+
| UMF_BUILD_SHARED_LIBRARY | Build UMF as shared library | ON/OFF | OFF |
86+
| UMF_BUILD_OS_MEMORY_PROVIDER | Build OS memory provider | ON/OFF | ON |
87+
| UMF_BUILD_LIBUMF_POOL_DISJOINT | Build the libumf_pool_disjoint static library | ON/OFF | OFF |
88+
| UMF_BUILD_LIBUMF_POOL_JEMALLOC | Build the libumf_pool_jemalloc static library | ON/OFF | OFF |
89+
| UMF_BUILD_LIBUMF_POOL_SCALABLE | Build the libumf_pool_scalable static library | ON/OFF | OFF |
90+
| UMF_BUILD_TESTS | Build UMF tests | ON/OFF | ON |
91+
| UMF_BUILD_BENCHMARKS | Build UMF benchmarks | ON/OFF | OFF |
92+
| UMF_BUILD_EXAMPLES | Build UMF examples | ON/OFF | ON |
93+
| UMF_ENABLE_POOL_TRACKING | Build UMF with pool tracking | ON/OFF | ON |
94+
| UMF_DEVELOPER_MODE | Treat warnings as errors and enables additional checks | ON/OFF | OFF |
95+
| UMF_FORMAT_CODE_STYLE | Add clang-format-check and clang-format-apply targets to make | ON/OFF | OFF |
96+
| USE_ASAN | Enable AddressSanitizer checks | ON/OFF | OFF |
97+
| USE_UBSAN | Enable UndefinedBehaviorSanitizer checks | ON/OFF | OFF |
98+
| USE_TSAN | Enable ThreadSanitizer checks | ON/OFF | OFF |
99+
| USE_MSAN | Enable MemorySanitizer checks | ON/OFF | OFF |
100+
101+
## Architecture: memory pools and providers
14102

15103
A UMF memory pool is a combination of a pool allocator and a memory provider. A memory provider is responsible for coarse-grained memory allocations and management of memory pages, while the pool allocator controls memory pooling and handles fine-grained memory allocations.
16104

@@ -20,13 +108,13 @@ UMF comes with predefined pool allocators (see include/pool) and providers (see
20108

21109
More detailed documentation is available here: https://oneapi-src.github.io/unified-memory-framework/
22110

23-
## Memory providers
111+
### Memory providers
24112

25-
### OS memory provider (Linux-only yet)
113+
#### OS memory provider (Linux-only yet)
26114

27115
A memory provider that provides memory from an operating system.
28116

29-
#### Requirements
117+
##### Requirements
30118

31119
1) Linux OS
32120
2) The `UMF_BUILD_OS_MEMORY_PROVIDER` option turned `ON` (by default)
@@ -35,103 +123,44 @@ A memory provider that provides memory from an operating system.
35123
4) Required packages for tests:
36124
- libnuma-dev
37125

38-
## Memory pool managers
126+
### Memory pool managers
39127

40-
### proxy_pool (part of libumf)
128+
#### proxy_pool (part of libumf)
41129

42130
This memory pool is distributed as part of libumf. It forwards all requests to the underlying
43131
memory provider. Currently umfPoolRealloc, umfPoolCalloc and umfPoolMallocUsableSize functions
44132
are not supported by the proxy pool.
45133

46-
### libumf_pool_disjoint
134+
#### libumf_pool_disjoint
47135

48136
TODO: Add a description
49137

50-
#### Requirements
138+
##### Requirements
51139

52140
To enable this feature, the `UMF_BUILD_LIBUMF_POOL_DISJOINT` option needs to be turned `ON`.
53141

54-
### libumf_pool_jemalloc (Linux-only)
142+
#### libumf_pool_jemalloc (Linux-only)
55143

56144
libumf_pool_jemalloc is a [jemalloc](https://github.com/jemalloc/jemalloc)-based memory pool manager built as a separate static library.
57145
The `UMF_BUILD_LIBUMF_POOL_JEMALLOC` option has to be turned `ON` to build this library.
58146

59-
#### Requirements
147+
##### Requirements
60148

61149
1) The `UMF_BUILD_LIBUMF_POOL_JEMALLOC` option turned `ON`
62150
2) Required packages:
63151
- libjemalloc-dev
64152

65-
### libumf_pool_scalable (Linux-only)
153+
#### libumf_pool_scalable (Linux-only)
66154

67155
libumf_pool_scalable is a [oneTBB](https://github.com/oneapi-src/oneTBB)-based memory pool manager built as a separate static library.
68156
The `UMF_BUILD_LIBUMF_POOL_SCALABLE` option has to be turned `ON` to build this library.
69157

70-
#### Requirements
158+
##### Requirements
71159

72160
1) The `UMF_BUILD_LIBUMF_POOL_SCALABLE` option turned `ON`
73161
2) Required packages:
74162
- libtbb-dev (libraries: libtbbmalloc.so.2)
75163

76-
## Building
77-
78-
### Requirements
79-
80-
Required packages:
81-
- C compiler
82-
- [CMake](https://cmake.org/) >= 3.14.0
83-
84-
For development and contributions:
85-
- clang-format-15.0 (can be installed with `python -m pip install clang-format==15.0.7`)
86-
87-
For building tests, multithreaded benchmarks and Disjoint Pool:
88-
- C++ compiler with C++17 support
89-
90-
### Benchmark
91-
92-
UMF comes with a single-threaded micro benchmark based on [ubench](https://github.com/sheredom/ubench.h).
93-
In order to build the benchmark, the `UMF_BUILD_BENCHMARKS` CMake configuration flag has to be turned `ON`.
94-
95-
UMF also provides multithreaded benchmarks that can be enabled by setting both
96-
`UMF_BUILD_BENCHMARKS` and `UMF_BUILD_BENCHMARKS_MT` CMake
97-
configuration flags to `ON`. Multithreaded benchmarks require a C++ support.
98-
99-
### Windows
100-
101-
Generating Visual Studio Project. EXE and binaries will be in **build/bin/{build_config}**
102-
103-
```bash
104-
$ mkdir build
105-
$ cd build
106-
$ cmake {path_to_source_dir} -G "Visual Studio 15 2017 Win64"
107-
```
108-
109-
### Linux
110-
111-
Executable and binaries will be in **build/bin**
112-
113-
```bash
114-
$ mkdir build
115-
$ cd build
116-
$ cmake {path_to_source_dir}
117-
$ make
118-
```
119-
120-
### Sanitizers
121-
122-
List of sanitizers available on Linux:
123-
- AddressSanitizer
124-
- UndefinedBehaviorSanitizer
125-
- ThreadSanitizer
126-
- Is mutually exclusive with other sanitizers.
127-
- MemorySanitizer
128-
- Requires linking against MSan-instrumented libraries to prevent false positive reports. More information [here](https://github.com/google/sanitizers/wiki/MemorySanitizerLibcxxHowTo).
129-
130-
List of sanitizers available on Windows:
131-
- AddressSanitizer
132-
133-
Listed sanitizers can be enabled with appropriate [CMake options](#cmake-standard-options).
134-
135164
## Contributions
136165

137166
All code has to be formatted using clang-format. To check the formatting do:
@@ -148,24 +177,3 @@ Additionally, to apply code formatting do:
148177
```bash
149178
$ make clang-format-apply
150179
```
151-
152-
### CMake standard options
153-
154-
List of options provided by CMake:
155-
156-
| Name | Description | Values | Default |
157-
| - | - | - | - |
158-
| UMF_BUILD_SHARED_LIBRARY | Build UMF as shared library | ON/OFF | OFF |
159-
| UMF_BUILD_OS_MEMORY_PROVIDER | Build OS memory provider | ON/OFF | ON |
160-
| UMF_BUILD_LIBUMF_POOL_DISJOINT | Build the libumf_pool_disjoint static library | ON/OFF | OFF |
161-
| UMF_BUILD_LIBUMF_POOL_JEMALLOC | Build the libumf_pool_jemalloc static library | ON/OFF | OFF |
162-
| UMF_BUILD_LIBUMF_POOL_SCALABLE | Build the libumf_pool_scalable static library | ON/OFF | OFF |
163-
| UMF_BUILD_TESTS | Build UMF tests | ON/OFF | ON |
164-
| UMF_BUILD_BENCHMARKS | Build UMF benchmarks | ON/OFF | OFF |
165-
| UMF_ENABLE_POOL_TRACKING | Build UMF with pool tracking | ON/OFF | ON |
166-
| UMF_DEVELOPER_MODE | Treat warnings as errors and enables additional checks | ON/OFF | OFF |
167-
| UMF_FORMAT_CODE_STYLE | Add clang-format-check and clang-format-apply targets to make | ON/OFF | OFF |
168-
| USE_ASAN | Enable AddressSanitizer checks | ON/OFF | OFF |
169-
| USE_UBSAN | Enable UndefinedBehaviorSanitizer checks | ON/OFF | OFF |
170-
| USE_TSAN | Enable ThreadSanitizer checks | ON/OFF | OFF |
171-
| USE_MSAN | Enable MemorySanitizer checks | ON/OFF | OFF |

examples/CMakeLists.txt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright (C) 2024 Intel Corporation
2+
# Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
3+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
5+
if(UMF_BUILD_OS_MEMORY_PROVIDER
6+
AND UMF_BUILD_LIBUMF_POOL_SCALABLE
7+
AND UMF_ENABLE_POOL_TRACKING)
8+
set(EXAMPLE_NAME umf_example_basic)
9+
add_umf_executable(NAME ${EXAMPLE_NAME}
10+
SRCS basic/basic.c
11+
LIBS umf
12+
scalable_pool)
13+
target_include_directories(${EXAMPLE_NAME} PRIVATE
14+
${UMF_CMAKE_SOURCE_DIR}/include)
15+
16+
add_test(NAME ${EXAMPLE_NAME}
17+
COMMAND ${EXAMPLE_NAME}
18+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
19+
20+
set_tests_properties(${EXAMPLE_NAME} PROPERTIES LABELS "example")
21+
22+
install(FILES basic/basic.c
23+
DESTINATION "${CMAKE_INSTALL_DOCDIR}/examples")
24+
25+
else()
26+
message(STATUS "Basic example requires UMF_BUILD_OS_MEMORY_PROVIDER,
27+
UMF_BUILD_LIBUMF_POOL_SCALABLE and UMF_ENABLE_POOL_TRACKING
28+
to be turned ON - skipping")
29+
endif()

examples/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Examples
2+
3+
This directory contains examples of UMF usage. Each example has a brief description below.
4+
5+
## Basic
6+
7+
This example covers the basics of UMF API. It walks you through a basic usage of a memory provider and a pool allocator. OS memory provider and Scalable pool are used for this purpose.
8+
9+
### Required CMake configuration flags
10+
* UMF_BUILD_OS_MEMORY_PROVIDER=ON
11+
* UMF_BUILD_LIBUMF_POOL_SCALABLE=ON
12+
* UMF_ENABLE_POOL_TRACKING=ON

0 commit comments

Comments
 (0)