PAPI  5.7.0.0
gpu_activity.c
Go to the documentation of this file.
1 /*
2  * Author: Sangamesh Ragate
3  * Date : 18th Nov 2015
4  * ICL-UTK
5  * Description : This is the parent process that Preloads the libactivity.so
6  * and launches the cuda application for performing PC-Sampling
7  */
8 
9 
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <unistd.h>
14 
15 #include <sys/wait.h>
16 
17 #include "path.h"
18 
19 
20 
21 
22 int main(int argc,char** argv){
23 
24 
25  int pid;
26  if(argc < 2){
27  printf("please supply Cuda app to be profiled \n");
28  exit(-1);
29  }
30 
31 
32  //get CUDA device ID and sampling rate from args
33  char *samp, *device;
34  extern char* optarg;
35  extern int optind;
36  int c,tmp,index;
37 
38  //set default samp and device
39  device="0";
40  samp="5";
41  index=1;
42  while((c=getopt(argc,argv,"d:s:"))!=-1){
43  switch(c){
44  case 'd':
45  device = optarg;
46  tmp=atoi(device);
47  index = optind;
48  if(tmp < 0){
49  printf("GPU device ID not valid \n");
50  exit(-1);
51  }
52  break;
53  case 's':
54  samp = optarg;
55  tmp=atoi(samp);
56  //record index for argument forming for cuda app
57  index = optind;
58  if(tmp < 0 || tmp > 5){
59  printf("PC sampling rate not valid \n");
60  exit(-1);
61  }
62  break;
63  case '?':
64  printf("Switch not recognized by papi_sampling_cuda utility \n");
65  break;
66  }
67  }
68 
69 
70  //form the arg list for the cuda app
71  char** var;
72  var=&argv[index];
73 
74 
75  char* ld_lib;
76  char env1[1024];
77  char env2[256];
78  char env3[256];
79  int status;
80 
81  //get the shared library load path
82  strcpy(env1,"LD_LIBRARY_PATH=");
83  ld_lib=getenv("LD_LIBRARY_PATH");
84  if(ld_lib == NULL){
85  printf("Error loading CUDA shared libraries: LD_LIBRARY_PATH=NULL \n");
86  exit(-1);
87  }
88  strcat(env1,ld_lib);
89 
90  strcpy(env2,"GPU_DEVICE_ID=");
91  strcat(env2,device);
92 
93  strcpy(env3,"PC_SAMPLING_RATE=");
94  strcat(env3,samp);
95 
96  //form the env variable
97  char* env[]={env1,env2,env3,ld_prld, NULL};
98 
99  printf("\n\n\n\n");
100  printf("***************** PAPI_SAMPLING_CUDA utility **********************\n");
101  pid=fork();
102  if(pid==0){
103  execve(var[0],var,env);
104  }else if(pid==-1){
105  printf("Profile fork failed \n");
106  exit(-1);
107  }else{
108  wait(&status);
109  }
110  return 0;
111 }
int atoi()
char * getenv()
int main(int argc, char **argv)
Definition: gpu_activity.c:22
double c
Definition: multiplex.c:22
double tmp
static int pid
int wait()
int optind
long long status
Definition: iozone.c:1335
void exit()
char * optarg