ELL格式稀疏矩阵向量乘法
#include <iostream>
#include <algorithm>
#include <vector>
#include <chrono>
#include <time.h>
#include <stdio.h>
using namespace std;
//ELL格式的矩阵
//value保存数组
void smvELL(float* value, int numRows, float*x , int* colIdx,float* r,int columnsPerRow, int stride)
{
//遍历行
for (int i = 0; i < numRows; i++)
{
float sum = r[i];
for (int n = 0; n < columnsPerRow; n++)
{
int offset = stride * i + n;
int col = colIdx[offset];
if (col != -1)
{
sum += value[offset] * x[col];//列向量
}
}
r[i] = sum;
}
}
void smvELL_unroll(float* value, int numRows, float*x, int* colIdx, float* r, int columnsPerRow, int stride)//外层循环展开
{
//遍历行
const int numUnroll = 2;
for (int row = 0; row < numRows; row+= numUnroll)
{
float sum[numUnroll];
for (int i = 0; i < numUnroll; i++)
{
sum[i] = r[i + row];
}
for (int i = 0; i < numUnroll; i++)//i与n的循环是可以交换的
{
for (int n = 0; n < columnsPerRow; n++)
{
int offset = (row + i) * stride + n;
int col = colIdx[offset];
if (col != -1)
{
sum[i] += value[offset] * x[col];//列向量
}
}
}
for (int i = 0; i < numUnroll; i++)
{
r[i + row] = sum[i];
}
}
}
int main()
{
}