所以这个程序应该计算一行结果矩阵。但我的代码并没有做到它应该做的事情。
#include <iostream>
#include <fstream>
#include <pthread.h>
#include <ctime>
#include <regex>
using namespace std;
//matrix A and B, result will be store as matrix C
int A[10000][10000], B[10000][10000], C[10000][10000];
int rowFirst, columnFirst, rowSecond, columnSecond;
int numThread;
struct threadLine {
int tid;
int lineNum;
int rowFirst;
int rowSecond;
int columnFirst;
int columnSecond;
};
//function to multiply matrix A and B
void* matrix (void* lineNumber)
{
int i, j, k;
int line = (long)lineNumber;
// Multiplying matrix A and B and store it in matrix C.
// per row
//for(i = 0; i < line; ++i)
//{
for(j = 0; j < columnSecond; ++j)
{
C[i][j] = 0;
for(k = 0; k < columnFirst; ++k)
{
C[i][j] += A[line][k] * B[k][j];
}
//C[i][j] = temp;
}
//}
return 0;
}
int main()
{
//read the file called matrix
ifstream read("matrix.txt");
// if it can read
if (read)
{
read >> rowFirst >> columnFirst; //read the first matrix
//thread ids
pthread_t tid[rowFirst];
struct threadLine tl[rowFirst];
for (int i = 0; i < rowFirst; i++)
{
for(int j = 0; j < columnFirst; j++)
{
read >>A[i][j]; //store it in array A
}
}
read >> rowSecond >> columnSecond; //read the second matrix
if (columnFirst != rowSecond) //column of first matrix must be the same as the second one
{
cout << "column of first matrix must have the same value as row of second matrix" << endl;
return -1;
}
else
{
for (int i = 0; i < rowSecond; i++)
{
for(int j = 0; j < columnSecond; j++)
{
read >>B[i][j]; //store it in array B
}
}
//print input data
cout << "Matrix A: "; //print array A
for (int i = 0; i < rowFirst; i++)
{
for(int j = 0; j < columnFirst; j++)
{
cout << A[i][j] << ' ';
}
cout << endl;
}
cout << "Matrix B: "; //print array B
for (int i = 0; i < rowSecond; i++)
{
for(int j = 0; j < columnSecond; j++)
{
cout << B[i][j] << ' ';
}
cout << endl;
}
cout << endl;
int timeStart = clock(); //start time counting
//loop for creating threads based on number of row (calculate per row)
for (int x = 0; x < rowFirst; x++)
{
int idSuccess = pthread_create(&tid[x], NULL, matrix, (void *) x);
cout << "Created worker thread " << tid[x] << " for row " << x << endl;
//check to see if thread is created sucessfully
if (idSuccess)
{
cout << "Thread creation failed : " << strerror(idSuccess);
return idSuccess;
}
}
cout << endl;
//wait for every threads complete
for (int x = 0; x < rowFirst; x++)
{
pthread_join(tid[x], NULL);
}
int timeStop = clock(); //stop time counting
cout << "Matrix C = A x B: "; //display result
for (int i = 0; i < rowFirst; i++)
{
for(int j = 0; j < columnSecond; j++)
{
cout << C[i][j] << " ";
if(j == columnSecond - 1)
{
cout << endl;
}
}
}
cout << endl;
//print out time
double timeVal = (timeStop -
timeStart)/double(CLOCKS_PER_SEC)*1000;
cout << "Total execution time using " << numThread << " threads is " << timeVal << " ms."<< endl;
}
}
else if(!read)
{
cout << "Unable to read file" << endl;
}
}
我原以为输出会是这样的: 矩阵C = A x B:3 17 10 11
2 -10 -6 -7
16 11 5 -5
4 52 30 27
相反,我得到了这个:
矩阵C = A x B:4 52 30 27
0 0 0 0
0 0 0 0
0 0 0 0
它只计算第一行。我不确定原因是什么。