Skip to content

Commit d4dd96b

Browse files
lochelOpenModelica-Hudson
authored andcommitted
Cleanup MatVer4
Belonging to [master]: - OpenModelica/OMCompiler#1956
1 parent 119136c commit d4dd96b

File tree

3 files changed

+59
-167
lines changed

3 files changed

+59
-167
lines changed

SimulationRuntime/c/simulation/results/MatVer4.cpp

Lines changed: 38 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
#include "MatVer4.h"
3333

34+
#include <assert.h>
3435
#include <stddef.h>
3536
#include <stdint.h>
3637
#include <stdio.h>
@@ -51,35 +52,29 @@ const char isBigEndian()
5152
return (1 == test.i8[0]);
5253
}
5354

54-
int writeMatVer4Matrix_4(FILE* file, const char* name, size_t rows, size_t cols, const void* matrixData, MatVer4Type_t type)
55+
size_t sizeofMatVer4Type(MatVer4Type_t type)
5556
{
56-
struct MatVer4Header
57-
{
58-
unsigned int type;
59-
unsigned int mrows;
60-
unsigned int ncols;
61-
unsigned int imagf;
62-
unsigned int namelen;
63-
} header;
64-
65-
size_t size;
6657
switch (type)
6758
{
6859
case MatVer4Type_DOUBLE:
69-
size = sizeof(double);
70-
break;
60+
return sizeof(double);
7161
case MatVer4Type_SINGLE:
72-
size = sizeof(float);
73-
break;
62+
return sizeof(float);
7463
case MatVer4Type_INT32:
75-
size = sizeof(int32_t);
76-
break;
64+
return sizeof(int32_t);
7765
case MatVer4Type_CHAR:
78-
size = sizeof(uint8_t);
79-
break;
66+
return sizeof(uint8_t);
8067
default:
81-
return -1;
68+
// Should never get here!
69+
assert(0);
70+
return 0;
8271
}
72+
}
73+
74+
void writeMatrix_matVer4(FILE* file, const char* name, size_t rows, size_t cols, const void* matrixData, MatVer4Type_t type)
75+
{
76+
MatVer4Header header;
77+
size_t size = sizeofMatVer4Type(type);
8378

8479
header.type = (isBigEndian() ? 1000 : 0) + type;
8580
header.mrows = (unsigned int) rows;
@@ -88,112 +83,40 @@ int writeMatVer4Matrix_4(FILE* file, const char* name, size_t rows, size_t cols,
8883
header.namelen = (unsigned int) strlen(name) + 1;
8984

9085
fwrite(&header, sizeof(MatVer4Header), 1, file);
91-
fwrite(name, sizeof(char), header.namelen, file);
92-
fwrite(matrixData, size, rows * cols, file);
86+
fwrite(name, sizeof(uint8_t), header.namelen, file);
9387

94-
return 0;
88+
if (matrixData)
89+
fwrite(matrixData, size, rows * cols, file);
9590
}
9691

97-
int appendMatVer4Matrix_4(FILE* file, long position, const char* name, size_t rows, size_t cols, const void* matrixData, MatVer4Type_t type)
92+
void updateHeader_matVer4(FILE* file, long position, const char* name, size_t rows, size_t additional_cols, MatVer4Type_t type)
9893
{
99-
struct MatVer4Header
100-
{
101-
unsigned int type;
102-
unsigned int mrows;
103-
unsigned int ncols;
104-
unsigned int imagf;
105-
unsigned int namelen;
106-
} header;
107-
108-
size_t size;
109-
switch (type)
110-
{
111-
case MatVer4Type_DOUBLE:
112-
size = sizeof(double);
113-
break;
114-
case MatVer4Type_SINGLE:
115-
size = sizeof(float);
116-
break;
117-
case MatVer4Type_INT32:
118-
size = sizeof(int32_t);
119-
break;
120-
case MatVer4Type_CHAR:
121-
size = sizeof(uint8_t);
122-
break;
123-
default:
124-
return -1;
125-
}
94+
MatVer4Header header;
12695

12796
long eof = ftell(file);
12897
fseek(file, position, SEEK_SET);
12998
fread(&header, sizeof(MatVer4Header), 1, file);
130-
if (header.type != (isBigEndian() ? 1000 : 0) + type)
131-
return -1;
132-
if (header.mrows != rows)
133-
return -1;
134-
header.ncols += (unsigned int) cols;
135-
if (header.imagf != 0)
136-
return -1;
137-
if (header.namelen != strlen(name) + 1)
138-
return -1;
99+
100+
assert(header.type == (isBigEndian() ? 1000 : 0) + type);
101+
assert(header.mrows == rows);
102+
assert(header.imagf == 0);
103+
assert(header.namelen == strlen(name) + 1);
104+
105+
header.ncols += (unsigned int) additional_cols;
139106

140107
fseek(file, position, SEEK_SET);
141108
fwrite(&header, sizeof(MatVer4Header), 1, file);
142109
fseek(file, eof, SEEK_SET);
143-
fwrite(matrixData, size, rows * cols, file);
144-
return 0;
145110
}
146111

147-
int writeMatVer4Header_4(FILE* file, long position, const char* name, size_t rows, size_t cols, MatVer4Type_t type)
112+
void appendMatrix_matVer4(FILE* file, long position, const char* name, size_t rows, size_t cols, const void* matrixData, MatVer4Type_t type)
148113
{
149-
struct MatVer4Header
150-
{
151-
unsigned int type;
152-
unsigned int mrows;
153-
unsigned int ncols;
154-
unsigned int imagf;
155-
unsigned int namelen;
156-
} header;
157-
158-
size_t size;
159-
switch (type)
160-
{
161-
case MatVer4Type_DOUBLE:
162-
size = sizeof(double);
163-
break;
164-
case MatVer4Type_SINGLE:
165-
size = sizeof(float);
166-
break;
167-
case MatVer4Type_INT32:
168-
size = sizeof(int32_t);
169-
break;
170-
case MatVer4Type_CHAR:
171-
size = sizeof(uint8_t);
172-
break;
173-
default:
174-
return -1;
175-
}
176-
177-
long eof = ftell(file);
178-
fseek(file, position, SEEK_SET);
179-
fread(&header, sizeof(MatVer4Header), 1, file);
180-
if (header.type != (isBigEndian() ? 1000 : 0) + type)
181-
return -1;
182-
if (header.mrows != rows)
183-
return -1;
184-
header.ncols = (unsigned int) cols;
185-
if (header.imagf != 0)
186-
return -1;
187-
if (header.namelen != strlen(name) + 1)
188-
return -1;
189-
190-
fseek(file, position, SEEK_SET);
191-
fwrite(&header, sizeof(MatVer4Header), 1, file);
192-
fseek(file, eof, SEEK_SET);
193-
return 0;
114+
size_t size = sizeofMatVer4Type(type);
115+
updateHeader_matVer4(file, position, name, rows, cols, type);
116+
fwrite(matrixData, size, rows * cols, file);
194117
}
195118

196-
MatVer4Matrix* readMatVer4Matrix_4(FILE* file)
119+
MatVer4Matrix* readMatVer4Matrix(FILE* file)
197120
{
198121
MatVer4Matrix *matrix = (MatVer4Matrix*) malloc(sizeof(MatVer4Matrix));
199122
if (!matrix)
@@ -204,33 +127,15 @@ MatVer4Matrix* readMatVer4Matrix_4(FILE* file)
204127
// skip name
205128
fseek(file, matrix->header.namelen, SEEK_CUR);
206129

207-
size_t size;
208-
switch (matrix->header.type % 100)
209-
{
210-
case MatVer4Type_DOUBLE:
211-
size = sizeof(double);
212-
break;
213-
case MatVer4Type_SINGLE:
214-
size = sizeof(float);
215-
break;
216-
case MatVer4Type_INT32:
217-
size = sizeof(int32_t);
218-
break;
219-
case MatVer4Type_CHAR:
220-
size = sizeof(uint8_t);
221-
break;
222-
default:
223-
free(matrix);
224-
return NULL;
225-
}
226-
130+
MatVer4Type_t type = (MatVer4Type_t) (matrix->header.type % 100);
131+
size_t size = sizeofMatVer4Type(type);
227132
matrix->data = malloc(matrix->header.mrows * matrix->header.ncols * size);
228133
fread(matrix->data, size, matrix->header.mrows*matrix->header.ncols, file);
229134

230135
return matrix;
231136
}
232137

233-
void freeMatVer4Matrix_4(MatVer4Matrix** matrix)
138+
void freeMatrix_matVer4(MatVer4Matrix** matrix)
234139
{
235140
if (*matrix)
236141
{
@@ -241,36 +146,18 @@ void freeMatVer4Matrix_4(MatVer4Matrix** matrix)
241146
}
242147
}
243148

244-
int skipMatVer4Matrix_4(FILE* file)
149+
void skipMatrix_matVer4(FILE* file)
245150
{
246151
MatVer4Header header;
247152
fread(&header, sizeof(MatVer4Header), 1, file);
248153

249154
// skip name
250155
fseek(file, header.namelen, SEEK_CUR);
251156

252-
size_t size;
253-
switch (header.type % 100)
254-
{
255-
case MatVer4Type_DOUBLE:
256-
size = sizeof(double);
257-
break;
258-
case MatVer4Type_SINGLE:
259-
size = sizeof(float);
260-
break;
261-
case MatVer4Type_INT32:
262-
size = sizeof(int32_t);
263-
break;
264-
case MatVer4Type_CHAR:
265-
size = sizeof(uint8_t);
266-
break;
267-
default:
268-
return -1;
269-
}
270-
271157
// skip data
158+
MatVer4Type_t type = (MatVer4Type_t) (header.type % 100);
159+
size_t size = sizeofMatVer4Type(type);
272160
fseek(file, header.mrows*header.ncols*size, SEEK_CUR);
273-
return 0;
274161
}
275162

276163
#ifdef __cplusplus

SimulationRuntime/c/simulation/results/MatVer4.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,16 @@ typedef struct MatVer4Matrix
6262
void *data;
6363
} MatVer4Matrix;
6464

65-
int writeMatVer4Matrix_4(FILE* file, const char* name, size_t rows, size_t cols, const void* matrixData, MatVer4Type_t type);
66-
int appendMatVer4Matrix_4(FILE* file, long position, const char* name, size_t rows, size_t cols, const void* matrixData, MatVer4Type_t type);
67-
int writeMatVer4Header_4(FILE* file, long position, const char* name, size_t rows, size_t cols, MatVer4Type_t type);
65+
size_t sizeofMatVer4Type(MatVer4Type_t type);
6866

69-
MatVer4Matrix* readMatVer4Matrix_4(FILE* file);
70-
void freeMatVer4Matrix_4(MatVer4Matrix** matrix);
67+
void writeMatrix_matVer4(FILE* file, const char* name, size_t rows, size_t cols, const void* matrixData, MatVer4Type_t type);
68+
void updateHeader_matVer4(FILE* file, long position, const char* name, size_t rows, size_t additional_cols, MatVer4Type_t type);
69+
void appendMatrix_matVer4(FILE* file, long position, const char* name, size_t rows, size_t cols, const void* matrixData, MatVer4Type_t type);
7170

72-
int skipMatVer4Matrix_4(FILE* file);
71+
MatVer4Matrix* readMatrix_matVer4(FILE* file);
72+
void freeMatrix_matVer4(MatVer4Matrix** matrix);
73+
74+
void skipMatrix_matVer4(FILE* file);
7375

7476
#ifdef __cplusplus
7577
}

SimulationRuntime/c/simulation/results/simulation_result_mat4.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ typedef struct mat_data {
5555
size_t nSignals;
5656
size_t nEmits;
5757
void* data_2;
58+
MatVer4Type_t type;
5859
} mat_data;
5960

6061
static const char timeName[] = "time";
@@ -74,6 +75,8 @@ void mat4_init4(simulation_result *self, DATA *data, threadData_t *threadData)
7475

7576
rt_tick(SIM_TIMER_OUTPUT);
7677

78+
matData->type = omc_flag[FLAG_SINGLE_PRECISION] ? MatVer4Type_SINGLE : MatVer4Type_DOUBLE;
79+
7780
matData->pFile = fopen(self->filename, "wb+");
7881
if (!matData->pFile)
7982
{
@@ -86,7 +89,7 @@ void mat4_init4(simulation_result *self, DATA *data, threadData_t *threadData)
8689
// Class Type: Character Array
8790
// Data Type: 8-bit, unsigned integer
8891
const char Aclass[] = "A1\0bt.\0ir1\0na\0\0Tj\0\0re\0\0ac\0\0nt\0\0so\0\0\0r\0\0\0y\0\0\0";
89-
writeMatVer4Matrix_4(matData->pFile, "Aclass", 4, 11, Aclass, MatVer4Type_CHAR);
92+
writeMatrix_matVer4(matData->pFile, "Aclass", 4, 11, Aclass, MatVer4Type_CHAR);
9093

9194
/* Find the longest var name and description. */
9295
size_t maxLengthName = strlen(timeName) + 1;
@@ -309,7 +312,7 @@ void mat4_init4(simulation_result *self, DATA *data, threadData_t *threadData)
309312
// Dimensions: maxLength x nVars
310313
// Class Type: Character Array
311314
// Data Type: 8-bit, unsigned integer
312-
writeMatVer4Matrix_4(matData->pFile, "name", maxLengthName, matData->nSignals, name, MatVer4Type_CHAR);
315+
writeMatrix_matVer4(matData->pFile, "name", maxLengthName, matData->nSignals, name, MatVer4Type_CHAR);
313316
free(name);
314317
name = NULL;
315318

@@ -318,7 +321,7 @@ void mat4_init4(simulation_result *self, DATA *data, threadData_t *threadData)
318321
// Dimensions: maxLength x nVars
319322
// Class Type: Character Array
320323
// Data Type: 8-bit, unsigned integer
321-
writeMatVer4Matrix_4(matData->pFile, "description", maxLengthDesc, matData->nSignals, description, MatVer4Type_CHAR);
324+
writeMatrix_matVer4(matData->pFile, "description", maxLengthDesc, matData->nSignals, description, MatVer4Type_CHAR);
322325
free(description);
323326
description = NULL;
324327
rt_accumulate(SIM_TIMER_OUTPUT);
@@ -565,11 +568,11 @@ void mat4_writeParameterData4(simulation_result *self, DATA *data, threadData_t
565568
// Dimensions: 4 x nVars
566569
// Class Type: 32-bit, signed integer array
567570
// Data Type: 32-bit, signed integer
568-
writeMatVer4Matrix_4(matData->pFile, "dataInfo", 4, matData->nSignals, dataInfo, MatVer4Type_INT32);
571+
writeMatrix_matVer4(matData->pFile, "dataInfo", 4, matData->nSignals, dataInfo, MatVer4Type_INT32);
569572
free(dataInfo);
570573
dataInfo = NULL;
571574

572-
size_t size = omc_flag[FLAG_SINGLE_PRECISION] ? sizeof(float) : sizeof(double);
575+
size_t size = sizeofMatVer4Type(matData->type);
573576
cur = 0;
574577
void* data_1 = malloc(size * matData->nData1 * 2);
575578

@@ -624,7 +627,7 @@ void mat4_writeParameterData4(simulation_result *self, DATA *data, threadData_t
624627
// Dimensions: nParams x 2
625628
// Class Type: Double Precision Array
626629
// Data Type: IEEE 754 double-precision
627-
writeMatVer4Matrix_4(matData->pFile, "data_1", matData->nData1, 2, data_1, omc_flag[FLAG_SINGLE_PRECISION] ? MatVer4Type_SINGLE : MatVer4Type_DOUBLE);
630+
writeMatrix_matVer4(matData->pFile, "data_1", matData->nData1, 2, data_1, matData->type);
628631
if (data_1)
629632
{
630633
free(data_1);
@@ -638,7 +641,7 @@ void mat4_writeParameterData4(simulation_result *self, DATA *data, threadData_t
638641
// Data Type: IEEE 754 double-precision
639642
matData->data2HdrPos = ftell(matData->pFile);
640643
matData->data_2 = malloc(size * matData->nData2);
641-
writeMatVer4Matrix_4(matData->pFile, "data_2", matData->nData2, 0, NULL, omc_flag[FLAG_SINGLE_PRECISION] ? MatVer4Type_SINGLE : MatVer4Type_DOUBLE);
644+
writeMatrix_matVer4(matData->pFile, "data_2", matData->nData2, 0, NULL, matData->type);
642645
rt_accumulate(SIM_TIMER_OUTPUT);
643646
}
644647

@@ -688,8 +691,8 @@ void mat4_emit4(simulation_result *self, DATA *data, threadData_t *threadData)
688691
if (mData->booleanAlias[i].negate)
689692
WRITE_REAL_VALUE(matData->data_2, cur++, (1-data->localData[0]->booleanVars[mData->booleanAlias[i].nameID]));
690693

691-
//appendMatVer4Matrix_4(matData->pFile, matData->data2HdrPos, "data_2", matData->nData2, 1, matData->data_2, omc_flag[FLAG_SINGLE_PRECISION] ? MatVer4Type_SINGLE : MatVer4Type_DOUBLE);
692-
fwrite(matData->data_2, omc_flag[FLAG_SINGLE_PRECISION] ? sizeof(float) : sizeof(double), matData->nData2, matData->pFile);
694+
//appendMatVer4Matrix_4(matData->pFile, matData->data2HdrPos, "data_2", matData->nData2, 1, matData->data_2, matData->type);
695+
fwrite(matData->data_2, sizeofMatVer4Type(matData->type), matData->nData2, matData->pFile);
693696
matData->nEmits++;
694697

695698
rt_accumulate(SIM_TIMER_OUTPUT);
@@ -706,7 +709,7 @@ void mat4_free4(simulation_result *self, DATA *data, threadData_t *threadData)
706709
return;
707710
}
708711

709-
writeMatVer4Header_4(matData->pFile, matData->data2HdrPos, "data_2", matData->nData2, matData->nEmits, omc_flag[FLAG_SINGLE_PRECISION] ? MatVer4Type_SINGLE : MatVer4Type_DOUBLE);
712+
updateHeader_matVer4(matData->pFile, matData->data2HdrPos, "data_2", matData->nData2, matData->nEmits, matData->type);
710713

711714
if (matData->data_2) {
712715
free(matData->data_2);

0 commit comments

Comments
 (0)