public OpenCLPlatform(final Logger aLogger, final OpenCLOptions aOptions) {
logger = aLogger;
openCLOptions = aOptions;
final List<Platform> thePlatforms = new ArrayList<>();
// Enable exceptions and subsequently omit error checks in this sample
CL.setExceptionsEnabled(true);
final int[] theNumPlatforms = new int[1];
clGetPlatformIDs(theNumPlatforms.length, null, theNumPlatforms);
if (theNumPlatforms[0] == 0) {
throw new IllegalArgumentException("No OpenCL Platform found!");
}
// Find all plattforms and devices
final cl_platform_id[] thePlattforms = new cl_platform_id[theNumPlatforms[0]];
clGetPlatformIDs(thePlattforms.length, thePlattforms, null);
for (int i=0;i<theNumPlatforms[0];i++) {
final cl_platform_id theSelectedPlatform = thePlattforms[i];
try {
final Platform thePlatform = new Platform(theSelectedPlatform, () -> getString(theSelectedPlatform, CL_PLATFORM_NAME));
logger.info("Platform with id {} is {}", i, thePlatform.platformProperties.getName());
// Obtain the number of devices for the platform
final int[] numDevicesArray = new int[1];
clGetDeviceIDs(theSelectedPlatform, CL_DEVICE_TYPE_ALL, 0, null, numDevicesArray);
final int numDevices = numDevicesArray[0];
final cl_device_id[] devices = new cl_device_id[numDevices];
clGetDeviceIDs(theSelectedPlatform, CL_DEVICE_TYPE_ALL, numDevices, devices, null);
for (final cl_device_id theDevice : devices) {
final DeviceProperties theDeviceProperties = new DeviceProperties() {
@Override
public String getName() {
return getString(theDevice, CL_DEVICE_NAME);
}
@Override
public int getNumberOfComputeUnits() {
return getInt(theDevice, CL_DEVICE_MAX_COMPUTE_UNITS);
}
@Override
public long[] getMaxWorkItemSizes() {
return getSizes(theDevice, CL_DEVICE_MAX_WORK_ITEM_SIZES, 3);
}
@Override
public long getMaxWorkGroupSize() {
return getSize(theDevice, CL_DEVICE_MAX_WORK_GROUP_SIZE);
}
@Override
public long getClockFrequency() {
return getLong(theDevice, CL_DEVICE_MAX_CLOCK_FREQUENCY);
}
};
logger.info("Found device {} with #CU {} and max workgroup size {} " + theDeviceProperties.getName() ,theDeviceProperties
.getNumberOfComputeUnits() ,theDeviceProperties.getMaxWorkGroupSize());
thePlatform.deviceList.add(new Device(theDevice, theDeviceProperties));
}
thePlatforms.add(thePlatform);
} catch (final Exception e) {
logger.warn("Error processing device {}", i);
}
}
if (thePlatforms.isEmpty()) {
throw new IllegalStateException("No OpenCL platform detected");
}
int thePlatformID = thePlatforms.size() - 1;
final String theOverriddenPlatform = System.getProperty("OPENCL_PLATFORM");
if (theOverriddenPlatform != null && theOverriddenPlatform.length() > 0) {
thePlatformID = Integer.parseInt(theOverriddenPlatform);
}
int theDeviceID = 0;
final String theOverriddenDevice = System.getProperty("OPENCL_DEVICE");
if (theOverriddenDevice != null && theOverriddenDevice.length() > 0) {
theDeviceID = Integer.parseInt(theOverriddenDevice);
}
selectedPlatform = thePlatforms.get(thePlatformID);
logger.info("Device detection done, platform {} selected", thePlatformID);
selectedDevice = selectedPlatform.deviceList.get(theDeviceID);
}
@Test
@Ignore
public void testHandcrafttedCode() {
// Enable exceptions and subsequently omit error checks in this sample
CL.setExceptionsEnabled(true);
final int[] theNumPlatforms = new int[1];
clGetPlatformIDs(theNumPlatforms.length, null, theNumPlatforms);
if (theNumPlatforms[0] == 0) {
throw new IllegalArgumentException("No OpenCL Platform found!");
}
// Find all plattforms and devices
final cl_platform_id[] thePlattforms = new cl_platform_id[theNumPlatforms[0]];
clGetPlatformIDs(thePlattforms.length, thePlattforms, null);
final cl_platform_id theSelectedPlatform = thePlattforms[0];
// Obtain the number of devices for the platform
final int[] numDevicesArray = new int[1];
clGetDeviceIDs(theSelectedPlatform, CL_DEVICE_TYPE_ALL, 0, null, numDevicesArray);
final int numDevices = numDevicesArray[0];
final cl_device_id[] devices = new cl_device_id[numDevices];
clGetDeviceIDs(theSelectedPlatform, CL_DEVICE_TYPE_ALL, numDevices, devices, null);
final cl_context_properties contextProperties = new cl_context_properties();
contextProperties.addProperty(CL_CONTEXT_PLATFORM, theSelectedPlatform);
final cl_context context = clCreateContextFromType(
contextProperties, CL_DEVICE_TYPE_ALL, null, null, null);
final cl_command_queue commandQueue =
clCreateCommandQueue(context, devices[0], 0, null);
final String content = "__kernel void BytecoderKernel(__global float2* val$theA, __global float2* val$theResult) {\n" +
" int local_1_INT = get_global_id(0);\n" +
" float2 local_2_REFERENCE = normalize(*&val$theA[local_1_INT]);\n" +
" val$theResult[local_1_INT].x = local_2_REFERENCE.s1;\n" +
"}";
final cl_program theCLProgram = clCreateProgramWithSource(context,
1, new String[]{ content }, null, null);
try {
clBuildProgram(theCLProgram, 0, null, null, null, null);
} catch (final Exception e) {
throw new RuntimeException("Error compiling : " + content, e);
}
final cl_kernel theKernel = clCreateKernel(theCLProgram, "BytecoderKernel", null);
}