4 Replies Latest reply on Nov 25, 2009 5:13 PM by drstrip

    kronecker delta on uint4?

    drstrip

      I want to compute the kronecker delta (i = j returns 1, otherwise 0) for each element of a stream of type uint4. If I recall correctly, conditionals operate only on the first element of a vector type, so anything based on a conditional is out. I tried abs(sign(i - j)) but abs() will not accept a uint4, presumably because it has a conditional buried in it. The best I've come up with is:

      kernel uint4 kronecker_delta(uint4 i<>, uint j<>

      {

         int4 k_d = sign((int4) i - (int4)j);

         return (uint4)(k_d * k_d);

      }

       

      My Brook+ experience is near zero, so I wonder if I'm missing some much easier/faster way to do this.

       

       

        • kronecker delta on uint4?
          riza.guntur

          just make it in 4 separate if

          afaik that's the only way

          btw, in brook+ you need to specifiy output variable explicitly

          and there is no return, all basic kernel in brook+ is void

          kernel void kronecker_delta(unit i<>, unit j<>, out unit4 k<>

          you do the rest

           

            • kronecker delta on uint4?
              drstrip

              I should have mentioned the pseudo-code I included was a sub-kernel.

                • kronecker delta on uint4?
                  MicahVillmow
                  kernel uint4 kronecker_delta(uint4 i<>, uint j<>

                  {

                  int4 k_d = sign((int4) i - (int4)j);

                  return (uint4)(k_d * k_d);

                  }

                  drstrip,
                  I'm not sure how many instructions sign extends to, but you can try this:
                  return (uint4)((i != j) & (int4)1);

                  The first conditional should give each component that is equivalent a -1 and the rest 0. By anding with 1, the -1 becomes a 1 and the zero stays the same. This should be 2 instructions.
                    • kronecker delta on uint4?
                      drstrip

                      that's what I was looking for. I was so focused on the fact that conditionals only operate on the value of the first vector component that I ignored that the boolean operation would operate on all components and set a result for each. Your answer helped me focus on this more powerful approach. Thanks.

                       

                      BTW, what you wrote doesnlt actually compile, you need to write

                            return (uint4)(((int4) i != (int4) j) & int4(1,1,1,1));

                      at least that's what I came up with to get rid of compile errors.