Skip to content

Commit

Permalink
- fixed log and parallel writeOutput
Browse files Browse the repository at this point in the history
- parallel writeOutput is now enabled if boost::threads are found

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@22639 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Marcus Walther committed Oct 7, 2014
1 parent 8425086 commit 6ad1c0d
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 39 deletions.
9 changes: 5 additions & 4 deletions SimulationRuntime/cpp/CMakeLists.txt
Expand Up @@ -74,9 +74,9 @@ ENDIF(NOT ANALYZATION_MODE )


# Handle parallel output
IF(USE_PARALLEL_OUTPUT)
ADD_DEFINITIONS(-DUSE_PARALLEL_OUTPUT)
ENDIF(USE_PARALLEL_OUTPUT)
#IF(USE_PARALLEL_OUTPUT)
# ADD_DEFINITIONS(-DUSE_PARALLEL_OUTPUT)
#ENDIF(USE_PARALLEL_OUTPUT)


# Handle build type
Expand Down Expand Up @@ -168,7 +168,8 @@ SET(Boost_LIBRARIES_TMP ${Boost_LIBRARIES_TMP} ${Boost_LIBRARIES})
FIND_PACKAGE(Threads)
IF(Boost_THREAD_FOUND AND Boost_ATOMIC_FOUND)
ADD_DEFINITIONS(-DUSE_BOOST_THREAD)
MESSAGE(STATUS "Using boost thread")
ADD_DEFINITIONS(-DUSE_PARALLEL_OUTPUT)
MESSAGE(STATUS "Using boost thread and parallel output")
ELSE(Boost_THREAD_FOUND AND Boost_ATOMIC_FOUND)
MESSAGE(STATUS "Boost thread disabled")
ENDIF(Boost_THREAD_FOUND AND Boost_ATOMIC_FOUND)
Expand Down
1 change: 1 addition & 0 deletions SimulationRuntime/cpp/Core/SimController/SimController.cpp
Expand Up @@ -86,6 +86,7 @@ void SimController::Start(boost::shared_ptr<IMixedSystem> mixedsystem,SimSetting
global_settings->setSelectedNonLinSolver(simsettings.nonlinear_solver_name);
global_settings->setSelectedSolver(simsettings.solver_name);
global_settings->setOutputFormat(simsettings.outputFormat);
global_settings->setLogType(simsettings.logType);

global_settings->setAlarmTime(simsettings.timeOut);

Expand Down
50 changes: 22 additions & 28 deletions SimulationRuntime/cpp/Include/Core/DataExchange/Writer.h
@@ -1,6 +1,6 @@
#pragma once

#ifdef USE_PARALLEL_OUTPUT
#if defined USE_PARALLEL_OUTPUT && defined USE_BOOST_THREAD
#include <boost/lockfree/queue.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/interprocess/sync/interprocess_semaphore.hpp>
Expand All @@ -20,11 +20,11 @@ class Writer
Writer()
: _writeContainers()
,_freeContainers()
#ifdef USE_PARALLEL_OUTPUT
,_freeContainerMutex(0)
,_writeContainerMutex(0)
#if defined USE_PARALLEL_OUTPUT && defined USE_BOOST_THREAD
,_freeContainerMutex(1)
,_writeContainerMutex(1)
,_nempty(CONTAINER_COUNT)
,_writerThread(&Writer::writeThread, this);
,_writerThread(&Writer::writeThread, this)
,_threadWorkDone(false)
#endif
{
Expand All @@ -37,18 +37,14 @@ class Writer
get < 1 > (*tpl) = dv;
_freeContainers.push_back(tpl);
}

#ifdef USE_PARALLEL_OUTPUT
_writerThread->start();
#endif
}

virtual ~Writer()
{
#ifdef USE_PARALLEL_OUTPUT
#if defined USE_PARALLEL_OUTPUT && defined USE_BOOST_THREAD
//wait until the writer-thread has written all results
_threadWorkDone = true;
_writeThread.join()
_writerThread.join();
#endif
}

Expand All @@ -57,50 +53,48 @@ class Writer
boost::tuple<value_type_v*, value_type_dv*, double>* getFreeContainer()
{
boost::tuple<value_type_v*, value_type_dv*, double>* container = NULL;
#ifdef USE_PARALLEL_OUTPUT
#if defined USE_PARALLEL_OUTPUT && defined USE_BOOST_THREAD
_nempty.wait();
_freeContainerMutex.wait();
#endif
container = _freeContainers.front();
_freeContainers.pop_front();
#ifdef USE_PARALLEL_OUTPUT
#if defined USE_PARALLEL_OUTPUT && defined USE_BOOST_THREAD
_freeContainerMutex.post();
#endif
return container;
}
;
};

void addContainerToWriteQueue(boost::tuple<value_type_v*, value_type_dv*, double> *container)
{
#ifdef USE_PARALLEL_OUTPUT
#if defined USE_PARALLEL_OUTPUT && defined USE_BOOST_THREAD
_writeContainerMutex.wait();
#endif
_writeContainers.push_back(container);
#ifdef USE_PARALLEL_OUTPUT
#if defined USE_PARALLEL_OUTPUT && defined USE_BOOST_THREAD
_writeContainerMutex.post();
#else
writeContainer();
#endif
}
;
};

protected:
void writeContainer()
{
boost::tuple<value_type_v*, value_type_dv*, double>* container = NULL;

#ifdef USE_PARALLEL_OUTPUT
#if defined USE_PARALLEL_OUTPUT && defined USE_BOOST_THREAD
_writeContainerMutex.wait();
#endif
if (!_writeContainers.empty())
container = _writeContainers.front();
#ifdef USE_PARALLEL_OUTPUT
#if defined USE_PARALLEL_OUTPUT && defined USE_BOOST_THREAD
_writeContainerMutex.post();
#endif

if (container == NULL)
{
#ifdef USE_PARALLEL_OUTPUT
#if defined USE_PARALLEL_OUTPUT && defined USE_BOOST_THREAD
usleep(1);
#endif
return;
Expand All @@ -112,27 +106,27 @@ class Writer

write(*v_list, *v2_list, time);

#ifdef USE_PARALLEL_OUTPUT
#if defined USE_PARALLEL_OUTPUT && defined USE_BOOST_THREAD
_writeContainerMutex.wait();
#endif
_writeContainers.pop_front();
#ifdef USE_PARALLEL_OUTPUT
#if defined USE_PARALLEL_OUTPUT && defined USE_BOOST_THREAD
_writeContainerMutex.post();
#endif

#ifdef USE_PARALLEL_OUTPUT
#if defined USE_PARALLEL_OUTPUT && defined USE_BOOST_THREAD
_freeContainerMutex.wait();
#endif
_freeContainers.push_back(container);
#ifdef USE_PARALLEL_OUTPUT
#if defined USE_PARALLEL_OUTPUT && defined USE_BOOST_THREAD
_freeContainerMutex.post();
_nempty.post();
#endif
}

void writeThread()
{
#ifdef USE_PARALLEL_OUTPUT
#if defined USE_PARALLEL_OUTPUT && defined USE_BOOST_THREAD
while(!_threadWorkDone)
{
writeContainer();
Expand All @@ -145,7 +139,7 @@ class Writer

std::deque<boost::tuple<value_type_v*, value_type_dv*, double>*> _writeContainers;
std::deque<boost::tuple<value_type_v*, value_type_dv*, double>*> _freeContainers;
#ifdef USE_PARALLEL_OUTPUT
#if defined USE_PARALLEL_OUTPUT && defined USE_BOOST_THREAD
boost::interprocess::interprocess_semaphore _freeContainerMutex;
boost::interprocess::interprocess_semaphore _writeContainerMutex;
boost::interprocess::interprocess_semaphore _nempty;
Expand Down
Expand Up @@ -4,7 +4,7 @@
#include <Core/SimController/ISimData.h>
#include <Core/System/IMixedSystem.h>
#include <Core/System/IAlgLoopSolverFactory.h>
#include <Core/SimulationSettings//IGlobalSettings.h>
#include <Core/SimulationSettings/IGlobalSettings.h>
#include <boost/weak_ptr.hpp>
#include <boost/shared_ptr.hpp>

Expand All @@ -27,7 +27,7 @@ struct SimSettings
OutputFormat outputFormat;
unsigned int timeOut;
OutputPointType outputPointType;

LogType logType;
};

/*SimController to start and stop the simulation*/
Expand Down
8 changes: 4 additions & 4 deletions SimulationRuntime/cpp/Makefile
Expand Up @@ -9,15 +9,15 @@ builddir_lib=$(top_builddir)/build/lib/omc
builddir_inc=$(top_builddir)/build/include/omc

ANALYZATION_MODE_COMMAND="-DANALYZATION_MODE=ON"
PARALLEL_OUTPUT_COMMAND="-DUSE_PARALLEL_OUTPUT=ON"
#PARALLEL_OUTPUT_COMMAND="-DUSE_PARALLEL_OUTPUT=ON"

runtimeCpp: clean
ifneq ("$(ANALYZATION_MODE)","true")
$(eval ANALYZATION_MODE_COMMAND="")
endif
ifneq ("$(USE_PARALLEL_OUTPUT)","true")
$(eval PARALLEL_OUTPUT_COMMAND="")
endif
#ifneq ("$(USE_PARALLEL_OUTPUT)","true")
# $(eval PARALLEL_OUTPUT_COMMAND="")
#endif
cd ./Build && echo "change to Build" && cmake $(ANALYZATION_MODE_COMMAND) $(PARALLEL_OUTPUT_COMMAND) -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_INSTALL_PREFIX:PATH="$(builddir_build)" ../
$(MAKE) -C Build;

Expand Down
10 changes: 9 additions & 1 deletion SimulationRuntime/cpp/SimCoreFactory/OMCFactory/OMCFactory.cpp
Expand Up @@ -137,6 +137,14 @@ SimSettings OMCFactory::ReadSimulationParameter(int argc, const char* argv[])
throw std::invalid_argument("results-filename is not set");
}

string logType_str;
LogType logType;
if (vm.count("log-type"))
{
logType_str = vm["log-type"].as<string>();
logType = logTypeMap[logType_str];
}

/*fs::path results_file_path = fs::path( resultsfilename) ;
if(!(results_file_path.extension().string() == ".csv"))
{
Expand All @@ -153,7 +161,7 @@ SimSettings OMCFactory::ReadSimulationParameter(int argc, const char* argv[])



SimSettings settings = {solver,linSolver,nonLinSolver,starttime,stoptime,stepsize,1e-24,0.01,tolerance,resultsfilename,outputFomat,time_out,outputPointType};
SimSettings settings = {solver,linSolver,nonLinSolver,starttime,stoptime,stepsize,1e-24,0.01,tolerance,resultsfilename,outputFomat,time_out,outputPointType,logType};


_library_path = libraries_path;
Expand Down

0 comments on commit 6ad1c0d

Please sign in to comment.