Aegisub/devel/athenasub/test/omp_pipeline.cpp
Amar Takhar aa506bfe40 SVN Transition Step 6/7
1. svn mv OverLua SSATool athenasub avisynth_prs kanamemo \
     motiontracker prs traydict unit_test vsfilter devel/

* See r2749 for full description.

Originally committed to SVN as r2755.
2009-03-08 08:31:54 +00:00

55 lines
1.3 KiB
C++

#include <iostream>
#include <omp.h>
double wasteTime(double in)
{
double accum = in;
for (size_t i=0;i<5000000;i++) {
accum = accum + accum/2.0 - accum/3.0;
}
return accum;
}
int main()
{
using namespace std;
cout << "OpenMP test:\n";
bool run = true;
int n = 0;
omp_set_num_threads(8);
#pragma omp parallel shared(run,n)
{
while (run) {
int myTask;
double lol = 0.0;
#pragma omp critical (secA)
{
myTask = n;
n++;
if (myTask >= 50) run = false;
if (run) {
cout << "Thread #" << omp_get_thread_num() << " on task " << myTask << "/1.\n";
lol -= wasteTime(myTask * 1.0);
cout << "Thread #" << omp_get_thread_num() << " got " << lol << " on task " << myTask << "/1. Task done.\n";
}
}
if (run) {
#pragma omp critical (secB)
{
cout << "Thread #" << omp_get_thread_num() << " on task " << myTask << "/2.\n";
lol += wasteTime(myTask * 2.0);
cout << "Thread #" << omp_get_thread_num() << " got " << lol << " on task " << myTask << "/2. Task done.\n";
}
#pragma omp critical (secC)
{
cout << "Thread #" << omp_get_thread_num() << " on task " << myTask << "/3.\n";
lol -= wasteTime(myTask * 3.0);
cout << "Thread #" << omp_get_thread_num() << " finished task " << myTask << " with the useless result of " << lol << "\n";
}
}
}
}
}