Path: blob/master/runtime/cuda/CudaFunction.cpp
5990 views
/*******************************************************************************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_CudaFunction.h"2425#ifdef OMR_OPT_CUDA2627/**28* Query a function attribute.29*30* Class: com.ibm.cuda.CudaFunction31* Method: getAttribute32* Signature: (IJI)I33*34* @param[in] env the JNI interface pointer35* @param[in] (unused) the class pointer36* @param[in] deviceId the device identifier37* @param[in] function the function pointer38* @param[in] attribute the attribute to query39* @return the value of the requested attribute40*/41jint JNICALL42Java_com_ibm_cuda_CudaFunction_getAttribute43(JNIEnv * env, jclass, jint deviceId, jlong function, jint attribute)44{45J9VMThread * thread = (J9VMThread *)env;4647Trc_cuda_functionGetAttribute_entry(thread, deviceId, (J9CudaFunction)function, attribute);4849PORT_ACCESS_FROM_ENV(env);5051int32_t value = 0;52int32_t error = j9cuda_funcGetAttribute(53(uint32_t)deviceId,54(J9CudaFunction)function,55(J9CudaFunctionAttribute)attribute,56&value);5758if (0 != error) {59throwCudaException(env, error);60}6162Trc_cuda_functionGetAttribute_exit(thread, value);6364return (jint)value;65}6667/**68* Launch a grid of threads executing a kernel function.69*70* Class: com.ibm.cuda.CudaFunction71* Method: launch72* Signature: (IJIIIIIIIJ[J)V73*74* @param[in] env the JNI interface pointer75* @param[in] (unused) the class pointer76* @param[in] deviceId the device identifier77* @param[in] function the function pointer78* @param[in] grdDimX the grid size in the X dimension79* @param[in] grdDimY the grid size in the Y dimension80* @param[in] grdDimZ the grid size in the Z dimension81* @param[in] blkDimX the block size in the X dimension82* @param[in] blkDimY the block size in the Y dimension83* @param[in] blkDimZ the block size in the Z dimension84* @param[in] sharedMemBytes the number of bytes of dynamic shared memory required85* @param[in] stream the stream, or null for the default stream86* @param[in] argValues the array of argument values87*/88void JNICALL89Java_com_ibm_cuda_CudaFunction_launch90(JNIEnv * env, jclass,91jint deviceId, jlong function,92jint grdDimX, jint grdDimY, jint grdDimZ,93jint blkDimX, jint blkDimY, jint blkDimZ,94jint sharedMemBytes, jlong stream,95jlongArray argValues)96{97J9VMThread * thread = (J9VMThread *)env;9899Trc_cuda_functionLaunch_entry(100thread,101deviceId,102(J9CudaFunction)function,103grdDimX, grdDimY, grdDimZ,104blkDimX, blkDimY, blkDimZ,105sharedMemBytes,106(J9CudaStream)stream,107argValues);108109int32_t error = J9CUDA_ERROR_OPERATING_SYSTEM;110jlong * values = env->GetLongArrayElements(argValues, NULL);111112if (NULL == values) {113Trc_cuda_functionLaunch_getFail(thread);114} else {115PORT_ACCESS_FROM_ENV(env);116117jsize argCount = env->GetArrayLength(argValues);118void ** args = (void **)J9CUDA_ALLOCATE_MEMORY(argCount * sizeof(void *));119120if (NULL == args) {121Trc_cuda_functionLaunch_allocFail(thread);122error = J9CUDA_ERROR_MEMORY_ALLOCATION;123} else {124// gather addresses of parameters125for (jsize i = 0; i < argCount; ++i) {126args[i] = &values[i];127}128129error = j9cuda_launchKernel(130(uint32_t)deviceId,131(J9CudaFunction)function,132(uint32_t)grdDimX, (uint32_t)grdDimY, (uint32_t)grdDimZ,133(uint32_t)blkDimX, (uint32_t)blkDimY, (uint32_t)blkDimZ,134(uint32_t)sharedMemBytes,135(J9CudaStream)stream,136(void **)args);137138J9CUDA_FREE_MEMORY(args);139}140141env->ReleaseLongArrayElements(argValues, values, JNI_ABORT);142}143144if (0 != error) {145throwCudaException(env, error);146}147148Trc_cuda_functionLaunch_exit(thread);149}150151/**152* Set the cache configuration of a kernel function.153*154* Class: com.ibm.cuda.CudaFunction155* Method: setCacheConfig156* Signature: (IJI)V157*158* @param[in] env the JNI interface pointer159* @param[in] (unused) the class pointer160* @param[in] deviceId the device identifier161* @param[in] function the function pointer162* @param[in] config the requested cache configuration163*/164void JNICALL165Java_com_ibm_cuda_CudaFunction_setCacheConfig166(JNIEnv * env, jclass, jint deviceId, jlong function, jint config)167{168J9VMThread * thread = (J9VMThread *)env;169170Trc_cuda_functionSetCacheConfig_entry(thread, deviceId, (J9CudaFunction)function, config);171172PORT_ACCESS_FROM_ENV(env);173174int32_t error = j9cuda_funcSetCacheConfig(175(uint32_t)deviceId,176(J9CudaFunction)function,177(J9CudaCacheConfig)config);178179if (0 != error) {180throwCudaException(env, error);181}182183Trc_cuda_functionSetCacheConfig_exit(thread);184}185186/**187* Set the shared memory configuration of a kernel function.188*189* Class: com.ibm.cuda.CudaFunction190* Method: setSharedMemConfig191* Signature: (IJI)V192*193* @param[in] env the JNI interface pointer194* @param[in] (unused) the class pointer195* @param[in] deviceId the device identifier196* @param[in] function the function pointer197* @param[in] config the requested shared memory configuration198*/199void JNICALL200Java_com_ibm_cuda_CudaFunction_setSharedMemConfig201(JNIEnv * env, jclass, jint deviceId, jlong function, jint config)202{203J9VMThread * thread = (J9VMThread *)env;204205Trc_cuda_functionSetSharedMemConfig_entry(thread, deviceId, (J9CudaFunction)function, config);206207PORT_ACCESS_FROM_ENV(env);208209int32_t error = j9cuda_funcSetSharedMemConfig(210(uint32_t)deviceId,211(J9CudaFunction)function,212(J9CudaSharedMemConfig)config);213214if (0 != error) {215throwCudaException(env, error);216}217218Trc_cuda_functionSetSharedMemConfig_exit(thread);219}220221#endif /* OMR_OPT_CUDA */222223224