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) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
23 // * Redistribution's in binary form must reproduce the above copyright notice,
24 // this list of conditions and the following disclaimer in the documentation
25 // and/or other materials provided with the distribution.
27 // * The name of the copyright holders may not be used to endorse or promote products
28 // derived from this software without specific prior written permission.
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
43 #include "precomp.hpp"
45 #ifdef HAVE_CONCURRENCY
47 #elif defined HAVE_OPENMP
49 #elif defined HAVE_GCD
50 # include <dispatch/dispatch.h>
51 #elif defined HAVE_TBB
52 # include "tbb/tbb_stddef.h"
53 # if TBB_VERSION_MAJOR*100 + TBB_VERSION_MINOR >= 202
55 # include "tbb/task.h"
60 # endif // end TBB version
61 #endif // HAVE_CONCURRENCY
66 HAVE_OPENMP - using OpenMP
67 HAVE_CONCURRENCY - using visual studio 2010 concurrency
72 ParallelLoopBody::~ParallelLoopBody() { }
75 class TbbProxyLoopBody
78 TbbProxyLoopBody(const ParallelLoopBody& _body) :
82 void operator ()(const tbb::blocked_range<int>& range) const
84 body->operator()(Range(range.begin(), range.end()));
88 const ParallelLoopBody* body;
90 #endif // end HAVE_TBB
94 void block_function(void* context, size_t index)
96 ParallelLoopBody* ptr_body = static_cast<ParallelLoopBody*>(context);
97 ptr_body->operator()(Range(index, index + 1));
101 void parallel_for_(const Range& range, const ParallelLoopBody& body)
105 tbb::parallel_for(tbb::blocked_range<int>(range.start, range.end), TbbProxyLoopBody(body));
107 #elif defined HAVE_CONCURRENCY
109 Concurrency::parallel_for(range.start, range.end, body);
111 #elif defined HAVE_OPENMP
113 #pragma omp parallel for schedule(dynamic)
114 for (int i = range.start; i < range.end; ++i)
115 body(Range(i, i + 1));
117 #elif defined (HAVE_GCD)
119 dispatch_queue_t concurrent_queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
120 dispatch_apply_f(range.end - range.start, concurrent_queue, &const_cast<ParallelLoopBody&>(body), block_function);
126 #endif // end HAVE_TBB