Skip to content

Commit

Permalink
memory release
Browse files Browse the repository at this point in the history
  • Loading branch information
ceccocats committed Jun 2, 2020
1 parent 0458f36 commit 62e4a3f
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 5 deletions.
5 changes: 3 additions & 2 deletions include/tkDNN/Layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Layer {
}
void setFinal() { this->final = true; }
dataDim_t input_dim, output_dim;
dnnType *dstData; //where results will be putted
dnnType *dstData = nullptr; //where results will be putted

int id = 0;
bool final; //if the layer is the final one
Expand Down Expand Up @@ -122,7 +122,7 @@ class LayerWgs : public Layer {
__half *data16_d = nullptr, *bias16_d = nullptr;
__half *bias216_h = nullptr, *bias216_d = nullptr;

__half *power16_h = nullptr;
__half *power16_h = nullptr, *power16_d = nullptr;
__half *scales16_h = nullptr, *scales16_d = nullptr;
__half *mean16_h = nullptr, *mean16_d = nullptr;
__half *variance16_h = nullptr, *variance16_d = nullptr;
Expand Down Expand Up @@ -164,6 +164,7 @@ class LayerWgs : public Layer {
if( scales16_d != nullptr) { cudaFree( scales16_d); scales16_d = nullptr; }
if( mean16_d != nullptr) { cudaFree( mean16_d); mean16_d = nullptr; }
if(variance16_d != nullptr) { cudaFree(variance16_d); variance16_d = nullptr; }
if( power16_d != nullptr) { cudaFree( power16_d); power16_d = nullptr; }
}
}
};
Expand Down
1 change: 1 addition & 0 deletions include/tkDNN/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,6 @@ void matrixMulAdd( cublasHandle_t handle, dnnType* srcData, dnnType* dstData,
dnnType* add_vector, int dim, dnnType mul);

void getMemUsage(double& vm_usage_kb, double& resident_set_kb);
void printCudaMemUsage();
void removePathAndExtension(const std::string &full_string, std::string &name);
#endif //UTILS_H
5 changes: 5 additions & 0 deletions src/Layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ Layer::~Layer() {

checkCUDNN( cudnnDestroyTensorDescriptor(srcTensorDesc) );
checkCUDNN( cudnnDestroyTensorDescriptor(dstTensorDesc) );

if(dstData != nullptr) {
cudaFree(dstData);
dstData = nullptr;
}
}

}}
6 changes: 3 additions & 3 deletions src/LayerWgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ LayerWgs::LayerWgs(Network *net, int inputs, int outputs,
variance16_h = new __half[b_size];
scales16_h = new __half[b_size];

//cudaMalloc(&power16_d, b_size*sizeof(__half));
cudaMalloc(&power16_d, b_size*sizeof(__half));
cudaMalloc(&mean16_d, b_size*sizeof(__half));
cudaMalloc(&variance16_d, b_size*sizeof(__half));
cudaMalloc(&scales16_d, b_size*sizeof(__half));
Expand All @@ -91,8 +91,8 @@ LayerWgs::LayerWgs(Network *net, int inputs, int outputs,

//init power array of ones
cudaMemcpy(tmp_d, power_h, b_size*sizeof(float), cudaMemcpyHostToDevice);
//float2half(tmp_d, power16_d, b_size);
//cudaMemcpy(power16_h, power16_d, b_size*sizeof(__half), cudaMemcpyDeviceToHost);
float2half(tmp_d, power16_d, b_size);
cudaMemcpy(power16_h, power16_d, b_size*sizeof(__half), cudaMemcpyDeviceToHost);

//mean array
cudaMemcpy(tmp_d, mean_h, b_size*sizeof(float), cudaMemcpyHostToDevice);
Expand Down
1 change: 1 addition & 0 deletions src/Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ void Network::print() {
}
printCenteredTitle("", '=', 60);
std::cout<<"\n";
printCudaMemUsage();
}
const char *Network::getNetworkRTName(const char *network_name){
networkName = network_name;
Expand Down
1 change: 1 addition & 0 deletions src/NetworkRT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ NetworkRT::NetworkRT(Network *net, const char *name) {
networkRT->markOutput(*input);

std::cout<<"Selected maxBatchSize: "<<builderRT->getMaxBatchSize()<<"\n";
printCudaMemUsage();
std::cout<<"Building tensorRT cuda engine...\n";
#if NV_TENSORRT_MAJOR >= 6
engineRT = builderRT->buildEngineWithConfig(*networkRT, *configRT);
Expand Down
6 changes: 6 additions & 0 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,12 @@ void getMemUsage(double& vm_usage_kb, double& resident_set_kb){
resident_set_kb = rss * page_size_kb;
}

void printCudaMemUsage() {
size_t free, total;
checkCuda( cudaMemGetInfo(&free, &total) );
std::cout<<"GPU free memory: "<<double(free)/1e6<<" mb.\n";
}

void removePathAndExtension(const std::string &full_string, std::string &name){
name = full_string;
std::string tmp_str = full_string;
Expand Down

0 comments on commit 62e4a3f

Please sign in to comment.