base/ThreadedLoopMacros.hpp
Go to the documentation of this file.
00001 /*
00002 
00003   This file is part of OpenFLUID software
00004   Copyright(c) 2007, INRA - Montpellier SupAgro
00005 
00006 
00007  == GNU General Public License Usage ==
00008 
00009   OpenFLUID is free software: you can redistribute it and/or modify
00010   it under the terms of the GNU General Public License as published by
00011   the Free Software Foundation, either version 3 of the License, or
00012   (at your option) any later version.
00013 
00014   OpenFLUID is distributed in the hope that it will be useful,
00015   but WITHOUT ANY WARRANTY; without even the implied warranty of
00016   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017   GNU General Public License for more details.
00018 
00019   You should have received a copy of the GNU General Public License
00020   along with OpenFLUID. If not, see <http://www.gnu.org/licenses/>.
00021 
00022 
00023  == Other Usage ==
00024 
00025   Other Usage means a use of OpenFLUID that is inconsistent with the GPL
00026   license, and requires a written agreement between You and INRA.
00027   Licensees for Other Usage of OpenFLUID may use this file in accordance
00028   with the terms contained in the written agreement between You and INRA.
00029   
00030 */
00031 
00032 
00033 /**
00034   \file LoopMacros.hpp
00035   \brief Header of ...
00036 
00037   \author Jean-Christophe FABRE <fabrejc@supagro.inra.fr>
00038  */
00039 
00040 
00041 #ifndef __THREADEDLOOPMACROS_HPP__
00042 #define __THREADEDLOOPMACROS_HPP__
00043 
00044 #include <QtGlobal>
00045 
00046 #if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
00047 #include <QtConcurrent>
00048 #endif
00049 
00050 #include <QtConcurrentRun>
00051 #include <QFutureSynchronizer>
00052 
00053 #include <openfluid/base/LoopMacros.hpp>
00054 
00055 // =====================================================================
00056 // =====================================================================
00057 
00058 
00059 
00060 #define _THREADSYNCID(_id) _M_##_id##_Sync
00061 
00062 
00063 #define _APPLY_UNITS_ORDERED_LOOP_THREADED_WITHID(id,unitclass,funcptr,...) \
00064   openfluid::core::UnitsList_t* _UNITSLISTID(id) = mp_CoreData->getUnits(unitclass)->getList(); \
00065   if (_UNITSLISTID(id) != NULL) \
00066   { \
00067     openfluid::core::UnitsList_t::iterator _UNITSLISTITERID(id) = _UNITSLISTID(id)->begin(); \
00068     if (_UNITSLISTITERID(id) != _UNITSLISTID(id)->end()) \
00069     { \
00070       openfluid::core::PcsOrd_t _PCSORDID(id) = _UNITSLISTITERID(id)->getProcessOrder(); \
00071       while (_UNITSLISTITERID(id) != _UNITSLISTID(id)->end()) \
00072       { \
00073         QFutureSynchronizer<void> _THREADSYNCID(id); \
00074         while (_UNITSLISTITERID(id) != _UNITSLISTID(id)->end() && _UNITSLISTITERID(id)->getProcessOrder() == _PCSORDID(id)) \
00075         { \
00076           try \
00077           { \
00078             _THREADSYNCID(id).addFuture(QtConcurrent::run(boost::bind(&funcptr,this,&(*_UNITSLISTITERID(id)),## __VA_ARGS__)));\
00079             if (_THREADSYNCID(id).futures().size() == OPENFLUID_GetSimulatorMaxThreads())\
00080             { \
00081               _THREADSYNCID(id).waitForFinished(); \
00082               _THREADSYNCID(id).clearFutures(); \
00083             }\
00084           }\
00085           catch(QtConcurrent::UnhandledException& E) \
00086           { \
00087             throw openfluid::base::FrameworkException("Unknown exception thrown from compute thread"); \
00088           } \
00089           ++_UNITSLISTITERID(id); \
00090         } \
00091         _THREADSYNCID(id).waitForFinished(); \
00092         _THREADSYNCID(id).clearFutures(); \
00093         if (_UNITSLISTITERID(id) != _UNITSLISTID(id)->end()) _PCSORDID(id) = _UNITSLISTITERID(id)->getProcessOrder(); \
00094       } \
00095     } \
00096   }
00097 
00098 /**
00099   Macro for applying a threaded simulator to each unit of a class, following their process order
00100   @param[in] unitclass name of the unit class
00101   @param[in] funcptr member simulator name
00102   @param[in] ... extra parameters to pass to the member simulator
00103 */
00104 #define APPLY_UNITS_ORDERED_LOOP_THREADED(unitclass,funcptr,...) \
00105     _APPLY_UNITS_ORDERED_LOOP_THREADED_WITHID(__LINE__,unitclass,funcptr,## __VA_ARGS__)
00106 
00107 
00108 
00109 #define _APPLY_ALLUNITS_ORDERED_LOOP_THREADED_WITHID(id,funcptr,...) \
00110   openfluid::core::UnitsPtrList_t* _UNITSPTRLISTID(id) = mp_CoreData->getUnitsGlobally(); \
00111   if (_UNITSPTRLISTID(id) != NULL) \
00112   { \
00113     openfluid::core::UnitsPtrList_t::iterator _UNITSPTRLISTITERID(id) = _UNITSPTRLISTID(id)->begin(); \
00114     if (_UNITSPTRLISTITERID(id) != _UNITSPTRLISTID(id)->end()) \
00115     { \
00116       openfluid::core::PcsOrd_t _PCSORDID(id) = (*_UNITSPTRLISTITERID(id))->getProcessOrder(); \
00117       while (_UNITSPTRLISTITERID(id) != _UNITSPTRLISTID(id)->end()) \
00118       { \
00119         QFutureSynchronizer<void> _THREADSYNCID(id); \
00120         while (_UNITSPTRLISTITERID(id) != _UNITSPTRLISTID(id)->end() && (*_UNITSPTRLISTITERID(id))->getProcessOrder() == _PCSORDID(id)) \
00121         { \
00122           try \
00123           { \
00124             _THREADSYNCID(id).addFuture(QtConcurrent::run(boost::bind(&funcptr,this,(*_UNITSPTRLISTITERID(id)),## __VA_ARGS__)));\
00125             if (_THREADSYNCID(id).futures().size() == OPENFLUID_GetSimulatorMaxThreads())\
00126             { \
00127               _THREADSYNCID(id).waitForFinished(); \
00128               _THREADSYNCID(id).clearFutures(); \
00129             }\
00130           }\
00131           catch(QtConcurrent::UnhandledException& E) \
00132           { \
00133             throw openfluid::base::FrameworkException("Unknown exception thrown from compute thread"); \
00134           } \
00135           ++_UNITSPTRLISTITERID(id); \
00136         } \
00137         _THREADSYNCID(id).waitForFinished(); \
00138         _THREADSYNCID(id).clearFutures(); \
00139         if (_UNITSPTRLISTITERID(id) != _UNITSPTRLISTID(id)->end()) _PCSORDID(id) = (*_UNITSPTRLISTITERID(id))->getProcessOrder(); \
00140       } \
00141     } \
00142   }
00143 
00144 /**
00145   Macro for applying a threaded simulator to each unit of the domain, following their process order
00146   @param[in] funcptr member simulator name
00147   @param[in] ... extra parameters to pass to the member simulator
00148 */
00149 #define APPLY_ALLUNITS_ORDERED_LOOP_THREADED(funcptr,...) \
00150     _APPLY_ALLUNITS_ORDERED_LOOP_THREADED_WITHID(__LINE__,funcptr,## __VA_ARGS__)
00151 
00152 
00153 
00154 
00155 // =====================================================================
00156 // =====================================================================
00157 
00158 
00159 #endif /* __THREADEDLOOPMACROS_HPP__ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines