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/CudaFunction.java
12917 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
import com.ibm.cuda.CudaKernel.Parameters;
28
29
/**
30
* The {@code CudaFunction} class represents a kernel entry point found in
31
* a specific {@code CudaModule} loaded on a CUDA-capable device.
32
*/
33
public final class CudaFunction {
34
35
/**
36
* The binary architecture version for which the function was compiled.
37
* This value is the major binary version * 10 + the minor binary version,
38
* so a binary version 1.3 function would return the value 13. Note that
39
* this will return a value of 10 for legacy cubins that do not have a
40
* properly-encoded binary architecture version.
41
*/
42
public static final int ATTRIBUTE_BINARY_VERSION = 6;
43
44
/**
45
* The size in bytes of user-allocated constant memory required by this
46
* function.
47
*/
48
public static final int ATTRIBUTE_CONST_SIZE_BYTES = 2;
49
50
/**
51
* The size in bytes of local memory used by each thread of this function.
52
*/
53
public static final int ATTRIBUTE_LOCAL_SIZE_BYTES = 3;
54
55
/**
56
* The maximum number of threads per block, beyond which a launch of the
57
* function would fail. This number depends on both the function and the
58
* device on which the function is currently loaded.
59
*/
60
public static final int ATTRIBUTE_MAX_THREADS_PER_BLOCK = 0;
61
62
/**
63
* The number of registers used by each thread of this function.
64
*/
65
public static final int ATTRIBUTE_NUM_REGS = 4;
66
67
/**
68
* The PTX virtual architecture version for which the function was
69
* compiled. This value is the major PTX version * 10 + the minor PTX
70
* version, so a PTX version 1.3 function would return the value 13.
71
* Note that this may return the undefined value of 0 for cubins
72
* compiled prior to CUDA 3.0.
73
*/
74
public static final int ATTRIBUTE_PTX_VERSION = 5;
75
76
/**
77
* The size in bytes of statically-allocated shared memory required by
78
* this function. This does not include dynamically-allocated shared
79
* memory requested by the user at runtime.
80
*/
81
public static final int ATTRIBUTE_SHARED_SIZE_BYTES = 1;
82
83
private static native int getAttribute(int deviceId, long nativeHandle,
84
int attribute) throws CudaException;
85
86
private static native void launch(int deviceId, long functionPtr, // <br/>
87
int gridDimX, int gridDimY, int gridDimZ, // <br/>
88
int blockDimX, int blockDimY, int blockDimZ, // <br/>
89
int sharedMemBytes, long stream, // <br/>
90
long[] parameterValues) throws CudaException;
91
92
static long nativeValueOf(Object parameter) {
93
long value = 0;
94
95
if (parameter != null) {
96
Class<? extends Object> type = parameter.getClass();
97
98
// type tests are sorted in order of decreasing (expected) likelihood
99
if (type == CudaBuffer.class) {
100
value = ((CudaBuffer) parameter).getAddress();
101
} else if (type == Integer.class) {
102
value = ((Integer) parameter).intValue();
103
} else if (type == Long.class) {
104
value = ((Long) parameter).longValue();
105
} else if (type == Double.class) {
106
value = Double.doubleToRawLongBits( // <br/>
107
((Double) parameter).doubleValue());
108
} else if (type == Float.class) {
109
value = Float.floatToRawIntBits( // <br/>
110
((Float) parameter).floatValue());
111
} else if (type == Short.class) {
112
value = ((Short) parameter).shortValue();
113
} else if (type == Byte.class) {
114
value = ((Byte) parameter).byteValue();
115
} else if (type == Character.class) {
116
value = ((Character) parameter).charValue();
117
} else {
118
throw new IllegalArgumentException();
119
}
120
}
121
122
return value;
123
}
124
125
private static native void setCacheConfig(int deviceId, long nativeHandle,
126
int config) throws CudaException;
127
128
private static native void setSharedMemConfig(int deviceId,
129
long nativeHandle, int config) throws CudaException;
130
131
final int deviceId;
132
133
private final long nativeHandle;
134
135
CudaFunction(int deviceId, long nativeHandle) {
136
super();
137
this.deviceId = deviceId;
138
this.nativeHandle = nativeHandle;
139
}
140
141
/**
142
* Returns the value of the specified @{code attribute}.
143
*
144
* @param attribute
145
* the attribute to be queried (see ATTRIBUTE_XXX)
146
* @return
147
* the attribute value
148
* @throws CudaException
149
* if a CUDA exception occurs
150
*/
151
public int getAttribute(int attribute) throws CudaException {
152
return getAttribute(deviceId, nativeHandle, attribute);
153
}
154
155
void launch(CudaGrid grid, Object... parameters) throws CudaException {
156
int parameterCount = parameters.length;
157
long[] nativeValues = new long[parameterCount];
158
159
for (int i = 0; i < parameterCount; ++i) {
160
nativeValues[i] = nativeValueOf(parameters[i]);
161
}
162
163
CudaStream stream = grid.stream;
164
165
launch(deviceId, nativeHandle, // <br/>
166
grid.gridDimX, grid.gridDimY, grid.gridDimZ, // <br/>
167
grid.blockDimX, grid.blockDimY, grid.blockDimZ, // <br/>
168
grid.sharedMemBytes, // <br/>
169
stream != null ? stream.getHandle() : 0, // <br/>
170
nativeValues);
171
}
172
173
void launch(CudaGrid grid, Parameters parameters) throws CudaException {
174
if (!parameters.isComplete()) {
175
throw new IllegalArgumentException();
176
}
177
178
CudaStream stream = grid.stream;
179
180
launch(deviceId, nativeHandle, // <br/>
181
grid.gridDimX, grid.gridDimY, grid.gridDimZ, // <br/>
182
grid.blockDimX, grid.blockDimY, grid.blockDimZ, // <br/>
183
grid.sharedMemBytes, // <br/>
184
stream != null ? stream.getHandle() : 0, // <br/>
185
parameters.values);
186
}
187
188
/**
189
* Configures the cache for this function.
190
*
191
* @param config
192
* the desired cache configuration
193
* @throws CudaException
194
* if a CUDA exception occurs
195
*/
196
public void setCacheConfig(CacheConfig config) throws CudaException {
197
setCacheConfig(deviceId, nativeHandle, config.nativeValue);
198
}
199
200
/**
201
* Configures the shared memory of this function.
202
*
203
* @param config
204
* the desired shared memory configuration
205
* @throws CudaException
206
* if a CUDA exception occurs
207
*/
208
public void setSharedMemConfig(SharedMemConfig config) throws CudaException {
209
setSharedMemConfig(deviceId, nativeHandle, config.nativeValue);
210
}
211
}
212
213