optimized gpu::minMax a little
authorAlexey Spizhevoy <no@email>
Wed, 24 Nov 2010 09:19:11 +0000 (09:19 +0000)
committerAlexey Spizhevoy <no@email>
Wed, 24 Nov 2010 09:19:11 +0000 (09:19 +0000)
modules/gpu/src/cuda/mathfunc.cu

index d557288..3f3fd71 100644 (file)
@@ -400,6 +400,16 @@ namespace cv { namespace gpu { namespace mathfunc
 // Min max\r
 \r
     enum { MIN, MAX };  \r
+\r
+    template <typename T> struct MinMaxTypeTraits {};\r
+    template <> struct MinMaxTypeTraits<unsigned char> { typedef int best_type; };\r
+    template <> struct MinMaxTypeTraits<signed char> { typedef int best_type; };\r
+    template <> struct MinMaxTypeTraits<unsigned short> { typedef int best_type; };\r
+    template <> struct MinMaxTypeTraits<signed short> { typedef int best_type; };\r
+    template <> struct MinMaxTypeTraits<int> { typedef int best_type; };\r
+    template <> struct MinMaxTypeTraits<float> { typedef float best_type; };\r
+    template <> struct MinMaxTypeTraits<double> { typedef double best_type; };\r
+\r
     template <typename T, int op> struct Cmp {};\r
     \r
     template <typename T>\r
@@ -407,9 +417,7 @@ namespace cv { namespace gpu { namespace mathfunc
     {\r
         static __device__ void call(unsigned int tid, unsigned int offset, volatile T* optval)\r
         {\r
-            T val = optval[tid + offset];\r
-            if (val < optval[tid]) optval[tid] = val;\r
-            //optval[tid] = min(optval[tid], optval[tid + offset]); \r
+            optval[tid] = min(optval[tid], optval[tid + offset]); \r
         }\r
     };\r
 \r
@@ -418,17 +426,16 @@ namespace cv { namespace gpu { namespace mathfunc
     {\r
         static __device__ void call(unsigned int tid, unsigned int offset, volatile T* optval)\r
         {\r
-            T val = optval[tid + offset];\r
-            if (val > optval[tid]) optval[tid] = val;\r
-            //optval[tid] = max(optval[tid], optval[tid + offset]);\r
+            optval[tid] = max(optval[tid], optval[tid + offset]);\r
         }\r
     };\r
 \r
 \r
-    template <int nthreads, typename Cmp, typename T>\r
+    template <int nthreads, int op, typename T>\r
     __global__ void opt_kernel(int cols, int rows, const PtrStep src, PtrStep optval)\r
     {\r
-        __shared__ T soptval[nthreads];\r
+        typedef typename MinMaxTypeTraits<T>::best_type best_type;\r
+        __shared__ best_type soptval[nthreads];\r
 \r
         unsigned int x0 = blockIdx.x * blockDim.x;\r
         unsigned int y0 = blockIdx.y * blockDim.y;\r
@@ -441,21 +448,21 @@ namespace cv { namespace gpu { namespace mathfunc
 \r
         __syncthreads();\r
 \r
-        if (nthreads >= 512) if (tid < 256) { Cmp::call(tid, 256, soptval); __syncthreads(); }\r
-        if (nthreads >= 256) if (tid < 128) { Cmp::call(tid, 128, soptval); __syncthreads(); }\r
-        if (nthreads >= 128) if (tid < 64) { Cmp::call(tid, 64, soptval); __syncthreads(); }\r
+        if (nthreads >= 512) if (tid < 256) { Cmp<best_type, op>::call(tid, 256, soptval); __syncthreads(); }\r
+        if (nthreads >= 256) if (tid < 128) { Cmp<best_type, op>::call(tid, 128, soptval); __syncthreads(); }\r
+        if (nthreads >= 128) if (tid < 64) { Cmp<best_type, op>::call(tid, 64, soptval); __syncthreads(); }\r
 \r
         if (tid < 32)\r
         {\r
-            if (nthreads >= 64) Cmp::call(tid, 32, soptval);\r
-            if (nthreads >= 32) Cmp::call(tid, 16, soptval);\r
-            if (nthreads >= 16) Cmp::call(tid, 8, soptval);\r
-            if (nthreads >= 8) Cmp::call(tid, 4, soptval);\r
-            if (nthreads >= 4) Cmp::call(tid, 2, soptval);\r
-            if (nthreads >= 2) Cmp::call(tid, 1, soptval);\r
+            if (nthreads >= 64) Cmp<best_type, op>::call(tid, 32, soptval);\r
+            if (nthreads >= 32) Cmp<best_type, op>::call(tid, 16, soptval);\r
+            if (nthreads >= 16) Cmp<best_type, op>::call(tid, 8, soptval);\r
+            if (nthreads >= 8) Cmp<best_type, op>::call(tid, 4, soptval);\r
+            if (nthreads >= 4) Cmp<best_type, op>::call(tid, 2, soptval);\r
+            if (nthreads >= 2) Cmp<best_type, op>::call(tid, 1, soptval);\r
         }\r
 \r
-        if (tid == 0) ((T*)optval.ptr(blockIdx.y))[blockIdx.x] = soptval[0];\r
+        if (tid == 0) ((T*)optval.ptr(blockIdx.y))[blockIdx.x] = (T)soptval[0];\r
     }\r
 \r
    \r
@@ -483,16 +490,16 @@ namespace cv { namespace gpu { namespace mathfunc
         dim3 cursize(src.cols, src.rows);\r
         dim3 grid(divUp(cursize.x, threads.x), divUp(cursize.y, threads.y));\r
 \r
-        opt_kernel<256, Cmp<T, MIN>, T><<<grid, threads>>>(cursize.x, cursize.y, src, minval_buf[curbuf]);\r
-        opt_kernel<256, Cmp<T, MAX>, T><<<grid, threads>>>(cursize.x, cursize.y, src, maxval_buf[curbuf]);\r
+        opt_kernel<256, MIN, T><<<grid, threads>>>(cursize.x, cursize.y, src, minval_buf[curbuf]);\r
+        opt_kernel<256, MAX, T><<<grid, threads>>>(cursize.x, cursize.y, src, maxval_buf[curbuf]);\r
         cursize = grid;\r
 \r
         while (cursize.x > 1 || cursize.y > 1)\r
         {\r
             grid.x = divUp(cursize.x, threads.x); \r
             grid.y = divUp(cursize.y, threads.y);  \r
-            opt_kernel<256, Cmp<T, MIN>, T><<<grid, threads>>>(cursize.x, cursize.y, minval_buf[curbuf], minval_buf[1 - curbuf]);\r
-            opt_kernel<256, Cmp<T, MAX>, T><<<grid, threads>>>(cursize.x, cursize.y, maxval_buf[curbuf], maxval_buf[1 - curbuf]);\r
+            opt_kernel<256, MIN, T><<<grid, threads>>>(cursize.x, cursize.y, minval_buf[curbuf], minval_buf[1 - curbuf]);\r
+            opt_kernel<256, MAX, T><<<grid, threads>>>(cursize.x, cursize.y, maxval_buf[curbuf], maxval_buf[1 - curbuf]);\r
             curbuf = 1 - curbuf;\r
             cursize = grid;\r
         }\r