Permalink
Browse files

A naive parallelization attempt.. no speedup

  • Loading branch information...
1 parent 66d5685 commit 815e60f128c35761a19ec8bc4a4f9c3b5fca4c22 @teqdruid teqdruid committed Jul 17, 2012
Showing with 128 additions and 14 deletions.
  1. +1 −1 SConstruct
  2. +60 −13 svf/matrix_correlate.hpp
  3. +30 −0 svf/timer.hpp
  4. +33 −0 tests/basictests.cpp
  5. +4 −0 tests/testrunner.cpp
View
@@ -4,7 +4,7 @@ env = Environment(CPPPATH = ['.'],
'-fopenmp'],
LIBS=[],
LIBPATH=[],
- LINKFLAGS=[])
+ LINKFLAGS=['-fopenmp'])
testrunner = env.Program('testrunner', Glob('tests/*.cpp'),
View
@@ -200,38 +200,85 @@ class RandomTraceProportional: public Selector {
}
};
+#define TBUFFER 500
class Pearson {
public:
double operator()(Matrix& x, Matrix& y, Selector& sel) {
- boost::optional<Selector::Pair> p;
size_t size = 0;
double xbar = 0.0, ybar = 0.0;
sel.reset();
- while ( (p = sel.next()) ) {
- size += 1;
- xbar += x[*p];
- ybar += y[*p];
+#pragma omp parallel default(none) shared(x, y, sel) reduction(+:size) reduction(+:xbar) reduction(+:ybar)
+ {
+ bool cont = true;
+ boost::optional<Selector::Pair> pArr[TBUFFER];
+ while (cont) {
+#pragma omp critical
+ {
+ for (size_t i=0; i<TBUFFER; i++) {
+ pArr[i] = sel.next();
+ if (!pArr[i])
+ break;
+ }
+ }
+
+ for (size_t i=0; i<TBUFFER; i++) {
+ if (!pArr[i]) {
+ cont = false;
+ break;
+ }
+
+ size += 1;
+ auto pl = *pArr[i];
+ xbar += x[pl];
+ ybar += y[pl];
+ }
+ }
}
+
+ boost::optional<Selector::Pair> p;
// Compute averages
xbar /= size;
ybar /= size;
sel.reset();
size_t secondCounter = 0;
double sum = 0.0, sx = 0.0, sy = 0.0;
- while (p = sel.next()) {
- secondCounter += 1;
-
- double xd = x[*p] - xbar;
- double yd = y[*p] - ybar;
- sum += xd * yd;
- sx += xd * xd;
- sy += yd * yd;
+#pragma omp parallel default(none) shared(x, y, sel, xbar, ybar) reduction(+:secondCounter) \
+ reduction(+:sum) reduction(+:sx) reduction(+:sy)
+ {
+ bool cont = true;
+ boost::optional<Selector::Pair> pArr[TBUFFER];
+ while (cont) {
+#pragma omp critical
+ {
+ for (size_t i=0; i<TBUFFER; i++) {
+ pArr[i] = sel.next();
+ if (!pArr[i])
+ break;
+ }
+ }
+
+ for (size_t i=0; i<TBUFFER; i++) {
+ if (!pArr[i]) {
+ cont = false;
+ break;
+ }
+ auto pl = *pArr[i];
+ secondCounter += 1;
+
+ double xd = x[pl] - xbar;
+ double yd = y[pl] - ybar;
+
+ sum += xd * yd;
+ sx += xd * xd;
+ sy += yd * yd;
+ }
+ }
}
assert(secondCounter == size);
View
@@ -0,0 +1,30 @@
+#ifndef __SVF_TIMER_HPP__
+#define __SVF_TIMER_HPP__
+
+#include <sys/time.h>
+
+struct Timer {
+ static double getTime() {
+ struct timeval tv;
+ gettimeofday(&tv, NULL);
+ double secs = tv.tv_usec;
+ secs /= 1000000;
+ secs += tv.tv_sec;
+ return secs;
+ }
+
+ double startTime;
+ double& timeResult;
+
+ Timer(double& timeResult) :
+ timeResult(timeResult) {
+ startTime = getTime();
+ }
+
+ ~Timer() {
+ double now = getTime();
+ timeResult = now - startTime;
+ }
+};
+
+#endif // __SVF_TIMER_HPP__
View
@@ -8,6 +8,10 @@
#include <iostream>
#include <cmath>
+#include <svf/timer.hpp>
+
+#include <omp.h>
+
class ScalarTests: public CPPUNIT_NS::TestCase
{
CPPUNIT_TEST_SUITE(ScalarTests);
@@ -19,6 +23,7 @@ class ScalarTests: public CPPUNIT_NS::TestCase
CPPUNIT_TEST(testRandomCorrel);
CPPUNIT_TEST(testRandomCorrelBig);
CPPUNIT_TEST(testPartialCorrel);
+ CPPUNIT_TEST(testRandomCorrelBigParallel);
CPPUNIT_TEST_SUITE_END();
protected:
@@ -130,6 +135,34 @@ class ScalarTests: public CPPUNIT_NS::TestCase
CPPUNIT_ASSERT(svfVal > 0.2);
CPPUNIT_ASSERT(svfVal < 0.8);
}
+
+ void testRandomCorrelBigParallel(void) {
+ // A bigger test for random correlation so we can lower the
+ // delta
+
+ SVF::SVF<int, ScalarDist, int, ScalarDist> svf;
+
+ srand(100);
+ for (size_t i=0; i<10000; i++) {
+ // Sine waves have zero linear correlation
+ svf.pushTimestep(rand(), rand());
+ }
+
+ for (size_t threads = 1; threads<16; threads++) {
+ omp_set_num_threads(threads);
+ printf("\n%lu threads: ", threads);
+ fflush(stdout);
+ double elapsed;
+ double svfVal;
+ {
+ Timer t(elapsed);
+ svfVal = svf.computeSVF();
+ }
+ CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, svfVal, 0.002);
+ printf("%lf seconds", elapsed);
+ }
+ printf("\n");
+ }
};
CPPUNIT_TEST_SUITE_REGISTRATION(ScalarTests);
View
@@ -7,8 +7,12 @@
#include <cppunit/BriefTestProgressListener.h>
#include <cppunit/TextOutputter.h>
+#include <omp.h>
+
int main( int ac, char **av )
{
+ omp_set_num_threads(1);
+
//--- Create the event manager and test controller
CPPUNIT_NS::TestResult controller;

0 comments on commit 815e60f

Please sign in to comment.