aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/mbien/opencl/CLCommandQueue.java
blob: 5abc5cd5fa55860403f25ff0770cf093599d466d (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package com.mbien.opencl;

import static com.mbien.opencl.CLException.*;

/**
 *
 * @author Michael Bien
 */
public class CLCommandQueue {

    public final long ID;
    private final CLContext context;
    private final CLDevice device;
    private final CL cl;

    CLCommandQueue(CLContext context, CLDevice device, long properties) {
        this.context = context;
        this.cl = context.cl;
        this.device = device;

        int[] status = new int[1];
        this.ID = cl.clCreateCommandQueue(context.ID, device.ID, properties, status, 0);

        if(status[0] != CL.CL_SUCCESS)
            throw new CLException(status[0], "can not create command queue on "+device);
    }

    public CLCommandQueue putWriteBuffer(CLBuffer writeBuffer, boolean blockingWrite) {

        int ret = cl.clEnqueueWriteBuffer(
                ID, writeBuffer.ID, blockingWrite ? CL.CL_TRUE : CL.CL_FALSE,
                0, writeBuffer.buffer.capacity(), writeBuffer.buffer,
//                0, null, null); //TODO solve NPE in gluegen when PointerBuffer == null (fast dircet memory path)
                0, null, 0, null, 0); //TODO events

        if(ret != CL.CL_SUCCESS)
            throw new CLException(ret, "can not enqueue WriteBuffer: " + writeBuffer);

        return this;
    }

    public CLCommandQueue putReadBuffer(CLBuffer readBuffer, boolean blockingRead) {

        int ret = cl.clEnqueueReadBuffer(
                ID, readBuffer.ID, blockingRead ? CL.CL_TRUE : CL.CL_FALSE,
                0, readBuffer.buffer.capacity(), readBuffer.buffer,
//                0, null, null); //TODO solve NPE in gluegen when PointerBuffer == null (fast dircet memory path)
                0, null, 0, null, 0); //TODO events

        if(ret != CL.CL_SUCCESS)
            throw new CLException(ret, "can not enqueue ReadBuffer: " + readBuffer);
        
        return this;
    }

    public CLCommandQueue putBarrier() {
        int ret = cl.clEnqueueBarrier(ID);
        checkForError(ret, "can not enqueue Barrier");
        return this;
    }

    public CLCommandQueue putCopyBuffer(CLBuffer src, CLBuffer dest, long bytesToCopy) {
        int ret = cl.clEnqueueCopyBuffer(
                        ID, src.ID, dest.ID, src.buffer.position(), dest.buffer.position(), bytesToCopy,
//                      0, null, null); //TODO solve NPE in gluegen when PointerBuffer == null
                        0, null, 0, null, 0); //TODO events
        checkForError(ret, "can not copy Buffer");
        return this;
    }
    
    //TODO implement remaining methods
    /*
    public CLCommandQueue putCopyImage() {

        return this;
    }
    public CLCommandQueue putCopyBufferToImage() {

        return this;
    }
    public CLCommandQueue putCopyImageToBuffer() {

        return this;
    }
    public CLCommandQueue putMarker() {

        return this;
    }

    public CLCommandQueue putWriteImage() {

        return this;
    }

    public CLCommandQueue putReadImage() {

        return this;
    }

    public CLCommandQueue putTask() {

        return this;
    }

    public CLBuffer putMapBuffer() {

        return null;
    }

    public CLCommandQueue putMapImage() {

        return this;
    }

    public CLCommandQueue putUnmapMemObject() {

        return this;
    }

    public CLCommandQueue putWaitForEvents() {

        return this;
    }
*/
    
    public CLCommandQueue putNDRangeKernel(CLKernel kernel, int workDimension, long globalWorkOffset, long globalWorkSize, long localWorkSize) {
        return this.putNDRangeKernel(
                kernel, workDimension,
                globalWorkOffset==0 ? null : new long[] {globalWorkOffset},
                globalWorkSize  ==0 ? null : new long[] {globalWorkSize  },
                localWorkSize   ==0 ? null : new long[] {localWorkSize   }  );
    }

    public CLCommandQueue putNDRangeKernel(CLKernel kernel, int workDimension, long[] globalWorkOffset, long[] globalWorkSize, long[] localWorkSize) {

       int ret = cl.clEnqueueNDRangeKernel(
                ID, kernel.ID, 1,
                globalWorkOffset, 0,
                globalWorkSize, 0,
                localWorkSize, 0,
                0,
                null, 0,
                null, 0 );

        if(ret != CL.CL_SUCCESS)
            throw new CLException(ret, "can not enqueue NDRangeKernel: " + kernel);

        return this;
    }

    public CLCommandQueue finish() {
        int ret = cl.clFinish(ID);
        checkForError(ret, "can not finish command queue");
        return this;
    }

    public void release() {
        int ret = cl.clReleaseCommandQueue(ID);
        context.onCommandQueueReleased(device, this);
        checkForError(ret, "can not release command queue");
    }

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

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 89 * hash + (int) (this.ID ^ (this.ID >>> 32));
        hash = 89 * hash + (this.context != null ? this.context.hashCode() : 0);
        hash = 89 * hash + (this.device != null ? this.device.hashCode() : 0);
        return hash;
    }


}