OpenMP-Examples/devices/sources/target_ptr_map.2.c
2022-11-04 09:35:42 -07:00

56 lines
1.1 KiB
C

/*
* @@name: target_ptr_map.2
* @@type: C
* @@operation: run
* @@expect: success
* @@version: omp_5.1
*/
#include <stdio.h>
#include <stdlib.h>
#define N 100
#pragma omp begin declare target
int *p;
extern void use_arg_p(int *p, int n);
extern void use_global_p( int n);
#pragma omp end declare target
int main()
{
int i;
p = (int *)malloc(sizeof(int)*N);
#pragma omp target map(p[:N]) // device p attached to array section
{
for (i=0; i<N; i++) p[i] = i;
use_arg_p(p, N);
use_global_p(N);
} // value of host p is preserved
printf(" %3.3d %3.3d\n", p[1], p[N-1]);
// 003 297 <- output
free(p);
return 0;
}
// A #pragma omp begin declare target is optional here
// because of prototype spec
void use_arg_p(int *q, int n)
{
int i;
for (i=0; i<n; i++)
q[i] *= 2;
}
void use_global_p(int n)
{
int i;
for (i=0; i<n; i++)
p[i] += i; // valid since p is in declare target and called from
// inside target region where p was attached to
// valid memory
}
// A #pragma omp end declare target is optional here
// because of prototype spec