Nodes in Graal: ParameterNode
Gilles Duboscq
duboscq at ssw.jku.at
Wed Apr 16 14:46:38 UTC 2014
Hello Juan,
I don't think you should try to modify the argument at the start of
the compilation, i would rather do the transformation at the end of
the compilation.
In order to do your transformation, you could do something similar to
what was done by AMD for the Sumatra HSAIL backend:
You have a method such as:
int[] apply(int[] x, int[]y, IntBinaryOperator op)
that can be implemented with
int[] apply(int[] x, int[]y, IntBinaryOperator op) {
assert x.length == y.length;
int[] result = new int[x.length];
for (int i = 0; i < x.length; i++) {
result[i] = op.applyAsInt(x[i], y[i]);
}
return result;
}
However, it can also be implemented as:
int[] apply(int[] x, int[]y, IntBinaryOperator op) {
assert x.length == y.length;
ResolvedJavaType type = meta.lookupJavaType(op.getClass());
ResolvedJavaMethod concrete = type.resolveMethod(applyAsIntMethod);
Kernel k = openclBackend.compileAndInstallKernel(concrete);
k.execute(x.length, op, x, y)
}
This means you are actually compiling a method with the int
IntBinaryOperator.applyAsInt(int, int) signature.
Maybe other can comment on how this is achieved in HSAIL but i suppose
this int[] -> int transform is done during the prolog/epilog which is
emitted for argument handling.
You can know which arguments should actually be pulled from arrays an
add some code to extract them.
For example with:
int[] x = ...
int[] y = ...
apply(x, y, (a, b) -> a + b);
You would end up compiling something like:
int applyAsInt(int a, int b) {
return a +b;
}
which in OpenCL probably will look very similar:
ret = a + b;
(Notice how the ReturnNode is lowered to a variable assignment rather
than an actuall return)
and you can emit the right prolog and epilog code:
__kernel void applyAsInt (__global const int* a_array,
__global const int* b_array,
__global int* res_array)
{
const int gid = get_global_id(0);
int a = a_array[gid];
int b = b_array[gid];
ret = a + b;
res_array[gid] = ret;
}
This would typically be done in the
com.oracle.graal.compiler.target.Backend.emitCode implementation. For
example for HSAIL in
com.oracle.graal.hotspot.hsail.HSAILHotSpotBackend.emitCode.
-Gilles
On Wed, Mar 19, 2014 at 3:16 PM, Juan Jose Fumero <juan.fumero at ed.ac.uk> wrote:
> Hello,
> Thanks Gilles for your explanation. As you said, map in stream.util is
> IntUnaryOperator. What I am using now is BiFunction<T,U,R>. Maybe I
> should change to a specific one as IntBinaryOperator.
>
> More specifically I am trying is this (this is an example):
>
> // Vector Addition
> Stream<Integer,Integer> stream = new Stream<>();
> stream.map((x,y) -> x + y).run(input);
>
> This is now running in CPU with my library. This map does not have side
> effects and each operation returns a new Stream. When the run is called,
> the pipeline is executed. What I am trying is to generate OpenCL code
> from the map lambda expression, but I need to "transform" or replace
> Parameters x and y to Arrays for example.
>
> Is there other ways available in Graal?
>
> Thanks
> Juanjo
>
> On Wed, 2014-03-19 at 11:49 +0100, Gilles Duboscq wrote:
>> Hello,
>>
>> The graph you present is the code for:
>>
>> Integer foo(Integer x, Integer y) {
>> return x + y;
>> }
>>
>> There are no arrays involved and you can not force the parameters to
>> be arrays, that would just not work.
>>
>> If you want to work with integer streams you can use IntStream which
>> will allow you to work without the boxing (even for the lambdas).
>> I'm not sure what the example should do exactly since map seems to
>> only accept unary functions.
>> In any case, this lambda would be the kernel which is the "what to
>> run" and thus does not/can not contain any information about "what to
>> run *on*".
>> This information can only be available in the code which is applying
>> this lambda to some specific data.
>>
>> Maybe you can give a small example of what you would like to achieve
>> from java code to OpenCL code?
>>
>> -Gilles
>>
>> On Tue, Mar 18, 2014 at 6:17 PM, Juan Jose Fumero <juan.fumero at ed.ac.uk> wrote:
>> > Hello,
>> > I am working with lambda expression on JDK8 and Graal. My question is
>> > related with the creation of new nodes in the Graph to update or change
>> > the information.
>> >
>> >
>> > Given this lambda expression:
>> >
>> > stream.map((x,y) -> x + y);
>> >
>> > The StructuredGraph is the following:
>> >
>> > 0|StartNode
>> > 1|Parameter(0)
>> > 2|Parameter(1)
>> > 3|FrameState at 0
>> > 4|MethodCallTarget
>> > 5|Invoke#intValue
>> > 6|FrameState at 4
>> > 8|MethodCallTarget
>> > 9|Invoke#intValue
>> > 10|FrameState at 8
>> > 12|+
>> > 13|MethodCallTarget
>> > 14|Invoke#valueOf
>> > 15|FrameState at 12
>> > 17|Return
>> > 13|Invoke#valueOf
>> >
>> >
>> > Parameter(0) and Parameter(1) are Objects in the moment that I get the
>> > lambda expression. But later on, I know that could be Integer[],
>> > Double[], etc.
>> >
>> > I would like to rewrite this part of the Graph with the new information.
>> > Is there any utility in Graal to do that?
>> >
>> >
>> > For instance: if I get the parameterNode from the previous graph:
>> >
>> > if (((ObjectStamp) parameterNode.stamp()).type().isArray()) {
>> > ...
>> > }
>> >
>> >
>> > The nodes Parameter(0) and Parameter(1) in the lambda expression are not
>> > arrays. What I want to do is to change or update these nodes. What I am
>> > using now is a new node (paramNode):
>> >
>> >
>> > IntegerStamp integerStamp = new IntegerStamp(Kind.Int);
>> > ParameterNode paramNode = new ParameterNode(index, integerStamp);
>> > newGraph.unique(paramNode);
>> >
>> > But I need also to store the array information (size and dimension). The
>> > aim is facilitates the OpenCL code generation from this expression.
>> >
>> >
>> > Many thanks
>> > Juanjo
>> >
>> >
>> > --
>> > The University of Edinburgh is a charitable body, registered in
>> > Scotland, with registration number SC005336.
>> >
>>
>
>
>
> --
> The University of Edinburgh is a charitable body, registered in
> Scotland, with registration number SC005336.
>
More information about the graal-dev
mailing list