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 materials 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"
47 #include "opencl_kernels.hpp"
50 using namespace cv::ocl;
58 static void merge_vector_run(const oclMat *mat_src, size_t n, oclMat &mat_dst)
60 if(!mat_dst.clCxt->supportsFeature(FEATURE_CL_DOUBLE) && mat_dst.type() == CV_64F)
62 CV_Error(CV_OpenCLDoubleNotSupported, "Selected device doesn't support double");
66 Context *clCxt = mat_dst.clCxt;
67 int channels = mat_dst.oclchannels();
68 int depth = mat_dst.depth();
70 string kernelName = "merge_vector";
72 int vector_lengths[4][7] = {{0, 0, 0, 0, 0, 0, 0},
73 {2, 2, 1, 1, 1, 1, 1},
74 {4, 4, 2, 2 , 1, 1, 1},
78 size_t vector_length = vector_lengths[channels - 1][depth];
79 int offset_cols = (mat_dst.offset / mat_dst.elemSize()) & (vector_length - 1);
80 int cols = divUp(mat_dst.cols + offset_cols, vector_length);
82 size_t localThreads[3] = { 64, 4, 1 };
83 size_t globalThreads[3] = { cols, mat_dst.rows, 1 };
85 int dst_step1 = mat_dst.cols * mat_dst.elemSize();
86 vector<pair<size_t , const void *> > args;
87 args.push_back( make_pair( sizeof(cl_mem), (void *)&mat_dst.data));
88 args.push_back( make_pair( sizeof(cl_int), (void *)&mat_dst.step));
89 args.push_back( make_pair( sizeof(cl_int), (void *)&mat_dst.offset));
90 args.push_back( make_pair( sizeof(cl_mem), (void *)&mat_src[0].data));
91 args.push_back( make_pair( sizeof(cl_int), (void *)&mat_src[0].step));
92 args.push_back( make_pair( sizeof(cl_int), (void *)&mat_src[0].offset));
93 args.push_back( make_pair( sizeof(cl_mem), (void *)&mat_src[1].data));
94 args.push_back( make_pair( sizeof(cl_int), (void *)&mat_src[1].step));
95 args.push_back( make_pair( sizeof(cl_int), (void *)&mat_src[1].offset));
99 args.push_back( make_pair( sizeof(cl_mem), (void *)&mat_src[2].data));
100 args.push_back( make_pair( sizeof(cl_int), (void *)&mat_src[2].step));
101 args.push_back( make_pair( sizeof(cl_int), (void *)&mat_src[2].offset));
105 args.push_back( make_pair( sizeof(cl_mem), (void *)&mat_src[2].data));
106 args.push_back( make_pair( sizeof(cl_int), (void *)&mat_src[2].step));
107 args.push_back( make_pair( sizeof(cl_int), (void *)&mat_src[2].offset));
111 args.push_back( make_pair( sizeof(cl_mem), (void *)&mat_src[3].data));
112 args.push_back( make_pair( sizeof(cl_int), (void *)&mat_src[3].step));
113 args.push_back( make_pair( sizeof(cl_int), (void *)&mat_src[3].offset));
117 args.push_back( make_pair( sizeof(cl_int), (void *)&mat_dst.rows));
118 args.push_back( make_pair( sizeof(cl_int), (void *)&cols));
119 args.push_back( make_pair( sizeof(cl_int), (void *)&dst_step1));
121 openCLExecuteKernel(clCxt, &merge_mat, kernelName, globalThreads, localThreads, args, channels, depth);
123 static void merge(const oclMat *mat_src, size_t n, oclMat &mat_dst)
128 int depth = mat_src[0].depth();
129 Size size = mat_src[0].size();
131 int total_channels = 0;
133 for(size_t i = 0; i < n; ++i)
135 CV_Assert(depth == mat_src[i].depth());
136 CV_Assert(size == mat_src[i].size());
138 total_channels += mat_src[i].oclchannels();
141 CV_Assert(total_channels <= 4);
143 if(total_channels == 1)
145 mat_src[0].copyTo(mat_dst);
149 mat_dst.create(size, CV_MAKETYPE(depth, total_channels));
150 merge_vector_run(mat_src, n, mat_dst);
152 static void split_vector_run(const oclMat &src, oclMat *dst)
155 if(!src.clCxt->supportsFeature(FEATURE_CL_DOUBLE) && src.type() == CV_64F)
157 CV_Error(CV_OpenCLDoubleNotSupported, "Selected device doesn't support double");
161 Context *clCtx = src.clCxt;
162 int channels = src.channels();
163 int depth = src.depth();
164 depth = (depth == CV_8S) ? CV_8U : depth;
165 depth = (depth == CV_16S) ? CV_16U : depth;
167 string kernelName = "split_vector";
171 vector<pair<size_t , const void *> > args;
172 args.push_back( make_pair( sizeof(cl_mem), (void *)&src.data));
173 args.push_back( make_pair( sizeof(cl_int), (void *)&src.step));
174 int srcOffsetXBytes = src.offset % src.step;
175 int srcOffsetY = src.offset / src.step;
176 cl_int2 srcOffset = {{srcOffsetXBytes, srcOffsetY}};
177 args.push_back( make_pair( sizeof(cl_int2), (void *)&srcOffset));
179 bool dst0Aligned = false, dst1Aligned = false, dst2Aligned = false, dst3Aligned = false;
180 int alignSize = dst[0].elemSize1() * VEC_SIZE;
181 int alignMask = alignSize - 1;
183 args.push_back( make_pair( sizeof(cl_mem), (void *)&dst[0].data));
184 args.push_back( make_pair( sizeof(cl_int), (void *)&dst[0].step));
185 int dst0OffsetXBytes = dst[0].offset % dst[0].step;
186 int dst0OffsetY = dst[0].offset / dst[0].step;
187 cl_int2 dst0Offset = {{dst0OffsetXBytes, dst0OffsetY}};
188 args.push_back( make_pair( sizeof(cl_int2), (void *)&dst0Offset));
189 if ((dst0OffsetXBytes & alignMask) == 0)
192 args.push_back( make_pair( sizeof(cl_mem), (void *)&dst[1].data));
193 args.push_back( make_pair( sizeof(cl_int), (void *)&dst[1].step));
194 int dst1OffsetXBytes = dst[1].offset % dst[1].step;
195 int dst1OffsetY = dst[1].offset / dst[1].step;
196 cl_int2 dst1Offset = {{dst1OffsetXBytes, dst1OffsetY}};
197 args.push_back( make_pair( sizeof(cl_int2), (void *)&dst1Offset));
198 if ((dst1OffsetXBytes & alignMask) == 0)
201 // DON'T MOVE VARIABLES INTO 'IF' BODY
202 int dst2OffsetXBytes, dst2OffsetY;
204 int dst3OffsetXBytes, dst3OffsetY;
208 args.push_back( make_pair( sizeof(cl_mem), (void *)&dst[2].data));
209 args.push_back( make_pair( sizeof(cl_int), (void *)&dst[2].step));
210 dst2OffsetXBytes = dst[2].offset % dst[2].step;
211 dst2OffsetY = dst[2].offset / dst[2].step;
212 dst2Offset.s[0] = dst2OffsetXBytes; dst2Offset.s[1] = dst2OffsetY;
213 args.push_back( make_pair( sizeof(cl_int2), (void *)&dst2Offset));
214 if ((dst2OffsetXBytes & alignMask) == 0)
220 args.push_back( make_pair( sizeof(cl_mem), (void *)&dst[3].data));
221 args.push_back( make_pair( sizeof(cl_int), (void *)&dst[3].step));
222 dst3OffsetXBytes = dst[3].offset % dst[3].step;
223 dst3OffsetY = dst[3].offset / dst[3].step;
224 dst3Offset.s[0] = dst3OffsetXBytes; dst3Offset.s[1] = dst3OffsetY;
225 args.push_back( make_pair( sizeof(cl_int2), (void *)&dst3Offset));
226 if ((dst3OffsetXBytes & alignMask) == 0)
230 cl_int2 size = {{ src.cols, src.rows }};
231 args.push_back( make_pair( sizeof(cl_int2), (void *)&size));
233 string build_options =
234 cv::format("-D VEC_SIZE=%d -D DATA_DEPTH=%d -D DATA_CHAN=%d",
235 (int)VEC_SIZE, depth, channels);
238 build_options += " -D DST0_ALIGNED";
240 build_options += " -D DST1_ALIGNED";
242 build_options += " -D DST2_ALIGNED";
244 build_options += " -D DST3_ALIGNED";
246 const DeviceInfo& devInfo = clCtx->getDeviceInfo();
248 // TODO Workaround for issues. Need to investigate a problem.
250 && devInfo.deviceType == CVCL_DEVICE_TYPE_CPU
251 && devInfo.platform->platformVendor.find("Intel") != std::string::npos
252 && (devInfo.deviceVersion.find("Build 56860") != std::string::npos
253 || devInfo.deviceVersion.find("Build 76921") != std::string::npos))
254 build_options += " -D BYPASS_VSTORE=true";
256 size_t globalThreads[3] = { divUp(src.cols, VEC_SIZE), src.rows, 1 };
257 openCLExecuteKernel(clCtx, &split_mat, kernelName, globalThreads, NULL, args, -1, -1, build_options.c_str());
259 static void split(const oclMat &mat_src, oclMat *mat_dst)
263 int depth = mat_src.depth();
264 int num_channels = mat_src.channels();
265 Size size = mat_src.size();
267 if (num_channels == 1)
269 mat_src.copyTo(mat_dst[0]);
273 for (int i = 0; i < mat_src.oclchannels(); i++)
274 mat_dst[i].create(size, CV_MAKETYPE(depth, 1));
276 split_vector_run(mat_src, mat_dst);
282 void cv::ocl::merge(const oclMat *src, size_t n, oclMat &dst)
284 split_merge::merge(src, n, dst);
286 void cv::ocl::merge(const vector<oclMat> &src, oclMat &dst)
288 split_merge::merge(&src[0], src.size(), dst);
291 void cv::ocl::split(const oclMat &src, oclMat *dst)
293 split_merge::split(src, dst);
295 void cv::ocl::split(const oclMat &src, vector<oclMat> &dst)
297 dst.resize(src.oclchannels()); // TODO Why oclchannels?
298 if(src.oclchannels() > 0)
299 split_merge::split(src, &dst[0]);