0

我正在尝试编写一些 CUDA 代码来计算最长的公共子序列。在计算它的单元格的依赖项得到满足之前,我无法弄清楚如何让线程休眠:

IE

// Ignore the spurious maths here, very messy data structures. Planning ahead to strings that are bigger then GPU blocks. i & j are correct though.
int real_i = blockDim.x * blockIdx.x + threadIdx.x;
int real_j = blockDim.y * (max_offset - blockIdx.x) + threadIdx.y;

char i_char = seq1[real_i];
char j_char = seq2[real_j];

// For i & j = 1 to length
if((real_i > 0 && real_j > 0) && (real_i < sequence_length && real_j < sequence_length) {

    printf("i: %d, j: %d\n", real_i, real_j);
    printf("I need to wait for dependancy at i: %d j: %d and i: %d j: %d\n", real_i, (real_j - 1), real_i - 1, real_j);
    printf("Is this true? %d\n", (depend[sequence_length * real_i + (real_j - 1)] && depend[sequence_length * (real_i - 1) + real_j]));

    //WAIT FOR DEPENDENCY TO BE SATISFIED
    //THIS IS WHERE I NEED THE CODE TO HANG
    while( (depend[sequence_length * real_i + (real_j - 1)] == false) && (depend[sequence_length * (real_i - 1) + real_j] == false) ) {
    }

    if (i_char == j_char)
        c[sequence_length * real_i + real_j] = (c[sequence_length * (real_i - 1) + (real_j - 1)]) + 1;
     else
        c[sequence_length * real_i + real_j] = max(c[sequence_length * real_i + (real_j - 1)], c[sequence_length * (real_i - 1) + real_j]);

    // SETTING THESE TO TRUE SHOULD ALLOW OTHER THREADS TO BREAK PAST THE WHILE BLOCK
    depend[sequence_length * real_i + (real_j - 1)] = true;
    depend[sequence_length * (real_i - 1) + real_j] = true;
}

所以基本上线程应该挂在while循环上,直到它的依赖关系在进入计算代码之前被其他线程满足。

我知道“第一个”线程在打印时满足其依赖关系

real i 1, real j 1
I need to wait for dependancy at i: 1 j: 0 and i: 0 j: 1
Is this true? 1

一旦它完成计算,就会将依赖矩阵中的一些单元设置为 true,从而允许另外 2 个线程通过 while 循环并且内核从那里移动。

但是,如果我取消注释 while 循环,我的整个系统将挂起约 10 秒,我得到

the launch timed out and was terminated

有什么建议么?

4

1 回答 1

1

睡觉是个坏主意,最好等待条件变量或互斥锁。

在 GPU 上,每个条件语句都非常昂贵。因此,如果可以,请尝试并行化所有代码。为了确保代码在您可以使用的所有线程中完成__syncthreads()

如果您仍想使用最简单的解决方案,请添加互斥锁,但这通常是个坏主意

于 2013-08-07T10:38:12.130 回答