LLVM OpenMP
20.0.0git
runtime
test
api
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
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(
"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
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
}
addr
void * addr
Definition:
ittnotify_static.h:152
alignments
int alignments[]
Definition:
kmp_aligned_malloc.c:7
test_kmp_aligned_malloc
int test_kmp_aligned_malloc()
Definition:
kmp_aligned_malloc.c:17
aligned_by
unsigned aligned_by(uint64_t addr)
Definition:
kmp_aligned_malloc.c:9
main
int main()
Definition:
kmp_aligned_malloc.c:51
alignment
static int alignment
Definition:
kmp_alloc.cpp:1500
i
#define i
Definition:
kmp_stub.cpp:87
kmp_free
#define kmp_free
Definition:
kmp_stub.cpp:51
kmp_aligned_malloc
#define kmp_aligned_malloc
Definition:
kmp_stub.cpp:48
omp_testsuite.h
REPETITIONS
#define REPETITIONS
Definition:
omp_testsuite.h:13
err
static int err
Definition:
teams-no-par.c:16
Generated on Wed Jan 29 2025 20:58:01 for LLVM OpenMP by
1.9.6