Skip to content
Snippets Groups Projects
Commit 3559579d authored by Stephan Seitz's avatar Stephan Seitz
Browse files

Bugfix: call cudaFree before resize of gpuVectors_

How could this have been working until now?
We should not call `cudaFree` on uninitialized memory.

We have to free the old pointers before resize. This also prevents
memory leakage in case the vector gets shrinked.

Apparently the previous code relied on the fact that `resize` somehow
initialized the memory to null pointers or that `cudaFree` just ignores
pointers it didn't allocate.
parent ba2c6619
Branches
Tags
No related merge requests found
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
#include "domain_decomposition/IBlock.h" #include "domain_decomposition/IBlock.h"
#include "blockforest/StructuredBlockForest.h" #include "blockforest/StructuredBlockForest.h"
#include "field/FlagField.h" #include "field/FlagField.h"
#include "core/debug/Debug.h"
#include <set> #include <set>
#include <vector> #include <vector>
...@@ -83,12 +84,15 @@ public: ...@@ -83,12 +84,15 @@ public:
void syncGPU() void syncGPU()
{ {
{% if target == 'gpu' -%} {% if target == 'gpu' -%}
for( auto & gpuVec: gpuVectors_)
cudaFree( gpuVec );
gpuVectors_.resize( cpuVectors_.size() ); gpuVectors_.resize( cpuVectors_.size() );
for(size_t i=0; i < size_t(NUM_TYPES); ++i )
WALBERLA_ASSERT_EQUAL(cpuVectors_.size(), NUM_TYPES);
for(size_t i=0; i < cpuVectors_.size(); ++i )
{ {
auto & gpuVec = gpuVectors_[i]; auto & gpuVec = gpuVectors_[i];
auto & cpuVec = cpuVectors_[i]; auto & cpuVec = cpuVectors_[i];
cudaFree( gpuVec );
cudaMalloc( &gpuVec, sizeof({{StructName}}) * cpuVec.size() ); cudaMalloc( &gpuVec, sizeof({{StructName}}) * cpuVec.size() );
cudaMemcpy( gpuVec, &cpuVec[0], sizeof({{StructName}}) * cpuVec.size(), cudaMemcpyHostToDevice ); cudaMemcpy( gpuVec, &cpuVec[0], sizeof({{StructName}}) * cpuVec.size(), cudaMemcpyHostToDevice );
} }
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment