154 lines
4.6 KiB
Java
154 lines
4.6 KiB
Java
package electrosphere.opencl;
|
|
|
|
import java.nio.ByteBuffer;
|
|
import java.nio.CharBuffer;
|
|
import java.nio.IntBuffer;
|
|
import java.nio.LongBuffer;
|
|
import java.util.ArrayList;
|
|
import java.util.Collections;
|
|
import java.util.HashSet;
|
|
import java.util.LinkedList;
|
|
import java.util.List;
|
|
import java.util.Set;
|
|
|
|
import org.lwjgl.PointerBuffer;
|
|
import org.lwjgl.opencl.CL;
|
|
import org.lwjgl.opencl.CL30;
|
|
import org.lwjgl.opencl.CLCapabilities;
|
|
import org.lwjgl.system.MemoryStack;
|
|
|
|
/**
|
|
* An object that encapsulates an openCL platform
|
|
*/
|
|
public class CLPlatform {
|
|
|
|
//the id of the platform
|
|
long id;
|
|
|
|
//the capabilities of the platform
|
|
CLCapabilities caps;
|
|
|
|
//Information about the platform
|
|
String vendor;
|
|
String name;
|
|
String version;
|
|
|
|
// the list of all cl devices on this platform
|
|
List<CLDevice> clDevices;
|
|
|
|
/**
|
|
* Creates a platform object
|
|
* @param id The id of the platform
|
|
*/
|
|
protected CLPlatform(long id, CLCapabilities caps, String vendor, String name, String version, List<CLDevice> clDevices){
|
|
this.id = id;
|
|
this.caps = caps;
|
|
this.vendor = vendor;
|
|
this.name = name;
|
|
this.version = version;
|
|
this.clDevices = clDevices;
|
|
}
|
|
|
|
|
|
/**
|
|
* Gets a set containing all currently available platforms
|
|
* @return The set containing all currently available platforms
|
|
*/
|
|
public static Set<CLPlatform> getPlatforms(){
|
|
Set<CLPlatform> platforms = new HashSet<CLPlatform>();
|
|
|
|
try(MemoryStack stack = MemoryStack.stackPush()){
|
|
//check if there are any platforms
|
|
IntBuffer pi = stack.mallocInt(1);
|
|
CLUtilities.checkCLError(CL30.clGetPlatformIDs(null, pi));
|
|
if (pi.get(0) == 0) {
|
|
throw new IllegalStateException("No OpenCL platforms found.");
|
|
}
|
|
//if there are platforms, get all platform IDs
|
|
PointerBuffer platformIDs = stack.mallocPointer(pi.get(0));
|
|
CLUtilities.checkCLError(CL30.clGetPlatformIDs(platformIDs, (IntBuffer)null));
|
|
|
|
//add all platforms to the returned list
|
|
for (int i = 0; i < platformIDs.capacity(); i++) {
|
|
//get basic identification
|
|
long platformId = platformIDs.get(i);
|
|
CLCapabilities caps = CL.createPlatformCapabilities(platformId);
|
|
String vendor = CLUtilities.getPlatformInfoStringUTF8(platformId, CL30.CL_PLATFORM_VENDOR);
|
|
String name = CLUtilities.getPlatformInfoStringUTF8(platformId, CL30.CL_PLATFORM_NAME);
|
|
String version = CLUtilities.getPlatformInfoStringUTF8(platformId, CL30.CL_PLATFORM_VERSION);
|
|
|
|
|
|
//get devices on the platform
|
|
int errcode = CL30.clGetDeviceIDs(platformId, CL30.CL_DEVICE_TYPE_GPU, null, pi);
|
|
List<CLDevice> devices = new LinkedList<CLDevice>();
|
|
if(errcode != CL30.CL_DEVICE_NOT_FOUND){
|
|
CLUtilities.checkCLError(errcode);
|
|
PointerBuffer deviceIDs = stack.mallocPointer(pi.get(0));
|
|
CLUtilities.checkCLError(CL30.clGetDeviceIDs(platformId, CL30.CL_DEVICE_TYPE_ALL, deviceIDs, (IntBuffer)null));
|
|
for (int j = 0; j < deviceIDs.capacity(); j++) {
|
|
long deviceId = deviceIDs.get(j);
|
|
devices.add(new CLDevice(deviceId));
|
|
}
|
|
}
|
|
|
|
platforms.add(new CLPlatform(platformId, caps, vendor, name, version, devices));
|
|
}
|
|
|
|
if (platforms.isEmpty()) {
|
|
throw new IllegalStateException("No OpenCL platform found.");
|
|
}
|
|
}
|
|
|
|
return platforms;
|
|
}
|
|
|
|
/**
|
|
* Gets the id of the platform
|
|
* @return the id of the platfomr
|
|
*/
|
|
public long getId(){
|
|
return id;
|
|
}
|
|
|
|
/**
|
|
* Gets the capabilities object of the platform
|
|
* @return The capabilities
|
|
*/
|
|
public CLCapabilities getCapabilities(){
|
|
return caps;
|
|
}
|
|
|
|
/**
|
|
* Gets the name of the vendor the platform
|
|
* @return The name of the vendor of the platform
|
|
*/
|
|
public String getVendor(){
|
|
return vendor;
|
|
}
|
|
|
|
/**
|
|
* Gets the name of the platform
|
|
* @return The name of the platform
|
|
*/
|
|
public String getName(){
|
|
return name;
|
|
}
|
|
|
|
/**
|
|
* Gets the version of opencl on the platform
|
|
* @return The version of opencl on the platform
|
|
*/
|
|
public String getVersion(){
|
|
return name;
|
|
}
|
|
|
|
/**
|
|
* Gets the list of cl devices on this platform
|
|
* @return The list of devices
|
|
*/
|
|
public List<CLDevice> getCLDevices(){
|
|
return clDevices;
|
|
}
|
|
|
|
}
|