Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Reduce number of MIOpen handles to 1 per GPU in convolution layers #37

Open
wants to merge 1 commit into
base: hip
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions include/caffe/util/cudnn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#ifdef USE_ACCMI

#ifdef USE_MIOPEN
#include <mutex>
#include <unordered_map>

#include <miopen/miopen.h>

#include "caffe/common.hpp"
Expand Down Expand Up @@ -41,6 +44,48 @@ namespace caffe {

namespace miopen {

class miopenHandleMap {
private:
miopenHandleMap() : map_(), lock_() {}
public:
static miopenHandleMap& getInstance() {
static miopenHandleMap instance_{};
return instance_;
}

~miopenHandleMap() {
for (auto iter : map_) {
miopenDestroy(iter.second);
}
map_.clear();
}

miopenHandle_t getHandle(int device) {
miopenHandle_t ret = nullptr;
std::lock_guard<std::mutex> l(lock_);
if (map_.find(device) != map_.end()) {
ret = map_[device];
}
return ret;
}

bool setHandle(int device, miopenHandle_t handle) {
bool ret = false;
std::lock_guard<std::mutex> l(lock_);
if (map_.find(device) != map_.end()) {
LOG(FATAL) << "Duplicated MIOpen handle for device: " << device;
} else {
map_[device] = handle;
ret = true;
}
return ret;
}

private:
std::unordered_map<int, miopenHandle_t> map_;
std::mutex lock_;
};

template <typename Dtype> class dataType;
template<> class dataType<float> {
public:
Expand Down
19 changes: 13 additions & 6 deletions src/caffe/layers/cudnn_conv_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ CuDNNConvolutionLayer<Dtype>::CuDNNConvolutionLayer(const LayerParameter& param)
: ConvolutionLayer<Dtype>(param), handles_setup_(false),
fwd_algo_(), bwd_weight_algo_(), bwd_data_algo_(),
workspace_fwd_sizes_(), workspace_bwd_filter_sizes_(), workspace_bwd_data_sizes_(),
workspace() { }
workspace(), handle_(nullptr) { }

/**
* TODO(dox) explain cuDNN interface
Expand Down Expand Up @@ -57,7 +57,18 @@ void CuDNNConvolutionLayer<Dtype>::LayerSetUp(

for (int g = 0; g < this->group_ * WORKSPACE_PER_GROUP; g++) {
#ifdef USE_MIOPEN
MIOPEN_CHECK(miopenCreateWithStream(&handle_, nullptr));
int device;
HIP_CHECK(hipGetDevice(&device));

auto& hmap = caffe::miopen::miopenHandleMap::getInstance();
handle_ = hmap.getHandle(device);
if (handle_ == nullptr) {
DLOG(INFO) << "Creating MIOpen handle on device: " << device;
MIOPEN_CHECK(miopenCreateWithStream(&handle_, nullptr));
hmap.setHandle(device, handle_);
} else {
DLOG(INFO) << "Get MIOpen handle from cache on device: " << device;
}
#endif

workspace[g] = NULL;
Expand Down Expand Up @@ -435,10 +446,6 @@ CuDNNConvolutionLayer<Dtype>::~CuDNNConvolutionLayer() {
#endif
#endif

#ifdef USE_MIOPEN
miopenDestroy(handle_);
#endif

hipFree(workspaceData);
}

Expand Down