1
#include "caffe2/core/common_gpu.h"
2
#include "caffe2/core/operator.h"
7
#include <cuda_profiler_api.h>
11
static std::vector<std::string> kCudaProfileConfiguration = {
21
"memtransferhostmemtype",
23
"cacheconfigrequested",
24
"cacheconfigexecuted",
25
"countermodeaggregate",
31
class CudaProfileInitializeOp : public OperatorBase {
33
CudaProfileInitializeOp(const OperatorDef& operator_def, Workspace* ws)
34
: OperatorBase(operator_def, ws),
35
output_(GetSingleArgument<std::string>("output", "/tmp/output")) {
36
std::array<char, 128> buf;
37
std::string tmpl = "/tmp/cuda_profile_config.XXXXXX";
38
CAFFE_ENFORCE_LT(tmpl.size(), buf.size());
39
memcpy(buf.data(), tmpl.data(), tmpl.size());
40
auto result = mktemp(buf.data());
41
CAFFE_ENFORCE_NE(strlen(result), 0, "mktemp: ", strerror(errno));
44
// Write configuration to temporary file
46
std::ofstream ofs(config_, std::ios::out | std::ios::trunc);
47
CAFFE_ENFORCE(ofs.is_open(), "ofstream: ", ofs.rdstate());
48
for (const auto& line : kCudaProfileConfiguration) {
49
ofs << line << std::endl;
54
~CudaProfileInitializeOp() override {
55
unlink(config_.c_str());
58
bool Run(int /* unused */ /*stream_id*/ = 0) override {
59
// If this fails, check the contents of "output" for hints.
60
#if defined(CUDA_VERSION) && CUDA_VERSION < 12000
61
// cudaProfilerInitialize is no longer needed after CUDA 12:
62
// https://forums.developer.nvidia.com/t/cudaprofilerinitialize-is-deprecated-alternative/200776/3
64
cudaProfilerInitialize(config_.c_str(), output_.c_str(), cudaCSV));
74
class CudaProfileStartOp : public OperatorBase {
76
CudaProfileStartOp(const OperatorDef& operator_def, Workspace* ws)
77
: OperatorBase(operator_def, ws) {}
79
bool Run(int /* unused */ /*stream_id*/ = 0) override {
80
CUDA_ENFORCE(cudaProfilerStart());
85
class CudaProfileStopOp : public OperatorBase {
87
CudaProfileStopOp(const OperatorDef& operator_def, Workspace* ws)
88
: OperatorBase(operator_def, ws) {}
90
bool Run(int /* unused */ /*stream_id*/ = 0) override {
91
CUDA_ENFORCE(cudaProfilerStop());
96
OPERATOR_SCHEMA(CudaProfileInitialize);
97
OPERATOR_SCHEMA(CudaProfileStart);
98
OPERATOR_SCHEMA(CudaProfileStop);
100
REGISTER_CPU_OPERATOR(CudaProfileInitialize, CudaProfileInitializeOp);
101
REGISTER_CPU_OPERATOR(CudaProfileStart, CudaProfileStartOp);
102
REGISTER_CPU_OPERATOR(CudaProfileStop, CudaProfileStopOp);
104
REGISTER_CUDA_OPERATOR(CudaProfileInitialize, CudaProfileInitializeOp);
105
REGISTER_CUDA_OPERATOR(CudaProfileStart, CudaProfileStartOp);
106
REGISTER_CUDA_OPERATOR(CudaProfileStop, CudaProfileStopOp);