main.cpp 4.72 KB
Newer Older
1
/**
Weichen's avatar
Weichen committed
2 3
 * \file        main.cpp
 * \date        Jan 15, 2013
4
 * \version     v0.8.1
Mohcine Chraibi's avatar
Mohcine Chraibi committed
5
 * \copyright   <2009-2015> Forschungszentrum Jülich GmbH. All rights reserved.
Weichen's avatar
Weichen committed
6
 *
7
 * \section License
8 9 10
 * This file is part of JuPedSim.
 *
 * JuPedSim is free software: you can redistribute it and/or modify
Weichen's avatar
Weichen committed
11
 * it under the terms of the GNU Lesser General Public License as published by
12 13 14 15 16 17 18 19
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * JuPedSim is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
Weichen's avatar
Weichen committed
20 21
 * You should have received a copy of the GNU Lesser General Public License
 * along with JuPedSim. If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 * \section Description
Weichen's avatar
Weichen committed
24
 * main function
25
 *
Mohcine Chraibi's avatar
Mohcine Chraibi committed
26
 *
Weichen's avatar
Weichen committed
27 28
 **/

Ulrich Kemloh's avatar
Ulrich Kemloh committed
29 30
#include "geometry/Building.h"
#include "general/ArgumentParser.h"
31
#include "./Simulation.h"
32

GrgrLmml's avatar
GrgrLmml committed
33 34 35 36
#ifdef _JPS_AS_A_SERVICE

#include "hybrid/HybridSimulationManager.h"

37
#endif
38 39

#include <thread>
Mohcine Chraibi's avatar
Mohcine Chraibi committed
40

GrgrLmml's avatar
GrgrLmml committed
41
int main(int argc, char** argv)
42
{
GrgrLmml's avatar
GrgrLmml committed
43 44 45 46 47 48 49 50
    //gathering some statistics about the runtime
    time_t starttime, endtime;

    // default logger
    Log = new STDIOHandler();

    Configuration* configuration = new Configuration();
    // Parsing the arguments
51
    bool status = false;
52
    {
Mohcine Chraibi's avatar
Mohcine Chraibi committed
53 54
          //ArgumentParser* p = new ArgumentParser(configuration); //Memory Leak
          std::unique_ptr<ArgumentParser> p(new ArgumentParser(configuration));
55 56
          status = p->ParseArgs(argc, argv);
    }
GrgrLmml's avatar
GrgrLmml committed
57 58 59 60 61 62 63 64
#ifdef _JPS_AS_A_SERVICE
    if (configuration->GetRunAsService()) {
          std::shared_ptr<HybridSimulationManager> hybridSimulationManager = std::shared_ptr<HybridSimulationManager>(
                    new HybridSimulationManager(configuration));
          configuration->SetHybridSimulationManager(hybridSimulationManager);
          std::thread t = std::thread(&HybridSimulationManager::Start, hybridSimulationManager);
          hybridSimulationManager->WaitForScenarioLoaded();
          t.detach();
65
     }
GrgrLmml's avatar
GrgrLmml committed
66 67 68 69
#endif
    // create and initialize the simulation engine
    // Simulation
    time(&starttime);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
70

GrgrLmml's avatar
GrgrLmml committed
71 72 73 74
    Simulation sim(configuration);

    if (status && sim.InitArgs()) {
        //evacuation time
75
        double evacTime = 0;
GrgrLmml's avatar
GrgrLmml committed
76 77 78 79 80 81 82 83 84 85 86 87
        Log->Write("INFO: \tStart runSimulation() with %d pedestrians", sim.GetPedsNumber());

#ifdef _JPS_AS_A_SERVICE

        if (configuration->GetRunAsService()) {
              configuration->GetHybridSimulationManager()->Run(sim);
         }
         else
#endif
        if (sim.GetAgentSrcManager().GetMaxAgentNumber()) {
            //Start the thread for managing the sources of agents if any
            //std::thread t1(sim.GetAgentSrcManager());
Mohcine Chraibi's avatar
Mohcine Chraibi committed
88
            double simMaxTime = configuration->GetTmax();
89
            std::thread t1(&AgentsSourcesManager::Run, &sim.GetAgentSrcManager());
90 91 92 93 94
            while(!sim.GetAgentSrcManager().IsRunning())
            {
                 // std::cout << "waiting...\n";
            }
           //main thread for the simulation
Mohcine Chraibi's avatar
Mohcine Chraibi committed
95
            evacTime = sim.RunStandardSimulation(simMaxTime);
GrgrLmml's avatar
GrgrLmml committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
            //Join the main thread
            t1.join();
        }
        else {
            //main thread for the simulation
            evacTime = sim.RunStandardSimulation(configuration->GetTmax());
        }

        Log->Write("\nINFO: \tEnd runSimulation()");
        time(&endtime);

        // some statistics output
        if (configuration->ShowStatistics()) {
            sim.PrintStatistics();
        }

        if (sim.GetPedsNumber()) {
            Log->Write("WARNING: Pedestrians not evacuated [%d] using [%d] threads",
                    sim.GetPedsNumber(), configuration->GetMaxOpenMPThreads());
        }

        double execTime = difftime(endtime, starttime);
        std::stringstream summary;
        summary << std::setprecision(2) << std::fixed;
        summary << "\nExec Time [s]     : " << execTime << std::endl;
        summary << "Evac Time [s]     : " << evacTime << std::endl;
        summary << "Realtime Factor   : " << evacTime/execTime << " X " << std::endl;
        summary << "Number of Threads : " << configuration->GetMaxOpenMPThreads() << std::endl;
        summary << "Warnings          : " << Log->GetWarnings() << std::endl;
        summary << "Errors            : " << Log->GetErrors() << std::endl;
        Log->Write(summary.str().c_str());

        //force an output to the screen if the log is not the standard output
        if (nullptr==dynamic_cast<STDIOHandler*>(Log)) {
            printf("%s\n", summary.str().c_str());
        }
    }
    else {
        Log->Write("INFO:\tFinishing...");
    }
    // do the last cleaning
    delete configuration;
    delete Log;

    return (EXIT_SUCCESS);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
141
}