gpt4 book ai didi

c - 在 Opencl 中 clGetDeviceIDs 返回错误 CL_OUT_OF_HOST_MEMORY

转载 作者:太空狗 更新时间:2023-10-29 12:42:21 26 4
gpt4 key购买 nike

我有firefly rk 3288 board使用 GPU,所以我想测试 openCL C 代码,编写非常基本的代码,可以读取如下平台信息:

#include <stdio.h>    
#include <stdlib.h>
#include <CL/cl.h>
#define LOGI printf

int main() {

cl_uint i_plat;
cl_int err;

// Discover the number of platforms:
cl_uint nplatforms;
err = clGetPlatformIDs(0, NULL, &nplatforms);

LOGI("\n Number of Platform %d: error = %d ", nplatforms, err );

//check_error(err, "clGetPlatformIds");
cl_device_id device_id = NULL;
cl_uint ret_num_devices;
// Now ask OpenCL for the platform IDs:
cl_platform_id* platforms = (cl_platform_id*)malloc(sizeof(cl_platform_id)* nplatforms);


if(platforms == NULL){
printf("\n Fail to allocate memory for platforms");
}


err = clGetPlatformIDs(nplatforms, platforms, NULL);
LOGI("\n clGetPlatformIDs return error = %d ", err );

//check_error(err, "clGetPlatformIds");

// Ask OpenCL about each platform to understand the problem:
char name[128];
char vendor[128];
char version[128];
char driver[128];
cl_uint val = 0;
size_t val1 = 0;
size_t val2 = 0;
size_t prof_tmr_res = 0;
cl_uint MaxComputeUnit = 0;
cl_uint MaxClockFrequency = 0;

cl_device_type deviceType;

cl_device_exec_capabilities exec_capabilities;

//LOGI(ANDROID_LOG_INFO,"POOJA","AKVEDIAHE");

for (i_plat = 0; i_plat < nplatforms; i_plat++) {
err |= clGetPlatformInfo(platforms[i_plat], CL_PLATFORM_VENDOR, 128, vendor, NULL);
err |= clGetPlatformInfo(platforms[i_plat], CL_PLATFORM_NAME, 128, name, NULL);
err |= clGetPlatformInfo(platforms[i_plat], CL_PLATFORM_VERSION, 128, version, NULL);
//check_error(err, "clGetPlatformInfo");

LOGI("\n Platform Info %d: Vendor = %s Name = %s Version = %s Error = %d", i_plat, vendor, name, version,err);

/*device info*/
//err = clGetDeviceIDs(platforms[i_plat], CL_DEVICE_TYPE_GPU, 1, &device_id, &ret_num_devices);
err = clGetDeviceIDs(platforms[i_plat], CL_DEVICE_TYPE_GPU, 1, &device_id, NULL);

LOGI("\n clGetDeviceIDs GPU Error %d ",err );

err |= clGetDeviceInfo(device_id, CL_DEVICE_TYPE,sizeof(cl_int), &deviceType, NULL);
err |= clGetDeviceInfo(device_id, CL_DEVICE_VENDOR, 128, vendor, NULL);
err |= clGetDeviceInfo(device_id, CL_DEVICE_NAME, 128, name, NULL);
err |= clGetDeviceInfo(device_id, CL_DEVICE_VERSION, 128, version, NULL);
err |= clGetDeviceInfo(device_id, CL_DRIVER_VERSION, 128, driver, NULL);
err |= clGetDeviceInfo(device_id, CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS, sizeof(cl_uint), &val, NULL);
err |= clGetDeviceInfo(device_id, CL_DEVICE_PROFILING_TIMER_RESOLUTION, sizeof(size_t), &prof_tmr_res, NULL);
err |= clGetDeviceInfo(device_id, CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(cl_uint), &MaxComputeUnit, NULL);
err |= clGetDeviceInfo(device_id, CL_DEVICE_MAX_CLOCK_FREQUENCY, sizeof(cl_uint), &MaxClockFrequency, NULL);
err |= clGetDeviceInfo(device_id, CL_DEVICE_EXECUTION_CAPABILITIES,sizeof(cl_int), &exec_capabilities, NULL);

LOGI("\n Device Type %08x:",deviceType );
for(int i =0; i < val ; i++){
err = clGetDeviceInfo(device_id, CL_DEVICE_MAX_WORK_ITEM_SIZES, sizeof(size_t), &val1, NULL);
LOGI("\n MAX_WORK_ITEM_SIZES[%d] = %d,", i,val1 );
}
err = clGetDeviceInfo(device_id, CL_DEVICE_MAX_WORK_GROUP_SIZE, sizeof(size_t), &val2, NULL);

LOGI("\n MAX_WORK_ITEM_DIMENSIONS = %d , MAX_WORK_GROUP_SIZE = %d ", val,val2 );
LOGI("\n device_id FOR Platform = %d vender = %s, device name = %s, device version = %s, driver version = %s", i_plat, vendor, name, version, driver);
LOGI("\n CL_DEVICE_PROFILING_TIMER_RESOLUTION = %d",prof_tmr_res );
LOGI("\n Max Compute Unit = %d",MaxComputeUnit );
LOGI("\n Max Clock Frequency = %d",MaxClockFrequency );
LOGI("\n Device exec capabilities = %08x",exec_capabilities );
}
return 0;
}

输出:

Number of Platform 1: error = 0

clGetPlatformIDs return error = 0

Platform Info 0: Vendor = ARM Name = ARM Platform Version = OpenCL 1.1 Error = 0

clGetDeviceIDs GPU Error -6

Device Type ffffffdf:

MAX_WORK_ITEM_DIMENSIONS = 0 , MAX_WORK_GROUP_SIZE = 0

device_id FOR Platform = 0 vender = ARM, device name = ARM Platform,

device version = OpenCL 1.1 , driver version = �*��

CL_DEVICE_PROFILING_TIMER_RESOLUTION = 0

Max Compute Unit = 0

Max Clock Frequency = 0

现在不明白为什么它返回 -6 (CL_OUT_OF_HOST_MEMORY)

为什么它无法在主机上或我遗漏的地方分配 OpenCL 实现所需的资源?

最佳答案

我在 RK3288(Android、Viewsonic VSD242)上的应用程序中使用 OpenCL,我对以下代码没有任何问题。我希望这段代码对某人有所帮助。

cl_context OPENCL_CONTEXT;
cl_command_queue OPENCL_COMMAND_QUEUE;
cl_kernel* OPENCL_KERNELS;
cl_device_id OPENCL_DEVICE_ID;
cl_program OPENCL_PROGRAM;
cl_uint OPENCL_NUM_KERNELS;


void init_openCL() {

size_t kernel_code_size;
cl_int ret;
cl_uint ret_num_platforms;
cl_platform_id platform_id = NULL;
cl_uint ret_num_devices;
char buildLog[1024];
int i;

ret = clGetPlatformIDs(1, &platform_id, &ret_num_platforms);
print_opencl_result("clGetPlatformIDs", ret);
LOGD("--->available number of platforms = %d", ret_num_platforms);
ret = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_GPU, 1, &OPENCL_DEVICE_ID, &ret_num_devices);
print_opencl_result("clGetDeviceIDs", ret);
LOGD("--->available number of devices = %d", ret_num_devices);
OPENCL_CONTEXT = clCreateContext(NULL, 1, &OPENCL_DEVICE_ID, NULL, NULL, &ret);
print_opencl_result("clCreateContext", ret);
//OPENCL_COMMAND_QUEUE = clCreateCommandQueue(OPENCL_CONTEXT, OPENCL_DEVICE_ID, 0, &ret);
OPENCL_COMMAND_QUEUE = clCreateCommandQueue(OPENCL_CONTEXT, OPENCL_DEVICE_ID, CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE, &ret);
print_opencl_result("clCreateCommandQueue", ret);


// load kernel code from the assets directory
const char* source = (const char*)loadResourceFromAssets("my_kernels.cl");
kernel_code_size = sizeof(source);


OPENCL_PROGRAM = clCreateProgramWithSource(OPENCL_CONTEXT, 1, &source, NULL, &ret);
print_opencl_result("clCreateProgramWithSource", ret);
ret = clBuildProgram(OPENCL_PROGRAM, 1, &OPENCL_DEVICE_ID, "", NULL, NULL);
print_opencl_result("clBuildProgram", ret);

ret = clGetProgramBuildInfo(OPENCL_PROGRAM, OPENCL_DEVICE_ID, CL_PROGRAM_BUILD_LOG, 1024, buildLog, NULL);
print_opencl_result("clGetProgramBuildInfo", ret);
LOGD("%s", buildLog);

ret = clCreateKernelsInProgram(OPENCL_PROGRAM, 0, NULL, &OPENCL_NUM_KERNELS);
OPENCL_KERNELS = (cl_kernel*)malloc(OPENCL_NUM_KERNELS * sizeof(cl_kernel));
ret = clCreateKernelsInProgram(OPENCL_PROGRAM, OPENCL_NUM_KERNELS, OPENCL_KERNELS, NULL);
print_opencl_result("clCreateKernelsInProgram", ret);

}



void close_openCL() {
int i;

clFinish(OPENCL_COMMAND_QUEUE);
clReleaseContext(OPENCL_CONTEXT);
clReleaseCommandQueue(OPENCL_COMMAND_QUEUE);

for (i = 0; i < OPENCL_NUM_KERNELS; i++) {
clReleaseKernel(OPENCL_KERNELS[i]);
}
clReleaseProgram(OPENCL_PROGRAM);
free(OPENCL_KERNELS);
}


void print_opencl_result(const char *name, int ret) {
if (ret == CL_SUCCESS) {
print_logD("--->%s = success", name);
} else if (ret == CL_DEVICE_NOT_FOUND) {
print_logE("--->%s = CL_DEVICE_NOT_FOUND", name);
} else if (ret == CL_DEVICE_NOT_AVAILABLE) {
print_logE("--->%s = CL_DEVICE_NOT_AVAILABLE", name);
} else if (ret == CL_COMPILER_NOT_AVAILABLE) {
print_logE("--->%s = CL_COMPILER_NOT_AVAILABLE", name);
} else if (ret == CL_MEM_OBJECT_ALLOCATION_FAILURE) {
print_logE("--->%s = CL_MEM_OBJECT_ALLOCATION_FAILURE", name);
} else if (ret == CL_OUT_OF_RESOURCES) {
print_logE("--->%s = CL_OUT_OF_RESOURCES", name);
} else if (ret == CL_OUT_OF_HOST_MEMORY) {
print_logE("--->%s = CL_OUT_OF_HOST_MEMORY", name);
} else if (ret == CL_PROFILING_INFO_NOT_AVAILABLE) {
print_logE("--->%s = CL_PROFILING_INFO_NOT_AVAILABLE", name);
} else if (ret == CL_MEM_COPY_OVERLAP) {
print_logE("--->%s = CL_MEM_COPY_OVERLAP", name);
} else if (ret == CL_IMAGE_FORMAT_MISMATCH) {
print_logE("--->%s = CL_IMAGE_FORMAT_MISMATCH", name);
} else if (ret == CL_IMAGE_FORMAT_NOT_SUPPORTED) {
print_logE("--->%s = CL_IMAGE_FORMAT_NOT_SUPPORTED", name);
} else if (ret == CL_BUILD_PROGRAM_FAILURE) {
print_logE("--->%s = CL_BUILD_PROGRAM_FAILURE", name);
} else if (ret == CL_MAP_FAILURE) {
print_logE("--->%s = CL_MAP_FAILURE", name);
} else if (ret == CL_MISALIGNED_SUB_BUFFER_OFFSET) {
print_logE("--->%s = CL_MISALIGNED_SUB_BUFFER_OFFSET", name);
} else if (ret == CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST) {
print_logE("--->%s = CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST", name);
} else if (ret == CL_INVALID_VALUE) {
print_logE("--->%s = CL_INVALID_VALUE", name);
} else if (ret == CL_INVALID_DEVICE_TYPE) {
print_logE("--->%s = CL_INVALID_DEVICE_TYPE", name);
} else if (ret == CL_INVALID_PLATFORM) {
print_logE("--->%s = CL_INVALID_PLATFORM", name);
} else if (ret == CL_INVALID_DEVICE) {
print_logE("--->%s = CL_INVALID_DEVICE", name);
} else if (ret == CL_INVALID_CONTEXT) {
print_logE("--->%s = CL_INVALID_CONTEXT", name);
} else if (ret == CL_INVALID_QUEUE_PROPERTIES) {
print_logE("--->%s = CL_INVALID_QUEUE_PROPERTIES", name);
} else if (ret == CL_INVALID_COMMAND_QUEUE) {
print_logE("--->%s = CL_INVALID_COMMAND_QUEUE", name);
} else if (ret == CL_INVALID_HOST_PTR) {
print_logE("--->%s = CL_INVALID_HOST_PTR", name);
} else if (ret == CL_INVALID_MEM_OBJECT) {
print_logE("--->%s = CL_INVALID_MEM_OBJECT", name);
} else if (ret == CL_INVALID_IMAGE_FORMAT_DESCRIPTOR) {
print_logE("--->%s = CL_INVALID_IMAGE_FORMAT_DESCRIPTOR", name);
} else if (ret == CL_INVALID_IMAGE_SIZE) {
print_logE("--->%s = CL_INVALID_IMAGE_SIZE", name);
} else if (ret == CL_INVALID_SAMPLER) {
print_logE("--->%s = CL_INVALID_SAMPLER", name);
} else if (ret == CL_INVALID_BINARY) {
print_logE("--->%s = CL_INVALID_BINARY", name);
} else if (ret == CL_INVALID_BUILD_OPTIONS) {
print_logE("--->%s = CL_INVALID_BUILD_OPTIONS", name);
} else if (ret == CL_INVALID_PROGRAM) {
print_logE("--->%s = CL_INVALID_PROGRAM", name);
} else if (ret == CL_INVALID_PROGRAM_EXECUTABLE) {
print_logE("--->%s = CL_INVALID_PROGRAM_EXECUTABLE", name);
} else if (ret == CL_INVALID_KERNEL_NAME) {
print_logE("--->%s = CL_INVALID_KERNEL_NAME", name);
} else if (ret == CL_INVALID_KERNEL_DEFINITION) {
print_logE("--->%s = CL_INVALID_KERNEL_DEFINITION", name);
} else if (ret == CL_INVALID_KERNEL) {
print_logE("--->%s = CL_INVALID_KERNEL", name);
} else if (ret == CL_INVALID_ARG_INDEX) {
print_logE("--->%s = CL_INVALID_ARG_INDEX", name);
} else if (ret == CL_INVALID_ARG_VALUE) {
print_logE("--->%s = CL_INVALID_ARG_VALUE", name);
} else if (ret == CL_INVALID_ARG_SIZE) {
print_logE("--->%s = CL_INVALID_ARG_SIZE", name);
} else if (ret == CL_INVALID_KERNEL_ARGS) {
print_logE("--->%s = CL_INVALID_KERNEL_ARGS", name);
} else if (ret == CL_INVALID_WORK_DIMENSION) {
print_logE("--->%s = CL_INVALID_WORK_DIMENSION", name);
} else if (ret == CL_INVALID_WORK_GROUP_SIZE) {
print_logE("--->%s = CL_INVALID_WORK_GROUP_SIZE", name);
} else if (ret == CL_INVALID_WORK_ITEM_SIZE) {
print_logE("--->%s = CL_INVALID_WORK_ITEM_SIZE", name);
} else if (ret == CL_INVALID_GLOBAL_OFFSET) {
print_logE("--->%s = CL_INVALID_GLOBAL_OFFSET", name);
} else if (ret == CL_INVALID_EVENT_WAIT_LIST) {
print_logE("--->%s = CL_INVALID_EVENT_WAIT_LIST", name);
} else if (ret == CL_INVALID_EVENT) {
print_logE("--->%s = CL_INVALID_EVENT", name);
} else if (ret == CL_INVALID_OPERATION) {
print_logE("--->%s = CL_INVALID_OPERATION", name);
} else if (ret == CL_INVALID_GL_OBJECT) {
print_logE("--->%s = CL_INVALID_GL_OBJECT", name);
} else if (ret == CL_INVALID_BUFFER_SIZE) {
print_logE("--->%s = CL_INVALID_BUFFER_SIZE", name);
} else if (ret == CL_INVALID_MIP_LEVEL) {
print_logE("--->%s = CL_INVALID_MIP_LEVEL", name);
} else if (ret == CL_INVALID_GLOBAL_WORK_SIZE) {
print_logE("--->%s = CL_INVALID_GLOBAL_WORK_SIZE", name);
} else if (ret == CL_INVALID_PROPERTY) {
print_logE("--->%s = CL_INVALID_PROPERTY", name);
} else {
print_logE("--->%s is failed", name);
}
}

关于c - 在 Opencl 中 clGetDeviceIDs 返回错误 CL_OUT_OF_HOST_MEMORY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38028748/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com