Path: blob/master/runtime/cuda/CudaJitOptions.cpp
5990 views
/*******************************************************************************1* Copyright (c) 2013, 2015 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_CudaJitOptions.h"2425namespace26{2728/**29* Destroy a JIT options instance.30*/31void32destroyOptions(JNIEnv * env, J9CudaJitOptions * options)33{34J9VMThread * thread = (J9VMThread *)env;3536Trc_cuda_jitOptionsDestroy0_entry(thread, options);3738PORT_ACCESS_FROM_ENV(env);3940void * errorLog = options->errorLogBuffer;4142if (NULL != errorLog) {43J9CUDA_FREE_MEMORY(errorLog);44}4546void * infoLog = options->infoLogBuffer;4748if (NULL != infoLog) {49J9CUDA_FREE_MEMORY(infoLog);50}5152J9CUDA_FREE_MEMORY(options);5354Trc_cuda_jitOptionsDestroy0_exit(thread);55}5657} // namespace5859/**60* Create and initialize a JIT options object.61*62* Class: com.ibm.cuda.CudaJitOptions63* Method: create64* Signature: ([I)J65*66* @param[in] env the JNI interface pointer67* @param[in] (unused) the class pointer68* @param[in] pairs an array of option/value pairs69* @return a pointer to a new JIT options object70*/71jlong JNICALL72Java_com_ibm_cuda_CudaJitOptions_create73(JNIEnv * env, jclass, jintArray pairs)74{75J9VMThread * thread = (J9VMThread *)env;7677Trc_cuda_jitOptionsCreate_entry(thread, pairs);7879jsize const pairLength = env->GetArrayLength(pairs) & ~1;80jint * pairData = NULL;8182if (0 != pairLength) {83pairData = env->GetIntArrayElements(pairs, NULL);8485if (NULL == pairData) {86Trc_cuda_jitOptionsCreate_getFail(thread);87throwCudaException(env, J9CUDA_ERROR_OPERATING_SYSTEM);88Trc_cuda_jitOptionsCreate_exit(thread, NULL);89return 0;90}91}9293PORT_ACCESS_FROM_ENV(env);9495int32_t error = 0;96J9CudaJitOptions * options = (J9CudaJitOptions *)J9CUDA_ALLOCATE_MEMORY(sizeof(J9CudaJitOptions));9798if (NULL == options) {99Trc_cuda_jitOptionsCreate_allocFail(thread);100error = J9CUDA_ERROR_MEMORY_ALLOCATION;101} else {102memset(options, 0, sizeof(J9CudaJitOptions));103104for (jint index = 0; (0 == error) && (index < pairLength); index += 2) {105jint key = pairData[index];106jint value = pairData[index + 1];107108switch (key) {109case com_ibm_cuda_CudaJitOptions_OPT_CACHE_MODE:110options->cacheMode = (J9CudaJitCacheMode)value;111break;112113case com_ibm_cuda_CudaJitOptions_OPT_ERROR_LOG_BUFFER_SIZE_BYTES:114if (NULL != options->errorLogBuffer) {115J9CUDA_FREE_MEMORY(options->errorLogBuffer);116}117118options->errorLogBuffer = (char *)J9CUDA_ALLOCATE_MEMORY((size_t)value);119options->errorLogBufferSize = (uintptr_t)value;120121if (NULL == options->errorLogBuffer) {122Trc_cuda_jitOptionsCreate_allocFail(thread);123options->errorLogBufferSize = 0;124error = J9CUDA_ERROR_MEMORY_ALLOCATION;125}126break;127128case com_ibm_cuda_CudaJitOptions_OPT_FALLBACK_STRATEGY:129options->fallbackStrategy = (J9CudaJitFallbackStrategy)value;130break;131132case com_ibm_cuda_CudaJitOptions_OPT_GENERATE_DEBUG_INFO:133options->generateDebugInfo = value ? J9CUDA_JIT_FLAG_ENABLED : J9CUDA_JIT_FLAG_DISABLED;134break;135136case com_ibm_cuda_CudaJitOptions_OPT_GENERATE_LINE_INFO:137options->generateLineInfo = value ? J9CUDA_JIT_FLAG_ENABLED : J9CUDA_JIT_FLAG_DISABLED;138break;139140case com_ibm_cuda_CudaJitOptions_OPT_INFO_LOG_BUFFER_SIZE_BYTES:141if (NULL != options->infoLogBuffer) {142J9CUDA_FREE_MEMORY(options->infoLogBuffer);143}144145options->infoLogBuffer = (char *)J9CUDA_ALLOCATE_MEMORY((size_t)value);146options->infoLogBufferSize = (uintptr_t)value;147148if (NULL == options->infoLogBuffer) {149Trc_cuda_jitOptionsCreate_allocFail(thread);150options->infoLogBufferSize = 0;151error = J9CUDA_ERROR_MEMORY_ALLOCATION;152}153break;154155case com_ibm_cuda_CudaJitOptions_OPT_LOG_VERBOSE:156options->verboseLogging = value ? J9CUDA_JIT_FLAG_ENABLED : J9CUDA_JIT_FLAG_DISABLED;157break;158159case com_ibm_cuda_CudaJitOptions_OPT_MAX_REGISTERS:160options->maxRegisters = (uint32_t)value;161break;162163case com_ibm_cuda_CudaJitOptions_OPT_OPTIMIZATION_LEVEL:164switch (value) {165case 0:166options->optimizationLevel = J9CUDA_JIT_OPTIMIZATION_LEVEL_0;167break;168case 1:169options->optimizationLevel = J9CUDA_JIT_OPTIMIZATION_LEVEL_1;170break;171case 2:172options->optimizationLevel = J9CUDA_JIT_OPTIMIZATION_LEVEL_2;173break;174case 3:175options->optimizationLevel = J9CUDA_JIT_OPTIMIZATION_LEVEL_3;176break;177case 4:178options->optimizationLevel = J9CUDA_JIT_OPTIMIZATION_LEVEL_4;179break;180default:181error = J9CUDA_ERROR_INVALID_VALUE;182break;183}184break;185186case com_ibm_cuda_CudaJitOptions_OPT_TARGET:187options->target = (uint32_t)value;188break;189190case com_ibm_cuda_CudaJitOptions_OPT_TARGET_FROM_CUCONTEXT:191options->targetFromContext = value ? J9CUDA_JIT_FLAG_ENABLED : J9CUDA_JIT_FLAG_DISABLED;192break;193194case com_ibm_cuda_CudaJitOptions_OPT_THREADS_PER_BLOCK:195if (0 != value) {196options->threadsPerBlock = (uint32_t)value;197} else {198error = J9CUDA_ERROR_INVALID_VALUE;199}200break;201202case com_ibm_cuda_CudaJitOptions_OPT_WALL_TIME:203options->recordWallTime = value ? J9CUDA_JIT_FLAG_ENABLED : J9CUDA_JIT_FLAG_DISABLED;204break;205206default:207Trc_cuda_jitOptionsCreate_badOption(thread, key);208error = J9CUDA_ERROR_INVALID_VALUE;209break;210}211}212}213214if (NULL != pairData) {215env->ReleaseIntArrayElements(pairs, pairData, JNI_ABORT);216}217218if (0 != error) {219throwCudaException(env, error);220}221222if (env->ExceptionCheck() && (NULL != options)) {223destroyOptions(env, options);224options = NULL;225}226227Trc_cuda_jitOptionsCreate_exit(thread, options);228229return (jlong)options;230}231232/**233* Destroy a JIT options object.234*235* Class: com.ibm.cuda.CudaJitOptions236* Method: destroy237* Signature: (J)V238*239* @param[in] env the JNI interface pointer240* @param[in] (unused) the class pointer241* @param[in] handle the options pointer242*/243void JNICALL244Java_com_ibm_cuda_CudaJitOptions_destroy245(JNIEnv * env, jclass, jlong handle)246{247J9VMThread * thread = (J9VMThread *)env;248J9CudaJitOptions * options = (J9CudaJitOptions *)handle;249250Trc_cuda_jitOptionsDestroy_entry(thread, options);251252destroyOptions(env, options);253254Trc_cuda_jitOptionsDestroy_exit(thread);255}256257/**258* Return the contents of the error log.259*260* Class: com.ibm.cuda.CudaJitOptions261* Method: getErrorLogBuffer262* Signature: (J)Ljava/lang/String;263*264* @param[in] env the JNI interface pointer265* @param[in] (unused) the class pointer266* @param[in] handle the options pointer267* @return a string containing the error log268*/269jstring JNICALL270Java_com_ibm_cuda_CudaJitOptions_getErrorLogBuffer271(JNIEnv * env, jclass, jlong handle)272{273J9VMThread * thread = (J9VMThread *)env;274J9CudaJitOptions * options = (J9CudaJitOptions *)handle;275276Trc_cuda_jitOptionsGetErrorLogBuffer_entry(thread, options);277278const char * buffer = ((J9CudaJitOptions *)handle)->errorLogBuffer;279jstring result = env->NewStringUTF((NULL == buffer) ? "" : buffer);280281Trc_cuda_jitOptionsGetErrorLogBuffer_exit(thread, result);282283return result;284}285286/**287* Return the contents of the information log.288*289* Class: com.ibm.cuda.CudaJitOptions290* Method: getInfoLogBuffer291* Signature: (J)Ljava/lang/String;292*293* @param[in] env the JNI interface pointer294* @param[in] (unused) the class pointer295* @param[in] handle the options pointer296* @return a string containing the information log297*/298jstring JNICALL299Java_com_ibm_cuda_CudaJitOptions_getInfoLogBuffer300(JNIEnv * env, jclass, jlong handle)301{302J9VMThread * thread = (J9VMThread *)env;303J9CudaJitOptions * options = (J9CudaJitOptions *)handle;304305Trc_cuda_jitOptionsGetInfoLogBuffer_entry(thread, options);306307const char * buffer = options->infoLogBuffer;308jstring result = env->NewStringUTF((NULL == buffer) ? "" : buffer);309310Trc_cuda_jitOptionsGetInfoLogBuffer_exit(thread, result);311312return result;313}314315/**316* Return the number of threads the compiler actually targeted per block.317*318* Class: com.ibm.cuda.CudaJitOptions319* Method: getThreadsPerBlock320* Signature: (J)I321*322* @param[in] env the JNI interface pointer323* @param[in] (unused) the class pointer324* @param[in] handle the options pointer325* @return the number of threads per block326*/327jint JNICALL328Java_com_ibm_cuda_CudaJitOptions_getThreadsPerBlock329(JNIEnv * env, jclass, jlong handle)330{331J9VMThread * thread = (J9VMThread *)env;332J9CudaJitOptions * options = (J9CudaJitOptions *)handle;333334Trc_cuda_jitOptionsGetThreadsPerBlock_entry(thread, options);335336jint result = (jint)options->threadsPerBlock;337338Trc_cuda_jitOptionsGetThreadsPerBlock_exit(thread, result);339340return result;341}342343/**344* Return the elapsed compile or link time in milliseconds.345*346* Class: com.ibm.cuda.CudaJitOptions347* Method: getWallTime348* Signature: (J)F349*350* @param[in] env the JNI interface pointer351* @param[in] (unused) the class pointer352* @param[in] handle the options pointer353* @return the elapsed compile/link time354*/355jfloat JNICALL356Java_com_ibm_cuda_CudaJitOptions_getWallTime357(JNIEnv * env, jclass, jlong handle)358{359J9VMThread * thread = (J9VMThread *)env;360J9CudaJitOptions * options = (J9CudaJitOptions *)handle;361362Trc_cuda_jitOptionsGetWallTime_entry(thread, options);363364jfloat result = (jfloat)options->wallTime;365366Trc_cuda_jitOptionsGetWallTime_exit(thread, result);367368return result;369}370371372