PAPI  5.7.0.0
cuda_ld_preload_example.c File Reference
Include dependency graph for cuda_ld_preload_example.c:

Go to the source code of this file.

Macros

#define _GNU_SOURCE
 
#define MAXDEVICES   5
 

Functions

int cudaSetDevice (int devnum, int n1, int n2, int n3, void *ptr1)
 
int gettimeofday (void *ptr1, void *ptr2)
 
int cudaFreeHost (void *ptr1, void *ptr2, int n1, int n2, void *ptr3)
 

Variables

int EventSet = PAPI_NULL
 
int devseen [MAXDEVICES] = {0}
 
static void * dl1
 
int(* PAPI_library_init_ptr )(int version)
 
int(* PAPI_create_eventset_ptr )(int *EventSet)
 
int(* PAPI_add_named_event_ptr )(int EventSet, char *EventName)
 
int(* PAPI_start_ptr )(int EventSet)
 
int(* PAPI_stop_ptr )(int EventSet, long long *values)
 

Macro Definition Documentation

◆ _GNU_SOURCE

#define _GNU_SOURCE

Definition at line 19 of file cuda_ld_preload_example.c.

◆ MAXDEVICES

#define MAXDEVICES   5

Definition at line 26 of file cuda_ld_preload_example.c.

Function Documentation

◆ cudaFreeHost()

int cudaFreeHost ( void *  ptr1,
void *  ptr2,
int  n1,
int  n2,
void *  ptr3 
)

Definition at line 93 of file cuda_ld_preload_example.c.

94 {
95  static int onetime = 0;
96  long long values[10];
97  int retval, devnum;
98  onetime++;
99  if ( onetime==1 ) {
100  printf("Intercept cudaFreeHost: Used to get PAPI results\n" );
102  if( retval != PAPI_OK ) fprintf( stderr, "PAPI_stop failed\n" );
103  for( devnum = 0; devnum < MAXDEVICES && devseen[devnum]==1 ; devnum++ )
104  printf( "PAPI counterValue: cuda::device:%d:%s: %12lld \n", devnum, "inst_executed", values[devnum] );
105  }
106  int (*original_function)(void *ptr1, void *ptr2, int n1, int n2, void *ptr3);
107  original_function = dlsym(RTLD_NEXT, "cudaFreeHost");
108  return (*original_function)(ptr1, ptr2, n1, n2, ptr3);
109 }
int devseen[MAXDEVICES]
#define PAPI_OK
Definition: fpapi.h:105
long long onetime
Definition: iozone.c:1290
int(* PAPI_stop_ptr)(int EventSet, long long *values)
int EventSet
int retval
Definition: zero_fork.c:53
#define MAXDEVICES
static long long values[NUM_EVENTS]
Definition: init_fini.c:10

◆ cudaSetDevice()

int cudaSetDevice ( int  devnum,
int  n1,
int  n2,
int  n3,
void *  ptr1 
)

Definition at line 38 of file cuda_ld_preload_example.c.

39 {
40  static int onetime = 0;
41  int retval, retval_cudaSetDevice;
42  //printf("cudaSetDevice wrapper %d\n", devnum);
43  if ( onetime==0 ) {
44  onetime=1;
45  // Load the papi library dynamically and read the relevant functions
46  dl1 = dlopen( "libpapi.so", RTLD_NOW | RTLD_GLOBAL );
47  if ( dl1==NULL ) printf("Intercept cudaSetDevice: Cannot load libpapi.so\n");
48  PAPI_library_init_ptr = dlsym( dl1, "PAPI_library_init" );
49  PAPI_create_eventset_ptr = dlsym( dl1, "PAPI_create_eventset" );
50  PAPI_add_named_event_ptr = dlsym( dl1, "PAPI_add_named_event" );
51  PAPI_start_ptr = dlsym( dl1, "PAPI_start" );
52  PAPI_stop_ptr = dlsym( dl1, "PAPI_stop" );
53  // Start using PAPI
54  printf("Intercept cudaSetDevice: Initializing PAPI on device %d\n", devnum);
56  if( retval != PAPI_VER_CURRENT ) fprintf( stdout, "PAPI_library_init failed\n" );
59  if( retval != PAPI_OK ) fprintf( stdout, "PAPI_create_eventset failed\n" );
60  }
61  int (*original_function)(int devnum, int n1, int n2, int n3, void *ptr1);
62  original_function = dlsym(RTLD_NEXT, "cudaSetDevice");
63  retval_cudaSetDevice = (*original_function)( devnum, n1, n2, n3, ptr1 );
64  if ( devseen[devnum]==0 ) {
65  devseen[devnum]=1;
66  char tmpEventName[120];
67  printf("Intercept cudaSetDevice: Attaching events for device on device %d\n", devnum);
68  snprintf( tmpEventName, 110, "cuda:::device:%d:%s", devnum, "inst_executed" );
69  retval = (PAPI_add_named_event_ptr)( EventSet, tmpEventName );
70  if (retval!=PAPI_OK) printf( "Could not add event %s\n", tmpEventName );
71  }
72  return retval_cudaSetDevice;
73 }
int devseen[MAXDEVICES]
#define PAPI_OK
Definition: fpapi.h:105
int(* PAPI_add_named_event_ptr)(int EventSet, char *EventName)
long long onetime
Definition: iozone.c:1290
int(* PAPI_stop_ptr)(int EventSet, long long *values)
#define PAPI_VER_CURRENT
Definition: fpapi.h:14
int EventSet
int retval
Definition: zero_fork.c:53
#define PAPI_VERSION
Definition: fpapi.h:15
#define PAPI_VERSION_MAJOR(x)
Definition: papi.h:217
#define PAPI_VERSION_REVISION(x)
Definition: papi.h:219
#define PAPI_VERSION_MINOR(x)
Definition: papi.h:218
int(* PAPI_create_eventset_ptr)(int *EventSet)
int(* PAPI_library_init_ptr)(int version)
int(* PAPI_start_ptr)(int EventSet)
static void * dl1

◆ gettimeofday()

int gettimeofday ( void *  ptr1,
void *  ptr2 
)

Definition at line 76 of file cuda_ld_preload_example.c.

77 {
78  static int onetime = 0;
79  onetime++;
80  // printf("gettimeofday onetime %d\n", onetime);
81  // Use above print statement to determine that the N-th gettime of day works
82  if ( onetime==17 ) {
83  printf("Intercept gettimeofday: Attaching PAPI_start to the %d th call to gettimeofday (this may need to be adjusted)\n", onetime);
84  int retval = (PAPI_start_ptr)( EventSet );
85  printf("Starting PAPI\n");
86  if( retval!=PAPI_OK ) fprintf( stdout, "PAPI_start failed\n" );
87  }
88  int (*original_function)(void *ptr1, void *ptr2);
89  original_function = dlsym(RTLD_NEXT, "gettimeofday");
90  return (*original_function)(ptr1, ptr2);
91 }
#define PAPI_OK
Definition: fpapi.h:105
long long onetime
Definition: iozone.c:1290
int EventSet
int retval
Definition: zero_fork.c:53
int(* PAPI_start_ptr)(int EventSet)
Here is the caller graph for this function:

Variable Documentation

◆ devseen

int devseen[MAXDEVICES] = {0}

Definition at line 28 of file cuda_ld_preload_example.c.

◆ dl1

void* dl1
static

Definition at line 30 of file cuda_ld_preload_example.c.

◆ EventSet

int EventSet = PAPI_NULL

Definition at line 27 of file cuda_ld_preload_example.c.

◆ PAPI_add_named_event_ptr

int(* PAPI_add_named_event_ptr) (int EventSet, char *EventName)

add an event by name to a PAPI event set

Definition at line 33 of file cuda_ld_preload_example.c.

◆ PAPI_create_eventset_ptr

int(* PAPI_create_eventset_ptr) (int *EventSet)

create a new empty PAPI event set

Definition at line 32 of file cuda_ld_preload_example.c.

◆ PAPI_library_init_ptr

int(* PAPI_library_init_ptr) (int version)

initialize the PAPI library

Definition at line 31 of file cuda_ld_preload_example.c.

◆ PAPI_start_ptr

int(* PAPI_start_ptr) (int EventSet)

start counting hardware events in an event set

Definition at line 34 of file cuda_ld_preload_example.c.

◆ PAPI_stop_ptr

int(* PAPI_stop_ptr) (int EventSet, long long *values)

stop counting hardware events in an event set and return current events

Definition at line 35 of file cuda_ld_preload_example.c.