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

运行Hello,world

#include <stdio.h>
#include <omp.h>
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次
在这里插入图片描述



矩阵乘法代码

并行计算核心代码

使用多线程并行计算,#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 <iostream>
#include <omp.h> // OpenMP编程需要包含的头文件
#include <time.h>
#include <stdlib.h>
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;
}
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值