I’m normally used to using OpenMP for parallelizing tasks. However, I thought I might give Parallel Patterns Library a shot when I started working on a new project recently.
Problem is that I have to store data in the list structure, which would have varying sizes throughout the process. I could parallelize lists with OpenMP with creating tasks with each iteration, and I tried to accomplish the same thing with the following code
#include <list>
#include <ppl.h>
#include <iostream>
#include <Windows.h>
using namespace std;
using namespace Concurrency;
void expo(double x){
double r = 1;
for(int i = 0 ; i<1000000 ; ++i){
r *= x;
}
cout << r << " ";
}
int wmain()
{
int begin,end;
list<int> numbers;
for(int i=1; i<11 ; ++i){
numbers.push_back(i);
}
list<int>::iterator lit;
structured_task_group tasks;
begin = GetTickCount();
for(lit=numbers.begin() ; lit!=numbers.end() ; lit++){
int k = *lit;
auto task1 = make_task([&k](){ expo(k); });
tasks.run(task1);
tasks.wait();
}
end = GetTickCount();
printf("elapsed: %d ms",end - begin);
cin.get();
return 0;
}
But, as you can see, I have to wait to the completion of the task inside the for-loop, which implies a serial execution. If I place “tasks.wait()” after the for-loop, I am getting an error, and the compiler says that there is a wait command missing after the run command.
How can I get around this problem?
By the way, do you know any good tutorial on PPL? I have found this page (http://msdn.microsoft.com/en-us/library/dd492418.aspx) which didn’t seem to me as the perfect tutorial for a beginner.
Thanks in advance!
You can replace your
structured_task_groupandfor()loop with the following: