source: CIVL/examples/openacc/acc_c2/acc_c2.c

main
Last change on this file was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago

Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@5704 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 2.3 KB
Line 
1/*
2 * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
3 *
4 * NVIDIA CORPORATION and its licensors retain all intellectual property
5 * and proprietary rights in and to this software, related documentation
6 * and any modifications thereto. Any use, reproduction, disclosure or
7 * distribution of this software and related documentation without an express
8 * license agreement from NVIDIA CORPORATION is strictly prohibited.
9 *
10 */
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <assert.h>
15#include <math.h>
16#include <openacc.h>
17#include <accelmath.h>
18
19#if defined(_WIN32) || defined(_WIN64)
20#include <sys/timeb.h>
21#define gettime(a) _ftime(a)
22#define usec(t1,t2) ((((t2).time-(t1).time)*1000+((t2).millitm-(t1).millitm))*100)
23typedef struct _timeb timestruct;
24#else
25#include <sys/time.h>
26#define gettime(a) gettimeofday(a,NULL)
27#define usec(t1,t2) (((t2).tv_sec-(t1).tv_sec)*1000000+((t2).tv_usec-(t1).tv_usec))
28typedef struct timeval timestruct;
29#endif
30
31int main( int argc, char* argv[] )
32{
33 int n; /* size of the vector */
34 float *a; /* the vector */
35 float *restrict r; /* the results */
36 float *e; /* expected results */
37 float s, c;
38 timestruct t1, t2, t3;
39 long long cgpu, chost;
40 int i, nerrors;
41 nerrors = 0;
42 if( argc > 1 )
43 n = atoi( argv[1] );
44 else
45 n = 1000000;
46 if( n <= 0 ) n = 1000000;
47
48 a = (float*)malloc(n*sizeof(float));
49 r = (float*)malloc(n*sizeof(float));
50 e = (float*)malloc(n*sizeof(float));
51 for( i = 0; i < n; ++i ) a[i] = (float)(i+1) * 2.0f;
52 /*acc_init( acc_device_nvidia );*/
53
54 gettime( &t1 );
55 #pragma acc kernels loop
56 for( i = 0; i < n; ++i ){
57 s = sinf(a[i]);
58 c = cosf(a[i]);
59 r[i] = s*s + c*c;
60 }
61 gettime( &t2 );
62 cgpu = usec(t1,t2);
63 for( i = 0; i < n; ++i ){
64 s = sinf(a[i]);
65 c = cosf(a[i]);
66 e[i] = s*s + c*c;
67 }
68 gettime( &t3 );
69 chost = usec(t2,t3);
70 /* check the results */
71 for( i = 0; i < n; ++i ) {
72 if ( fabsf(r[i] - e[i]) >= 0.000001f ) {
73 nerrors++;
74 }
75 }
76
77 printf( "%13d iterations completed\n", n );
78 printf( "%13ld microseconds on GPU\n", cgpu );
79 printf( "%13ld microseconds on host\n", chost );
80 if ( nerrors != 0 ) {
81 printf( "Test FAILED\n");
82 } else {
83 printf( "Test PASSED\n");
84 }
85 return 0;
86}
Note: See TracBrowser for help on using the repository browser.