-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add vector class enter/exit data demo
- Loading branch information
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
class Vector { | ||
public: | ||
Vector(int n) : len(n) { | ||
v = new double[len]; | ||
#pragma omp target enter data map(alloc:v[0:len]) | ||
} | ||
~Vector() { | ||
#pragma omp target exit data map(delete:v[0:len]) | ||
delete[] v; | ||
} | ||
double *v; | ||
int len; | ||
}; | ||
|
||
int main(void) | ||
{ | ||
int N=1000; | ||
int i; | ||
Vector vec = Vector(N); | ||
double *v = vec.v; | ||
|
||
for (i=0; i<N; i++) { | ||
vec.v[i] = 0.5; | ||
} | ||
#pragma omp target update to(vec.v[0:N]) | ||
|
||
#pragma omp target teams | ||
#pragma omp distribute parallel for | ||
for (i=0; i<N; i++) { | ||
v[i] = v[i] * v[i]; | ||
} | ||
|
||
#pragma omp target update from(vec.v[0:N]) | ||
printf("%f %f %f\n", vec.v[0], vec.v[1], vec.v[2]); | ||
|
||
return 0; | ||
} |