# SpMV implementation problem

**URL:** <https://forums.developer.nvidia.com/t/spmv-implementation-problem/8899>\
**Category:** CUDA Programming and Performance\
**Created:** [April 14, 2009, 7:38pm UTC](https://forums.developer.nvidia.com/t/spmv-implementation-problem/8899 "2009-04-14T19:38:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vvotan](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@vvotan](https://forums.developer.nvidia.com/u/vvotan)\
**Post date:** [April 14, 2009, 7:38pm UTC](https://forums.developer.nvidia.com/t/spmv-implementation-problem/8899/1 "2009-04-14T19:38:43Z")

</div>

I’m trying to implement the sparse vector-matrix multiplication routine as described in “Effcient Sparse Matrix-Vector Multiplication on CUDA” by Bell Garlandy (spmv\_csr\_vector\_kernel function). The source code is pretty similar to that given in the article and it seems to work correctly but it is painfully slow(more than 3 times slower than simple scalar implementation). I wonder, what can be the reason of it? I use GeForce 8800 GT for testing.

the code:

```auto
#define WARP_SZ 32

	__shared__ float vals [THREAD_BLOCK_SIZE*THREAD_BLOCK_SIZE];

	int thread_id = compute_thread_index(); // global thread index

	int warp_id = thread_id / WARP_SZ; // global warp index

	int lane = thread_id & (WARP_SZ - 1); // thread index within the warp

	// one warp per row

	int row = warp_id;

	const int thread_block_id = threadIdx.x + THREAD_BLOCK_SIZE*threadIdx.y;

	if ( row < size_vec)

	{

		uint2 rowptr_bounds = rowptr[row];

		const int row_start	=	rowptr_bounds.x;

		const int row_end	=	rowptr_bounds.y;

		// compute running sum per thread

		vals [thread_block_id] = 0;

		for ( int jj = row_start + lane; jj < row_end; jj += WARP_SZ){

			const float xv = tex1Dfetch(texX,colind[jj]);

			vals [thread_block_id] += matrix [jj] * xv;

		}

		// parallel reduction in shared memory

		if ( lane < 16) vals [thread_block_id] += vals [thread_block_id + 16];

		if ( lane < 8) vals [thread_block_id] += vals [thread_block_id + 8];

		if ( lane < 4) vals [thread_block_id] += vals [thread_block_id + 4];

		if ( lane < 2) vals [thread_block_id] += vals [thread_block_id + 2];

		if ( lane < 1) vals [thread_block_id] += vals [thread_block_id + 1];

		// first thread writes the result

		if (lane == 0){

			b[row] = vals [thread_block_id];

		}

	}

```

---

<div class="post-metadata">

**Author:** ![vvotan](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@vvotan](https://forums.developer.nvidia.com/u/vvotan)\
**Post date:** [April 17, 2009, 4:14pm UTC](https://forums.developer.nvidia.com/t/spmv-implementation-problem/8899/2 "2009-04-17T16:14:31Z")

</div>

Tried it on GTX260. It’s better but still slower than the scalar implementation(thar uses a thread per row rather than a warp per row)

---

<div class="post-metadata">

**Author:** ![jack](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/jack/32/12069_2.png) [@jack](https://forums.developer.nvidia.com/u/jack)\
**Post date:** [April 17, 2009, 5:42pm UTC](https://forums.developer.nvidia.com/t/spmv-implementation-problem/8899/3 "2009-04-17T17:42:14Z")

</div>

I think the semi-official nVidia source for sparse matrix multiplication was posted in the General Computing forum a few months back, if you want to take a look at it.

---

<div class="post-metadata">

**Author:** ![vvotan](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@vvotan](https://forums.developer.nvidia.com/u/vvotan)\
**Post date:** [April 17, 2009, 6:31pm UTC](https://forums.developer.nvidia.com/t/spmv-implementation-problem/8899/4 "2009-04-17T18:31:23Z")

</div>

> [@](#):
>
> I think the semi-official nVidia source for sparse matrix multiplication was posted in the General Computing forum a few months back, if you want to take a look at it.

I’m aware of that. In fact, I’m trying to mix the implementation by Buatois that uses the blocked CRS format with some optimizations from NVIDIA’s paper. And it does not work wery well. But after searching the general computing forum for the topic you pointed out I found an interesting statement

> [@](#):
>
> It’s entirely possible for the CSR\_scalar kernel to be faster. CSR\_scalar is generally faster than CSR\_vector when the number of nonzeros per row is small (say, less than 10). If you look at Figure 27 of the tech report you’ll see that CSR\_vector performance increases as the number of nonzeros per row increases. Conversely, CSR\_scalar performance drops as the number of diagonals increases. The break-even point is somewhere around 9-10 nonzeros per row

maybe it is my case, need to examine the test matrices.
