3

これは、例によるcudaの最初の並列コードです。

カーネル呼び出しについて誰か説明してもらえますか:<<< N、1 >>>

これは重要なポイントを持つコードです:

#define N   10

__global__ void add( int *a, int *b, int *c ) {
    int tid = blockIdx.x;    // this thread handles the data at its thread id
    if (tid < N)
        c[tid] = a[tid] + b[tid];
}

int main( void ) {
    int a[N], b[N], c[N];
    int *dev_a, *dev_b, *dev_c;

    // allocate the memory on the GPU
    // fill the arrays 'a' and 'b' on the CPU
    // copy the arrays 'a' and 'b' to the GPU

    add<<<N,1>>>( dev_a, dev_b, dev_c );

    // copy the array 'c' back from the GPU to the CPU
    // display the results
    // free the memory allocated on the GPU

    return 0;
}

なぜそれを使用したの<<< N , 1 >>>かというと、各ブロックにN個のブロックと1個のスレッドを使用したということですか?これを記述して<<< 1 , N >>>、さらに最適化するためにこのブロックで1ブロックとNスレッドを使用できるためです。

4

1 に答える 1

5

この小さな例では、特別な理由はありません(Bartがコメントですでに言っているように)。ただし、より大きく、より現実的な例では、ブロックあたりのスレッド数が制限されていることを常に念頭に置いておく必要があります。つまり、N = 10000を使用する<<<1,N>>>と、それ以上使用できなくなりますが、<<<N,1>>>引き続き機能します。

于 2012-07-20T12:57:25.010 に答える