aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/mbien/opencl/CLContext.java
blob: 533d45d4ba7ca5bee3a6925e545dde5a6ac63e19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package com.mbien.opencl;

import com.mbien.opencl.impl.CLImpl;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;

/**
 *
 * @author Michael Bien
 */
public final class CLContext {

    private final static CL cl;
    public final long contextID;

    static{
        System.loadLibrary("gluegen-rt");
        System.loadLibrary("jocl");
        cl = new CLImpl();
    }

    private CLContext(long contextID) {
        this.contextID = contextID;
    }

    /**
     * Creates a default context on all available devices.
     */
    public static CLContext create() {
        IntBuffer ib = IntBuffer.allocate(1);
        long context = cl.clCreateContextFromType(null, 0, CL.CL_DEVICE_TYPE_ALL, null, null, null, 0);

//        int errorCode = ib.get();
//        if(errorCode != CL.CL_SUCCESS)
//            throw new CLException(errorCode, "can not create CL context");

        return new CLContext(context);
    }

    /**
     * Creates a default context on the specified device types.
     */
    public static CLContext create(CLDevice.Type... deviceTypes) {

        int type = deviceTypes[0].CL_TYPE;
        for (int i = 1; i < deviceTypes.length; i++) {
            type |= deviceTypes[i].CL_TYPE;
        }

        long context = cl.clCreateContextFromType(null, 0, type, null, null, null, 0);
        return new CLContext(context);
    }

    /**
     * Releases the context and all resources.
     */
    public void release() {
        int ret = cl.clReleaseContext(contextID);
        if(CL.CL_SUCCESS != ret)
            throw new CLException(ret, "error releasing context");
    }

    /**
     * Gets the device with maximal FLOPS from this context.
     */
    public CLDevice getMaxFlopsDevice() {

        //TODO not finished

        long[] longBuffer = new long[1];
//        ByteBuffer bb = ByteBuffer.allocate(8);
//        bb.order(ByteOrder.nativeOrder());

        int ret = cl.clGetContextInfo(contextID, CL.CL_CONTEXT_DEVICES, 0, null, longBuffer, 0);
        if(CL.CL_SUCCESS != ret)
            throw new CLException(ret, "can not receive context info");

        System.out.println("#devices: "+longBuffer[0]);

        long[] deviceIDs = new long[(int)longBuffer[0]];
        ret = cl.clGetContextInfo(contextID, CL.CL_CONTEXT_DEVICES, 0, null, deviceIDs, 0);

        if(CL.CL_SUCCESS != ret)
            throw new CLException(ret, "can not receive context info");

        for (int i = 0; i < deviceIDs.length; i++) {
            long l = deviceIDs[i];
            System.out.println("device id"+l);
        }

            // get the list of GPU devices associated with context
//        ciErrNum = clGetContextInfo(cxGPUContext, CL_CONTEXT_DEVICES, 0, NULL, &dataBytes);
//        cl_device_id *cdDevices = (cl_device_id *)malloc(dataBytes);
//        ciErrNum |= clGetContextInfo(cxGPUContext, CL_CONTEXT_DEVICES, dataBytes, cdDevices, NULL);
//        shrCheckError(ciErrNum, CL_SUCCESS);

        return null;
    }

    /**
     * Lists all available OpenCL implementaitons.
     * @throws CLException if something went wrong initializing OpenCL
     */
    public static CLPlatform[] listCLPlatforms() {

        int[] intBuffer = new int[1];
        // find all available OpenCL platforms
        int ret = cl.clGetPlatformIDs(0, null, 0, intBuffer, 0);
        if(CL.CL_SUCCESS != ret)
            throw new CLException(ret, "can not enumerate platforms");

        // receive platform ids
        long[] platformId = new long[intBuffer[0]];
        ret = cl.clGetPlatformIDs(platformId.length, platformId, 0, null, 0);
        if(CL.CL_SUCCESS != ret)
            throw new CLException(ret, "can not enumerate platforms");

        CLPlatform[] platforms = new CLPlatform[platformId.length];

        for (int i = 0; i < platformId.length; i++)
            platforms[i] = new CLPlatform(cl, platformId[i]);

        return platforms;
    }

}