View Javadoc
1   package net.bmahe.genetics4j.gpu.opencl;
2   
3   import org.apache.commons.lang3.StringUtils;
4   import org.apache.commons.lang3.Validate;
5   import org.apache.logging.log4j.LogManager;
6   import org.apache.logging.log4j.Logger;
7   import org.jocl.CL;
8   import org.jocl.cl_device_id;
9   import org.jocl.cl_platform_id;
10  
11  import net.bmahe.genetics4j.gpu.opencl.model.Device;
12  import net.bmahe.genetics4j.gpu.opencl.model.DeviceType;
13  
14  public class DeviceReader {
15  	public static final Logger logger = LogManager.getLogger(DeviceReader.class);
16  
17  	public Device read(final cl_platform_id platformId, final cl_device_id deviceId) {
18  		Validate.notNull(platformId);
19  		Validate.notNull(deviceId);
20  
21  		final var deviceBuilder = Device.builder();
22  		deviceBuilder.deviceId(deviceId);
23  
24  		final String name = DeviceUtils.getDeviceInfoString(deviceId, CL.CL_DEVICE_NAME);
25  		deviceBuilder.name(name);
26  
27  		final String vendor = DeviceUtils.getDeviceInfoString(deviceId, CL.CL_DEVICE_VENDOR);
28  		deviceBuilder.vendor(vendor);
29  
30  		final String deviceVersion = DeviceUtils.getDeviceInfoString(deviceId, CL.CL_DEVICE_VERSION);
31  		deviceBuilder.deviceVersion(deviceVersion);
32  
33  		final String driverVersion = DeviceUtils.getDeviceInfoString(deviceId, CL.CL_DRIVER_VERSION);
34  		deviceBuilder.driverVersion(driverVersion);
35  
36  		final String builtInKernelsStr = DeviceUtils.getDeviceInfoString(deviceId, CL.CL_DEVICE_BUILT_IN_KERNELS);
37  		if (StringUtils.isNotBlank(builtInKernelsStr)) {
38  			final String[] builtInKernelsArr = builtInKernelsStr.split(";");
39  			if (builtInKernelsArr.length > 0) {
40  				deviceBuilder.addBuiltInKernels(builtInKernelsArr);
41  			}
42  		}
43  
44  		final long deviceTypeLong = DeviceUtils.getDeviceInfoLong(deviceId, CL.CL_DEVICE_TYPE);
45  		if (deviceTypeLong == CL.CL_DEVICE_TYPE_ALL) {
46  			deviceBuilder.addDeviceType(DeviceType.ALL);
47  		} else {
48  
49  			if ((deviceTypeLong & CL.CL_DEVICE_TYPE_DEFAULT) != 0) {
50  				deviceBuilder.addDeviceType(DeviceType.DEFAULT);
51  			}
52  			if ((deviceTypeLong & CL.CL_DEVICE_TYPE_CPU) != 0) {
53  				deviceBuilder.addDeviceType(DeviceType.CPU);
54  			}
55  			if ((deviceTypeLong & CL.CL_DEVICE_TYPE_GPU) != 0) {
56  				deviceBuilder.addDeviceType(DeviceType.GPU);
57  			}
58  			if ((deviceTypeLong & CL.CL_DEVICE_TYPE_ACCELERATOR) != 0) {
59  				deviceBuilder.addDeviceType(DeviceType.ACCELERATOR);
60  			}
61  			if ((deviceTypeLong & CL.CL_DEVICE_TYPE_CUSTOM) != 0) {
62  				deviceBuilder.addDeviceType(DeviceType.CUSTOM);
63  			}
64  		}
65  
66  		final int maxComputeUnits = DeviceUtils.getDeviceInfoInt(deviceId, CL.CL_DEVICE_MAX_COMPUTE_UNITS);
67  		deviceBuilder.maxComputeUnits(maxComputeUnits);
68  
69  		final int maxWorkItemDimensions = DeviceUtils.getDeviceInfoInt(deviceId, CL.CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS);
70  		deviceBuilder.maxWorkItemDimensions(maxWorkItemDimensions);
71  
72  		final int maxClockFrequency = DeviceUtils.getDeviceInfoInt(deviceId, CL.CL_DEVICE_MAX_CLOCK_FREQUENCY);
73  		deviceBuilder.maxClockFrequency(maxClockFrequency);
74  
75  		final int hasImageSupport = DeviceUtils.getDeviceInfoInt(deviceId, CL.CL_DEVICE_IMAGE_SUPPORT);
76  		deviceBuilder.imageSupport(hasImageSupport != 0);
77  
78  		final long maxWorkGroupSize = DeviceUtils.getDeviceInfoLong(deviceId, CL.CL_DEVICE_MAX_WORK_GROUP_SIZE);
79  		deviceBuilder.maxWorkGroupSize(maxWorkGroupSize);
80  
81  		final int preferredVectorWidthFloat = DeviceUtils.getDeviceInfoInt(deviceId,
82  				CL.CL_DEVICE_PREFERRED_VECTOR_WIDTH_FLOAT);
83  		deviceBuilder.preferredVectorWidthFloat(preferredVectorWidthFloat);
84  
85  		final long[] maxWorkItemSizes = DeviceUtils
86  				.getDeviceInfoLongArray(deviceId, CL.CL_DEVICE_MAX_WORK_ITEM_SIZES, maxWorkItemDimensions);
87  		deviceBuilder.maxWorkItemSizes(maxWorkItemSizes);
88  
89  		return deviceBuilder.build();
90  	}
91  }