1 | // RUN: %libomp-compile-and-run |
2 | #include <stdio.h> |
3 | #include <stdint.h> |
4 | #include <omp.h> |
5 | #include "omp_testsuite.h" |
6 | |
7 | int alignments[] = {64, 128, 256, 512, 1024, 2048, 4096}; |
8 | |
9 | unsigned aligned_by(uint64_t addr) { |
10 | uint64_t alignment = 1; |
11 | while((addr & (alignment-1)) == 0) { |
12 | alignment <<= 1; |
13 | } |
14 | return (alignment >> 1); |
15 | } |
16 | |
17 | int test_kmp_aligned_malloc() |
18 | { |
19 | int err = 0; |
20 | #pragma omp parallel shared(err) |
21 | { |
22 | int i; |
23 | int* ptr; |
24 | uint64_t addr; |
25 | int tid = omp_get_thread_num(); |
26 | |
27 | for(i = 0; i < sizeof(alignments)/sizeof(int); i++) { |
28 | int alignment = alignments[i]; |
29 | // allocate 64 bytes with 64-byte alignment |
30 | // allocate 128 bytes with 128-byte alignment, etc. |
31 | ptr = (int*)kmp_aligned_malloc(alignment, alignment); |
32 | addr = (uint64_t)ptr; |
33 | if(addr & (alignment-1)) { |
34 | printf(format: "thread %d: addr = %p (aligned to %u bytes) but expected " |
35 | " alignment = %d\n" , tid, ptr, aligned_by(addr), alignment); |
36 | err = 1; |
37 | } |
38 | kmp_free(ptr); |
39 | } |
40 | |
41 | ptr = kmp_aligned_malloc(128, 127); |
42 | if (ptr != NULL) { |
43 | printf(format: "thread %d: kmp_aligned_malloc() didn't return NULL when " |
44 | "alignment was not power of 2\n" , tid); |
45 | err = 1; |
46 | } |
47 | } /* end of parallel */ |
48 | return !err; |
49 | } |
50 | |
51 | int main() |
52 | { |
53 | int i; |
54 | int num_failed=0; |
55 | |
56 | for(i = 0; i < REPETITIONS; i++) { |
57 | if(!test_kmp_aligned_malloc()) { |
58 | num_failed++; |
59 | } |
60 | } |
61 | return num_failed; |
62 | } |
63 | |