LLVM OpenMP 19.0.0git
kmp_aligned_malloc.c
Go to the documentation of this file.
1// RUN: %libomp-compile-and-run
2#include <stdio.h>
3#include <stdint.h>
4#include <omp.h>
5#include "omp_testsuite.h"
6
7int alignments[] = {64, 128, 256, 512, 1024, 2048, 4096};
8
9unsigned 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
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.
32 addr = (uint64_t)ptr;
33 if(addr & (alignment-1)) {
34 printf("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("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
51int main()
52{
53 int i;
54 int num_failed=0;
55
56 for(i = 0; i < REPETITIONS; i++) {
58 num_failed++;
59 }
60 }
61 return num_failed;
62}
void * addr
int alignments[]
int test_kmp_aligned_malloc()
unsigned aligned_by(uint64_t addr)
int main()
static int alignment
Definition: kmp_alloc.cpp:1500
#define i
Definition: kmp_stub.cpp:87
#define kmp_free
Definition: kmp_stub.cpp:51
#define kmp_aligned_malloc
Definition: kmp_stub.cpp:48
#define REPETITIONS
Definition: omp_testsuite.h:13
static int err
Definition: teams-no-par.c:16