我正在尝试学习OpenCL,但我甚至无法使一个简单的内核工作。
下面的代码来自书" OpenCL Programming by Example",我对其进行了修改,修改,修改......但仍然没有找到解决问题的方法。
每次我在我的PC(带有Radeon R3的AMD Athlon 5350 APU)中执行程序时,它会将结果打印为" 0.0000"。 如果我运行相同的可执行文件,在我的另一台机器(这是HD的克隆,所以一切都是相同的)与NVIDIA 1080 TI,程序输出" 3.000"结果。
我注意到编译器输出中有一个警告,因此我将过时的clCreateCommandQueue调用更改为clCreateCommandQueueWithProperties()。
现在......它只是段错误(使用printf()测试我知道clCreateCommandQueueWithProperties期间/之后的段错误。)
在配备NVIDIA GPU的系统上,它可以正常工作。
我错过了什么?
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <malloc.h>
#include <CL/cl.h>
#define VECTOR_NS 4096
#define VECTOR_SIZE (VECTOR_NS*sizeof(float))
static const char* saxpy_kernel =
"__kernel void saxpy_kernel(__global float *A, __global float *B, __global float *C)\n"
"{\n"
"int index = get_global_id(0);\n"
"C[0] = 3;\n"
"}\n"
;
int main(void)
{
int i;
float total;
float* A;
float* B;
float* C;
float* Cmapped;
cl_mem Acl;
cl_mem Bcl;
cl_mem Ccl;
cl_context context;
cl_platform_id* platforms;
cl_uint num_platforms;
cl_uint num_devices;
cl_command_queue queue;
cl_kernel kernel;
cl_int clStatus;
// Get platform and device information
platforms = NULL;
//Set up the Platform
clStatus = clGetPlatformIDs(0, NULL, &num_platforms);
platforms = (cl_platform_id *)malloc(sizeof(cl_platform_id)*num_platforms);
clStatus = clGetPlatformIDs(num_platforms, platforms, NULL);
//Get the devices list and choose the device you want to run on
cl_device_id* device_list = NULL;
clStatus = clGetDeviceIDs( platforms[0], CL_DEVICE_TYPE_GPU, 0, NULL, &num_devices);
device_list = (cl_device_id *) malloc(sizeof(cl_device_id)*num_devices);
clStatus = clGetDeviceIDs( platforms[0], CL_DEVICE_TYPE_GPU, num_devices, device_list, NULL);
// Create one OpenCL context for each device in the platform
context = clCreateContext( NULL, num_devices, device_list, NULL, NULL, &clStatus);
/* Create the command queue */
//queue = clCreateCommandQueue(context, device_list[0], 0, &clStatus);
queue = clCreateCommandQueueWithProperties(context, device_list[0], NULL, &clStatus);
if(clStatus != CL_SUCCESS){
fprintf(stderr, "ERROR: failed to execute the kernel: %d.\n", clStatus);
exit(1);
}
/* */
if((A = aligned_alloc(sysconf(_SC_PAGESIZE), VECTOR_SIZE)) == NULL){
fprintf(stderr, "ERROR: failed to allocate memory.\n");
exit(1);
}
if((B = aligned_alloc(sysconf(_SC_PAGESIZE), VECTOR_SIZE)) == NULL){
fprintf(stderr, "ERROR: failed to allocate memory.\n");
exit(1);
}
if((C = aligned_alloc(sysconf(_SC_PAGESIZE), VECTOR_SIZE)) == NULL){
fprintf(stderr, "ERROR: failed to allocate memory.\n");
exit(1);
}
/* Initialize it */
i = 0;
do {
A[i] = 1;
B[i] = 2;
C[i] = 0;
} while(++i != VECTOR_NS);
Acl = clCreateBuffer(context, CL_MEM_USE_HOST_PTR | CL_MEM_READ_WRITE, VECTOR_SIZE, A, &clStatus); // CL_MEM_READ_ONLY
Bcl = clCreateBuffer(context, CL_MEM_USE_HOST_PTR | CL_MEM_READ_WRITE, VECTOR_SIZE, B, &clStatus); // CL_MEM_READ_ONLY
Ccl = clCreateBuffer(context, CL_MEM_USE_HOST_PTR | CL_MEM_READ_WRITE, VECTOR_SIZE, C, &clStatus); // CL_MEM_WRITE_ONLY
// Create a program from the kernel source
// Build the program
cl_program program = clCreateProgramWithSource(context, 1, (const char**)&saxpy_kernel, NULL, &clStatus);
if(clStatus != CL_SUCCESS){
fprintf(stderr, "ERROR: failed to compile the OpenCL code.\n");
exit(1);
}
clStatus = clBuildProgram(program, 1, device_list, NULL, NULL, NULL);
if(clStatus != CL_SUCCESS){
fprintf(stderr, "ERROR: failed to compile the OpenCL code.\n");
exit(1);
}
// Create the OpenCL kernel
kernel = clCreateKernel(program, "saxpy_kernel", &clStatus);
// Set the arguments of the kernel
clSetKernelArg(kernel, 0, sizeof(cl_mem), (void*)&Acl);
clSetKernelArg(kernel, 1, sizeof(cl_mem), (void*)&Bcl);
clSetKernelArg(kernel, 2, sizeof(cl_mem), (void*)&Ccl);
// Execute the OpenCL kernel on the list
size_t global_size = VECTOR_NS; // Process the entire lists
size_t local_size = 1;
// Process one item at a time
clStatus = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, &global_size, &local_size, 0, NULL, NULL);
if(clStatus != CL_SUCCESS){
fprintf(stderr, "ERROR: failed to execute the kernel: %d.\n", clStatus);
exit(1);
}
//* Clean up and wait for all the comands to complete. */
clFlush(queue);
/* Display the result to the screen */
Cmapped = (float*) clEnqueueMapBuffer(queue, Ccl, CL_TRUE, CL_MAP_READ, 0, VECTOR_SIZE, 0, NULL, NULL, &clStatus); // CL_MEM_USE_HOST_PTR
if(clStatus != CL_SUCCESS){
fprintf(stderr, "ERROR: failed to execute the kernel: %d.\n", clStatus);
exit(1);
}
total = 0;
for(i = 0; i < VECTOR_NS; i++)
total += C[i];
printf("TOTAL: %f\n", total);
/* Clean up and wait for all the comands to complete. */
clFlush(queue);
clFinish(queue);
/* Finally release all OpenCL allocated objects and host buffers. */
clReleaseKernel(kernel);
clReleaseProgram(program);
clReleaseMemObject(Acl);
clReleaseMemObject(Bcl);
clReleaseMemObject(Ccl);
clReleaseCommandQueue(queue);
clReleaseContext(context);
free(platforms);
free(device_list);
return 0;
}
答案 0 :(得分:0)
虽然您的代码在我的计算机上按原样运行,但我认为这可能会导致您的问题。电话
Cmapped = (float*)clEnqueueMapBuffer(queue, Ccl, CL_TRUE, CL_MAP_READ, 0, VECTOR_SIZE, 0, NULL, NULL, &clStatus);
更改Cmapped
,但您尝试从
C
缓冲区中读取
total = 0;
for(i = 0; i < VECTOR_NS; i++)
total += C[i];
这应该是
total = 0;
for(i = 0; i < VECTOR_NS; i++)
total += Cmapped[i];
但是,由于您使用CL_MEM_USE_HOST_PTR
标志创建了缓冲区,因此如果使用clEnqueueReadBuffer
,则使用原始指针作为目标,OpenCL驱动程序也应该能够优化复制操作:
clEnqueueReadBuffer(queue, Ccl, CL_TRUE, 0, VECTOR_SIZE, C, 0, NULL, NULL);
如果OpenCL实现没有将数据缓存到设备内存,那么这应该是无操作。