| 1 |
|
| 2 |
#include <stdio.h> |
| 3 |
#include <cuda.h> |
| 4 |
|
| 5 |
static int _Tnum_timers; |
| 6 |
static CUevent *_Tstart_events, *_Tstop_events; |
| 7 |
static CUstream _Tstream; |
| 8 |
|
| 9 |
void |
| 10 |
init_timers (int ntimers) |
| 11 |
{ |
| 12 |
int i; |
| 13 |
CUresult r; |
| 14 |
|
| 15 |
_Tnum_timers = ntimers; |
| 16 |
|
| 17 |
_Tstart_events = (CUevent *) malloc (_Tnum_timers * sizeof (CUevent)); |
| 18 |
_Tstop_events = (CUevent *) malloc (_Tnum_timers * sizeof (CUevent)); |
| 19 |
|
| 20 |
r = cuStreamCreate (&_Tstream, CU_STREAM_DEFAULT); |
| 21 |
if (r != CUDA_SUCCESS) |
| 22 |
{ |
| 23 |
fprintf (stderr, "cuStreamCreate failed: %d\n", r); |
| 24 |
abort (); |
| 25 |
} |
| 26 |
|
| 27 |
for (i = 0; i < _Tnum_timers; i++) |
| 28 |
{ |
| 29 |
r = cuEventCreate (&_Tstart_events[i], CU_EVENT_DEFAULT); |
| 30 |
if (r != CUDA_SUCCESS) |
| 31 |
{ |
| 32 |
fprintf (stderr, "cuEventCreate failed: %d\n", r); |
| 33 |
abort (); |
| 34 |
} |
| 35 |
|
| 36 |
r = cuEventCreate (&_Tstop_events[i], CU_EVENT_DEFAULT); |
| 37 |
if (r != CUDA_SUCCESS) |
| 38 |
{ |
| 39 |
fprintf (stderr, "cuEventCreate failed: %d\n", r); |
| 40 |
abort (); |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
void |
| 46 |
fini_timers (void) |
| 47 |
{ |
| 48 |
int i; |
| 49 |
|
| 50 |
for (i = 0; i < _Tnum_timers; i++) |
| 51 |
{ |
| 52 |
cuEventDestroy (_Tstart_events[i]); |
| 53 |
cuEventDestroy (_Tstop_events[i]); |
| 54 |
} |
| 55 |
|
| 56 |
cuStreamDestroy (_Tstream); |
| 57 |
|
| 58 |
free (_Tstart_events); |
| 59 |
free (_Tstop_events); |
| 60 |
} |
| 61 |
|
| 62 |
void |
| 63 |
start_timer (int timer) |
| 64 |
{ |
| 65 |
CUresult r; |
| 66 |
|
| 67 |
r = cuEventRecord (_Tstart_events[timer], _Tstream); |
| 68 |
if (r != CUDA_SUCCESS) |
| 69 |
{ |
| 70 |
fprintf (stderr, "cuEventRecord failed: %d\n", r); |
| 71 |
abort (); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
float |
| 76 |
stop_timer (int timer) |
| 77 |
{ |
| 78 |
CUresult r; |
| 79 |
float etime; |
| 80 |
|
| 81 |
r = cuEventRecord (_Tstop_events[timer], _Tstream); |
| 82 |
if (r != CUDA_SUCCESS) |
| 83 |
{ |
| 84 |
fprintf (stderr, "cuEventRecord failed: %d\n", r); |
| 85 |
abort (); |
| 86 |
} |
| 87 |
|
| 88 |
r = cuEventSynchronize (_Tstop_events[timer]); |
| 89 |
if (r != CUDA_SUCCESS) |
| 90 |
{ |
| 91 |
fprintf (stderr, "cuEventSynchronize failed: %d\n", r); |
| 92 |
abort (); |
| 93 |
} |
| 94 |
|
| 95 |
r = cuEventElapsedTime (&etime, _Tstart_events[timer], _Tstop_events[timer]); |
| 96 |
if (r != CUDA_SUCCESS) |
| 97 |
{ |
| 98 |
fprintf (stderr, "cuEventElapsedTime failed: %d\n", r); |
| 99 |
abort (); |
| 100 |
} |
| 101 |
|
| 102 |
return etime; |
| 103 |
} |