Combinable containers
Preventing from excessive use of locks, uses combinable containers, such as following
vector<int> values(10);
int i = 0;
generate(values.begin(), values.end(), [&]{return ++i;} );
// 1. Calculate the sum of the elements of the vector in parallel.
combinable<int> sums;
parallel_for_each(values.begin(),values.end(), [&](int n)
{
sums.local() += n;}
);
// 2. Combine/Reduce into final result
int result = sums.combine([](int left, int right) { return left + right; });
The reason behind nullptr
Due to template type prediction, either of the following declaration would result to deduct int version of the template and eventually cause compilation to fail.
#define NULL 0 //or sometimes #define NULL (void *)0
This is explained at http://channel9.msdn.com/shows/Going+Deep/Stephan-T-Lavavej-Everything-you-ever-wanted-to-know-about-nullptr/
parallel_for_each Example
#include<iostream>
#include <conio.h>
#include <ppl.h>
#include <windows.h>
#include <vector>
#include <algorithm>
using namespace std;
using namespace Concurrency;
__int64 GetTime(){
return GetTickCount();
}
void makeDouble(vector<int> vec){
for_each(vec.begin(),vec.end(),[&](int i){
cout<<2*i<<" ";
});
cout<<endl<<endl<<endl<<endl;
}
void parallel_makeDouble(vector<int> vec){
parallel_for_each(vec.begin(),vec.end(),[=](int i){
cout<<2*i<<" ";
});
cout<<endl<<endl<<endl<<endl;
}
int main(int argc, char** argv){
vector<int> v(200);
int n=0;
generate(v.begin(),v.end(),[&](){ return n++;});
__int64 begin=GetTime();
makeDouble(v);
__int64 end=GetTime();
cout<<"Seq:"<<end-begin<<endl;
begin=GetTime();
parallel_makeDouble(v);
end=GetTime();
cout<<"Parallel:"<<end-begin<<endl;
getch();
return 0;
}
parallel_for example
#include<iostream>
#include <conio.h>
#include <ppl.h>
using namespace std;
using namespace Concurrency;
void makeDouble(int n){
for(int i=0;i<n;i++){
cout<<2*i<<" ";
}
cout<<endl<<endl<<endl<<endl;
}
void parallel_makeDouble(int n){
parallel_for(0,n,[](int i){
cout<<2*i<<" ";
});
}
int main(int argc, char** argv){
makeDouble(100);
parallel_makeDouble(100);
getch();
return 0;
}
Output:
0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 5
6 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106
108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146
148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178 180 182 184 186
188 190 192 194 196 198
50 0 150 100 52 54 56 58 60 176 178 180 182 184 186 188 190 192 194 196 198 62 1
02 104 152 154 156 164 166 168 1702 64 66 68 70 72 172 174 158 106 108 110 112
114 116 118 120 162 4 122 124 126 128 130 132 134 136 138 140 28 74 76 78 80 82
84 86 88 90 92 94 96 98 30 32 34 36 38 40 42 44 46 48 142 6 144 160 146 8 10 12
18 20 148 22 14 24 16 26
Secret History of SV
On google tech talk i saw this talk and it is quite entertaining.
http://www.youtube.com/user/GoogleTechTalks#p/u/6/hFSPHfZQpIQ
-
Recent
-
Links
-
Archives
- April 2011 (3)
- March 2011 (21)
- May 2010 (1)
- January 2010 (2)
- December 2009 (2)
- August 2009 (1)
- July 2009 (2)
- April 2009 (4)
- March 2009 (6)
- February 2009 (5)
- January 2009 (4)
- December 2008 (3)
-
Categories
-
RSS
Entries RSS
Comments RSS