Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 循环大小的OpenCL限制?_C++_C_Opencl - Fatal编程技术网

C++ 循环大小的OpenCL限制?

C++ 循环大小的OpenCL限制?,c++,c,opencl,C++,C,Opencl,更新:clenqueueredbuffer(命令队列,c_mem_obj,CL_TRUE,0,列表大小*sizeof(双精度),c,0,NULL,NULL)正在返回-5,CL\u OUT\u OF\u RESOURCES。此函数/调用不应返回此值 我开始使用OpenCL,但遇到了一个问题。如果我允许一个for循环(在内核中)运行10000次,我得到所有的C为0,如果我允许循环运行8000次,结果都是正确的 我在内核周围添加了等待,以确保它完成,认为我是在完成之前提取数据,并尝试了Clwaitfo

更新:
clenqueueredbuffer(命令队列,c_mem_obj,CL_TRUE,0,列表大小*sizeof(双精度),c,0,NULL,NULL)正在返回-5,
CL\u OUT\u OF\u RESOURCES
。此函数/调用不应返回此值

我开始使用OpenCL,但遇到了一个问题。如果我允许一个for循环(在内核中)运行10000次,我得到所有的C为0,如果我允许循环运行8000次,结果都是正确的

我在内核周围添加了等待,以确保它完成,认为我是在完成之前提取数据,并尝试了Clwaitforevent和CLFinish。任何呼叫都不会发出错误信号。当我使用ints时,for循环的大小为4000000。Float和double有同样的问题,但是当我使用我删除的Float时,Float在10000工作,而不是20000工作。pragma OPENCL EXTENSION cl_khr_fp64:enable检查这不是问题所在

这是不是有些奇怪的记忆问题,我用OpenCL错了?我意识到在大多数内核中,我不会实现这样的循环,但这似乎是一个问题。我还删除了
\uuu private
,看看这是否是问题所在,没有变化。那么OpenCL内核中for循环的大小有限制吗?硬件是特定的吗?还是这是一只虫子

内核是一个简单的内核,它将两个数组(a+B)添加在一起并输出另一个数组(C)。为了获得对性能的感觉,我在每次计算中都放了一个for循环,以降低计算速度/增加每次运行的操作数

内核的代码如下所示:

#pragma OPENCL EXTENSION cl_khr_fp64 : enable

__kernel void vector_add(__global double *A, __global double *B, __global double *C)
{

    // Get the index of the current element
    int i = get_global_id(0);

    // Do the operation

    for (__private unsigned int j = 0; j < 10000; j++)
    {
        C[i] = A[i] + B[i];
    }
}
#pragma OPENCL扩展cl_khr_fp64:启用
__核空向量加(uuu全局双精度*A,uu全局双精度*B,uu全局双精度*C)
{
//获取当前元素的索引
int i=获取全局id(0);
//做手术
对于(_private unsigned int j=0;j<10000;j++)
{
C[i]=A[i]+B[i];
}
}
我运行的代码如下:(当我在float和double之间切换时,我确保两段代码之间的变量是一致的)

#包括
#包括
#包括
#苹果__
#包括
#否则
#包括
#恩迪夫
#定义最大源大小(0x100000)
内部主(空){
//创建两个输入向量
int i;
const int LIST_SIZE=4000000;
double*A=(double*)malloc(sizeof(double)*列表大小);
double*B=(double*)malloc(sizeof(double)*列表大小);
对于(i=0;istd::cout我发现了问题,连接到显示器/活动VGA/等的GPU有一个看门狗定时器,在~5s后超时。这是非特斯拉卡的情况,该功能需要关闭。在辅助卡上运行是一个解决办法。这很糟糕,需要尽快修复。这肯定是NVidia的问题,不确定不管怎样,这都是可怕的

解决方法是在Windows中更改注册表,在Linux/Ubuntu中更改X配置并放置:

选项“交互式”0


但是,在与图形卡的差距中,X conf现在不会在更高版本中生成,可能必须手动创建。如果有人对此进行了复制和粘贴控制台代码修复,这将是一个很好的答案。

C++或C?如果您不确定:这看起来很像普通的C,exc
#include <stdio.h>
#include <stdlib.h>
#include <iostream>

#ifdef __APPLE__
#include <OpenCL/opencl.h>
#else
#include <CL/cl.h>
#endif

#define MAX_SOURCE_SIZE (0x100000)

int main(void) {
    // Create the two input vectors
    int i;
    const int LIST_SIZE = 4000000;
    double *A = (double*)malloc(sizeof(double)*LIST_SIZE);
    double *B = (double*)malloc(sizeof(double)*LIST_SIZE);
    for(i = 0; i < LIST_SIZE; i++) {
        A[i] = static_cast<double>(i);
        B[i] = static_cast<double>(LIST_SIZE - i);
    }

    // Load the kernel source code into the array source_str
    FILE *fp;
    char *source_str;
    size_t source_size;

    fp = fopen("vector_add_kernel.cl", "r");
    if (!fp) {
        fprintf(stderr, "Failed to load kernel.\n");
        exit(1);
    }
    source_str = (char*)malloc(MAX_SOURCE_SIZE);
    source_size = fread( source_str, 1, MAX_SOURCE_SIZE, fp);
    fclose( fp );

    // Get platform and device information
    cl_platform_id platform_id = NULL;
    cl_device_id device_id = NULL;
    cl_uint ret_num_devices;
    cl_uint ret_num_platforms;
//    clGetPlatformIDs(1, &platform_id, NULL);
//clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_GPU, 1, &device_id, ret_num_devices);


    cl_int ret = clGetPlatformIDs(1, &platform_id, NULL);
                if (ret != CL_SUCCESS) {
printf("Error: Failed to get platforms! (%d) \n", ret);
return EXIT_FAILURE;
}
    ret = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_GPU, 1, &device_id, &ret_num_devices);
            if (ret != CL_SUCCESS) {
printf("Error: Failed to query platforms to get devices! (%d) \n", ret);
return EXIT_FAILURE;
}
/*
    cl_int ret = clGetPlatformIDs(1, &platform_id, NULL);
                if (ret != CL_SUCCESS) {
printf("Error: Failed to get platforms! (%d) \n", ret);
return EXIT_FAILURE;
}
    ret = clGetDeviceIDs( platform_id, CL_DEVICE_TYPE_CPU, 1,
            &device_id, &ret_num_devices);
            if (ret != CL_SUCCESS) {
printf("Error: Failed to query platforms to get devices! (%d) \n", ret);
return EXIT_FAILURE;
}
*/
    // Create an OpenCL context
    cl_context context = clCreateContext( NULL, 1, &device_id, NULL, NULL, &ret);

    // Create a command queue
    cl_command_queue command_queue = clCreateCommandQueue(context, device_id, 0, &ret);

    // Create memory buffers on the device for each vector
    cl_mem a_mem_obj = clCreateBuffer(context, CL_MEM_READ_ONLY,
            LIST_SIZE * sizeof(double), NULL, &ret);
    cl_mem b_mem_obj = clCreateBuffer(context, CL_MEM_READ_ONLY,
            LIST_SIZE * sizeof(double), NULL, &ret);
    cl_mem c_mem_obj = clCreateBuffer(context, CL_MEM_WRITE_ONLY,
            LIST_SIZE * sizeof(double), NULL, &ret);
            if (ret != CL_SUCCESS) {
printf("Error: Buffer Fail! (%d) \n", ret);
return EXIT_FAILURE;
}

    // Copy the lists A and B to their respective memory buffers
    ret = clEnqueueWriteBuffer(command_queue, a_mem_obj, CL_TRUE, 0,
            LIST_SIZE * sizeof(double), A, 0, NULL, NULL);
    ret = clEnqueueWriteBuffer(command_queue, b_mem_obj, CL_TRUE, 0,
            LIST_SIZE * sizeof(double), B, 0, NULL, NULL);

    std::cout << "Begin Compile" << "\n";
    // Create a program from the kernel source
    cl_program program = clCreateProgramWithSource(context, 1,
            (const char **)&source_str, (const size_t *)&source_size, &ret);
             if (ret != CL_SUCCESS) {
printf("Error: Program Fail! (%d) \n", ret);
return EXIT_FAILURE;
}

    // Build the program
    ret = clBuildProgram(program, 1, &device_id, NULL, NULL, NULL);
    if (ret != CL_SUCCESS) {
printf("Error: ProgramBuild Fail! (%d) \n", ret);
return EXIT_FAILURE;
}

    // Create the OpenCL kernel
    cl_kernel kernel = clCreateKernel(program, "vector_add", &ret);
    if (ret != CL_SUCCESS) {
printf("Error: Kernel Build Fail! (%d) \n", ret);
return EXIT_FAILURE;
}
    std::cout << "End Compile" << "\n";

    std::cout << "Begin Data Move" << "\n";
    // Set the arguments of the kernel
    ret = clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *)&a_mem_obj);
    ret = clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *)&b_mem_obj);
    ret = clSetKernelArg(kernel, 2, sizeof(cl_mem), (void *)&c_mem_obj);
    std::cout << "End Data Move" << "\n";

    // Execute the OpenCL kernel on the list
    size_t global_item_size = LIST_SIZE; // Process the entire lists
    size_t local_item_size = 64; // Process in groups of 64

    std::cout << "Begin Execute" << "\n";
    cl_event event;
    ret = clEnqueueNDRangeKernel(command_queue, kernel, 1, NULL,
            &global_item_size, &local_item_size, 0, NULL, &event);
            clFinish(command_queue);
            //clWaitForEvents(1, &event);
    std::cout << "End Execute" << "\n";
    if (ret != CL_SUCCESS) {
printf("Error: Execute Fail! (%d) \n", ret);
return EXIT_FAILURE;
}

    // Read the memory buffer C on the device to the local variable C
    std::cout << "Begin Data Move" << "\n";

    double *C = (double*)malloc(sizeof(double)*LIST_SIZE);
    ret = clEnqueueReadBuffer(command_queue, c_mem_obj, CL_TRUE, 0,
            LIST_SIZE * sizeof(double), C, 0, NULL, NULL);
            if (ret != CL_SUCCESS) {
            printf("Error: Read Fail! (%d) \n", ret);
            return EXIT_FAILURE;
            }
            clFinish(command_queue);
    std::cout << "End Data Move" << "\n";

    std::cout << "Done" << "\n";
    std::cin.get();
    // Display the result to the screen
    for(i = 0; i < LIST_SIZE; i++)
        printf("%f + %f = %f \n", A[i], B[i], C[i]);

    // Clean up
    ret = clFlush(command_queue);
    ret = clFinish(command_queue);
    ret = clReleaseKernel(kernel);
    ret = clReleaseProgram(program);
    ret = clReleaseMemObject(a_mem_obj);
    ret = clReleaseMemObject(b_mem_obj);
    ret = clReleaseMemObject(c_mem_obj);
    ret = clReleaseCommandQueue(command_queue);
    ret = clReleaseContext(context);
    free(A);
    free(B);
    free(C);
    std::cout << "Number of Devices: " << ret_num_devices << "\n";
    std::cin.get();
    return 0;
}