llvm-project
56 строк · 1.3 Кб
1// RUN: %libomptarget-compile-run-and-check-generic
2
3#include <cstdio>
4#include <cstdlib>
5
6#define NUM 1024
7
8class C {
9public:
10int *a;
11};
12
13#pragma omp declare mapper(id : C s) map(s.a[0 : NUM])
14
15int main() {
16C c;
17int sum = 0;
18c.a = (int *)malloc(sizeof(int) * NUM);
19for (int i = 0; i < NUM; i++) {
20c.a[i] = 1;
21}
22#pragma omp target enter data map(mapper(id), alloc : c)
23#pragma omp target teams distribute parallel for
24for (int i = 0; i < NUM; i++) {
25c.a[i] = 0;
26}
27#pragma omp target update from(mapper(id) : c)
28for (int i = 0; i < NUM; i++) {
29sum += c.a[i];
30}
31// CHECK: Sum (after first update from) = 0
32printf("Sum (after first update from) = %d\n", sum);
33for (int i = 0; i < NUM; i++) {
34c.a[i] = 1;
35}
36#pragma omp target update to(mapper(id) : c)
37#pragma omp target teams distribute parallel for
38for (int i = 0; i < NUM; i++) {
39++c.a[i];
40}
41sum = 0;
42for (int i = 0; i < NUM; i++) {
43sum += c.a[i];
44}
45// CHECK: Sum (after update to) = 1024
46printf("Sum (after update to) = %d\n", sum);
47#pragma omp target update from(mapper(id) : c)
48sum = 0;
49for (int i = 0; i < NUM; i++) {
50sum += c.a[i];
51}
52// CHECK: Sum (after second update from) = 2048
53printf("Sum (after second update from) = %d\n", sum);
54#pragma omp target exit data map(mapper(id), delete : c)
55return 0;
56}
57