Reputation: 13
So I am trying to multithread the dot product computation between two matrix objects in a "library" I'm writing. here is the code of interest
double mat_cont::dot_t( mat_cont & other, const int offset, \
const int upper_lim)
{
double local_sum = 0;
for (int i = offset; i < upper_lim+offset; ++i)
local_sum+=(this->mat[i] + other.mat[i]);
return local_sum;
}
double mat_cont::dot( mat_cont & other){
future<double> threads[3];
int part = (int)(dim * dim) / (int)4;
double sum = 0;
for (int i = 1; i < 4; ++i){
threads[i-1] =
async (std::launch::async,&mat_cont::dot_t,
other, part*i, (i+1)*part);
}
for(int i = 0; i < part; ++i)
sum+=(this->mat[i] + other.mat[i]);
for(int i = 1; i < 3; ++i)
sum+=threads[i].get();
return sum;
}
and this compilation error is thrown
error: no matching function for call to 'async'
threads[i-1] = async (std::launch::async,&mat_cont::dot_t, other, part*i, (i+1)*part);
^~~~~
/Applications/Xcode.app/Contents/Developer/
Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/future:2337:1:
note: candidate template ignored: substitution failure [with _Fp = double (mat_cont::*)(mat_cont &, int, int), _Args =
<mat_cont &, int, int>]: no type named 'type' in 'std::__1::__invoke_of<double (mat_cont::*)(mat_cont &, int, int), mat_cont, int, int>'async(launch __policy, _Fp&& __f, _Args&&... __args)
I'm wondering if I can multithread this part, or if I need to pass both of these objects into a friend function for multithreading. I've been trying to debug this for 2 hours now, any tips?
Upvotes: 0
Views: 220
Reputation: 30619
mat_cont::dot_t
is a non-static member function, so it needs a this
object to work on.
The easiest way to deal with that is to use a lambda that captures the this
pointer. Then you can call dot_t
exactly like you would in any other member function:
threads[i-1] = std::async(std::launch::async,
[this, i, part, &other]() {
return dot_t(other, part*i, (i+1)*part);
});
Upvotes: 1
Reputation: 20949
dot_t
is member function of mat_cont
which takes 3 parameters, so there is one missing element in the call of async
function - this
pointer should be passed as third paremeter of the call
threads[i-1] = async (std::launch::async,&mat_cont::dot_t, this, std::ref(other), part*i, (i+1)*part);
Upvotes: 1