OpenMP-Examples/devices/sources/target_defaultmap.1.c
2024-04-16 08:59:23 -07:00

105 lines
3.2 KiB
C

/*
* @@name: target_defaultmap.1
* @@type: C
* @@operation: run
* @@expect: success
* @@version: omp_5.2
*/
#include <stdlib.h>
#include <stdio.h>
#define N 2
int main(){
typedef struct S_struct { int s; int A[N]; } S_struct_t;
int s; //scalar int variable (scalar)
int A[N]; //aggregate variable (array)
S_struct_t S; //aggregate variable (structure)
int *ptr; //scalar, pointer variable (pointer)
int s1, s2, s3;
// Initialize everything to zero;
s=2; s1=s2=s3=0;
A[0]=0; A[1]=0;
S.s=0; S.A[0]=0; S.A[1]=0;
// Target Region 1
// Uses defaultmap to set scalars, aggregates &
// pointers to normal defaults.
#pragma omp target \
defaultmap(firstprivate: scalar) /* may also be default */ \
defaultmap(tofrom: aggregate)/* may also be default */ \
defaultmap(default: pointer) /* must be default */
{
s = 3; //SCALAR firstprivate, value not returned
A[0] = 3; A[1] = 3; //AGGREGATE array, default map tofrom
//AGGREGATE structure, default tofrom
S.s = 2;
S.A[0] = 2; S.A[1] = 2;
ptr = &A[0]; //POINTER is private
ptr[0] = 2; ptr[1] = 2;
}
if(s==2 && A[0]==2 && S.s==2 && S.A[0]==2)
printf(" PASSED 1 of 5\n");
// Target Region 2
// no implicit mapping allowed.
#pragma omp target defaultmap(none) map(tofrom: s, A, S)
{
s +=5; // All variables must be explicitly mapped
A[0] +=5; A[1]+=5;
S.s +=5;
S.A[0]+=5; S.A[1]+=5;
}
if(s==7 && A[0]==7 && S.s==7 && S.A[0]==7)
printf(" PASSED 2 of 5\n");
// Target Region 3
// defaultmap & explicit data-sharing clause
// with variables in same category
s1=s2=s3=1;
#pragma omp target defaultmap(tofrom: scalar) firstprivate(s1,s2)
{
s1 += 5; // firstprivate (s1 value not returned to host)
s2 += 5; // firstprivate (s2 value not returned to host)
s3 += s1 + s2; // mapped as tofrom
}
if(s1==1 && s2==1 && s3==13 ) printf(" PASSED 3 of 5\n");
// Target Region 4
A[0]=0; A[1]=0;
S.A[0]=0; S.A[1]=0;
// arrays and structure are firstprivate, and scalars are from
#pragma omp target defaultmap(firstprivate: aggregate) \
map(from: s1, s2)
{
A[0]+=1; S.A[0]+=1; //Aggregate changes not returned to host
A[1]+=1; S.A[1]+=1; //Aggregate changes not returned to host
s1 = A[0]+S.A[0]; //s1 value returned to host
s2 = A[1]+S.A[1]; //s1 value returned to host
}
if( A[0]==0 && S.A[0]==0 && s1==2 ) printf(" PASSED 4 of 5\n");
// Target Region 5
// defaultmap using all variable category
s1=s2=s3=1;
#pragma omp target defaultmap(to: all) map(from: s3)
{
s1 += 5; // mapped as to
s2 += 5; // mapped as to
s3 = s1 + s2; // mapped as from
}
if(s1==1 && s2==1 && s3==12 ) printf(" PASSED 5 of 5\n");
}