Skip to content

Commit

Permalink
[janitor] Refactor ring buffer (#9231)
Browse files Browse the repository at this point in the history
  • Loading branch information
phannebohm committed Jul 13, 2022
1 parent 0e77908 commit a265d62
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 47 deletions.
Expand Up @@ -183,7 +183,8 @@ static short initial_guess_ipopt_sim(OptData *optData, SOLVER_INFO* solverInfo,
if(op==1)
smallIntSolverStep(data, threadData, solverInfo, (double)optData->time.t[i][j]);
else{
rotateRingBuffer(data->simulationData, 1, (void**) data->localData);
rotateRingBuffer(data->simulationData, 1);
lookupRingBuffer(data->simulationData, (void**) data->localData);
importStartValues(data, threadData, cflags, (double)optData->time.t[i][j]);
for(l=0; l<nReal; ++l){
data->localData[0]->realVars[l] = data->modelData->realVarsData[l].attribute.start;
Expand Down Expand Up @@ -336,7 +337,8 @@ static inline void smallIntSolverStep(DATA* data, threadData_t *threadData, SOLV
a = 1.0;
iter = 0;

rotateRingBuffer(data->simulationData, 1, (void**) data->localData);
rotateRingBuffer(data->simulationData, 1);
lookupRingBuffer(data->simulationData, (void**) data->localData);
do{
if(data->modelData->nStates < 1){
solverInfo->currentTime = tstop;
Expand Down
4 changes: 2 additions & 2 deletions OMCompiler/SimulationRuntime/c/simulation/solver/delay.c
Expand Up @@ -84,7 +84,7 @@ static int findTime(double time, RINGBUFFER *delayStruct, int* foundEvent)
/* Check for an event */
if (fabs(prevTime-curTime)< 1e-12) {
*foundEvent = 1 /* true */;
plotRingBuffer(delayStruct, LOG_UTIL, printDelayBuffer);
printRingBuffer(delayStruct, LOG_UTIL, printDelayBuffer);
}
if (curTime > time) {
pos--;
Expand Down Expand Up @@ -159,7 +159,7 @@ void storeDelayedExpression(DATA* data, threadData_t *threadData, int exprNumber

/* Debug print */
infoStreamPrint(LOG_DELAY, 0, "storeDelayed[%d] (%g,%g) position=%d", exprNumber, time, exprValue, ringBufferLength(data->simulationInfo->delayStructure[exprNumber]));
plotRingBuffer(data->simulationInfo->delayStructure[exprNumber], LOG_DELAY, printDelayBuffer);
printRingBuffer(data->simulationInfo->delayStructure[exprNumber], LOG_DELAY, printDelayBuffer);
}


Expand Down
Expand Up @@ -964,7 +964,7 @@ int gbodef_main(DATA *data, threadData_t *threadData, SOLVER_INFO *solverInfo, d

// Rotate ring buffer
for (i = (gbfData->ringBufferSize - 1); i > 0 ; i--) {
gbfData->tv[i] = gbfData->tv[i - 1];
gbfData->tv[i] = gbfData->tv[i - 1];
memcpy(gbfData->yv + i * nStates, gbfData->yv + (i - 1) * nStates, nStates * sizeof(double));
memcpy(gbfData->kv + i * nStates, gbfData->kv + (i - 1) * nStates, nStates * sizeof(double));
}
Expand Down
8 changes: 4 additions & 4 deletions OMCompiler/SimulationRuntime/c/simulation/solver/gbode_main.h
Expand Up @@ -72,8 +72,8 @@ typedef struct DATA_GBODEF{
enum GB_NLS_METHOD nlsSolverMethod; /* Non-linear solver method uses by generic RK method. */
NONLINEAR_SYSTEM_DATA* nlsData; /* Non-linear system
* Something like
* 0 = yold-x + h*(sum(A[i,j]*k[j], j=1..i-1) + A[i,i]*f(t + c[i]*h, x))
* */
* 0 = yold-x + h*(sum(A[i,j]*k[j], j=1..i-1) + A[i,i]*f(t + c[i]*h, x))
* */
ANALYTIC_JACOBIAN* jacobian; /* Jacobian of non-linear system of implicit Runge-Kutta method */
SPARSE_PATTERN* sparsePattern_DIRK; /* Sparsity pattern for the DIRK methd, will be reduced based on the fast states selection */

Expand Down Expand Up @@ -125,8 +125,8 @@ typedef struct DATA_GBODE{
enum GB_NLS_METHOD nlsSolverMethod; /* Non-linear solver method uses by generic RK method. */
NONLINEAR_SYSTEM_DATA* nlsData; /* Non-linear system
* Something like
* 0 = yold-x + h*(sum(A[i,j]*k[j], j=1..i-1) + A[i,i]*f(t + c[i]*h, x))
* */
* 0 = yold-x + h*(sum(A[i,j]*k[j], j=1..i-1) + A[i,i]*f(t + c[i]*h, x))
* */
ANALYTIC_JACOBIAN* jacobian; /* Jacobian of non-linear system of implicit Runge-Kutta method */
double *y; /* State vector of the current Runge-Kutta step */
double *yt, *y1; /* Result vector of the states of embedded RK step */
Expand Down
Expand Up @@ -1019,7 +1019,7 @@ void initializeDataStruc(DATA *data, threadData_t *threadData)
}
data->localData = (SIMULATION_DATA**) omc_alloc_interface.malloc_uncollectable(SIZERINGBUFFER * sizeof(SIMULATION_DATA));
memset(data->localData, 0, SIZERINGBUFFER * sizeof(SIMULATION_DATA));
rotateRingBuffer(data->simulationData, 0, (void**) data->localData);
lookupRingBuffer(data->simulationData, (void**) data->localData);

/* create modelData var arrays */
data->modelData->realVarsData = (STATIC_REAL_DATA*) omc_alloc_interface.malloc_uncollectable(data->modelData->nVariablesReal * sizeof(STATIC_REAL_DATA));
Expand Down
Expand Up @@ -482,7 +482,8 @@ int prefixedName_performSimulation(DATA* data, threadData_t *threadData, SOLVER_

clear_rt_step(data);
if (!compiledInDAEMode || data->modelData->nStates == 0) { /* do not use ringbuffer for daeMode */
rotateRingBuffer(data->simulationData, 1, (void**) data->localData);
rotateRingBuffer(data->simulationData, 1);
lookupRingBuffer(data->simulationData, (void**) data->localData);
}

modelica_boolean syncEventStep = solverInfo->didEventStep || syncStep == TIMER_FIRED || syncStep == TIMER_FIRED_EVENT;
Expand Down Expand Up @@ -537,7 +538,8 @@ int prefixedName_performSimulation(DATA* data, threadData_t *threadData, SOLVER_
* skip that step and go further */
if (solverInfo->currentStepSize < 1e-15 && syncEventStep){
__currStepNo++;
rotateRingBuffer(data->simulationData, 1, (void**) data->localData);
rotateRingBuffer(data->simulationData, 1);
lookupRingBuffer(data->simulationData, (void**) data->localData);
continue;
}

Expand Down
61 changes: 28 additions & 33 deletions OMCompiler/SimulationRuntime/c/util/ringbuffer.c
Expand Up @@ -113,19 +113,9 @@ void *getRingData(RINGBUFFER *rb, int i)
*/
void expandRingBuffer(RINGBUFFER *rb)
{
int i;

void *tmp = calloc(2*rb->bufferSize, rb->itemSize);
assertStreamPrint(NULL, 0!=tmp, "out of memory");

for(i=0; i<rb->nElements; i++) {
memcpy(((char*)tmp)+(i*rb->itemSize), getRingData(rb, i), rb->itemSize);
}

free(rb->buffer);
rb->buffer = tmp;
rb->bufferSize *= 2;
rb->firstElement = 0;
rb->buffer = realloc(rb->buffer, rb->bufferSize*rb->itemSize);
assertStreamPrint(NULL, 0 != rb->buffer, "out of memory");
}

/**
Expand Down Expand Up @@ -196,15 +186,10 @@ int ringBufferLength(RINGBUFFER *rb)
/**
* @brief Rotate start point of ring buffer by n elements.
*
* Copy all buffer elements into lookup, if provided.
*
* @param rb Pointer to ring buffer.
* @param n Number of items to rotate.
* @param lookup Pointer to array of buffer element type and of length buffer->nElements.
* Ring data will be written into lookup.
* Can be NULL.
*/
void rotateRingBuffer(RINGBUFFER *rb, int n, void **lookup)
void rotateRingBuffer(RINGBUFFER *rb, int n)
{
TRACE_PUSH

Expand All @@ -214,11 +199,25 @@ void rotateRingBuffer(RINGBUFFER *rb, int n, void **lookup)

rb->firstElement = (rb->firstElement+(n*(rb->bufferSize-1)))%rb->bufferSize;

if(lookup)
{
for(long i=0; i<rb->nElements; ++i){
lookup[i] = getRingData(rb, i);
}
TRACE_POP
}

/**
* @brief Copy addresses of all buffer elements in order.
*
* @param rb Pointer to ring buffer.
* @param lookup Pointer to array of buffer element pointer type and of length buffer->nElements.
* Ring data addresses will be written into lookup.
*/
void lookupRingBuffer(RINGBUFFER *rb, void **lookup)
{
TRACE_PUSH

assertStreamPrint(NULL, rb->nElements > 0, "empty RingBuffer");
assertStreamPrint(NULL, lookup, "Target buffer is NULL");

for (int i = 0; i < rb->nElements; i++) {
lookup[i] = ((char*)rb->buffer) + (((rb->firstElement+i)%rb->bufferSize)*rb->itemSize);
}

TRACE_POP
Expand Down Expand Up @@ -248,23 +247,19 @@ void infoRingBuffer(RINGBUFFER *rb)
* @param stream Stream of type LOG_STREAM.
* @param printDataFunc Function to print address of buffer element and its data to stream.
*/
void plotRingBuffer(RINGBUFFER *rb, int stream, void (*printDataFunc)(void*,int,void*)) {
int pos = 0;
void printRingBuffer(RINGBUFFER *rb, int stream, void (*printDataFunc)(void*,int,void*)) {
int i;
void* bufferElemData;

if (useStream[stream]) {
infoStreamPrint(stream, 1, "Printing ring buffer:");
infoStreamPrint(stream, 0, "itemSize: %d [size of one item in bytes]", rb->itemSize);
infoStreamPrint(stream, 0, "firstElement: %d [position of first element in buffer]", rb->firstElement);
infoStreamPrint(stream, 0, "nElements: %d [number of elements in buffer]", rb->nElements);
infoStreamPrint(stream, 0, "bufferSize: %d [number of elements which could be stored in buffer]", rb->bufferSize);
infoRingBuffer(rb);

while(pos < rb->nElements) {
bufferElemData = getRingData(rb, pos);
for(i = 0; i < rb->nElements; ++i) {
bufferElemData = getRingData(rb, i);
printDataFunc(bufferElemData, stream, (void*) bufferElemData);
pos++;
}

messageClose(stream);
}
}
}
5 changes: 3 additions & 2 deletions OMCompiler/SimulationRuntime/c/util/ringbuffer.h
Expand Up @@ -55,11 +55,12 @@ extern "C" {

int ringBufferLength(RINGBUFFER *rb);

void rotateRingBuffer(RINGBUFFER *rb, int n, void **lookup);
void rotateRingBuffer(RINGBUFFER *rb, int n);
void lookupRingBuffer(RINGBUFFER *rb, void **lookup);

void infoRingBuffer(RINGBUFFER *rb);

void plotRingBuffer(RINGBUFFER *rb, int stream, void (*printDataFunc)(void*,int,void*));
void printRingBuffer(RINGBUFFER *rb, int stream, void (*printDataFunc)(void*,int,void*));
#ifdef __cplusplus
}
#endif
Expand Down

0 comments on commit a265d62

Please sign in to comment.