稀疏矩阵向量乘

#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;
	}
posted @ 2023-01-28 20:58  13763857269  阅读(35)  评论(0)    收藏  举报