Solving a linear system of equations using threads

Hi everyone,

I am trying to write a code that solves a system of linear equations such as A*B=C. My system has a dimension equal to 1600. The matrix A cab be separated into 4 sub matrices and each can be handled by a different thread. I tried to solve this using the following code:


int main() {
int count = 0;

//Inputing matrix A
ifstream matrix;
matrix.open("example.txt");
for (int i = 0; i < DIM; i++)
{
for (int j = 0; j < DIM; j++)
{
matrix >> matrix_A[i][j];
}
}

//Input matrix C
ifstream matrix1;
matrix1.open("example1.txt");
for (int i = 0; i < DIM; i++)
{
matrix1 >> matrix_C[i];
}

vector<std::thread> threadList;

threadList.push_back(std::thread(solution,
(double *)matrix_A, (double *)matrix_B, (double *)matrix_C, 400, 0));
threadList.push_back(std::thread(solution,
(double *)matrix_A, (double *)matrix_B, (double *)matrix_C, 400, 399));
threadList.push_back(std::thread(solution,
(double *)matrix_A, (double *)matrix_B, (double *)matrix_C, 400, 799));
threadList.push_back(std::thread(solution,
(double *)matrix_A, (double *)matrix_B, (double *)matrix_C, 400, 1199));



// wait for all threads to finish
for (auto& threadID : threadList){
threadID.join();
}

cout << endl << endl;
cout << "The solution of system A*B=C is the following" << endl;
print1(matrix_B, DIM);

long int after = GetTickCount();
cout << "Execution Time : " << (after - before) << " ms." << endl;

system("pause");
return 0;
}

Although the above code gives the correct answer, the time needs to find the solution is bigger than that needed without using threads.

Can anyone explain !

Thanks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
int main()
{
    int count = 0;

    //Inputing matrix A
    ifstream matrix;
    matrix.open( "example.txt" );
    for ( int i = 0; i < DIM; i++ )
        {
            for ( int j = 0; j < DIM; j++ )
                {
                    matrix >> matrix_A[i][j];
                }
        }

    //Input matrix C
    ifstream matrix1;
    matrix1.open( "example1.txt" );
    for ( int i = 0; i < DIM; i++ )
        {
            matrix1 >> matrix_C[i];
        }

    vector<std::thread> threadList;

    threadList.push_back( std::thread( solution,
                                       ( double * )matrix_A, ( double * )matrix_B, ( double * )matrix_C, 400, 0 ) );
    threadList.push_back( std::thread( solution,
                                       ( double * )matrix_A, ( double * )matrix_B, ( double * )matrix_C, 400, 399 ) );
    threadList.push_back( std::thread( solution,
                                       ( double * )matrix_A, ( double * )matrix_B, ( double * )matrix_C, 400, 799 ) );
    threadList.push_back( std::thread( solution,
                                       ( double * )matrix_A, ( double * )matrix_B, ( double * )matrix_C, 400, 1199 ) );



    // wait for all threads to finish
    for ( auto& threadID : threadList )
        {
            threadID.join();
        }

    cout << endl << endl;
    cout << "The solution of system A*B=C is the following" << endl;
    print1( matrix_B, DIM );

    long int after = GetTickCount();
    cout << "Execution Time : " << ( after - before ) << " ms." << endl;

    system( "pause" );
    return 0;
}
Topic archived. No new replies allowed.