[mlpack-git] master: Transforms omp loop to reduction (2fee61e)

gitdub at mlpack.org gitdub at mlpack.org
Fri Jul 8 09:23:24 EDT 2016


Repository : https://github.com/mlpack/mlpack
On branch  : master
Link       : https://github.com/mlpack/mlpack/compare/34cf8d94f79c9a72ff4199676033b060cd039fcd...425324bf7fb7c86c85d10a909d8a59d4f69b7164

>---------------------------------------------------------------

commit 2fee61e28ceb1f1cd07396e6683158debe5c8bff
Author: Yannis Mentekidis <mentekid at gmail.com>
Date:   Fri Jul 8 14:23:24 2016 +0100

    Transforms omp loop to reduction


>---------------------------------------------------------------

2fee61e28ceb1f1cd07396e6683158debe5c8bff
 CMakeLists.txt                             |  3 ++
 src/mlpack/methods/lsh/lsh_search_impl.hpp | 44 +++++++++++++++++-------------
 2 files changed, 28 insertions(+), 19 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 02e9803..2ca0ace 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -238,10 +238,13 @@ add_definitions(-DBOOST_TEST_DYN_LINK)
 # If OpenMP is found, define HAS_OPENMP to be 1. Otherwise define it to be 0.
 find_package(OpenMP)
 if (OPENMP_FOUND)
+  add_definitions(-DHAS_OPENMP)
+  set(HAS_OPENMP 1)
   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
 else ()
   add_definitions(-DHAS_OPENMP)
+  set(HAS_OPENMP 0)
   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unknown-pragmas")
 endif ()
 
diff --git a/src/mlpack/methods/lsh/lsh_search_impl.hpp b/src/mlpack/methods/lsh/lsh_search_impl.hpp
index 066d5c0..77d4794 100644
--- a/src/mlpack/methods/lsh/lsh_search_impl.hpp
+++ b/src/mlpack/methods/lsh/lsh_search_impl.hpp
@@ -822,16 +822,20 @@ void LSHSearch<SortPolicy>::Search(const arma::mat& querySet,
   Timer::Start("computing_neighbors");
 
   // Parallelization to process more than one query at a time.
-  #pragma omp parallel for \
-    shared(avgIndicesReturned, resultingNeighbors, distances) \
-    schedule(dynamic)
 #ifdef _WIN32
   // Tiny workaround: Visual Studio only implements OpenMP 2.0, which doesn't
   // support unsigned loop variables. If we're building for Visual Studio, use
   // the intmax_t type instead.
-  intmax_t querySetSize = (intmax_t) querySet.n_cols;
-  for (intmax_t i = 0; i < querySetSize; ++i)
+  #pragma omp parallel for \
+    shared(resultingNeighbors, distances) \
+    schedule(dynamic)\
+    reduction(+:avgIndicesReturned)
+  for (intmax_t i = 0; i < (intmax_t) querySet.n_cols; ++i)
 #else
+  #pragma omp parallel for \
+    shared(resultingNeighbors, distances) \
+    schedule(dynamic)\
+    reduction(+:avgIndicesReturned)
   for (size_t i = 0; i < querySet.n_cols; ++i)
 #endif
   {
@@ -845,8 +849,8 @@ void LSHSearch<SortPolicy>::Search(const arma::mat& querySet,
     // An informative book-keeping for the number of neighbor candidates
     // returned on average.
     // Make atomic to avoid race conditions when multiple threads are running
-    #pragma omp atomic
-    avgIndicesReturned += refIndices.n_elem;
+    // #pragma omp atomic
+    avgIndicesReturned = avgIndicesReturned + refIndices.n_elem;
 
     // Sequentially go through all the candidates and save the best 'k'
     // candidates.
@@ -897,22 +901,24 @@ Search(const size_t k,
   Timer::Start("computing_neighbors");
 
   // Parallelization to process more than one query at a time.
-  #pragma omp parallel for \
-    shared(avgIndicesReturned, resultingNeighbors, distances) \
-    schedule(dynamic)
 #ifdef _WIN32
   // Tiny workaround: Visual Studio only implements OpenMP 2.0, which doesn't
   // support unsigned loop variables. If we're building for Visual Studio, use
   // the intmax_t type instead.
-  intmax_t referenceSetSize = (intmax_t) referenceSet->n_cols;
-  for (intmax_t i = 0; i < referenceSetSize; ++i)
+  #pragma omp parallel for \
+    shared(resultingNeighbors, distances) \
+    schedule(dynamic)\
+    reduction(+:avgIndicesReturned)
+  for (intmax_t i = 0; i < (intmax_t) referenceSet->n_cols; ++i)
 #else
+  #pragma omp parallel for \
+    shared(resultingNeighbors, distances) \
+    schedule(dynamic)\
+    reduction(+:avgIndicesReturned)
   for (size_t i = 0; i < referenceSet->n_cols; ++i)
 #endif
-  // Go through every query point. Use long int because some compilers complain
-  // for openMP unsigned index variables.
-  for (size_t i = 0; i < referenceSet->n_cols; i++)
   {
+    // Go through every query point.
     // Hash every query into every hash table and eventually into the
     // 'secondHashTable' to obtain the neighbor candidates.
     arma::uvec refIndices;
@@ -920,10 +926,10 @@ Search(const size_t k,
         Teffective);
 
     // An informative book-keeping for the number of neighbor candidates
-    // returned on average. Make atomic to avoid race conditions when multiple
-    // threads are running.
-    #pragma omp atomic
-    avgIndicesReturned += refIndices.n_elem;
+    // returned on average.
+    // Make atomic to avoid race conditions when multiple threads are running.
+    // #pragma omp atomic
+    avgIndicesReturned = avgIndicesReturned + refIndices.n_elem;
 
     // Sequentially go through all the candidates and save the best 'k'
     // candidates.




More information about the mlpack-git mailing list