src/profile.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <brotensor/profile.h> | ||
| 2 | |||
| 3 | #include "profile_report.h" | ||
| 4 | |||
| 5 | #include <cstdio> | ||
| 6 | #include <cstdlib> | ||
| 7 | #include <fstream> | ||
| 8 | #include <string> | ||
| 9 | |||
| 10 | #if defined(BROTENSOR_HAS_CUDA) | ||
| 11 | #include "cuda/cuda_profile.h" | ||
| 12 | #endif | ||
| 13 | #if defined(BROTENSOR_HAS_VULKAN) | ||
| 14 | #include "vulkan/vk_profile.h" | ||
| 15 | #endif | ||
| 16 | #if defined(BROTENSOR_HAS_METAL) | ||
| 17 | #include "metal/mtl_profile.h" | ||
| 18 | #endif | ||
| 19 | |||
| 20 | namespace brotensor { | ||
| 21 | |||
| 22 | ✗ | void profile_begin() { | |
| 23 | #if defined(BROTENSOR_HAS_CUDA) | ||
| 24 | detail::cuda::prof::session_begin(); | ||
| 25 | #endif | ||
| 26 | #if defined(BROTENSOR_HAS_VULKAN) | ||
| 27 | detail::vulkan::prof::session_begin(); | ||
| 28 | #endif | ||
| 29 | #if defined(BROTENSOR_HAS_METAL) | ||
| 30 | ✗ | detail::metal::prof::session_begin(); | |
| 31 | #endif | ||
| 32 | ✗ | } | |
| 33 | |||
| 34 | ✗ | std::string profile_end(int iterations) { | |
| 35 | ✗ | std::string out; | |
| 36 | ✗ | detail::prof::appendf(out, "brotensor profile (per iteration, %d iteration%s)\n", iterations, | |
| 37 | ✗ | iterations == 1 ? "" : "s"); | |
| 38 | ✗ | std::string body; | |
| 39 | #if defined(BROTENSOR_HAS_CUDA) | ||
| 40 | body += detail::cuda::prof::session_end(iterations); | ||
| 41 | #endif | ||
| 42 | #if defined(BROTENSOR_HAS_VULKAN) | ||
| 43 | body += detail::vulkan::prof::session_end(iterations); | ||
| 44 | #endif | ||
| 45 | #if defined(BROTENSOR_HAS_METAL) | ||
| 46 | ✗ | body += detail::metal::prof::session_end(iterations); | |
| 47 | #endif | ||
| 48 | ✗ | out += body.empty() ? std::string(" no GPU work was recorded\n") : body; | |
| 49 | ✗ | return out; | |
| 50 | ✗ | } | |
| 51 | |||
| 52 | namespace detail { | ||
| 53 | |||
| 54 | namespace { | ||
| 55 | |||
| 56 | ✗ | std::string& env_target() { | |
| 57 | ✗ | static std::string t; | |
| 58 | ✗ | return t; | |
| 59 | } | ||
| 60 | |||
| 61 | ✗ | void report_at_exit() { | |
| 62 | ✗ | const std::string report = profile_end(1); | |
| 63 | ✗ | const std::string& target = env_target(); | |
| 64 | ✗ | if (target == "1") { | |
| 65 | ✗ | std::fputs(report.c_str(), stderr); | |
| 66 | ✗ | return; | |
| 67 | } | ||
| 68 | ✗ | std::ofstream f(target, std::ios::binary); | |
| 69 | ✗ | if (f) { | |
| 70 | ✗ | f << report; | |
| 71 | ✗ | } else { | |
| 72 | ✗ | std::fputs(report.c_str(), stderr); | |
| 73 | } | ||
| 74 | ✗ | } | |
| 75 | |||
| 76 | } // namespace | ||
| 77 | |||
| 78 | // BROTENSOR_PROFILE=1 profiles the whole process from init() and prints the | ||
| 79 | // report to stderr at exit; any other value (but 0) is a file to write it to. | ||
| 80 | 172 | void profile_from_env() { | |
| 81 | 172 | const char* v = std::getenv("BROTENSOR_PROFILE"); | |
| 82 | 172 | if (!v || !*v || std::string(v) == "0") return; | |
| 83 | ✗ | env_target() = v; | |
| 84 | ✗ | profile_begin(); | |
| 85 | ✗ | std::atexit(report_at_exit); | |
| 86 | 172 | } | |
| 87 | |||
| 88 | } // namespace detail | ||
| 89 | |||
| 90 | } // namespace brotensor | ||
| 91 |