Parallel processing C+中的并行矩阵乘法+; 我尝试在C++中实现并行多线程矩阵乘法。我遵循的方法包括将数组划分为4个子数组,并在这4个子数组上使用4个线程执行并行乘法 我编写了C++代码,但它是抛出错误并显式终止的。错误: “在抛出std::system_错误实例后调用terminate” what():无效参数“
这是我的完整代码。对于C++和多线程,我比较新。p>Parallel processing C+中的并行矩阵乘法+; 我尝试在C++中实现并行多线程矩阵乘法。我遵循的方法包括将数组划分为4个子数组,并在这4个子数组上使用4个线程执行并行乘法 我编写了C++代码,但它是抛出错误并显式终止的。错误: “在抛出std::system_错误实例后调用terminate” what():无效参数“,parallel-processing,matrix-multiplication,Parallel Processing,Matrix Multiplication,这是我的完整代码。对于C++和多线程,我比较新。p> #include <iostream> #include <thread> #include <mutex> #include <vector> #include <algorithm> #include <string> #define N 4 using namespace std; mutex mu; void stage_1_multiply(int *a,i
#include <iostream>
#include <thread>
#include <mutex>
#include <vector>
#include <algorithm>
#include <string>
#define N 4
using namespace std;
mutex mu;
void stage_1_multiply(int *a,int *b,int *d){
int *xij;
int *yij;
int *zij;
int COLS = N,ROWS = N;
cout<< " thread "<< this_thread::get_id() << " "<<endl;
for(int i = 0;i<(N/2);++i){
for(int j = 0;j < (N/2); j++){
for(int k = 0; k<(N/2);k++){
mu.lock();
xij = a + ((COLS * i) + k);
yij = b + ((COLS * k) + j);
zij = d + ((COLS * i) + j);
*zij += ( (*xij) * (*yij) );
mu.unlock();
}
}
}
}
int main(){
int A[4][4],B[4][4],C[4][4],D_1[4][4],D_2[4][4];
for(int i = 0;i<4;i++){
for(int j = 0;j<4;j++){
A[i][j] = i + 1;
B[i][j] = i + 1;
C[i][j] = 0;
D_1[i][j] = 0;
D_2[i][j] = 0;
}
}
for(int i = 0;i<4;i++){
for(int j = 0;j< 4;j++){
cout << A[i][j] << " ";
}
cout << endl;
}
for(int i = 0;i<4;i++){
for(int j = 0;j< 4;j++){
cout << B[i][j] << " ";
}
cout << endl;
}
vector< thread> threads(8);
int th = 0;
threads[th++] = thread(stage_1_multiply,&A[0][0],&B[0][0],&D_1[0][0]);
threads[th++] = thread(stage_1_multiply,&A[0][2],&B[2][0],&D_2[0][0]);
threads[th++] = thread(stage_1_multiply,&A[2][0],&B[0][2],&D_1[2][2]);
threads[th++] = thread(stage_1_multiply,&A[2][2],&B[2][2],&D_2[2][2]);
for( auto& t : threads){
t.join();
}
threads[th++] = thread(stage_1_multiply,&A[0][0],&B[0][2],&D_1[0][2]);
threads[th++] = thread(stage_1_multiply,&A[0][2],&B[2][2],&D_2[0][2]);
threads[th++] = thread(stage_1_multiply,&A[2][0],&B[0][0],&D_1[2][0]);
threads[th++] = thread(stage_1_multiply,&A[2][2],&B[2][0],&D_2[2][0]);
for( auto& t : threads){
t.join();
}
// code to add The Matrices D_1 and D_2 goes here.
for(int i = 0;i<4;i++){
for(int j = 0;j< 4;j++){
cout << D_1[i][j] << " ";
}
cout << endl;
}
cout << " Main Close "<<endl;
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#定义n4
使用名称空间std;
互斥mu;
无效阶段1乘以(int*a,int*b,int*d){
int*xij;
int*yij;
int*zij;
int COLS=N,ROWS=N;
设置图片链接不是一个好主意。至少让它们内联,或者更好的是仍然有图片链接text@EdHeal很抱歉,这是我的第一篇帖子。