Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/runtime/cuda/CudaModule.cpp
5990 views
1
/*******************************************************************************
2
* Copyright (c) 2013, 2016 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* 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
21
*******************************************************************************/
22
23
#include "CudaCommon.hpp"
24
#include "java/com_ibm_cuda_CudaModule.h"
25
26
#ifdef OMR_OPT_CUDA
27
28
/**
29
* Find a kernel function in a module.
30
*
31
* Class: com.ibm.cuda.CudaModule
32
* Method: getFunction
33
* Signature: (IJLjava/lang/String;)J
34
*
35
* @param[in] env the JNI interface pointer
36
* @param[in] (unused) the class pointer
37
* @param[in] deviceId the device identifier
38
* @param[in] module the module pointer
39
* @param[in] name the requested function name
40
* @return the function pointer
41
*/
42
jlong JNICALL
43
Java_com_ibm_cuda_CudaModule_getFunction
44
(JNIEnv * env, jclass, jint deviceId, jlong module, jstring name)
45
{
46
J9VMThread * thread = (J9VMThread *)env;
47
48
Trc_cuda_moduleGetFunction_entry(thread, deviceId, (J9CudaModule)module, name);
49
50
const char * cName = env->GetStringUTFChars(name, NULL);
51
int32_t error = J9CUDA_ERROR_OPERATING_SYSTEM;
52
J9CudaFunction function = 0;
53
54
if (NULL == cName) {
55
Trc_cuda_module_getStringFail(thread);
56
} else {
57
PORT_ACCESS_FROM_ENV(env);
58
59
error = j9cuda_moduleGetFunction(
60
(uint32_t)deviceId,
61
(J9CudaModule)module,
62
cName,
63
&function);
64
65
env->ReleaseStringUTFChars(name, cName);
66
}
67
68
if (0 != error) {
69
throwCudaException(env, error);
70
}
71
72
Trc_cuda_moduleGetFunction_exit(thread, function);
73
74
return (jlong)function;
75
}
76
77
/**
78
* Find a global symbol in a module.
79
*
80
* Class: com.ibm.cuda.CudaModule
81
* Method: getGlobal
82
* Signature: (IJLjava/lang/String;)J
83
*
84
* @param[in] env the JNI interface pointer
85
* @param[in] (unused) the class pointer
86
* @param[in] deviceId the device identifier
87
* @param[in] module the module pointer
88
* @param[in] name the requested global symbol name
89
* @return the global pointer
90
*/
91
jlong JNICALL
92
Java_com_ibm_cuda_CudaModule_getGlobal
93
(JNIEnv * env, jclass, jint deviceId, jlong module, jstring name)
94
{
95
J9VMThread * thread = (J9VMThread *)env;
96
97
Trc_cuda_moduleGetGlobal_entry(thread, deviceId, (J9CudaModule)module, name);
98
99
const char * cName = env->GetStringUTFChars(name, NULL);
100
int32_t error = J9CUDA_ERROR_OPERATING_SYSTEM;
101
uintptr_t size = 0;
102
uintptr_t symbol = 0;
103
104
if (NULL == cName) {
105
Trc_cuda_module_getStringFail(thread);
106
} else {
107
PORT_ACCESS_FROM_ENV(env);
108
109
error = j9cuda_moduleGetGlobal(
110
(uint32_t)deviceId,
111
(J9CudaModule)module,
112
cName,
113
&symbol,
114
&size);
115
116
env->ReleaseStringUTFChars(name, cName);
117
}
118
119
if (0 != error) {
120
throwCudaException(env, error);
121
}
122
123
Trc_cuda_moduleGetGlobal_exit(thread, (uintptr_t)symbol);
124
125
return (jlong)symbol;
126
}
127
128
/**
129
* Find a surface in a module.
130
*
131
* Class: com.ibm.cuda.CudaModule
132
* Method: getSurface
133
* Signature: (IJLjava/lang/String;)J
134
*
135
* @param[in] env the JNI interface pointer
136
* @param[in] (unused) the class pointer
137
* @param[in] deviceId the device identifier
138
* @param[in] module the module pointer
139
* @param[in] name the requested surface name
140
* @return the surface pointer
141
*/
142
jlong JNICALL
143
Java_com_ibm_cuda_CudaModule_getSurface
144
(JNIEnv * env, jclass, jint deviceId, jlong module, jstring name)
145
{
146
J9VMThread * thread = (J9VMThread *)env;
147
148
Trc_cuda_moduleGetSurface_entry(thread, deviceId, (J9CudaModule)module, name);
149
150
const char * cName = env->GetStringUTFChars(name, NULL);
151
int32_t error = J9CUDA_ERROR_OPERATING_SYSTEM;
152
uintptr_t surface = 0;
153
154
if (NULL == cName) {
155
Trc_cuda_module_getStringFail(thread);
156
} else {
157
PORT_ACCESS_FROM_ENV(env);
158
159
error = j9cuda_moduleGetSurfaceRef(
160
(uint32_t)deviceId,
161
(J9CudaModule)module,
162
cName,
163
&surface);
164
165
env->ReleaseStringUTFChars(name, cName);
166
}
167
168
if (0 != error) {
169
throwCudaException(env, error);
170
}
171
172
Trc_cuda_moduleGetSurface_exit(thread, (uintptr_t)surface);
173
174
return (jlong)surface;
175
}
176
177
/**
178
* Find a texture in a module.
179
*
180
* Class: com.ibm.cuda.CudaModule
181
* Method: getTexture
182
* Signature: (IJLjava/lang/String;)J
183
*
184
* @param[in] env the JNI interface pointer
185
* @param[in] (unused) the class pointer
186
* @param[in] deviceId the device identifier
187
* @param[in] module the module pointer
188
* @param[in] name the requested texture name
189
* @return the texture pointer
190
*/
191
jlong JNICALL
192
Java_com_ibm_cuda_CudaModule_getTexture
193
(JNIEnv * env, jclass, jint deviceId, jlong module, jstring name)
194
{
195
J9VMThread * thread = (J9VMThread *)env;
196
197
Trc_cuda_moduleGetTexture_entry(thread, deviceId, (J9CudaModule)module, name);
198
199
const char * cName = env->GetStringUTFChars(name, NULL);
200
int32_t error = J9CUDA_ERROR_OPERATING_SYSTEM;
201
uintptr_t texture = 0;
202
203
if (NULL == cName) {
204
Trc_cuda_module_getStringFail(thread);
205
} else {
206
PORT_ACCESS_FROM_ENV(env);
207
208
error = j9cuda_moduleGetTextureRef(
209
(uint32_t)deviceId,
210
(J9CudaModule)module,
211
cName,
212
&texture);
213
214
env->ReleaseStringUTFChars(name, cName);
215
}
216
217
if (0 != error) {
218
throwCudaException(env, error);
219
}
220
221
Trc_cuda_moduleGetTexture_exit(thread, (uintptr_t)texture);
222
223
return (jlong)texture;
224
}
225
226
#endif /* OMR_OPT_CUDA */
227
228
/**
229
* Load a module onto a device.
230
*
231
* Class: com.ibm.cuda.CudaModule
232
* Method: load
233
* Signature: (I[BJ)J
234
*
235
* @param[in] env the JNI interface pointer
236
* @param[in] (unused) the class pointer
237
* @param[in] deviceId the device identifier
238
* @param[in] image the module image
239
* @param[in] options Fan optional options pointer
240
* @return the module pointer
241
*/
242
jlong JNICALL
243
Java_com_ibm_cuda_CudaModule_load
244
(JNIEnv * env, jclass, jint deviceId, jbyteArray image, jlong options)
245
{
246
J9VMThread * thread = (J9VMThread *)env;
247
248
Trc_cuda_moduleLoad_entry(thread, deviceId, image, (J9CudaJitOptions *)options);
249
250
J9CudaModule module = NULL;
251
int32_t error = J9CUDA_ERROR_NO_DEVICE;
252
#ifdef OMR_OPT_CUDA
253
jbyte * imageData = env->GetByteArrayElements(image, NULL);
254
255
if (NULL == imageData) {
256
error = J9CUDA_ERROR_OPERATING_SYSTEM;
257
Trc_cuda_moduleLoad_fail(thread);
258
} else {
259
PORT_ACCESS_FROM_ENV(env);
260
261
error = j9cuda_moduleLoad(
262
(uint32_t)deviceId,
263
imageData,
264
(J9CudaJitOptions *)options,
265
&module);
266
267
env->ReleaseByteArrayElements(image, imageData, JNI_ABORT);
268
}
269
#endif /* OMR_OPT_CUDA */
270
271
if (0 != error) {
272
throwCudaException(env, error);
273
}
274
275
Trc_cuda_moduleLoad_exit(thread, module);
276
277
return (jlong)module;
278
}
279
280
#ifdef OMR_OPT_CUDA
281
282
/**
283
* Unload a module from a device.
284
*
285
* Class: com.ibm.cuda.CudaModule
286
* Method: unload
287
* Signature: (IJ)V
288
*
289
* @param[in] env the JNI interface pointer
290
* @param[in] (unused) the class pointer
291
* @param[in] deviceId the device identifier
292
* @param[in] module the module pointer
293
*/
294
void JNICALL
295
Java_com_ibm_cuda_CudaModule_unload
296
(JNIEnv * env, jclass, jint deviceId, jlong module)
297
{
298
J9VMThread * thread = (J9VMThread *)env;
299
300
Trc_cuda_moduleUnload_entry(thread, deviceId, (J9CudaModule)module);
301
302
PORT_ACCESS_FROM_ENV(env);
303
304
int32_t error = j9cuda_moduleUnload((uint32_t)deviceId, (J9CudaModule)module);
305
306
if (0 != error) {
307
throwCudaException(env, error);
308
}
309
310
Trc_cuda_moduleUnload_exit(thread);
311
}
312
313
#endif /* OMR_OPT_CUDA */
314
315