Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/jcl/src/openj9.gpu/share/classes/com/ibm/gpu/Maths.java
12558 views
1
/*[INCLUDE-IF Sidecar18-SE]*/
2
/*******************************************************************************
3
* Copyright (c) 2014, 2021 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.gpu;
24
25
import java.util.Objects;
26
27
/**
28
* This class is used to perform sorting operations of
29
* primitive arrays of type int, long, float, double
30
* on any connected CUDA GPU. A successful sort operation
31
* results in the array being sorted in ascending order.
32
*/
33
/*[IF JAVA_SPEC_VERSION >= 16]*/
34
public final class Maths {
35
36
private Maths() {
37
super();
38
}
39
/*[ELSE] JAVA_SPEC_VERSION >= 16
40
public class Maths {
41
/*[ENDIF] JAVA_SPEC_VERSION >= 16*/
42
43
private static int getDefaultDevice() {
44
return CUDAManager.instanceInternal().getDefaultDevice();
45
}
46
47
/**
48
* Sort the given array of doubles into ascending order, using the default CUDA device.
49
*
50
* @param array
51
* the array that will be sorted
52
* @throws GPUConfigurationException
53
* if an issue has occurred with the CUDA environment
54
* @throws GPUSortException
55
* if any of the following happens:
56
* <ul>
57
* <li>the device is not available</li>
58
* <li>insufficient device memory is available</li>
59
* <li>an error occurs transferring the data to or from the device</li>
60
* <li>a device execution error occurs</li>
61
* </ul>
62
*/
63
public static void sortArray(double[] array)
64
throws GPUConfigurationException, GPUSortException {
65
Objects.requireNonNull(array);
66
SortNetwork.sortArray(getDefaultDevice(), array, 0, array.length);
67
}
68
69
/**
70
* Sort the specified range of the array of doubles into ascending order, using the default CUDA device.
71
*
72
* @param array
73
* the array that will be sorted
74
* @param fromIndex
75
* the range starting index (inclusive)
76
* @param toIndex
77
* the range ending index (exclusive)
78
* @throws GPUConfigurationException
79
* if an issue has occurred with the CUDA environment
80
* @throws GPUSortException
81
* if any of the following happens:
82
* <ul>
83
* <li>the device is not available</li>
84
* <li>insufficient device memory is available</li>
85
* <li>an error occurs transferring the data to or from the device</li>
86
* <li>a device execution error occurs</li>
87
* </ul>
88
*/
89
public static void sortArray(double[] array, int fromIndex, int toIndex)
90
throws GPUConfigurationException, GPUSortException {
91
Objects.requireNonNull(array);
92
SortNetwork.sortArray(getDefaultDevice(), array, fromIndex, toIndex);
93
}
94
95
/**
96
* Sort the given array of floats into ascending order, using the default CUDA device.
97
*
98
* @param array
99
* the array that will be sorted
100
* @throws GPUConfigurationException
101
* if an issue has occurred with the CUDA environment
102
* @throws GPUSortException
103
* if any of the following happens:
104
* <ul>
105
* <li>the device is not available</li>
106
* <li>insufficient device memory is available</li>
107
* <li>an error occurs transferring the data to or from the device</li>
108
* <li>a device execution error occurs</li>
109
* </ul>
110
*/
111
public static void sortArray(float[] array) throws GPUSortException,
112
GPUConfigurationException {
113
Objects.requireNonNull(array);
114
SortNetwork.sortArray(getDefaultDevice(), array, 0, array.length);
115
}
116
117
/**
118
* Sort the specified range of the array of floats into ascending order, using the default CUDA device.
119
*
120
* @param array
121
* the array that will be sorted
122
* @param fromIndex
123
* the range starting index (inclusive)
124
* @param toIndex
125
* the range ending index (exclusive)
126
* @throws GPUConfigurationException
127
* if an issue has occurred with the CUDA environment
128
* @throws GPUSortException
129
* if any of the following happens:
130
* <ul>
131
* <li>the device is not available</li>
132
* <li>insufficient device memory is available</li>
133
* <li>an error occurs transferring the data to or from the device</li>
134
* <li>a device execution error occurs</li>
135
* </ul>
136
*/
137
public static void sortArray(float[] array, int fromIndex, int toIndex)
138
throws GPUConfigurationException, GPUSortException {
139
Objects.requireNonNull(array);
140
sortArray(getDefaultDevice(), array, fromIndex, toIndex);
141
}
142
143
/**
144
* Sort the given array of doubles into ascending order, using the specified CUDA device.
145
*
146
* @param deviceId
147
* the CUDA device to be used
148
* @param array
149
* the array that will be sorted
150
* @throws GPUConfigurationException
151
* if an issue has occurred with the CUDA environment
152
* @throws GPUSortException
153
* if any of the following happens:
154
* <ul>
155
* <li>the device is not available</li>
156
* <li>insufficient device memory is available</li>
157
* <li>an error occurs transferring the data to or from the device</li>
158
* <li>a device execution error occurs</li>
159
* </ul>
160
*/
161
public static void sortArray(int deviceId, double[] array)
162
throws GPUConfigurationException, GPUSortException {
163
Objects.requireNonNull(array);
164
SortNetwork.sortArray(deviceId, array, 0, array.length);
165
}
166
167
/**
168
* Sort the specified range of the array of doubles into ascending order, using the specified CUDA device.
169
*
170
* @param deviceId
171
* the CUDA device to be used
172
* @param array
173
* the array that will be sorted
174
* @param fromIndex
175
* the range starting index (inclusive)
176
* @param toIndex
177
* the range ending index (exclusive)
178
* @throws GPUConfigurationException
179
* if an issue has occurred with the CUDA environment
180
* @throws GPUSortException
181
* if any of the following happens:
182
* <ul>
183
* <li>the device is not available</li>
184
* <li>insufficient device memory is available</li>
185
* <li>an error occurs transferring the data to or from the device</li>
186
* <li>a device execution error occurs</li>
187
* </ul>
188
*/
189
public static void sortArray(int deviceId, double[] array, int fromIndex,
190
int toIndex) throws GPUConfigurationException, GPUSortException {
191
Objects.requireNonNull(array);
192
SortNetwork.sortArray(deviceId, array, fromIndex, toIndex);
193
}
194
195
/**
196
* Sort the given array of floats into ascending order, using the specified CUDA device.
197
*
198
* @param deviceId
199
* the CUDA device to be used
200
* @param array
201
* the array that will be sorted
202
* @throws GPUConfigurationException
203
* if an issue has occurred with the CUDA environment
204
* @throws GPUSortException
205
* if any of the following happens:
206
* <ul>
207
* <li>the device is not available</li>
208
* <li>insufficient device memory is available</li>
209
* <li>an error occurs transferring the data to or from the device</li>
210
* <li>a device execution error occurs</li>
211
* </ul>
212
*/
213
public static void sortArray(int deviceId, float[] array)
214
throws GPUConfigurationException, GPUSortException {
215
Objects.requireNonNull(array);
216
SortNetwork.sortArray(deviceId, array, 0, array.length);
217
}
218
219
/**
220
* Sort the specified range of the array of floats into ascending order, using the specified CUDA device.
221
*
222
* @param deviceId
223
* the CUDA device to be used
224
* @param array
225
* the array that will be sorted
226
* @param fromIndex
227
* the range starting index (inclusive)
228
* @param toIndex
229
* the range ending index (exclusive)
230
* @throws GPUConfigurationException
231
* if an issue has occurred with the CUDA environment
232
* @throws GPUSortException
233
* if any of the following happens:
234
* <ul>
235
* <li>the device is not available</li>
236
* <li>insufficient device memory is available</li>
237
* <li>an error occurs transferring the data to or from the device</li>
238
* <li>a device execution error occurs</li>
239
* </ul>
240
*/
241
public static void sortArray(int deviceId, float[] array, int fromIndex,
242
int toIndex) throws GPUConfigurationException, GPUSortException {
243
Objects.requireNonNull(array);
244
SortNetwork.sortArray(deviceId, array, fromIndex, toIndex);
245
}
246
247
/**
248
* Sort the given array of integers into ascending order, using the specified CUDA device.
249
*
250
* @param deviceId
251
* the CUDA device to be used
252
* @param array
253
* the array that will be sorted
254
* @throws GPUConfigurationException
255
* if an issue has occurred with the CUDA environment
256
* @throws GPUSortException
257
* if any of the following happens:
258
* <ul>
259
* <li>the device is not available</li>
260
* <li>insufficient device memory is available</li>
261
* <li>an error occurs transferring the data to or from the device</li>
262
* <li>a device execution error occurs</li>
263
* </ul>
264
*/
265
public static void sortArray(int deviceId, int[] array)
266
throws GPUConfigurationException, GPUSortException {
267
Objects.requireNonNull(array);
268
SortNetwork.sortArray(deviceId, array, 0, array.length);
269
}
270
271
/**
272
* Sort the specified range of the array of integers into ascending order, using the specified CUDA device.
273
*
274
* @param deviceId
275
* the CUDA device to be used
276
* @param array
277
* the array that will be sorted
278
* @param fromIndex
279
* the range starting index (inclusive)
280
* @param toIndex
281
* the range ending index (exclusive)
282
* @throws GPUConfigurationException
283
* if an issue has occurred with the CUDA environment
284
* @throws GPUSortException
285
* if any of the following happens:
286
* <ul>
287
* <li>the device is not available</li>
288
* <li>insufficient device memory is available</li>
289
* <li>an error occurs transferring the data to or from the device</li>
290
* <li>a device execution error occurs</li>
291
* </ul>
292
*/
293
public static void sortArray(int deviceId, int[] array, int fromIndex,
294
int toIndex) throws GPUConfigurationException, GPUSortException {
295
Objects.requireNonNull(array);
296
SortNetwork.sortArray(deviceId, array, fromIndex, toIndex);
297
}
298
299
/**
300
* Sort the given array of longs into ascending order, using the specified CUDA device.
301
*
302
* @param deviceId
303
* the CUDA device to be used
304
* @param array
305
* the array that will be sorted
306
* @throws GPUConfigurationException
307
* if an issue has occurred with the CUDA environment
308
* @throws GPUSortException
309
* if any of the following happens:
310
* <ul>
311
* <li>the device is not available</li>
312
* <li>insufficient device memory is available</li>
313
* <li>an error occurs transferring the data to or from the device</li>
314
* <li>a device execution error occurs</li>
315
* </ul>
316
*/
317
public static void sortArray(int deviceId, long[] array)
318
throws GPUConfigurationException, GPUSortException {
319
Objects.requireNonNull(array);
320
SortNetwork.sortArray(deviceId, array, 0, array.length);
321
}
322
323
/**
324
* Sort the specified range of the array of longs into ascending order, using the specified CUDA device.
325
*
326
* @param deviceId
327
* the CUDA device to be used
328
* @param array
329
* the array that will be sorted
330
* @param fromIndex
331
* the range starting index (inclusive)
332
* @param toIndex
333
* the range ending index (exclusive)
334
* @throws GPUConfigurationException
335
* if an issue has occurred with the CUDA environment
336
* @throws GPUSortException
337
* if any of the following happens:
338
* <ul>
339
* <li>the device is not available</li>
340
* <li>insufficient device memory is available</li>
341
* <li>an error occurs transferring the data to or from the device</li>
342
* <li>a device execution error occurs</li>
343
* </ul>
344
*/
345
public static void sortArray(int deviceId, long[] array, int fromIndex,
346
int toIndex) throws GPUConfigurationException, GPUSortException {
347
Objects.requireNonNull(array);
348
SortNetwork.sortArray(deviceId, array, fromIndex, toIndex);
349
}
350
351
/**
352
* Sort the given array of integers into ascending order, using the default CUDA device.
353
*
354
* @param array
355
* the array that will be sorted
356
* @throws GPUConfigurationException
357
* if an issue has occurred with the CUDA environment
358
* @throws GPUSortException
359
* if any of the following happens:
360
* <ul>
361
* <li>the device is not available</li>
362
* <li>insufficient device memory is available</li>
363
* <li>an error occurs transferring the data to or from the device</li>
364
* <li>a device execution error occurs</li>
365
* </ul>
366
*/
367
public static void sortArray(int[] array) throws GPUConfigurationException,
368
GPUSortException {
369
Objects.requireNonNull(array);
370
SortNetwork.sortArray(getDefaultDevice(), array, 0, array.length);
371
}
372
373
/**
374
* Sort the specified range of the array of integers into ascending order, using the default CUDA device.
375
*
376
* @param array
377
* the array that will be sorted
378
* @param fromIndex
379
* the range starting index (inclusive)
380
* @param toIndex
381
* the range ending index (exclusive)
382
* @throws GPUConfigurationException
383
* if an issue has occurred with the CUDA environment
384
* @throws GPUSortException
385
* if any of the following happens:
386
* <ul>
387
* <li>the device is not available</li>
388
* <li>insufficient device memory is available</li>
389
* <li>an error occurs transferring the data to or from the device</li>
390
* <li>a device execution error occurs</li>
391
* </ul>
392
*/
393
public static void sortArray(int[] array, int fromIndex, int toIndex)
394
throws GPUConfigurationException, GPUSortException {
395
Objects.requireNonNull(array);
396
SortNetwork.sortArray(getDefaultDevice(), array, fromIndex, toIndex);
397
}
398
399
/**
400
* Sort the given array of longs into ascending order, using the default CUDA device.
401
*
402
* @param array
403
* the array that will be sorted
404
* @throws GPUConfigurationException
405
* if an issue has occurred with the CUDA environment
406
* @throws GPUSortException
407
* if any of the following happens:
408
* <ul>
409
* <li>the device is not available</li>
410
* <li>insufficient device memory is available</li>
411
* <li>an error occurs transferring the data to or from the device</li>
412
* <li>a device execution error occurs</li>
413
* </ul>
414
*/
415
public static void sortArray(long[] array)
416
throws GPUConfigurationException, GPUSortException {
417
Objects.requireNonNull(array);
418
SortNetwork.sortArray(getDefaultDevice(), array, 0, array.length);
419
}
420
421
/**
422
* Sort the specified range of the array of longs into ascending order, using the default CUDA device.
423
*
424
* @param array
425
* the array that will be sorted
426
* @param fromIndex
427
* the range starting index (inclusive)
428
* @param toIndex
429
* the range ending index (exclusive)
430
* @throws GPUConfigurationException
431
* if an issue has occurred with the CUDA environment
432
* @throws GPUSortException
433
* if any of the following happens:
434
* <ul>
435
* <li>the device is not available</li>
436
* <li>insufficient device memory is available</li>
437
* <li>an error occurs transferring the data to or from the device</li>
438
* <li>a device execution error occurs</li>
439
* </ul>
440
*/
441
public static void sortArray(long[] array, int fromIndex, int toIndex)
442
throws GPUConfigurationException, GPUSortException {
443
Objects.requireNonNull(array);
444
SortNetwork.sortArray(getDefaultDevice(), array, fromIndex, toIndex);
445
}
446
447
}
448
449