blob: afad4a504e3a8f5a25b6c1b573c6956e36de2f8d [file] [log] [blame]
Jonathan Peyton92ca6182018-09-07 18:25:49 +00001// RUN: %libomp-compile-and-run
2#include <stdio.h>
3#include <stdint.h>
4#include <omp.h>
5#include "omp_testsuite.h"
6
7#define ARRAY_SIZE 10000
8
9int test_omp_alloc() {
10 int err;
11 int i, j;
12 int *shared_array;
13 const omp_allocator_t *allocator;
14 const omp_allocator_t *test_allocator;
15 // Currently, only default memory allocator is implemented
16 const omp_allocator_t *allocators[] = {
17 omp_default_mem_alloc,
18 };
19
20 err = 0;
21 for (i = 0; i < sizeof(allocators) / sizeof(allocators[0]); ++i) {
22 allocator = allocators[i];
23 printf("Using %p allocator\n", test_allocator);
24 omp_set_default_allocator(allocator);
25 test_allocator = omp_get_default_allocator();
26 if (test_allocator != allocator) {
27 printf("error: omp_set|get_default_allocator() not working\n");
28 return 0;
29 }
30 shared_array = (int *)omp_alloc(sizeof(int) * ARRAY_SIZE, test_allocator);
31 if (shared_array == NULL) {
32 printf("error: shared_array is NULL\n");
33 return 0;
34 }
35 for (j = 0; j < ARRAY_SIZE; ++j) {
36 shared_array[j] = j;
37 }
38 #pragma omp parallel shared(shared_array)
39 {
40 int i;
41 int tid = omp_get_thread_num();
42 int *private_array =
43 (int *)omp_alloc(sizeof(int) * ARRAY_SIZE, omp_default_mem_alloc);
44 if (private_array == NULL) {
45 printf("error: thread %d private_array is NULL\n", tid);
46 #pragma omp atomic
47 err++;
48 }
49 for (i = 0; i < ARRAY_SIZE; ++i) {
50 private_array[i] = shared_array[i] + tid;
51 }
52 for (i = 0; i < ARRAY_SIZE; ++i) {
53 if (private_array[i] != i + tid) {
54 printf("error: thread %d element %d is %d instead of %d\n", tid, i,
55 private_array[i], i + tid);
56 #pragma omp atomic
57 err++;
58 }
59 }
60 omp_free(private_array, omp_default_mem_alloc);
61 } /* end of parallel */
62 omp_free(shared_array, test_allocator);
63 }
64
65 return !err;
66}
67
68int main() {
69 int i;
70 int num_failed = 0;
71
72 for (i = 0; i < REPETITIONS; i++) {
73 if (!test_omp_alloc()) {
74 num_failed++;
75 }
76 }
77 return num_failed;
78}