Skip to content

Commit 6368455

Browse files
committed
[OpenMP][NFC] Add offloading tests for the new ompx APIs
1 parent aaf1149 commit 6368455

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// RUN: %libomptarget-compileopt-run-and-check-generic
2+
//
3+
// UNSUPPORTED: x86_64-pc-linux-gnu
4+
// UNSUPPORTED: x86_64-pc-linux-gnu-LTO
5+
6+
#include <omp.h>
7+
#include <ompx.h>
8+
#include <stdio.h>
9+
#include <stdlib.h>
10+
#include <string.h>
11+
12+
struct info {
13+
int tid, bid, tdim;
14+
};
15+
16+
int main(int argc, char **argv) {
17+
int N = 1 << 20;
18+
if (argc > 1)
19+
N = atoi(argv[1]);
20+
21+
struct info *X = (struct info *)malloc(sizeof(*X) * N);
22+
memset(X, '0', sizeof(*X) * N);
23+
24+
int TL = 256;
25+
int NT = (N + TL - 1) / TL;
26+
27+
#pragma omp target data map(tofrom : X [0:N])
28+
#pragma omp target teams num_teams(NT) thread_limit(TL)
29+
{
30+
#pragma omp parallel
31+
{
32+
int tid = ompx_thread_id_x();
33+
int bid = ompx_block_id_x();
34+
int tdim = ompx_thread_dim_x();
35+
int gid = tid + bid * tdim;
36+
if (gid < N) {
37+
X[gid].tid = tid;
38+
X[gid].bid = bid;
39+
X[gid].tdim = tdim;
40+
};
41+
}
42+
}
43+
44+
int tid = 0, bid = 0, tdim = 256;
45+
for (int i = 0; i < N; i++) {
46+
if (X[i].tid != tid || X[i].bid != bid || X[i].tdim != tdim) {
47+
printf("%i: %i vs %i, %i vs %i, %i vs %i\n", i, X[i].tid, tid, X[i].bid,
48+
bid, X[i].tdim, tdim);
49+
return 1;
50+
}
51+
tid++;
52+
if (tid == tdim) {
53+
tid = 0;
54+
bid++;
55+
}
56+
}
57+
58+
// CHECK: OK
59+
printf("OK");
60+
return 0;
61+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// RUN: %libomptarget-compileopt-run-and-check-generic
2+
//
3+
// UNSUPPORTED: x86_64-pc-linux-gnu
4+
// UNSUPPORTED: x86_64-pc-linux-gnu-LTO
5+
6+
#include <math.h>
7+
#include <omp.h>
8+
#include <ompx.h>
9+
#include <stdio.h>
10+
#include <stdlib.h>
11+
12+
int main(int argc, char **argv) {
13+
int N = 1 << 29;
14+
if (argc > 1)
15+
N = atoi(argv[1]);
16+
float a = 2.f;
17+
18+
float *X = (float *)malloc(sizeof(*X) * N);
19+
float *Y = (float *)malloc(sizeof(*X) * N);
20+
21+
for (int i = 0; i < N; i++) {
22+
X[i] = 1.0f;
23+
Y[i] = 2.0f;
24+
}
25+
26+
int TL = 256;
27+
int NT = (N + TL - 1) / TL;
28+
29+
#pragma omp target data map(to : X [0:N]) map(Y [0:N])
30+
#pragma omp target teams num_teams(NT) thread_limit(TL)
31+
{
32+
#pragma omp parallel
33+
{
34+
int tid = ompx_thread_id_x();
35+
int bid = ompx_block_id_x();
36+
int tdim = ompx_thread_dim_x();
37+
int gid = tid + bid * tdim;
38+
if (gid < N)
39+
Y[gid] = a * X[gid] + Y[gid];
40+
}
41+
}
42+
43+
float maxError = 0.0f;
44+
for (int i = 0; i < N; i++) {
45+
maxError = fmax(maxError, fabs(Y[i] - 4.0f));
46+
if (maxError) {
47+
printf("%i %f %f\n", i, maxError, Y[i]);
48+
break;
49+
}
50+
}
51+
// CHECK: Max error: 0.00
52+
printf("Max error: %f\n", maxError);
53+
54+
return 0;
55+
}

0 commit comments

Comments
 (0)