aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/mbien/opencl/CLPlatform.java
blob: 56ef37133e8651041e2c35596863a77b765b2f3e (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package com.mbien.opencl;

import com.mbien.opencl.impl.CLImpl;
import java.nio.ByteBuffer;
import static com.mbien.opencl.CLException.*;
/**
 *
 * @author Michael Bien
 */
public final class CLPlatform {

    /**
     * OpenCL platform id for this platform.
     */
    public final long ID;

    private static final CL cl;

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

    CLPlatform(long id) {
        this.ID = id;
    }

    /**
     * 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);
        checkForError(ret, "can not enumerate platforms");

        // receive platform ids
        long[] platformId = new long[intBuffer[0]];
        ret = cl.clGetPlatformIDs(platformId.length, platformId, 0, null, 0);
        checkForError(ret, "can not enumerate platforms");

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

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

        return platforms;
    }

    /**
     * Returns the low level binding interface to the OpenCL APIs.
     */
    public static CL getLowLevelBinding() {
        return cl;
    }

    /**
     * Lists all physical devices available on this platform.
     */
    public CLDevice[] listCLDevices() {

        int[] intBuffer = new int[1];

        //find all devices
        int ret = cl.clGetDeviceIDs(ID, CL.CL_DEVICE_TYPE_ALL, 0, null, 0, intBuffer, 0);
        checkForError(ret, "error while enumerating devices");

        long[] deviceIDs = new long[intBuffer[0]];
        ret = cl.clGetDeviceIDs(ID, CL.CL_DEVICE_TYPE_ALL, deviceIDs.length, deviceIDs, 0, null, 0);
        checkForError(ret, "error while enumerating devices");

        CLDevice[] devices = new CLDevice[deviceIDs.length];

        //print device info
        for (int i = 0; i < deviceIDs.length; i++)
            devices[i] = new CLDevice(cl, deviceIDs[i]);

        return devices;

    }

    /**
     * Returns the platform name.
     */
    public String getName() {
        return getInfoString(CL.CL_PLATFORM_NAME);
    }

    /**
     * Returns the platform version.
     */
    public String getVersion() {
        return getInfoString(CL.CL_PLATFORM_VERSION);
    }

    /**
     * Returns the platform profile.
     */
    public String getProfile() {
        return getInfoString(CL.CL_PLATFORM_PROFILE);
    }

    /**
     * Returns the platform vendor.
     */
    public String getVendor() {
        return getInfoString(CL.CL_PLATFORM_VENDOR);
    }

    /**
     * Returns a info string in exchange for a key (CL_PLATFORM_*).
     */
    public String getInfoString(int key) {
        long[] longBuffer = new long[1];
        ByteBuffer bb = ByteBuffer.allocate(512);

        int ret = cl.clGetPlatformInfo(ID, key, bb.capacity(), bb, longBuffer, 0);
        checkForError(ret, "can not receive info string");

        return new String(bb.array(), 0, (int)longBuffer[0]);
    }

    @Override
    public String toString() {
        return "CLPlatform [name:" + getName()
                         +" vendor:"+getVendor()
                         +" profile:"+getProfile()
                         +" version:"+getVersion()+"]";
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final CLPlatform other = (CLPlatform) obj;
        if (this.ID != other.ID) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 71 * hash + (int) (this.ID ^ (this.ID >>> 32));
        return hash;
    }


}