mardi 16 mars 2021

std::async slowing down program

Basically i wrote a simple c++ program to solve system of linear equations i tried to async the row operations and the result i got is suprisingly slower i am stuck the fact that i am opening too many threads may be a problem when dealing with 1000x1000 matrix , batch processing is a lil tough any other ideas?? (btw ,i am only trying to async the ge function )

#include <bits/stdc++.h>
using Vec = std::vector<double>;
using Mat = std::vector<Vec>;
using eqn = Mat;
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));
                std::async(std::launch::async,vecSub,std::ref(mat[c]), std::ref(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];
    }
};
std::ostream &operator<<(std::ostream &os, Mat mat)
{

    for (auto &x : mat)
    {
        for (auto &y : x)
            std::cout << y << ',' << ' ';
        std::cout << '\n';
    }
    return os;
}
std::ostream &operator<<(std::ostream &os, Vec v)
{
    using std::cout;
    using std::endl;
    for (auto &x : v)
        os << x << ",";
    os << endl;
    return os;
}
driver code:
#include "solver.hpp"
class Timer
{
private:
  std::chrono::_V2::system_clock::time_point m_StartTime;

public:
  void Start();
  float GetDuration();
};
void Timer::Start()
{
  m_StartTime = std::chrono::high_resolution_clock::now();
}

float Timer::GetDuration()
{
  std::chrono::duration<float> duration = std::chrono::high_resolution_clock::now() - m_StartTime;
  return duration.count();
}
int main()
{
  Timer t;
  int n = 2;
  using namespace std;
  cout << "size:";
  cin >> n;

  Mat a(n);
  for (int i = 0; i < n; i++)
  {
    for (int j = 0; j < n + 1; j++)
    {
      a[i].push_back((rand() % 200) + 1);
    }
  }
  solver s;
  t.Start();
  auto res = s.solve(a);
  cout << "time:" << t.GetDuration();

  return 0;
}

Aucun commentaire:

Enregistrer un commentaire