summaryrefslogtreecommitdiff
path: root/libgomp/testsuite/libgomp.oacc-c-c++-common/timer.h
blob: 53749da5a0dec6cf6fc81ddc80e4b8e069585107 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103

#include <stdio.h>
#include <cuda.h>

static int _Tnum_timers;
static CUevent *_Tstart_events, *_Tstop_events;
static CUstream _Tstream;

void
init_timers (int ntimers)
{
  int i;
  CUresult r;

  _Tnum_timers = ntimers;

  _Tstart_events = (CUevent *) malloc (_Tnum_timers * sizeof (CUevent));
  _Tstop_events = (CUevent *) malloc (_Tnum_timers * sizeof (CUevent));

  r = cuStreamCreate (&_Tstream, CU_STREAM_DEFAULT);
  if (r != CUDA_SUCCESS)
    {
      fprintf (stderr, "cuStreamCreate failed: %d\n", r);
      abort ();
    }

  for (i = 0; i < _Tnum_timers; i++)
    {
      r = cuEventCreate (&_Tstart_events[i], CU_EVENT_DEFAULT);
      if (r != CUDA_SUCCESS)
	{
	  fprintf (stderr, "cuEventCreate failed: %d\n", r);
	  abort ();
	}

      r = cuEventCreate (&_Tstop_events[i], CU_EVENT_DEFAULT);
      if (r != CUDA_SUCCESS)
	{
	  fprintf (stderr, "cuEventCreate failed: %d\n", r);
	  abort ();
	}
    }
}

void
fini_timers (void)
{
  int i;

  for (i = 0; i < _Tnum_timers; i++)
    {
      cuEventDestroy (_Tstart_events[i]);
      cuEventDestroy (_Tstop_events[i]);
    }

  cuStreamDestroy (_Tstream);

  free (_Tstart_events);
  free (_Tstop_events);
}

void
start_timer (int timer)
{
  CUresult r;

  r = cuEventRecord (_Tstart_events[timer], _Tstream);
  if (r != CUDA_SUCCESS)
    {
      fprintf (stderr, "cuEventRecord failed: %d\n", r);
      abort ();
    }
}

float
stop_timer (int timer)
{
  CUresult r;
  float etime;

  r = cuEventRecord (_Tstop_events[timer], _Tstream);
  if (r != CUDA_SUCCESS)
    {
      fprintf (stderr, "cuEventRecord failed: %d\n", r);
      abort ();
    }

  r = cuEventSynchronize (_Tstop_events[timer]);
  if (r != CUDA_SUCCESS)
    {
      fprintf (stderr, "cuEventSynchronize failed: %d\n", r);
      abort ();
    }

  r = cuEventElapsedTime (&etime, _Tstart_events[timer], _Tstop_events[timer]);
  if (r != CUDA_SUCCESS)
    {
      fprintf (stderr, "cuEventElapsedTime failed: %d\n", r);
      abort ();
    }

  return etime;
}