Community
cancel
Showing results for
Did you mean:
Beginner
116 Views

## Using TBB Task Scheduler to calculate determinant of matrix

Having a problem with understanding why task scheduler makes program slower than its serial version is...

Here is serial version of my program which should calculate determinant of given matrix:

```double serial(vector<vector<double>> matrix)
{
double matrix_ord = matrix.size();
double det = 0;

if (matrix_ord == 1)
det = matrix[0][0];
else
{
for (int i = 0; i < matrix_ord; i++)
{
vector<vector<double>> minor = get_minor(matrix, i);

bool sign = (i % 2 == 0) ? 1 : -1;
det += matrix[0] * serial(minor) * sign;
};
}

return det;
}```

Because it is reccursive function, using TBB tasks i made Recursive chain reaction in parallel version, just like Fibonacci numbers example in TBB documentation:

First i am creating root task:

```double parallel(vector<vector<double>> matrix)
{

double det = 0;

return det;
}```

MyTask class with execute() function looks like this:

```class MyTask: public task {
public:
vector<vector<double>> matrix;
double* det;

det(det_), matrix(matrix_)  {}

{
int matrix_ord = matrica.size();

if (matrix_ord == 1)
*det= matrix[0][0];
else
{
vector<double> determinants_of_minors(matrix_ord, 0);

set_ref_count(matrix_ord+ 1);

for (int i = 1; i < matrix_ord; i++)
{
vector<vector<double>> minor = get_minor(matrix, i);

}

for (int i = 0; i < matrix_ord; i++)
{
bool sign = (i % 2 == 0) ? 1 : -1;
*det += matrix[0] * determinants_of_minors.at(i) * sign;
}
}

return NULL;
}
};```

As you can see, the idea is to create subtasks for every minor of current matrix till matrix size is 1x1 -> then the task is returning the only element of matrix to its successor.

Where the problem could be?