Merge remote-tracking branch 'origin/2.4' into merge-2.4
[profile/ivi/opencv.git] / modules / ocl / src / split_merge.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                           License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2010-2012, Institute Of Software Chinese Academy Of Science, all rights reserved.
14 // Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // @Authors
18 //    Jia Haipeng, jiahaipeng95@gmail.com
19 //
20 // Redistribution and use in source and binary forms, with or without modification,
21 // are permitted provided that the following conditions are met:
22 //
23 //   * Redistribution's of source code must retain the above copyright notice,
24 //     this list of conditions and the following disclaimer.
25 //
26 //   * Redistribution's in binary form must reproduce the above copyright notice,
27 //     this list of conditions and the following disclaimer in the documentation
28 //     and/or other oclMaterials provided with the distribution.
29 //
30 //   * The name of the copyright holders may not be used to endorse or promote products
31 //     derived from this software without specific prior written permission.
32 //
33 // This software is provided by the copyright holders and contributors "as is" and
34 // any express or implied warranties, including, but not limited to, the implied
35 // warranties of merchantability and fitness for a particular purpose are disclaimed.
36 // In no event shall the Intel Corporation or contributors be liable for any direct,
37 // indirect, incidental, special, exemplary, or consequential damages
38 // (including, but not limited to, procurement of substitute goods or services;
39 // loss of use, data, or profits; or business interruption) however caused
40 // and on any theory of liability, whether in contract, strict liability,
41 // or tort (including negligence or otherwise) arising in any way out of
42 // the use of this software, even if advised of the possibility of such damage.
43 //
44 //M*/
45
46 #include "precomp.hpp"
47 #include <vector>
48
49 using namespace cv;
50 using namespace cv::ocl;
51
52 ////////////////////////////////////////////////////////////////////////
53 ///////////////// oclMat merge and split ///////////////////////////////
54 ////////////////////////////////////////////////////////////////////////
55
56 namespace cv
57 {
58     namespace ocl
59     {
60         ///////////////////////////OpenCL kernel strings///////////////////////////
61         extern const char *merge_mat;
62         extern const char *split_mat;
63     }
64 }
65 namespace cv
66 {
67     namespace ocl
68     {
69         namespace split_merge
70         {
71             ///////////////////////////////////////////////////////////
72             ///////////////common/////////////////////////////////////
73             /////////////////////////////////////////////////////////
74             inline int divUp(int total, int grain)
75             {
76                 return (total + grain - 1) / grain;
77             }
78             ////////////////////////////////////////////////////////////////////////////
79             ////////////////////merge//////////////////////////////////////////////////
80             ////////////////////////////////////////////////////////////////////////////
81             // static void merge_vector_run_no_roi(const oclMat *mat_src, size_t n, oclMat &mat_dst)
82             // {
83             //     Context  *clCxt = mat_dst.clCxt;
84             //     int channels = mat_dst.oclchannels();
85             //     int depth = mat_dst.depth();
86
87             //     String kernelName = "merge_vector";
88
89             //     int indexes[4][7] = {{0, 0, 0, 0, 0, 0, 0},
90             //         {4, 4, 2, 2, 1, 1, 1},
91             //         {4, 4, 2, 2 , 1, 1, 1},
92             //         {4, 4, 2, 2, 1, 1, 1}
93             //     };
94
95             //     size_t index = indexes[channels - 1][mat_dst.depth()];
96             //     int    cols = divUp(mat_dst.cols, index);
97             //     size_t localThreads[3]  = { 64, 4, 1 };
98             //     size_t globalThreads[3] = { divUp(cols, localThreads[0]) *localThreads[0],
99             //                                 divUp(mat_dst.rows, localThreads[1]) *localThreads[1],
100             //                                 1
101             //                               };
102
103             //     std::vector<std::pair<size_t , const void *> > args;
104             //     args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_dst.rows));
105             //     args.push_back( std::make_pair( sizeof(cl_int), (void *)&cols));
106             //     args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_dst.data));
107             //     args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_dst.step));
108             //     args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_src[0].data));
109             //     args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src[0].step));
110             //     args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_src[1].data));
111             //     args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src[1].step));
112             //     if(n >= 3)
113             //     {
114             //         args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_src[2].data));
115             //         args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src[2].step));
116             //     }
117             //     if(n >= 4)
118             //     {
119             //         args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_src[3].data));
120             //         args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src[3].step));
121             //     }
122
123             //     openCLExecuteKernel(clCxt, &merge_mat, kernelName, globalThreads, localThreads, args, channels, depth);
124             // }
125
126             static void merge_vector_run(const oclMat *mat_src, size_t n, oclMat &mat_dst)
127             {
128                 if(!mat_dst.clCxt->supportsFeature(Context::CL_DOUBLE) && mat_dst.type() == CV_64F)
129                 {
130                     CV_Error(Error::GpuNotSupported, "Selected device don't support double\r\n");
131                     return;
132                 }
133
134                 Context  *clCxt = mat_dst.clCxt;
135                 int channels = mat_dst.oclchannels();
136                 int depth = mat_dst.depth();
137
138                 String kernelName = "merge_vector";
139
140                 int vector_lengths[4][7] = {{0, 0, 0, 0, 0, 0, 0},
141                     {2, 2, 1, 1, 1, 1, 1},
142                     {4, 4, 2, 2 , 1, 1, 1},
143                     {1, 1, 1, 1, 1, 1, 1}
144                 };
145
146                 size_t vector_length = vector_lengths[channels - 1][depth];
147                 int offset_cols = (mat_dst.offset / mat_dst.elemSize()) & (vector_length - 1);
148                 int cols = divUp(mat_dst.cols + offset_cols, vector_length);
149
150                 size_t localThreads[3]  = { 64, 4, 1 };
151                 size_t globalThreads[3] = { divUp(cols, localThreads[0]) *localThreads[0],
152                                             divUp(mat_dst.rows, localThreads[1]) *localThreads[1],
153                                             1
154                                           };
155
156                 int dst_step1 = mat_dst.cols * mat_dst.elemSize();
157                 std::vector<std::pair<size_t , const void *> > args;
158                 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_dst.data));
159                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_dst.step));
160                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_dst.offset));
161                 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_src[0].data));
162                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src[0].step));
163                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src[0].offset));
164                 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_src[1].data));
165                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src[1].step));
166                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src[1].offset));
167
168                 if(channels == 4)
169                 {
170                     args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_src[2].data));
171                     args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src[2].step));
172                     args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src[2].offset));
173
174                     // if channel == 3, then the matrix will convert to channel =4
175                     //if(n == 3)
176                     //   args.push_back( std::make_pair( sizeof(cl_int), (void *)&offset_cols));
177
178                     if(n == 3)
179                     {
180                         args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_src[2].data));
181                         args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src[2].step));
182                         args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src[2].offset));
183                     }
184                     else if( n == 4)
185                     {
186                         args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_src[3].data));
187                         args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src[3].step));
188                         args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src[3].offset));
189                     }
190                 }
191
192                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_dst.rows));
193                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&cols));
194                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst_step1));
195
196                 openCLExecuteKernel(clCxt, &merge_mat, kernelName, globalThreads, localThreads, args, channels, depth);
197             }
198             static void merge(const oclMat *mat_src, size_t n, oclMat &mat_dst)
199             {
200                 CV_Assert(mat_src);
201                 CV_Assert(n > 0);
202
203                 int depth = mat_src[0].depth();
204                 Size size = mat_src[0].size();
205
206                 int total_channels = 0;
207
208                 for(size_t i = 0; i < n; ++i)
209                 {
210                     CV_Assert(depth == mat_src[i].depth());
211                     CV_Assert(size == mat_src[i].size());
212
213                     total_channels += mat_src[i].oclchannels();
214                 }
215
216                 CV_Assert(total_channels <= 4);
217
218                 if(total_channels == 1)
219                 {
220                     mat_src[0].copyTo(mat_dst);
221                     return;
222                 }
223
224                 mat_dst.create(size, CV_MAKETYPE(depth, total_channels));
225                 merge_vector_run(mat_src, n, mat_dst);
226             }
227             ////////////////////////////////////////////////////////////////////////////////////////////////////
228             //////////////////////////////////////split/////////////////////////////////////////////////////////////
229             //////////////////////////////////////////////////////////////////////////////////////////////////
230             // static void split_vector_run_no_roi(const oclMat &mat_src, oclMat *mat_dst)
231             // {
232             //     Context  *clCxt = mat_src.clCxt;
233             //     int channels = mat_src.oclchannels();
234             //     int depth = mat_src.depth();
235
236             //     String kernelName = "split_vector";
237
238             //     int indexes[4][7] = {{0, 0, 0, 0, 0, 0, 0},
239             //         {8, 8, 8, 8, 4, 4, 2},
240             //         {8, 8, 8, 8 , 4, 4, 4},
241             //         {4, 4, 2, 2, 1, 1, 1}
242             //     };
243
244             //     size_t index = indexes[channels - 1][mat_dst[0].depth()];
245             //     int cols = divUp(mat_src.cols, index);
246             //     size_t localThreads[3]  = { 64, 4, 1 };
247             //     size_t globalThreads[3] = { divUp(cols, localThreads[0]) *localThreads[0],
248             //                                 divUp(mat_src.rows, localThreads[1]) *localThreads[1],
249             //                                 1
250             //                               };
251
252             //     std::vector<std::pair<size_t , const void *> > args;
253             //     args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_src.data));
254             //     args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src.step));
255             //     args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src.rows));
256             //     args.push_back( std::make_pair( sizeof(cl_int), (void *)&cols));
257             //     args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_dst[0].data));
258             //     args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_dst[0].step));
259             //     args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_dst[1].data));
260             //     args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_dst[1].step));
261             //     if(channels >= 3)
262             //     {
263             //         args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_dst[2].data));
264             //         args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_dst[2].step));
265             //     }
266             //     if(channels >= 4)
267             //     {
268             //         args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_dst[3].data));
269             //         args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_dst[3].step));
270             //     }
271
272             //     openCLExecuteKernel(clCxt, &split_mat, kernelName, globalThreads, localThreads, args, channels, depth);
273             // }
274             static void split_vector_run(const oclMat &mat_src, oclMat *mat_dst)
275             {
276
277                 if(!mat_src.clCxt->supportsFeature(Context::CL_DOUBLE) && mat_src.type() == CV_64F)
278                 {
279                     CV_Error(Error::GpuNotSupported, "Selected device don't support double\r\n");
280                     return;
281                 }
282
283                 Context  *clCxt = mat_src.clCxt;
284                 int channels = mat_src.oclchannels();
285                 int depth = mat_src.depth();
286
287                 String kernelName = "split_vector";
288
289                 int vector_lengths[4][7] = {{0, 0, 0, 0, 0, 0, 0},
290                     {4, 4, 2, 2, 1, 1, 1},
291                     {4, 4, 2, 2 , 1, 1, 1},
292                     {4, 4, 2, 2, 1, 1, 1}
293                 };
294
295                 size_t vector_length = vector_lengths[channels - 1][mat_dst[0].depth()];
296
297                 int max_offset_cols = 0;
298                 for(int i = 0; i < channels; i++)
299                 {
300                     int offset_cols = (mat_dst[i].offset / mat_dst[i].elemSize()) & (vector_length - 1);
301                     if(max_offset_cols < offset_cols)
302                         max_offset_cols = offset_cols;
303                 }
304
305                 int cols =  vector_length == 1 ? divUp(mat_src.cols, vector_length)
306                             : divUp(mat_src.cols + max_offset_cols, vector_length);
307
308                 size_t localThreads[3]  = { 64, 4, 1 };
309                 size_t globalThreads[3] = { divUp(cols, localThreads[0]) *localThreads[0],
310                                             divUp(mat_src.rows, localThreads[1]) *localThreads[1], 1
311                                           };
312
313                 int dst_step1 = mat_dst[0].cols * mat_dst[0].elemSize();
314                 std::vector<std::pair<size_t , const void *> > args;
315                 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_src.data));
316                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src.step));
317                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src.offset));
318                 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_dst[0].data));
319                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_dst[0].step));
320                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_dst[0].offset));
321                 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_dst[1].data));
322                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_dst[1].step));
323                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_dst[1].offset));
324                 if(channels >= 3)
325                 {
326
327                     args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_dst[2].data));
328                     args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_dst[2].step));
329                     args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_dst[2].offset));
330                 }
331                 if(channels >= 4)
332                 {
333                     args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_dst[3].data));
334                     args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_dst[3].step));
335                     args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_dst[3].offset));
336                 }
337
338                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src.rows));
339                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&cols));
340                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst_step1));
341
342                 openCLExecuteKernel(clCxt, &split_mat, kernelName, globalThreads, localThreads, args, channels, depth);
343             }
344             static void split(const oclMat &mat_src, oclMat *mat_dst)
345             {
346                 CV_Assert(mat_dst);
347
348                 int depth = mat_src.depth();
349                 int num_channels = mat_src.oclchannels();
350                 Size size = mat_src.size();
351
352                 if(num_channels == 1)
353                 {
354                     mat_src.copyTo(mat_dst[0]);
355                     return;
356                 }
357
358                 int i;
359                 for(i = 0; i < num_channels; i++)
360                     mat_dst[i].create(size, CV_MAKETYPE(depth, 1));
361
362                 split_vector_run(mat_src, mat_dst);
363             }
364         }
365     }
366 }
367
368 void cv::ocl::merge(const oclMat *src, size_t n, oclMat &dst)
369 {
370     split_merge::merge(src, n, dst);
371 }
372 void cv::ocl::merge(const std::vector<oclMat> &src, oclMat &dst)
373 {
374     split_merge::merge(&src[0], src.size(), dst);
375 }
376
377 void cv::ocl::split(const oclMat &src, oclMat *dst)
378 {
379     split_merge::split(src, dst);
380 }
381 void cv::ocl::split(const oclMat &src, std::vector<oclMat> &dst)
382 {
383     dst.resize(src.oclchannels());
384     if(src.oclchannels() > 0)
385         split_merge::split(src, &dst[0]);
386 }