Skip to content

Commit

Permalink
Call cudaDeviceReset() before exiting.
Browse files Browse the repository at this point in the history
Explicitly destroys and cleans up all resources associated with the
current device in the destructor of the CUDAService.
Useful to check for memory leaks with

  cuda-memcheck --tool memcheck --leak-check full
  • Loading branch information
fwyzard committed Jul 24, 2018
1 parent 5c6ebd3 commit 2dafe91
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
7 changes: 4 additions & 3 deletions HeterogeneousCore/CUDAServices/interface/CUDAService.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#ifndef HeterogeneousCore_CUDAServices_CUDAService_h
#define HeterogeneousCore_CUDAServices_CUDAService_h

#include "FWCore/Utilities/interface/StreamID.h"

#include <utility>
#include <vector>

#include "FWCore/Utilities/interface/StreamID.h"

namespace edm {
class ParameterSet;
class ActivityRegistry;
Expand All @@ -23,6 +23,7 @@ namespace edm {
class CUDAService {
public:
CUDAService(edm::ParameterSet const& iConfig, edm::ActivityRegistry& iRegistry);
~CUDAService();

static void fillDescriptions(edm::ConfigurationDescriptions & descriptions);

Expand All @@ -49,7 +50,7 @@ class CUDAService {
private:
int numberOfDevices_ = 0;
unsigned int numberOfStreamsTotal_ = 0;
std::vector<std::pair<int, int> > computeCapabilities_;
std::vector<std::pair<int, int>> computeCapabilities_;
bool enabled_ = false;
};

Expand Down
21 changes: 14 additions & 7 deletions HeterogeneousCore/CUDAServices/src/CUDAService.cc
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
#include "HeterogeneousCore/CUDAServices/interface/CUDAService.h"
#include <cuda.h>
#include <cuda/api_wrappers.h>

#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"

#include <cuda.h>
#include <cuda/api_wrappers.h>
#include "HeterogeneousCore/CUDAServices/interface/CUDAService.h"
#include "HeterogeneousCore/CUDAUtilities/interface/cudaCheck.h"
#include "HeterogeneousCore/CUDAUtilities/interface/getCudaDrvErrorString.h"

#include <dlfcn.h>

CUDAService::CUDAService(edm::ParameterSet const& iConfig, edm::ActivityRegistry& iRegistry) {
bool configEnabled = iConfig.getUntrackedParameter<bool>("enabled");
if(!configEnabled) {
Expand Down Expand Up @@ -64,6 +62,15 @@ CUDAService::CUDAService(edm::ParameterSet const& iConfig, edm::ActivityRegistry
enabled_ = true;
}

CUDAService::~CUDAService() {
if (enabled_) {
// Explicitly destroys and cleans up all resources associated with the current device in the
// current process. Any subsequent API call to this device will reinitialize the device.
// Useful to check for memory leaks with `cuda-memcheck --tool memcheck --leak-check full`.
cudaCheck(cudaDeviceReset());
}
}

void CUDAService::fillDescriptions(edm::ConfigurationDescriptions & descriptions) {
edm::ParameterSetDescription desc;
desc.addUntracked<bool>("enabled", true);
Expand Down

0 comments on commit 2dafe91

Please sign in to comment.