From 2878e688703c269d744b4cd610d7f25c922c242d Mon Sep 17 00:00:00 2001
From: Florian Schornbaum <florian.schornbaum@fau.de>
Date: Tue, 18 Jul 2017 22:25:59 +0200
Subject: [PATCH] Improves weighted SFC partitioning

---
 src/blockforest/loadbalancing/DynamicCurve.h  | 7 +++++--
 src/blockforest/loadbalancing/StaticCurve.cpp | 8 ++++++--
 2 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/src/blockforest/loadbalancing/DynamicCurve.h b/src/blockforest/loadbalancing/DynamicCurve.h
index 1f8dbf975..8af01a821 100644
--- a/src/blockforest/loadbalancing/DynamicCurve.h
+++ b/src/blockforest/loadbalancing/DynamicCurve.h
@@ -32,6 +32,7 @@
 #include "core/mpi/Gatherv.h"
 #include "core/mpi/MPIManager.h"
 
+#include <cmath>
 #include <map>
 #include <set>
 #include <stack>
@@ -751,9 +752,11 @@ void DynamicLevelwiseCurveBalance< PhantomData_T >::balanceWeighted( const std::
       uint_t c( uint_t(0) );
       for( uint_t p = uint_t(0); p != processes; ++p )
       {
-         const long double minWeight = totalWeight / numeric_cast< long double >( processes - p );
+         const long double pWeight = totalWeight / numeric_cast< long double >( processes - p );
          long double weight( 0 );
-         while( weight < minWeight && c < blocks.size() )
+         while( c < blocks.size() &&
+                std::abs( pWeight - weight - numeric_cast< long double >( allBlocks[ uint_c( blocks[c].first ) ][ blocks[c].second ].second ) ) <=
+                std::abs( pWeight - weight ) )
          {
             targets[ uint_c( blocks[c].first ) ][ blocks[c].second ] = pid_c(p);
             sender[p].insert( blocks[c].first );
diff --git a/src/blockforest/loadbalancing/StaticCurve.cpp b/src/blockforest/loadbalancing/StaticCurve.cpp
index 7d33d2503..9bb9c184d 100644
--- a/src/blockforest/loadbalancing/StaticCurve.cpp
+++ b/src/blockforest/loadbalancing/StaticCurve.cpp
@@ -21,6 +21,8 @@
 
 #include "StaticCurve.h"
 
+#include <cmath>
+
 
 
 namespace walberla {
@@ -119,9 +121,11 @@ uint_t StaticLevelwiseCurveBalanceWeighted::operator()( SetupBlockForest & fores
       uint_t c( uint_t(0) );
       for( uint_t p = uint_t(0); p != numberOfProcesses; ++p )
       {
-         const workload_t minWeight = totalWeight / workload_c( numberOfProcesses - p );
+         const workload_t pWeight = totalWeight / workload_c( numberOfProcesses - p );
          workload_t weight( 0 );
-         while( weight < minWeight && c < blocksOnLevel.size() )
+         while( c < blocksOnLevel.size() &&
+                std::abs( pWeight - weight - blocksOnLevel[c]->getWorkload() ) <=
+                std::abs( pWeight - weight ) )
          {
             blocksOnLevel[c]->assignTargetProcess(p);
 
-- 
GitLab