Thực hiện cộng/trừ hai ma trận m x n song song bằng pthread. Mỗi luồng xử lý một hàng.
Chương trình C++ ví dụ
#include <iostream>
#include <pthread.h>
#include <cstdint>
#define CORE 3
#define MAX 3
using namespace std;
int AMat[MAX][MAX]={{10,20,30},{40,50,60},{70,80,50}};
int BMat[MAX][MAX]={{80,60,20},{30,20,15},{10,14,35}};
pthread_t thread[CORE*2];
int add[MAX][MAX], sub[MAX][MAX];
void* addMatrices(void* arg) {
intptr_t core=(intptr_t)arg;
for(int i=core*MAX/3;i<(core+1)*MAX/3;i++)
for(int j=0;j
Kết quả
Matrix A: 10 20 30 / 40 50 60 / 70 80 50
Matrix B: 80 60 20 / 30 20 15 / 10 14 35
Addition: 90 80 50 / 70 70 75 / 80 94 85
Subtraction: -70 -40 10 / 10 30 45 / 60 66 15