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/CudaStream.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.Objects;
26
import java.util.concurrent.atomic.AtomicLong;
27
28
/**
29
* The {@code CudaStream} class represents an independent queue of work for a
30
* specific {@link CudaDevice}.
31
* <p>
32
* When no longer required, a stream must be {@code close}d.
33
*/
34
public final class CudaStream implements AutoCloseable {
35
36
/**
37
* Default stream creation flag.
38
*/
39
public static final int FLAG_DEFAULT = 0;
40
41
/**
42
* Stream creation flag requesting no implicit synchronization with the
43
* default stream.
44
*/
45
public static final int FLAG_NON_BLOCKING = 1;
46
47
private static native long create(int deviceId) throws CudaException;
48
49
private static native long createWithPriority(int deviceId, int flags,
50
int priority) throws CudaException;
51
52
private static native void destroy(int deviceId, long streamHandle)
53
throws CudaException;
54
55
private static native int getFlags(int deviceId, long streamHandle)
56
throws CudaException;
57
58
private static native int getPriority(int deviceId, long streamHandle)
59
throws CudaException;
60
61
private static native int query(int deviceId, long streamHandle);
62
63
private static native void synchronize(int deviceId, long streamHandle)
64
throws CudaException;
65
66
private static native void waitFor(int deviceId, long streamHandle,
67
long eventHandle) throws CudaException;
68
69
final int deviceId;
70
71
private final AtomicLong nativeHandle;
72
73
/**
74
* Creates a new stream on the specified device, with the default flags
75
* and the default priority.
76
*
77
* @param device
78
* the specified device
79
* @throws CudaException
80
* if a CUDA exception occurs
81
*/
82
public CudaStream(CudaDevice device) throws CudaException {
83
super();
84
this.deviceId = device.getDeviceId();
85
this.nativeHandle = new AtomicLong(create(this.deviceId));
86
}
87
88
/**
89
* Creates a new stream on the specified device, with the specified flags
90
* and priority.
91
*
92
* @param device
93
* the specified device
94
* @param flags
95
* the desired flags
96
* @param priority
97
* the desired priority
98
* @throws CudaException
99
* if a CUDA exception occurs
100
*/
101
public CudaStream(CudaDevice device, int flags, int priority)
102
throws CudaException {
103
super();
104
this.deviceId = device.getDeviceId();
105
this.nativeHandle = new AtomicLong( // <br/>
106
createWithPriority(this.deviceId, flags, priority));
107
}
108
109
/**
110
* Enqueues a callback to be run after all previous work on this stream
111
* has been completed.
112
* <p>
113
* Note that the callback will occur on a distinct thread. Further, any
114
* attempts to interact with CUDA devices will fail with a CudaException
115
* with code {@link CudaError#NotPermitted}.
116
*
117
* @param callback
118
* the runnable to be run
119
* @throws CudaException
120
* if a CUDA exception occurs
121
*/
122
public void addCallback(Runnable callback) throws CudaException {
123
Objects.requireNonNull(callback);
124
CudaDevice.addCallback(deviceId, getHandle(), callback);
125
}
126
127
/**
128
* Closes this stream.
129
* Any work queued on this stream will be allowed to complete: this method
130
* does not wait for that work (if any) to complete.
131
* @throws CudaException
132
* if a CUDA exception occurs
133
*/
134
@Override
135
public void close() throws CudaException {
136
long handle = nativeHandle.getAndSet(0);
137
138
if (handle != 0) {
139
destroy(deviceId, handle);
140
}
141
}
142
143
/**
144
* Returns the flags of this stream.
145
* @return
146
* the flags of this stream
147
* @throws CudaException
148
* if a CUDA exception occurs
149
* @throws IllegalStateException
150
* if this stream has been closed (see {@link #close()})
151
*/
152
public int getFlags() throws CudaException {
153
return getFlags(deviceId, getHandle());
154
}
155
156
long getHandle() {
157
long handle = nativeHandle.get();
158
159
if (handle == 0) {
160
throw new IllegalStateException();
161
}
162
163
return handle;
164
}
165
166
/**
167
* Returns the priority of this stream.
168
* @return
169
* the priority of this stream
170
* @throws CudaException
171
* if a CUDA exception occurs
172
* @throws IllegalStateException
173
* if this stream has been closed (see {@link #close()})
174
*/
175
public int getPriority() throws CudaException {
176
return getPriority(deviceId, getHandle());
177
}
178
179
/**
180
* Queries the state of this stream.
181
* The common normally occurring states are:
182
* <ul>
183
* <li>CudaError.Success - stream has no pending work</li>
184
* <li>CudaError.NotReady - stream has pending work</li>
185
* </ul>
186
*
187
* @return
188
* the state of this stream
189
* @throws IllegalStateException
190
* if this stream has been closed (see {@link #close()})
191
*/
192
public int query() {
193
return query(deviceId, getHandle());
194
}
195
196
/**
197
* Synchronizes with this stream.
198
* This method blocks until all work queued on this stream has completed.
199
*
200
* @throws CudaException
201
* if a CUDA exception occurs
202
* @throws IllegalStateException
203
* if this stream has been closed (see {@link #close()})
204
*/
205
public void synchronize() throws CudaException {
206
synchronize(deviceId, getHandle());
207
}
208
209
/**
210
* Makes all future work submitted to this stream wait for the specified
211
* event to occur.
212
*
213
* @param event
214
* the specified event
215
* @throws CudaException
216
* if a CUDA exception occurs
217
* @throws IllegalStateException
218
* if this stream has been closed (see {@link #close()}),
219
* or the event has been closed (see {@link CudaEvent#close()})
220
*/
221
public void waitFor(CudaEvent event) throws CudaException {
222
waitFor(deviceId, getHandle(), event.getHandle());
223
}
224
}
225
226