From b7be9aedc7e6d60d58a7ac9823f557927a7f3c5c Mon Sep 17 00:00:00 2001
From: Russell Taylor <taylorrj@ornl.gov>
Date: Thu, 6 May 2010 14:57:46 +0000
Subject: [PATCH] Make UnaryOperation multi-threaded. Also unlocked reading of
 ParameterMap cache and fix bug in Cache updating code. Re #1226.

---
 Code/Mantid/Algorithms/src/UnaryOperation.cpp |  5 +++-
 .../Geometry/src/Instrument/ParameterMap.cpp  | 18 ++-----------
 Code/Mantid/Kernel/inc/MantidKernel/Cache.h   | 25 ++++++++-----------
 3 files changed, 16 insertions(+), 32 deletions(-)

diff --git a/Code/Mantid/Algorithms/src/UnaryOperation.cpp b/Code/Mantid/Algorithms/src/UnaryOperation.cpp
index 3a0d64fdf58..1e219d8c6d3 100644
--- a/Code/Mantid/Algorithms/src/UnaryOperation.cpp
+++ b/Code/Mantid/Algorithms/src/UnaryOperation.cpp
@@ -52,8 +52,10 @@ namespace Mantid
       Progress progress(this,0.0,1.0,numSpec);
       
       // Loop over every cell in the workspace, calling the abstract correction function
+      PARALLEL_FOR2(in_work,out_work)
       for (int i = 0; i < numSpec; ++i)
       {
+        PARALLEL_START_INTERUPT_REGION
         // Copy the X values over
         out_work->setX(i,in_work->refX(i));
         // Get references to the data
@@ -72,8 +74,9 @@ namespace Mantid
         }
         
         progress.report();
+        PARALLEL_END_INTERUPT_REGION
       }
-      
+      PARALLEL_CHECK_INTERUPT_REGION
       return;
     }
        
diff --git a/Code/Mantid/Geometry/src/Instrument/ParameterMap.cpp b/Code/Mantid/Geometry/src/Instrument/ParameterMap.cpp
index 8616ee545ad..65ab8b2366b 100644
--- a/Code/Mantid/Geometry/src/Instrument/ParameterMap.cpp
+++ b/Code/Mantid/Geometry/src/Instrument/ParameterMap.cpp
@@ -246,14 +246,7 @@ void ParameterMap::setCachedLocation(const IComponent* comp, V3D& location) cons
 /// @returns true if the location is in the map, otherwise false
 bool ParameterMap::getCachedLocation(const IComponent* comp, V3D& location) const
 {
-  bool result;
-  // Call to getCachedLocation is a needs to be marked as the same critical region as a 
-  // write to the cache
-  PARALLEL_CRITICAL(positionCache)
-  {
-    result = m_cacheLocMap.getCache(comp->getComponentID(),location);
-  }
-  return result;
+  return m_cacheLocMap.getCache(comp->getComponentID(),location);
 }
 
 ///Sets a cached rotation on the rotation cache
@@ -274,14 +267,7 @@ void ParameterMap::setCachedRotation(const IComponent* comp, Quat& rotation) con
 /// @returns true if the rotation is in the map, otherwise false
 bool ParameterMap::getCachedRotation(const IComponent* comp, Quat& rotation) const
 {
-  bool result;
-  // Call to getCachedRotation is a needs to be marked as the same critical region as a 
-  // write to the cache
-  PARALLEL_CRITICAL(rotationCache)
-  {
-    result = m_cacheRotMap.getCache(comp->getComponentID(),rotation);
-  }
-  return result;
+  return m_cacheRotMap.getCache(comp->getComponentID(),rotation);
 }
 
 
diff --git a/Code/Mantid/Kernel/inc/MantidKernel/Cache.h b/Code/Mantid/Kernel/inc/MantidKernel/Cache.h
index 608c9bcedf2..e4dca99b5e8 100644
--- a/Code/Mantid/Kernel/inc/MantidKernel/Cache.h
+++ b/Code/Mantid/Kernel/inc/MantidKernel/Cache.h
@@ -6,6 +6,8 @@
 //----------------------------------------------------------------------
 #include <map>
 #include "MantidKernel/System.h"
+#include "MantidKernel/MultiThreaded.h"
+
 namespace Mantid
 {
   namespace Kernel
@@ -74,46 +76,39 @@ namespace Mantid
       }
 
       ///Sets a cached value on the rotation cache
-      void setCache(const KEYTYPE key, VALUETYPE value)
+      void setCache(const KEYTYPE& key, const VALUETYPE& value)
       {
-        if (getCacheNoStats(key,value))
-        {
-          //remove key
-          removeCache(key);
-        }
-        m_cacheMap.insert(std::make_pair(key,value));
+        m_cacheMap[key] = value;
       }
 
       ///Attempts to retreive a value from the cache
-      bool getCache(const KEYTYPE key, VALUETYPE& value) const
+      bool getCache(const KEYTYPE& key, VALUETYPE& value) const
       {
         bool found = getCacheNoStats(key,value);
         if (found) 
         {
+          PARALLEL_ATOMIC
           m_cacheHit++;
         }
         else
         {
+          PARALLEL_ATOMIC
           m_cacheMiss++;
         }
         return found;
       }
 
       ///removes the value associated with a key
-      void removeCache(const KEYTYPE key)
+      void removeCache(const KEYTYPE& key)
       {
-        CacheMapIterator it_found = m_cacheMap.find(key);
-        if (it_found != m_cacheMap.end()) 
-        {
-          m_cacheMap.erase(it_found);
-        }
+        m_cacheMap.erase(key);
       }
 
     private:
       ///Attempts to retreive a value from the cache
       bool getCacheNoStats(const KEYTYPE key, VALUETYPE& value) const
       {
-	CacheMapConstIterator it_found = m_cacheMap.find(key);
+        CacheMapConstIterator it_found = m_cacheMap.find(key);
         if (it_found == m_cacheMap.end()) 
         {
           return false; // did not find the component
-- 
GitLab