Created
March 24, 2019 23:17
-
-
Save lisanhu/b52a2673a47ab0bfc2f68c1b3b9a1501 to your computer and use it in GitHub Desktop.
OpenACC test code
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
/** | |
* A test code to test whether PGI compiler is successfully installed and configured | |
* for openacc code. | |
* | |
* Origin: https://www.olcf.ornl.gov/tutorials/openacc-vector-addition/ | |
* | |
* Minor changes to print out information when data initialization on host is done | |
* Compile the code with command line: `pgcc -Minfo -ta=tesla openacc_test.c -o test.out` | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <math.h> | |
int main( int argc, char* argv[] ) | |
{ | |
// Size of vectors | |
//int n = 1000000000; | |
int n = 100000000; | |
// Input vectors | |
double *restrict a; | |
double *restrict b; | |
// Output vector | |
double *restrict c; | |
// Size, in bytes, of each vector | |
size_t bytes = n*sizeof(double); | |
// Allocate memory for each vector | |
a = (double*)malloc(bytes); | |
b = (double*)malloc(bytes); | |
c = (double*)malloc(bytes); | |
// Initialize content of input vectors, vector a[i] = sin(i)^2 vector b[i] = cos(i)^2 | |
int i; | |
for(i=0; i<n; i++) { | |
a[i] = sin(i)*sin(i); | |
b[i] = cos(i)*cos(i); | |
} | |
printf("Done initialization!\n"); | |
// sum component wise and save result into vector c | |
#pragma acc kernels copyin(a[0:n],b[0:n]), copyout(c[0:n]) | |
for(i=0; i<n; i++) { | |
c[i] = a[i] + b[i]; | |
} | |
// Sum up vector c and print result divided by n, this should equal 1 within error | |
double sum = 0.0; | |
for(i=0; i<n; i++) { | |
sum += c[i]; | |
} | |
sum = sum/n; | |
printf("final result: %f\n", sum); | |
// Release memory | |
free(a); | |
free(b); | |
free(c); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment