package parallelencode;
import org.jocl.*;
import static org.jocl.CL.*;
public class ParallelEncode {
/**
* The source code of the OpenCL program to execute
*/
private static String programSource =
"__kernel void "+
"sampleKernel(__global const float *a,"+
" __global const float *b,"+
" __global uchar16 *c,"+
" __global char *d)"+
"{"+
" int gid = get_global_id(0);"+
" c[gid] = 'q';"+
" "+
" d[gid] = 'm';"+
"}";
/**
* The entry point of this sample
*
* @param args Not used
*/
public static void main(String args[])
{
// Create input- and output data
int n = 17;
float srcArrayA[] = new float[n];
float srcArrayB[] = new float[n];
char dstArray[] = new char[n];
char charArray[] = new char[n];
for (int i=0; i<n; i++)
{
srcArrayA[i] = i;
srcArrayB[i] = i;
}
Pointer srcA = Pointer.to(srcArrayA);
Pointer srcB = Pointer.to(srcArrayB);
Pointer dst = Pointer.to(dstArray);
Pointer cArr = Pointer.to(charArray);
// The platform, device type and device number
// that will be used
final int platformIndex = 0;
final long deviceType = CL_DEVICE_TYPE_ALL;
final int deviceIndex = 0;
// Enable exceptions and subsequently omit error checks in this sample
CL.setExceptionsEnabled(true);
// Obtain the number of platforms
int numPlatformsArray[] = new int[1];
clGetPlatformIDs(0, null, numPlatformsArray);
int numPlatforms = numPlatformsArray[0];
// Obtain a platform ID
cl_platform_id platforms[] = new cl_platform_id[numPlatforms];
clGetPlatformIDs(platforms.length, platforms, null);
cl_platform_id platform = platforms[platformIndex];
// Initialize the context properties
cl_context_properties contextProperties = new cl_context_properties();
contextProperties.addProperty(CL_CONTEXT_PLATFORM, platform);
// Obtain the number of devices for the platform
int numDevicesArray[] = new int[1];
clGetDeviceIDs(platform, deviceType, 0, null, numDevicesArray);
int numDevices = numDevicesArray[0];
// Obtain a device ID
cl_device_id devices[] = new cl_device_id[numDevices];
clGetDeviceIDs(platform, deviceType, numDevices, devices, null);
cl_device_id device = devices[deviceIndex];
// Create a context for the selected device
cl_context context = clCreateContext(
contextProperties, 1, new cl_device_id[]{device},
null, null, null);
// Create a command-queue for the selected device
cl_command_queue commandQueue =
clCreateCommandQueue(context, device, 0, null);
// Allocate the memory objects for the input- and output data
cl_mem memObjects[] = new cl_mem[4];
memObjects[0] = clCreateBuffer(context,
CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
Sizeof.cl_float * n, srcA, null);
memObjects[1] = clCreateBuffer(context,
CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
Sizeof.cl_float * n, srcB, null);
memObjects[2] = clCreateBuffer(context,
CL_MEM_READ_WRITE,
Sizeof.cl_char * n, null, null);
memObjects[3] = clCreateBuffer(context, CL_MEM_READ_WRITE, Sizeof.cl_char * n, null, null);
//char *h_rp = (char*)malloc(length);
//cl_mem d_rp = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, length, h_rp, &err);
//err = clSetKernelArg(ckKernel, 0, sizeof(cl_mem), &d_rp)
// Create the program from the source code
cl_program program = clCreateProgramWithSource(context,
1, new String[]{ programSource }, null, null);
// Build the program
clBuildProgram(program, 0, null, null, null, null);
// Create the kernel
cl_kernel kernel = clCreateKernel(program, "sampleKernel", null);
// Set the arguments for the kernel
clSetKernelArg(kernel, 0, Sizeof.cl_mem, Pointer.to(memObjects[0]));
clSetKernelArg(kernel, 1, Sizeof.cl_mem, Pointer.to(memObjects[1]));
clSetKernelArg(kernel, 2, Sizeof.cl_mem, Pointer.to(memObjects[2]));
clSetKernelArg(kernel, 3, Sizeof.cl_mem, Pointer.to(memObjects[3]));
// Set the work-item dimensions
long global_work_size[] = new long[]{n};
long local_work_size[] = new long[]{1};
// Execute the kernel
clEnqueueNDRangeKernel(commandQueue, kernel, 1, null,
global_work_size, local_work_size, 0, null, null);
// Read the output data
clEnqueueReadBuffer(commandQueue, memObjects[2], CL_TRUE, 0,
n * Sizeof.cl_char, dst, 0, null, null);
clEnqueueReadBuffer(commandQueue, memObjects[3], CL_TRUE, 0,
n * Sizeof.cl_char, cArr, 0, null, null);
// Release kernel, program, and memory objects
clReleaseMemObject(memObjects[0]);
clReleaseMemObject(memObjects[1]);
clReleaseMemObject(memObjects[2]);
clReleaseMemObject(memObjects[3]);
clReleaseKernel(kernel);
clReleaseProgram(program);
clReleaseCommandQueue(commandQueue);
clReleaseContext(context);
System.out.println(java.util.Arrays.toString(dstArray));
System.out.println(java.util.Arrays.toString(charArray));
}
}
结果:
[?, ?, ?, ?, ?, ?, ?, ?, q, , , , , , , , ]
[?, ?, ?, ?, ?, ?, ?, ?, m, , , , , , , , ]
为什么它不为数组中的每一个产生q,问题是什么?我尝试更改了一些内容,例如int gid = get_global_id(0);
到int gid = get_global_id(1);
,最终结果类似于[q, , , ...]
和[m, , , ...]
。有人可以解释一下,以及如何将多个char
作为输入传递给OpenCL内核吗?
答案 0 :(得分:2)
int n = 17;
除缓冲区副本外,这没关系。
clEnqueueReadBuffer(commandQueue, memObjects[3], CL_TRUE, 0,
n * Sizeof.cl_char, cArr, 0, null, null);
这将读取8个半char
值或17个字节。 java char(为2个字节)和设备端char(1个字节)之间不匹配。
这就是为什么你在第17个字节或第9个元素看到正确的q
。
类似的错误也是使用java bool数组。
uchar16也意味着16个字节。
clEnqueueReadBuffer(commandQueue, memObjects[2], CL_TRUE, 0,
n * Sizeof.cl_char, dst, 0, null, null);
除非每个元素都适用于所有16个元素,否则需要乘以16。如果你有17个元素,每个16字节,那么n * 16应该在那里,主机端(java)应该给出字节数组。
答案 1 :(得分:1)
the answer by huseyin tugrul buyukisik中已经提到了最重要的一点:
java char
由两个字节(16位)组成。在C和OpenCL中,char
是一个字节(8位)。
目前还不完全清楚你的程序最终会做什么,但正如huseyin所说:我非常确定你打算使用char2
而不是{{ 1}}(假设这将是一些与UTF16相关的程序 - 否则,主机代码将没有意义)。此外,您似乎以某种方式混合了输入和输出数组。
如果您现在将其更改为使用char16
,您可能还会遇到OpenCL的限制:根据restrictions that are mentioned on the Khronos Website:
大小小于32位的内置类型,即char,uchar,char2,uchar2,short,ushort和half有以下限制:
写入指针(或数组)类型为 char,uchar,char2, uchar2 ,short,ushort和half .. 。不受支持。
如果你更清楚地描述了该程序应该做什么(并在这里给我留言,作为评论),我将尝试添加一个小例子来演示如何处理这个问题。