>> element-wise and doesn't depend on any of the other values, split into N
>> chunks (ideally N = number of cores * threads per core), send off copies
>> to the worker threads and then recombine the result in a new vector in
>
> Here is something similar (based on code by Bjarne Stroustrup):
> Calculate the sum of a vector in parallel.
>
> #include <algorithm>
> #include <future>
> #include <initializer_list>
> #include <iostream>
> #include <ostream>
> #include <thread>
> #include <utility>
> #include <vector>
>
> double sum( double const * const beginning, double const * const end )
> { return ::std::accumulate( beginning, end, 0.0 ); }
>
> double sum_in_parallel( ::std::vector< double > const & vector )
> { using task_type = double( double const *, double const * );
> ::std::packaged_task< task_type >package0{ sum };
> ::std::packaged_task< task_type >package1{ sum };
> ::std::future< double >future0{ package0.get_future() };
> ::std::future< double >future1{ package1.get_future() };
> double const * const p = &vector[ 0 ];
> { auto len { vector.size };
> ::std::thread thread0{ ::std::move( package0 ), p, p + len/2, 0 };
> ::std::thread thread1{ ::std::move( package0 ), p + len/2, p + len, 0 }; }
> return future0.get() + future1.get(); }
>
> int main()
> { ::std::vector const vector< double >{};
> ::std::cout << sum_in_parallel( vector )<< '\n'; }
>
> But I cannot get it compiled:
>
> error: variable 'std::packaged_task<double(const double*, const double*)> package0'
> has initializer but incomplete type
> ::std::packaged_task< task_type >package0{ sum };
> ^
> . Maybe someone can explain how to remove the error?
> (Is it my compiler not supporting all of the library?)
>
I have tried to keep the structure and logic of your code, while
removing the worst jumbled mess of formatting and the extra includes.
And it is crazy to call your std::vector instance "vector". (I hope you
don't teach your students that weird bracketing style, unusual spacing,
and unnecessary ::std. They are just going to have to unlearn it all
before working with any real-world code.)
Key errors:
1. Messed up type for "p"
2. Using "vector.size" instead of "vector.size()"
3. Extra parameter to your thread initialisers
4. Forgetting to join your threads
5. Using an empty vector for testing!
#include <numeric>
#include <vector>
#include <iostream>
#include <future>
static double sum(const double * const beginning, const double * const end)
{
return std::accumulate(beginning, end, 0.0);
}
static double sum_in_parallel(const std::vector<double> &vect)
{
using task_type = double(const double *, const double *);
std::packaged_task<task_type> package0 { sum };
std::packaged_task<task_type> package1 { sum };
std::future<double> future0 { package0.get_future() };
std::future<double> future1 { package1.get_future() };
const double * p = &vect[0];
const auto len { vect.size() };
std::thread thread0 { std::move(package0), p, p + len / 2 };
std::thread thread1 { std::move(package1), p + len / 2, p + len};
thread0.join();
thread1.join();
return future0.get() + future1.get();
}
int main()
{
const std::vector<double> vect { 1.0, 2.0, 3.0, 4.0 };
std::cout << sum_in_parallel(vect) << '\n';
}