arithm_minMax.cl 5.73 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                           License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2010-2012, Institute Of Software Chinese Academy Of Science, all rights reserved.
// Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// @Authors
//    Shengen Yan,yanshengen@gmail.com
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
Andrey Pavlenko's avatar
Andrey Pavlenko committed
28
//     and/or other materials provided with the distribution.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
//
//   * The name of the copyright holders may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors as is and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

/**************************************PUBLICFUNC*************************************/
47

48
#if defined (DOUBLE_SUPPORT)
49 50 51
#ifdef cl_amd_fp64
#pragma OPENCL EXTENSION cl_amd_fp64:enable
#elif defined (cl_khr_fp64)
52 53
#pragma OPENCL EXTENSION cl_khr_fp64:enable
#endif
54
#endif
55

56 57 58 59 60 61 62
#ifdef DEPTH_5
#define MIN_VAL (-FLT_MAX)
#define MAX_VAL FLT_MAX
#elif defined DEPTH_6
#define MIN_VAL (-DBL_MAX)
#define MAX_VAL DBL_MAX
#endif
63 64

/**************************************Array minMax**************************************/
Ilya Lavrenov's avatar
Ilya Lavrenov committed
65 66 67

__kernel void arithm_op_minMax(__global const T * src, __global T * dst,
    int cols, int invalid_cols, int offset, int elemnum, int groupnum)
68 69 70
{
   unsigned int lid = get_local_id(0);
   unsigned int gid = get_group_id(0);
Ilya Lavrenov's avatar
Ilya Lavrenov committed
71 72
   unsigned int id = get_global_id(0);

73
   unsigned int idx = offset + id + (id / cols) * invalid_cols;
Ilya Lavrenov's avatar
Ilya Lavrenov committed
74 75 76 77 78

   __local T localmem_max[128], localmem_min[128];
   T minval = (T)(MAX_VAL), maxval = (T)(MIN_VAL), temp;

   for (int grainSize = groupnum << 8; id < elemnum; id += grainSize)
79
   {
Ilya Lavrenov's avatar
Ilya Lavrenov committed
80
       idx = offset + id + (id / cols) * invalid_cols;
81
       temp = src[idx];
Ilya Lavrenov's avatar
Ilya Lavrenov committed
82 83 84 85
       minval = min(minval, temp);
       maxval = max(maxval, temp);
   }

86
   if (lid > 127)
Ilya Lavrenov's avatar
Ilya Lavrenov committed
87 88 89 90 91 92
   {
       localmem_min[lid - 128] = minval;
       localmem_max[lid - 128] = maxval;
   }
   barrier(CLK_LOCAL_MEM_FENCE);

93
   if (lid < 128)
Ilya Lavrenov's avatar
Ilya Lavrenov committed
94 95 96 97 98 99 100 101 102
   {
       localmem_min[lid] = min(minval, localmem_min[lid]);
       localmem_max[lid] = max(maxval, localmem_max[lid]);
   }
   barrier(CLK_LOCAL_MEM_FENCE);

   for (int lsize = 64; lsize > 0; lsize >>= 1)
   {
       if (lid < lsize)
103
       {
Ilya Lavrenov's avatar
Ilya Lavrenov committed
104 105 106
           int lid2 = lsize + lid;
           localmem_min[lid] = min(localmem_min[lid], localmem_min[lid2]);
           localmem_max[lid] = max(localmem_max[lid], localmem_max[lid2]);
107
       }
Ilya Lavrenov's avatar
Ilya Lavrenov committed
108
       barrier(CLK_LOCAL_MEM_FENCE);
109
   }
Ilya Lavrenov's avatar
Ilya Lavrenov committed
110 111

   if (lid == 0)
112
   {
Ilya Lavrenov's avatar
Ilya Lavrenov committed
113 114
       dst[gid] = localmem_min[0];
       dst[gid + groupnum] = localmem_max[0];
115
   }
Ilya Lavrenov's avatar
Ilya Lavrenov committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
}

__kernel void arithm_op_minMax_mask(__global const T * src, __global T * dst,
    int cols, int invalid_cols, int offset,
    int elemnum, int groupnum,
    const __global uchar * mask, int minvalid_cols, int moffset)
{
   unsigned int lid = get_local_id(0);
   unsigned int gid = get_group_id(0);
   unsigned int id = get_global_id(0);

   unsigned int idx = offset + id + (id / cols) * invalid_cols;
   unsigned int midx = moffset + id + (id / cols) * minvalid_cols;

   __local T localmem_max[128], localmem_min[128];
   T minval = (T)(MAX_VAL), maxval = (T)(MIN_VAL), temp;

   for (int grainSize = groupnum << 8; id < elemnum; id += grainSize)
134 135
   {
       idx = offset + id + (id / cols) * invalid_cols;
Ilya Lavrenov's avatar
Ilya Lavrenov committed
136 137 138
       midx = moffset + id + (id / cols) * minvalid_cols;

       if (mask[midx])
139
       {
Ilya Lavrenov's avatar
Ilya Lavrenov committed
140 141 142
           temp = src[idx];
           minval = min(minval, temp);
           maxval = max(maxval, temp);
143 144
       }
   }
Ilya Lavrenov's avatar
Ilya Lavrenov committed
145

146
   if (lid > 127)
147 148 149 150 151
   {
       localmem_min[lid - 128] = minval;
       localmem_max[lid - 128] = maxval;
   }
   barrier(CLK_LOCAL_MEM_FENCE);
Ilya Lavrenov's avatar
Ilya Lavrenov committed
152

153
   if (lid < 128)
154
   {
Ilya Lavrenov's avatar
Ilya Lavrenov committed
155 156
       localmem_min[lid] = min(minval, localmem_min[lid]);
       localmem_max[lid] = max(maxval, localmem_max[lid]);
157 158
   }
   barrier(CLK_LOCAL_MEM_FENCE);
Ilya Lavrenov's avatar
Ilya Lavrenov committed
159 160

   for (int lsize = 64; lsize > 0; lsize >>= 1)
161
   {
Ilya Lavrenov's avatar
Ilya Lavrenov committed
162
       if (lid < lsize)
163 164
       {
           int lid2 = lsize + lid;
Ilya Lavrenov's avatar
Ilya Lavrenov committed
165 166
           localmem_min[lid] = min(localmem_min[lid], localmem_min[lid2]);
           localmem_max[lid] = max(localmem_max[lid], localmem_max[lid2]);
167 168 169
       }
       barrier(CLK_LOCAL_MEM_FENCE);
   }
Ilya Lavrenov's avatar
Ilya Lavrenov committed
170 171

   if (lid == 0)
172 173 174 175 176
   {
       dst[gid] = localmem_min[0];
       dst[gid + groupnum] = localmem_max[0];
   }
}