2017-10-31 6 views
0

私はCUDAで単純な素朴な文字列検索に取り組んでいます。大きなファイルのCUDA文字列検索、間違った結果

CUDAの新機能です。それは細かい小さなファイル(aprox〜1メガバイト)を動作させます。私は(メモ帳でCtrl + Aキー、Ctrl + C数回++)、これらのファイルが大きく行った後、私のプログラムの結果は

grep -o text file_name | wc -l 

より(1 +程度%)高いそれは非常に単純な関数であるので、私はしないでくださいこれを引き起こす原因を知っている。私はより大きなファイル(〜500MB)で動作する必要があります。

カーネルコード(gpuCount__device__ int global variableです):私はちょうどそれをホストして印刷するには、結果をコピーし、この後

int blocks = 1, threads = fileSize; 

    if (fileSize > 1024) 
    { 
     blocks = (fileSize/1024) + 1; 
     threads = 1024; 
    } 

    clock_t cpu_start = clock(); 
    // kernel call 
    stringSearchGpu<<<blocks, threads>>>(cudaBuffer, strlen(buffer), cudaInput, strlen(input)); 
    cudaDeviceSynchronize(); 

__global__ void stringSearchGpu(char *data, int dataLength, char *input, int inputLength){ 
    int id = blockDim.x*blockIdx.x + threadIdx.x; 
    if (id < dataLength) 
    { 
     int fMatch = 1; 
     for (int j = 0; j < inputLength; j++) 
     { 
      if (data[id + j] != input[j]) fMatch = 0; 
     } 
     if (fMatch) 
     { 
      atomicAdd(&gpuCount, 1); 
     } 
    } 
} 

これは、main関数でカーネルを呼び出しています。

誰でもこの手伝いできますか?

答えて

3

まず、CUDA関数の戻り値をチェックしてエラーをチェックする必要があります。そうするための最良の方法は、次のようになります。

#define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); } 
inline void gpuAssert(cudaError_t code, const char *file, int line, bool abort=true) 
{ 
    if (code != cudaSuccess) 
    { 
     fprintf(stderr,"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line); 
     if (abort) exit(code); 
    } 
} 

のようなあなたのCUDA呼び出し、ラップ:

gpuErrchk(cudaDeviceSynchronize()); 

を第二に、あなたのカーネルが境界メモリからアクセスします。 dataLength=100,inputLength=7およびid=98とする。

if (id < dataLength) // 98 is less than 100, so condition true 
    { 
     int fMatch = 1; 
     for (int j = 0; j < inputLength; j++) // j runs from [0 - 6] 
     { 
      // if j>1 then id+j>=100, which is out of bounds, illegal operation 
      if (data[id + j] != input[j]) fMatch = 0; 
     } 

の変更のようなものへの条件:カーネルコードでのぞき見のため

if (id < dataLength - inputLength) 
+0

おかげで、このMiroslavProcházkaポスト@私の元の問題 –

+0

変更した、完全なコードで助けにはなりませんでした。 – zindarod

+0

https://pastebin.com/3ubxXVhP –

関連する問題