1 2 3 4 5 6 7 8 9 10 11
| double sommerAvecParallelisation( std::vector<double> const& v ) {
const int N = v.size() / 4;
std::future<double> fut1 = std::async( std::launch::async , [&] { return std::accumulate( v.cbegin() , v.cbegin() + N , 0. ); } );
std::future<double> fut2 = std::async( std::launch::async , [&] { return std::accumulate( v.cbegin() + N , v.cbegin() + N*2 , 0. ); } );
std::future<double> fut3 = std::async( std::launch::async , [&] { return std::accumulate( v.cbegin() + N*2 , v.cbegin() + N*3 , 0. ); } );
double r4 = std::accumulate( v.cbegin() + N*3 , v.cend() , 0. );
fut1.wait(); fut2.wait(); fut3.wait(); // attendre la fin des 3 traitements parralèles
return fut1.get() + fut2.get() + fut3.get() + r4; // et cumuler les 4 sous-résultats
} |
Partager