ocl: split initialization.cpp into 3 files: context, operations, programcache
[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 using namespace std;
52
53
54 using std::cout;
55 using std::endl;
56
57 ////////////////////////////////////////////////////////////////////////
58 ///////////////// oclMat merge and split ///////////////////////////////
59 ////////////////////////////////////////////////////////////////////////
60
61 namespace cv
62 {
63     namespace ocl
64     {
65         ///////////////////////////OpenCL kernel strings///////////////////////////
66         extern const char *merge_mat;
67         extern const char *split_mat;
68     }
69 }
70 namespace cv
71 {
72     namespace ocl
73     {
74         namespace split_merge
75         {
76             static void merge_vector_run(const oclMat *mat_src, size_t n, oclMat &mat_dst)
77             {
78                 if(!mat_dst.clCxt->supportsFeature(FEATURE_CL_DOUBLE) && mat_dst.type() == CV_64F)
79                 {
80                     CV_Error(CV_GpuNotSupported, "Selected device don't support double\r\n");
81                     return;
82                 }
83
84                 Context  *clCxt = mat_dst.clCxt;
85                 int channels = mat_dst.oclchannels();
86                 int depth = mat_dst.depth();
87
88                 string kernelName = "merge_vector";
89
90                 int vector_lengths[4][7] = {{0, 0, 0, 0, 0, 0, 0},
91                     {2, 2, 1, 1, 1, 1, 1},
92                     {4, 4, 2, 2 , 1, 1, 1},
93                     {1, 1, 1, 1, 1, 1, 1}
94                 };
95
96                 size_t vector_length = vector_lengths[channels - 1][depth];
97                 int offset_cols = (mat_dst.offset / mat_dst.elemSize()) & (vector_length - 1);
98                 int cols = divUp(mat_dst.cols + offset_cols, vector_length);
99
100                 size_t localThreads[3]  = { 64, 4, 1 };
101                 size_t globalThreads[3] = { cols, mat_dst.rows, 1 };
102
103                 int dst_step1 = mat_dst.cols * mat_dst.elemSize();
104                 vector<pair<size_t , const void *> > args;
105                 args.push_back( make_pair( sizeof(cl_mem), (void *)&mat_dst.data));
106                 args.push_back( make_pair( sizeof(cl_int), (void *)&mat_dst.step));
107                 args.push_back( make_pair( sizeof(cl_int), (void *)&mat_dst.offset));
108                 args.push_back( make_pair( sizeof(cl_mem), (void *)&mat_src[0].data));
109                 args.push_back( make_pair( sizeof(cl_int), (void *)&mat_src[0].step));
110                 args.push_back( make_pair( sizeof(cl_int), (void *)&mat_src[0].offset));
111                 args.push_back( make_pair( sizeof(cl_mem), (void *)&mat_src[1].data));
112                 args.push_back( make_pair( sizeof(cl_int), (void *)&mat_src[1].step));
113                 args.push_back( make_pair( sizeof(cl_int), (void *)&mat_src[1].offset));
114
115                 if(channels == 4)
116                 {
117                     args.push_back( make_pair( sizeof(cl_mem), (void *)&mat_src[2].data));
118                     args.push_back( make_pair( sizeof(cl_int), (void *)&mat_src[2].step));
119                     args.push_back( make_pair( sizeof(cl_int), (void *)&mat_src[2].offset));
120
121                     if(n == 3)
122                     {
123                         args.push_back( make_pair( sizeof(cl_mem), (void *)&mat_src[2].data));
124                         args.push_back( make_pair( sizeof(cl_int), (void *)&mat_src[2].step));
125                         args.push_back( make_pair( sizeof(cl_int), (void *)&mat_src[2].offset));
126                     }
127                     else if( n == 4)
128                     {
129                         args.push_back( make_pair( sizeof(cl_mem), (void *)&mat_src[3].data));
130                         args.push_back( make_pair( sizeof(cl_int), (void *)&mat_src[3].step));
131                         args.push_back( make_pair( sizeof(cl_int), (void *)&mat_src[3].offset));
132                     }
133                 }
134
135                 args.push_back( make_pair( sizeof(cl_int), (void *)&mat_dst.rows));
136                 args.push_back( make_pair( sizeof(cl_int), (void *)&cols));
137                 args.push_back( make_pair( sizeof(cl_int), (void *)&dst_step1));
138
139                 openCLExecuteKernel(clCxt, &merge_mat, kernelName, globalThreads, localThreads, args, channels, depth);
140             }
141             static void merge(const oclMat *mat_src, size_t n, oclMat &mat_dst)
142             {
143                 CV_Assert(mat_src);
144                 CV_Assert(n > 0);
145
146                 int depth = mat_src[0].depth();
147                 Size size = mat_src[0].size();
148
149                 int total_channels = 0;
150
151                 for(size_t i = 0; i < n; ++i)
152                 {
153                     CV_Assert(depth == mat_src[i].depth());
154                     CV_Assert(size == mat_src[i].size());
155
156                     total_channels += mat_src[i].oclchannels();
157                 }
158
159                 CV_Assert(total_channels <= 4);
160
161                 if(total_channels == 1)
162                 {
163                     mat_src[0].copyTo(mat_dst);
164                     return;
165                 }
166
167                 mat_dst.create(size, CV_MAKETYPE(depth, total_channels));
168                 merge_vector_run(mat_src, n, mat_dst);
169             }
170             static void split_vector_run(const oclMat &mat_src, oclMat *mat_dst)
171             {
172
173                 if(!mat_src.clCxt->supportsFeature(FEATURE_CL_DOUBLE) && mat_src.type() == CV_64F)
174                 {
175                     CV_Error(CV_GpuNotSupported, "Selected device don't support double\r\n");
176                     return;
177                 }
178
179                 Context  *clCxt = mat_src.clCxt;
180                 int channels = mat_src.oclchannels();
181                 int depth = mat_src.depth();
182
183                 string kernelName = "split_vector";
184
185                 int vector_lengths[4][7] = {{0, 0, 0, 0, 0, 0, 0},
186                     {4, 4, 2, 2, 1, 1, 1},
187                     {4, 4, 2, 2 , 1, 1, 1},
188                     {4, 4, 2, 2, 1, 1, 1}
189                 };
190
191                 size_t vector_length = vector_lengths[channels - 1][mat_dst[0].depth()];
192
193                 int max_offset_cols = 0;
194                 for(int i = 0; i < channels; i++)
195                 {
196                     int offset_cols = (mat_dst[i].offset / mat_dst[i].elemSize()) & (vector_length - 1);
197                     if(max_offset_cols < offset_cols)
198                         max_offset_cols = offset_cols;
199                 }
200
201                 int cols =  vector_length == 1 ? divUp(mat_src.cols, vector_length)
202                             : divUp(mat_src.cols + max_offset_cols, vector_length);
203
204                 size_t localThreads[3]  = { 64, 4, 1 };
205                 size_t globalThreads[3] = { cols, mat_src.rows, 1 };
206
207                 int dst_step1 = mat_dst[0].cols * mat_dst[0].elemSize();
208                 vector<pair<size_t , const void *> > args;
209                 args.push_back( make_pair( sizeof(cl_mem), (void *)&mat_src.data));
210                 args.push_back( make_pair( sizeof(cl_int), (void *)&mat_src.step));
211                 args.push_back( make_pair( sizeof(cl_int), (void *)&mat_src.offset));
212                 args.push_back( make_pair( sizeof(cl_mem), (void *)&mat_dst[0].data));
213                 args.push_back( make_pair( sizeof(cl_int), (void *)&mat_dst[0].step));
214                 args.push_back( make_pair( sizeof(cl_int), (void *)&mat_dst[0].offset));
215                 args.push_back( make_pair( sizeof(cl_mem), (void *)&mat_dst[1].data));
216                 args.push_back( make_pair( sizeof(cl_int), (void *)&mat_dst[1].step));
217                 args.push_back( make_pair( sizeof(cl_int), (void *)&mat_dst[1].offset));
218                 if(channels >= 3)
219                 {
220
221                     args.push_back( make_pair( sizeof(cl_mem), (void *)&mat_dst[2].data));
222                     args.push_back( make_pair( sizeof(cl_int), (void *)&mat_dst[2].step));
223                     args.push_back( make_pair( sizeof(cl_int), (void *)&mat_dst[2].offset));
224                 }
225                 if(channels >= 4)
226                 {
227                     args.push_back( make_pair( sizeof(cl_mem), (void *)&mat_dst[3].data));
228                     args.push_back( make_pair( sizeof(cl_int), (void *)&mat_dst[3].step));
229                     args.push_back( make_pair( sizeof(cl_int), (void *)&mat_dst[3].offset));
230                 }
231
232                 args.push_back( make_pair( sizeof(cl_int), (void *)&mat_src.rows));
233                 args.push_back( make_pair( sizeof(cl_int), (void *)&cols));
234                 args.push_back( make_pair( sizeof(cl_int), (void *)&dst_step1));
235
236                 openCLExecuteKernel(clCxt, &split_mat, kernelName, globalThreads, localThreads, args, channels, depth);
237             }
238             static void split(const oclMat &mat_src, oclMat *mat_dst)
239             {
240                 CV_Assert(mat_dst);
241
242                 int depth = mat_src.depth();
243                 int num_channels = mat_src.oclchannels();
244                 Size size = mat_src.size();
245
246                 if(num_channels == 1)
247                 {
248                     mat_src.copyTo(mat_dst[0]);
249                     return;
250                 }
251
252                 int i;
253                 for(i = 0; i < num_channels; i++)
254                     mat_dst[i].create(size, CV_MAKETYPE(depth, 1));
255
256                 split_vector_run(mat_src, mat_dst);
257             }
258         }
259     }
260 }
261
262 void cv::ocl::merge(const oclMat *src, size_t n, oclMat &dst)
263 {
264     split_merge::merge(src, n, dst);
265 }
266 void cv::ocl::merge(const vector<oclMat> &src, oclMat &dst)
267 {
268     split_merge::merge(&src[0], src.size(), dst);
269 }
270
271 void cv::ocl::split(const oclMat &src, oclMat *dst)
272 {
273     split_merge::split(src, dst);
274 }
275 void cv::ocl::split(const oclMat &src, vector<oclMat> &dst)
276 {
277     dst.resize(src.oclchannels());
278     if(src.oclchannels() > 0)
279         split_merge::split(src, &dst[0]);
280 }