9

http://developer.download.nvidia.com/CUDA/training/GTC_Express_Sarah_Tariq_June2011.pdf

上記のチュートリアル (スライド 29) では、int への 3 つのポインターを開始します。

int *a, *b, *c;

明らかにこれは (int *) 型ですが、何らかの方法でカーネルが構文でインデックスにアクセスできるようにします。a[index]

また、(私には) 不明なコマンドを使用して値を初期化します。

a = (int *)malloc(size); random_ints(a, N);

では、このコマンドは何をするのでしょうか? まず、ポインター *a をキャストして int を指すようにします (ただし、後でa魔法のようにベクトルになります)。random_ints が正確に行うことに関するソースを見つけることができません(おそらくいくつかのインクルードが必要なため、コンパイラもそれを認識しません)。a長さ N のベクトルをランダムな intで作成すると思います (aタイプは ですint)。

etc etcで同じことをしてこれを回避しようとしましたvector <int> * a;が、それをカーネルに渡すのにまだ問題があります(何を試しても要素が追加されません)。

私はC++で作業しています。前もって感謝します。

編集:これは疑似コードでしょうか? 明示的な C++ の例では、これを別の (わかりやすい方法で) 行っているためです。

4

1 に答える 1

11
  • You can use pointer in C/C++ in the way like normal array. See:

Example

int* p = new int[2];
p[1] = 2;  
*(p + 1) = 2; // same as line above - just other syntax
*(1 + p) = 2; // and other way
 1[p] = 2; // don't use it - but still valid
  • You can allocate memory by malloc in C++ (it is derived from C way) but only for POD types, like int

Example

int* p = (int*)malloc(12 * sizeof(int)); // C way equivalent to C++: new int [12]

Be aware that you must free this pointer by free(p), not by delete [] p.

  • I guess the implementation of this function is just assigning N random numbers to int array represented by pointer:

Example:

void random_ints(int* a, int N)
{
   int i;
   for (i = 0; i < N; ++i)
    a[i] = rand();
}
于 2012-10-21T22:27:07.390 に答える