The com.ibm.gpu application programming interface (Linux®, Windows only)
You can use the com.ibm.gpu application programming interface (API) to develop applications that sort arrays of primitive types (int, long, float, double) on the GPU.
Parallel processing of sort operations on data arrays can improve performance, provided the array is large enough to justify the overhead of moving the data. Before you decide to offload sort operations to the GPU, you must ensure that there is a performance benefit in doing so. Testing indicates that moving the data from the CPU to the GPU is cost neutral at 20,000 entries, whereas for larger arrays you see a reduced sort time when you use the GPU rather than the CPU.
Coding a sort operation on any GPU device
The following code sample shows a section of code that offloads the sort operation of a simple array to a GPU.
import com.ibm.gpu.GPUConfigurationException;
import com.ibm.gpu.GPUSortException;
import com.ibm.gpu.Maths;
public class Sample3 {
public static void main(String[] args) {
int[] toSort = { 5, 17, 12, 32, 2, 6, 3, 20 };
try {
Maths.sortArray(toSort);
} catch (GPUSortException e) {
e.printStackTrace();
} catch (GPUConfigurationException e) {
e.printStackTrace();
}
}
}
Coding a sort operation on any GPU device with verbose output enabled
The following code sample shows the same sort operation, but enables verbose output to STDOUT. Enabling verbose output can also be specified on the command line by using a system property. For more information, see -Dcom.ibm.gpu.verbose (Linux, Windows only).
import com.ibm.gpu.GPUConfigurationException;
import com.ibm.gpu.GPUSortException;
import com.ibm.gpu.CUDAManager;
import com.ibm.gpu.Maths;
public class Sample2 {
public static void main(String[] args) {
try {
CUDAManager.getInstance().setVerboseGPU(true);
} catch (GPUConfigurationException e1) {
e1.printStackTrace();
}
int[] toSort = { 5, 17, 12, 32, 2, 6, 3, 20 };
try {
Maths.sortArray(toSort);
} catch (GPUSortException e) {
e.printStackTrace();
} catch (GPUConfigurationException e) {
e.printStackTrace();
}
}
}
Coding a sort operation on a specific GPU device
In the
following code sample, the sort operation is offloaded to device identifier
0
, which is the first GPU device in the system. You can determine the device
identifier programmatically by using getDeviceID(), or by specifying
nvidia-smi on the command line of the system that runs the Java™ application.
import com.ibm.gpu.*;
public class Sample1 {
public static void main(String[] args) {
int[] toSort = { 5, 17, 12, 32, 2, 6, 3, 20 };
try {
Maths.sortArray(0, toSort);
} catch (GPUSortException e) {
e.printStackTrace();
} catch (GPUConfigurationException e) {
e.printStackTrace(); }
}
}
For more information, see the com.ibm.gpu application
programming reference.