基本上,我草率地编写了一个 OpenCL 程序来使用这些全局变量进行赋值:
int devType = CL_DEVICE_TYPE_GPU;
cl_int err; /* Error code returned from api calls. */
size_t global; /* Global domain size for our calculation. */
size_t local; /* Local domain size for our calculation. */
cl_platform_id cpPlatform; /* openCL platform. */
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. */
/* Create data for the run. */
float *data = NULL; /* Original data set given to device. */
float *results = NULL; /* Results returned from device. */
unsigned int correct; /* Number of correct results returned. */
cl_mem input; /* Device memory used for the input array. */
cl_mem output; /* Device memory used for the output SUM. */
int rc = EXIT_FAILURE;
现在我正在尝试将它们全部本地化以整理程序。
我通过将全局变量 N 从上面的变量移到 main() 函数中来转换它。然后,我将每个使用 N 的函数头更新为将“int N”作为参数,并将 N 传递给任何需要它作为参数的函数调用。该程序按预期工作。
所以我想我要问的是,对于其余的这些变量,会这么简单吗?我了解通过引用和值传递的概念,并意识到某些函数可能会更改变量,因此我需要使用指针引用/取消引用。我担心的是我的指针理论有点粗糙,我担心我会遇到问题。我也不确定我定义的函数是否可以采用所有这些 cl 变量。
另外,在函数中使用相同的变量名有什么问题吗?
编辑:
正如我所担心的,在尝试本地化 device_id 时,以下函数确实会出现问题:
void deviceSetup(int devType) {
cl_platform_id cpPlatform; /* openCL platform. */
/* Connect to a compute device. */
if (CL_SUCCESS != clGetPlatformIDs (1, &cpPlatform, NULL))
die ("Error: Failed to find a platform!");
/* Get a device of the appropriate type. */
if (CL_SUCCESS != clGetDeviceIDs (cpPlatform, devType, 1, &device_id, NULL))
die ("Error: Failed to create a device group!");
}
/* Create a compute context. */
void createContext(cl_int err){
context = clCreateContext (0, 1, &device_id, NULL, NULL, &err);
if (!context || err != CL_SUCCESS)
die ("Error: Failed to create a compute context!");
}
/* Create a command commands. */
void createCommandQueue(cl_int err) {
commands = clCreateCommandQueue (context, device_id, 0, &err);
if (!commands || err != CL_SUCCESS)
die ("Error: Failed to create a command commands!");
}
void createAndCompile(cl_int err){
/* Create the compute program from the source buffer. */
program = clCreateProgramWithSource (context, 1,
(const char **) &KernelSource,
NULL, &err);
if (!program || err != CL_SUCCESS)
die ("Error: Failed to create compute program!");
/* Build the program executable. */
err = clBuildProgram (program, 0, NULL, NULL, NULL, NULL);
if (err != CL_SUCCESS)
{
size_t len;
char buffer[2048];
clGetProgramBuildInfo (program, device_id, CL_PROGRAM_BUILD_LOG,
sizeof (buffer), buffer, &len);
die ("Error: Failed to build program executable!\n%s", buffer);
}
}