Parallel processing CUDA字符串搜索在大文件中,结果错误

Parallel processing CUDA字符串搜索在大文件中,结果错误,parallel-processing,cuda,gpu,Parallel Processing,Cuda,Gpu,我正在CUDA中进行简单朴素的字符串搜索 我是CUDA的新手。它在较小的文件(大约1MB)下工作良好。当我将这些文件放大(在记事本++中多次使用ctrl+a ctrl+c)后,我的程序的结果(大约+1%)会高于a grep -o text file_name | wc -l 这是一个非常简单的函数,所以我不知道是什么导致了这个。我需要它来处理更大的文件(~500MB) 内核代码(gpuCount是一个\uuuuu设备\uuuuuuu int全局变量): \uuuu全局\uuuuu无效stri

我正在CUDA中进行简单朴素的字符串搜索

我是CUDA的新手。它在较小的文件(大约1MB)下工作良好。当我将这些文件放大(在记事本++中多次使用ctrl+a ctrl+c)后,我的程序的结果(大约+1%)会高于a

 grep -o text file_name | wc -l
这是一个非常简单的函数,所以我不知道是什么导致了这个。我需要它来处理更大的文件(~500MB)

内核代码(
gpuCount
是一个
\uuuuu设备\uuuuuuu int全局变量
):

\uuuu全局\uuuuu无效stringSearchGpu(char*data,int-dataLength,char*input,int-inputLength){
int id=blockDim.x*blockIdx.x+threadIdx.x;
if(id
这是在主函数中调用内核:

    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();
int blocks=1,threads=fileSize;
如果(文件大小>1024)
{
块=(文件大小/1024)+1;
线程数=1024;
}
时钟\u t cpu\u启动=时钟();
//内核调用
stringSearchGpu(cudaBuffer,strlen(缓冲区),cudaInput,strlen(输入));
cudaDeviceSynchronize();
在此之后,我只需将结果复制到主机并打印它


有人能帮我吗

首先,您应该始终检查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;
         }
如果(id1,则id+j>=100,这是越界的非法操作
如果(数据[id+j]!=输入[j])fMatch=0;
}
将条件更改为类似以下内容:

if (id < dataLength - inputLength)
if(id
首先,您应该始终检查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;
         }
如果(id1,则id+j>=100,这是越界的非法操作
如果(数据[id+j]!=输入[j])fMatch=0;
}
将条件更改为类似以下内容:

if (id < dataLength - inputLength)
if(id
感谢您的关注,但这对我的原创作品没有帮助problem@MiroslavProcházka发布修改后的完整代码。@MiroslavProcházka发布此代码给出错误结果的文件。@MiroslavProcházka您的代码为我提供了一致的结果。您将结果与
grep-o text test.txt | wc-l
进行了比较?我是使用CUDA 9.0.176版在VS 2015上运行,谢谢大家的关注,但这对我的原创作品没有帮助problem@MiroslavProcházka发布修改后的完整代码。@MiroslavProcházka发布此代码给出错误结果的文件。@MiroslavProcházka您的代码为我提供了一致的结果。您将结果与
grep进行了比较-o text test.txt | wc-l
?我正在使用CUDA 9.0.176版在VS 2015上运行它