1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11 // For Open Source Computer Vision Library
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.
18 // Jia Haipeng, jiahaipeng95@gmail.com
20 // Redistribution and use in source and binary forms, with or without modification,
21 // are permitted provided that the following conditions are met:
23 // * Redistribution's of source code must retain the above copyright notice,
24 // this list of conditions and the following disclaimer.
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.
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.
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.
46 #include "precomp.hpp"
50 using namespace cv::ocl;
52 ////////////////////////////////////////////////////////////////////////
53 ///////////////// oclMat merge and split ///////////////////////////////
54 ////////////////////////////////////////////////////////////////////////
60 ///////////////////////////OpenCL kernel strings///////////////////////////
61 extern const char *merge_mat;
62 extern const char *split_mat;
71 ///////////////////////////////////////////////////////////
72 ///////////////common/////////////////////////////////////
73 /////////////////////////////////////////////////////////
74 inline int divUp(int total, int grain)
76 return (total + grain - 1) / grain;
78 ////////////////////////////////////////////////////////////////////////////
79 ////////////////////merge//////////////////////////////////////////////////
80 ////////////////////////////////////////////////////////////////////////////
81 // static void merge_vector_run_no_roi(const oclMat *mat_src, size_t n, oclMat &mat_dst)
83 // Context *clCxt = mat_dst.clCxt;
84 // int channels = mat_dst.oclchannels();
85 // int depth = mat_dst.depth();
87 // String kernelName = "merge_vector";
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}
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],
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));
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));
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));
123 // openCLExecuteKernel(clCxt, &merge_mat, kernelName, globalThreads, localThreads, args, channels, depth);
126 static void merge_vector_run(const oclMat *mat_src, size_t n, oclMat &mat_dst)
128 if(!mat_dst.clCxt->supportsFeature(Context::CL_DOUBLE) && mat_dst.type() == CV_64F)
130 CV_Error(Error::GpuNotSupported, "Selected device don't support double\r\n");
134 Context *clCxt = mat_dst.clCxt;
135 int channels = mat_dst.oclchannels();
136 int depth = mat_dst.depth();
138 String kernelName = "merge_vector";
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}
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);
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],
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));
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));
174 // if channel == 3, then the matrix will convert to channel =4
176 // args.push_back( std::make_pair( sizeof(cl_int), (void *)&offset_cols));
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));
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));
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));
196 openCLExecuteKernel(clCxt, &merge_mat, kernelName, globalThreads, localThreads, args, channels, depth);
198 static void merge(const oclMat *mat_src, size_t n, oclMat &mat_dst)
203 int depth = mat_src[0].depth();
204 Size size = mat_src[0].size();
206 int total_channels = 0;
208 for(size_t i = 0; i < n; ++i)
210 CV_Assert(depth == mat_src[i].depth());
211 CV_Assert(size == mat_src[i].size());
213 total_channels += mat_src[i].oclchannels();
216 CV_Assert(total_channels <= 4);
218 if(total_channels == 1)
220 mat_src[0].copyTo(mat_dst);
224 mat_dst.create(size, CV_MAKETYPE(depth, total_channels));
225 merge_vector_run(mat_src, n, mat_dst);
227 ////////////////////////////////////////////////////////////////////////////////////////////////////
228 //////////////////////////////////////split/////////////////////////////////////////////////////////////
229 //////////////////////////////////////////////////////////////////////////////////////////////////
230 // static void split_vector_run_no_roi(const oclMat &mat_src, oclMat *mat_dst)
232 // Context *clCxt = mat_src.clCxt;
233 // int channels = mat_src.oclchannels();
234 // int depth = mat_src.depth();
236 // String kernelName = "split_vector";
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}
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],
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));
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));
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));
272 // openCLExecuteKernel(clCxt, &split_mat, kernelName, globalThreads, localThreads, args, channels, depth);
274 static void split_vector_run(const oclMat &mat_src, oclMat *mat_dst)
277 if(!mat_src.clCxt->supportsFeature(Context::CL_DOUBLE) && mat_src.type() == CV_64F)
279 CV_Error(Error::GpuNotSupported, "Selected device don't support double\r\n");
283 Context *clCxt = mat_src.clCxt;
284 int channels = mat_src.oclchannels();
285 int depth = mat_src.depth();
287 String kernelName = "split_vector";
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}
295 size_t vector_length = vector_lengths[channels - 1][mat_dst[0].depth()];
297 int max_offset_cols = 0;
298 for(int i = 0; i < channels; i++)
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;
305 int cols = vector_length == 1 ? divUp(mat_src.cols, vector_length)
306 : divUp(mat_src.cols + max_offset_cols, vector_length);
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
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));
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));
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));
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));
342 openCLExecuteKernel(clCxt, &split_mat, kernelName, globalThreads, localThreads, args, channels, depth);
344 static void split(const oclMat &mat_src, oclMat *mat_dst)
348 int depth = mat_src.depth();
349 int num_channels = mat_src.oclchannels();
350 Size size = mat_src.size();
352 if(num_channels == 1)
354 mat_src.copyTo(mat_dst[0]);
359 for(i = 0; i < num_channels; i++)
360 mat_dst[i].create(size, CV_MAKETYPE(depth, 1));
362 split_vector_run(mat_src, mat_dst);
368 void cv::ocl::merge(const oclMat *src, size_t n, oclMat &dst)
370 split_merge::merge(src, n, dst);
372 void cv::ocl::merge(const std::vector<oclMat> &src, oclMat &dst)
374 split_merge::merge(&src[0], src.size(), dst);
377 void cv::ocl::split(const oclMat &src, oclMat *dst)
379 split_merge::split(src, dst);
381 void cv::ocl::split(const oclMat &src, std::vector<oclMat> &dst)
383 dst.resize(src.oclchannels());
384 if(src.oclchannels() > 0)
385 split_merge::split(src, &dst[0]);