Search in sources :

Example 1 with JoclDevice

use of com.jme3.opencl.jocl.JoclDevice in project jmonkeyengine by jMonkeyEngine.

the class JoglContext method initOpenCL.

@SuppressWarnings("unchecked")
protected void initOpenCL() {
    logger.info("Initialize OpenCL with JOGL");
    //load platforms and devices
    StringBuilder platformInfos = new StringBuilder();
    ArrayList<JoclPlatform> platforms = new ArrayList<JoclPlatform>();
    for (CLPlatform p : CLPlatform.listCLPlatforms()) {
        platforms.add(new JoclPlatform(p));
    }
    platformInfos.append("Available OpenCL platforms:");
    for (int i = 0; i < platforms.size(); ++i) {
        JoclPlatform platform = platforms.get(i);
        platformInfos.append("\n * Platform ").append(i + 1);
        platformInfos.append("\n *   Name: ").append(platform.getName());
        platformInfos.append("\n *   Vendor: ").append(platform.getVendor());
        platformInfos.append("\n *   Version: ").append(platform.getVersion());
        platformInfos.append("\n *   Profile: ").append(platform.getProfile());
        platformInfos.append("\n *   Supports interop: ").append(platform.hasOpenGLInterop());
        List<JoclDevice> devices = platform.getDevices();
        platformInfos.append("\n *   Available devices:");
        for (int j = 0; j < devices.size(); ++j) {
            JoclDevice device = devices.get(j);
            platformInfos.append("\n *    * Device ").append(j + 1);
            platformInfos.append("\n *    *   Name: ").append(device.getName());
            platformInfos.append("\n *    *   Vendor: ").append(device.getVendor());
            platformInfos.append("\n *    *   Version: ").append(device.getVersion());
            platformInfos.append("\n *    *   Profile: ").append(device.getProfile());
            platformInfos.append("\n *    *   Compiler version: ").append(device.getCompilerVersion());
            platformInfos.append("\n *    *   Device type: ").append(device.getDeviceType());
            platformInfos.append("\n *    *   Compute units: ").append(device.getComputeUnits());
            platformInfos.append("\n *    *   Work group size: ").append(device.getMaxiumWorkItemsPerGroup());
            platformInfos.append("\n *    *   Global memory: ").append(device.getGlobalMemorySize()).append("B");
            platformInfos.append("\n *    *   Local memory: ").append(device.getLocalMemorySize()).append("B");
            platformInfos.append("\n *    *   Constant memory: ").append(device.getMaximumConstantBufferSize()).append("B");
            platformInfos.append("\n *    *   Supports double: ").append(device.hasDouble());
            platformInfos.append("\n *    *   Supports half floats: ").append(device.hasHalfFloat());
            platformInfos.append("\n *    *   Supports writable 3d images: ").append(device.hasWritableImage3D());
            platformInfos.append("\n *    *   Supports interop: ").append(device.hasOpenGLInterop());
        }
    }
    logger.info(platformInfos.toString());
    //choose devices
    PlatformChooser chooser = null;
    if (settings.getOpenCLPlatformChooser() != null) {
        try {
            chooser = (PlatformChooser) Class.forName(settings.getOpenCLPlatformChooser()).newInstance();
        } catch (Exception ex) {
            logger.log(Level.WARNING, "unable to instantiate custom PlatformChooser", ex);
        }
    }
    if (chooser == null) {
        chooser = new DefaultPlatformChooser();
    }
    List<? extends Device> choosenDevices = chooser.chooseDevices(platforms);
    List<CLDevice> devices = new ArrayList<>(choosenDevices.size());
    JoclPlatform platform = null;
    for (Device d : choosenDevices) {
        if (!(d instanceof JoclDevice)) {
            logger.log(Level.SEVERE, "attempt to return a custom Device implementation from PlatformChooser: {0}", d);
            return;
        }
        JoclDevice ld = (JoclDevice) d;
        if (platform == null) {
            platform = ld.getPlatform();
        } else if (platform != ld.getPlatform()) {
            logger.severe("attempt to use devices from different platforms");
            return;
        }
        devices.add(ld.getDevice());
    }
    if (devices.isEmpty()) {
        logger.warning("no devices specified, no OpenCL context created");
        return;
    }
    logger.log(Level.INFO, "chosen platform: {0}", platform.getName());
    logger.log(Level.INFO, "chosen devices: {0}", choosenDevices);
    //create context
    try {
        CLGLContext c = CLGLContext.create(GLContext.getCurrent(), devices.toArray(new CLDevice[devices.size()]));
        clContext = new com.jme3.opencl.jocl.JoclContext(c, (List<JoclDevice>) choosenDevices);
    } catch (Exception ex) {
        logger.log(Level.SEVERE, "Unable to create OpenCL context", ex);
        return;
    }
    logger.info("OpenCL context created");
}
Also used : JoclPlatform(com.jme3.opencl.jocl.JoclPlatform) JoclDevice(com.jme3.opencl.jocl.JoclDevice) CLDevice(com.jogamp.opencl.CLDevice) Device(com.jme3.opencl.Device) CLDevice(com.jogamp.opencl.CLDevice) ArrayList(java.util.ArrayList) JoclDevice(com.jme3.opencl.jocl.JoclDevice) DefaultPlatformChooser(com.jme3.opencl.DefaultPlatformChooser) PlatformChooser(com.jme3.opencl.PlatformChooser) CLGLContext(com.jogamp.opencl.gl.CLGLContext) RendererException(com.jme3.renderer.RendererException) ArrayList(java.util.ArrayList) List(java.util.List) CLPlatform(com.jogamp.opencl.CLPlatform) DefaultPlatformChooser(com.jme3.opencl.DefaultPlatformChooser)

Aggregations

DefaultPlatformChooser (com.jme3.opencl.DefaultPlatformChooser)1 Device (com.jme3.opencl.Device)1 PlatformChooser (com.jme3.opencl.PlatformChooser)1 JoclDevice (com.jme3.opencl.jocl.JoclDevice)1 JoclPlatform (com.jme3.opencl.jocl.JoclPlatform)1 RendererException (com.jme3.renderer.RendererException)1 CLDevice (com.jogamp.opencl.CLDevice)1 CLPlatform (com.jogamp.opencl.CLPlatform)1 CLGLContext (com.jogamp.opencl.gl.CLGLContext)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1