diff src/Silvet.cpp @ 104:62b7be1226d5 openmp

OpenMP support in main EM iteration
author Chris Cannam
date Tue, 06 May 2014 16:28:04 +0100
parents 9c7e6086192d
children ac750e222ad3
line wrap: on
line diff
--- a/src/Silvet.cpp	Tue May 06 11:31:14 2014 +0100
+++ b/src/Silvet.cpp	Tue May 06 16:28:04 2014 +0100
@@ -375,6 +375,8 @@
 
     FeatureSet fs;
 
+    if (filtered.empty()) return fs;
+
     for (int i = 0; i < (int)filtered.size(); ++i) {
         Feature f;
         for (int j = 0; j < processingHeight; ++j) {
@@ -387,37 +389,56 @@
 
     int iterations = 12;
 
-    for (int i = 0; i < width; ++i) {
+    int stride = 8;
 
-        double sum = 0.0;
-        for (int j = 0; j < processingHeight; ++j) {
-            sum += filtered[i][j];
+    for (int i = 0; i < width; i += stride) {
+
+        int chunk = stride;
+        if (i + chunk > width) {
+            chunk = width - i;
         }
 
-        if (sum < 1e-5) continue;
+        vector<vector<double> > pitchSubMatrix
+            (chunk, vector<double>(processingNotes));
 
-        EM em;
-        for (int j = 0; j < iterations; ++j) {
-            em.iterate(filtered[i]);
+#pragma omp parallel for
+        for (int k = 0; k < chunk; ++k) {
+
+            double sum = 0.0;
+            for (int j = 0; j < processingHeight; ++j) {
+                sum += filtered[i + k][j];
+            }
+
+            if (sum < 1e-5) continue;
+
+            EM em;
+            for (int j = 0; j < iterations; ++j) {
+                em.iterate(filtered[i + k]);
+            }
+
+            vector<double> pitches = em.getPitchDistribution();
+        
+            for (int j = 0; j < processingNotes; ++j) {
+                pitchSubMatrix[k][j] = pitches[j] * sum;
+            }
         }
+        
+        for (int k = 0; k < chunk; ++k) {
 
-        vector<double> pitches = em.getPitchDistribution();
-        
-        for (int j = 0; j < processingNotes; ++j) {
-            pitches[j] *= sum;
-        }
+            const vector<double> &pitches = pitchSubMatrix[k];
 
-        Feature f;
-        for (int j = 0; j < processingNotes; ++j) {
-            f.values.push_back(float(pitches[j]));
-        }
-        fs[m_pitchOutputNo].push_back(f);
+            Feature f;
+            for (int j = 0; j < processingNotes; ++j) {
+                f.values.push_back(float(pitches[j]));
+            }
+            fs[m_pitchOutputNo].push_back(f);
 
-        FeatureList noteFeatures = postProcess(pitches);
+            FeatureList noteFeatures = postProcess(pitches);
 
-        for (FeatureList::const_iterator fi = noteFeatures.begin();
-             fi != noteFeatures.end(); ++fi) {
-            fs[m_notesOutputNo].push_back(*fi);
+            for (FeatureList::const_iterator fi = noteFeatures.begin();
+                 fi != noteFeatures.end(); ++fi) {
+                fs[m_notesOutputNo].push_back(*fi);
+            }
         }
     }