xref: /llvm-project/openmp/runtime/test/api/omp_aligned_calloc.c (revision f5c0c9179f555b2406fcc1a5921d60fd1e534425)
1 // RUN: %libomp-compile-and-run
2 // UNSUPPORTED: gnu
3 
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <omp.h>
7 #define NTH 8
8 #define AL0 64
9 #define AL1 128
10 
main()11 int main()
12 {
13   int err = 0;
14   omp_alloctrait_t at[3];
15   omp_allocator_handle_t a;
16   void *p[NTH];
17   at[0].key = omp_atk_pool_size;
18   at[0].value = 16*1024*1024;
19   at[1].key = omp_atk_fallback;
20   at[1].value = omp_atv_null_fb;
21   a = omp_init_allocator(omp_large_cap_mem_space, 2, at);
22   printf("allocator large created: %p\n", (void *)a);
23   #pragma omp parallel num_threads(8)
24   {
25     int i = omp_get_thread_num();
26     p[i] = omp_aligned_calloc(AL0, 1024*128, 8, a); // API's alignment only
27     #pragma omp barrier
28     printf("th %d, ptr %p\n", i, p[i]);
29     if ((size_t)p[i] % AL0) {
30       #pragma omp atomic
31         err++;
32       printf("Error param: th %d, ptr %p is not %d-byte aligned\n",
33              i, p[i], AL0);
34     }
35     omp_free(p[i], a);
36   }
37   omp_destroy_allocator(a);
38   at[2].key = omp_atk_alignment;
39   at[2].value = AL1;
40   a = omp_init_allocator(omp_large_cap_mem_space, 3, at);
41   printf("allocator large aligned %d created: %p\n", AL1, (void *)a);
42   if (a != omp_null_allocator)
43   #pragma omp parallel num_threads(8)
44   {
45     int i = omp_get_thread_num();
46     p[i] = omp_aligned_calloc(AL0, 1024*128, 8, a); // allocator's alignment wins
47     #pragma omp barrier
48     printf("th %d, ptr %p\n", i, p[i]);
49     if ((size_t)p[i] % AL1) {
50       #pragma omp atomic
51         err++;
52       printf("Error allocator: th %d, ptr %p is not %d-byte aligned\n",
53              i, p[i], AL1);
54     }
55     omp_free(p[i], a);
56   }
57   omp_destroy_allocator(a);
58   at[2].key = omp_atk_alignment;
59   at[2].value = AL0;
60   a = omp_init_allocator(omp_large_cap_mem_space, 3, at);
61   printf("allocator large aligned %d created: %p\n", AL0, (void *)a);
62   #pragma omp parallel num_threads(8)
63   {
64     int i = omp_get_thread_num();
65     p[i] = omp_aligned_calloc(AL1, 1024*128, 8, a); // API's alignment wins
66     #pragma omp barrier
67     printf("th %d, ptr %p\n", i, p[i]);
68     if ((size_t)p[i] % AL1) {
69       #pragma omp atomic
70         err++;
71       printf("Error param: th %d, ptr %p is not %d-byte aligned\n",
72              i, p[i], AL1);
73     }
74     omp_free(p[i], a);
75   }
76   omp_destroy_allocator(a);
77 
78   if (err == 0) {
79     printf("passed\n");
80     return 0;
81   } else {
82     printf("failed\n");
83     return 1;
84   }
85 }
86