File: cuda_profile_ops.cc

package info (click to toggle)
pytorch 1.13.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 139,252 kB
  • sloc: cpp: 1,100,274; python: 706,454; ansic: 83,052; asm: 7,618; java: 3,273; sh: 2,841; javascript: 612; makefile: 323; xml: 269; ruby: 185; yacc: 144; objc: 68; lex: 44
file content (104 lines) | stat: -rw-r--r-- 2,959 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "caffe2/core/common_gpu.h"
#include "caffe2/core/operator.h"

#include <stdlib.h>
#include <string.h>

#include <cuda_profiler_api.h>

namespace caffe2 {

static std::vector<std::string> kCudaProfileConfiguration = {
    "gpustarttimestamp",
    "gpuendtimestamp",
    "gridsize3d",
    "threadblocksize",
    "dynsmemperblock",
    "stasmemperblock",
    "regperthread",
    "memtransfersize",
    "memtransferdir",
    "memtransferhostmemtype",
    "streamid",
    "cacheconfigrequested",
    "cacheconfigexecuted",
    "countermodeaggregate",
    "enableonstart 0",
    "active_warps",
    "active_cycles",
};

class CudaProfileInitializeOp : public OperatorBase {
 public:
  CudaProfileInitializeOp(const OperatorDef& operator_def, Workspace* ws)
      : OperatorBase(operator_def, ws),
        output_(GetSingleArgument<std::string>("output", "/tmp/output")) {
    std::array<char, 128> buf;
    std::string tmpl = "/tmp/cuda_profile_config.XXXXXX";
    CAFFE_ENFORCE_LT(tmpl.size(), buf.size());
    memcpy(buf.data(), tmpl.data(), tmpl.size());
    auto result = mktemp(buf.data());
    CAFFE_ENFORCE_NE(strlen(result), 0, "mktemp: ", strerror(errno));
    config_ = result;

    // Write configuration to temporary file
    {
      std::ofstream ofs(config_, std::ios::out | std::ios::trunc);
      CAFFE_ENFORCE(ofs.is_open(), "ofstream: ", ofs.rdstate());
      for (const auto& line : kCudaProfileConfiguration) {
        ofs << line << std::endl;
      }
    }
  }

  ~CudaProfileInitializeOp() override {
    unlink(config_.c_str());
  }

  bool Run(int /* unused */ /*stream_id*/ = 0) override {
    // If this fails, check the contents of "output" for hints.
    CUDA_CHECK(
        cudaProfilerInitialize(config_.c_str(), output_.c_str(), cudaCSV));
    return true;
  }

 protected:
  std::string config_;
  std::string output_;
};

class CudaProfileStartOp : public OperatorBase {
 public:
  CudaProfileStartOp(const OperatorDef& operator_def, Workspace* ws)
      : OperatorBase(operator_def, ws) {}

  bool Run(int /* unused */ /*stream_id*/ = 0) override {
    CUDA_ENFORCE(cudaProfilerStart());
    return true;
  }
};

class CudaProfileStopOp : public OperatorBase {
 public:
  CudaProfileStopOp(const OperatorDef& operator_def, Workspace* ws)
      : OperatorBase(operator_def, ws) {}

  bool Run(int /* unused */ /*stream_id*/ = 0) override {
    CUDA_ENFORCE(cudaProfilerStop());
    return true;
  }
};

OPERATOR_SCHEMA(CudaProfileInitialize);
OPERATOR_SCHEMA(CudaProfileStart);
OPERATOR_SCHEMA(CudaProfileStop);

REGISTER_CPU_OPERATOR(CudaProfileInitialize, CudaProfileInitializeOp);
REGISTER_CPU_OPERATOR(CudaProfileStart, CudaProfileStartOp);
REGISTER_CPU_OPERATOR(CudaProfileStop, CudaProfileStopOp);

REGISTER_CUDA_OPERATOR(CudaProfileInitialize, CudaProfileInitializeOp);
REGISTER_CUDA_OPERATOR(CudaProfileStart, CudaProfileStartOp);
REGISTER_CUDA_OPERATOR(CudaProfileStop, CudaProfileStopOp);

} // namespace caffe2