/*******************************************************************************1* Copyright (c) 2013, 2016 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* 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-exception20*******************************************************************************/2122#include "CudaCommon.hpp"23#include "java/com_ibm_cuda_CudaModule.h"2425#ifdef OMR_OPT_CUDA2627/**28* Find a kernel function in a module.29*30* Class: com.ibm.cuda.CudaModule31* Method: getFunction32* Signature: (IJLjava/lang/String;)J33*34* @param[in] env the JNI interface pointer35* @param[in] (unused) the class pointer36* @param[in] deviceId the device identifier37* @param[in] module the module pointer38* @param[in] name the requested function name39* @return the function pointer40*/41jlong JNICALL42Java_com_ibm_cuda_CudaModule_getFunction43(JNIEnv * env, jclass, jint deviceId, jlong module, jstring name)44{45J9VMThread * thread = (J9VMThread *)env;4647Trc_cuda_moduleGetFunction_entry(thread, deviceId, (J9CudaModule)module, name);4849const char * cName = env->GetStringUTFChars(name, NULL);50int32_t error = J9CUDA_ERROR_OPERATING_SYSTEM;51J9CudaFunction function = 0;5253if (NULL == cName) {54Trc_cuda_module_getStringFail(thread);55} else {56PORT_ACCESS_FROM_ENV(env);5758error = j9cuda_moduleGetFunction(59(uint32_t)deviceId,60(J9CudaModule)module,61cName,62&function);6364env->ReleaseStringUTFChars(name, cName);65}6667if (0 != error) {68throwCudaException(env, error);69}7071Trc_cuda_moduleGetFunction_exit(thread, function);7273return (jlong)function;74}7576/**77* Find a global symbol in a module.78*79* Class: com.ibm.cuda.CudaModule80* Method: getGlobal81* Signature: (IJLjava/lang/String;)J82*83* @param[in] env the JNI interface pointer84* @param[in] (unused) the class pointer85* @param[in] deviceId the device identifier86* @param[in] module the module pointer87* @param[in] name the requested global symbol name88* @return the global pointer89*/90jlong JNICALL91Java_com_ibm_cuda_CudaModule_getGlobal92(JNIEnv * env, jclass, jint deviceId, jlong module, jstring name)93{94J9VMThread * thread = (J9VMThread *)env;9596Trc_cuda_moduleGetGlobal_entry(thread, deviceId, (J9CudaModule)module, name);9798const char * cName = env->GetStringUTFChars(name, NULL);99int32_t error = J9CUDA_ERROR_OPERATING_SYSTEM;100uintptr_t size = 0;101uintptr_t symbol = 0;102103if (NULL == cName) {104Trc_cuda_module_getStringFail(thread);105} else {106PORT_ACCESS_FROM_ENV(env);107108error = j9cuda_moduleGetGlobal(109(uint32_t)deviceId,110(J9CudaModule)module,111cName,112&symbol,113&size);114115env->ReleaseStringUTFChars(name, cName);116}117118if (0 != error) {119throwCudaException(env, error);120}121122Trc_cuda_moduleGetGlobal_exit(thread, (uintptr_t)symbol);123124return (jlong)symbol;125}126127/**128* Find a surface in a module.129*130* Class: com.ibm.cuda.CudaModule131* Method: getSurface132* Signature: (IJLjava/lang/String;)J133*134* @param[in] env the JNI interface pointer135* @param[in] (unused) the class pointer136* @param[in] deviceId the device identifier137* @param[in] module the module pointer138* @param[in] name the requested surface name139* @return the surface pointer140*/141jlong JNICALL142Java_com_ibm_cuda_CudaModule_getSurface143(JNIEnv * env, jclass, jint deviceId, jlong module, jstring name)144{145J9VMThread * thread = (J9VMThread *)env;146147Trc_cuda_moduleGetSurface_entry(thread, deviceId, (J9CudaModule)module, name);148149const char * cName = env->GetStringUTFChars(name, NULL);150int32_t error = J9CUDA_ERROR_OPERATING_SYSTEM;151uintptr_t surface = 0;152153if (NULL == cName) {154Trc_cuda_module_getStringFail(thread);155} else {156PORT_ACCESS_FROM_ENV(env);157158error = j9cuda_moduleGetSurfaceRef(159(uint32_t)deviceId,160(J9CudaModule)module,161cName,162&surface);163164env->ReleaseStringUTFChars(name, cName);165}166167if (0 != error) {168throwCudaException(env, error);169}170171Trc_cuda_moduleGetSurface_exit(thread, (uintptr_t)surface);172173return (jlong)surface;174}175176/**177* Find a texture in a module.178*179* Class: com.ibm.cuda.CudaModule180* Method: getTexture181* Signature: (IJLjava/lang/String;)J182*183* @param[in] env the JNI interface pointer184* @param[in] (unused) the class pointer185* @param[in] deviceId the device identifier186* @param[in] module the module pointer187* @param[in] name the requested texture name188* @return the texture pointer189*/190jlong JNICALL191Java_com_ibm_cuda_CudaModule_getTexture192(JNIEnv * env, jclass, jint deviceId, jlong module, jstring name)193{194J9VMThread * thread = (J9VMThread *)env;195196Trc_cuda_moduleGetTexture_entry(thread, deviceId, (J9CudaModule)module, name);197198const char * cName = env->GetStringUTFChars(name, NULL);199int32_t error = J9CUDA_ERROR_OPERATING_SYSTEM;200uintptr_t texture = 0;201202if (NULL == cName) {203Trc_cuda_module_getStringFail(thread);204} else {205PORT_ACCESS_FROM_ENV(env);206207error = j9cuda_moduleGetTextureRef(208(uint32_t)deviceId,209(J9CudaModule)module,210cName,211&texture);212213env->ReleaseStringUTFChars(name, cName);214}215216if (0 != error) {217throwCudaException(env, error);218}219220Trc_cuda_moduleGetTexture_exit(thread, (uintptr_t)texture);221222return (jlong)texture;223}224225#endif /* OMR_OPT_CUDA */226227/**228* Load a module onto a device.229*230* Class: com.ibm.cuda.CudaModule231* Method: load232* Signature: (I[BJ)J233*234* @param[in] env the JNI interface pointer235* @param[in] (unused) the class pointer236* @param[in] deviceId the device identifier237* @param[in] image the module image238* @param[in] options Fan optional options pointer239* @return the module pointer240*/241jlong JNICALL242Java_com_ibm_cuda_CudaModule_load243(JNIEnv * env, jclass, jint deviceId, jbyteArray image, jlong options)244{245J9VMThread * thread = (J9VMThread *)env;246247Trc_cuda_moduleLoad_entry(thread, deviceId, image, (J9CudaJitOptions *)options);248249J9CudaModule module = NULL;250int32_t error = J9CUDA_ERROR_NO_DEVICE;251#ifdef OMR_OPT_CUDA252jbyte * imageData = env->GetByteArrayElements(image, NULL);253254if (NULL == imageData) {255error = J9CUDA_ERROR_OPERATING_SYSTEM;256Trc_cuda_moduleLoad_fail(thread);257} else {258PORT_ACCESS_FROM_ENV(env);259260error = j9cuda_moduleLoad(261(uint32_t)deviceId,262imageData,263(J9CudaJitOptions *)options,264&module);265266env->ReleaseByteArrayElements(image, imageData, JNI_ABORT);267}268#endif /* OMR_OPT_CUDA */269270if (0 != error) {271throwCudaException(env, error);272}273274Trc_cuda_moduleLoad_exit(thread, module);275276return (jlong)module;277}278279#ifdef OMR_OPT_CUDA280281/**282* Unload a module from a device.283*284* Class: com.ibm.cuda.CudaModule285* Method: unload286* Signature: (IJ)V287*288* @param[in] env the JNI interface pointer289* @param[in] (unused) the class pointer290* @param[in] deviceId the device identifier291* @param[in] module the module pointer292*/293void JNICALL294Java_com_ibm_cuda_CudaModule_unload295(JNIEnv * env, jclass, jint deviceId, jlong module)296{297J9VMThread * thread = (J9VMThread *)env;298299Trc_cuda_moduleUnload_entry(thread, deviceId, (J9CudaModule)module);300301PORT_ACCESS_FROM_ENV(env);302303int32_t error = j9cuda_moduleUnload((uint32_t)deviceId, (J9CudaModule)module);304305if (0 != error) {306throwCudaException(env, error);307}308309Trc_cuda_moduleUnload_exit(thread);310}311312#endif /* OMR_OPT_CUDA */313314315