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