@H2CO3
这是我的主要代码:
#pragma OPENCL EXTENSION cl_ amd_ printf : enable
#define PROGRAM_FILE "matvec.cl"
#define KERNEL_FUNC "matvec_mult"
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#ifdef MAC
#include <OpenCL/cl.h>
#else
#include <CL/cl.h>
#endif
int main() {
cl_platform_id platform;
cl_device_id device;
cl_context context;
cl_command_queue queue;
cl_int i,err;
cl_int length = 512;
cl_program program;
FILE *program_handle;
char *program_buffer;
size_t program_size;
cl_kernel kernel;
size_t work_units_per_kernel;
float mat_a[length], mat_b[length];
cl_mem mat_a_buff, mat_b_buff, res_buff;
cl_event timing_event;
cl_ulong time_start, time_end , read_time;
//********************************************************************
// making matrix a & b
for(i=0; i<length; i++) {
mat_a[i] = i ;
mat_b[i] = i +1;
}
//********************************************************************
clGetPlatformIDs(2, &platform, NULL);
clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1 , &device, NULL);
context = clCreateContext(NULL, 1, &device, NULL,NULL, &err);
program_handle = fopen(PROGRAM_FILE, "r");
fseek(program_handle, 0, SEEK_END);
program_size = ftell(program_handle);
rewind(program_handle);
program_buffer = (char*)malloc(program_size + 1);
program_buffer[program_size] = '\0';
//*******************************************************************
// !!!!!!!!! reading buffer :
fread(program_buffer, sizeof(char), program_size,program_handle);
//*******************************************************************
fclose(program_handle);
program = clCreateProgramWithSource(context, 1,(const char**)&program_buffer,&program_size,&err);
free(program_buffer);
clBuildProgram(program, 0 , NULL , NULL , NULL , NULL);
// !!! Creating & Queueing Kernel :
//*********************************************************************************
kernel = clCreateKernel(program , KERNEL_FUNC , &err);
queue = clCreateCommandQueue(context, device , CL_QUEUE_PROFILING_ENABLE , &err);
//*********************************************************************************
mat_a_buff = clCreateBuffer(context, CL_MEM_READ_ONLY |CL_MEM_COPY_HOST_PTR,
sizeof(float)*4, mat_a, &err);
mat_b_buff = clCreateBuffer(context, CL_MEM_READ_ONLY |CL_MEM_COPY_HOST_PTR,
sizeof(float)*4 , mat_b, &err);
res_buff = clCreateBuffer(context, CL_MEM_WRITE_ONLY,sizeof(float)*4, NULL, &err);
// !!! Setting Kernel Arguments :
clSetKernelArg(kernel, 0, sizeof(cl_mem), &mat_a_buff);
clSetKernelArg(kernel, 0, sizeof(cl_mem), &mat_b_buff);
clSetKernelArg(kernel, 1, sizeof(cl_mem), &res_buff);
work_units_per_kernel = 512;
// !!! Parallelism with clEnqueueNDRangekernel structure
//**********************************************************************************************
clEnqueueNDRangeKernel(queue, kernel, 1, NULL,&work_units_per_kernel,
NULL, 0, NULL, &timing_event);
//**********************************************************************************************
//******************** Profilling : *****************************
clGetEventProfilingInfo(timing_event, CL_PROFILING_COMMAND_START,
sizeof(time_start), &time_start, NULL);
clGetEventProfilingInfo(timing_event, CL_PROFILING_COMMAND_END,
sizeof(time_end), &time_end, NULL);
read_time = time_end - time_start;
printf("The average time is : %lu\n", read_time);
//*********************************************************************************************
clReleaseMemObject(mat_a_buff);
clReleaseMemObject(mat_b_buff);
clReleaseMemObject(res_buff);
clReleaseKernel(kernel);
clReleaseCommandQueue(queue);
clReleaseProgram(program);
clReleaseContext(context);
return 0;
}
但是虽然 cl_ulong 是 unsigned long ,但带有 %lu 标志的 printf 不会起作用。这是错误代码行:(格式 %lu 需要 long unsigned int 类型的参数,但参数 2 的类型为 cl_ulong[-Wformat])
所以我不知道是什么问题以及为什么 printf 不能正常工作???