Experiments with Allocation on Sumatra
In order to widen the types of lambda expressions that could be offloaded to the GPU, we wanted to be able to handle lambdas that used object allocation. The following describes some experiments with allocation using the HSAIL Backend to graal.
Note: As is true of many modern compilers, graal can avoid the actual allocation when it can use escape analysis to prove that the allocated objects do not escape. Here's an example junit test where graal can successfully use escape analysis.
But we also wanted to handle lambdas where allocated objects really did escape. Here is a simple example where we start with an array of longs and we want to produce an array of Strings, one for each long.
package simplealloc;
import java.util.stream.IntStream;
public class SimpleAlloc {
public static void main(String[] args) {
final int length = 20;
long[] input = new long[length];
String[] output = new String[length];
Random rand = new Random();
// Initialize the input array - not offloaded
IntStream.range(0, length).parallel().forEach(p -> {
input[p] = rand.nextLong();
});
// call toString on each input element - this is offloadable
IntStream.range(0, length).parallel().forEach(p -> {
output[p] = Long.toString(input[p]);
});
// Print results - not offloadable since it is
// calling native code etc.
IntStream.range(0, length).forEach(p -> {
System.out.println(output[p]);
});
}
}