一.  矩阵乘法串行实现

例子选择两个1024*1024的矩阵相乘,根据矩阵乘法运算得到运算结果。其中,两个矩阵中的数为double类型,初值由随机数函数产生。代码如下:

#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 = ;
for(int transNumber = ; transNumber < MatrixOrder ; transNumber++) {
resultValue += firstParaMatrix [row] [transNumber] * secondParaMatrix [transNumber] [col] ;
}
return resultValue;
} /* * * * * * * * * * * * * * * * * * * * * * * * *
* 使用随机数为乘数矩阵和被乘数矩阵赋double型初值 *
* * * * * * * * * * * * * * * * * * * * * * * * */
void matrixInit()
{
for(int row = ; row < MatrixOrder ; row++ ) {
for(int col = ; col < MatrixOrder ;col++){
srand(row+col);
firstParaMatrix [row] [col] = ( rand() % ) * FactorIntToDouble;
secondParaMatrix [row] [col] = ( rand() % ) * FactorIntToDouble;
}
}
} /* * * * * * * * * * * * * * * * * * * * * * *
* 实现矩阵相乘 *
* * * * * * * * * * * * * * * * * * * * * * * */
void matrixMulti()
{
for(int row = ; row < MatrixOrder ; row++){
for(int col = ; 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; system("pause"); return ;
}

二  矩阵乘法并行实现

使用#pragma omp parallel for为for循环添加并行,使用num_threads()函数指定并行线程数。

使用VS2010编译,需要先在项目属性中选择支持openmp,在头文件中包含<omp.h>即可使用openmp为矩阵乘法实现并行。

代码如下:

#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 = ;
for(int transNumber = ; transNumber < MatrixOrder ; transNumber++) {
resultValue += firstParaMatrix [row] [transNumber] * secondParaMatrix [transNumber] [col] ;
}
return resultValue;
} /* * * * * * * * * * * * * * * * * * * * * * * * *
* 使用随机数为乘数矩阵和被乘数矩阵赋double型初值 *
* * * * * * * * * * * * * * * * * * * * * * * * */
void matrixInit()
{
#pragma omp parallel for num_threads(64)
for(int row = ; row < MatrixOrder ; row++ ) {
for(int col = ; col < MatrixOrder ;col++){
srand(row+col);
firstParaMatrix [row] [col] = ( rand() % ) * FactorIntToDouble;
secondParaMatrix [row] [col] = ( rand() % ) * FactorIntToDouble;
}
}
//#pragma omp barrier
} /* * * * * * * * * * * * * * * * * * * * * * *
* 实现矩阵相乘 *
* * * * * * * * * * * * * * * * * * * * * * * */
void matrixMulti()
{ #pragma omp parallel for num_threads(64)
for(int row = ; row < MatrixOrder ; row++){
for(int col = ; col < MatrixOrder ; col++){
matrixMultiResult [row] [col] = calcuPartOfMatrixMulti (row,col);
}
}
//#pragma omp barrier
} int main()
{
matrixInit(); clock_t t1 = clock(); //开始计时;
matrixMulti();
clock_t t2 = clock(); //结束计时
cout<<"time: "<<t2-t1<<endl; system("pause"); return ;
}

三 效率对比

运行以上两种方法,对比程序运行时间。

当矩阵阶数为1024时,串行和并行中矩阵乘法耗费时间如下:

串行:

并行:

可看出,阶数为1024时并行花费的时间大约是串行的五分之一。

当改变矩阵阶数,并行和串行所花费时间如下:

128

256

512

1024

2048

并行

0

31

164

3491

43203

串行

16

100

516

15584

134818

画成折线图如下:

加速比曲线如下(将串行时间除以并行时间):

从以上图表可以看出当矩阵规模不大(阶数小于500)时,并行算法与串行算法差距不大,当阶数到达1000、2000时,差距就非常明显。而且,并非随着矩阵规模越大,加速比就会越大。在本机硬件条件下,并行线程数为64时,大约在1024附近会有较高加速比。

四 矩阵分块相乘并行算法

将矩阵乘法的计算转化为其各自分块矩阵相乘而后相加,能够有效减少乘数矩阵和被乘数矩阵调入内存的次数,可加快程序执行。

代码如下:

#include <iostream>
#include <omp.h> // OpenMP编程需要包含的头文件
#include <time.h>
#include <stdlib.h> using namespace std; #define MatrixOrder 2048
#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}; /* * * * * * * * * * * * * * * * * * * * * * * * *
* 使用随机数为乘数矩阵和被乘数矩阵赋double型初值 *
* * * * * * * * * * * * * * * * * * * * * * * * */
void matrixInit()
{
//#pragma omp parallel for num_threads(64)
for(int row = ; row < MatrixOrder ; row++ ) {
for(int col = ; col < MatrixOrder ;col++){
srand(row+col);
firstParaMatrix [row] [col] = ( rand() % ) * FactorIntToDouble;
secondParaMatrix [row] [col] = ( rand() % ) * FactorIntToDouble;
}
}
//#pragma omp barrier
} void smallMatrixMult (int upperOfRow , int bottomOfRow ,
int leftOfCol , int rightOfCol ,
int transLeft ,int transRight )
{
int row=upperOfRow;
int col=leftOfCol;
int trans=transLeft; #pragma omp parallel for num_threads(64)
for(int row = upperOfRow ; row <= bottomOfRow ; row++){
for(int col = leftOfCol ; col < rightOfCol ; col++){
for(int trans = transLeft ; trans <= transRight ; trans++){
matrixMultiResult [row] [col] += firstParaMatrix [row] [trans] * secondParaMatrix [trans] [col] ;
}
}
}
//#pragma omp barrier
} /* * * * * * * * * * * * * * * * * * * * * * *
* 实现矩阵相乘 *
* * * * * * * * * * * * * * * * * * * * * * * */
void matrixMulti(int upperOfRow , int bottomOfRow ,
int leftOfCol , int rightOfCol ,
int transLeft ,int transRight )
{
if ( ( bottomOfRow - upperOfRow ) < )
smallMatrixMult ( upperOfRow , bottomOfRow ,
leftOfCol , rightOfCol ,
transLeft , transRight ); else
{
#pragma omp task
{
matrixMulti( upperOfRow , ( upperOfRow + bottomOfRow ) / ,
leftOfCol , ( leftOfCol + rightOfCol ) / ,
transLeft , ( transLeft + transRight ) / );
matrixMulti( upperOfRow , ( upperOfRow + bottomOfRow ) / ,
leftOfCol , ( leftOfCol + rightOfCol ) / ,
( transLeft + transRight ) / + , transRight );
} #pragma omp task
{
matrixMulti( upperOfRow , ( upperOfRow + bottomOfRow ) / ,
( leftOfCol + rightOfCol ) / + , rightOfCol ,
transLeft , ( transLeft + transRight ) / );
matrixMulti( upperOfRow , ( upperOfRow + bottomOfRow ) / ,
( leftOfCol + rightOfCol ) / + , rightOfCol ,
( transLeft + transRight ) / + , transRight );
} #pragma omp task
{
matrixMulti( ( upperOfRow + bottomOfRow ) / + , bottomOfRow ,
leftOfCol , ( leftOfCol + rightOfCol ) / ,
transLeft , ( transLeft + transRight ) / );
matrixMulti( ( upperOfRow + bottomOfRow ) / + , bottomOfRow ,
leftOfCol , ( leftOfCol + rightOfCol ) / ,
( transLeft + transRight ) / + , transRight );
} #pragma omp task
{
matrixMulti( ( upperOfRow + bottomOfRow ) / + , bottomOfRow ,
( leftOfCol + rightOfCol ) / + , rightOfCol ,
transLeft , ( transLeft + transRight ) / );
matrixMulti( ( upperOfRow + bottomOfRow ) / + , bottomOfRow ,
( leftOfCol + rightOfCol ) / + , rightOfCol ,
( transLeft + transRight ) / + , transRight );
} #pragma omp taskwait
}
} int main()
{
matrixInit(); clock_t t1 = clock(); //开始计时; //smallMatrixMult( 0 , MatrixOrder - 1 , 0 , MatrixOrder -1 , 0 , MatrixOrder -1 );
matrixMulti( , MatrixOrder - , , MatrixOrder - , , MatrixOrder - ); clock_t t2 = clock(); //结束计时
cout<<"time: "<<t2-t1<<endl; system("pause"); return ;
}

  由于task是openmp 3.0版本支持的特性,尚不支持VS2010,2013,2015,支持的编译器包括GCC,PGI,INTEL等。

程序大致框架与前面并行算法区别不大,只是将计算的矩阵大小约定为512,大于512的矩阵就分块,直到小于512.具体大小可根据实际情况而定。

五  小结

  本文首先实现基于串行算法的高阶矩阵相乘和基于OpenMP的并行算法的高阶矩阵相乘。接着,对比了128,256,512,1024,2049阶数下,两种算法的耗费时间,并通过表格和曲线图的形式直观表现时间的差别,发现,两种算法并非随着阶数的增大,加速比一直增大,具体原因应该和本机运行环境有关。最后,根据矩阵分块能有效减少数据加载进内存次数,完成了矩阵分块相乘并行算法的代码。考虑到编译环境的限制,未及时将结果运行出来。下一步可装linux虚拟机,使用gcc编译器得出算法的运行时间,进行进一步的分析对比。

基于OpenMP的矩阵乘法实现及效率提升分析的更多相关文章

  1. 基于MapReduce的矩阵乘法

    参考:http://blog.csdn.net/xyilu/article/details/9066973文章 文字未得及得总结,明天再写文字,先贴代码 package matrix; import ...

  2. 【BZOJ-4386】Wycieczki DP + 矩阵乘法

    4386: [POI2015]Wycieczki Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 197  Solved: 49[Submit][Sta ...

  3. 基于MPI的大规模矩阵乘法问题

    转载请注明出处. /* Function:C++实现并行矩阵乘法; Time: 19/03/25; Writer:ZhiHong Cc; */ 运行方法:切到工程文件x64\Debug文件下,打开命令 ...

  4. [转]OpenBLAS项目与矩阵乘法优化

    课程内容 OpenBLAS项目介绍 矩阵乘法优化算法 一步步调优实现 以下为公开课完整视频,共64分钟: 以下为公开课内容的文字及 PPT 整理. 雷锋网的朋友们大家好,我是张先轶,今天主要介绍一下我 ...

  5. OpenGL学习进程(12)第九课:矩阵乘法实现3D变换

    本节是OpenGL学习的第九个课时,下面将详细介绍OpenGL的多种3D变换和如何操作矩阵堆栈.     (1)3D变换: OpenGL中绘制3D世界的空间变换包括:模型变换.视图变换.投影变换和视口 ...

  6. 4-2.矩阵乘法的Strassen算法详解

    题目描述 请编程实现矩阵乘法,并考虑当矩阵规模较大时的优化方法. 思路分析 根据wikipedia上的介绍:两个矩阵的乘法仅当第一个矩阵B的列数和另一个矩阵A的行数相等时才能定义.如A是m×n矩阵和B ...

  7. 2.3CUDA矩阵乘法

    CPU 矩阵乘法 能相乘的两个矩阵,必须满足一个矩阵的行数和第二个矩阵的列数相同. A(N*P) * B(P*M) = C(N*M). 其中P是行数,N是列数, 从宽高的角度来说,即 A的宽度和B的高 ...

  8. poj3233之经典矩阵乘法

    Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 12346   Accepted:  ...

  9. MapReduce实现矩阵乘法

    简单回想一下矩阵乘法: 矩阵乘法要求左矩阵的列数与右矩阵的行数相等.m×n的矩阵A,与n×p的矩阵B相乘,结果为m×p的矩阵C.具体内容能够查看:矩阵乘法. 为了方便描写叙述,先进行如果: 矩阵A的行 ...

随机推荐

  1. SID与GUID的区别

    1.在AD里面创建一个用户或者组都会为其分配一个SID,同时也会为这些对象分配一个GUID,GUID是一个128位的字符串,一个标识符,GUID不仅在整个域里面是唯一的,并且在全世界的范围内都是唯一的 ...

  2. C# 事务处理

    前言: 通常SqlHelper类为了方便处理,做成了静态类,静态类的问题是不方便添加事务处理. 实例化类方便添加事务处理,DoTrans/CommitTrans/RollBackTrans  三个函数 ...

  3. Longest Common Prefix [LeetCode 14]

    1- 问题描述 Write a function to find the longest common prefix string amongst an array of strings. 2- 思路 ...

  4. jQuery下拉友情链接美化效果代码分享

    这篇文章主要介绍了jQuery下拉友情链接美化效果,很实用的代码,推荐给大家,有需要的小伙伴可以参考下. 文实例讲述了jQuery下拉友情链接美化效果,jQuery下拉友情链接美化代码是一款基于jQu ...

  5. POJ C++程序设计 编程作业—类和对象 编程题#3

    编程题 #3 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 下面程序的输出 ...

  6. 64位操作系统弹出"Failed to load the JNI shared library “E:/2000/Java/JDK6/bin/..jre/bin/client/jvm.dll”

    64位操作系统弹出"Failed to load the JNI shared library /..jre/bin/client/jvm.dll”,最大的可能就是jdk的版本问题.去你的C ...

  7. SQL Server编程(05)游标

    在关系数据库中,我们对于查询的思考是面向集合的.而游标打破了这一规则,游标使得我们思考方式变为逐行进行.对于类C的开发人员来着,这样的思考方式会更加舒服. 正常面向集合的思维方式是: 而对于游标来说: ...

  8. 基于zookeeper的远程方法调用(RMI)的实现

    采用zookeeper的命名服务,采用不同的目录结构存储不同模块不同服务的rmi的url,使用key来对应不同的服务.同时采用zookeeper解决了单点问题. 当有两个相同的服务注册时,因为采用的是 ...

  9. 前端javascript发送ajax请求、后台书写function小案例

    HTML端页面: <td> <input class="pp_text" type="text" name="" valu ...

  10. Android中内容观察者的使用---- ContentObserver类详解

    详解:http://blog.csdn.net/qinjuning/article/details/7047607