xref: /llvm-project/offload/test/api/omp_device_managed_memory_alloc.c (revision 330d8983d25d08580fc1642fea48b2473f47a9da)
1 // RUN: %libomptarget-compile-run-and-check-generic
2 // RUN: %libomptarget-compileopt-run-and-check-generic
3 
4 #include <omp.h>
5 #include <stdio.h>
6 
main()7 int main() {
8   const int N = 64;
9 
10   // Allocates device managed memory that is shared between the host and device.
11   int *shared_ptr =
12       omp_alloc(N * sizeof(int), llvm_omp_target_shared_mem_alloc);
13 
14 #pragma omp target teams distribute parallel for is_device_ptr(shared_ptr)
15   for (int i = 0; i < N; ++i) {
16     shared_ptr[i] = 1;
17   }
18 
19   int sum = 0;
20   for (int i = 0; i < N; ++i)
21     sum += shared_ptr[i];
22 
23   // CHECK: PASS
24   if (sum == N)
25     printf("PASS\n");
26 
27   omp_free(shared_ptr, llvm_omp_target_shared_mem_alloc);
28 }
29