HPCToolkit
gpu_ctxt_actions.c
Go to the documentation of this file.
1 #include <messages/messages.h>
2 #include "gpu_ctxt_actions.h"
3 
4 // ******** CUDA API *************
5 #include <cuda.h>
6 #include <cuda_runtime.h>
7 // *******************************************************
8 
9 // ****************** utility macros *********************
10 #define Cuda_RTcall(fn) cudaRuntimeFunctionPointer[fn ## Enum].fn ## Real
11 #define Cuda_Dcall(fn) cuDriverFunctionPointer[fn ## Enum].fn ## Real
12 // *******************************************************
13 
14 // ****************Atomic ops*****************************
15 #include <lib/prof-lean/atomic.h>
16 // *******************************************************
17 
18 
19 // ********** (table of) cuda API functions **************
20 #include "gpu_blame-cuda-runtime-header.h"
21 #include "gpu_blame-cuda-driver-header.h"
22 // *******************************************************
23 
24 // keep count of # of contexts
25 // (If more than 1 simultaneous context is created, that is bad)
26 static uint64_t ncontexts = 0L;
27 
28 // reset context counter to 0
29 void
31 {
32  fetch_and_store(&ncontexts, 0);
33 }
34 
35 uint64_t
37 {
38  atomic_add_i64(&ncontexts, 1L);
39  return ncontexts;
40 }
41 
42 uint64_t
44 {
45  atomic_add_i64(&ncontexts, -1L);
46  return ncontexts;
47 }
48 
49 
50 struct cuda_ctxt_t {
51  CUcontext ctxt;
52 } local_ctxt;
53 
56 {
57  if (! Cuda_Dcall(cuCtxGetCurrent)) {
58  EMSG("??? Called cuCtxGetCurrent, but fn ptr not set ???");
59  return NULL;
60  }
61  Cuda_Dcall(cuCtxGetCurrent)(&local_ctxt.ctxt);
62  return &local_ctxt;
63 }
64 
65 void
67 {
68  if (! Cuda_Dcall(cuCtxSetCurrent)) {
69  EMSG("??? Called cuCtxSetCurrent, but fn ptr not set ???");
70  return;
71  }
72  Cuda_Dcall(cuCtxSetCurrent)(ctxt->ctxt);
73  local_ctxt = *ctxt;
74 }
75 
76 void*
78 {
79  return (void*) (ctxt->ctxt);
80 }
cuda_ctxt_t * cuda_capture_ctxt(void)
struct cuda_ctxt_t local_ctxt
void * cuda_get_handle(cuda_ctxt_t *ctxt)
#define Cuda_Dcall(fn)
#define EMSG
Definition: messages.h:70
CUcontext ctxt
uint64_t cuda_ncontexts_decr(void)
static uint64_t ncontexts
void cuda_ncontexts_reset(void)
#define NULL
Definition: ElfHelper.cpp:85
uint64_t cuda_ncontexts_incr(void)
void cuda_set_ctxt(cuda_ctxt_t *ctxt)