PAPI 7.1.0.0
Loading...
Searching...
No Matches
linux-cuda.c
Go to the documentation of this file.
1
21#include <papi.h>
22#include <papi_internal.h>
23#include <papi_vector.h>
24
25#include <string.h>
26
27#include "cupti_dispatch.h"
28#include "lcuda_debug.h"
29
31
33
34static int cuda_init_component(int cidx);
35static int cuda_shutdown_component(void);
38
39static int cuda_ntv_enum_events(unsigned int *event_code, int modifier);
40static int cuda_ntv_code_to_name(unsigned int event_code, char *name, int len);
41static int cuda_ntv_name_to_code(const char *name, unsigned int *event_code);
42static int cuda_ntv_code_to_descr(unsigned int event_code, char *descr, int len);
43
45static int cuda_set_domain(hwd_control_state_t * ctrl, int domain);
47 NativeInfo_t *ntv_info,
48 int ntv_count, hwd_context_t *ctx);
49
53static int cuda_read(hwd_context_t *ctx, hwd_control_state_t *ctl, long long **val, int flags);
55static int cuda_init_private(void);
56
57#define PAPI_CUDA_MPX_COUNTERS 512
58#define PAPI_CUDA_MAX_COUNTERS 30
59
60typedef struct cuda_ctl {
67
69 .cmp_info = {
70 .name = "cuda",
71 .short_name = "cuda",
72 .version = "0.1",
73 .description = "CUDA profiling via NVIDIA CuPTI interfaces",
74 .num_mpx_cntrs = PAPI_CUDA_MPX_COUNTERS,
75 .num_cntrs = PAPI_CUDA_MAX_COUNTERS,
76 .default_domain = PAPI_DOM_USER,
77 .default_granularity = PAPI_GRN_THR,
78 .available_granularities = PAPI_GRN_THR,
79 .hardware_intr_sig = PAPI_INT_SIGNAL,
80 /* component specific cmp_info initializations */
81 .fast_real_timer = 0,
82 .fast_virtual_timer = 0,
83 .attach = 0,
84 .attach_must_ptrace = 0,
85 .available_domains = PAPI_DOM_USER | PAPI_DOM_KERNEL,
86 .initialized = 0,
87 },
88 .size = {
89 .context = 0,
90 .control_state = sizeof(cuda_ctl_t),
91 },
92 .init_component = cuda_init_component,
93 .shutdown_component = cuda_shutdown_component,
94
95 .init_thread = cuda_init_thread,
96 .shutdown_thread = cuda_shutdown_thread,
97
98 .ntv_enum_events = cuda_ntv_enum_events,
99 .ntv_code_to_name = cuda_ntv_code_to_name,
100 .ntv_name_to_code = cuda_ntv_name_to_code,
101 .ntv_code_to_descr = cuda_ntv_code_to_descr,
102
103 .init_control_state = cuda_init_control_state,
104 .set_domain = cuda_set_domain,
105 .update_control_state = cuda_update_control_state,
106 .cleanup_eventset = cuda_cleanup_eventset,
107
108 .start = cuda_start,
109 .stop = cuda_stop,
110 .read = cuda_read,
111 .reset = cuda_reset,
112};
113
115{
116 COMPDBG("Entering with component idx: %d\n", cidx);
117
121
125 "Not initialized. Access component events to initialize it.");
126 return PAPI_EDELAY_INIT;
127}
128
130{
131 COMPDBG("Entering.\n");
133
136 return PAPI_OK;
137 }
138
140
141 return cuptid_shutdown();
142}
143
144static int cuda_init_private(void)
145{
146 int papi_errno = PAPI_OK;
147 const char *disabled_reason;
148 COMPDBG("Entering.\n");
149
151 if (papi_errno != PAPI_OK) {
152 goto fn_exit;
153 }
154
155 papi_errno = cuptid_init();
156 if (papi_errno != PAPI_OK) {
157 cuptid_disabled_reason_get(&disabled_reason);
158 sprintf(_cuda_vector.cmp_info.disabled_reason, disabled_reason);
159 _cuda_vector.cmp_info.disabled = papi_errno;
160 goto fn_exit;
161 }
162
165
166fn_exit:
167 return papi_errno;
168}
169
170static int check_n_initialize(void)
171{
173 int papi_errno = PAPI_OK;
176 ) {
177 papi_errno = cuda_init_private();
178 }
179
181 return papi_errno;
182}
183
184static int cuda_ntv_enum_events(unsigned int *event_code, int modifier)
185{
186 int papi_errno = check_n_initialize();
187 if (papi_errno != PAPI_OK) {
188 goto fn_exit;
189 }
190
192 LOCKDBG("Locked COMPONENT_LOCK to enumerate all events.\n");
195 LOCKDBG("Unlocked COMPONENT_LOCK.\n");
196 if (papi_errno != PAPI_OK) {
197 goto fn_exit;
198 }
199
201 switch (modifier) {
202 case PAPI_ENUM_FIRST:
203 *event_code = 0;
204 papi_errno = PAPI_OK;
205 break;
206 case PAPI_ENUM_EVENTS:
207 if (global_event_names->count == 0) {
208 papi_errno = PAPI_ENOEVNT;
209 } else if (*event_code < global_event_names->count - 1) {
210 *event_code = *event_code + 1;
211 papi_errno = PAPI_OK;
212 } else {
213 papi_errno = PAPI_ENOEVNT;
214 }
215 break;
216 default:
217 papi_errno = PAPI_EINVAL;
218 }
219fn_exit:
220 return papi_errno;
221}
222
223static int cuda_ntv_name_to_code(const char *name, unsigned int *event_code)
224{
225 int papi_errno = check_n_initialize();
226 if (papi_errno != PAPI_OK) {
227 goto fn_exit;
228 }
229 ntv_event_t evt_rec;
231 if (papi_errno == PAPI_OK) {
232 *event_code = evt_rec->evt_code;
233 }
234 else {
236 *event_code = global_event_names->count;
239 }
240fn_exit:
241 return papi_errno;
242}
243
244static int cuda_ntv_code_to_name(unsigned int event_code, char *name, int len)
245{
246 int papi_errno = check_n_initialize();
247 if (papi_errno != PAPI_OK) {
248 return papi_errno;
249 }
250 ntv_event_t evt_rec;
251 papi_errno = cuptid_event_table_get_item(global_event_names, event_code, &evt_rec);
252 if (papi_errno != PAPI_OK) {
253 return PAPI_ENOEVNT;
254 }
255 strncpy(name, evt_rec->name, len);
256 return PAPI_OK;
257}
258
259static int cuda_ntv_code_to_descr(unsigned int event_code, char *descr, int __attribute__((unused)) len)
260{
262 int papi_errno;
263 papi_errno = check_n_initialize();
264 if (papi_errno != PAPI_OK) {
265 goto fn_exit;
266 }
267
271 if (papi_errno != PAPI_OK) {
272 goto fn_exit;
273 }
274
275 papi_errno = cuda_ntv_code_to_name(event_code, evt_name, PAPI_2MAX_STR_LEN);
276 if (papi_errno != PAPI_OK) {
277 goto fn_exit;
278 }
280fn_exit:
281 return papi_errno;
282}
283
285{
286 return PAPI_OK;
287}
288
290{
291 return PAPI_OK;
292}
293
295{
296 COMPDBG("Entering.\n");
297 return PAPI_OK;
298}
299
300static int cuda_set_domain(hwd_control_state_t __attribute__((unused)) *ctrl, int domain)
301{
302 COMPDBG("Entering\n");
303 if((PAPI_DOM_USER & domain) || (PAPI_DOM_KERNEL & domain) || (PAPI_DOM_OTHER & domain) || (PAPI_DOM_ALL & domain))
304 return (PAPI_OK);
305 else
306 return (PAPI_EINVAL);
307}
308
310 NativeInfo_t *ntv_info,
311 int ntv_count, __attribute__((unused)) hwd_context_t *ctx
312) {
313 COMPDBG("Entering with events_count %d.\n", ntv_count);
314 int i, papi_errno;
315 papi_errno = check_n_initialize();
316 if (papi_errno != PAPI_OK) {
317 return papi_errno;
318 }
319 if (ntv_count == 0) {
320 return PAPI_OK;
321 }
322
323 cuda_ctl_t *control = (cuda_ctl_t *) ctl;
324 if (control->info == NULL) {
325 papi_errno = cuptid_thread_info_create(&(control->info));
326 if (papi_errno != PAPI_OK) {
327 goto fn_exit;
328 }
329 }
330 control->events_count = ntv_count;
331
332 if (ntv_count > PAPI_CUDA_MAX_COUNTERS) {
333 ERRDBG("Too many events added.\n");
334 papi_errno = PAPI_ECMP;
335 goto fn_exit;
336 }
337 for (i=0; i<ntv_count; i++) {
338 control->events_id[i] = ntv_info[i].ni_event;
339 ntv_info[i].ni_position = i;
340 }
341
342 void *tmp_context = NULL;
343 ntv_event_table_t select_names;
344 papi_errno = cuptid_event_table_select_by_idx(global_event_names, control->events_count, control->events_id, &select_names);
345 if (papi_errno != PAPI_OK) {
346 goto fn_exit;
347 }
348 papi_errno = cuptid_control_create(select_names, control->info, &tmp_context);
349 if (papi_errno != PAPI_OK) {
350 cuptid_control_destroy(&tmp_context);
351 goto fn_exit;
352 }
353 papi_errno = cuptid_control_destroy(&tmp_context);
354
355fn_exit:
356 cuptid_event_table_destroy(&select_names);
357 return papi_errno;
358}
359
361{
362 COMPDBG("Entering.\n");
363 cuda_ctl_t *control = (cuda_ctl_t *) ctl;
364 int papi_errno = PAPI_OK;
365 if (control->cupti_ctl) {
366 papi_errno += cuptid_control_destroy(&(control->cupti_ctl));
367 }
368 if (control->info) {
369 papi_errno += cuptid_thread_info_destroy(&(control->info));
370 }
371 if (papi_errno != PAPI_OK) {
372 return PAPI_ECMP;
373 }
374 return PAPI_OK;
375}
376
378{
379 COMPDBG("Entering.\n");
380 int papi_errno, i;
381
382 cuda_ctl_t *control = (cuda_ctl_t *) ctl;
383 for (i = 0; i < control->events_count; i++) {
384 control->values[i] = 0;
385 }
386 ntv_event_table_t select_names;
387 papi_errno = cuptid_event_table_select_by_idx(global_event_names, control->events_count, control->events_id, &select_names);
388 if (papi_errno != PAPI_OK) {
389 goto fn_exit;
390 }
391 papi_errno = cuptid_control_create(select_names, control->info, &(control->cupti_ctl));
392 if (papi_errno != PAPI_OK) {
393 goto fn_exit;
394 }
395
396 papi_errno = cuptid_control_start( control->cupti_ctl );
397
398fn_exit:
399 cuptid_event_table_destroy(&select_names);
400 return papi_errno;
401}
402
404{
405 COMPDBG("Entering.\n");
406 cuda_ctl_t *control = (cuda_ctl_t *) ctl;
407 int papi_errno;
408 papi_errno = cuptid_control_stop( control->cupti_ctl );
409 if (papi_errno != PAPI_OK) {
410 goto fn_exit;
411 }
412 papi_errno = cuptid_control_destroy( &(control->cupti_ctl) );
413fn_exit:
414 return papi_errno;
415}
416
417static int cuda_read(hwd_context_t __attribute__((unused)) *ctx, hwd_control_state_t *ctl, long long **val, int __attribute__((unused)) flags)
418{
419 COMPDBG("Entering.\n");
420 cuda_ctl_t *control = (cuda_ctl_t *) ctl;
421 int papi_errno;
422
423 papi_errno = cuptid_control_read( control->cupti_ctl, (long long *) &(control->values) );
424 if (papi_errno != PAPI_OK) {
425 goto fn_exit;
426 }
427 *val = control->values;
428
429fn_exit:
430 return papi_errno;
431}
432
434{
435 cuda_ctl_t *control = (cuda_ctl_t *) ctl;
436 int i;
437 for (i = 0; i < control->events_count; i++) {
438 control->values[i] = 0;
439 }
440 return cuptid_control_reset( control->cupti_ctl );
441}
int i
static long count
unsigned int _cuda_lock
Definition: cupti_common.c:21
int cuptid_event_table_create(ntv_event_table_t *evt_table)
int cuptid_event_enum(cuptiu_event_table_t *all_evt_names)
int cuptid_event_table_insert_record(ntv_event_table_t evt_table, const char *evt_name, unsigned int evt_code, int evt_pos)
int cuptid_control_create(ntv_event_table_t event_names, cuptid_info_t info, cuptid_ctl_t *pcupti_ctl)
int cuptid_control_start(cuptid_ctl_t cupti_ctl)
int cuptid_init(void)
int cuptid_event_table_get_item(ntv_event_table_t evt_table, unsigned int evt_idx, ntv_event_t *record)
int cuptid_control_destroy(cuptid_ctl_t *pcupti_ctl)
int cuptid_control_read(cuptid_ctl_t cupti_ctl, long long *values)
int cuptid_event_table_find_name(ntv_event_table_t evt_table, const char *evt_name, ntv_event_t *found_rec)
int cuptid_event_table_select_by_idx(ntv_event_table_t evt_table, int count, int *idcs, ntv_event_table_t *pevt_names)
void cuptid_event_table_destroy(ntv_event_table_t *evt_table)
int cuptid_shutdown(void)
int cuptid_event_name_to_descr(char *evt_name, char *descr)
int cuptid_control_reset(cuptid_ctl_t cupti_ctl)
int cuptid_thread_info_destroy(cuptid_info_t *info)
int cuptid_control_stop(cuptid_ctl_t cupti_ctl)
int cuptid_thread_info_create(cuptid_info_t *info)
void cuptid_disabled_reason_get(const char **msg)
void * cuptid_info_t
void * cuptid_ctl_t
char * evt_name(evstock *stock, int index)
Definition: eventstock.c:193
#define PAPI_DOM_USER
Definition: f90papi.h:174
#define PAPI_ENUM_EVENTS
Definition: f90papi.h:224
#define PAPI_DOM_OTHER
Definition: f90papi.h:21
#define PAPI_OK
Definition: f90papi.h:73
#define PAPI_ENUM_FIRST
Definition: f90papi.h:85
#define PAPI_NUM_LOCK
Definition: f90papi.h:80
#define PAPI_DOM_KERNEL
Definition: f90papi.h:254
#define PAPI_ENOEVNT
Definition: f90papi.h:139
#define PAPI_EDELAY_INIT
Definition: f90papi.h:271
#define PAPI_EINVAL
Definition: f90papi.h:115
#define PAPI_2MAX_STR_LEN
Definition: f90papi.h:180
#define PAPI_ECMP
Definition: f90papi.h:214
#define PAPI_GRN_THR
Definition: f90papi.h:265
#define PAPI_DOM_ALL
Definition: f90papi.h:261
static long long values[NUM_EVENTS]
Definition: init_fini.c:10
#define ERRDBG(format, args...)
Definition: lcuda_debug.h:30
#define LOCKDBG(format, args...)
Definition: lcuda_debug.h:27
#define COMPDBG(format, args...)
Definition: lcuda_debug.h:21
papi_vector_t _cuda_vector
Definition: linux-cuda.c:30
static int cuda_cleanup_eventset(hwd_control_state_t *ctl)
Definition: linux-cuda.c:360
static int check_n_initialize(void)
Definition: linux-cuda.c:170
static int cuda_read(hwd_context_t *ctx, hwd_control_state_t *ctl, long long **val, int flags)
static int cuda_init_component(int cidx)
Definition: linux-cuda.c:114
static int cuda_stop(hwd_context_t *ctx, hwd_control_state_t *ctl)
static int cuda_set_domain(hwd_control_state_t *ctrl, int domain)
static int cuda_shutdown_thread(hwd_context_t *ctx)
#define PAPI_CUDA_MAX_COUNTERS
Definition: linux-cuda.c:58
#define PAPI_CUDA_MPX_COUNTERS
Definition: linux-cuda.c:57
static int cuda_reset(hwd_context_t *ctx, hwd_control_state_t *ctl)
static int cuda_ntv_enum_events(unsigned int *event_code, int modifier)
Definition: linux-cuda.c:184
static int cuda_ntv_name_to_code(const char *name, unsigned int *event_code)
Definition: linux-cuda.c:223
static int cuda_update_control_state(hwd_control_state_t *ctl, NativeInfo_t *ntv_info, int ntv_count, hwd_context_t *ctx)
static int cuda_start(hwd_context_t *ctx, hwd_control_state_t *ctl)
ntv_event_table_t global_event_names
Definition: linux-cuda.c:32
static int cuda_init_control_state(hwd_control_state_t *ctl)
static int cuda_ntv_code_to_name(unsigned int event_code, char *name, int len)
Definition: linux-cuda.c:244
static int cuda_shutdown_component(void)
Definition: linux-cuda.c:129
static int cuda_init_private(void)
Definition: linux-cuda.c:144
static int cuda_ntv_code_to_descr(unsigned int event_code, char *descr, int len)
static int cuda_init_thread(hwd_context_t *ctx)
unsigned long AO_t __attribute__((__aligned__(4)))
Definition: m68k.h:21
Return codes and api definitions.
#define COMPONENT_LOCK
Definition: papi_internal.h:90
#define PAPI_INT_SIGNAL
Definition: papi_internal.h:52
#define NUM_INNER_LOCK
Definition: papi_lock.h:17
static int cidx
uint64_t * events_id
const char * name
Definition: rocs.c:225
char name[PAPI_MAX_STR_LEN]
Definition: papi.h:627
char disabled_reason[PAPI_HUGE_STR_LEN]
Definition: papi.h:634
int events_count
Definition: linux-cuda.c:61
cuptid_ctl_t cupti_ctl
Definition: linux-cuda.c:65
long long values[PAPI_CUDA_MAX_COUNTERS]
Definition: linux-cuda.c:63
cuptid_info_t info
Definition: linux-cuda.c:64
int events_id[PAPI_CUDA_MAX_COUNTERS]
Definition: linux-cuda.c:62
char * name
Definition: roc_profiler.c:43
PAPI_component_info_t cmp_info
Definition: papi_vector.h:20
inline_static int _papi_hwi_lock(int lck)
Definition: threads.h:69
inline_static int _papi_hwi_unlock(int lck)
Definition: threads.h:83
char * descr