diff --git a/CMakeLists.txt b/CMakeLists.txt index 253a305f..b8364946 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -144,12 +144,6 @@ if(APPLE) # set(CMAKE_SHARED_LIBRARY_SUFFIX ".so") endif(APPLE) -####################################################################### -if(NOT INTEL_MIC AND NOT WIN32 AND NOT APPLE) - find_package(OpenMP) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}") -endif(NOT INTEL_MIC AND NOT WIN32 AND NOT APPLE) - ####################################################################### find_package(ZLIB REQUIRED) include_directories(${ZLIB_INCLUDE_DIRS}) diff --git a/libsrc/general/optmem.cpp b/libsrc/general/optmem.cpp index 7621f22c..73ca693b 100644 --- a/libsrc/general/optmem.cpp +++ b/libsrc/general/optmem.cpp @@ -14,6 +14,7 @@ namespace netgen { + static mutex block_allocator_mutex; BlockAllocator :: BlockAllocator (unsigned asize, unsigned ablocks) : bablocks (0) @@ -36,8 +37,8 @@ namespace netgen void * BlockAllocator :: Alloc () { void * p; -#pragma omp critical (BlockAllocator) { + lock_guard guard(block_allocator_mutex); // return new char[size]; if (!freelist) { @@ -60,8 +61,8 @@ namespace netgen void BlockAllocator :: Free (void * p) { -#pragma omp critical (BlockAllocator) { + lock_guard guard(block_allocator_mutex); if (bablocks.Size()) { *(void**)p = freelist; diff --git a/libsrc/general/parthreads.hpp b/libsrc/general/parthreads.hpp index c1037909..9b676b10 100644 --- a/libsrc/general/parthreads.hpp +++ b/libsrc/general/parthreads.hpp @@ -187,6 +187,27 @@ public: #endif +// Simple ParallelFor function to replace OpenMP +template +void ParallelFor( int first, int next, const TFunc & f ) +{ + int nthreads = thread::hardware_concurrency(); + thread * threads = new thread[nthreads]; + for (int i=0; i #include #include +#include +#include #include @@ -35,9 +37,6 @@ #include // for usleep (only for parallel) #endif -#ifdef _OPENMP -#include -#endif /* diff --git a/libsrc/interface/nginterface.cpp b/libsrc/interface/nginterface.cpp index 3a138633..af39eab8 100644 --- a/libsrc/interface/nginterface.cpp +++ b/libsrc/interface/nginterface.cpp @@ -2363,8 +2363,6 @@ void LinkFunction () void Ng_TclCmd(string cmd) { -#pragma omp critical(tcltodo) - { - *(multithread.tcl_todo) += cmd; - } + lock_guard guard(tcl_todo_mutex); + *(multithread.tcl_todo) += cmd; } diff --git a/libsrc/meshing/global.cpp b/libsrc/meshing/global.cpp index b94876c2..f01a0ab8 100644 --- a/libsrc/meshing/global.cpp +++ b/libsrc/meshing/global.cpp @@ -68,6 +68,8 @@ namespace netgen Array tets_in_qualclass; + mutex tcl_todo_mutex; + int h_argc = 0; char ** h_argv = NULL; diff --git a/libsrc/meshing/global.hpp b/libsrc/meshing/global.hpp index fe421965..776fd93a 100644 --- a/libsrc/meshing/global.hpp +++ b/libsrc/meshing/global.hpp @@ -29,6 +29,8 @@ namespace netgen DLL_HEADER extern Array tets_in_qualclass; + DLL_HEADER extern mutex tcl_todo_mutex; + class multithreadt { public: diff --git a/libsrc/meshing/meshclass.cpp b/libsrc/meshing/meshclass.cpp index c4326b35..42a2c675 100644 --- a/libsrc/meshing/meshclass.cpp +++ b/libsrc/meshing/meshclass.cpp @@ -4,6 +4,8 @@ namespace netgen { + static mutex buildsearchtree_mutex; + Mesh :: Mesh () : surfarea(*this) { @@ -4101,8 +4103,8 @@ namespace netgen { if (elementsearchtreets == GetTimeStamp()) return; -#pragma omp critical (buildsearchtree) { + std::lock_guard guard(buildsearchtree_mutex); if (elementsearchtreets != GetTimeStamp()) { NgLock lock(mutex); diff --git a/libsrc/stlgeom/stlgeom.cpp b/libsrc/stlgeom/stlgeom.cpp index 78d0058a..bdff6cb5 100644 --- a/libsrc/stlgeom/stlgeom.cpp +++ b/libsrc/stlgeom/stlgeom.cpp @@ -2138,13 +2138,13 @@ int STLGeometry :: CheckGeometryOverlapping() } -#pragma omp parallel { Array inters; + mutex inters_mutex; -#pragma omp for - for (int i = 1; i <= GetNT(); i++) - { + ParallelFor( 1, GetNT()+1, [&] (int first, int next) + { + for (int i=first; i tpmin = tri.box.PMin(); @@ -2176,7 +2176,7 @@ int STLGeometry :: CheckGeometryOverlapping() if (IntersectTriangleTriangle (&trip1[0], &trip2[0])) { -#pragma omp critical + lock_guard guard(inters_mutex); { oltrigs++; PrintMessage(5,"Intersecting Triangles: trig ",i," with ",inters.Get(j),"!"); @@ -2186,6 +2186,7 @@ int STLGeometry :: CheckGeometryOverlapping() } } } + }); } PrintMessage(3,"Check overlapping geometry ... ", oltrigs, " triangles overlap"); return oltrigs; diff --git a/libsrc/visualization/vssolution.cpp b/libsrc/visualization/vssolution.cpp index 39919dfa..8a79a430 100644 --- a/libsrc/visualization/vssolution.cpp +++ b/libsrc/visualization/vssolution.cpp @@ -2474,27 +2474,36 @@ namespace netgen NgProfiler::RegionTimer reg1 (timer1); int ne = mesh->GetNE(); - double hminv = numeric_limits::max(); - double hmaxv = -numeric_limits::max(); - bool hhasit = false; -#if defined _OPENMP && _OPENMP >= 201107 -#pragma omp parallel for reduction (max : hmaxv) reduction (min : hminv) reduction (|| : hhasit) -#endif - for (int i = 0; i < ne; i++) - { - double val; - bool considerElem = GetValue (sol, i, 0.333, 0.333, 0.333, comp, val); - if (considerElem) - { - if (val > hmaxv) hmaxv = val; - if (val < hminv) hminv = val; - hhasit = true; - } - } - minv = min(minv, hminv); - maxv = max(maxv, hmaxv); - hasit |= hhasit; + mutex min_mutex; + mutex max_mutex; + + ParallelFor(0, ne, [&] (int first, int next) + { + double minv_local = numeric_limits::max(); + double maxv_local = -numeric_limits::max(); + for (int i=first; i maxv_local) maxv_local = val; + if (val < minv_local) minv_local = val; + hasit = true; + } + } + if(minv_local < minv) + { + lock_guard guard(min_mutex); + minv = minv_local; + } + if(maxv_local > maxv) + { + lock_guard guard(max_mutex); + maxv = maxv_local; + } + }); } if (sol->draw_surface) diff --git a/ng/ngpkg.cpp b/ng/ngpkg.cpp index 0c5b8563..a62775d9 100644 --- a/ng/ngpkg.cpp +++ b/ng/ngpkg.cpp @@ -710,8 +710,8 @@ namespace netgen strcat (lstring, " 0"); Tcl_SetVar (interp, "::status_tetqualclasses", lstring, 0); -#pragma omp critical(tcltodo) { + lock_guard guard(tcl_todo_mutex); if (multithread.tcl_todo->length()) { Tcl_Eval (interp, multithread.tcl_todo->c_str());