编译例子示范
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <assert.h> 4 5 int main( int argc, char* argv[] ) 6 { 7 int n; /* size of the vector */ 8 float *restrict a; /* the vector */ 9 float *restrict r; /* the results */ 10 float *restrict e; /* expected results */ 11 int i; 12 if( argc > 1 ) 13 n = atoi( argv[1] ); 14 else 15 n = 100000; 16 if( n <= 0 ) n = 100000; 17 18 a = (float*)malloc(n*sizeof(float)); 19 r = (float*)malloc(n*sizeof(float)); 20 e = (float*)malloc(n*sizeof(float)); 21 for( i = 0; i < n; ++i ) a[i] = (float)(i+1); 22 23 #pragma acc region 24 { 25 for( i = 0; i < n; ++i ) r[i] = a[i]*2.0f; 26 } 27 /* compute on the host to compare */ 28 for( i = 0; i < n; ++i ) e[i] = a[i]*2.0f; 29 /* check the results */ 30 for( i = 0; i < n; ++i ) 31 assert( r[i] == e[i] ); 32 printf( "%d iterations completed\n", n ); 33 return 0; 34 }