#include<stdio.h>
#include<omp.h>
#include<stdlib.h>
#include<math.h>
#include <iostream>
using namespace std;
inline void smmSerial(const int numRows,const int *rowIndex,
const int *columnIndex,const float *val,const float *x,float *r)
{
int rowStart;
int rowEnd;double t0 = omp_get_wtime();
#pragma omp parallel for schedule(dynamic)
for(int i=0;i<numRows;i++){
rowStart = rowIndex[i];
rowEnd = rowIndex[i+1];
float sum = 0.0f;
for(int j=rowStart;j<rowEnd;j++){
sum += val[j] * x[columnIndex[j]];
}
r[i] = sum;
}
double t1 = omp_get_wtime();
double T1 = (t1-t0)*1000;
printf("优化后运行耗时: %lf ms\n", T1);
}
int main(){
const int rowIndex[5]={0,2,4,7,8};
const int columnIndex[8]={0,1,1,3,2,3,4,5};
const float val[8]={10,20,30,40,50,60,70,80};
const int numRows=4;
const float x[6]={1,2,3,4,5,6};
float r[4];
smmSerial(numRows,rowIndex,columnIndex,val,x,r);
return 0;
}
原文链接: https://www.cnblogs.com/sunyang13763857269/p/17071263.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/314394
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!