title: Returning the minimum and maximum elements of an array in CUDA
body: <p>I'm performing some array manipulation/calculation in CUDA (via the <a href="http://www.hybriddsp.com/Products/CUDAfyNET.aspx" rel="nofollow">Cudafy.NET library</a>, though I'm equally interested in CUDA/C++ methods), and need to calculate the minimum and maximum values that are in the array. One of the kernels looks like this:</p>
  
  <pre><code>    [Cudafy]
      public static void UpdateEz(GThread thread, float time, float ca, float cb, float[,] hx, float[,] hy, float[,] ez)
      {
          var i = thread.blockIdx.x;
          var j = thread.blockIdx.y;
  
          if (i &gt; 0 &amp;&amp; i &lt; ez.GetLength(0) - 1 &amp;&amp; j &gt; 0 &amp;&amp; j &lt; ez.GetLength(1) - 1)
              ez[i, j] =
                  ca * ez[i, j]
                  + cb * (hx[i, j] - hx[i - 1, j])
                  + cb * (hy[i, j - 1] - hy[i, j])
                  ;
      }
  </code></pre>
  
  <p>I'd like to do something like this:</p>
  
  <pre><code>    [Cudafy]
      public static void UpdateEz(GThread thread, float time, float ca, float cb, float[,] hx, float[,] hy, float[,] ez, out float min, out float max)
      {
          var i = thread.blockIdx.x;
          var j = thread.blockIdx.y;
  
          min = float.MaxValue;
          max = float.MinValue;
  
          if (i &gt; 0 &amp;&amp; i &lt; ez.GetLength(0) - 1 &amp;&amp; j &gt; 0 &amp;&amp; j &lt; ez.GetLength(1) - 1)
          {
              ez[i, j] =
                  ca * ez[i, j]
                  + cb * (hx[i, j] - hx[i - 1, j])
                  + cb * (hy[i, j - 1] - hy[i, j])
                  ;
  
              min = Math.Min(ez[i, j], min);
              max = Math.Max(ez[i, j], max);
  
          }
      }
  </code></pre>
  
  <p>Anyone knows of a convenient way to return the minimum and maximum values (for the entire array, not just per thread or block)?</p>