2 #ifndef CAFFE2_CORE_MIOPEN_WRAPPERS_H_ 3 #define CAFFE2_CORE_MIOPEN_WRAPPERS_H_ 5 #include "caffe2/core/hip/common_miopen.h" 6 #include "caffe2/core/hip/context_gpu.h" 8 #include <c10/hip/HIPGuard.h> 26 void*
get(
size_t nbytes)
31 data_ = HIPContext::New(nbytes);
34 CAFFE_ENFORCE_GE(nbytes_, nbytes);
56 explicit MIOPENState(
size_t gpu_id) : gpu_id_(gpu_id)
59 MIOPEN_ENFORCE(miopenCreate(&miopen_handle_));
60 HIP_ENFORCE(hipEventCreate(&before_));
61 HIP_ENFORCE(hipEventCreate(&after_));
62 HIP_ENFORCE(hipStreamCreate(&stream_));
63 MIOPEN_ENFORCE(miopenSetStream(miopen_handle_, stream_));
69 MIOPEN_CHECK(miopenDestroy(miopen_handle_));
70 HIP_CHECK(hipStreamDestroy(stream_));
71 HIP_CHECK(hipEventDestroy(after_));
72 HIP_CHECK(hipEventDestroy(before_));
75 miopenHandle_t& miopen_handle() {
return miopen_handle_; }
80 void execute(hipStream_t stream, F&& f)
82 HIP_ENFORCE(hipEventRecord(before_, stream));
83 HIP_ENFORCE(hipStreamWaitEvent(stream_, before_, 0));
85 HIP_ENFORCE(hipEventRecord(after_, stream_));
86 HIP_ENFORCE(hipStreamWaitEvent(stream, after_, 0));
90 miopenHandle_t miopen_handle_{
nullptr};
91 hipEvent_t before_{
nullptr};
92 hipEvent_t after_{
nullptr};
93 hipStream_t stream_{
nullptr};
124 template <
typename F>
125 void with_miopen_state(
size_t state_idx, F&& f)
127 CAFFE_ENFORCE(state_idx < CAFFE2_COMPILE_TIME_MAX_MIOPEN_STATES,
"Invalid state_idx");
128 auto& sync_state = miopen_states()[context_->device_id()][state_idx];
130 HIPGuard dg(context_->device_id());
136 std::lock_guard<std::mutex> g(sync_state.mutex);
137 if(!sync_state.state.get())
139 sync_state.state.reset(
new MIOPENState(context_->device_id()));
141 CHECK_NOTNULL(sync_state.state.get())->execute(context_->hip_stream(), f);
146 HIPContext* context_;
148 static constexpr
size_t CAFFE2_COMPILE_TIME_MAX_MIOPEN_STATES = 4;
153 std::unique_ptr<MIOPENState> state;
156 using PerGPUMIOPENStates = std::array<
157 std::array<SyncedMIOPENState, CAFFE2_COMPILE_TIME_MAX_MIOPEN_STATES>,
158 C10_COMPILE_TIME_MAX_GPUS>;
159 static PerGPUMIOPENStates& miopen_states();
MIOPENWorkspace is a wrapper around a raw cuda pointer that holds the miopen scratch space...
miopenHandle_t inline_miopen_handle()
Returns the inline miopen handle that executes on the current thread's hip_stream.
MIOPENWrapper is a class that wraps the miopen handles and miopen workspaces.
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
MIOPENWrapper(HIPContext *context)
Creates a miopen wrapper associated with a HIPContext object.