/*******************************************************************************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_CudaStream.h"2425/**26* Create a stream with the default priority and flags.27*28* Class: com.ibm.cuda.CudaStream29* Method: create30* Signature: (I)J31*32* @param[in] env the JNI interface pointer33* @param[in] (unused) the class pointer34* @param[in] deviceId the device identifier35* @return a new stream36*/37jlong JNICALL38Java_com_ibm_cuda_CudaStream_create39(JNIEnv * env, jclass, jint deviceId)40{41J9VMThread * thread = (J9VMThread *)env;4243Trc_cuda_streamCreate_entry(thread, deviceId);4445J9CudaStream stream = NULL;46int32_t error = J9CUDA_ERROR_NO_DEVICE;47#ifdef OMR_OPT_CUDA48PORT_ACCESS_FROM_ENV(env);49error = j9cuda_streamCreate((uint32_t)deviceId, &stream);50#endif /* OMR_OPT_CUDA */5152if (0 != error) {53throwCudaException(env, error);54}5556Trc_cuda_streamCreate_exit(thread, stream);5758return (jlong)stream;59}6061/**62* Create a stream with the specified flags and priority.63*64* Class: com.ibm.cuda.CudaStream65* Method: createWithPriority66* Signature: (III)J67*68* @param[in] env the JNI interface pointer69* @param[in] (unused) the class pointer70* @param[in] deviceId the device identifier71* @param[in] flags the stream creation flags72* @param[in] priority the requested stream priority73* @return a new stream74*/75jlong JNICALL76Java_com_ibm_cuda_CudaStream_createWithPriority77(JNIEnv * env, jclass, jint deviceId, jint flags, jint priority)78{79J9VMThread * thread = (J9VMThread *)env;8081Trc_cuda_streamCreateWithPriority_entry(thread, deviceId, flags, priority);8283J9CudaStream stream = NULL;84int32_t error = J9CUDA_ERROR_NO_DEVICE;85#ifdef OMR_OPT_CUDA86PORT_ACCESS_FROM_ENV(env);87error = j9cuda_streamCreateWithPriority(88(uint32_t)deviceId,89priority,90(uint32_t)flags,91&stream);92#endif /* OMR_OPT_CUDA */9394if (0 != error) {95throwCudaException(env, error);96}9798Trc_cuda_streamCreateWithPriority_exit(thread, stream);99100return (jlong)stream;101}102103#ifdef OMR_OPT_CUDA104105/**106* Destroy a stream.107*108* Class: com.ibm.cuda.CudaStream109* Method: destroy110* Signature: (IJ)V111*112* @param[in] env the JNI interface pointer113* @param[in] (unused) the class pointer114* @param[in] deviceId the device identifier115* @param[in] stream the stream to be destroyed116*/117void JNICALL118Java_com_ibm_cuda_CudaStream_destroy119(JNIEnv * env, jclass, jint deviceId, jlong stream)120{121J9VMThread * thread = (J9VMThread *)env;122123Trc_cuda_streamDestroy_entry(thread, deviceId, (J9CudaStream)stream);124125PORT_ACCESS_FROM_ENV(env);126127int32_t error = j9cuda_streamDestroy((uint32_t)deviceId, (J9CudaStream)stream);128129if (0 != error) {130throwCudaException(env, error);131}132133Trc_cuda_streamDestroy_exit(thread, error);134}135136/**137* Query the flags of a stream.138*139* Class: com.ibm.cuda.CudaStream140* Method: getFlags141* Signature: (IJ)I142*143* @param[in] env the JNI interface pointer144* @param[in] (unused) the class pointer145* @param[in] deviceId the device identifier146* @param[in] stream the stream147* @return the flags of the stream148*/149jint JNICALL150Java_com_ibm_cuda_CudaStream_getFlags151(JNIEnv * env, jclass, jint deviceId, jlong stream)152{153J9VMThread * thread = (J9VMThread *)env;154155Trc_cuda_streamGetFlags_entry(thread, deviceId, (J9CudaStream)stream);156157PORT_ACCESS_FROM_ENV(env);158159uint32_t flags = 0;160int32_t error = j9cuda_streamGetFlags((uint32_t)deviceId, (J9CudaStream)stream, &flags);161162if (0 != error) {163throwCudaException(env, error);164}165166Trc_cuda_streamGetFlags_exit(thread, error, flags);167168return (jint)flags;169}170171/**172* Query the priority of a stream.173*174* Class: com.ibm.cuda.CudaStream175* Method: getPriority176* Signature: (IJ)I177*178* @param[in] env the JNI interface pointer179* @param[in] (unused) the class pointer180* @param[in] deviceId the device identifier181* @param[in] stream the stream182* @return the priority of the stream183*/184jint JNICALL185Java_com_ibm_cuda_CudaStream_getPriority186(JNIEnv * env, jclass, jint deviceId, jlong stream)187{188J9VMThread * thread = (J9VMThread *)env;189190Trc_cuda_streamGetPriority_entry(thread, deviceId, (J9CudaStream)stream);191192PORT_ACCESS_FROM_ENV(env);193194int32_t priority = 0;195int32_t error = j9cuda_streamGetPriority((uint32_t)deviceId, (J9CudaStream)stream, &priority);196197if (0 != error) {198throwCudaException(env, error);199}200201Trc_cuda_streamGetPriority_exit(thread, error, priority);202203return priority;204}205206/**207* Query the completion status of a stream.208*209* Class: com.ibm.cuda.CudaStream210* Method: query211* Signature: (IJ)I212*213* @param[in] env the JNI interface pointer214* @param[in] (unused) the class pointer215* @param[in] deviceId the device identifier216* @param[in] stream the stream217* @return 0 if the stream is complete; else cudaErrorNotReady or another error218*/219jint JNICALL220Java_com_ibm_cuda_CudaStream_query221(JNIEnv * env, jclass, jint deviceId, jlong stream)222{223J9VMThread * thread = (J9VMThread *)env;224225Trc_cuda_streamQuery_entry(thread, deviceId, (J9CudaStream)stream);226227PORT_ACCESS_FROM_ENV(env);228229int32_t error = j9cuda_streamQuery((uint32_t)deviceId, (J9CudaStream)stream);230231Trc_cuda_streamQuery_exit(thread, error);232233return error;234}235236/**237* Synchronize with a stream.238*239* Class: com.ibm.cuda.CudaStream240* Method: synchronize241* Signature: (IJ)V242*243* @param[in] env the JNI interface pointer244* @param[in] (unused) the class pointer245* @param[in] deviceId the device identifier246* @param[in] stream the stream247*/248void JNICALL249Java_com_ibm_cuda_CudaStream_synchronize250(JNIEnv * env, jclass, jint deviceId, jlong stream)251{252J9VMThread * thread = (J9VMThread *)env;253254Trc_cuda_streamSynchronize_entry(thread, deviceId, (J9CudaStream)stream);255256PORT_ACCESS_FROM_ENV(env);257258int32_t error = j9cuda_streamSynchronize((uint32_t)deviceId, (J9CudaStream)stream);259260if (0 != error) {261throwCudaException(env, error);262}263264Trc_cuda_streamSynchronize_exit(thread, error);265}266267/**268* Cause a stream to wait for an event.269*270* Class: com.ibm.cuda.CudaStream271* Method: waitFor272* Signature: (IJJ)V273*274* @param[in] env the JNI interface pointer275* @param[in] (unused) the class pointer276* @param[in] deviceId the device identifier277* @param[in] stream the stream278* @param[in] event the event to wait for279*/280void JNICALL281Java_com_ibm_cuda_CudaStream_waitFor282(JNIEnv * env, jclass, jint deviceId, jlong stream, jlong event)283{284J9VMThread * thread = (J9VMThread *)env;285286Trc_cuda_streamWaitFor_entry(thread, deviceId, (J9CudaStream)stream, (J9CudaEvent)event);287288PORT_ACCESS_FROM_ENV(env);289290int32_t error = j9cuda_streamWaitEvent((uint32_t)deviceId, (J9CudaStream)stream, (J9CudaEvent)event);291292if (0 != error) {293throwCudaException(env, error);294}295296Trc_cuda_streamWaitFor_exit(thread, error);297}298299#endif /* OMR_OPT_CUDA */300301302