iLand
threadrunner.h
Go to the documentation of this file.
1/********************************************************************************************
2** iLand - an individual based forest landscape and disturbance model
3** http://iland-model.org
4** Copyright (C) 2009- Werner Rammer, Rupert Seidl
5**
6** This program is free software: you can redistribute it and/or modify
7** it under the terms of the GNU General Public License as published by
8** the Free Software Foundation, either version 3 of the License, or
9** (at your option) any later version.
10**
11** This program is distributed in the hope that it will be useful,
12** but WITHOUT ANY WARRANTY; without even the implied warranty of
13** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14** GNU General Public License for more details.
15**
16** You should have received a copy of the GNU General Public License
17** along with this program. If not, see <http://www.gnu.org/licenses/>.
18********************************************************************************************/
19
20#ifndef THREADRUNNER_H
21#define THREADRUNNER_H
22#include <QList>
23#include <QtConcurrent/QtConcurrent>
24class ResourceUnit;
25class Species;
27{
28public:
30 ThreadRunner(const QList<Species*> &speciesList) { setup(speciesList);}
31
32 void setup(const QList<ResourceUnit*> &resourceUnitList);
33 void setup(const QList<Species*> &speciesList) { mSpeciesMap = speciesList; }
34 // access
35 bool multithreading() const { return mMultithreaded; }
36 void setMultithreading(const bool do_multithreading) { mMultithreaded = do_multithreading; }
37 void print();
38 // actions
39 void run( void (*funcptr)(ResourceUnit*), const bool forceSingleThreaded=false ) const;
40 void run( void (*funcptr)(Species*), const bool forceSingleThreaded=false ) const;
41 // run over elements of a vector of type T
42 template<class T> void run(T* (*funcptr)(T*), const QVector<T*> &container, const bool forceSingleThreaded=false) const;
43 template<class T> void run(void (*funcptr)(T&), QVector<T> &container, const bool forceSingleThreaded=false) const;
44 // run over chunks of a larger array (or grid)
45 template<class T> void runGrid(void (*funcptr)(T*, T*), T* begin, T* end, const bool forceSingleThreaded=false, int minsize=10000, int maxchunks=10000) const;
46private:
47 QList<ResourceUnit*> mMap1, mMap2;
48 QList<Species*> mSpeciesMap;
49 static bool mMultithreaded;
50};
51
52template<class T>
53void ThreadRunner::runGrid(void (*funcptr)(T *, T*), T *begin, T *end, const bool forceSingleThreaded, int minsize, int maxchunks) const
54{
55 int length = end - begin; // # of elements
56 if (mMultithreaded && length>minsize*3 && forceSingleThreaded==false) {
57 // create multiple calls
58 int chunksize = minsize;
59 if (length > chunksize*maxchunks) {
60 chunksize = length / maxchunks;
61 }
62 // execute operations
63 T* p = begin;
64 while (p<end) {
65 T* pend = std::min(p+chunksize, end);
66 QtConcurrent::run(funcptr, p, pend);
67 p = pend;
68 }
69 } else {
70 // run all in one big function call
71 (*funcptr)(begin, end);
72 }
73}
74
75// multirunning function
76template<class T>
77void ThreadRunner::run(T *(*funcptr)(T *), const QVector<T *> &container, const bool forceSingleThreaded) const
78{
79 if (mMultithreaded && container.count() > 3 && forceSingleThreaded==false) {
80 // execute using QtConcurrent for larger amounts of elements
81 QtConcurrent::blockingMap(container,funcptr);
82 } else {
83 // execute serialized in main thread
84 T *element;
85 foreach(element, container)
86 (*funcptr)(element);
87 }
88
89}
90
91// multirunning function
92template<class T>
93void ThreadRunner::run(void (*funcptr)(T &), QVector<T> &container, const bool forceSingleThreaded) const
94{
95 if (mMultithreaded && container.count() > 3 && forceSingleThreaded==false) {
96 // execute using QtConcurrent for larger amounts of elements
97 QtConcurrent::blockingMap(container,funcptr);
98 } else {
99 // execute serialized in main thread
100 for (int i=0;i<container.size();++i)
101 (*funcptr)(container[i]);
102
103 }
104
105}
106
107#endif // THREADRUNNER_H
ResourceUnit is the spatial unit that encapsulates a forest stand and links to several environmental ...
Definition: resourceunit.h:49
The behavior and general properties of tree species.
Definition: species.h:75
Encapsulates the invokation of multiple threads for paralellized tasks.
Definition: threadrunner.h:27
void setMultithreading(const bool do_multithreading)
Definition: threadrunner.h:36
ThreadRunner(const QList< Species * > &speciesList)
Definition: threadrunner.h:30
void runGrid(void(*funcptr)(T *, T *), T *begin, T *end, const bool forceSingleThreaded=false, int minsize=10000, int maxchunks=10000) const
Definition: threadrunner.h:53
void setup(const QList< Species * > &speciesList)
Definition: threadrunner.h:33
void setup(const QList< ResourceUnit * > &resourceUnitList)
Definition: threadrunner.cpp:44
ThreadRunner()
Definition: threadrunner.cpp:33
bool multithreading() const
Definition: threadrunner.h:35
void run(void(*funcptr)(ResourceUnit *), const bool forceSingleThreaded=false) const
execute 'funcptr' for all resource units in parallel
Definition: threadrunner.cpp:60
void print()
print useful debug messages
Definition: threadrunner.cpp:38