From 3559579d2a32e818ddc8be55445be768f9a521df Mon Sep 17 00:00:00 2001
From: Stephan Seitz <stephan.seitz@fau.de>
Date: Sun, 4 Oct 2020 14:58:01 +0200
Subject: [PATCH] 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.
---
 python/pystencils_walberla/templates/Boundary.tmpl.h | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/python/pystencils_walberla/templates/Boundary.tmpl.h b/python/pystencils_walberla/templates/Boundary.tmpl.h
index 46c8b69d2..c856adcea 100644
--- a/python/pystencils_walberla/templates/Boundary.tmpl.h
+++ b/python/pystencils_walberla/templates/Boundary.tmpl.h
@@ -29,6 +29,7 @@
 #include "domain_decomposition/IBlock.h"
 #include "blockforest/StructuredBlockForest.h"
 #include "field/FlagField.h"
+#include "core/debug/Debug.h"
 
 #include <set>
 #include <vector>
@@ -83,12 +84,15 @@ public:
         void syncGPU()
         {
             {% if target == 'gpu' -%}
+            for( auto & gpuVec: gpuVectors_)
+                cudaFree( gpuVec );
             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 & cpuVec = cpuVectors_[i];
-                cudaFree( gpuVec );
                 cudaMalloc( &gpuVec, sizeof({{StructName}}) * cpuVec.size() );
                 cudaMemcpy( gpuVec, &cpuVec[0], sizeof({{StructName}}) * cpuVec.size(), cudaMemcpyHostToDevice );
             }
-- 
GitLab