diff src/Silvet.cpp @ 312:796d403dc83b

Replace OpenMP usage with (hopefully more portable) C++11 async. Define MAX_EM_THREADS=1 if you don't want to use it.
author Chris Cannam
date Tue, 28 Apr 2015 10:02:36 +0100
parents 99af9557dfc1
children fa2ffbb786df
line wrap: on
line diff
--- a/src/Silvet.cpp	Tue Apr 28 09:41:40 2015 +0100
+++ b/src/Silvet.cpp	Tue Apr 28 10:02:36 2015 +0100
@@ -23,6 +23,7 @@
 #include "flattendynamics-ladspa.h"
 
 #include <vector>
+#include <future>
 
 #include <cstdio>
 
@@ -31,6 +32,8 @@
 using std::cerr;
 using std::endl;
 using std::pair;
+using std::future;
+using std::async;
 using Vamp::RealTime;
 
 static int processingSampleRate = 44100;
@@ -555,12 +558,36 @@
         localBestShifts = vector<vector<int> >(width);
     }
 
+#ifndef MAX_EM_THREADS
+#define MAX_EM_THREADS 8
+#endif
+
+#if (defined(MAX_EM_THREADS) && (MAX_EM_THREADS > 1))
+    for (int i = 0; i < width; ) {
+        typedef future<pair<vector<double>, vector<int>>> EMFuture;
+        vector<EMFuture> results;
+        for (int j = 0; j < MAX_EM_THREADS && i + j < width; ++j) {
+            results.push_back
+                (async(std::launch::async,
+                       [&](int index) {
+                           return applyEM(pack, filtered.at(index), wantShifts);
+                       }, i + j));
+        }
+        for (int j = 0; j < MAX_EM_THREADS && i + j < width; ++j) {
+            auto out = results[j].get();
+            localPitches[i+j] = out.first;
+            if (wantShifts) localBestShifts[i+j] = out.second;
+        }
+        i += MAX_EM_THREADS;
+    }
+#else
     for (int i = 0; i < width; ++i) {
         auto out = applyEM(pack, filtered.at(i), wantShifts);
         localPitches[i] = out.first;
         if (wantShifts) localBestShifts[i] = out.second;
     }
-        
+#endif
+    
     for (int i = 0; i < width; ++i) {
 
         // This returns a filtered column, and pushes the