Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/jcl/src/openj9.cuda/share/classes/com/ibm/cuda/CudaEvent.java
12927 views
1
/*[INCLUDE-IF Sidecar18-SE]*/
2
/*******************************************************************************
3
* Copyright (c) 2013, 2018 IBM Corp. and others
4
*
5
* This program and the accompanying materials are made available under
6
* the terms of the Eclipse Public License 2.0 which accompanies this
7
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
8
* or the Apache License, Version 2.0 which accompanies this distribution and
9
* is available at https://www.apache.org/licenses/LICENSE-2.0.
10
*
11
* This Source Code may also be made available under the following
12
* Secondary Licenses when the conditions for such availability set
13
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
14
* General Public License, version 2 with the GNU Classpath
15
* Exception [1] and GNU General Public License, version 2 with the
16
* OpenJDK Assembly Exception [2].
17
*
18
* [1] https://www.gnu.org/software/classpath/license.html
19
* [2] http://openjdk.java.net/legal/assembly-exception.html
20
*
21
* 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-exception
22
*******************************************************************************/
23
package com.ibm.cuda;
24
25
import java.util.concurrent.atomic.AtomicLong;
26
27
/**
28
* The {@code CudaEvent} class represents an event that can be queued in a
29
* stream on a CUDA-capable device.
30
* <p>
31
* When no longer required, an event must be {@code close}d.
32
*/
33
public final class CudaEvent implements AutoCloseable {
34
35
/** Default event creation flag. */
36
public static final int FLAG_DEFAULT = 0;
37
38
/** Use blocking synchronization. */
39
public static final int FLAG_BLOCKING_SYNC = 1;
40
41
/** Do not record timing data. */
42
public static final int FLAG_DISABLE_TIMING = 2;
43
44
/** Event is suitable for interprocess use. FLAG_DISABLE_TIMING must be set. */
45
public static final int FLAG_INTERPROCESS = 4;
46
47
private static native long create(int deviceId, int flags)
48
throws CudaException;
49
50
private static native void destroy(int deviceId, long eventHandle)
51
throws CudaException;
52
53
private static native float elapsedTimeSince(long thisHandle,
54
long priorEventHandle) throws CudaException;
55
56
private static native int query(long eventHandle);
57
58
private static native void record(int deviceId, long streamHandle,
59
long eventHandle) throws CudaException;
60
61
private static native void synchronize(long eventHandle)
62
throws CudaException;
63
64
private final int deviceId;
65
66
private final AtomicLong nativeHandle;
67
68
/**
69
* Creates a new event on the specified device with default flags.
70
*
71
* @param device
72
* the specified device
73
* @throws CudaException
74
* if a CUDA exception occurs
75
*/
76
public CudaEvent(CudaDevice device) throws CudaException {
77
this(device, FLAG_DEFAULT);
78
}
79
80
/**
81
* Creates a new event on the specified device with the specified {@code flags}.
82
*
83
* @param device
84
* the specified device
85
* @param flags
86
* the desired flags
87
* @throws CudaException
88
* if a CUDA exception occurs
89
*/
90
public CudaEvent(CudaDevice device, int flags) throws CudaException {
91
super();
92
this.deviceId = device.getDeviceId();
93
this.nativeHandle = new AtomicLong(create(deviceId, flags));
94
}
95
96
/**
97
* Releases resources associated with this event.
98
*
99
* @throws CudaException
100
* if a CUDA exception occurs
101
*/
102
@Override
103
public void close() throws CudaException {
104
long handle = nativeHandle.getAndSet(0);
105
106
if (handle != 0) {
107
destroy(deviceId, handle);
108
}
109
}
110
111
/**
112
* Returns the elapsed time (in milliseconds) relative to the specified
113
* {@code priorEvent}.
114
*
115
* @param priorEvent
116
* the prior event
117
* @return
118
* the elapsed time (in milliseconds) between the occurrence of
119
* the prior event and this event
120
* @throws CudaException
121
* if a CUDA exception occurs
122
* @throws IllegalStateException
123
* if this event or the prior event has been closed (see {@link #close()})
124
*/
125
public float elapsedTimeSince(CudaEvent priorEvent) throws CudaException {
126
return elapsedTimeSince(getHandle(), priorEvent.getHandle());
127
}
128
129
long getHandle() {
130
long handle = nativeHandle.get();
131
132
if (handle == 0) {
133
throw new IllegalStateException();
134
}
135
136
return handle;
137
}
138
139
/**
140
* Queries the state of this event.
141
* The common normally occurring states are:
142
* <ul>
143
* <li>CudaError.Success - event has occurred</li>
144
* <li>CudaError.NotReady - event has not occurred</li>
145
* </ul>
146
*
147
* @return
148
* the state of this event
149
* @throws IllegalStateException
150
* if this event has been closed (see {@link #close()})
151
* @see CudaError
152
*/
153
public int query() {
154
return query(getHandle());
155
}
156
157
/**
158
* Records this event on the default stream of the specified device.
159
*
160
* @param device
161
* the specified device
162
* @throws CudaException
163
* if a CUDA exception occurs
164
* @throws IllegalStateException
165
* if this event has been closed (see {@link #close()})
166
*/
167
public void record(CudaDevice device) throws CudaException {
168
record(device.getDeviceId(), 0, getHandle());
169
}
170
171
/**
172
* Records this event on the specified stream.
173
*
174
* @param stream
175
* the specified stream
176
* @throws CudaException
177
* if a CUDA exception occurs
178
* @throws IllegalStateException
179
* if this event has been closed (see {@link #close()}),
180
* or the stream has been closed (see {@link CudaStream#close()})
181
*/
182
public void record(CudaStream stream) throws CudaException {
183
record(stream.deviceId, stream.getHandle(), getHandle());
184
}
185
186
/**
187
* Synchronizes on this event.
188
* This method blocks until the associated event has occurred.
189
*
190
* @throws CudaException
191
* if a CUDA exception occurs
192
* @throws IllegalStateException
193
* if this event has been closed (see {@link #close()})
194
*/
195
public void synchronize() throws CudaException {
196
synchronize(getHandle());
197
}
198
}
199
200