Search code examples
javagpubigdecimal

Simplest way to use GPU in Java


I have a java process with some basic operations with BigDecimal, and it runs millions of times, and I was thinking about using the idle capacity of the server's GPU, in parallel with other String operations on the CPU for better performance. I wonder if there is a generic/simple way to consume this idle GPU for math operations.

I mean generic, because the server can change.

HP server with Matrox G200eh card.

Sample code:

BigDecimal bdTest = new BigDecimal(10);
BigDecimal bdSum = bdTest.add(new BigDecimal(20));
...

Solution

  • You can use Aparapi for GPU computations, but you cannot use BigDecimal.

    Alternatively you can use Apfloat (arbitrary precision library) instead of BigDecimal. Apfloat should be faster but again - you cannot use it with Aparapi.

    Here is an example of multiplying numbers from two arrays with Aparapi:

    final float[] arrayA = new float[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    final float[] arrayB = new float[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    final float[] result = new float[arrayA.length];
    final float[] aparapiResult = new float[arrayA.length];
    
    /* Normal */
    for (int i = 0; i < arrayA.length; i++) {
        result[i] = arrayA[i] * arrayB[i];
    }
    
    /* Aparapi */
    Kernel kernel = new Kernel() {
        @Override
        public void run() {
            int i = getGlobalId();
            aparapiResult[i] = arrayA[i] * arrayB[i];
        }
    };
    Range range = Range.create(aparapiResult.length);
    kernel.execute(range);
    

    Maven dependency:

    <dependency>
        <groupId>com.aparapi</groupId>
        <artifactId>aparapi</artifactId>
        <version>3.0.0</version>
    </dependency>
    

    Repeating that calculations 100 000 times for arrays with 1 000 000 elements, first "normal" snippet takes 1:15 while Aparapi snippet takes 0:24, on my machine.