00001 /*************************************************************************** 00002 * Copyright (C) 2005-2011 LpzRobots development team * 00003 * Georg Martius <georg dot martius at web dot de> * 00004 * Frank Guettler <guettler at informatik dot uni-leipzig dot de * 00005 * Frank Hesse <frank at nld dot ds dot mpg dot de> * 00006 * Ralf Der <ralfder at mis dot mpg dot de> * 00007 * * 00008 * This program is free software; you can redistribute it and/or modify * 00009 * it under the terms of the GNU General Public License as published by * 00010 * the Free Software Foundation; either version 2 of the License, or * 00011 * (at your option) any later version. * 00012 * * 00013 * This program is distributed in the hope that it will be useful, * 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00016 * GNU General Public License for more details. * 00017 * * 00018 * You should have received a copy of the GNU General Public License * 00019 * along with this program; if not, write to the * 00020 * Free Software Foundation, Inc., * 00021 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 00022 * * 00023 ***************************************************************************/ 00024 #ifndef _SIMULATIONTASKSUPERVISOR_H_ 00025 #define _SIMULATIONTASKSUPERVISOR_H_ 00026 00027 #include "simulationtask.h" 00028 #include <string> 00029 00030 namespace osg 00031 { 00032 class ArgumentParser; 00033 } 00034 00035 00036 namespace lpzrobots 00037 { 00038 00039 class LpzRobotsViewer; 00040 00041 class SimulationTaskSupervisor 00042 { 00043 public: 00044 00045 /** 00046 * Returns the singleton instance of this class. 00047 * There is no way to instantiate yourself this class. 00048 * @param simTaskHandle The global SimulationTaskHandle were you can put in your needed data. 00049 * This handle is shared by all parallel running simulations. 00050 * @return the singleton instance of this class 00051 */ 00052 static inline SimulationTaskSupervisor* getInstance() 00053 { 00054 if(singletonInstance==0) 00055 singletonInstance = new SimulationTaskSupervisor(); 00056 return singletonInstance; 00057 } 00058 00059 /** 00060 * Destroys the singleton instance of this class. 00061 */ 00062 static void destroyInstance() 00063 { 00064 if (singletonInstance!=0) 00065 { 00066 delete singletonInstance; 00067 singletonInstance=0; 00068 } 00069 } 00070 00071 static void setSimTaskHandle(SimulationTaskHandle& _simTaskHandle) 00072 { 00073 simTaskHandle= &_simTaskHandle; 00074 } 00075 00076 static void setTaskedSimCreator(TaskedSimulationCreator& _taskedSimCreator) 00077 { 00078 taskedSimCreator = &_taskedSimCreator; 00079 } 00080 00081 /** 00082 * Sets the number of total threads running at one time. 00083 * @param numberThreads 00084 */ 00085 static void setNumberThreads(int numberThreads); 00086 00087 /** 00088 * Sets the number of threads created per core. The default value is 1. 00089 * So if your machine has e.g. 4 cores, 4 threads are created. 00090 * If you have much code which must be synchronized, it may be 00091 * useful to increase the number of threads per core, 2 is a good value. 00092 * @param numberThreadsPerCore 00093 */ 00094 static void setNumberThreadsPerCore(int numberThreadsPerCore); 00095 00096 /** 00097 * Creates one SimulationTask with taskId=SimulationTaskHandle.simTaskList.size(). 00098 * @param argc count of arguments in argv 00099 * @param argv array of arguments, given to Simulation when the tasks starts 00100 */ 00101 virtual void createSimTask() 00102 { 00103 SimulationTask* simTask = new SimulationTask(simTaskList.size()); 00104 SimulationTaskSupervisor::simTaskList.push_back(simTask); 00105 } 00106 00107 /** 00108 * Same as createSimTask, but create more than one task at once. 00109 * taskIds assigned in ascending order. 00110 * @param taskCount number of tasks to create 00111 * @param argc count of arguments in argv 00112 * @param argv array of arguments, given to Simulation when the tasks starts 00113 */ 00114 virtual void createSimTasks(int taskCount) 00115 { 00116 for (int i=0; i<taskCount; i++) 00117 createSimTask(); 00118 } 00119 00120 /** 00121 * Runs all generated SimulationTasks. 00122 */ 00123 virtual void runSimTasks(int* argc, char** argv); 00124 00125 /** 00126 * Sets a suffix to be appended to the window name to identify your simTask 00127 */ 00128 virtual void setSimTaskNameSuffix(std::string name); 00129 00130 protected: 00131 00132 SimulationTaskSupervisor() {} 00133 00134 virtual ~SimulationTaskSupervisor() {} 00135 00136 static SimulationTaskSupervisor* singletonInstance; 00137 static SimulationTaskHandle* simTaskHandle; 00138 static TaskedSimulationCreator* taskedSimCreator; 00139 static std::vector<SimulationTask*> simTaskList; 00140 static int* argc; 00141 static char** argv; 00142 static osg::ArgumentParser* parser; 00143 static LpzRobotsViewer* viewer; 00144 static std::string nameSuffix; 00145 00146 00147 }; 00148 00149 } // end namespace lpzrobots 00150 00151 #endif /* _SIMULATIONTASKSUPERVISOR_H_ */