diff options
author | Jonathan Peyton <jonathan.l.peyton@intel.com> | 2018-09-07 18:25:49 +0000 |
---|---|---|
committer | Jonathan Peyton <jonathan.l.peyton@intel.com> | 2018-09-07 18:25:49 +0000 |
commit | 92ca61884b6a675a3c2be33244ef3dfca61f94ae (patch) | |
tree | 256075a55b79eed432512c0f4c2264ba0d5b8c5c /openmp/runtime/test/api/omp_alloc.c | |
parent | b97d18945b821fe4f9b4c7059eb87e2a8734ab5d (diff) | |
download | bcm5719-llvm-92ca61884b6a675a3c2be33244ef3dfca61f94ae.tar.gz bcm5719-llvm-92ca61884b6a675a3c2be33244ef3dfca61f94ae.zip |
[OpenMP] Initial implementation of OMP 5.0 Memory Management routines
Implemented omp_alloc, omp_free, omp_{set,get}_default_allocator entries,
and OMP_ALLOCATOR environment variable.
Added support for HBW memory on Linux if libmemkind.so library is accessible
(dynamic library only, no support for static libraries).
Only used stable API (hbwmalloc) of the memkind library
though we may consider using experimental API in future.
The ICV def-allocator-var is implemented per implicit task similar to
place-partition-var. In the absence of a requested allocator, the uses the
default allocator.
Predefined allocators (the only ones currently available) are made similar
for C and Fortran, - pointers (long integers) with values 1 to 8.
Patch by Andrey Churbanov
Differential Revision: https://reviews.llvm.org/D51232
llvm-svn: 341687
Diffstat (limited to 'openmp/runtime/test/api/omp_alloc.c')
-rw-r--r-- | openmp/runtime/test/api/omp_alloc.c | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/openmp/runtime/test/api/omp_alloc.c b/openmp/runtime/test/api/omp_alloc.c new file mode 100644 index 00000000000..afad4a504e3 --- /dev/null +++ b/openmp/runtime/test/api/omp_alloc.c @@ -0,0 +1,78 @@ +// RUN: %libomp-compile-and-run +#include <stdio.h> +#include <stdint.h> +#include <omp.h> +#include "omp_testsuite.h" + +#define ARRAY_SIZE 10000 + +int test_omp_alloc() { + int err; + int i, j; + int *shared_array; + const omp_allocator_t *allocator; + const omp_allocator_t *test_allocator; + // Currently, only default memory allocator is implemented + const omp_allocator_t *allocators[] = { + omp_default_mem_alloc, + }; + + err = 0; + for (i = 0; i < sizeof(allocators) / sizeof(allocators[0]); ++i) { + allocator = allocators[i]; + printf("Using %p allocator\n", test_allocator); + omp_set_default_allocator(allocator); + test_allocator = omp_get_default_allocator(); + if (test_allocator != allocator) { + printf("error: omp_set|get_default_allocator() not working\n"); + return 0; + } + shared_array = (int *)omp_alloc(sizeof(int) * ARRAY_SIZE, test_allocator); + if (shared_array == NULL) { + printf("error: shared_array is NULL\n"); + return 0; + } + for (j = 0; j < ARRAY_SIZE; ++j) { + shared_array[j] = j; + } + #pragma omp parallel shared(shared_array) + { + int i; + int tid = omp_get_thread_num(); + int *private_array = + (int *)omp_alloc(sizeof(int) * ARRAY_SIZE, omp_default_mem_alloc); + if (private_array == NULL) { + printf("error: thread %d private_array is NULL\n", tid); + #pragma omp atomic + err++; + } + for (i = 0; i < ARRAY_SIZE; ++i) { + private_array[i] = shared_array[i] + tid; + } + for (i = 0; i < ARRAY_SIZE; ++i) { + if (private_array[i] != i + tid) { + printf("error: thread %d element %d is %d instead of %d\n", tid, i, + private_array[i], i + tid); + #pragma omp atomic + err++; + } + } + omp_free(private_array, omp_default_mem_alloc); + } /* end of parallel */ + omp_free(shared_array, test_allocator); + } + + return !err; +} + +int main() { + int i; + int num_failed = 0; + + for (i = 0; i < REPETITIONS; i++) { + if (!test_omp_alloc()) { + num_failed++; + } + } + return num_failed; +} |