OpenCL clFinish never returns - opencl

I'm looking to start learning OpenCL. I was looking for a "hello world" example in order to first verify that everything behaves as expected on my system. After trying multiple such examples, I'm unable to find (or to make) one that works.
The code below is blocking on the call to clFinish and appears to never return.
#include <math.h>
#ifdef __APPLE__
#include <OpenCL/opencl.h>
#else
#include <CL/cl.h>
#endif
const char *KernelSource = "\n" \
"__kernel void hello( \n" \
" __global char* a, \n" \
" __global char* b, \n" \
" __global char* c, \n" \
" const unsigned int count) \n" \
"{ \n" \
" int i = get_global_id(0); \n" \
" if(i < count) \n" \
" c[i] = a[i] + b[i]; \n" \
"} \n" \
"\n";
#define DATA_SIZE (16)
int main(int argc, char** argv)
{
int err; // error code returned from api calls
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
cl_mem input; // device memory used for the input array
cl_mem input2; // device memory used for the input array
cl_mem output; // device memory used for the output array
size_t global; // global domain size for our calculation
size_t local; // local domain size for our calculation
int i;
unsigned int count = DATA_SIZE;
// Input data
char a[DATA_SIZE] = "Hello \0\0\0\0\0\0";
char b[DATA_SIZE] = {15, 10, 6, 0, -11, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
// Output data
char c[DATA_SIZE];
cl_platform_id platform;
unsigned int no_plat;
err = clGetPlatformIDs(1,&platform,&no_plat);
// Where to run
err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &device_id, NULL);
if (err != CL_SUCCESS) return -1;
context = clCreateContext(0, 1, &device_id, NULL, NULL, &err);
if (!context) return -1;
commands = clCreateCommandQueue(context, device_id, 0, &err);
if (!commands) return -1;
// What to run
program = clCreateProgramWithSource(context, 1, (const char **) & KernelSource, NULL, &err);
if (!program) return -1;
err = clBuildProgram(program, 0, NULL, NULL, NULL, NULL);
if (err != CL_SUCCESS) return -1;
kernel = clCreateKernel(program, "hello", &err);
if (!kernel || err != CL_SUCCESS) return -1;
// Create space for data and copy a and b to device (note that we could also use clEnqueueWriteBuffer to upload)
input = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, sizeof(char) * DATA_SIZE, a, NULL);
input2 = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, sizeof(char) * DATA_SIZE, b, NULL);
output = clCreateBuffer(context, CL_MEM_WRITE_ONLY, sizeof(char) * DATA_SIZE, NULL, NULL);
if (!input || !output) return -1;
// Send data
err = clSetKernelArg(kernel, 0, sizeof(cl_mem), &input);
err |= clSetKernelArg(kernel, 1, sizeof(cl_mem), &input2);
err |= clSetKernelArg(kernel, 2, sizeof(cl_mem), &output);
err |= clSetKernelArg(kernel, 3, sizeof(unsigned int), &count);
if (err != CL_SUCCESS) return -1;
local = DATA_SIZE;
// Run kernel!
global = DATA_SIZE; // count;
err = clEnqueueNDRangeKernel(commands, kernel, 1, NULL, &global, &local, 0, NULL, NULL);
if (err != CL_SUCCESS) return -1;
clFinish(commands);
// Read result
err = clEnqueueReadBuffer( commands, output, CL_TRUE, 0, sizeof(char) * count, c, 0, NULL, NULL );
if (err != CL_SUCCESS) return -1;
printf("%s\n", c);
// Clean up
clReleaseMemObject(input);
clReleaseMemObject(output);
clReleaseProgram(program);
clReleaseKernel(kernel);
clReleaseCommandQueue(commands);
clReleaseContext(context);
sleep(1); // Leopard pty bug workaround.
return 0;
}
I have a Ryzen 5 3400G and I'm using Linux.
gcc (Debian 10.2.1-6) 10.2.1 20210110
During compilation I see a couple of warnings (I couldn't find an example that gives no warnings).
from world.c:11:
/usr/include/CL/cl_version.h:22:9: note: ‘#pragma message: cl_version.h: CL_TARGET_OPENCL_VERSION is not defined. Defaulting to 300 (OpenCL 3.0)’
22 | #pragma message("cl_version.h: CL_TARGET_OPENCL_VERSION is not defined. Defaulting to 300 (OpenCL 3.0)")
| ^~~~~~~
world.c: In function ‘main’:
world.c:61:2: warning: ‘clCreateCommandQueue’ is deprecated [-Wdeprecated-declarations]
61 | commands = clCreateCommandQueue(context, device_id, 0, &err);
| ^~~~~~~~
In file included from world.c:11:
/usr/include/CL/cl.h:1906:1: note: declared here
1906 | clCreateCommandQueue(cl_context context,
| ^~~~~~~~~~~~~~~~~~~~
Am I doing something wrong or is the example wrong? In the latter case, where can I find a simple opencl program that works?
EDIT
I happened to restart the computer and clinfo works after the restart:
Number of platforms 1
Platform Name Clover
Platform Vendor Mesa
Platform Version OpenCL 1.1 Mesa 20.3.5
Platform Profile FULL_PROFILE
Platform Extensions cl_khr_icd
Platform Extensions function suffix MESA
Platform Name Clover
Number of devices 1
Device Name AMD Radeon(TM) Vega 11 Graphics (RAVEN, DRM 3.40.0, 4.19.0-12-amd64, LLVM 11.0.1)
Device Vendor AMD
Device Vendor ID 0x1002
Device Version OpenCL 1.1 Mesa 20.3.5
Driver Version 20.3.5
Device OpenCL C Version OpenCL C 1.1
Device Type GPU
Device Profile FULL_PROFILE
Device Available Yes
Compiler Available Yes
Max compute units 11
Max clock frequency 1400MHz
Max work item dimensions 3
Max work item sizes 256x256x256
Max work group size 256
Preferred work group size multiple (kernel) 64
Preferred / native vector sizes
char 16 / 16
short 8 / 8
int 4 / 4
long 2 / 2
half 0 / 0 (n/a)
float 4 / 4
double 2 / 2 (cl_khr_fp64)
Half-precision Floating-point support (n/a)
Single-precision Floating-point support (core)
Denormals No
Infinity and NANs Yes
Round to nearest Yes
Round to zero No
Round to infinity No
IEEE754-2008 fused multiply-add No
Support is emulated in software No
Correctly-rounded divide and sqrt operations No
Double-precision Floating-point support (cl_khr_fp64)
Denormals Yes
Infinity and NANs Yes
Round to nearest Yes
Round to zero Yes
Round to infinity Yes
IEEE754-2008 fused multiply-add Yes
Support is emulated in software No
Address bits 64, Little-Endian
Global memory size 31611129856 (29.44GiB)
Error Correction support No
Max memory allocation 22127790899 (20.61GiB)
Unified memory for Host and Device No
Minimum alignment for any data type 128 bytes
Alignment of base address 32768 bits (4096 bytes)
Global Memory cache type None
Image support No
Local memory type Local
Local memory size 32768 (32KiB)
Max number of constant args 16
Max constant buffer size 67108864 (64MiB)
Max size of kernel argument 1024
Queue properties
Out-of-order execution No
Profiling Yes
Profiling timer resolution 0ns
Execution capabilities
Run OpenCL kernels Yes
Run native kernels No
Device Extensions cl_khr_byte_addressable_store cl_khr_global_int32_base_atomics cl_khr_global_int32_extended_atomics cl_khr_local_int32_base_atomics cl_khr_local_int32_extended_atomics cl_khr_int64_base_atomics cl_khr_int64_extended_atomics cl_khr_fp64
NULL platform behavior
clGetPlatformInfo(NULL, CL_PLATFORM_NAME, ...) No platform
clGetDeviceIDs(NULL, CL_DEVICE_TYPE_ALL, ...) No platform
clCreateContext(NULL, ...) [default] No platform
clCreateContext(NULL, ...) [other] Success [MESA]
clCreateContextFromType(NULL, CL_DEVICE_TYPE_DEFAULT) Success (1)
Platform Name Clover
Device Name AMD Radeon(TM) Vega 11 Graphics (RAVEN, DRM 3.40.0, 4.19.0-12-amd64, LLVM 11.0.1)
clCreateContextFromType(NULL, CL_DEVICE_TYPE_CPU) No devices found in platform
clCreateContextFromType(NULL, CL_DEVICE_TYPE_GPU) Success (1)
Platform Name Clover
Device Name AMD Radeon(TM) Vega 11 Graphics (RAVEN, DRM 3.40.0, 4.19.0-12-amd64, LLVM 11.0.1)
clCreateContextFromType(NULL, CL_DEVICE_TYPE_ACCELERATOR) No devices found in platform
clCreateContextFromType(NULL, CL_DEVICE_TYPE_CUSTOM) No devices found in platform
clCreateContextFromType(NULL, CL_DEVICE_TYPE_ALL) Success (1)
Platform Name Clover
Device Name AMD Radeon(TM) Vega 11 Graphics (RAVEN, DRM 3.40.0, 4.19.0-12-amd64, LLVM 11.0.1)

I checked the sample code, it executes properly. So there must be an issue with your OpenCL Runtime installation. Did a reboot fix it already? Are you on Windows or Linux?

Related

Why clEnqueueNDRangeKernel does not return to the host immediately?

I checked out some answers, which indicates an immediate return after clEnqueueNDRangeKernel starts (for example in this link the 1st question). But in my case, the kernel executes serially (one by one).
A simple example of my code:
cl_int err;
cl_platform_id* platforms;
cl_device_id* devices;
cl_context context;
cl_command_queue queue;
cl_int gpu_platform_id;
SetupPlatform(&platforms, &gpu_platform_id);
SetupDevice(platforms, &devices, gpu_platform_id);
SetupContext(platforms, devices, &context, 1, gpu_platform_id);
SetupQueue(&queue, context, devices);
cl_program programs[2];
cl_event events[3];
...
programs[0] = SetupBuildProgramWithBinary(context, devices, "Kernel1_Opt.bin");
programs[1] = SetupBuildProgramWithBinary(context, devices, "Kernel2_Opt.bin");
cl_kernel kernels[2];
char kernel_name[][50] = { "kernel1","kernel2"};
SetupKernel(kernels, programs, 2, kernel_name);
...(some host data preparation)
err = clEnqueueNDRangeKernel(queue, kernels[0], 3, 0, kernel1_global_size, kernel1_local_size,
0, NULL, &events[0]); checkErr(err);
clWaitForEvents(1, &events[0]);
for(int i=0;i<2;i++){
...(some host data preparation)
err = clEnqueueNDRangeKernel(queue, kernels[1], 2, 0, kernel2_global_size,
kernel2_local_size, 0, NULL, &events[i+1]); checkErr(err);
}
I want the kernel execution in the for loop to be asynchronousso that the host data preparation in the second loop can be executed while the kernel in the first loop is executing. But the fact is that clEnqueueNDRangeKernel does not return immediately to the host, so I can not process the data preparation in the second loop immediately.
My queue setup is:
cl_int err = 0;
cl_command_queue_properties props[] = { CL_QUEUE_PROFILING_ENABLE |
CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE };
//choose 0th device
*queue = clCreateCommandQueue(context, devices[0], *props, &err); checkErr(err);
I am using Windows OS, NVIDIA RTX 3090 GPU card, kernels are compiled with OpenCL 3.0.

OpenCL pipes on intel CPU

I am working on my dissertation project trying to investigate if and when the use of OpenCL pipes can be useful also on CPUs (we already know they are widely used in FPGAs).
I am trying to implement even the simplest algorithm where I have a producer (writing to the pipe) and a consumer kernel (reading from the pipe). I would like to execute the two kernels in parallel and have a blocking behaviour for the pipe (read only when the pipe is not empty). Reading on Intel documentation at Intel_FPGA_Opencl it is explained that the attribute __attribute__((blocking)) declared for the pipe should have the read operation blocking when the pipe is empty. However, when I try to use the attribute I get __write_pipe_2_bl is undefined. I even tried to emulate the blocking behaviour with a while loop (as shown in the intel documentation) but the kernel stalls even if the pipe is not empty.
Furthermore, the kernels seem not to run in parallel unless I use two different command queues.
Kernel code:
#pragma OPENCL EXTENSION cl_intel_printf : enable
#define SIZE 1000
__kernel void pipe_writer(__global int *in,
write_only pipe int __attribute((depth(SIZE))) p)
{
for(int i = 0; i < SIZE; i++){
write_pipe(p, &in[i]);
printf("written: %d\n", in[i]);
}
}
__kernel void pipe_reader(__global int *out,
read_only pipe int __attribute((depth(SIZE))) p)
{
for(int i = 0; i < SIZE; i++){
while (read_pipe(p, &out[i]) == -1){
//printf("blocked read\n";
}
//int check = read_pipe(p, &out[i]);
printf("read: %d\n", out[i]);
}
}
Host Code:
#include <stdio.h>
#include <stdlib.h>
#ifdef __APPLE__
#include <OpenCL/opencl.h>
#else
#include <CL/cl.h>
#endif
#define MAX_SOURCE_SIZE (0x100000)
int main(void) {
// Create the two input vectors
int bb = 0;
int i;
const int LIST_SIZE = 1000;
int *A = (int*)malloc(sizeof(int)*LIST_SIZE);
int *B = (int*)malloc(sizeof(int)*LIST_SIZE);
for(i = 0; i < LIST_SIZE; i++) {
A[i] = i;
}
// Load the kernel source code into the array source_str
FILE *fp;
char *source_str;
size_t source_size;
fp = fopen("kernel.cl", "r");
if (!fp) {
fprintf(stderr, "Failed to load kernel.\n");
exit(1);
}
source_str = (char*)malloc(MAX_SOURCE_SIZE);
source_size = fread( source_str, 1, MAX_SOURCE_SIZE, fp);
fclose( fp );
// Get platform and device information
cl_platform_id platform_id = NULL;
cl_device_id device_id = NULL;
cl_uint ret_num_devices;
cl_uint ret_num_platforms;
cl_int ret = clGetPlatformIDs(1, &platform_id, &ret_num_platforms);
if(ret != CL_SUCCESS){
printf("getPlatformId, ERROR CODE: %d\n", ret);
bb=1;
}
ret = clGetDeviceIDs( platform_id, CL_DEVICE_TYPE_CPU, 1,
&device_id, &ret_num_devices);
if(ret != CL_SUCCESS){
printf("getDevice, ERROR CODE: %d\n", ret);
bb=1;
}
// Create an OpenCL context
cl_context context = clCreateContext( NULL, 1, &device_id, NULL, NULL, &ret);
if(ret != CL_SUCCESS){
printf("createContext, ERROR CODE: %d\n", ret);
bb=1;
}
// Create a command queue
cl_command_queue command_queue = clCreateCommandQueue(context, device_id, 0, &ret);
cl_command_queue command_queue2 = clCreateCommandQueue(context, device_id, 0, &ret);
if(ret != CL_SUCCESS){
printf("commandQueue, ERROR CODE: %d\n", ret);
bb=1;
}
// Create memory buffers on the device for each vector
cl_mem a_mem_obj = clCreateBuffer(context, CL_MEM_READ_ONLY,
LIST_SIZE * sizeof(int), NULL, &ret);
cl_mem b_mem_obj = clCreateBuffer(context, CL_MEM_WRITE_ONLY,
LIST_SIZE * sizeof(int), NULL, &ret);
if(ret != CL_SUCCESS){
printf("memobj, ERROR CODE: %d\n", ret);
bb=1;
}
ret = clEnqueueWriteBuffer(command_queue, a_mem_obj, CL_TRUE, 0,
LIST_SIZE * sizeof(int), A, 0, NULL, NULL);
if(ret != CL_SUCCESS){
printf("enqueuewritebuffer, ERROR CODE: %d\n", ret);
bb=1;
}
cl_program program = clCreateProgramWithSource(context, 1,
(const char **)&source_str, (const size_t *)&source_size, &ret);
if(ret != CL_SUCCESS){
printf("crateProgWithSource, ERROR CODE: %d\n", ret);
bb=1;
}
// Build the program
ret = clBuildProgram(program, 1, &device_id, "-cl-std=CL2.0", NULL, NULL);
if(ret != CL_SUCCESS){
printf("buildProgram, ERROR CODE: %d\n", ret);
bb=1;
}
/////Debug Kernel compilation:
size_t ret_val_size;
clGetProgramBuildInfo(program, device_id, CL_PROGRAM_BUILD_LOG, 0, NULL, &ret_val_size);
char * build_log = (char*) malloc(sizeof(char) * (ret_val_size));
clGetProgramBuildInfo(program, device_id, CL_PROGRAM_BUILD_LOG, ret_val_size, build_log, NULL);
printf("LOG: \n%s\n", build_log);
///////////////////////////////
cl_kernel pipe_writer = clCreateKernel(program, "pipe_writer", &ret);
if(ret != CL_SUCCESS){
printf("createkernelwriter, ERROR CODE: %d\n", ret);
bb=1;
}
cl_kernel pipe_reader = clCreateKernel(program, "pipe_reader", &ret);
if(ret != CL_SUCCESS){
printf("createkernelReader, ERROR CODE: %d\n", ret);
bb=1;
}
cl_mem pipe = clCreatePipe(context, 0, sizeof(cl_int), 1000, NULL, &ret);
if(ret != CL_SUCCESS){
printf("createPipe, ERROR CODE: %d\n", ret);
bb=1;
}
// Set the arguments of the kernel
ret = clSetKernelArg(pipe_writer, 0, sizeof(cl_mem), (void *)&a_mem_obj);
if(ret != CL_SUCCESS){
printf("setArgWriterZERO, ERROR CODE: %d\n", ret);
bb=1;
}
ret = clSetKernelArg(pipe_writer, 1, sizeof(cl_mem), &pipe);
if(ret != CL_SUCCESS){
printf("setArgWriterONE, ERROR CODE: %d\n", ret);
bb=1;
}
ret = clSetKernelArg(pipe_reader, 0, sizeof(cl_mem), (void *)&b_mem_obj);
if(ret != CL_SUCCESS){
printf("setArgReaderZERO, ERROR CODE: %d\n", ret);
bb=1;
}
ret = clSetKernelArg(pipe_reader, 1, sizeof(cl_mem), &pipe);
if(ret != CL_SUCCESS){
printf("setArgReaderONE, ERROR CODE: %d\n", ret);
bb=1;
}
// Execute the OpenCL kernel on the list
size_t global_item_size = 1; // Process the entire lists
size_t local_item_size = 1; // Divide work items into groups of 64
cl_event sync; //??
ret = clEnqueueTask (command_queue, pipe_writer, NULL, NULL, NULL);
if(ret != CL_SUCCESS){
printf("EnqueueKernelWriter, ERROR CODE: %d\n", ret);
bb=1;
}
if(ret != CL_SUCCESS){
printf("EnqueueKernelwriter, ERROR CODE: %d\n", ret);
bb=1;
}
ret = clEnqueueTask (command_queue2, pipe_reader, NULL, NULL, NULL);
if(ret != CL_SUCCESS){
printf("EnqueueKernelWriter, ERROR CODE: %d\n", ret);
bb=1;
}
if(ret != CL_SUCCESS){
printf("EnqueueKernelReader, ERROR CODE: %d\n", ret);
bb=1;
}
ret = clEnqueueReadBuffer(command_queue2, b_mem_obj, CL_TRUE, 0,
LIST_SIZE * sizeof(int), B, 0, NULL, NULL);
if(ret != CL_SUCCESS){
printf("EnqueueReadBuffer, ERROR CODE: %d\n", ret);
bb=1;
}
if(bb == 0){
// Display the result to the screen
for(i = 0; i < LIST_SIZE; i++)
printf("%d and %d\n", A[i], B[i]);
}
// Clean up
ret = clFlush(command_queue);
ret = clFinish(command_queue);
ret = clReleaseKernel(pipe_writer);
ret = clReleaseKernel(pipe_reader);
ret = clReleaseProgram(program);
ret = clReleaseMemObject(a_mem_obj);
ret = clReleaseMemObject(b_mem_obj);
ret = clReleaseCommandQueue(command_queue);
ret = clReleaseContext(context);
free(A);
free(B);
return 0;
}
And this is what I get about the CPU I am using running 'clinfo':
Platform Name Intel(R) CPU Runtime for OpenCL(TM) Applications
Number of devices 1
Device Name Intel(R) Xeon(R) CPU E5-2698 v4 # 2.20GHz
Device Vendor Intel(R) Corporation
Device Vendor ID 0x8086
Device Version OpenCL 2.1 (Build 0)
Driver Version 18.1.0.0920
Device OpenCL C Version OpenCL C 2.0
Device Type CPU
Device Profile FULL_PROFILE
Max compute units 80
Max clock frequency 2200MHz
Device Partition (core)
Max number of sub-devices 80
Supported partition types by counts, equally, by names (Intel)
Max work item dimensions 3
Max work item sizes 8192x8192x8192
Max work group size 8192
Preferred work group size multiple 128
Max sub-groups per work group 1
Preferred / native vector sizes
char 1 / 32
short 1 / 16
int 1 / 8
long 1 / 4
half 0 / 0 (n/a)
float 1 / 8
double 1 / 4 (cl_khr_fp64)
Half-precision Floating-point support (n/a)
Single-precision Floating-point support (core)
Denormals Yes
Infinity and NANs Yes
Round to nearest Yes
Round to zero No
Round to infinity No
IEEE754-2008 fused multiply-add No
Support is emulated in software No
Correctly-rounded divide and sqrt operations No
Double-precision Floating-point support (cl_khr_fp64)
Denormals Yes
Infinity and NANs Yes
Round to nearest Yes
Round to zero Yes
Round to infinity Yes
IEEE754-2008 fused multiply-add Yes
Support is emulated in software No
Correctly-rounded divide and sqrt operations No
Address bits 64, Little-Endian
Global memory size 540956721152 (503.8GiB)
Error Correction support No
Max memory allocation 135239180288 (126GiB)
Unified memory for Host and Device Yes
Shared Virtual Memory (SVM) capabilities (core)
Coarse-grained buffer sharing Yes
Fine-grained buffer sharing Yes
Fine-grained system sharing Yes
Atomics Yes
Minimum alignment for any data type 128 bytes
Alignment of base address 1024 bits (128 bytes)
Preferred alignment for atomics
SVM 64 bytes
Global 64 bytes
Local 0 bytes
Max size for global variable 65536 (64KiB)
Preferred total size of global vars 65536 (64KiB)
Global Memory cache type Read/Write
Global Memory cache size 262144
Global Memory cache line 64 bytes
Image support Yes
Max number of samplers per kernel 480
Max size for 1D images from buffer 8452448768 pixels
Max 1D or 2D image array size 2048 images
Base address alignment for 2D image buffers 64 bytes
Pitch alignment for 2D image buffers 64 bytes
Max 2D image size 16384x16384 pixels
Max 3D image size 2048x2048x2048 pixels
Max number of read image args 480
Max number of write image args 480
Max number of read/write image args 480
Max number of pipe args 16
Max active pipe reservations 3276
Max pipe packet size 1024
Local memory type Global
Local memory size 32768 (32KiB)
Max constant buffer size 131072 (128KiB)
Max number of constant args 480
Max size of kernel argument 3840 (3.75KiB)
Queue properties (on host)
Out-of-order execution Yes
Profiling Yes
Local thread execution (Intel) Yes
Queue properties (on device)
Out-of-order execution Yes
Profiling Yes
Preferred size 4294967295 (4GiB)
Max size 4294967295 (4GiB)
Max queues on device 4294967295
Max events on device 4294967295
Prefer user sync for interop No
Profiling timer resolution 1ns
Execution capabilities
Run OpenCL kernels Yes
Run native kernels Yes
Sub-group independent forward progress No
IL version SPIR-V_1.0
SPIR versions 1.2
printf() buffer size 1048576 (1024KiB)
Built-in kernels
Device Available Yes
Compiler Available Yes
Linker Available Yes
Device Extensions cl_khr_icd cl_khr_global_int32_base_atomics cl_khr_global_int32_extended_atomics cl_khr_local_int32_base_atomics cl_khr_local_int32_extended_atomics cl_khr_byte_addressable_store cl_khr_depth_images cl_khr_3d_image_writes cl_intel_exec_by_local_thread cl_khr_spir cl_khr_fp64 cl_khr_image2d_from_buffer cl_intel_vec_len_hint
OpenCL 2.0 Pipes used for Intel FPGA are quite different from the
standard OpenCL 2.0 used for CPU.
One important difference is that standard OpenCL 2.0 Pipes are not
meant to be used to establish a communication between concurrent
kernels. Pipes are a subclass of memory objects and their state is
enforced only at a syncronization point (see s3.3.1 Memory Consistency
of OpenCL 1.2 Specification), where syncronization point is either a
command queue barrier, or a wait on event (see s3.4.3
Synchronization). In other words, according to the OpenCL
Specification, data written to a pipe is only visible when the kernel
finishes execution.
Intel OpenCL for FPGA have additional features (extensions) to make
OpenCL 2.0 Pipes usable for FPGA: specifically, it guarantess that
kernels can communicate over pipes, and provides a few extensions to
make such communication simpler and more efficient (blocking pipes,
host pipes, pipe depth). All these features are not supported by
Intel OpenCL runtime for CPU.
However, for the purpose of your dissertation project, you can look at
the Fast Emulator from Intel FPGA SDK: basically it is a CPU runtime
that supports FPGA extensions, including pipes (with kernel-to-kernel communication) and host pipes. See
https://www.intel.com/content/dam/www/programmable/us/en/pdfs/literature/hb/opencl-sdk/aocl_programming_guide.pdf
s8.7. Using the Fast Emulator (Preview).
Furthermore, the kernels seem not to run in parallel unless I use two different command queues.
If you don't create a command queue with
CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE, a queue is in-order, meaning
that there is an implicit dependency between commands pushed to this
queue, so they cannot run in parallel.
Also, you should call clFlush(command_queue) before
clEnqueueReadBuffer(command_queue2, ...) to ensure that the writer
kernel is started before you make a blocking call for the reader.

Getting low Host-Device transfer rate with NVIDIA Quadro M4000

I am doing OpenCL on an NVIDIA Quadro M4000 installed on PCIe 3x16. On the card documentation, it is stated that the transfer rate CPU->GPU can go up to 15.7Gb/s while on my benchmark it is yielding only ~2.4Gb/s. I know that effective transfer rate can significantly differ from theoretical one but I wasn't expecting the difference to be that much.
Anyone has any experience with the quadro CPU->GPU data transfer.
Thanks
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<string>
#include<cmath>
#include<CL/cl.h>
#include <Windows.h>
using namespace std;
SYSTEMTIME last_call;
cl_platform_id platform_id = NULL;
cl_uint ret_num_platform;
cl_device_id device_id = NULL;
cl_uint ret_num_device;
cl_context context = NULL;
cl_command_queue command_queue = NULL;
cl_program program = NULL;
cl_kernel kernel = NULL;
cl_int err;
void _profile(char* msg){
SYSTEMTIME tmp;
clFinish(command_queue);
GetSystemTime(&tmp);
printf("__Profile --- %s --- : %d : %d : %d\n", msg, (tmp.wMinute - last_call.wMinute),
(tmp.wSecond - last_call.wSecond),
(tmp.wMilliseconds - last_call.wMilliseconds));
last_call = tmp;
}
int main()
{
// Reading Kernel Program
char *kernel_src_std = "__kernel void copy(__global const uchar *x, __global uchar *z){\
const int id = get_global_id(0);\
z[id] = x[id]; \
}";
size_t kernel_src_size = strlen(kernel_src_std);
// Create Input data
int w = 1920;
int h = 1080;
int c = 3;
float* input = (float*)malloc(w * h * c * sizeof(float));
for(int i=0;i<w*h*c;i++)
input[i] = (float)rand()/RAND_MAX;
// getting platform ID
err = clGetPlatformIDs(1, &platform_id, &ret_num_platform);
// Get Device ID
err = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_GPU, 1, &device_id, &ret_num_device );
// Create Context
context = clCreateContext(NULL,1,&device_id,NULL,NULL,&err);
// Create Command Queue
command_queue = clCreateCommandQueue(context, device_id, 0, &err);
// Create buffer Object
cl_mem buf_in = clCreateBuffer(context,CL_MEM_READ_ONLY, sizeof(float) * w*h*c,
0, &err);
cl_mem buf_out = clCreateBuffer(context,CL_MEM_WRITE_ONLY, sizeof(float) * w*h*c,
0, &err);
_profile("Start transfer input...");
// Copy Data from Host to Device
cl_event event[5];
err = clEnqueueWriteBuffer(command_queue,buf_in,CL_TRUE, 0, sizeof(float)*w*h*c,input,0,NULL, NULL);
_profile("End transfer input...");
// Create and Build Program
program = clCreateProgramWithSource(context, 1, (const char **)&kernel_src_std, 0, &err);
// Create Kernel
kernel = clCreateKernel(program,"copy",&err );
// Set Kernel Arguments
err = clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *)&buf_in);
err = clSetKernelArg(kernel, 1,sizeof(cl_mem), (void *)&buf_out);
// Execute Kernel
size_t ws[]={h*w*c};
size_t lws[]={100};
err = clEnqueueNDRangeKernel(command_queue, kernel, 1, NULL, ws, lws, 0, NULL, NULL);
// Create output buf
float* output = (float*)malloc(sizeof(float)*w*h*c);
// Read output Data, from Device to Host
err = clEnqueueReadBuffer(command_queue, buf_out, CL_TRUE, 0, sizeof(float)*w*h*c, output,NULL,NULL,NULL);
//Release Objects
clReleaseMemObject(buf_in);
clReleaseMemObject(buf_out);
clReleaseKernel(kernel);
clReleaseProgram(program);
clReleaseCommandQueue(command_queue);
clReleaseContext(context);
free(input);
free(output);
while(1);
return(0);
}
As your question is vague it is hard to pinpoint the exact reason for your poor performance. Some concrete code might help.
However, in your comments you say that you transfer an array of 6220800 floats. That is about 200 megabits to transfer. At maximum transfer rate (15.7Gb/s) that should give about 12ms.
However, with every new transfer request there is also a latency that is added, which --- for small transfers --- can effectively degrade your transfer rate.
Have you tried benchmarking on significantly bigger arrays (say, 100x the size)?
You're using blocking transfers which means you're incurring a stall on the read/write requests (additionally you're not using pinned memory, but you addressed that). At the moment, your code goes
Begin timing -> Write -> stall -> kernel -> read -> stall -> end timing. This will drastically affect the timings for your memory bandwidth transfer if your transfer scale is on the order of 2ms, as the stalls are comparable in size to this. You'll need to eliminate these stalls if you want to measure the bandwidth accurately

OpenCL Memory Transfer Issue (Err. code -6)

I'm dying a little inside. I've been working on this all day to no avail. I was having issues running some code that previously ran fine, so I wrote a short "toy" OpenCL program to try and figure out what was going on, but my toy program has me baffled and incredibly frustrated.
I'm working with an Nvidia 780i with 3Gb of global memory. It has a maximum allocation of ~780 Mb. At first, it wouldn't error out when I was intentionally over-allocating. Solved that (it was typographical, but the compiler/analyzer wasn't catching it). Now, even when trying to allocate WAY below what the device should be able to handle, I get an error code of -6 (CL_OUT_OF_HOST_MEMORY) on the second big buffer allocation.
I've been researching this error and I just can't track how it applies in this case. I have 32 gb of ram in the machine I'm using so there's certainly not a shortage there. I figure there's something that I just don't understand going on here.
It will allocate the first buffer alright, but then choke on the second. I basically just cannot allocate nearly the amount of global memory I both want and need to.
Any help is much appreciated. If you can help me with this and you're near LA I'll take you out for drinks. That's how frustrated I am.
Code and output is below for my machine.
Thanks,
John
Main program:
#define _CRT_SECURE_NO_WARNINGS
#define PROGRAM_FILE "kernels.cl"
#define KERNEL_NAME "test"
#include <CL/cl.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#define N_PROJ 4000
#define N_CHANNELS 736
#define N_ROWS 32
/* Find a GPU or CPU associated with the first available platform */
cl_device_id create_device() {
cl_platform_id platform;
cl_device_id dev;
int err;
/* Identify a platform */
err = clGetPlatformIDs(1, &platform, NULL);
if(err < 0) {
perror("Couldn't identify a platform");
exit(1);
}
/* Access a device */
err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &dev, NULL);
if(err == CL_DEVICE_NOT_FOUND) {
perror("Just a heads up: I'm not going to run on the GPU");
err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_CPU, 1, &dev, NULL);
}
if(err < 0) {
perror("Couldn't access any devices");
exit(1);
}
cl_ulong16 alloc_size,mem_size;
char name[40];
clGetDeviceInfo(dev,CL_DEVICE_MAX_MEM_ALLOC_SIZE,sizeof(cl_ulong16),&alloc_size,NULL);
clGetDeviceInfo(dev,CL_DEVICE_NAME,sizeof(name),name,NULL);
clGetDeviceInfo(dev,CL_DEVICE_GLOBAL_MEM_SIZE,sizeof(cl_ulong16),&mem_size,NULL);
printf("Using device: %s\n",name);
printf("Global memory size: %lu\n",mem_size);
printf("Max. allocation: %lu\n",alloc_size);
return dev;
}
/* Create program from a file and compile it */
cl_program build_program(cl_context ctx, cl_device_id dev, const char* filename) {
cl_program program;
FILE *program_handle;
char *program_buffer, *program_log;
size_t program_size, log_size;
int err;
/* Read program file and place content into buffer */
program_handle = fopen(filename, "r");
if(program_handle == NULL) {
perror("Couldn't find the program file");
exit(1);
}
fseek(program_handle, 0, SEEK_END);
program_size = ftell(program_handle)-13;
rewind(program_handle);
program_buffer = (char*)malloc(program_size + 1);
program_buffer[program_size] = '\0';
fread(program_buffer, sizeof(char), program_size, program_handle);
fclose(program_handle);
/* Create program from file */
program = clCreateProgramWithSource(ctx, 1,
(const char**)&program_buffer, &program_size, &err);
if(err < 0) {
perror("Couldn't create the program");
exit(1);
}
free(program_buffer);
/* Build program */
err = clBuildProgram(program, 0, NULL, NULL, NULL, NULL);
if(err < 0) {
/* Find size of log and print to std output */
clGetProgramBuildInfo(program, dev, CL_PROGRAM_BUILD_LOG,
0, NULL, &log_size);
program_log = (char*) malloc(log_size + 1);
program_log[log_size] = '\0';
clGetProgramBuildInfo(program, dev, CL_PROGRAM_BUILD_LOG,
log_size + 1, program_log, NULL);
printf("%s\n", program_log);
free(program_log);
exit(1);
}
return program;
}
int main(int argc, const char * argv[])
{
/* This file serves as a backbone for OpenCL programs. */
/* All the user needs to do is enter their OpenCL data */
/* structures, set kernel args, and kernel dispatches. */
/* Standard OCL structures */
cl_device_id device;
cl_context context;
cl_program program;
cl_kernel kernel;
cl_command_queue queue;
device=create_device();
context = clCreateContext(NULL, 1, &device, NULL, NULL, NULL);
program = build_program(context, device, PROGRAM_FILE);
queue = clCreateCommandQueue(context, device,0, NULL);
kernel = clCreateKernel(program, KERNEL_NAME, NULL);
/* User code goes here */
cl_int err;
/* Declare and set data */
int a[]={1,2,3};
float *rebin;
rebin =(float*) calloc(N_PROJ*N_CHANNELS*N_ROWS,sizeof(float));
float *mat;
mat =(float*) calloc(N_PROJ*N_CHANNELS*N_ROWS,sizeof(float));
printf("\nAllocation size: %lu\n",N_PROJ*N_CHANNELS*N_ROWS*sizeof(float));
/* Declare and set buffer objects */
cl_mem a_buff,rebin_buff,mat_buff;
printf("Total memory to be allocated: %lu\n",2*N_PROJ*N_CHANNELS*N_ROWS*sizeof(float)+sizeof(a) );
a_buff =clCreateBuffer(context,CL_MEM_COPY_HOST_PTR|CL_MEM_READ_WRITE,sizeof(a),a,NULL);
rebin_buff =clCreateBuffer(context,CL_MEM_COPY_HOST_PTR|CL_MEM_READ_WRITE,N_PROJ*N_CHANNELS*N_ROWS*sizeof(float),rebin,&err);
if (err<0){
printf("Error: %i\n",err);
perror("Couldn't create buffer 1");
exit(1);
}
mat_buff =clCreateBuffer(context,CL_MEM_COPY_HOST_PTR|CL_MEM_READ_WRITE,N_PROJ*N_CHANNELS*N_ROWS*sizeof(float),mat ,&err);
if (err<0){
printf("Error: %i\n",err);
perror("Couldn't create buffer 2");
exit(1);
}
/* Copy data over to the device */
err=clSetKernelArg(kernel,0,sizeof(cl_mem),&mat_buff);
if (err<0){
perror("Couldn't set kernel argument");
exit(1);
}
err=clSetKernelArg(kernel,1,sizeof(cl_mem),&rebin_buff);
err=clSetKernelArg(kernel,2,sizeof(cl_mem),&a_buff);
clEnqueueTask(queue,kernel,0,NULL,NULL);
clEnqueueReadBuffer(queue,mat_buff ,CL_TRUE,0,N_PROJ*N_CHANNELS*N_ROWS*sizeof(float),mat ,0,NULL,NULL);
clEnqueueReadBuffer(queue,rebin_buff,CL_TRUE,0,N_PROJ*N_CHANNELS*N_ROWS*sizeof(float),rebin,0,NULL,NULL);
clEnqueueReadBuffer(queue,a_buff, CL_TRUE,0,sizeof(a),a,0,NULL,NULL);
printf("%f %f %f\n",mat[1],mat[2],mat[3]);
printf("%f %f %f\n",rebin[1],rebin[2],rebin[3]);
printf("%i %i %i",a[0],a[1],a[2]);
/***********************/
clReleaseKernel(kernel);
clReleaseCommandQueue(queue);
clReleaseProgram(program);
clReleaseContext(context);
//clReleaseDevice(device);
printf("\n\nProgram apparently executed fully. \n");
return 0;
}
Kernel:
__kernel void test(__global float *mat,__global float *rebin,__global int *a){
a[0]=3;
a[1]=2;
a[2]=1;
rebin[1]=1.0f;
rebin[2]=2.0f;
rebin[3]=3.0f;
mat[1]=3.0f;
mat[2]=2.0f;
mat[3]=1.0f;
}
Console output for my machine:
Using device: GeForce GTX 780
Global memory size: 3221225472
Max. allocation: 805306368
Allocation size: 376832000
Total memory to be allocated: 753664012
Error: -6
Couldn't create buffer 2: No error
Process returned 1 (0x1) execution time : 0.435 s
Press any key to continue.

clGetDeviceIDs fails in OpenCL with error code -30

The output of the following program on my machine with ATI Firepro V8750 is as follows:
"Couldn't find any devices:No error"
(this happens at the call of first clGetDeviceIDs). the error code returned is -30. What does that mean?
I am not able to understand why it is unable to find the device. I have checked that CLinfo.exe lists my GPU along with the Intel CPU I am having. Can some one give my some pointers as to what is wrong here?
Additional info:
AMD APP SK 2.4
Firepro Driver: 8.911.3.3_VistaWin7_X32X64_135673
12-4_vista_win7_32_dd_ccc
Windows 7
Also I must mention that the firePro Driver's some componenets failed to get install.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef MAC
#include <OpenCL/cl.h>
#else
#include <CL/cl.h>
#endif
int main() {
/* Host/device data structures */
cl_platform_id platform;
cl_device_id *devices;
cl_uint num_devices, addr_data;
cl_int i, err;
/* Extension data */
char name_data[48], ext_data[4096];
/* Identify a platform */
err = clGetPlatformIDs(1, &platform, NULL);
if(err < 0) {
perror("Couldn't find any platforms");
exit(1);
}
/* Determine number of connected devices */
err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, NULL, &num_devices);
if(err < 0) {
perror("Couldn't find any devices");
exit(1);
}
/* Access connected devices */
devices = (cl_device_id*)
malloc(sizeof(cl_device_id) * num_devices);
clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU,
num_devices, devices, NULL);
/* Obtain data for each connected device */
for(i=0; i<num_devices; i++) {
err = clGetDeviceInfo(devices[i], CL_DEVICE_NAME,
sizeof(name_data), name_data, NULL);
if(err < 0) {
perror("Couldn't read extension data");
exit(1);
}
clGetDeviceInfo(devices[i], CL_DEVICE_ADDRESS_BITS,
sizeof(ext_data), &addr_data, NULL);
clGetDeviceInfo(devices[i], CL_DEVICE_EXTENSIONS,
sizeof(ext_data), ext_data, NULL);
printf("NAME: %s\nADDRESS_WIDTH: %u\nEXTENSIONS: %s",
name_data, addr_data, ext_data);
}
free(devices);
return 0;
}
Here is CLINFO output:
GPU:
CPU:
Why are the two highlighted versions different?
Could it be that you have multiple OpenCL platforms installed on your system? So, perhaps your first platform is a CPU-only playform, so the query for a GPU device fails.
EDIT:
Here's the problem: The first call to clGetDeviceIDs passes 1 for num_entries, but NULL for the devices pointer. I think you want to pass in 0 for num_entries.

Resources