cancel
Showing results for 
Search instead for 
Did you mean: 

Archives Discussions

cristina_oprean
Journeyman III

dot product & bidimesional stream storage

 Hi,

I have 3 questions:

- I tried to use kernel intrinsic function dot(x,y).  Brook+ specification says "dot(x,y) - Dot product of the two vectors x and y". What does vector means: an unidimensional stream or an unidimensional table? Using this function, I observed that dot(x,y) is equivalent with x*y, where x and y are numbers, not streams nor vectors.  Can anybody provide an example where   this function is applied on 2 streams ?

=====================================================

- How are bidimensional streams organised in memory: row after row (linke in C) or column after colums?  Suppose  we have:

kernel void test(float x[][], ... )

{

  float2 idx={2,3};

 x[idx.xy]=5;

 ... .

}

Which element is getting the value 5: the element from the row 2 and colums 3 or the element from the row 3 and column 2? 

======================================================

- Suppose that I have global stream variable, when calling a kernel having as parameter this stream, it is created a local copy of this stream or all the operations within the kernel are executed directly on the global stream?

                                                                                         Thank you .

 

 

 

0 Likes
4 Replies
udeepta
Staff

Originally posted by: cristina.oprean  Hi,

 

I have 3 questions:

 

- I tried to use kernel intrinsic function dot(x,y).  Brook+ specification says "dot(x,y) - Dot product of the two vectors x and y". What does vector means: an unidimensional stream or an unidimensional table? Using this function, I observed that dot(x,y) is equivalent with x*y, where x and y are numbers, not streams nor vectors.  Can anybody provide an example where   this function is applied on 2 streams ? =====================================================



Vector here refers to float4 or double2 etc. If you have a float4 stream, you should be able to use it with dot().


- How are bidimensional streams organised in memory: row after row (linke in C) or column after colums?  Suppose  we have:

kernel void test(float x[][], ... ) {   float2 idx={2,3};  x[idx.xy]=5;  ... . } Which element is getting the value 5: the element from the row 2 and colums 3 or the element from the row 3 and column 2?  ======================================================

It is row after row (row-major, as in C). However, we always use float2 as an index; we do not need to do "index = y*width+i".


 

- Suppose that I have global stream variable, when calling a kernel having as parameter this stream, it is created a local copy of this stream or all the operations within the kernel are executed directly on the global stream?


                                                                                         Thank you .

 



If any changes are made to stream inside a kernel (you have to declare it an out stream), those changes will be visible to the stream once the kernel has finished executing.

0 Likes
cristina_oprean
Journeyman III

In respect with my second question, I made the following program that computes the sum of each row in a bidimensional stream. 

#include <stdio.h>

//computing in B the sum of elements from row i of A.

kernel void sum(float Length, float A[][], out float B<>)

{

float2 idx={0,0};

float accumulator = 0.0f;

float2 step={0,1};

float i;

idx.x = indexof(B).x;

for(i=0;i<Length;i++)

{

accumulator += A[idx.xy];

idx.xy+=step;

}

 

B = accumulator;

}

int main(int argc, char** argv)

{

unsigned int i,j;

const unsigned int Length = 3;

float A[Length][Length];

float StreamA<Length,Length>;

float rezStream<Length>;

float rez[Length];

 

for(i=0;i<Length;i++)

{

for(j=0;j<Length;j++)

{

A=i*Length+j;

printf("%f ",A);

}

printf("\n");

}

 

streamRead(StreamA, A);

sum(Length,StreamA,rezStream);

 

streamWrite(rezStream,rez);

printf("\nSums on rows:\n");

for(i=0;i<Length;i++)

printf("%f ",rez);

scanf("%*c");

return 0;

}

The output is:

0.000000 1.000000 2.000000
3.000000 4.000000 5.000000
6.000000 7.000000 8.000000

Sums on rows:
9.000000 12.000000 15.000000

The expected output should be: 3      12      21

 

It appears that the x part from an indices of type float2 represents the column, not the row! Is this true ?

0 Likes

Originally posted by: cristina.oprean

...

 

float2 step={0,1};

 

...

idx.xy+=step;

 

...

 

The program is in fact incrementing 'y' by 1 in each loop. 'x' remains constant. Hence the result. To do a sum over rows, we will use

float2 step={1,0};



0 Likes

In conclusion, the x part of an index represents the column in a bidimensional stream, not the row. This is opposite to C arrays, where first index always selects the row. The only reason that could justify this is that (from a graphical point of view) x represent s the x coordinate, y - the y coordinate  ... It is true?

This index selection seems non-intuitive for n-dimensional stream, but, anyway, Brook implements only 1D and 2D streams.

0 Likes