Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/jcl/src/openj9.cuda/share/classes/com/ibm/cuda/CudaKernel.java
12927 views
1
/*[INCLUDE-IF Sidecar18-SE]*/
2
/*******************************************************************************
3
* Copyright (c) 2013, 2018 IBM Corp. and others
4
*
5
* This program and the accompanying materials are made available under
6
* the terms of the Eclipse Public License 2.0 which accompanies this
7
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
8
* or the Apache License, Version 2.0 which accompanies this distribution and
9
* is available at https://www.apache.org/licenses/LICENSE-2.0.
10
*
11
* This Source Code may also be made available under the following
12
* Secondary Licenses when the conditions for such availability set
13
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
14
* General Public License, version 2 with the GNU Classpath
15
* Exception [1] and GNU General Public License, version 2 with the
16
* OpenJDK Assembly Exception [2].
17
*
18
* [1] https://www.gnu.org/software/classpath/license.html
19
* [2] http://openjdk.java.net/legal/assembly-exception.html
20
*
21
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
22
*******************************************************************************/
23
package com.ibm.cuda;
24
25
import com.ibm.cuda.CudaDevice.CacheConfig;
26
import com.ibm.cuda.CudaDevice.SharedMemConfig;
27
28
/**
29
* The {@code CudaKernel} class represents a kernel {@link CudaFunction function}
30
* in a loaded {@link CudaModule}.
31
*/
32
public class CudaKernel {
33
34
/**
35
* The {@code Parameters} class represents the actual parameters in
36
* a {@link CudaKernel kernel} launch.
37
*/
38
public static final class Parameters implements Cloneable {
39
40
/* A bit-mask of missing values (the bit (1<<i) is set if the values[i] is missing). */
41
private long mask;
42
43
final long[] values;
44
45
/**
46
* Creates a new bundle of parameter values.
47
*
48
* @param count
49
* the number of values to be passed when the kernel is launched
50
* @throws IllegalArgumentException
51
* if the count is negative or greater than 64
52
*/
53
public Parameters(int count) {
54
super();
55
if (0 <= count && count <= Long.SIZE) {
56
this.mask = count == Long.SIZE ? -1L : (1L << count) - 1;
57
this.values = new long[count];
58
} else {
59
throw new IllegalArgumentException();
60
}
61
}
62
63
/**
64
* Creates a new bundle of parameter values.
65
* <p>
66
* Each parameter value must be one of the following:
67
* <ul>
68
* <li>a boxed primitive value</li>
69
* <li>a CudaBuffer object</li>
70
* <li>null</li>
71
* </ul>
72
*
73
* @param values
74
* the values to be passed when the kernel is launched
75
* @throws IllegalArgumentException
76
* if {@code parameters} contains any unsupported types
77
*/
78
public Parameters(Object... values) {
79
super();
80
81
int count = values.length;
82
83
this.mask = 0;
84
this.values = new long[count];
85
86
for (int i = 0; i < count; ++i) {
87
this.values[i] = CudaFunction.nativeValueOf(values[i]);
88
}
89
}
90
91
/**
92
* Creates a copy of the given parameter block.
93
*
94
* @param that
95
* the parameter block to be copied
96
*/
97
public Parameters(Parameters that) {
98
super();
99
this.mask = that.mask;
100
this.values = that.values.clone();
101
}
102
103
/**
104
* Appends a byte value to the list of parameter values.
105
*
106
* @param value
107
* the value to be passed when the kernel is launched
108
* @return
109
* this parameter list
110
* @throws IndexOutOfBoundsException
111
* if all positions already have values defined
112
*/
113
public Parameters add(byte value) {
114
return add((long) value);
115
}
116
117
/**
118
* Appends a character value to the list of parameter values.
119
*
120
* @param value
121
* the value to be passed when the kernel is launched
122
* @return
123
* this parameter list
124
* @throws IndexOutOfBoundsException
125
* if all positions already have values defined
126
*/
127
public Parameters add(char value) {
128
return add((long) value);
129
}
130
131
/**
132
* Appends a buffer address to the list of parameter values.
133
*
134
* @param value
135
* the value to be passed when the kernel is launched,
136
* or null to pass a null pointer
137
* @return
138
* this parameter list
139
* @throws IllegalStateException
140
* if the buffer has been closed (see {@link CudaBuffer#close()})
141
* @throws IndexOutOfBoundsException
142
* if all positions already have values defined
143
*/
144
public Parameters add(CudaBuffer value) {
145
return add(value == null ? 0 : value.getAddress());
146
}
147
148
/**
149
* Appends a double value to the list of parameter values.
150
*
151
* @param value
152
* the value to be passed when the kernel is launched
153
* @return
154
* this parameter list
155
* @throws IndexOutOfBoundsException
156
* if all positions already have values defined
157
*/
158
public Parameters add(double value) {
159
return add(Double.doubleToRawLongBits(value));
160
}
161
162
/**
163
* Appends a float value to the list of parameter values.
164
*
165
* @param value
166
* the value to be passed when the kernel is launched
167
* @return
168
* this parameter list
169
* @throws IndexOutOfBoundsException
170
* if all positions already have values defined
171
*/
172
public Parameters add(float value) {
173
return add((long) Float.floatToRawIntBits(value));
174
}
175
176
/**
177
* Appends a integer value to the list of parameter values.
178
*
179
* @param value
180
* the value to be passed when the kernel is launched
181
* @return
182
* this parameter list
183
* @throws IndexOutOfBoundsException
184
* if all positions already have values defined
185
*/
186
public Parameters add(int value) {
187
return add((long) value);
188
}
189
190
/**
191
* Appends a long value to the list of parameter values.
192
*
193
* @param value
194
* the value to be passed when the kernel is launched
195
* @return
196
* this parameter list
197
* @throws IndexOutOfBoundsException
198
* if all positions already have values defined
199
*/
200
public Parameters add(long value) {
201
if (isComplete()) {
202
throw new IndexOutOfBoundsException();
203
}
204
205
int index = Long.numberOfTrailingZeros(mask);
206
207
return set(index, value);
208
}
209
210
/**
211
* Appends a short value to the list of parameter values.
212
*
213
* @param value
214
* the value to be passed when the kernel is launched
215
* @return
216
* this parameter list
217
* @throws IndexOutOfBoundsException
218
* if all positions already have values defined
219
*/
220
public Parameters add(short value) {
221
return add((long) value);
222
}
223
224
/**
225
* Creates a copy of this parameter block.
226
*/
227
@Override
228
public Parameters clone() {
229
return new Parameters(this);
230
}
231
232
boolean isComplete() {
233
return mask == 0;
234
}
235
236
/**
237
* Replaces the parameter at the specified index with the given byte value.
238
*
239
* @param index
240
* the index of the parameter to be set
241
* @param value
242
* the value to be passed when the kernel is launched
243
* @return
244
* this parameter list
245
* @throws IndexOutOfBoundsException
246
* if {@code index} &lt; 0 or {@code index} &gt;= the size of this parameter list
247
*/
248
public Parameters set(int index, byte value) {
249
return set(index, (long) value);
250
}
251
252
/**
253
* Replaces the parameter at the specified index with the given character value.
254
*
255
* @param index
256
* the index of the parameter to be set
257
* @param value
258
* the value to be passed when the kernel is launched
259
* @return
260
* this parameter list
261
* @throws IndexOutOfBoundsException
262
* if {@code index} &lt; 0 or {@code index} &gt;= the size of this parameter list
263
*/
264
public Parameters set(int index, char value) {
265
return set(index, (long) value);
266
}
267
268
/**
269
* Replaces the parameter at the specified index with the given buffer address.
270
*
271
* @param index
272
* the index of the parameter to be set
273
* @param value
274
* the value to be passed when the kernel is launched,
275
* or null to pass a null pointer
276
* @return
277
* this parameter list
278
* @throws IndexOutOfBoundsException
279
* if {@code index} &lt; 0 or {@code index} &gt;= the size of this parameter list
280
*/
281
public Parameters set(int index, CudaBuffer value) {
282
return set(index, value == null ? 0 : value.getAddress());
283
}
284
285
/**
286
* Replaces the parameter at the specified index with the given double value.
287
*
288
* @param index
289
* the index of the parameter to be set
290
* @param value
291
* the value to be passed when the kernel is launched
292
* @return
293
* this parameter list
294
* @throws IndexOutOfBoundsException
295
* if {@code index} &lt; 0 or {@code index} &gt;= the size of this parameter list
296
*/
297
public Parameters set(int index, double value) {
298
return set(index, Double.doubleToRawLongBits(value));
299
}
300
301
/**
302
* Replaces the parameter at the specified index with the given float value.
303
*
304
* @param index
305
* the index of the parameter to be set
306
* @param value
307
* the value to be passed when the kernel is launched
308
* @return
309
* this parameter list
310
* @throws IndexOutOfBoundsException
311
* if {@code index} &lt; 0 or {@code index} &gt;= the size of this parameter list
312
*/
313
public Parameters set(int index, float value) {
314
return set(index, (long) Float.floatToRawIntBits(value));
315
}
316
317
/**
318
* Replaces the parameter at the specified index with the given int value.
319
*
320
* @param index
321
* the index of the parameter to be set
322
* @param value
323
* the value to be passed when the kernel is launched
324
* @return
325
* this parameter list
326
* @throws IndexOutOfBoundsException
327
* if {@code index} &lt; 0 or {@code index} &gt;= the size of this parameter list
328
*/
329
public Parameters set(int index, int value) {
330
return set(index, (long) value);
331
}
332
333
/**
334
* Replaces the parameter at the specified index with the given long value.
335
*
336
* @param index
337
* the index of the parameter to be set
338
* @param value
339
* the value to be passed when the kernel is launched
340
* @return
341
* this parameter list
342
* @throws IndexOutOfBoundsException
343
* if {@code index} &lt; 0 or {@code index} &gt;= the size of this parameter list
344
*/
345
public Parameters set(int index, long value) {
346
if (0 <= index && index < values.length) {
347
mask &= ~(1L << index);
348
values[index] = value;
349
return this;
350
} else {
351
throw new IndexOutOfBoundsException(Integer.toString(index));
352
}
353
}
354
355
/**
356
* Replaces the parameter at the specified index with a short value.
357
*
358
* @param index
359
* the index of the parameter to be set
360
* @param value
361
* the value to be passed when the kernel is launched
362
* @return
363
* this parameter list
364
* @throws IndexOutOfBoundsException
365
* if {@code index} &lt; 0 or {@code index} &gt;= the size of this parameter list
366
*/
367
public Parameters set(int index, short value) {
368
return set(index, (long) value);
369
}
370
}
371
372
private final CudaFunction function;
373
374
/**
375
* Creates a new kernel object in the given module whose entry point
376
* is the specified function.
377
*
378
* @param module
379
* the module containing the kernel code
380
* @param function
381
* the entry point of the kernel
382
*/
383
public CudaKernel(CudaModule module, CudaFunction function) {
384
super();
385
386
if (function.deviceId != module.deviceId) {
387
throw new IllegalArgumentException();
388
}
389
390
this.function = function;
391
}
392
393
/**
394
* Creates a new kernel object in the given module whose entry point
395
* is the function with the specified name.
396
*
397
* @param module
398
* the module containing the kernel code
399
* @param functionName
400
* the name of the entry point of the kernel
401
* @throws CudaException
402
* if a CUDA exception occurs
403
*/
404
public CudaKernel(CudaModule module, String functionName)
405
throws CudaException {
406
this(module, module.getFunction(functionName));
407
}
408
409
/**
410
* Returns the value of the specified @{code attribute} for the
411
* {@link CudaFunction function} associated with this kernel.
412
*
413
* @param attribute
414
* the attribute to be queried (see CudaFunction.ATTRIBUTE_XXX)
415
* @return
416
* the attribute value
417
* @throws CudaException
418
* if a CUDA exception occurs
419
*/
420
public final int getAttribute(int attribute) throws CudaException {
421
return function.getAttribute(attribute);
422
}
423
424
/**
425
* Launches this kernel. The launch configuration is given by {@code grid}
426
* and the actual parameter values are specified by {@code parameters}.
427
* <p>
428
* Each parameter value must be one of the following:
429
* <ul>
430
* <li>a boxed primitive value</li>
431
* <li>a CudaBuffer object</li>
432
* <li>null</li>
433
* </ul>
434
*
435
* @param grid
436
* the launch configuration
437
* @param parameters
438
* the actual parameter values
439
* @throws CudaException
440
* if a CUDA exception occurs
441
* @throws IllegalArgumentException
442
* if {@code parameters} contains any unsupported types
443
*/
444
public final void launch(CudaGrid grid, Object... parameters)
445
throws CudaException {
446
function.launch(grid, parameters);
447
}
448
449
/**
450
* Launches this kernel. The launch configuration is given by {@code grid}
451
* and the actual parameter values are specified by {@code parameters}.
452
*
453
* @param grid
454
* the launch configuration
455
* @param parameters
456
* the actual parameter values
457
* @throws CudaException
458
* if a CUDA exception occurs
459
* @throws IllegalArgumentException
460
* if {@code parameters} does not contain the correct number of values
461
*/
462
public final void launch(CudaGrid grid, Parameters parameters)
463
throws CudaException {
464
function.launch(grid, parameters);
465
}
466
467
/**
468
* Configures the cache for the {@link CudaFunction function} associated
469
* with this kernel.
470
*
471
* @param config
472
* the desired cache configuration
473
* @throws CudaException
474
* if a CUDA exception occurs
475
*/
476
public final void setCacheConfig(CacheConfig config) throws CudaException {
477
function.setCacheConfig(config);
478
}
479
480
/**
481
* Configures the shared memory of the {@link CudaFunction function}
482
* associated with this kernel.
483
*
484
* @param config
485
* the desired shared memory configuration
486
* @throws CudaException
487
* if a CUDA exception occurs
488
*/
489
public final void setSharedMemConfig(SharedMemConfig config)
490
throws CudaException {
491
function.setSharedMemConfig(config);
492
}
493
}
494
495