Created
May 6, 2011 01:28
-
-
Save Ignition/958302 to your computer and use it in GitHub Desktop.
Test for Zena
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <omp.h> | |
#include <cstdlib> | |
#include <iostream> | |
#include <list> | |
#include <time.h> | |
bool ompon = false; | |
std::list<int> intList; | |
const int N = 1000; | |
long fib(long n){ | |
if (n <= 2) | |
return 1; | |
else | |
return fib(n-1) + fib(n-2); | |
} | |
void work(void){ | |
long w; | |
long sum = 0; | |
time_t stopclock; | |
std::list<int>::iterator it; | |
stopclock = time(NULL); | |
#pragma omp parallel if(ompon) private(w, it) reduction(+:sum) | |
for (it = intList.begin(); it != intList.end(); it++) { | |
#pragma omp single nowait | |
{ | |
w = fib(*it); | |
sum += w; | |
} | |
} | |
stopclock = time(NULL) - stopclock; | |
std::cout << "The sum of fibs of list size " << N << std::endl << "=> is " << sum; | |
if (ompon) { | |
std::cout << " (openMP)" << std::endl << "Time taken with openMP: "; | |
} else { | |
std::cout << " (normal)" << std::endl << "Time taken without openMP: "; | |
} | |
std::cout << stopclock << "seconds" << std::endl << std::endl; | |
} | |
int main(int argc, char* argv[]) { | |
// Make random list of size N | |
std::cout << "Generate list...."; | |
srand ( (unsigned int) time(NULL) ); | |
do { | |
intList.push_back(rand() % 36 + 1); | |
} while (intList.size() < N); | |
std::cout << " done" << std::endl; | |
omp_set_num_threads(20); | |
ompon = true; | |
work(); | |
ompon = false; | |
work(); | |
exit(EXIT_SUCCESS); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can't remember exactly what the issue was with this code. Its OpenMP that I was looking into for Zena, something to do with the semantics of "#pragma omp single nowait" I think.