From 3ad4a4a1e0a9e02a312cc6b4f8b71304f05aca43 Mon Sep 17 00:00:00 2001
From: Michael Kuron <mkuron@icp.uni-stuttgart.de>
Date: Mon, 9 Apr 2018 14:12:06 +0200
Subject: [PATCH] Fix tests failing with new OpenMP on Intel compiler

- NearestNeighborFieldInterpolator::get needs to break out of all loops, just not the current one. While the test seemed to succeed anyway, it crashed with corrupted memory during exit when compiled with Intel 17 in Release mode.
- FftTest should not use thread-unsafe RNG in parallel section. Otherwise the Intel 16 compiler crashes.
- Revert to -openmp on Intel < 16.0.3, which crashes while processing some of our OpenMP loops (e.g. BoundaryHandling::treatDirection).
---
 CMakeLists.txt                                | 21 +++++++++++--------
 .../NearestNeighborFieldInterpolator.h        |  2 +-
 tests/fft/FftTest.cpp                         |  2 +-
 3 files changed, 14 insertions(+), 11 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index a48d27e4b..0ee1e578c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1027,19 +1027,22 @@ endif()
 option ( WALBERLA_THREAD_SAFE_LOGGING "Enables/Disables thread-safe logging" ON )
 
 if ( WALBERLA_BUILD_WITH_OPENMP )
-    if ( WALBERLA_CXX_COMPILER_IS_NEC )
-      add_flag ( CMAKE_C_FLAGS   "-Popenmp" )
-      add_flag ( CMAKE_CXX_FLAGS "-Popenmp" )
+    if ( WALBERLA_CXX_COMPILER_IS_INTEL AND "${CMAKE_CXX_COMPILER_VERSION}" VERSION_LESS "16.0.3" )
+       add_flag ( CMAKE_C_FLAGS   "-openmp" )
+       add_flag ( CMAKE_CXX_FLAGS "-openmp" )
+    elseif ( WALBERLA_CXX_COMPILER_IS_NEC )
+       add_flag ( CMAKE_C_FLAGS   "-Popenmp" )
+       add_flag ( CMAKE_CXX_FLAGS "-Popenmp" )
     else()
-      find_package( OpenMP )
-      add_flag ( CMAKE_C_FLAGS   "${OpenMP_C_FLAGS}" )
-      add_flag ( CMAKE_CXX_FLAGS "${OpenMP_CXX_FLAGS}" )
-      list ( APPEND SERVICE_LIBS ${OpenMP_CXX_LIBRARIES} )
+       find_package( OpenMP )
+       add_flag ( CMAKE_C_FLAGS   "${OpenMP_C_FLAGS}" )
+       add_flag ( CMAKE_CXX_FLAGS "${OpenMP_CXX_FLAGS}" )
+       list ( APPEND SERVICE_LIBS ${OpenMP_CXX_LIBRARIES} )
     endif()
 else()
     if ( WALBERLA_CXX_COMPILER_IS_CRAY )
-      add_flag ( CMAKE_C_FLAGS   "-h noomp" )
-      add_flag ( CMAKE_CXX_FLAGS "-h noomp" )
+       add_flag ( CMAKE_C_FLAGS   "-h noomp" )
+       add_flag ( CMAKE_CXX_FLAGS "-h noomp" )
     endif()
 endif()
 ############################################################################################################################
diff --git a/src/field/interpolators/NearestNeighborFieldInterpolator.h b/src/field/interpolators/NearestNeighborFieldInterpolator.h
index 45e9110de..975c55edb 100644
--- a/src/field/interpolators/NearestNeighborFieldInterpolator.h
+++ b/src/field/interpolators/NearestNeighborFieldInterpolator.h
@@ -121,7 +121,7 @@ public:
                            *interpolationResultBegin = baseField_(curCell, f);
                            ++interpolationResultBegin;
                         }
-                        break;
+                        return;
                      }
                   }
                }
diff --git a/tests/fft/FftTest.cpp b/tests/fft/FftTest.cpp
index 58d80c486..99e934dbf 100644
--- a/tests/fft/FftTest.cpp
+++ b/tests/fft/FftTest.cpp
@@ -41,7 +41,7 @@ int main (int argc, char** argv)
    {
       Field_T *data_in = block->getData< Field_T >( originalFieldId );
       Field_T *data_out = block->getData< Field_T >( fftFieldId );
-      WALBERLA_FOR_ALL_CELLS_XYZ(data_in, {
+      WALBERLA_FOR_ALL_CELLS_XYZ_OMP(data_in, omp critical, {
          Vector3<real_t> point( real_c(x), real_c(y), real_c(z) );
          blocks->transformBlockLocalToGlobal(point, *block);
          data_in->get(x,y,z) = real_c(std::ranlux48_base(uint_c(point[0])+(uint_c(point[1])*L+uint_c(point[2]))*L)())*real_c(std::pow(2,-48));
-- 
GitLab