▶ 各种稀疏矩阵数据结构下 y(n,1) = A(n,m) * x(m,1) 的实现,GPU版本

● MAT 乘法

 __global__ void dotGPU(const MAT *a, const MAT *x, MAT *y)
{
int id = blockIdx.x * blockDim.x + threadIdx.x;
if (id < a->row)
{
format sum = ;
for (int i = ; i < a->col; i++)
sum += a->data[id * a->col + i] * x->data[i];
y->data[id] = sum;
}
if (id == )
{
y->row = a->row;
y->col = x->col;
COUNT_MAT(y);
}
return;
}

● CSR 乘法

 __global__ void dotGPU(const CSR *a, const MAT *x, MAT *y)
{
int id = blockIdx.x * blockDim.x + threadIdx.x;
if (id < a->row)
{
format sum = ;
for (int j = a->ptr[id]; j < a->ptr[id + ]; j++)
sum += a->data[j] * x->data[a->index[j]];
y->data[id] = sum;
}
if (id == )
{
y->row = a->row;
y->col = x->col;
COUNT_MAT(y);
}
return;
}

● ELL 乘法

 __global__ void dotGPU(const ELL *a, const MAT *x, MAT *y)
{
int id = blockIdx.x * blockDim.x + threadIdx.x;
if (id < a->col)
{
format sum = ;
for (int j = ; j < a->row; j++)
sum += a->data[id + j * a->col] * (a->index[id + j * a->col] < ? : x->data[a->index[id + j * a->col]]);
y->data[id] = sum;
}
if (id == )
{
y->row = a->col;
y->col = x->col;
COUNT_MAT(y);
}
return;
}

● COO 乘法

 __global__ void dotGPU(const ELL *a, const MAT *x, MAT *y)// GPU ELL乘法
{
int id = blockIdx.x * blockDim.x + threadIdx.x;
if (id < a->col)
{
format sum = ;
for (int j = ; j < a->row; j++)
sum += a->data[id + j * a->col] * (a->index[id + j * a->col] < ? : x->data[a->index[id + j * a->col]]);
y->data[id] = sum;
}
if (id == )
{
y->row = a->col;
y->col = x->col;
COUNT_MAT(y);
}
return;
}

● DIA 乘法,留坑

04-28 13:23