MUDA
Loading...
Searching...
No Matches
profiler.h
1#pragma once
2#include <string>
3#include <chrono>
4
5#include <cuda.h>
6#include <cuda_runtime.h>
7#include <cuda_runtime_api.h>
8#include <device_launch_parameters.h>
9
10#include <cuda_profiler_api.h>
11#include <nvtx3/nvToolsExt.h>
12#include <nvtx3/nvToolsExtCuda.h>
13
14#include <muda/check/check_cuda_errors.h>
15
16namespace muda
17{
18template <typename F>
19MUDA_HOST double profile_host(F&& f)
20{
21 using namespace std::chrono;
22 wait_device();
23 auto start = high_resolution_clock::now();
24 f();
25 wait_device();
26 auto end = high_resolution_clock::now();
27 return duration_cast<microseconds>(end - start).count() / 1000.0;
28}
29
31{
32 bool need_pop;
33
34 public:
35 Profile() MUDA_NOEXCEPT : need_pop(false)
36 {
37 checkCudaErrors(cudaProfilerStart());
38 }
39 Profile(const std::string& name) MUDA_NOEXCEPT : need_pop(true)
40 {
41 nvtxEventAttributes_t eventAttrib = {0};
42 eventAttrib.version = NVTX_VERSION;
43 eventAttrib.size = NVTX_EVENT_ATTRIB_STRUCT_SIZE;
44 eventAttrib.colorType = NVTX_COLOR_ARGB;
45 eventAttrib.color = 255;
46 eventAttrib.messageType = NVTX_MESSAGE_TYPE_ASCII;
47 eventAttrib.message.ascii = name.c_str();
48 nvtxRangePushEx(&eventAttrib);
49
50 checkCudaErrors(cudaProfilerStart());
51 }
52 ~Profile()
53 {
54 checkCudaErrors(cudaProfilerStop());
55 if(need_pop)
56 nvtxRangePop();
57 }
58};
59
61{
62 public:
63 RangeName(const std::string& name) MUDA_NOEXCEPT
64 {
65 nvtxEventAttributes_t eventAttrib = {0};
66 eventAttrib.version = NVTX_VERSION;
67 eventAttrib.size = NVTX_EVENT_ATTRIB_STRUCT_SIZE;
68 eventAttrib.colorType = NVTX_COLOR_ARGB;
69 eventAttrib.color = 255;
70 eventAttrib.messageType = NVTX_MESSAGE_TYPE_ASCII;
71 eventAttrib.message.ascii = name.c_str();
72 nvtxRangePushEx(&eventAttrib);
73 }
74
75 ~RangeName() { nvtxRangePop(); }
76};
77} // namespace muda
Definition profiler.h:31
Definition profiler.h:61