Path: blob/master/jcl/src/openj9.cuda/share/classes/com/ibm/cuda/CudaEvent.java
12927 views
/*[INCLUDE-IF Sidecar18-SE]*/1/*******************************************************************************2* Copyright (c) 2013, 2018 IBM Corp. and others3*4* This program and the accompanying materials are made available under5* the terms of the Eclipse Public License 2.0 which accompanies this6* distribution and is available at https://www.eclipse.org/legal/epl-2.0/7* or the Apache License, Version 2.0 which accompanies this distribution and8* is available at https://www.apache.org/licenses/LICENSE-2.0.9*10* This Source Code may also be made available under the following11* Secondary Licenses when the conditions for such availability set12* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU13* General Public License, version 2 with the GNU Classpath14* Exception [1] and GNU General Public License, version 2 with the15* OpenJDK Assembly Exception [2].16*17* [1] https://www.gnu.org/software/classpath/license.html18* [2] http://openjdk.java.net/legal/assembly-exception.html19*20* 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-exception21*******************************************************************************/22package com.ibm.cuda;2324import java.util.concurrent.atomic.AtomicLong;2526/**27* The {@code CudaEvent} class represents an event that can be queued in a28* stream on a CUDA-capable device.29* <p>30* When no longer required, an event must be {@code close}d.31*/32public final class CudaEvent implements AutoCloseable {3334/** Default event creation flag. */35public static final int FLAG_DEFAULT = 0;3637/** Use blocking synchronization. */38public static final int FLAG_BLOCKING_SYNC = 1;3940/** Do not record timing data. */41public static final int FLAG_DISABLE_TIMING = 2;4243/** Event is suitable for interprocess use. FLAG_DISABLE_TIMING must be set. */44public static final int FLAG_INTERPROCESS = 4;4546private static native long create(int deviceId, int flags)47throws CudaException;4849private static native void destroy(int deviceId, long eventHandle)50throws CudaException;5152private static native float elapsedTimeSince(long thisHandle,53long priorEventHandle) throws CudaException;5455private static native int query(long eventHandle);5657private static native void record(int deviceId, long streamHandle,58long eventHandle) throws CudaException;5960private static native void synchronize(long eventHandle)61throws CudaException;6263private final int deviceId;6465private final AtomicLong nativeHandle;6667/**68* Creates a new event on the specified device with default flags.69*70* @param device71* the specified device72* @throws CudaException73* if a CUDA exception occurs74*/75public CudaEvent(CudaDevice device) throws CudaException {76this(device, FLAG_DEFAULT);77}7879/**80* Creates a new event on the specified device with the specified {@code flags}.81*82* @param device83* the specified device84* @param flags85* the desired flags86* @throws CudaException87* if a CUDA exception occurs88*/89public CudaEvent(CudaDevice device, int flags) throws CudaException {90super();91this.deviceId = device.getDeviceId();92this.nativeHandle = new AtomicLong(create(deviceId, flags));93}9495/**96* Releases resources associated with this event.97*98* @throws CudaException99* if a CUDA exception occurs100*/101@Override102public void close() throws CudaException {103long handle = nativeHandle.getAndSet(0);104105if (handle != 0) {106destroy(deviceId, handle);107}108}109110/**111* Returns the elapsed time (in milliseconds) relative to the specified112* {@code priorEvent}.113*114* @param priorEvent115* the prior event116* @return117* the elapsed time (in milliseconds) between the occurrence of118* the prior event and this event119* @throws CudaException120* if a CUDA exception occurs121* @throws IllegalStateException122* if this event or the prior event has been closed (see {@link #close()})123*/124public float elapsedTimeSince(CudaEvent priorEvent) throws CudaException {125return elapsedTimeSince(getHandle(), priorEvent.getHandle());126}127128long getHandle() {129long handle = nativeHandle.get();130131if (handle == 0) {132throw new IllegalStateException();133}134135return handle;136}137138/**139* Queries the state of this event.140* The common normally occurring states are:141* <ul>142* <li>CudaError.Success - event has occurred</li>143* <li>CudaError.NotReady - event has not occurred</li>144* </ul>145*146* @return147* the state of this event148* @throws IllegalStateException149* if this event has been closed (see {@link #close()})150* @see CudaError151*/152public int query() {153return query(getHandle());154}155156/**157* Records this event on the default stream of the specified device.158*159* @param device160* the specified device161* @throws CudaException162* if a CUDA exception occurs163* @throws IllegalStateException164* if this event has been closed (see {@link #close()})165*/166public void record(CudaDevice device) throws CudaException {167record(device.getDeviceId(), 0, getHandle());168}169170/**171* Records this event on the specified stream.172*173* @param stream174* the specified stream175* @throws CudaException176* if a CUDA exception occurs177* @throws IllegalStateException178* if this event has been closed (see {@link #close()}),179* or the stream has been closed (see {@link CudaStream#close()})180*/181public void record(CudaStream stream) throws CudaException {182record(stream.deviceId, stream.getHandle(), getHandle());183}184185/**186* Synchronizes on this event.187* This method blocks until the associated event has occurred.188*189* @throws CudaException190* if a CUDA exception occurs191* @throws IllegalStateException192* if this event has been closed (see {@link #close()})193*/194public void synchronize() throws CudaException {195synchronize(getHandle());196}197}198199200