1 | /* |
2 | * Copyright (C) 2011 University of Szeged |
3 | * Copyright (C) 2011 Gabor Loki <[email protected]> |
4 | * All rights reserved. |
5 | * |
6 | * Redistribution and use in source and binary forms, with or without |
7 | * modification, are permitted provided that the following conditions |
8 | * are met: |
9 | * 1. Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * 2. Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
14 | * |
15 | * THIS SOFTWARE IS PROVIDED BY UNIVERSITY OF SZEGED ``AS IS'' AND ANY |
16 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
18 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL UNIVERSITY OF SZEGED OR |
19 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
20 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
22 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
23 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | */ |
27 | |
28 | #pragma once |
29 | |
30 | #include <wtf/Assertions.h> |
31 | #include <wtf/Vector.h> |
32 | |
33 | // Usage: |
34 | // |
35 | // // Initialize parallel jobs |
36 | // ParallelJobs<TypeOfParameter> parallelJobs(&worker [, requestedNumberOfJobs]); |
37 | // |
38 | // // Fill the parameter array |
39 | // for(i = 0; i < parallelJobs.numberOfJobs(); ++i) { |
40 | // TypeOfParameter& params = parallelJobs.parameter(i); |
41 | // params.attr1 = localVars ... |
42 | // ... |
43 | // } |
44 | // |
45 | // // Execute parallel jobs |
46 | // parallelJobs.execute(); |
47 | // |
48 | |
49 | #if ENABLE(THREADING_GENERIC) |
50 | #include <wtf/ParallelJobsGeneric.h> |
51 | |
52 | #elif ENABLE(THREADING_OPENMP) |
53 | #include <wtf/ParallelJobsOpenMP.h> |
54 | |
55 | #elif ENABLE(THREADING_LIBDISPATCH) |
56 | #include <wtf/ParallelJobsLibdispatch.h> |
57 | |
58 | #else |
59 | #error "No parallel processing API for ParallelJobs" |
60 | |
61 | #endif |
62 | |
63 | namespace WTF { |
64 | |
65 | template<typename Type> |
66 | class ParallelJobs { |
67 | WTF_MAKE_FAST_ALLOCATED; |
68 | public: |
69 | typedef void (*WorkerFunction)(Type*); |
70 | |
71 | ParallelJobs(WorkerFunction func, int requestedJobNumber) : |
72 | m_parallelEnvironment(reinterpret_cast<ParallelEnvironment::ThreadFunction>(func), sizeof(Type), requestedJobNumber) |
73 | { |
74 | m_parameters.grow(m_parallelEnvironment.numberOfJobs()); |
75 | ASSERT(numberOfJobs() == m_parameters.size()); |
76 | } |
77 | |
78 | size_t numberOfJobs() |
79 | { |
80 | return m_parameters.size(); |
81 | } |
82 | |
83 | Type& parameter(size_t i) |
84 | { |
85 | return m_parameters[i]; |
86 | } |
87 | |
88 | void execute() |
89 | { |
90 | m_parallelEnvironment.execute(reinterpret_cast<unsigned char*>(m_parameters.data())); |
91 | } |
92 | |
93 | private: |
94 | ParallelEnvironment m_parallelEnvironment; |
95 | Vector<Type> m_parameters; |
96 | }; |
97 | |
98 | } // namespace WTF |
99 | |
100 | using WTF::ParallelJobs; |
101 | |