OpenACC documentation updates.
[official-gcc.git] / libgomp / testsuite / libgomp.oacc-c / timer.h
blob53749da5a0dec6cf6fc81ddc80e4b8e069585107
2 #include <stdio.h>
3 #include <cuda.h>
5 static int _Tnum_timers;
6 static CUevent *_Tstart_events, *_Tstop_events;
7 static CUstream _Tstream;
9 void
10 init_timers (int ntimers)
12 int i;
13 CUresult r;
15 _Tnum_timers = ntimers;
17 _Tstart_events = (CUevent *) malloc (_Tnum_timers * sizeof (CUevent));
18 _Tstop_events = (CUevent *) malloc (_Tnum_timers * sizeof (CUevent));
20 r = cuStreamCreate (&_Tstream, CU_STREAM_DEFAULT);
21 if (r != CUDA_SUCCESS)
23 fprintf (stderr, "cuStreamCreate failed: %d\n", r);
24 abort ();
27 for (i = 0; i < _Tnum_timers; i++)
29 r = cuEventCreate (&_Tstart_events[i], CU_EVENT_DEFAULT);
30 if (r != CUDA_SUCCESS)
32 fprintf (stderr, "cuEventCreate failed: %d\n", r);
33 abort ();
36 r = cuEventCreate (&_Tstop_events[i], CU_EVENT_DEFAULT);
37 if (r != CUDA_SUCCESS)
39 fprintf (stderr, "cuEventCreate failed: %d\n", r);
40 abort ();
45 void
46 fini_timers (void)
48 int i;
50 for (i = 0; i < _Tnum_timers; i++)
52 cuEventDestroy (_Tstart_events[i]);
53 cuEventDestroy (_Tstop_events[i]);
56 cuStreamDestroy (_Tstream);
58 free (_Tstart_events);
59 free (_Tstop_events);
62 void
63 start_timer (int timer)
65 CUresult r;
67 r = cuEventRecord (_Tstart_events[timer], _Tstream);
68 if (r != CUDA_SUCCESS)
70 fprintf (stderr, "cuEventRecord failed: %d\n", r);
71 abort ();
75 float
76 stop_timer (int timer)
78 CUresult r;
79 float etime;
81 r = cuEventRecord (_Tstop_events[timer], _Tstream);
82 if (r != CUDA_SUCCESS)
84 fprintf (stderr, "cuEventRecord failed: %d\n", r);
85 abort ();
88 r = cuEventSynchronize (_Tstop_events[timer]);
89 if (r != CUDA_SUCCESS)
91 fprintf (stderr, "cuEventSynchronize failed: %d\n", r);
92 abort ();
95 r = cuEventElapsedTime (&etime, _Tstart_events[timer], _Tstop_events[timer]);
96 if (r != CUDA_SUCCESS)
98 fprintf (stderr, "cuEventElapsedTime failed: %d\n", r);
99 abort ();
102 return etime;