📜  使用pthread加法和减法矩阵

📅  最后修改于: 2021-04-29 12:51:45             🧑  作者: Mango

矩阵的加法或减法在没有线程的情况下花费O(n ^ 2)时间,但使用线程不会降低程序的时间复杂度,而是将任务划分为多个核心,例如如果我们有4个核心,然后将矩阵划分为4个部分核心占用矩阵的一部分并计算操作,最后在完成每个任务后,所有4个线程将加入主程序并显示最终输出。

注–仅当我们只有一个CPU内核时,该方法才有效。如果我们只有一个内核,则不要创建线程,因为我们将任务划分为仅在一个CPU上执行,因此浪费时间来划分任务,然后计算。

例子:

Input : 

Matrix A:
3 7 3 6 
9 2 0 3 
0 2 1 7 
2 2 7 9 

Matrix B:
6 5 5 2 
1 7 9 6 
6 6 8 9 
0 3 5 2 

Output :

Sum of Matrix A and B:
9   12   8   8   
10   9   9   9   
6   8   9   16   
2   5   12   11   

Subtraction of Matrix A and B:
-3   2  -2   4   
 8  -5  -9  -3   
-6  -4  -7  -2   
 2  -1   2   7 

代码–建议在基于Linux的系统中执行程序。

#include 
#include 
#include 
  
// Value depend on System core
#define CORE 4
  
// Maximum matrix size
#define MAX 4
  
// Maximum threads is equal to total core of system
pthread_t thread[CORE * 2];
int mat_A[MAX][MAX], mat_B[MAX][MAX], sum[MAX][MAX], sub[MAX][MAX];
  
// Addition of a Matrix
void* addition(void* arg)
{
  
    int i, j;
    int core = (int)arg;
  
    // Each thread computes 1/4th of matrix addition
    for (i = core * MAX / 4; i < (core + 1) * MAX / 4; i++) {
  
        for (j = 0; j < MAX; j++) {
  
            // Compute Sum Row wise
            sum[i][j] = mat_A[i][j] + mat_B[i][j];
        }
  
    }
  
}
  
  
// Subtraction of a Matrix
void* subtraction(void* arg)
{
  
    int i, j;
    int core = (int)arg;
  
    // Each thread computes 1/4th of matrix subtraction
    for (i = core * MAX / 4; i < (core + 1) * MAX / 4; i++) {
  
        for (j = 0; j < MAX; j++) {
  
            // Compute Subtract row wise
            sub[i][j] = mat_A[i][j] - mat_B[i][j];
        }
  
    }
  
}
  
  
// Driver Code
int main()
{
  
    int i, j, step = 0;
    // Generating random values in mat_A and mat_B
    for (i = 0; i < MAX; i++)  {
  
        for (j = 0; j < MAX; j++)  {
  
            mat_A[i][j] = rand() % 10;
            mat_B[i][j] = rand() % 10;
  
        }
  
    }
  
  
    // Displaying mat_A
    printf("\nMatrix A:\n");
  
    for (i = 0; i < MAX; i++) {
  
        for (j = 0; j < MAX; j++) {
  
            printf("%d ", mat_A[i][j]);
        }
  
        printf("\n");
    }
  
    // Displaying mat_B
    printf("\nMatrix B:\n");
  
    for (i = 0; i < MAX; i++) {
  
        for (j = 0; j < MAX; j++) {
  
            printf("%d ", mat_B[i][j]);
        }
  
        printf("\n");
    }
  
    // Creating threads equal
    // to core size and compute matrix row
    for (i = 0; i < CORE; i++) {
  
        pthread_create(&thread[i], NULL, &addition, (void*)step);
        pthread_create(&thread[i + CORE], NULL, &subtraction, (void*)step);
        step++;
    }
  
    // Waiting for join threads after compute
    for (i = 0; i < CORE * 2; i++) {
  
        pthread_join(thread[i], NULL);
    }
  
    // Display Addition of mat_A and mat_B
    printf("\n Sum of Matrix A and B:\n");
  
    for (i = 0; i < MAX; i++) {
  
        for (j = 0; j < MAX; j++) {
  
            printf("%d   ", sum[i][j]);
        }
  
        printf("\n");
    }
  
    // Display Subtraction of mat_A and mat_B
    printf("\n Subtraction of Matrix A and B:\n");
  
    for (i = 0; i < MAX; i++) {
  
        for (j = 0; j < MAX; j++) {
  
            printf("%d   ", sub[i][j]);
        }
  
        printf("\n");
    }
  
    return 0;
}

输出:

Matrix A:
3 7 3 6 
9 2 0 3 
0 2 1 7 
2 2 7 9 

Matrix B:
6 5 5 2 
1 7 9 6 
6 6 8 9 
0 3 5 2 

Sum of Matrix A and B:

9   12   8   8   
10   9   9   9   
6   8   9   16   
2   5   12   11   

Subtraction of Matrix A and B:

-3   2  -2   4   
 8  -5  -9  -3   
-6  -4  -7  -2   
 2  -1   2   7