/*******************************************************************************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_CudaEvent.h"2425/**26* Create an event with the specified flags.27*28* Class: com.ibm.cuda.CudaEvent29* Method: create30* Signature: (II)J31*32* @param[in] env the JNI interface pointer33* @param[in] (unused) the class pointer34* @param[in] deviceId the device identifier35* @param[in] flags the event creation flags36* @return a new event37*/38jlong JNICALL39Java_com_ibm_cuda_CudaEvent_create40(JNIEnv * env, jclass, jint deviceId, jint flags)41{42J9VMThread * thread = (J9VMThread *)env;4344Trc_cuda_eventCreate_entry(thread, deviceId, flags);4546J9CudaEvent event = NULL;47int32_t error = J9CUDA_ERROR_NO_DEVICE;48#ifdef OMR_OPT_CUDA49PORT_ACCESS_FROM_ENV(env);50error = j9cuda_eventCreate((uint32_t)deviceId, (uint32_t)flags, &event);51#endif /* OMR_OPT_CUDA */5253if (0 != error) {54throwCudaException(env, error);55}5657Trc_cuda_eventCreate_exit(thread, event);5859return (jlong)event;60}6162#ifdef OMR_OPT_CUDA6364/**65* Destroy an event.66*67* Class: com.ibm.cuda.CudaEvent68* Method: destroy69* Signature: (IJ)V70*71* @param[in] env the JNI interface pointer72* @param[in] (unused) the class pointer73* @param[in] deviceId the device identifier74* @param[in] event the event75*/76void JNICALL77Java_com_ibm_cuda_CudaEvent_destroy78(JNIEnv * env, jclass, jint deviceId, jlong event)79{80J9VMThread * thread = (J9VMThread *)env;8182Trc_cuda_eventDestroy_entry(thread, deviceId, (J9CudaEvent)event);8384PORT_ACCESS_FROM_ENV(env);8586int32_t error = j9cuda_eventDestroy((uint32_t)deviceId, (J9CudaEvent)event);8788if (0 != error) {89throwCudaException(env, error);90}9192Trc_cuda_eventDestroy_exit(thread, error);93}9495/**96* Return the elapsed time in milliseconds between two events.97*98* Class: com.ibm.cuda.CudaEvent99* Method: elapsedTimeSince100* Signature: (JJ)F101*102* @param[in] env the JNI interface pointer103* @param[in] (unused) the class pointer104* @param[in] event the reference event105* @param[in] priorEvent the earlier event106* @return the elapsed time in milliseconds between the two events107*/108jfloat JNICALL109Java_com_ibm_cuda_CudaEvent_elapsedTimeSince110(JNIEnv * env, jclass, jlong event, jlong priorEvent)111{112J9VMThread * thread = (J9VMThread *)env;113114Trc_cuda_eventElapsedTimeSince_entry(thread, (J9CudaEvent)priorEvent, (J9CudaEvent)event);115116PORT_ACCESS_FROM_ENV(env);117118float elapsed = 0.0f;119int32_t error = j9cuda_eventElapsedTime((J9CudaEvent)priorEvent, (J9CudaEvent)event, &elapsed);120121if (0 != error) {122throwCudaException(env, error);123}124125Trc_cuda_eventElapsedTimeSince_exit(thread, (jint)error, (jfloat)elapsed);126127return (jfloat)elapsed;128}129130/**131* Query the status of an event.132*133* Class: com.ibm.cuda.CudaEvent134* Method: query135* Signature: (J)I136*137* @param[in] env the JNI interface pointer138* @param[in] (unused) the class pointer139* @param[in] event the event140* @return 0 if the event has been recorded; else cudaErrorNotReady or another error141*/142jint JNICALL143Java_com_ibm_cuda_CudaEvent_query144(JNIEnv * env, jclass, jlong event)145{146J9VMThread * thread = (J9VMThread *)env;147148Trc_cuda_eventQuery_entry(thread, (J9CudaEvent)event);149150PORT_ACCESS_FROM_ENV(env);151152int32_t error = j9cuda_eventQuery((J9CudaEvent)event);153154Trc_cuda_eventQuery_exit(thread, error);155156return (jint)error;157}158159/**160* Record an event on a stream or the default stream.161*162* Class: com.ibm.cuda.CudaEvent163* Method: record164* Signature: (IJJ)V165*166* @param[in] env the JNI interface pointer167* @param[in] (unused) the class pointer168* @param[in] deviceId the device identifier169* @param[in] stream the stream, or null for the default stream170* @param[in] event the event to be recorded171*/172void JNICALL173Java_com_ibm_cuda_CudaEvent_record174(JNIEnv * env, jclass, jint deviceId, jlong stream, jlong event)175{176J9VMThread * thread = (J9VMThread *)env;177178Trc_cuda_eventRecord_entry(thread, deviceId, (J9CudaStream)stream, (J9CudaEvent)event);179180PORT_ACCESS_FROM_ENV(env);181182int32_t error = j9cuda_eventRecord((uint32_t)deviceId, (J9CudaEvent)event, (J9CudaStream)stream);183184if (0 != error) {185throwCudaException(env, error);186}187188Trc_cuda_eventRecord_exit(thread);189}190191/**192* Synchronize with an event.193*194* Class: com.ibm.cuda.CudaEvent195* Method: synchronize196* Signature: (J)V197*198* @param[in] env the JNI interface pointer199* @param[in] (unused) the class pointer200* @param[in] event the event to be recorded201*/202void JNICALL203Java_com_ibm_cuda_CudaEvent_synchronize204(JNIEnv * env, jclass, jlong event)205{206J9VMThread * thread = (J9VMThread *)env;207208Trc_cuda_eventSynchronize_entry(thread, (J9CudaEvent)event);209210PORT_ACCESS_FROM_ENV(env);211212int32_t error = j9cuda_eventSynchronize((J9CudaEvent)event);213214if (0 != error) {215throwCudaException(env, error);216}217218Trc_cuda_eventSynchronize_exit(thread, (jint)error);219}220221#endif /* OMR_OPT_CUDA */222223224