Path: blob/master/jcl/src/openj9.gpu/share/classes/com/ibm/gpu/Maths.java
12558 views
/*[INCLUDE-IF Sidecar18-SE]*/1/*******************************************************************************2* Copyright (c) 2014, 2021 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.gpu;2324import java.util.Objects;2526/**27* This class is used to perform sorting operations of28* primitive arrays of type int, long, float, double29* on any connected CUDA GPU. A successful sort operation30* results in the array being sorted in ascending order.31*/32/*[IF JAVA_SPEC_VERSION >= 16]*/33public final class Maths {3435private Maths() {36super();37}38/*[ELSE] JAVA_SPEC_VERSION >= 1639public class Maths {40/*[ENDIF] JAVA_SPEC_VERSION >= 16*/4142private static int getDefaultDevice() {43return CUDAManager.instanceInternal().getDefaultDevice();44}4546/**47* Sort the given array of doubles into ascending order, using the default CUDA device.48*49* @param array50* the array that will be sorted51* @throws GPUConfigurationException52* if an issue has occurred with the CUDA environment53* @throws GPUSortException54* if any of the following happens:55* <ul>56* <li>the device is not available</li>57* <li>insufficient device memory is available</li>58* <li>an error occurs transferring the data to or from the device</li>59* <li>a device execution error occurs</li>60* </ul>61*/62public static void sortArray(double[] array)63throws GPUConfigurationException, GPUSortException {64Objects.requireNonNull(array);65SortNetwork.sortArray(getDefaultDevice(), array, 0, array.length);66}6768/**69* Sort the specified range of the array of doubles into ascending order, using the default CUDA device.70*71* @param array72* the array that will be sorted73* @param fromIndex74* the range starting index (inclusive)75* @param toIndex76* the range ending index (exclusive)77* @throws GPUConfigurationException78* if an issue has occurred with the CUDA environment79* @throws GPUSortException80* if any of the following happens:81* <ul>82* <li>the device is not available</li>83* <li>insufficient device memory is available</li>84* <li>an error occurs transferring the data to or from the device</li>85* <li>a device execution error occurs</li>86* </ul>87*/88public static void sortArray(double[] array, int fromIndex, int toIndex)89throws GPUConfigurationException, GPUSortException {90Objects.requireNonNull(array);91SortNetwork.sortArray(getDefaultDevice(), array, fromIndex, toIndex);92}9394/**95* Sort the given array of floats into ascending order, using the default CUDA device.96*97* @param array98* the array that will be sorted99* @throws GPUConfigurationException100* if an issue has occurred with the CUDA environment101* @throws GPUSortException102* if any of the following happens:103* <ul>104* <li>the device is not available</li>105* <li>insufficient device memory is available</li>106* <li>an error occurs transferring the data to or from the device</li>107* <li>a device execution error occurs</li>108* </ul>109*/110public static void sortArray(float[] array) throws GPUSortException,111GPUConfigurationException {112Objects.requireNonNull(array);113SortNetwork.sortArray(getDefaultDevice(), array, 0, array.length);114}115116/**117* Sort the specified range of the array of floats into ascending order, using the default CUDA device.118*119* @param array120* the array that will be sorted121* @param fromIndex122* the range starting index (inclusive)123* @param toIndex124* the range ending index (exclusive)125* @throws GPUConfigurationException126* if an issue has occurred with the CUDA environment127* @throws GPUSortException128* if any of the following happens:129* <ul>130* <li>the device is not available</li>131* <li>insufficient device memory is available</li>132* <li>an error occurs transferring the data to or from the device</li>133* <li>a device execution error occurs</li>134* </ul>135*/136public static void sortArray(float[] array, int fromIndex, int toIndex)137throws GPUConfigurationException, GPUSortException {138Objects.requireNonNull(array);139sortArray(getDefaultDevice(), array, fromIndex, toIndex);140}141142/**143* Sort the given array of doubles into ascending order, using the specified CUDA device.144*145* @param deviceId146* the CUDA device to be used147* @param array148* the array that will be sorted149* @throws GPUConfigurationException150* if an issue has occurred with the CUDA environment151* @throws GPUSortException152* if any of the following happens:153* <ul>154* <li>the device is not available</li>155* <li>insufficient device memory is available</li>156* <li>an error occurs transferring the data to or from the device</li>157* <li>a device execution error occurs</li>158* </ul>159*/160public static void sortArray(int deviceId, double[] array)161throws GPUConfigurationException, GPUSortException {162Objects.requireNonNull(array);163SortNetwork.sortArray(deviceId, array, 0, array.length);164}165166/**167* Sort the specified range of the array of doubles into ascending order, using the specified CUDA device.168*169* @param deviceId170* the CUDA device to be used171* @param array172* the array that will be sorted173* @param fromIndex174* the range starting index (inclusive)175* @param toIndex176* the range ending index (exclusive)177* @throws GPUConfigurationException178* if an issue has occurred with the CUDA environment179* @throws GPUSortException180* if any of the following happens:181* <ul>182* <li>the device is not available</li>183* <li>insufficient device memory is available</li>184* <li>an error occurs transferring the data to or from the device</li>185* <li>a device execution error occurs</li>186* </ul>187*/188public static void sortArray(int deviceId, double[] array, int fromIndex,189int toIndex) throws GPUConfigurationException, GPUSortException {190Objects.requireNonNull(array);191SortNetwork.sortArray(deviceId, array, fromIndex, toIndex);192}193194/**195* Sort the given array of floats into ascending order, using the specified CUDA device.196*197* @param deviceId198* the CUDA device to be used199* @param array200* the array that will be sorted201* @throws GPUConfigurationException202* if an issue has occurred with the CUDA environment203* @throws GPUSortException204* if any of the following happens:205* <ul>206* <li>the device is not available</li>207* <li>insufficient device memory is available</li>208* <li>an error occurs transferring the data to or from the device</li>209* <li>a device execution error occurs</li>210* </ul>211*/212public static void sortArray(int deviceId, float[] array)213throws GPUConfigurationException, GPUSortException {214Objects.requireNonNull(array);215SortNetwork.sortArray(deviceId, array, 0, array.length);216}217218/**219* Sort the specified range of the array of floats into ascending order, using the specified CUDA device.220*221* @param deviceId222* the CUDA device to be used223* @param array224* the array that will be sorted225* @param fromIndex226* the range starting index (inclusive)227* @param toIndex228* the range ending index (exclusive)229* @throws GPUConfigurationException230* if an issue has occurred with the CUDA environment231* @throws GPUSortException232* if any of the following happens:233* <ul>234* <li>the device is not available</li>235* <li>insufficient device memory is available</li>236* <li>an error occurs transferring the data to or from the device</li>237* <li>a device execution error occurs</li>238* </ul>239*/240public static void sortArray(int deviceId, float[] array, int fromIndex,241int toIndex) throws GPUConfigurationException, GPUSortException {242Objects.requireNonNull(array);243SortNetwork.sortArray(deviceId, array, fromIndex, toIndex);244}245246/**247* Sort the given array of integers into ascending order, using the specified CUDA device.248*249* @param deviceId250* the CUDA device to be used251* @param array252* the array that will be sorted253* @throws GPUConfigurationException254* if an issue has occurred with the CUDA environment255* @throws GPUSortException256* if any of the following happens:257* <ul>258* <li>the device is not available</li>259* <li>insufficient device memory is available</li>260* <li>an error occurs transferring the data to or from the device</li>261* <li>a device execution error occurs</li>262* </ul>263*/264public static void sortArray(int deviceId, int[] array)265throws GPUConfigurationException, GPUSortException {266Objects.requireNonNull(array);267SortNetwork.sortArray(deviceId, array, 0, array.length);268}269270/**271* Sort the specified range of the array of integers into ascending order, using the specified CUDA device.272*273* @param deviceId274* the CUDA device to be used275* @param array276* the array that will be sorted277* @param fromIndex278* the range starting index (inclusive)279* @param toIndex280* the range ending index (exclusive)281* @throws GPUConfigurationException282* if an issue has occurred with the CUDA environment283* @throws GPUSortException284* if any of the following happens:285* <ul>286* <li>the device is not available</li>287* <li>insufficient device memory is available</li>288* <li>an error occurs transferring the data to or from the device</li>289* <li>a device execution error occurs</li>290* </ul>291*/292public static void sortArray(int deviceId, int[] array, int fromIndex,293int toIndex) throws GPUConfigurationException, GPUSortException {294Objects.requireNonNull(array);295SortNetwork.sortArray(deviceId, array, fromIndex, toIndex);296}297298/**299* Sort the given array of longs into ascending order, using the specified CUDA device.300*301* @param deviceId302* the CUDA device to be used303* @param array304* the array that will be sorted305* @throws GPUConfigurationException306* if an issue has occurred with the CUDA environment307* @throws GPUSortException308* if any of the following happens:309* <ul>310* <li>the device is not available</li>311* <li>insufficient device memory is available</li>312* <li>an error occurs transferring the data to or from the device</li>313* <li>a device execution error occurs</li>314* </ul>315*/316public static void sortArray(int deviceId, long[] array)317throws GPUConfigurationException, GPUSortException {318Objects.requireNonNull(array);319SortNetwork.sortArray(deviceId, array, 0, array.length);320}321322/**323* Sort the specified range of the array of longs into ascending order, using the specified CUDA device.324*325* @param deviceId326* the CUDA device to be used327* @param array328* the array that will be sorted329* @param fromIndex330* the range starting index (inclusive)331* @param toIndex332* the range ending index (exclusive)333* @throws GPUConfigurationException334* if an issue has occurred with the CUDA environment335* @throws GPUSortException336* if any of the following happens:337* <ul>338* <li>the device is not available</li>339* <li>insufficient device memory is available</li>340* <li>an error occurs transferring the data to or from the device</li>341* <li>a device execution error occurs</li>342* </ul>343*/344public static void sortArray(int deviceId, long[] array, int fromIndex,345int toIndex) throws GPUConfigurationException, GPUSortException {346Objects.requireNonNull(array);347SortNetwork.sortArray(deviceId, array, fromIndex, toIndex);348}349350/**351* Sort the given array of integers into ascending order, using the default CUDA device.352*353* @param array354* the array that will be sorted355* @throws GPUConfigurationException356* if an issue has occurred with the CUDA environment357* @throws GPUSortException358* if any of the following happens:359* <ul>360* <li>the device is not available</li>361* <li>insufficient device memory is available</li>362* <li>an error occurs transferring the data to or from the device</li>363* <li>a device execution error occurs</li>364* </ul>365*/366public static void sortArray(int[] array) throws GPUConfigurationException,367GPUSortException {368Objects.requireNonNull(array);369SortNetwork.sortArray(getDefaultDevice(), array, 0, array.length);370}371372/**373* Sort the specified range of the array of integers into ascending order, using the default CUDA device.374*375* @param array376* the array that will be sorted377* @param fromIndex378* the range starting index (inclusive)379* @param toIndex380* the range ending index (exclusive)381* @throws GPUConfigurationException382* if an issue has occurred with the CUDA environment383* @throws GPUSortException384* if any of the following happens:385* <ul>386* <li>the device is not available</li>387* <li>insufficient device memory is available</li>388* <li>an error occurs transferring the data to or from the device</li>389* <li>a device execution error occurs</li>390* </ul>391*/392public static void sortArray(int[] array, int fromIndex, int toIndex)393throws GPUConfigurationException, GPUSortException {394Objects.requireNonNull(array);395SortNetwork.sortArray(getDefaultDevice(), array, fromIndex, toIndex);396}397398/**399* Sort the given array of longs into ascending order, using the default CUDA device.400*401* @param array402* the array that will be sorted403* @throws GPUConfigurationException404* if an issue has occurred with the CUDA environment405* @throws GPUSortException406* if any of the following happens:407* <ul>408* <li>the device is not available</li>409* <li>insufficient device memory is available</li>410* <li>an error occurs transferring the data to or from the device</li>411* <li>a device execution error occurs</li>412* </ul>413*/414public static void sortArray(long[] array)415throws GPUConfigurationException, GPUSortException {416Objects.requireNonNull(array);417SortNetwork.sortArray(getDefaultDevice(), array, 0, array.length);418}419420/**421* Sort the specified range of the array of longs into ascending order, using the default CUDA device.422*423* @param array424* the array that will be sorted425* @param fromIndex426* the range starting index (inclusive)427* @param toIndex428* the range ending index (exclusive)429* @throws GPUConfigurationException430* if an issue has occurred with the CUDA environment431* @throws GPUSortException432* if any of the following happens:433* <ul>434* <li>the device is not available</li>435* <li>insufficient device memory is available</li>436* <li>an error occurs transferring the data to or from the device</li>437* <li>a device execution error occurs</li>438* </ul>439*/440public static void sortArray(long[] array, int fromIndex, int toIndex)441throws GPUConfigurationException, GPUSortException {442Objects.requireNonNull(array);443SortNetwork.sortArray(getDefaultDevice(), array, fromIndex, toIndex);444}445446}447448449