Skip to content

Commit

Permalink
Give the error functions default values. (#8073)
Browse files Browse the repository at this point in the history
* Give the error functions default values.

- The default values given to the are the 'functions' versions, e.g.
  `omc_assert = omc_assert_function`

- This way we do not have to initialize them in every executable code
  that links to the OpenModelicaRuntimeC. If you do not specify anything
  you get the default value.

- For simulations set them to the 'simulation' versions in the simulation
  executables 'main' function, e.g,
  `omc_assert = omc_assert_simulation`

- Windows tests are now on track to start working without complaining
  about undefined references in 'function' mode.

- The testcase is intended to test parameter overriding. However it had
   a terminate statement that fired when t > 1 but also specified an
   experiment stop time of 2.
   Use the default stop time of 1 so the test does not terminate before
   doing what is is supposed to test.

Co-authored-by: Mahder Gebremedhin <mahder.geb@gmail.com>
  • Loading branch information
sjoelund and mahge committed Nov 3, 2021
1 parent 0ea1e76 commit af03d95
Show file tree
Hide file tree
Showing 19 changed files with 92 additions and 67 deletions.
5 changes: 0 additions & 5 deletions OMCompiler/Compiler/.cmake/omc_entry_point.c
Expand Up @@ -14,11 +14,6 @@ omc_Main_main
#endif
(threadData_t*,modelica_metatype);

void (*omc_assert)(threadData_t*,FILE_INFO info,const char *msg,...) __attribute__((noreturn)) = omc_assert_function;
void (*omc_assert_warning)(FILE_INFO info,const char *msg,...) = omc_assert_warning_function;
void (*omc_terminate)(FILE_INFO info,const char *msg,...) = omc_terminate_function;
void (*omc_throw)(threadData_t*) __attribute__ ((noreturn)) = omc_throw_function;

#ifdef _OPENMP
#include<omp.h>
/* Hack to make gcc-4.8 link in the OpenMP runtime if -fopenmp is given */
Expand Down
13 changes: 13 additions & 0 deletions OMCompiler/Compiler/Template/CodegenC.tpl
Expand Up @@ -118,6 +118,7 @@ end translateModel;
#include "simulation/simulation_runtime.h"
#include "util/omc_error.h"
#include "util/parallel_helper.h"
#include "simulation/simulation_omc_assert.h"
#include "simulation/solver/model_help.h"
#include "simulation/solver/delay.h"
#include "simulation/solver/linearSystem.h"
Expand Down Expand Up @@ -1223,6 +1224,18 @@ template simulationFile(SimCode simCode, String guid, String isModelExchangeFMU)
/* call the simulation runtime main from our main! */
int main(int argc, char**argv)
{
/*
Set the error functions to be used for simulation.
The default value for them is 'functions' version. Change it here to 'simulation' versions
*/
omc_assert = omc_assert_simulation;
omc_assert_withEquationIndexes = omc_assert_simulation_withEquationIndexes;
omc_assert_warning_withEquationIndexes = omc_assert_warning_simulation_withEquationIndexes;
omc_assert_warning = omc_assert_warning_simulation;
omc_terminate = omc_terminate_simulation;
omc_throw = omc_throw_simulation;
int res;
DATA data;
MODEL_DATA modelData;
Expand Down
10 changes: 0 additions & 10 deletions OMCompiler/Compiler/Template/CodegenCFunctions.tpl
Expand Up @@ -48,11 +48,6 @@ extern void
#endif
(threadData_t*,modelica_metatype);

void (*omc_assert)(threadData_t*,FILE_INFO info,const char *msg,...) __attribute__((noreturn)) = omc_assert_function;
void (*omc_assert_warning)(FILE_INFO info,const char *msg,...) = omc_assert_warning_function;
void (*omc_terminate)(FILE_INFO info,const char *msg,...) = omc_terminate_function;
void (*omc_throw)(threadData_t*) __attribute__ ((noreturn)) = omc_throw_function;

#ifdef _OPENMP
#include<omp.h>
/* Hack to make gcc-4.8 link in the OpenMP runtime if -fopenmp is given */
Expand Down Expand Up @@ -1430,11 +1425,6 @@ template generateInFunc(Text fname, list<Variable> functionArguments, list<Varia
return 1;
}

void (*omc_assert)(threadData_t*,FILE_INFO info,const char *msg,...) __attribute__((noreturn)) = omc_assert_function;
void (*omc_assert_warning)(FILE_INFO info,const char *msg,...) = omc_assert_warning_function;
void (*omc_terminate)(FILE_INFO info,const char *msg,...) = omc_terminate_function;
void (*omc_throw)(threadData_t*) __attribute__ ((noreturn)) = omc_throw_function;

int main(int argc, char **argv) {
MMC_INIT(0);
{
Expand Down
1 change: 1 addition & 0 deletions OMCompiler/Compiler/boot/Makefile.common
Expand Up @@ -72,6 +72,7 @@ bootstrap-from-tarball2:
$(MAKE) -f $(defaultMakefileTarget) --no-print-directory clean OMC="$(BOOTSTRAP_OMC)"
$(MAKE) -f $(defaultMakefileTarget) --no-print-directory runtime-depends OMBUILDDIR=$(OMBUILDDIR) OMC="$(BOOTSTRAP_OMC)"
OPENMODELICA_BACKEND_STUBS=1 $(MAKE) -f $(defaultMakefileTarget) generate-files-in-steps OMC="$(BOOTSTRAP_OMC)" OMC_EXTRA_FLAGS= TEMPLATES_TARGET=templates-bootstrap
cp bootstrap-sources/build/_main.c build/
# We have not compiled OpenModelicaScriptingAPI.mo yet
touch build/OpenModelicaScriptingAPI.h
$(MAKE) -f $(defaultMakefileTarget) install INCLUDESOURCES=1 OMC="$(BOOTSTRAP_OMC)" CPPFLAGS="$(CPPFLAGS) -DOMC_BOOTSTRAPPING_STAGE_2"
Expand Down
4 changes: 0 additions & 4 deletions OMCompiler/Compiler/boot/bootstrap-sources/build/_main.c
Expand Up @@ -19,10 +19,6 @@ extern void
omc_Main_main
#endif
(threadData_t*,modelica_metatype);
void (*omc_assert)(threadData_t*,FILE_INFO info,const char *msg,...) __attribute__((noreturn)) = omc_assert_function;
void (*omc_assert_warning)(FILE_INFO info,const char *msg,...) = omc_assert_warning_function;
void (*omc_terminate)(FILE_INFO info,const char *msg,...) = omc_terminate_function;
void (*omc_throw)(threadData_t*) __attribute__ ((noreturn)) = omc_throw_function;
#ifdef _OPENMP
#include<omp.h>
int (*force_link_omp)(void) = omp_get_num_threads;
Expand Down
Expand Up @@ -7,10 +7,6 @@
#include "scripting-API/OpenModelicaScriptingAPI.h"

extern "C" {
void (*omc_assert)(threadData_t*, FILE_INFO info, const char* msg,...) __attribute__((noreturn)) = omc_assert_function;
void (*omc_assert_warning)(FILE_INFO info, const char* msg,...) = omc_assert_warning_function;
void (*omc_terminate)(FILE_INFO info, const char* msg,...) = omc_terminate_function;
void (*omc_throw)(threadData_t*) __attribute__((noreturn)) = omc_throw_function;
int omc_Main_handleCommand(void* threadData, void* imsg, void** omsg);
void* omc_Main_init(void* threadData, void* args);
void omc_Main_readSettings(void* threadData, void* args);
Expand Down
1 change: 1 addition & 0 deletions OMCompiler/SimulationRuntime/c/Makefile.common
Expand Up @@ -65,6 +65,7 @@ RUNTIMESIMULATION_HEADERS = ./simulation/modelinfo.h \
./simulation/options.h \
./simulation/simulation_info_json.h \
./simulation/simulation_input_xml.h \
./simulation/simulation_omc_assert.h \
./simulation/simulation_runtime.h \
./simulation/omc_simulation_util.h

Expand Down
1 change: 1 addition & 0 deletions OMCompiler/SimulationRuntime/c/Makefile.objs
Expand Up @@ -259,6 +259,7 @@ SIM_HFILES = ../dataReconciliation/dataReconciliation.h \
omc_simulation_util.h \
simulation_info_json.h \
simulation_input_xml.h \
simulation_omc_assert.h \
simulation_runtime.h \
socket.h \
options.h
Expand Down
33 changes: 13 additions & 20 deletions OMCompiler/SimulationRuntime/c/simulation/simulation_omc_assert.c
Expand Up @@ -30,11 +30,17 @@

#include <stdarg.h>
#include <stddef.h>
#include "../util/omc_error.h"
#include "../util/utility.h"
#include "../meta/meta_modelica.h"
#include "simulation_omc_assert.h"
#include "simulation_runtime.h"


void (*omc_assert_withEquationIndexes)(threadData_t*, FILE_INFO info, const int *indexes, const char *msg, ...) __attribute__ ((noreturn)) = omc_assert_simulation_withEquationIndexes;

void (*omc_assert_warning_withEquationIndexes)(FILE_INFO info, const int *indexes, const char *msg, ...) = omc_assert_warning_simulation_withEquationIndexes;


int terminationTerminate = 0; /* Becomes non-zero when user terminates simulation. */
FILE_INFO TermInfo; /* message for termination. */
char* TermMsg; /* message for termination. */
Expand Down Expand Up @@ -63,11 +69,6 @@ static void setTermMsg(const char *msg, va_list ap)
}
}

static void omc_assert_simulation(threadData_t *threadData, FILE_INFO info, const char *msg, ...) __attribute__ ((noreturn));
static void omc_assert_simulation_withEquationIndexes(threadData_t *threadData, FILE_INFO info, const int *indexes, const char *msg, ...) __attribute__ ((noreturn));
static void omc_throw_simulation(threadData_t* threadData) __attribute__ ((noreturn));
static void va_omc_assert_simulation_withEquationIndexes(threadData_t *threadData, FILE_INFO info, const int *indexes, const char *msg, va_list args) __attribute__ ((noreturn));

static void va_omc_assert_simulation_withEquationIndexes(threadData_t *threadData, FILE_INFO info, const int *indexes, const char *msg, va_list args)
{
threadData = threadData ? threadData : (threadData_t*)pthread_getspecific(mmc_thread_data_key);
Expand Down Expand Up @@ -105,15 +106,15 @@ static void va_omc_assert_simulation_withEquationIndexes(threadData_t *threadDat
}
}

static void omc_assert_simulation(threadData_t *threadData, FILE_INFO info, const char *msg, ...)
void omc_assert_simulation(threadData_t *threadData, FILE_INFO info, const char *msg, ...)
{
va_list args;
va_start(args, msg);
va_omc_assert_simulation_withEquationIndexes(threadData, info, NULL, msg, args);
va_end(args);
}

static void omc_assert_simulation_withEquationIndexes(threadData_t *threadData, FILE_INFO info, const int *indexes, const char *msg, ...)
void omc_assert_simulation_withEquationIndexes(threadData_t *threadData, FILE_INFO info, const int *indexes, const char *msg, ...)
{
va_list args;
va_start(args, msg);
Expand All @@ -127,23 +128,23 @@ static void va_omc_assert_warning_simulation(FILE_INFO info, const int *indexes,
va_warningStreamPrintWithEquationIndexes(LOG_ASSERT, 0, indexes, msg, args);
}

static void omc_assert_warning_simulation(FILE_INFO info, const char *msg, ...)
void omc_assert_warning_simulation(FILE_INFO info, const char *msg, ...)
{
va_list args;
va_start(args, msg);
va_omc_assert_warning_simulation(info, NULL, msg, args);
va_end(args);
}

static void omc_assert_warning_simulation_withEquationIndexes(FILE_INFO info, const int *indexes, const char *msg, ...)
void omc_assert_warning_simulation_withEquationIndexes(FILE_INFO info, const int *indexes, const char *msg, ...)
{
va_list args;
va_start(args, msg);
va_omc_assert_warning_simulation(info, indexes, msg, args);
va_end(args);
}

static void omc_terminate_simulation(FILE_INFO info, const char *msg, ...)
void omc_terminate_simulation(FILE_INFO info, const char *msg, ...)
{
va_list ap;
va_start(ap,msg);
Expand All @@ -164,18 +165,10 @@ void setTermMsg_empty_va_list(const char *msg, ...) {
va_end(dummy);
}

static void omc_throw_simulation(threadData_t* threadData)
void omc_throw_simulation(threadData_t* threadData)
{
setTermMsg_empty_va_list("Assertion triggered by external C function");
set_struct(FILE_INFO, TermInfo, omc_dummyFileInfo);
threadData = threadData ? threadData : (threadData_t*)pthread_getspecific(mmc_thread_data_key);
longjmp(*threadData->globalJumpBuffer, 1);
}

void (*omc_assert)(threadData_t*, FILE_INFO info, const char *msg, ...) __attribute__ ((noreturn)) = omc_assert_simulation;
void (*omc_assert_withEquationIndexes)(threadData_t*, FILE_INFO info, const int *indexes, const char *msg, ...) __attribute__ ((noreturn)) = omc_assert_simulation_withEquationIndexes;

void (*omc_assert_warning_withEquationIndexes)(FILE_INFO info, const int *indexes, const char *msg, ...) = omc_assert_warning_simulation_withEquationIndexes;
void (*omc_assert_warning)(FILE_INFO info, const char *msg, ...) = omc_assert_warning_simulation;
void (*omc_terminate)(FILE_INFO info, const char *msg, ...) = omc_terminate_simulation;
void (*omc_throw)(threadData_t*) __attribute__ ((noreturn)) = omc_throw_simulation;
51 changes: 51 additions & 0 deletions OMCompiler/SimulationRuntime/c/simulation/simulation_omc_assert.h
@@ -0,0 +1,51 @@
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-CurrentYear, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THE BSD NEW LICENSE OR THE
* GPL VERSION 3 LICENSE OR THE OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
* ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the OSMC (Open Source Modelica Consortium)
* Public License (OSMC-PL) are obtained from OSMC, either from the above
* address, from the URLs: http://www.openmodelica.org or
* http://www.ida.liu.se/projects/OpenModelica, and in the OpenModelica
* distribution. GNU version 3 is obtained from:
* http://www.gnu.org/copyleft/gpl.html. The New BSD License is obtained from:
* http://www.opensource.org/licenses/BSD-3-Clause.
*
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, EXCEPT AS
* EXPRESSLY SET FORTH IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE
* CONDITIONS OF OSMC-PL.
*
*/

#ifndef OMC_SIMULATION_OMC_ASSERT_H
#define OMC_SIMULATION_OMC_ASSERT_H

#include "../util/omc_error.h"

DLLExport extern void (*omc_assert_withEquationIndexes)(threadData_t*, FILE_INFO info, const int *indexes, const char *msg, ...) __attribute__ ((noreturn));

DLLExport extern void (*omc_assert_warning_withEquationIndexes)(FILE_INFO info, const int *indexes, const char *msg, ...);



void omc_assert_simulation_withEquationIndexes(threadData_t *threadData, FILE_INFO info, const int *indexes, const char *msg, ...) __attribute__ ((noreturn));

void omc_assert_warning_simulation_withEquationIndexes(FILE_INFO info, const int *indexes, const char *msg, ...);

void omc_assert_simulation(threadData_t *threadData, FILE_INFO info, const char *msg, ...);
void omc_terminate_simulation(FILE_INFO info, const char *msg, ...);
void omc_throw_simulation(threadData_t* threadData);
void omc_assert_warning_simulation(FILE_INFO info, const char *msg, ...);

#endif // Header
6 changes: 6 additions & 0 deletions OMCompiler/SimulationRuntime/c/util/omc_error.c
Expand Up @@ -34,6 +34,12 @@
/* For MMC_THROW, so we can end this thing */
#include "../meta/meta_modelica.h"

void (*omc_assert)(threadData_t*,FILE_INFO info,const char *msg,...) __attribute__((noreturn)) = omc_assert_function;
void (*omc_assert_warning)(FILE_INFO info,const char *msg,...) = omc_assert_warning_function;
void (*omc_terminate)(FILE_INFO info,const char *msg,...) = omc_terminate_function;
void (*omc_throw)(threadData_t*) __attribute__ ((noreturn)) = omc_throw_function;


const int firstOMCErrorStream = 1;

const char *LOG_STREAM_NAME[SIM_LOG_MAX] = {
Expand Down
2 changes: 2 additions & 0 deletions OMCompiler/SimulationRuntime/c/util/omc_error.h
Expand Up @@ -56,11 +56,13 @@ typedef struct _FILE_INFO
#define omc_dummyFileInfo {"",0,0,0,0,0}

DLLExport extern void printInfo(FILE *stream, FILE_INFO info);
// Defined in omc_error.c
DLLExport extern void (*omc_assert)(threadData_t*, FILE_INFO, const char*, ...) __attribute__ ((noreturn));
DLLExport extern void (*omc_assert_warning)(FILE_INFO, const char*, ...);
DLLExport extern void (*omc_terminate)(FILE_INFO, const char*, ...);
DLLExport extern void (*omc_throw)(threadData_t*) __attribute__ ((noreturn));

// Defined in simulation_omc_assert.c
DLLExport extern void (*omc_assert_withEquationIndexes)(threadData_t*,FILE_INFO, const int*, const char*, ...) __attribute__ ((noreturn));
DLLExport extern void (*omc_assert_warning_withEquationIndexes)(FILE_INFO, const int*, const char*, ...);

Expand Down
4 changes: 0 additions & 4 deletions OMEdit/OMEditLIB/OMC/OMCProxy.cpp
Expand Up @@ -37,10 +37,6 @@ extern "C" {
#include "omc_config.h"
#include "gc.h"

void (*omc_assert)(threadData_t*,FILE_INFO info,const char *msg,...) __attribute__((noreturn)) = omc_assert_function;
void (*omc_assert_warning)(FILE_INFO info,const char *msg,...) = omc_assert_warning_function;
void (*omc_terminate)(FILE_INFO info,const char *msg,...) = omc_terminate_function;
void (*omc_throw)(threadData_t*) __attribute__ ((noreturn)) = omc_throw_function;
int omc_Main_handleCommand(void *threadData, void *imsg, void **omsg);
void* omc_Main_init(void *threadData, void *args);
void omc_System_initGarbageCollector(void *threadData);
Expand Down
Expand Up @@ -56,10 +56,6 @@
#include "gc.h"

extern "C" {
void (*omc_assert)(threadData_t*,FILE_INFO info,const char *msg,...) __attribute__((noreturn)) = omc_assert_function;
void (*omc_assert_warning)(FILE_INFO info,const char *msg,...) = omc_assert_warning_function;
void (*omc_terminate)(FILE_INFO info,const char *msg,...) = omc_terminate_function;
void (*omc_throw)(threadData_t*) __attribute__ ((noreturn)) = omc_throw_function;
int omc_Main_handleCommand(void *threadData, void *imsg, void **omsg);
void* omc_Main_init(void *threadData, void *args);
void omc_System_initGarbageCollector(void *threadData);
Expand Down
2 changes: 1 addition & 1 deletion OMOptim
Submodule OMOptim updated 1 files
+0 −2 OMOptim/main.cpp
3 changes: 0 additions & 3 deletions OMPlot/OMPlot/OMPlotGUI/main.cpp
Expand Up @@ -45,9 +45,6 @@

#include "util/omc_error.h"

void (*omc_assert)(threadData_t*,FILE_INFO info,const char *msg,...) __attribute__((noreturn)) = omc_assert_function;
void (*omc_assert_warning)(FILE_INFO info,const char *msg,...) = omc_assert_warning_function;

using namespace OMPlot;

#define CONSUME_BOOL_ARG(i,n,var) { \
Expand Down
4 changes: 0 additions & 4 deletions OMShell/OMShell/OMShellGUI/omcinteractiveenvironment.cpp
Expand Up @@ -56,10 +56,6 @@
#include "gc.h"

extern "C" {
void (*omc_assert)(threadData_t*,FILE_INFO info,const char *msg,...) __attribute__((noreturn)) = omc_assert_function;
void (*omc_assert_warning)(FILE_INFO info,const char *msg,...) = omc_assert_warning_function;
void (*omc_terminate)(FILE_INFO info,const char *msg,...) = omc_terminate_function;
void (*omc_throw)(threadData_t*) __attribute__ ((noreturn)) = omc_throw_function;
int omc_Main_handleCommand(void *threadData, void *imsg, void **omsg);
void* omc_Main_init(void *threadData, void *args);
void omc_System_initGarbageCollector(void *threadData);
Expand Down
4 changes: 0 additions & 4 deletions OMShell/mosh/src/omcinteractiveenvironment.cpp
Expand Up @@ -42,10 +42,6 @@
#include "gc.h"

extern "C" {
void (*omc_assert)(threadData_t*,FILE_INFO info,const char *msg,...) __attribute__((noreturn)) = omc_assert_function;
void (*omc_assert_warning)(FILE_INFO info,const char *msg,...) = omc_assert_warning_function;
void (*omc_terminate)(FILE_INFO info,const char *msg,...) = omc_terminate_function;
void (*omc_throw)(threadData_t*) __attribute__ ((noreturn)) = omc_throw_function;
int omc_Main_handleCommand(void *threadData, void *imsg, void **omsg);
modelica_metatype omc_Main_init(void *threadData, modelica_metatype args);
modelica_metatype omc_Main_readSettings(void *threadData, modelica_metatype args);
Expand Down
Expand Up @@ -21,7 +21,6 @@ equation
when time>1 then
terminate(\"Simulation done by \" + s);
end when;
annotation(experiment(StopTime=2));
end test_ChangeParam;
");
getErrorString();
Expand All @@ -42,7 +41,7 @@ val(y, 1);
getErrorString();

system("./test_ChangeParam_me_FMU -override a=-2,i=-10,b=false,s=\"bar\" -r test_ChangeParam.mat");
readSimulationResult("test_ChangeParam.mat", {x,a,y}, 7);
readSimulationResult("test_ChangeParam.mat", {x,a,y});
getErrorString();


Expand All @@ -58,7 +57,7 @@ getErrorString();
// ""
// record SimulationResult
// resultFile = "test_ChangeParam_me_FMU_res.mat",
// simulationOptions = "startTime = 0.0, stopTime = 2.0, numberOfIntervals = 1, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'test_ChangeParam_me_FMU', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''",
// simulationOptions = "startTime = 0.0, stopTime = 1.0, numberOfIntervals = 1, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'test_ChangeParam_me_FMU', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''",
// messages = "LOG_SUCCESS | info | The initialization finished successfully without homotopy method.
// LOG_SUCCESS | info | The simulation finished successfully.
// "
Expand All @@ -70,6 +69,6 @@ getErrorString();
// LOG_SUCCESS | info | The initialization finished successfully without homotopy method.
// LOG_SUCCESS | info | The simulation finished successfully.
// 0
// {{1.0,1.0,1.0,0.1353363211838032,0.1353363211838032,0.01831624751345867,0.01831624751345867},{-2.0,-2.0,-2.0,-2.0,-2.0,-2.0,-2.0},{-11.0,-11.0,-11.0,-10.1353363211838,-10.1353363211838,-10.01831624751346,-10.01831624751346}}
// {{1.0,1.0,1.0,0.1353363211838032,0.1353363211838032},{-2.0,-2.0,-2.0,-2.0,-2.0},{-11.0,-11.0,-11.0,-10.1353363211838,-10.1353363211838}}
// ""
// endResult

0 comments on commit af03d95

Please sign in to comment.