Hello,
I would like to write a reduction kernel which finds (or returns) the index of max value in array.
We have seen a beautiful reduction function which finds max value in array as follows by Ceq and by jski.
reduce void red(double a<>, reduce double b<>{
if(b < a) b = a;
}
reduce void red( float value<>, reduce float result<> ){
result = max( value, value );
}
Now, I would like to find the index of that max value. Do you guys think it is possible with reduction kernel?
Micah, thanks very much for helping me.
BTW, I tried your suggestion as follows and got incorrect answer. I am suspecting that indexof() is not valid in reduction kernel.
====================================
reduce void red(float a<>, reduce float4 b<> {
if(b.x > a) {
b.x = a;
b.y = indexof(a).x;
b.z = indexof(a).y;
}
}
int main(int argc, char* argv[])
{
float input[8] = { 5, 2, 8, 6, 3, 7, 9, 4} ;
float4 result;
{
float inputStream<8>;
streamRead(inputStream, input);
red(inputStream, result);
}
printf("result.x is %f \n", result.x);
printf("result.y is %f \n", result.y);
printf("result.z is %f \n", result.z);
return 0;
}
========================
result.x is 2.000000
result.y is 2.000000
result.z is 2.000000
========================
Ceq,
I am very positive that you have a correct answer. Thanks.
BTW, do you have any other idea to do it efficiently on GPU, other than reduction kernel?
Thanks, Ceq and Micah,
BTW, I keep wondering what is benefit of using reduction kernel in terms of performance. I couldn't find any information on performance of reduction kernel. Of course, it looks pretty just like recursive function in C.
The disassembled binary of reduction kernel looks pretty neat in GSA, seeming very fast. I didn't measure the performance yet though.
Thanks in advance.
My problem is similar but I have to find minimum of my 3D stream and reduce it to 2D. As a result of result of reduction I sould have index of c-value from input1 stream.
reduce kernel void test2(float input1 <a,b,c> , reduce float output1<a,b> )
{
}
I don't really know how to deal with this. Could anyone help how to write in Brook?