Hard
A per-thread SpMV loop with data-dependent trip counts
This kernel processes a sparse matrix in CSR format: each thread handles one row, and rowLength[tid] varies per row (some rows have 2 nonzeros, others have 200). Within a single warp, lane 0 has rowLength == 2 and lane 31 has rowLength == 200; all other lanes fall somewhere in between. What determines this warp's total execution time?
kernel.cu
__global__ void spmv(const int* rowStart, const int* rowLength,
const float* vals, const int* cols,
const float* x, float* y) {
int tid = blockIdx.x * blockDim.x + threadIdx.x;
float sum = 0.0f;
int start = rowStart[tid];
int len = rowLength[tid];
for (int i = 0; i < len; ++i) {
sum += vals[start + i] * x[cols[start + i]];
}
y[tid] = sum;
}4 of 46 in GPU / CUDA