xref: /llvm-project/offload/test/mapping/array_section_implicit_capture.c (revision 16bb7e89a995dfe8b14a8ddbce3da79c217e884a)
1 // RUN: %libomptarget-compile-generic
2 // RUN: %libomptarget-run-generic 2>&1 \
3 // RUN: | %fcheck-generic
4 
5 #include <stdio.h>
6 #include <stdlib.h>
7 
8 #define N 1024
9 #define FROM 64
10 #define LENGTH 128
11 
main()12 int main() {
13   float *A = (float *)malloc(N * sizeof(float));
14   float *B = (float *)malloc(N * sizeof(float));
15   float *C = (float *)malloc(N * sizeof(float));
16 
17   for (int i = 0; i < N; i++) {
18     C[i] = 0.0;
19   }
20 
21   for (int i = 0; i < N; i++) {
22     A[i] = i;
23     B[i] = 2 * i;
24   }
25 
26 #pragma omp target enter data map(to : A[FROM : LENGTH], B[FROM : LENGTH])
27 #pragma omp target enter data map(alloc : C[FROM : LENGTH])
28 
29 // A, B and C have been mapped starting at index FROM, but inside the kernel
30 // they are captured implicitly so the library must look them up using their
31 // base address.
32 #pragma omp target
33   {
34     for (int i = FROM; i < FROM + LENGTH; i++) {
35       C[i] = A[i] + B[i];
36     }
37   }
38 
39 #pragma omp target exit data map(from : C[FROM : LENGTH])
40 #pragma omp target exit data map(delete : A[FROM : LENGTH], B[FROM : LENGTH])
41 
42   int errors = 0;
43   for (int i = FROM; i < FROM + LENGTH; i++)
44     if (C[i] != A[i] + B[i])
45       ++errors;
46 
47   // CHECK: Success
48   if (errors)
49     fprintf(stderr, "Failure\n");
50   else
51     fprintf(stderr, "Success\n");
52 
53   free(A);
54   free(B);
55   free(C);
56 
57   return 0;
58 }
59