← Back to team overview

yade-dev team mailing list archive

[Branch ~yade-pkg/yade/git-trunk] Rev 3923: Add OpenMPVector for thread-safe vector work.

 

------------------------------------------------------------
revno: 3923
committer: Anton Gladky <gladky.anton@xxxxxxxxx>
timestamp: Tue 2014-04-15 16:45:52 +0200
message:
  Add OpenMPVector for thread-safe vector work.
modified:
  lib/base/openmp-accu.hpp


--
lp:yade
https://code.launchpad.net/~yade-pkg/yade/git-trunk

Your team Yade developers is subscribed to branch lp:yade.
To unsubscribe from this branch go to https://code.launchpad.net/~yade-pkg/yade/git-trunk/+edit-subscription
=== modified file 'lib/base/openmp-accu.hpp'
--- lib/base/openmp-accu.hpp	2014-04-14 14:44:08 +0000
+++ lib/base/openmp-accu.hpp	2014-04-15 14:45:52 +0000
@@ -113,6 +113,57 @@
 	// only useful for debugging
 	std::vector<T> getPerThreadData() const { std::vector<T> ret; for(int i=0; i<nThreads; i++) ret.push_back(*(T*)(data+i*eSize)); return ret; }
 };
+
+/* OpenMP implementation of std::vector. 
+ * Very minimal functionality, which is required by Yade
+ */ 
+template<typename T>
+class OpenMPVector{
+  std::vector<std::vector<T> > vals;
+  size_t sizeV;
+  public:
+    OpenMPVector() {sizeV = omp_get_max_threads(); vals.resize(sizeV);};
+    void push_back (const T& val) {vals[omp_get_thread_num()].push_back(val);};
+    size_t size() const {
+      size_t sumSize = 0;
+      for (size_t i=0; i<sizeV; i++) {
+        sumSize += vals[i].size();
+      }
+      return sumSize;
+    }
+    
+    size_t size(size_t t) const {
+      if (t >= sizeV) {
+        std::cerr<< ("Index is out of range.")<<std::endl; exit (EXIT_FAILURE);
+      } else {
+        return vals[t].size();
+      }
+    }
+    
+    size_t sizeT() {
+      return sizeV;
+    }
+    
+    T operator[](size_t ix) const {
+      if (ix >= size()) {
+        std::cerr<< ("Index is out of range.")<<std::endl; exit (EXIT_FAILURE);
+      } else {
+        size_t t = 0;
+        while (ix >= vals[t].size()) {
+          ix-=vals[t].size();
+          t+=1;
+        }
+        return vals[t][ix];
+      }
+    }
+    
+    void clear() {
+      for (size_t i=0; i<sizeV; i++) {
+        vals[i].clear();
+      }
+    }
+    
+};
 #else 
 template<typename T>
 class OpenMPArrayAccumulator{
@@ -145,6 +196,8 @@
 	// debugging only
 	std::vector<T> getPerThreadData() const { std::vector<T> ret; ret.push_back(data); return ret; }
 };
+
+using OpenMPVector=std::vector;
 #endif
 
 // boost serialization