summaryrefslogtreecommitdiffstats
path: root/src/com/mbien/opencl/CLKernel.java
blob: 33d307301a0d31b06f17d9f260565a193d80017e (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
package com.mbien.opencl;

import com.sun.gluegen.runtime.BufferFactory;
import com.sun.gluegen.runtime.CPU;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

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

/**
 * High level abstraction for an OpenCL Kernel.
 * CLKernel is not threadsafe.
 * "A kernel is a function declared in a program. A kernel is identified by the <code>kernel</code> qualifier
 * applied to any function in a program. A kernel object encapsulates the specific <code>kernel</code>
 * function declared in a program and the argument values to be used when executing this
 * <code>kernel</code> function."
 * @author Michael Bien
 */
public class CLKernel implements CLResource {

    public final long ID;
    public final String name;
    public final int numArgs;

    private final CLProgram program;
    private final CL cl;

    private final ByteBuffer buffer;

    private int argIndex;

    CLKernel(CLProgram program, long id) {
        this.ID = id;
        this.program = program;
        this.cl = program.context.cl;
        this.buffer = BufferFactory.newDirectByteBuffer(8);

        long[] longArray = new long[1];

        // get function name
        int ret = cl.clGetKernelInfo(ID, CL_KERNEL_FUNCTION_NAME, 0, null, longArray, 0);
        checkForError(ret, "error while asking for kernel function name");

        ByteBuffer bb = ByteBuffer.allocate((int)longArray[0]).order(ByteOrder.nativeOrder());

        ret = cl.clGetKernelInfo(ID, CL_KERNEL_FUNCTION_NAME, bb.capacity(), bb, null, 0);
        checkForError(ret, "error while asking for kernel function name");

        this.name = CLUtils.clString2JavaString(bb.array(), bb.capacity());

        // get number of arguments
        ret = cl.clGetKernelInfo(ID, CL_KERNEL_NUM_ARGS, bb.capacity(), bb, null, 0);
        checkForError(ret, "error while asking for number of function arguments.");

        numArgs = bb.getInt(0);

    }
    
    public CLKernel putArg(CLMemory<?> value) {
        setArg(argIndex++, value);
        return this;
    }

    public CLKernel putArg(int value) {
        setArg(argIndex++, value);
        return this;
    }

    public CLKernel putArg(long value) {
        setArg(argIndex++, value);
        return this;
    }

    public CLKernel putArg(float value) {
        setArg(argIndex++, value);
        return this;
    }

    public CLKernel putArg(double value) {
        setArg(argIndex++, value);
        return this;
    }

    public CLKernel putArgs(CLMemory<?>... values) {
        setArgs(argIndex, values);
        argIndex += values.length;
        return this;
    }

    public CLKernel setArg(int argumentIndex, CLMemory<?> value) {
        setArgument(argumentIndex, CPU.is32Bit()?4:8, wrap(value.ID));
        return this;
    }

    public CLKernel setArg(int argumentIndex, int value) {
        setArgument(argumentIndex, 4, wrap(value));
        return this;
    }

    public CLKernel setArg(int argumentIndex, long value) {
        setArgument(argumentIndex, 8, wrap(value));
        return this;
    }

    public CLKernel setArg(int argumentIndex, float value) {
        setArgument(argumentIndex, 4, wrap(value));
        return this;
    }

    public CLKernel setArg(int argumentIndex, double value) {
        setArgument(argumentIndex, 8, wrap(value));
        return this;
    }

    public CLKernel setArgs(CLMemory<?>... values) {
        setArgs(0, values);
        return this;
    }

    private final void setArgs(int startIndex, CLMemory<?>... values) {
        for (int i = 0; i < values.length; i++) {
            setArg(i+startIndex, values[i]);
        }
    }

    private final void setArgument(int argumentIndex, int size, Buffer value) {
        if(argumentIndex >= numArgs || argumentIndex < 0) {
            throw new IndexOutOfBoundsException("kernel "+ toString() +" has "+numArgs+
                    " arguments, can not set argument number "+argumentIndex);
        }
        if(!program.isExecutable()) {
            throw new IllegalStateException("can not set program" +
                    " arguments for a not excecutable program. "+program);
        }

        int ret = cl.clSetKernelArg(ID, argumentIndex, size, value);
        checkForError(ret, "error on clSetKernelArg");
    }

    private final Buffer wrap(float value) {
        return buffer.putFloat(value).rewind();
    }

    private final Buffer wrap(double value) {
        return buffer.putDouble(value).rewind();
    }

    private final Buffer wrap(int value) {
        return buffer.putInt(value).rewind();
    }

    private final Buffer wrap(long value) {
        return buffer.putLong(value).rewind();
    }

    public CLKernel rewind() {
        argIndex = 0;
        return this;
    }

    /**
     * Releases all resources of this kernel from its context.
     */
    public void release() {
        int ret = cl.clReleaseKernel(ID);
        program.onKernelReleased(this);
        checkForError(ret, "can not release kernel");
    }

    @Override
    public String toString() {
        return "CLKernel [id: " + ID
                      + " name: " + name+"]";
    }

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

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 43 * hash + (int) (this.ID ^ (this.ID >>> 32));
        hash = 43 * hash + (this.program != null ? this.program.hashCode() : 0);
        return hash;
    }
    
    CLKernel copy() {
        int[] err = new int[1];
        long newID = cl.clCreateKernel(program.ID, name, err, 0);
        checkForError(err[0], "can not copy kernel");
        return new CLKernel(program, newID);
    }

}