openCL长时间溢出

openCL长时间溢出,opencl,integer-overflow,Opencl,Integer Overflow,在我开始之前,我是一个C初学者,我正在尝试做一些openCL的工作,这可能是一个错误。下面是我的内核代码: __kernel void collatz(__global int* in, __global int* out) { uint id = get_global_id(0); unsigned long n = (unsigned long)id; uint count = 0; while (n > 1) { if (n % 2

在我开始之前,我是一个C初学者,我正在尝试做一些openCL的工作,这可能是一个错误。下面是我的内核代码:

__kernel void collatz(__global int* in, __global int* out)
{
    uint id = get_global_id(0);
    unsigned long n = (unsigned long)id;
    uint count = 0;

    while (n > 1) { 
        if (n % 2 == 0) {
            n = n / 2; 
        } else { 
            if(n == 1572066143) {
                unsigned long test = n;
                printf("BEFORE - %lu\n", n);
                test = (3 * test) + 1; 
                printf("AFTER  - %lu\n", test);

                n = (3 * n) + 1; 
             } else {
                 n = (3 * n) + 1; 
            }

       }

       count = count + 1;
    }

    out[id] = count;

}
以及输出:

BEFORE - 1572066143
AFTER  - 421231134
对我来说,它看起来像是n溢出,但我不明白它为什么会发生

有趣的是,如果我创建一个新变量来存储与n相同的值,那么它似乎可以正常工作

unsigned long test = 1572066143;
printf("BEFORE - %lu\n", test);
test = (3 * test) + 1; 
printf("AFTER  - %lu\n", test);
输出:

 BEFORE - 1572066143
 AFTER  - 4716198430
正如我说的,我是C初学者,所以我可能会做一些非常愚蠢的事情!任何帮助都将不胜感激,因为我已经把头发拔了好几个小时了

谢谢, 斯蒂芬

更新:

这是我的主机代码,以防我在这方面做了一些愚蠢的事情:

int _tmain(int argc, _TCHAR* argv[])
{
    /*Step1: Getting platforms and choose an available one.*/
    cl_uint numPlatforms;   //the NO. of platforms
    cl_platform_id platform = NULL; //the chosen platform
    cl_int  status = clGetPlatformIDs(0, NULL, &numPlatforms);

    cl_platform_id* platforms = (cl_platform_id*)malloc(numPlatforms*   sizeof(cl_platform_id));
    status = clGetPlatformIDs(numPlatforms, platforms, NULL);
    platform = platforms[0];
    free(platforms);

    /*Step 2:Query the platform and choose the first GPU device if has one.*/
    cl_device_id        *devices;
    devices = (cl_device_id*)malloc(1 * sizeof(cl_device_id));
    clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, devices, NULL);

    /*Step 3: Create context.*/
    cl_context context = clCreateContext(NULL, 1, devices, NULL, NULL, NULL);

    /*Step 4: Creating command queue associate with the context.*/
    cl_command_queue commandQueue = clCreateCommandQueue(context, devices[0], 0, NULL);

    /*Step 5: Create program object */
    const char *filename = "HelloWorld_Kernel.cl";
    std::string sourceStr;
    status = convertToString(filename, sourceStr);
    const char *source = sourceStr.c_str();
    size_t sourceSize[] = { strlen(source) };
    cl_program program = clCreateProgramWithSource(context, 1, &source, sourceSize, NULL);

    status = clBuildProgram(program, 1, devices, NULL, NULL, NULL);

    /*Step 7: Initial input,output for the host and create memory objects for the kernel*/
    cl_ulong max = 2000000;
    cl_ulong *numbers = NULL;
    numbers = new cl_ulong[max];
    for (int i = 1; i <= max; i++) {
        numbers[i] = i;
    }

    int *output = (int*)malloc(sizeof(cl_ulong) * max);

    cl_mem inputBuffer = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, max * sizeof(cl_ulong), (void *)numbers, NULL);
    cl_mem outputBuffer = clCreateBuffer(context, CL_MEM_WRITE_ONLY, max * sizeof(cl_ulong), NULL, NULL);

    /*Step 8: Create kernel object */
    cl_kernel kernel = clCreateKernel(program, "collatz", NULL);

    /*Step 9: Sets Kernel arguments.*/
    status = clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *)&inputBuffer);


    // Determine the size of the log
    size_t log_size;
    clGetProgramBuildInfo(program, devices[0], CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size);

    // Allocate memory for the log
    char *log = (char *)malloc(log_size);

    // Get the log
    clGetProgramBuildInfo(program, devices[0], CL_PROGRAM_BUILD_LOG, log_size, log, NULL);

    // Print the log
    printf("%s\n", log);


    status = clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *)&outputBuffer);

    /*Step 10: Running the kernel.*/
    size_t global_work_size[] = { max };
    status = clEnqueueNDRangeKernel(commandQueue, kernel, 1, NULL, global_work_size, NULL, 0, NULL, NULL);

   /*Step 11: Read the data put back to host memory.*/
   status = clEnqueueReadBuffer(commandQueue, outputBuffer, CL_TRUE, 0, max * sizeof(cl_ulong), output, 0, NULL, NULL);


return SUCCESS;
int-tmain(int-argc,_-TCHAR*argv[]
{
/*步骤1:获取平台并选择一个可用的平台*/
cl_uint numPlatforms;//平台数量
cl_platform_id platform=NULL;//所选平台
cl_int status=clGetPlatformIDs(0、NULL和numPlatforms);
cl_平台id*平台=(cl_平台id*)malloc(numPlatforms*sizeof(cl_平台id));
状态=clGetPlatformIDs(numPlatforms、platforms、NULL);
平台=平台[0];
免费(月台);
/*步骤2:查询平台并选择第一个GPU设备(如果有)*/
cl_设备\u id*设备;
设备=(cl_设备id*)malloc(1*sizeof(cl_设备id));
CLGetDeviceID(平台,CL\U设备类型\U GPU,1,设备,空);
/*步骤3:创建上下文*/
cl_context context=clCreateContext(NULL,1,devices,NULL,NULL,NULL);
/*步骤4:创建与上下文关联的命令队列*/
cl_command_queue commandQueue=clCreateCommandQueue(上下文,设备[0],0,NULL);
/*步骤5:创建程序对象*/
const char*filename=“HelloWorld\u Kernel.cl”;
std::字符串sourceStr;
status=convertToString(文件名,sourceStr);
const char*source=sourceStr.c_str();
size_t sourceSize[]={strlen(source)};
cl_program program=clCreateProgramWithSource(上下文,1,&source,sourceSize,NULL);
状态=clBuildProgram(程序,1,设备,空,空,空);
/*步骤7:主机的初始输入、输出和为内核创建内存对象*/
cl_ulong最大值=2000000;
cl_ulong*编号=空;
数字=新的cl_ulong[最大值];

对于(int i=1;i主机端和设备大小值具有不同的大小。在主机中,
long
可以从32位到64位不等,具体取决于平台。在设备中,
long
仅指64位

printf()
函数,如C中定义的,
%ld
用于打印长(主机端长)数字。您在内核中使用printf,因此…可能使用了类似C的解析器,因此将变量打印为32位长


您可以尝试将其打印为
%lld
或浮点数吗?

主机端和设备大小值具有不同的大小。在主机中,
long
可以从32位到64位不等,具体取决于平台。在设备中,
long
仅指64位

printf()
函数,如C中定义的,
%ld
用于打印长(主机端长)数字。您在内核中使用printf,因此…可能使用了类似C的解析器,因此将变量打印为32位长


你能试着把它打印成
%lld
还是浮点数?

我终于找到了问题的症结所在

我在我的Intel HD Graphics 4600芯片上运行代码,它产生了原始问题中所示的奇怪行为。我切换到使用我的AMD卡,然后它开始按预期工作


非常奇怪。感谢大家的帮助!

我终于弄清了问题的真相

我在我的Intel HD Graphics 4600芯片上运行代码,它产生了原始问题中所示的奇怪行为。我切换到使用我的AMD卡,然后它开始按预期工作


非常奇怪。感谢大家的帮助!

可能只是一个编译器错误,当我在自己的机器上运行同一个内核时,我得到了正确的结果。您使用的是哪种OpenCL平台和设备?您尝试过不同的平台和设备吗?感谢您的回复并在您这方面进行了尝试。我使用的是AMD SDK。我已经在我的Intel Int和整合的图形以及我的AMD图形卡280x,但都产生了错误的结果。我可能会尝试在AMD开发者论坛上提出相同的问题。可能只是一个编译器错误,当我在自己的机器上运行相同的内核时,我得到了正确的结果。你使用的是哪种OpenCL平台和设备?你尝试过不同的吗?谢谢我正在使用AMD SDK。我在我的英特尔集成显卡和AMD图形卡280x上都试用过它,但都产生了错误的结果。我可能会尝试在AMD开发者论坛上提出同样的问题。您好,感谢您的回复,我尝试使用%lld,但得到了相同的结果。是吗您没有解释printf输出?工作项没有按顺序运行,因此打印可以是任何线程。Mybe您只是查看一个线程的后面和另一个线程的前面。溢出是发生在所有情况下还是仅发生在该情况下?您可以向其添加id吗?
printf(“%u before-%lu\n”,id,test)
Hi,感谢您的回复,我尝试使用%lld,但得到了相同的结果。可能是您没有解释printf输出吗?工作项不是按顺序运行的,因此打印可以是任何线程。Mybe您只是查看一个线程的后面和另一个线程的前面。是所有情况下都会发生溢出,还是仅此一种情况下会发生溢出?可以吗向其添加id?
printf(%u在-%lu\n之前),id,test);