我需要执行尺寸为10,000 X 10,000的两个矩阵的矩阵乘法,每个元素从1到10,000的范围内随机生成。我需要使用线程(25个线程)并且没有线程来比较时间。 我使用简单的矩阵乘法算法O(n ^ 3)。 没有线程的程序一直在执行(超过一天),当我尝试运行它时,线程版本会中止。 它适用于1000 X 1000 Matrix
我在大学服务器上使用CC prog.cc -lpthread -lposix4编译它 这是非线程版本
/ * 编译器:Unix服务器
该程序在没有线程的情况下执行两个10000 * 10000矩阵的矩阵乘法 该程序的目的是使用线程演示性能增益 反对不使用线程对不同的数据执行相同的计算。 * /
#include <pthread.h>
#include <iostream.h>
#include <semaphore.h>
#include <unistd.h>
#include<math.h>
int main()
{
double **A;//Matrix A
double **B;//Matrix B
double **C;//Output Matrix C
const int MATRIX_DIMENSION = 5000;
//Assign Matrix A first dimension
//-----------------------------------------------------------
A = new double*[MATRIX_DIMENSION];
//Assign second dimension
for(int i = 0; i < MATRIX_DIMENSION; i++)
{
A[i] = new double[MATRIX_DIMENSION];
}
//Assign Matrix B first dimension
B = new double*[MATRIX_DIMENSION];
//Assign second dimension
for(int i = 0; i < MATRIX_DIMENSION; i++)
{
B[i] = new double[MATRIX_DIMENSION];
}
//Assign Matrix C first dimension
C = new double*[MATRIX_DIMENSION];
//Assign second dimension
for(int i = 0; i < MATRIX_DIMENSION; i++)
{
C[i] = new double[MATRIX_DIMENSION];
}
//-----------------------------------------------------------
//Generate random numbers for matrices A and B and assign C to 0
for(int i=0;i<MATRIX_DIMENSION;i++)
{
for(int j=0;j<MATRIX_DIMENSION;j++)
{
A[i][j] = rand() % 10000;
B[i][j] = rand() % 10000;
C[i][j] = 0; // initialize C to zero
}
}
//-----------------------------------------------------------
//Do the matrix multiplication
for(int i=0;i<MATRIX_DIMENSION;i++)
{
for(int j=0 ;j<MATRIX_DIMENSION; j++)
{
for(int k=0;k<MATRIX_DIMENSION;k++)
{
C[i][j]+=A[i][k]*B[k][j];
}
}
}
//-----------------------------------------------------------
//delete the dynamic memory of A
for (int i = 0; i < MATRIX_DIMENSION; i++)
{
delete[] A[i];
}
delete[] A;
//delete the dynamic memory of B
for (int i = 0; i < MATRIX_DIMENSION; i++)
{
delete[] B[i];
}
delete[] B;
//delete the dynamic memory of C
for (int i = 0; i < MATRIX_DIMENSION; i++)
{
delete[] C[i];
}
delete[] C;
//-----------------------------------------------------------
return(0);
}
这是线程版本 / * 名称: 编译器:Unix服务器
该程序在没有线程的情况下执行两个10000 * 10000矩阵的矩阵乘法 该程序的目的是使用线程演示性能增益 反对不使用线程对不同的数据执行相同的计算。 * /
#include <pthread.h>
#include <iostream.h>
#include <semaphore.h>
#include <unistd.h>
#include<math.h>
//Global variables
double **A;//Matrix A
double **B;//Matrix B
double **C;//Output Matrix C
const int MATRIX_DIMENSION = 10000; //We need a 10000 X 10000 Matrix
const int NUM_THREADS = 25; // One thread completes 1/25th of the work
const int THREAD_DIMENSION = MATRIX_DIMENSION / NUM_THREADS; //Array that each thread will operate on
pthread_t * thread[NUM_THREADS];
/***************************************************************************
Function that does matrix multiplication of 1/25th of the whole matrix,
The division is done by dividing the Matrix into row's all 1/25 of the total matrix
Each row of Matrix A operates on all the columns of Matrix B to get corresponding elements of Matrix C
Parameter : arg, this is used as and index for which part of the Matrix this particular thread operates on
Return type: void
****************************************************************************/
void *MatrixMul (void * arg)
{
int index;
index = (int) arg;
int operation_Lower_Limit = ((index+1) * THREAD_DIMENSION) - THREAD_DIMENSION ; //Multiplication starting row
int operation_Upper_Limit = ((index+1) * THREAD_DIMENSION) - 1; //Multiplication ending row
for(int i=operation_Lower_Limit;i<=operation_Upper_Limit;i++) //only 1/25th of Matrix A is used
{
for(int j=0 ;j<MATRIX_DIMENSION; j++) // The whole B matrix is used
{
for(int k=0;k<MATRIX_DIMENSION;k++)
{
C[i][j]+=A[i][k]*B[k][j];
}
}
}
return NULL;
}
int main()
{
srand(time(0));
//Assign memory for threads
for(int i=0;i < NUM_THREADS;i++)
{
thread[i] = new pthread_t;
}
//Assign Matrix A first dimension
//-----------------------------------------------------------
A = new double*[MATRIX_DIMENSION];
//Assign second dimension
for(int i = 0; i < MATRIX_DIMENSION; i++)
{
A[i] = new double[MATRIX_DIMENSION];
}
//Assign Matrix B first dimension
B = new double*[MATRIX_DIMENSION];
//Assign second dimension
for(int i = 0; i < MATRIX_DIMENSION; i++)
{
B[i] = new double[MATRIX_DIMENSION];
}
//Assign Matrix C first dimension
C = new double*[MATRIX_DIMENSION];
//Assign second dimension
for(int i = 0; i < MATRIX_DIMENSION; i++)
{
C[i] = new double[MATRIX_DIMENSION];
}
//-----------------------------------------------------------
//Generate random numbers for matrices A and B and assign C to 0
for(int i=0;i<MATRIX_DIMENSION;i++)
{
for(int j=0;j<MATRIX_DIMENSION;j++)
{
A[i][j] = rand() % 10000;
B[i][j] = rand() % 10000;
C[i][j] = 0; // initialize C to zero
}
}
//-----------------------------------------------------------
//Do the matrix multiplication
for(int i=0;i<NUM_THREADS;i++)
{
pthread_create( thread[ i ], NULL, (MatrixMul), (void *) (i) );
}
//wait for all the threads to complete execution
for(int i=0;i<NUM_THREADS;i++)
{
pthread_join(*thread[i],NULL);
}
//-----------------------------------------------------------
//delete the dynamic memory of A
for (int i = 0; i < MATRIX_DIMENSION; i++)
{
delete[] A[i];
}
delete[] A;
//delete the dynamic memory of B
for (int i = 0; i < MATRIX_DIMENSION; i++)
{
delete[] B[i];
}
delete[] B;
//delete the dynamic memory of C
for (int i = 0; i < MATRIX_DIMENSION; i++)
{
delete[] C[i];
}
delete[] C;
//-----------------------------------------------------------
return(0);
}
答案 0 :(得分:0)
你的程序内存不足吗?如果是这样,你可能在执行乘法时释放一些内存?