C++: simplify output from suggest_alternatives_for
[official-gcc.git] / libgomp / testsuite / libgomp.oacc-c-c++-common / lib-81.c
blobd5f18f00319cb07d0dc33a9189ce5faad2151177
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 *streams, stream;
21 unsigned long *a, *d_a, dticks;
22 int nbytes;
23 float atime, dtime;
24 void *kargs[2];
25 int clkrate;
26 int devnum, nprocs;
28 acc_init (acc_device_nvidia);
30 devnum = acc_get_device_num (acc_device_nvidia);
32 r = cuDeviceGet (&dev, devnum);
33 if (r != CUDA_SUCCESS)
35 fprintf (stderr, "cuDeviceGet failed: %d\n", r);
36 abort ();
39 r =
40 cuDeviceGetAttribute (&nprocs, CU_DEVICE_ATTRIBUTE_MULTIPROCESSOR_COUNT,
41 dev);
42 if (r != CUDA_SUCCESS)
44 fprintf (stderr, "cuDeviceGetAttribute failed: %d\n", r);
45 abort ();
48 r = cuDeviceGetAttribute (&clkrate, CU_DEVICE_ATTRIBUTE_CLOCK_RATE, dev);
49 if (r != CUDA_SUCCESS)
51 fprintf (stderr, "cuDeviceGetAttribute failed: %d\n", r);
52 abort ();
55 r = cuModuleLoad (&module, "subr.ptx");
56 if (r != CUDA_SUCCESS)
58 fprintf (stderr, "cuModuleLoad failed: %d\n", r);
59 abort ();
62 r = cuModuleGetFunction (&delay, module, "delay");
63 if (r != CUDA_SUCCESS)
65 fprintf (stderr, "cuModuleGetFunction failed: %d\n", r);
66 abort ();
69 nbytes = nprocs * sizeof (unsigned long);
71 dtime = 500.0;
73 dticks = (unsigned long) (dtime * clkrate);
75 N = nprocs;
77 a = (unsigned long *) malloc (nbytes);
78 d_a = (unsigned long *) acc_malloc (nbytes);
80 acc_map_data (a, d_a, nbytes);
82 streams = (CUstream *) malloc (N * sizeof (void *));
84 for (i = 0; i < N; i++)
86 streams[i] = (CUstream) acc_get_cuda_stream (i);
87 if (streams[i] != NULL)
88 abort ();
90 r = cuStreamCreate (&streams[i], CU_STREAM_DEFAULT);
91 if (r != CUDA_SUCCESS)
93 fprintf (stderr, "cuStreamCreate failed: %d\n", r);
94 abort ();
97 if (!acc_set_cuda_stream (i, streams[i]))
98 abort ();
101 init_timers (1);
103 kargs[0] = (void *) &d_a;
104 kargs[1] = (void *) &dticks;
106 stream = (CUstream) acc_get_cuda_stream (N);
107 if (stream != NULL)
108 abort ();
110 r = cuStreamCreate (&stream, CU_STREAM_DEFAULT);
111 if (r != CUDA_SUCCESS)
113 fprintf (stderr, "cuStreamCreate failed: %d\n", r);
114 abort ();
117 if (!acc_set_cuda_stream (N, stream))
118 abort ();
120 start_timer (0);
122 for (i = 0; i < N; i++)
124 r = cuLaunchKernel (delay, 1, 1, 1, 1, 1, 1, 0, streams[i], kargs, 0);
125 if (r != CUDA_SUCCESS)
127 fprintf (stderr, "cuLaunchKernel failed: %d\n", r);
128 abort ();
132 acc_wait_all_async (N);
134 for (i = 0; i <= N; i++)
136 if (acc_async_test (i) != 0)
137 abort ();
140 acc_wait (N);
142 for (i = 0; i <= N; i++)
144 if (acc_async_test (i) != 1)
145 abort ();
148 atime = stop_timer (0);
150 if (atime < dtime)
152 fprintf (stderr, "actual time < delay time\n");
153 abort ();
156 start_timer (0);
158 stream = (CUstream) acc_get_cuda_stream (N + 1);
159 if (stream != NULL)
160 abort ();
162 r = cuStreamCreate (&stream, CU_STREAM_DEFAULT);
163 if (r != CUDA_SUCCESS)
165 fprintf (stderr, "cuStreamCreate failed: %d\n", r);
166 abort ();
169 if (!acc_set_cuda_stream (N + 1, stream))
170 abort ();
172 acc_wait_all_async (N + 1);
174 acc_wait (N + 1);
176 atime = stop_timer (0);
178 if (0.10 < atime)
180 fprintf (stderr, "actual time too long\n");
181 abort ();
184 start_timer (0);
186 acc_wait_all_async (N);
188 acc_wait (N);
190 atime = stop_timer (0);
192 if (0.10 < atime)
194 fprintf (stderr, "actual time too long\n");
195 abort ();
198 acc_unmap_data (a);
200 fini_timers ();
202 free (streams);
203 free (a);
204 acc_free (d_a);
206 acc_shutdown (acc_device_nvidia);
208 exit (0);
211 /* { dg-output "" } */