fortran使用mkl函数库中的mkl_dcsrsymv计算矩阵与向量的乘积
用fortran语言编写数值程序时,如果要计算一个大型稀疏矩阵与一个向量的乘积,可以使用下面高效的方法
1. 首先使用CSR格式大型稀疏矩阵进行存储
2. 调用mkl函数库中的mkl_dcsrsymv计算
这样做的目的不仅可以节约内存,而且计算速度也比较快示例代码如下:
program mkl_symvimplicit noneinteger, parameter :: n = 5 !// depend on your equationinteger :: i, j, mm, tmp, fileid, first, numreal(kind=8) :: matrix(n,n), x(n), y(n)real(kind=8), allocatable :: csra(:)integer, allocatable :: ia(:), ja(:)character(len=1) :: uplo = 'u'type :: nodereal(kind=8) :: sinteger :: k1, k2type (node), pointer :: nextend type node type (node), pointer :: head, tail, p, q open ( newunit = fileid, file = 'SparseMatrix.txt' ) !// The sparse matrix data needs to be given by itselfdo i = 1, nread ( fileid,* ) matrix(i,:)end doread( fileid,* ) xclose ( fileid )!// =========================store data by CSR format=======================first = 1if ( .not.associated(p) ) thenallocate( p )q => pnullify( p%next )q%k2 = firsttmp = q%k2end ifnum = 0 !// calculate the number of no-zerodo i = 1, nmm = 0 !// calculate the position of no-zero in every rowdo j = i, nIf ( matrix(i,j) /= 0.0 ) thenif ( .not.associated(head) ) thenallocate( head )tail => headnullify( tail%next )tail%s = matrix(i,j)tail%k1 = jnum = num + 1mm = mm + 1elseallocate( tail%next )tail => tail%nexttail%s = matrix(i,j)tail%k1 = jnum = num + 1 mm = mm + 1 nullify( tail%next )end ifEnd ifend doif ( associated(p) ) thenallocate( q%next )q => q%nextq%k2 = tmp + mmtmp = q%k2nullify( q%next )end ifend doallocate( csra(num), ja(num), ia(n+1) ) !// store the no-zero element!// output a and jatail => headj = 1do if ( .not.associated(tail) ) exitcsra(j) = tail%sja(j) = tail%k1j = j + 1tail => tail%nextend do!// output iaq => pj = 1do if ( .not.associated(q) ) exitia(j) = q%k2j = j + 1q => q%nextend donullify( head, tail, p, q )write ( *,'(a)' ) '>> Original data'write ( *,'(5f7.2)' ) matrixwrite ( *,'(a)' ) '>> CSR data'write ( *,'(*(f7.2))' ) csrawrite ( *,'(a)' ) '>> X data'write ( *,'(*(f7.2))' ) xwrite ( *,'(a)' ) '>> Colnum of CSR data'write ( *,'(*(i4))' ) jawrite ( *,'(a)' ) '>> The position of CSR data'write ( *,'(*(i4))' ) iawrite ( *,'(1x,a)' ) "The result of matmul function is..."write ( *,'(*(f9.3))' ) matmul(matrix,x)call mkl_dcsrsymv( uplo, n, csra, ia, ja, x, y )print*write ( *,'(1x,a)' ) "The result of mkl_dcsrsymv is..."write ( *,'(*(f9.3))' ) yend program mkl_symv结果如下:
>> Original data1.00 -1.00 0.00 -3.00 0.00-1.00 5.00 0.00 0.00 0.000.00 0.00 4.00 6.00 4.00-3.00 0.00 6.00 7.00 0.000.00 0.00 4.00 0.00 -5.00
>> CSR data1.00 -1.00 -3.00 5.00 4.00 6.00 4.00 7.00 -5.00
>> X data-13.00 9.00 56.00 43.00 -13.00
>> Colnum of CSR data1 2 4 2 3 4 5 4 5
>> The position of CSR data1 4 5 8 9 10The result of matmul function is...-151.000 58.000 430.000 676.000 289.000The result of mkl_dcsrsymv is...-151.000 58.000 430.000 676.000 289.000
从结果可以看出,计算正确代码中用到的示例数据存储在SparseMatrix.txt中,如下所示:1.00 -1.00 0.00 -3.00 0.00-1.00 5.00 0.00 0.00 0.000.00 0.00 4.00 6.00 4.00-3.00 0.00 6.00 7.00 0.000.00 0.00 4.00 0.00 -5.00-13.00 9.00 56.00 43.00 -13.00
前五行为矩阵m,最后一行为向量v
fortran使用mkl函数库中的mkl_dcsrsymv计算矩阵与向量的乘积相关推荐
- fortran使用MKL函数库中的scal计算一个标量和向量的乘积
下面的代码使用MKL函数库中的scal计算一个标量和向量的乘积 program MKL_dotcuse blas95implicit noneinteger, parameter :: n = 10r ...
- fortran使用MKL函数库求解普通稀疏矩阵与向量的乘积
使用MKL函数库中的mkl_dcsrgemv可以实现普通稀疏矩阵与向量的乘积 program mainimplicit noneinteger :: n, n1, ja(9), ia(7)real*8 ...
- fortran使用MKL函数库计算一个复数向量的共轭与另一个复数向量的内积
下面的代码使用MKL函数库计算了共轭向量与另一个向量的点积 program MKL_dotcuse blas95implicit noneinteger, parameter :: n = 10rea ...
- 转载:如何将一个新函数加到MATLAB函数库中
如何将一个新函数加到MATLAB函数库中 nkszjx2 2017-09-29 11:25:26 13864 收藏 2 分类专栏: matlab 文章标签: matlab 库 </div> ...
- 利用Intel IPP函数库实现信号频谱计算
Intel IPP(Intel Integrated Performance Primitives)函数库是一套跨平台的软件函数库,它为用户提供了一套高效.实用的函数集,可用于实现通信.图像.语音等多 ...
- 剑指offer刷题:javaHZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学。今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解
题目描述 HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学.今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决.但是,如果向量 ...
- 剑指offer_31:HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学。今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决。但
题目:HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学.今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决.但是,如果向量中包 ...
- c++调用gcd函数_c++函数库中一些实用的函数
有一些程序,虽然写起来不难,但是可能比较麻烦或容易出错,这时就可以用c++函数库里自带的一些实用的函数. 这里只记录一些不太常见的函数. ------------------------------- ...
- C语言中string函数库中的一些函数的用法
目录 1.memcpy函数 1.标准形式为 2.所对应的参数 3.该函数的返回值指向的是目标存储区 dist的指针 4.示例 2.memmoer 3.strcpy 1.函数原型 2.所对应的参数 3. ...
最新文章
- iOS 11开发教程(三)运行第一个iOS 11程序
- IntelliJ IDEA的使用操作链接
- c语言变长数组_2018级C语言大作业 - 祖玛
- jmeter ForEach控制器学习
- groovy怎样从sql语句中截取表名_SQl-查询篇
- oracle数据库11g完全卸载,oracle 11g完全卸载
- 路由交换笔记(27)--ACL访问控制列表之练习
- docker 代理_利用Docker容器实现代理转发和数据备份
- WAVE族函数的使用
- android底部显示不出来,Android studio 底部的状态栏不见了如何显示
- mmap java_Java文件映射[Mmap]揭秘 | 学步园
- 2022年最新全国各省五级行政区划代码及mysql数据库代码(省/市/区县/乡镇/村)
- IMX6之SPI接口驱动测试
- python人口普查数据数据分析_Python:第六次全国人口普查数据分析及可视化(pandas、matplotlib)...
- 铁路、公路施工企业劳务实名制管理系统解决方案
- 打印DPI如何与计算机DPI一致,像素英寸与dpi的那些事儿
- 塔夫茨计算机科学,塔夫斯大学计算机科学博士专业详情及要求是怎样的?
- 安卓手机如何把PDF文件转换为JPG图片
- 2款免费的安卓后台游戏
- 蓝桥云课linux入门4:目录结构及文件基本操作