【发布时间】:2017-04-17 08:32:30
【问题描述】:
过去几周我一直在研究 c/c++ OpenCL 解决方案。对于我的解决方案,我需要将一个类从我的 CPU(主机)传递到 GPU(设备)。当我尝试将类作为参数传递时,它会给出错误“未知类型标识符类”。我怀疑英特尔平台上的 OpenCL 是否允许我们将类传递给内核或任何解决方法都可以使用。在 CUDA 中,我看到了一些示例,它在平台上运行得非常好。但是,关于 OpenCL,我找不到任何参考资料,也没有与此查询相关的示例。我将非常感谢有关此问题的任何帮助。我在英特尔网站上发布了同样的问题,但无济于事。如果有人能很好地帮助我了解我哪里出错了或者我应该如何处理这个问题,我会非常感谢你。
//主机端代码
#include<stdio.h>
#include<iostream>
#include"CL/cl.h"
class test
{
public:
cl_int a;
cl_char b;
};
int main()
{
test *tempstruct = new test;
cl_platform_id platfrom_id;
cl_device_id device_id; // compute device id
cl_context context; // compute context
cl_command_queue commands; // compute command queue
cl_program program; // compute program
cl_kernel kernel; // compute kernel
int err;
err = clGetPlatformIDs(1, &platfrom_id, NULL);
if (err != CL_SUCCESS)
{
printf("Error: Failed to create a platfrom group!\n");
return -1;
}
err = clGetDeviceIDs(platfrom_id, CL_DEVICE_TYPE_GPU, 1, &device_id, NULL);
if (err != CL_SUCCESS)
{
printf("Error: Failed to create a device group!\n");
return -1;
}
context = clCreateContext(0, 1, &device_id, NULL, NULL, NULL);
if (!context)
{
printf("Error: Failed to create a compute context!\n");
return -1;
}
commands = clCreateCommandQueue(context, device_id, 0, NULL);
if (!commands)
{
printf("Error: Failed to create a command commands!\n");
return -1;
}
#define MAX_SOURCE_SIZE (0x100000)
FILE *fp, *fp1;
char filename[] = "Template.cl";
fp = fopen(filename, "r");
if (fp == NULL)
{
printf("\n file not found \n");
return -1;
}
char * source_str = (char*)malloc(MAX_SOURCE_SIZE);
size_t size = fread(source_str, 1, MAX_SOURCE_SIZE, fp);
fclose(fp);
cl_mem classobj = clCreateBuffer(context, CL_MEM_USE_HOST_PTR, sizeof(tempstruct), &tempstruct, &err);
if (err != CL_SUCCESS)
{
printf("Error: Failed to allocate device memory!\n");
return -1;
}
program = clCreateProgramWithSource(context, 1, (const char **)& source_str, (const size_t *)&size, &err);
if (!program)
{
printf("Error: Failed to create program with source!\n");
return -1;
}
err = clBuildProgram(program, 1, &device_id, NULL, NULL, NULL);
if (err != CL_SUCCESS)
{
printf("Error: Failed to build program executable!\n");
return -1;
}
test *resptr = (test *)clEnqueueMapBuffer(commands, classobj, CL_TRUE, CL_MAP_WRITE, NULL, sizeof(test), NULL, NULL, NULL, &err);
// INITIALISATION OF CLASS
tempstruct->a = 10;
if (!resptr)
{
printf("Error: Failed to create enqueuemapbuffer!\n");
return -1;
}
err = clEnqueueUnmapMemObject(commands, classobj, resptr, 0, NULL, NULL);
if (err != CL_SUCCESS)
{
printf("Error: Failed to write to source array!\n");
return -1;
}
kernel = clCreateKernel(program, "CLASS", &err);
if (err != CL_SUCCESS)
{
printf("Error: Failed to create compute kernel!\n");
return -1;
}
err = clSetKernelArg(kernel, 0, sizeof(cl_mem), &classobj);
if (err != CL_SUCCESS)
{
printf("Error: Failed to set kernel arguments! %d\n", err);
return -1;
}
size_t globalsize = 1;
size_t local = 1;
err = clEnqueueNDRangeKernel(commands, kernel, 1, NULL, &globalsize, &local, 0, NULL, NULL);
if (err)
{
printf("Error: Failed to execute nd range!\n");
return -1;
}
test *resptr1 = (test *)clEnqueueMapBuffer(commands, classobj, CL_TRUE, CL_MAP_READ, NULL, sizeof(test), NULL, NULL, NULL, &err);
err = clEnqueueUnmapMemObject(commands, classobj, resptr1, 0, NULL, NULL);
if (err != CL_SUCCESS)
{
printf("Error: Failed to read output array! %d\n", err);
return -1;
}
// again i am printing the class value
printf("\n in cpu side = %d\n", tempstruct->a);
}
//HOST END
//DEVICE SIDE(KERNEL CODE)
// filename : Template.cl
class test
{
public:
cl_int a;
cl_char b;
};
__kernel void CLASS(__global test *inclass )
{
inclass->a = 10;
printf("\n in kernel side = %d \n",inclass->a);
}
//内核结束
错误:
我只在内核端面临这些错误
1) 错误 Tempalte.CL 未知类型名称“测试”
2) 错误 Tempalte.CL 预期为 ';'在顶级声明符之后
3) 错误 Tempalte.CL 程序范围变量需要在常量地址空间中声明
4) 错误 Tempalte.CL unknown type name 'class'
查询:
问)我的主要问题是如何在英特尔架构中将 CLASS 传递给内核
* 我已经成功地将课程传递给 AMD 的内核。每当我在英特尔方面尝试使用相同的代码时,它都会显示上述四个错误。
* 是否有任何替代方法可以将类传递给英特尔的内核,或者是否可以将类传递给英特尔架构的内核?
【问题讨论】:
-
你需要 sycl 。它让您几乎可以使用纯粹的 C++。您可以发布出错的代码吗?您使用哪个版本的 opencl?
-
@huseyin tugrul buyukisik,我已根据您的要求添加了代码。让我知道你能从中找到什么。提前致谢。