View Javadoc
1   package net.bmahe.genetics4j.gpu.opencl;
2   
3   import java.util.Arrays;
4   import java.util.List;
5   
6   import org.apache.commons.lang3.Validate;
7   import org.jocl.CL;
8   import org.jocl.Pointer;
9   import org.jocl.Sizeof;
10  import org.jocl.cl_device_id;
11  import org.jocl.cl_platform_id;
12  
13  public class DeviceUtils {
14  
15  	private DeviceUtils() {
16  
17  	}
18  
19  	public static int numDevices(final cl_platform_id platformId, final long deviceType) {
20  		Validate.notNull(platformId);
21  
22  		final int numDevices[] = new int[1];
23  		CL.clGetDeviceIDs(platformId, deviceType, 0, null, numDevices);
24  
25  		return numDevices[0];
26  	}
27  
28  	public static int numDevices(final cl_platform_id platformId) {
29  		return numDevices(platformId, CL.CL_DEVICE_TYPE_ALL);
30  	}
31  
32  	public static List<cl_device_id> getDeviceIds(final cl_platform_id platformId, final int numDevices,
33  			final long deviceType) {
34  		Validate.notNull(platformId);
35  		Validate.isTrue(numDevices > 0);
36  
37  		cl_device_id deviceIds[] = new cl_device_id[numDevices];
38  		CL.clGetDeviceIDs(platformId, deviceType, numDevices, deviceIds, null);
39  
40  		return Arrays.asList(deviceIds);
41  	}
42  
43  	public static List<cl_device_id> getDeviceIds(final cl_platform_id platformId, final int numDevices) {
44  		return getDeviceIds(platformId, numDevices, CL.CL_DEVICE_TYPE_ALL);
45  	}
46  
47  	public static String getDeviceInfoString(final cl_device_id deviceId, final int parameter) {
48  		Validate.notNull(deviceId);
49  
50  		final long[] size = new long[1];
51  		CL.clGetDeviceInfo(deviceId, parameter, 0, null, size);
52  
53  		final byte[] buffer = new byte[(int) size[0]];
54  		CL.clGetDeviceInfo(deviceId, parameter, buffer.length, Pointer.to(buffer), null);
55  
56  		return new String(buffer, 0, buffer.length - 1);
57  	}
58  
59  	public static long[] getDeviceInfoLongArray(final cl_device_id deviceId, final int parameter, final int size) {
60  		Validate.notNull(deviceId);
61  		Validate.isTrue(size > 0);
62  
63  		final long[] values = new long[size];
64  		CL.clGetDeviceInfo(deviceId, parameter, Sizeof.cl_long * size, Pointer.to(values), null);
65  
66  		return values;
67  	}
68  
69  	public static long getDeviceInfoLong(final cl_device_id deviceId, final int parameter) {
70  		Validate.notNull(deviceId);
71  
72  		final long[] values = getDeviceInfoLongArray(deviceId, parameter, 1);
73  
74  		return values[0];
75  	}
76  
77  	public static int getDeviceInfoInt(final cl_device_id deviceId, final int parameter) {
78  		Validate.notNull(deviceId);
79  
80  		final int[] values = new int[1];
81  		CL.clGetDeviceInfo(deviceId, parameter, Sizeof.cl_int, Pointer.to(values), null);
82  
83  		return values[0];
84  	}
85  }