私は、カーネル内のローカル配列は、前回の実行から値を維持している、そして新しい値がすべてのランの累積結果であることを奇妙なケースに直面しています。 動的に定義された配列を削除します。しかし、結果を蓄積し続ける。ここで は、例えば、以下のコードである: 最初の実行結果は以下の通りであった:CUDAは、前回の実行からの結果を蓄積している
Before: 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
After: 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ,
次いで、次の実行結果であった:
Before: 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ,
After: 2 , 4 , 6 , 8 , 10 , 12 , 14 , 16 , 18 ,
ように、前回のラン値がクリアされていません。どこに問題があるの?どのように私はそれを修正することができます。
私の環境は次のとおりです。
OS:LinuxのSL7
GPU:テスラK20m(SM_35)
CUDAバージョン:
#include <iostream>
#include <numeric>
#include <stdlib.h>
#include <stdio.h>
__global__ void myKernel(int *data, int vectorSize)
{
int const size = vectorSize;
int *countArray = new int[size];
/*Print array values before modifications*/
printf("\nBefore: ");
for (int i = 0; i < vectorSize; ++i)
{
printf("%d , ", countArray[i]);
}
/*Modifying array values*/
for (int i = 0; i < vectorSize; ++i)
{
countArray[i] += data[i];
}
printf("\nAfter: ");
/*Print array values after modifications*/
for (int i = 0; i < vectorSize; ++i)
{
printf("%d , ", countArray[i]);
}
printf("\n");
delete[] countArray;
}
int main(void)
{
const int size = 9;
int array[size] ={1, 2, 3, 4, 5, 6, 7, 8, 9};
int *dev_array;
cudaMalloc((void**) &dev_array, size * sizeof(int));
cudaMemcpy(dev_array, array, size * sizeof(int), cudaMemcpyHostToDevice);
myKernel<<<1, 1>>>(dev_array, size);
cudaFree(dev_array);
return 0;
}
。ランタイムは、NULLポインタを返すことによって割り当てが失敗したかどうかを知ることができます。あなたの "答え"(ヒント:代わりにあなたの質問を編集する)のあなたのエラーは0のアドレスへの無効な書き込みを示します - NULLポインタ。だからあなたが示していないあなたの「本当の」コードで起こっていることは、新しい割り当てのために利用可能なスペースを超えていることです。あなたは制限を調整することができ、および[ドキュメント](http://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#dynamic-global-memory-allocation-and-を読むことをお勧めしますオペレーション)。 –