libgomp testsuite: OpenACC C++ testing.
[official-gcc.git] / libgomp / testsuite / libgomp.oacc-c-c++-common / lib-79.c
blobef3df13ebc9169ea58fc66b11f3f6bd7a3ebd481
1 /* { dg-do run { target openacc_nvidia_accel_selected } } */
2 /* { dg-additional-options "-lcuda" } */
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <openacc.h>
8 #include <cuda.h>
9 #include "timer.h"
11 int
12 main (int argc, char **argv)
14 CUdevice dev;
15 CUfunction delay;
16 CUmodule module;
17 CUresult r;
18 int N;
19 int i;
20 CUstream stream;
21 unsigned long *a, *d_a, dticks;
22 int nbytes;
23 float atime, dtime, hitime, lotime;
24 void *kargs[2];
25 int clkrate;
26 int devnum, nprocs;
28 devnum = 2;
30 acc_init (acc_device_nvidia);
32 devnum = acc_get_device_num (acc_device_nvidia);
34 r = cuDeviceGet (&dev, devnum);
35 if (r != CUDA_SUCCESS)
37 fprintf (stderr, "cuDeviceGet failed: %d\n", r);
38 abort ();
41 r =
42 cuDeviceGetAttribute (&nprocs, CU_DEVICE_ATTRIBUTE_MULTIPROCESSOR_COUNT,
43 dev);
44 if (r != CUDA_SUCCESS)
46 fprintf (stderr, "cuDeviceGetAttribute failed: %d\n", r);
47 abort ();
50 r = cuDeviceGetAttribute (&clkrate, CU_DEVICE_ATTRIBUTE_CLOCK_RATE, dev);
51 if (r != CUDA_SUCCESS)
53 fprintf (stderr, "cuDeviceGetAttribute failed: %d\n", r);
54 abort ();
57 r = cuModuleLoad (&module, "subr.ptx");
58 if (r != CUDA_SUCCESS)
60 fprintf (stderr, "cuModuleLoad failed: %d\n", r);
61 abort ();
64 r = cuModuleGetFunction (&delay, module, "delay");
65 if (r != CUDA_SUCCESS)
67 fprintf (stderr, "cuModuleGetFunction failed: %d\n", r);
68 abort ();
71 nbytes = nprocs * sizeof (unsigned long);
73 dtime = 200.0;
75 dticks = (unsigned long) (dtime * clkrate);
77 N = nprocs;
79 a = (unsigned long *) malloc (nbytes);
80 d_a = (unsigned long *) acc_malloc (nbytes);
82 acc_map_data (a, d_a, nbytes);
84 r = cuStreamCreate (&stream, CU_STREAM_DEFAULT);
85 if (r != CUDA_SUCCESS)
87 fprintf (stderr, "cuStreamCreate failed: %d\n", r);
88 abort ();
91 if (!acc_set_cuda_stream (1, stream))
92 abort ();
94 stream = (CUstream) acc_get_cuda_stream (0);
95 if (stream != NULL)
96 abort ();
98 r = cuStreamCreate (&stream, CU_STREAM_DEFAULT);
99 if (r != CUDA_SUCCESS)
101 fprintf (stderr, "cuStreamCreate failed: %d\n", r);
102 abort ();
105 if (!acc_set_cuda_stream (0, stream))
106 abort ();
108 init_timers (1);
110 kargs[0] = (void *) &d_a;
111 kargs[1] = (void *) &dticks;
113 start_timer (0);
115 for (i = 0; i < N; i++)
117 r = cuLaunchKernel (delay, 1, 1, 1, 1, 1, 1, 0, stream, kargs, 0);
118 if (r != CUDA_SUCCESS)
120 fprintf (stderr, "cuLaunchKernel failed: %d\n", r);
121 abort ();
125 acc_wait_async (0, 1);
127 if (acc_async_test (0) != 0)
128 abort ();
130 if (acc_async_test (1) != 0)
131 abort ();
133 acc_wait (1);
135 atime = stop_timer (0);
137 if (acc_async_test (0) != 1)
138 abort ();
140 if (acc_async_test (1) != 1)
141 abort ();
143 hitime = dtime * N;
144 hitime += hitime * 0.02;
146 lotime = dtime * N;
147 lotime -= lotime * 0.02;
149 if (atime > hitime || atime < lotime)
151 fprintf (stderr, "actual time < delay time\n");
152 abort ();
155 acc_unmap_data (a);
157 fini_timers ();
159 free (a);
160 acc_free (d_a);
162 acc_shutdown (acc_device_nvidia);
164 exit (0);
167 /* { dg-output "" } */