I’ve got a program that uses a few CUDA kernels and this one is taking 50-100ms to run while the others take 0-5ms. I expect this has something to do with all the branching, but I’m not really sure how to reduce it. I’m compiling for a compute capability 2.1 device. If someone could point me in the right direction that would be great.
// chosen using occupancy spreadsheet
#define SCORE_THREADS_PER_BLOCK 448
__device__ double ScoringMatrixVal(double *scoring_matrix, size_t pitch, unsigned int row, unsigned int column) {
return *((double*)((char*) scoring_matrix + row * pitch) + column);
}
__global__ void ScoreBindingSites(char *input_sequence, unsigned long is_length, unsigned int *rvd_sequence, unsigned int rs_len, double cutoff, unsigned int rvd_num, double *scoring_matrix, size_t sm_pitch, unsigned char *results) {
int block_seq_index = SCORE_THREADS_PER_BLOCK * (blockIdx.y * gridDim.x + blockIdx.x);
int thread_id = (blockDim.x * threadIdx.y) + threadIdx.x;
int seq_index = block_seq_index + thread_id;
if (seq_index < 1 || seq_index >= is_length || seq_index + rs_len >= is_length - 1) return;
if (input_sequence[seq_index - 1] == 'T' || input_sequence[seq_index - 1] == 't') {
double thread_result = 0;
for (int i = 0; i < rs_len; i++) {
int rvd_index = i;
int sm_col = 4;
char base = input_sequence[seq_index + i];
if (base == 'A' || base == 'a')
sm_col = 0;
if (base == 'C' || base == 'c')
sm_col = 1;
if (base == 'G' || base == 'g')
sm_col = 2;
if (base == 'T' || base == 't')
sm_col = 3;
thread_result += ScoringMatrixVal(scoring_matrix, sm_pitch, rvd_sequence[rvd_index], sm_col);
}
results[seq_index] |= (thread_result < cutoff ? 1UL : 0UL) << (2 * rvd_num);
}
if (input_sequence[seq_index + rs_len] == 'A' || input_sequence[seq_index + rs_len] == 'a') {
double thread_result = 0;
for (int i = 0; i < rs_len; i++) {
int rvd_index = rs_len - i - 1;
int sm_col = 4;
char base = input_sequence[seq_index + i];
if (base == 'A' || base == 'a')
sm_col = 3;
if (base == 'C' || base == 'c')
sm_col = 2;
if (base == 'G' || base == 'g')
sm_col = 1;
if (base == 'T' || base == 't')
sm_col = 0;
thread_result += ScoringMatrixVal(scoring_matrix, sm_pitch, rvd_sequence[rvd_index], sm_col);
}
results[seq_index] |= (thread_result < cutoff ? 1UL : 0UL) << (2 * rvd_num + 1);
}
}
ScoreBindingSites is launched with (32, 14) threads per block and enough blocks to cover the input sequence. The full source can be found here if that would be helpful.
There are a few things you can do to improve this code:
As has been suggested above, merge the two loops for
'T'and'A'. This is probably your greatest source of branch divergence as the small cascade ofif-statements inside the loop will very probably be compiled as predicated instructions (see Section 5.4.2 of the NVidia CUDA C Programming guide).Byte-sized global memory access is a terrible idea. Instead, I would suggest declaring
input_sequence,resultsandbaseaschar4and, in each iteration of your main loop, do your thing for each value ofbase.x,base.y,base.zandbase.w.You may also want to have a closer look at what
ScoringMatrixValis doing. Is it just reading values from memory? If so, could you replace it with constant memory? Or a texture?Update
As requested, here is what I meant with the second point. I haven’t tested the code, though, so feel free to keep any bugs or typos you find. Note that I’ve assumed, for simplicity, that
rs_lenis a multiple of four.A few notes:
ScoringMatrixValto use regular array access, as the whole mess with pointer arithmetic may be throwing the compiler off.if-statements to a cascade ofif-elseif-statements, as they seem mutually exclusive. I’m guessing the compiler will use predicated instructions and will interleave the fourif-elseifblocks.char[256]where everything is set to4except for the character codes atA,a,C,c, etc…if-elseif-statements to a table lookup, you could use two different tables forinput_sequence[seq_index - 1] == 'T'andinput_sequence[seq_index + rs_len] == 'A', thus keeping it all in one loop.I hope I haven’t messed-up the code too much and that this helps!