summaryrefslogtreecommitdiffstats
path: root/src/com/jogamp/opencl/util/pp/Reduction.java
blob: 5ef1bc74b4ba86ab893f9948ee3bd138f3da2fca (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
 * Copyright (c) 2011, Michael Bien
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice, this list of
 *       conditions and the following disclaimer.
 *
 *    2. Redistributions in binary form must reproduce the above copyright notice, this list
 *       of conditions and the following disclaimer in the documentation and/or other materials
 *       provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

/*
 * Created on Thursday, September 08 2011 21:22
 */
package com.jogamp.opencl.util.pp;

import java.nio.DoubleBuffer;
import java.nio.FloatBuffer;
import java.nio.LongBuffer;
import java.nio.ShortBuffer;
import com.jogamp.opencl.CLBuffer;
import com.jogamp.opencl.CLCommandQueue;
import com.jogamp.opencl.CLContext;
import com.jogamp.opencl.CLProgram;
import com.jogamp.opencl.CLResource;
import com.jogamp.opencl.CLWork.CLWork1D;
import com.jogamp.opencl.util.CLUtil;
import com.jogamp.opencl.util.concurrent.CLQueueContext;
import com.jogamp.opencl.util.concurrent.CLTask;
import com.jogamp.opencl.util.concurrent.CLQueueContext.CLResourceQueueContext;
import com.jogamp.opencl.util.CLProgramConfiguration;
import java.io.IOException;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;

import static com.jogamp.opencl.CLMemory.Mem.*;
import static java.lang.Math.*;

/**
 *
 * @author Michael Bien
 */
public class Reduction<B extends Buffer> implements CLResource {

    private static final String SOURCES;

    private final Op OPERATION;
    private final ArgType ELEMENT;

    private final int VECTOR_SIZE;
    private final CLProgram program;
    private final CLWork1D reduction;

    static{
        try {
            StringBuilder sb = new StringBuilder(2048);
            CLUtil.readStream(Reduction.class.getResourceAsStream("reduce.cl"), sb);
            SOURCES = sb.toString();
        } catch (IOException ex) {
            throw new RuntimeException("can not initialize Reduction.", ex);
        }
    }

    private <B extends Buffer> Reduction(CLContext context, Op op, Class<B> elementType) {

        this.ELEMENT = ArgType.valueOf(elementType);
        this.OPERATION = op;
        this.VECTOR_SIZE = 4;

        this.program = context.createProgram(SOURCES);

        CLProgramConfiguration config = program.prepare();
        config.withDefine("OP_"+op.name())
              .withDefine("TYPE", ELEMENT.vectorType(VECTOR_SIZE));
        if(ELEMENT.equals(ArgType.DOUBLE)) {
            config.withDefine("DOUBLE_FP");
        }
        config.build();

        reduction = CLWork1D.create1D(program.createCLKernel("reduce"));
    }

    public static <B extends Buffer> Reduction<B> create(CLContext context, Op op, Class<B> elementType) {
        return new Reduction<B>(context, op, elementType);
    }

    public static <B extends Buffer> Reduction<B> create(CLCommandQueue queue, Op op, Class<B> elementType) {
        return create(queue.getContext(), op, elementType);
    }

    public static <B extends Buffer> CLTask<CLResourceQueueContext<Reduction<B>>, B> createTask(B input, B output, Op op, Class<B> elementType) {
        return new CLReductionTask<B>(input, output, op, elementType);
    }

    public B reduce(CLCommandQueue queue, B input, B output) {

        int length = input.capacity();

        //TODO thats temporary...
        if(length%(VECTOR_SIZE*2) != 0) {
            throw new IllegalArgumentException("input buffer must be evenly devideable through "+VECTOR_SIZE*2);
        }

        int groupSize = (int)reduction.getKernel().getWorkGroupSize(queue.getDevice());
        int realSize  = length / VECTOR_SIZE;
        int workItems = CLUtil.roundUp(realSize, groupSize*2) / 2;

        int groups = workItems / groupSize;
        int sharedBufferSize = groupSize / 2 * ELEMENT.SIZE*VECTOR_SIZE;

        int outputSize = groups * ELEMENT.SIZE*VECTOR_SIZE;

        CLContext context = queue.getContext();

        CLBuffer<B> in = context.createBuffer(input, READ_ONLY);
        CLBuffer<ByteBuffer> out = context.createByteBuffer(outputSize, WRITE_ONLY);

        reduction.getKernel().putArgs(in, out).putArgSize(sharedBufferSize).putArg(realSize/2).rewind();
        reduction.setWorkSize(workItems, groupSize);

//        System.out.println(groups);
//        System.out.println(reduction);

        queue.putWriteBuffer(in, false);
        queue.putWork(reduction);
        queue.putReadBuffer(out, true);

        in.release();
        out.release();

        if(OPERATION.equals(Op.MAX)) {
            finishMax(output, out.getBuffer());
        }else if(OPERATION.equals(Op.MIN)) {
            finishMin(output, out.getBuffer());
        }else if(OPERATION.equals(Op.ADD)) {
            finishAdd(output, out.getBuffer());
        }else if(OPERATION.equals(Op.MUL)) {
            finishMul(output, out.getBuffer());
        }else{
            throw new RuntimeException();
        }

        return output;
    }

    private <B extends Buffer> void finishMax(B output, ByteBuffer buffer) {
        if(output instanceof ByteBuffer) {
            byte max = Byte.MIN_VALUE;
            while(buffer.hasRemaining()) {
                max = (byte) max(max, buffer.get());
            }
            ((ByteBuffer)output).put(max);
        }else if(output instanceof ShortBuffer) {
            short max = Short.MIN_VALUE;
            while(buffer.hasRemaining()) {
                max = (short) max(max, buffer.getShort());
            }
            ((ShortBuffer)output).put(max);
        }else if(output instanceof IntBuffer) {
            int max = Integer.MIN_VALUE;
            while(buffer.hasRemaining()) {
                max = max(max, buffer.getInt());
            }
            ((IntBuffer)output).put(max);
        }else if(output instanceof LongBuffer) {
            long max = Long.MIN_VALUE;
            while(buffer.hasRemaining()) {
                max = max(max, buffer.getLong());
            }
            ((LongBuffer)output).put(max);
        }else if(output instanceof FloatBuffer) {
            float max = Float.MIN_VALUE;
            while(buffer.hasRemaining()) {
                max = max(max, buffer.getFloat());
            }
            ((FloatBuffer)output).put(max);
        }else if(output instanceof DoubleBuffer) {
            double max = Double.MIN_VALUE;
            while(buffer.hasRemaining()) {
                max = max(max, buffer.getDouble());
            }
            ((DoubleBuffer)output).put(max);
        }
        buffer.rewind();
    }

    private <B extends Buffer> void finishMin(B output, ByteBuffer buffer) {
        if(output instanceof ByteBuffer) {
            byte min = Byte.MAX_VALUE;
            while(buffer.hasRemaining()) {
                min = (byte) min(min, buffer.get());
            }
            ((ByteBuffer)output).put(min);
        }else if(output instanceof ShortBuffer) {
            short min = Short.MAX_VALUE;
            while(buffer.hasRemaining()) {
                min = (short) min(min, buffer.getShort());
            }
            ((ShortBuffer)output).put(min);
        }else if(output instanceof IntBuffer) {
            int min = Integer.MAX_VALUE;
            while(buffer.hasRemaining()) {
                min = min(min, buffer.getInt());
            }
            ((IntBuffer)output).put(min);
        }else if(output instanceof LongBuffer) {
            long min = Long.MAX_VALUE;
            while(buffer.hasRemaining()) {
                min = min(min, buffer.getLong());
            }
            ((LongBuffer)output).put(min);
        }else if(output instanceof FloatBuffer) {
            float min = Float.MAX_VALUE;
            while(buffer.hasRemaining()) {
                min = min(min, buffer.getFloat());
            }
            ((FloatBuffer)output).put(min);
        }else if(output instanceof DoubleBuffer) {
            double min = Double.MAX_VALUE;
            while(buffer.hasRemaining()) {
                min = min(min, buffer.getDouble());
            }
            ((DoubleBuffer)output).put(min);
        }
        buffer.rewind();
    }


    private <B extends Buffer> void finishAdd(B output, ByteBuffer buffer) {
        if(output instanceof ByteBuffer) {
            long result = 0;
            while(buffer.hasRemaining()) {
                result += buffer.get();
            }
            ((ByteBuffer)output).put((byte)result);
        }else if(output instanceof ShortBuffer) {
            long result = 0;
            while(buffer.hasRemaining()) {
                result += buffer.getShort();
            }
            ((ShortBuffer)output).put((short)result);
        }else if(output instanceof IntBuffer) {
            long result = 0;
            while(buffer.hasRemaining()) {
                result += buffer.getInt();
            }
            ((IntBuffer)output).put((int)result);
        }else if(output instanceof LongBuffer) {
            long result = 0;
            while(buffer.hasRemaining()) {
                result += buffer.getLong();
            }
            ((LongBuffer)output).put(result);
        }else if(output instanceof FloatBuffer) {
            double result = 0;
            while(buffer.hasRemaining()) {
                result += buffer.getFloat();
            }
            ((FloatBuffer)output).put((float)result);
        }else if(output instanceof DoubleBuffer) {
            double result = 0;
            while(buffer.hasRemaining()) {
                result += buffer.getDouble();
            }
            ((DoubleBuffer)output).put(result);
        }
        buffer.rewind();
    }

    private <B extends Buffer> void finishMul(B output, ByteBuffer buffer) {
        if(output instanceof ByteBuffer) {
            long result = buffer.get();
            while(buffer.hasRemaining()) {
                result *= buffer.get();
            }
            ((ByteBuffer)output).put((byte)result);
        }else if(output instanceof ShortBuffer) {
            long result = buffer.getShort();
            while(buffer.hasRemaining()) {
                result *= buffer.getShort();
            }
            ((ShortBuffer)output).put((short)result);
        }else if(output instanceof IntBuffer) {
            long result = buffer.getInt();
            while(buffer.hasRemaining()) {
                result *= buffer.getInt();
            }
            ((IntBuffer)output).put((int)result);
        }else if(output instanceof LongBuffer) {
            long result = buffer.getLong();
            while(buffer.hasRemaining()) {
                result *= buffer.getLong();
            }
            ((LongBuffer)output).put(result);
        }else if(output instanceof FloatBuffer) {
            double result = buffer.getFloat();
            while(buffer.hasRemaining()) {
                result *= buffer.getFloat();
            }
            ((FloatBuffer)output).put((float)result);
        }else if(output instanceof DoubleBuffer) {
            double result = buffer.getDouble();
            while(buffer.hasRemaining()) {
                result *= buffer.getDouble();
            }
            ((DoubleBuffer)output).put(result);
        }
        buffer.rewind();
    }

    @Override
    public void release() {
        program.release();
    }

    @Override
    public boolean isReleased() {
        return program == null || program.isReleased();
    }

    @Override
    public String toString() {
        return getClass().getSimpleName()+"["+OPERATION+", "+ELEMENT+VECTOR_SIZE+"]";
    }


    public enum Op {ADD, MUL, MIN, MAX}

    private static class CLReductionTask<B extends Buffer> extends CLTask<CLResourceQueueContext<Reduction<B>>, B> {

        private final static int TYPE_ID = 1;

        private final B input;
        private final B output;
        private final Op op;
        private final Class<B> elementType;
        private final Integer KEY;

        private CLReductionTask(B input, B output, Op op, Class<B> elementType) {
            this.input = input;
            this.output = output;
            this.op = op;
            this.elementType = elementType;
            this.KEY = TYPE_ID + op.ordinal()*10 + 1000*ArgType.valueOf(elementType).ordinal();
        }

        @Override
        public CLResourceQueueContext<Reduction<B>> createQueueContext(CLCommandQueue queue) {
            Reduction<B> reduction = Reduction.create(queue, op, elementType);
            return new CLQueueContext.CLResourceQueueContext<Reduction<B>>(queue, reduction);
        }

        @Override
        public B execute(CLResourceQueueContext<Reduction<B>> context) {
            return context.resource.reduce(context.queue, input, output);
        }

        @Override
        public Object getContextKey() {
            return KEY;
        }

        @Override
        public String toString() {
            return getClass().getSimpleName()+"["+op+", "+elementType+", "+KEY+"]";
        }
    }

}