Extend dynamic_allocators example and fix previous code.
int main() {
int n = 1024;
+ int r = 0;
+ #pragma omp target map(tofrom:r)
+ {
+ omp_allocator_handle_t al = omp_init_allocator(omp_default_mem_space, 0, {});
+
+ int *p = (int *)omp_alloc(n*sizeof(int), al);
+ #pragma omp parallel for
+ for(int i = 0; i < n; i++)
+ p[i] = i;
+
+ #pragma omp parallel for reduction(+:r)
+ for(int i = 0; i < n; i++)
+ r += p[i];
+
+ omp_free(p, al);
+ }
+
+ // sum of first n numbers starting from 0 is is (n-1)*n/2
+ int res = (n-1)*n/2;
+ if (r != res) {
+ printf("Error: reduction value is %d and should be %d\n", r, res);
+ return 1;
+ }
+
int *q = nullptr;
- #pragma omp target is_device_ptr(q)
+ #pragma omp target map(from:q)
{
omp_allocator_handle_t al = omp_init_allocator(omp_default_mem_space, 0, {});
+
int *p = (int *)omp_alloc(n*sizeof(int), al);
#pragma omp parallel for
for(int i = 0; i < n; i++)
@@ -32,5 +57,8 @@ int main() {
err++;
}
+ // not freeing device memory, it will be free'ed when de-initializing the object module
+ // calling omp_free requires to save omp_allocator_handle_t amongst successive target regions
+
return err;
}
