1 /** 2 * Copyright 2020-2021 Huawei Technologies Co., Ltd 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef MINDSPORE_CCSRC_PROFILER_DEVICE_GPU_GPU_DATA_SAVER_H 18 #define MINDSPORE_CCSRC_PROFILER_DEVICE_GPU_GPU_DATA_SAVER_H 19 #include <iostream> 20 #include <algorithm> 21 #include <unordered_map> 22 #include <vector> 23 #include <string> 24 #include <memory> 25 #include "profiler/device/gpu/gpu_profiling.h" 26 #include "profiler/device/cpu/cpu_data_saver.h" 27 #include "profiler/device/data_saver.h" 28 namespace mindspore { 29 namespace profiler { 30 namespace gpu { 31 struct ActivityData { 32 std::shared_ptr<Event> basic_info_{nullptr}; 33 std::string block_dim_; 34 std::string grid_dim_; 35 int count_{0}; 36 float total_duration_{0}; 37 float avg_duration_{0}; 38 float max_duration_{0}; 39 float min_duration_{0}; 40 std::vector<StartDuration> start_duration; 41 42 ActivityData() = default; 43 44 explicit ActivityData(std::shared_ptr<Event> data); 45 GetHeaderActivityData46 std::string GetHeader() const { 47 return "name,type,op_full_name,stream_id,block_dim,grid_dim,occurrences," 48 "total_duration(us),avg_duration(us),max_duration(us),min_duration(us)"; 49 } 50 51 friend std::ostream &operator<<(std::ostream &os, const ActivityData &event) { 52 os << "\"" << event.basic_info_->kernel_name << "\"," << event.basic_info_->kernel_type << ',' 53 << event.basic_info_->op_name << ',' << event.basic_info_->stream_id << ',' << event.block_dim_ << ',' 54 << event.grid_dim_ << ',' << event.count_ << ',' << event.total_duration_ << ',' << event.avg_duration_ << ',' 55 << event.max_duration_ << ',' << event.min_duration_; 56 return os; 57 } 58 59 ActivityData &operator+=(const ActivityData &other); 60 }; 61 62 using DeviceActivityInfos = std::unordered_map<std::string, ActivityData>; // <device_id, ActivityData> 63 using AllActivityInfos = std::unordered_map<uint32_t, DeviceActivityInfos>; // <device_id, ActivityData> 64 65 class GpuDataSaver : public DataSaver { 66 public: GpuDataSaver(ProfilingTraceInfo step_trace_op_name,const std::vector<OneStepStartEndInfo> & all_step_start_end_info)67 GpuDataSaver(ProfilingTraceInfo step_trace_op_name, const std::vector<OneStepStartEndInfo> &all_step_start_end_info) 68 : step_trace_op_name_(step_trace_op_name), all_step_start_end_info_(all_step_start_end_info) { 69 step_trace_op_name_from_graph_ = step_trace_op_name; 70 } 71 72 ~GpuDataSaver() = default; 73 74 GpuDataSaver(const GpuDataSaver &) = delete; 75 76 GpuDataSaver &operator=(const GpuDataSaver &) = delete; 77 78 void ParseEvent(const std::vector<Event> &events); 79 80 void WriteFile(std::string out_path, const BaseTime &start_time); 81 82 private: 83 void AddKernelEvent(const Event &event); 84 85 void AddKernelEventToDevice(const Event &event, DeviceActivityInfos *device_activity_infos); 86 87 void WriteActivity(const std::string &saver_base_dir); 88 89 void WriteStepTrace(const std::string &saver_base_dir); 90 91 void WriteStepTraceAsyncLaunchKernel(const std::string &saver_base_dir); 92 93 void WriteStartTime(const std::string &saver_base_dir, const BaseTime &start_time); 94 95 void CpuProfilingTimeSynchronizedToGpu(const BaseTime &start_time); 96 97 AllActivityInfos activity_infos_; 98 ProfilingTraceInfo step_trace_op_name_from_graph_; 99 ProfilingTraceInfo step_trace_op_name_; 100 const std::vector<OneStepStartEndInfo> &all_step_start_end_info_; 101 }; 102 } // namespace gpu 103 } // namespace profiler 104 } // namespace mindspore 105 106 #endif // MINDSPORE_CCSRC_PROFILER_DEVICE_GPU_GPU_DATA_SAVER_H 107