i have written simple class to solve system of linear equations , to parallelize the matrix row subtractions i used std::async . after size of 10x10 matrix the program is crashing due to segmentation fault.
my first implementation was to replace vecSub((mat[c]), (temp));
with just std::async(std::launch::async, vecSub, std::ref(mat[c]), std::ref(temp))
but then discovered that if we don't assign it to any variable destructor is called and .get() is called , and stops the main thread (which was the reason it slowed down), so i changed it to the below implementation and now i am getting seg fault
this is single threaded:
class solver
{
Mat mat;
public:
//give eqn in the form ax1+ax2+ax3..axN = k (coeffiants only)
Vec solve(Mat &in)
{
mat = in;
ge(mat);
return (bs(mat));
}
Vec solve(Mat &&in)
{
mat = std::move(in);
ge(mat);
return (bs(mat));
}
private:
void ge(Mat &mat)
{
using li = long int;
for (li p = 0; p < mat[0].size() - 1; p++)
{
for (li c = p + 1; c < mat.size(); c++)
{
auto x = mat[c][p] / mat[p][p];
auto temp = mat[p];
vecMul(x, temp);
vecSub((mat[c]), (temp));
}
}
}
Vec bs(Mat &mat)
{
using li = long int;
Vec x(mat.size());
for (li i = mat.size() - 1; i >= 0; i--)
{
double s = 0;
for (li j = i; j < mat[0].size() - 1; j++)
{
s += mat[i][j] * x[j];
x[i] = ((mat[i][mat[0].size() - 1] - s) / (mat[i][i]));
}
}
return x;
}
static void vecMul(double a, Vec &b)
{
using li = size_t;
for (li i = 0; i < b.size(); i++)
b[i] *= a;
}
//static
static void vecAdd(Vec &a, Vec &b)
{
using li = size_t;
assert(a.size() == b.size());
for (li i = 0; i < a.size(); i++)
a[i] = a[i] + b[i];
}
static void vecSub(Vec &a, Vec &b)
{
using li = size_t;
assert(a.size() == b.size());
for (li i = 0; i < a.size(); i++)
a[i] = a[i] - b[i];
}
};
multithreaded
class solver
{
Mat mat;
public:
//give eqn in the form ax1+ax2+ax3..axN = k (coeffiants only)
Vec solve(Mat &in)
{
mat = in;
ge(mat);
return (bs(mat));
}
Vec solve(Mat &&in)
{
mat = std::move(in);
ge(mat);
return (bs(mat));
}
private:
void ge(Mat &mat)
{
using li = long int;
for (li p = 0; p < mat[0].size() - 1; p++)
{
std::vector<std::future<void>> ts;
for (li c = p + 1; c < mat.size(); c++)
{
auto x = mat[c][p] / mat[p][p];
auto temp = mat[p];
vecMul(x, temp);
ts.push_back(std::async(std::launch::async, vecSub, std::ref(mat[c]), std::ref(temp)));
}
for (auto &t : ts)
{
t.get();
}
}
}
Vec bs(Mat &mat)
{
using li = long int;
Vec x(mat.size());
for (li i = mat.size() - 1; i >= 0; i--)
{
double s = 0;
for (li j = i; j < mat[0].size() - 1; j++)
{
s += mat[i][j] * x[j];
x[i] = ((mat[i][mat[0].size() - 1] - s) / (mat[i][i]));
}
}
return x;
}
static void vecMul(double a, Vec &b)
{
using li = size_t;
for (li i = 0; i < b.size(); i++)
b[i] *= a;
}
//static
static void vecAdd(Vec &a, Vec &b)
{
using li = size_t;
assert(a.size() == b.size());
for (li i = 0; i < a.size(); i++)
a[i] = a[i] + b[i];
}
static void vecSub(Vec &a, Vec &b)
{
using li = size_t;
// assert(a.size() == b.size());
for (li i = 0; i < a.size(); i++)
a[i] = a[i] - b[i];
}
};
the segmentation is occuring at
static void vecSub(Vec &a, Vec &b)
{
using li = size_t;
assert(a.size() == b.size());
for (li i = 0; i < a.size(); i++)
a[i] = a[i] - b[i];
}
Whatever the Mat
type is, I'm pretty sure this:
auto temp = mat[p];
does not create a reference but a copy. It means this
ts.push_back(std::async(std::launch::async, vecSub, std::ref(mat[c]), std::ref(temp)));`
actually makes a reference to a temporary value temp
which is about to be destroyed at the end of this loop. That's undefined behavior.
This could possibly fix the issue:
auto& temp = mat[p];