高性能计算实验——基于OpenMP的矩阵乘法

运行Hello,world

#include 
#include 
int main(int argc, char **argv)
{
    int nthreads, thread_id;
    printf("I am the main thread.\n");
#pragma omp parallel private(nthreads, thread_id)
    {
        nthreads = omp_get_num_threads();
        thread_id = omp_get_thread_num();
        printf("Hello. I am thread %d out of a team of %d\n", thread_id, nthreads);
    }
    printf("Here I am, back to the main thread.\n");
    return 0;
}

输出结果

因为电脑是12核的,所以nthreads=12,因此每个进程都会执行一次#pragma omp parallel private(nthreads, thread_id)下边的内容,因而输出12次
高性能计算实验——基于OpenMP的矩阵乘法_第1张图片



矩阵乘法代码

并行计算核心代码

使用多线程并行计算,#pragma omp parallel for num_threads(64)使用64个线程计算,他将下边for循环分为64份,并行执行

void matrixMultiOMP()
{
    #pragma omp parallel for num_threads(64)
    for (int row = 0; row < MatrixOrder; row++)
    {
        for (int col = 0; col < MatrixOrder; col++)
        {
            matrixMultiResult[row][col] = calcuPartOfMatrixMulti(row, col);
        }
    }
}

完整代码

#include 
#include  // OpenMP编程需要包含的头文件
#include 
#include 
using namespace std;
#define MatrixOrder 1024
#define FactorIntToDouble 1.1; //使用rand()函数产生int型随机数,将其乘以因子转化为double型;
double firstParaMatrix[MatrixOrder][MatrixOrder] = {0.0};
double secondParaMatrix[MatrixOrder][MatrixOrder] = {0.0};
double matrixMultiResult[MatrixOrder][MatrixOrder] = {0.0};

//计算matrixMultiResult[row][col]

double calcuPartOfMatrixMulti(int row, int col)
{
    double resultValue = 0;
    for (int transNumber = 0; transNumber < MatrixOrder; transNumber++)
    {
        resultValue += firstParaMatrix[row][transNumber] * secondParaMatrix[transNumber][col];
    }
    return resultValue;
}


// 使用随机数为乘数矩阵和被乘数矩阵赋double型初值 
void matrixInit()
{
    for (int row = 0; row < MatrixOrder; row++)
    {
        for (int col = 0; col < MatrixOrder; col++)
        {
            srand(row + col);
            firstParaMatrix[row][col] = (rand() % 10) * FactorIntToDouble;
            secondParaMatrix[row][col] = (rand() % 10) * FactorIntToDouble;
        }
    }
}

/*
 实现矩阵相乘
*/
void matrixMulti()
{
    for (int row = 0; row < MatrixOrder; row++)
    {
        for (int col = 0; col < MatrixOrder; col++)
        {
            matrixMultiResult[row][col] = calcuPartOfMatrixMulti(row, col);
        }
    }
}

// 多线程实现矩阵相乘
void matrixMultiOMP()
{
    #pragma omp parallel for num_threads(64)
    for (int row = 0; row < MatrixOrder; row++)
    {
        for (int col = 0; col < MatrixOrder; col++)
        {
            matrixMultiResult[row][col] = calcuPartOfMatrixMulti(row, col);
        }
    }
}

int main()
{
    matrixInit();

    clock_t t1 = clock(); //开始计时;
    matrixMulti();
    clock_t t2 = clock(); //结束计时

    cout << "time: " << t2 - t1 << endl;

    return 0;
}

你可能感兴趣的:(矩阵,c++,算法)