4274caf340b8ed932528816cbcd2ae679a376ee5
[profile/ivi/opencv.git] / modules / core / src / parallel.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) 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.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 //   * Redistribution's of source code must retain the above copyright notice,
21 //     this list of conditions and the following disclaimer.
22 //
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.
26 //
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.
29 //
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.
40 //
41 //M*/
42
43 #include "precomp.hpp"
44
45 #ifdef HAVE_CONCURRENCY
46 #  include <ppl.h>
47 #elif defined HAVE_OPENMP
48 #  include <omp.h>
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
54 #    include "tbb/tbb.h"
55 #    include "tbb/task.h"
56 #    undef min
57 #    undef max
58 #  else
59 #    undef HAVE_TBB
60 #  endif // end TBB version
61 #endif // HAVE_CONCURRENCY
62
63 /*
64     HAVE_TBB - using TBB
65     HAVE_GCD - using GCD
66     HAVE_OPENMP - using OpenMP
67     HAVE_CONCURRENCY - using visual studio 2010 concurrency
68 */
69
70 namespace cv
71 {
72     ParallelLoopBody::~ParallelLoopBody() { }
73
74 #ifdef HAVE_TBB
75     class TbbProxyLoopBody
76     {
77     public:
78         TbbProxyLoopBody(const ParallelLoopBody& _body) :
79             body(&_body)
80         { }
81
82         void operator ()(const tbb::blocked_range<int>& range) const
83         {
84             body->operator()(Range(range.begin(), range.end()));
85         }
86
87     private:
88         const ParallelLoopBody* body;
89     };
90 #endif // end HAVE_TBB
91
92 #ifdef HAVE_GCD
93     static
94     void block_function(void* context, size_t index)
95     {
96         ParallelLoopBody* ptr_body = static_cast<ParallelLoopBody*>(context);
97         ptr_body->operator()(Range(index, index + 1));
98     }
99 #endif // HAVE_GCD
100
101     void parallel_for_(const Range& range, const ParallelLoopBody& body)
102     {
103 #ifdef HAVE_TBB
104
105         tbb::parallel_for(tbb::blocked_range<int>(range.start, range.end), TbbProxyLoopBody(body));
106
107 #elif defined HAVE_CONCURRENCY
108
109         Concurrency::parallel_for(range.start, range.end, body);
110
111 #elif defined HAVE_OPENMP
112
113 #pragma omp parallel for schedule(dynamic)
114         for (int i = range.start; i < range.end; ++i)
115             body(Range(i, i + 1));
116
117 #elif defined (HAVE_GCD)
118
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);
121
122 #else
123
124         body(range);
125
126 #endif // end HAVE_TBB
127     }
128
129 } // namespace cv