Linux GNU 11.4.0 Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 73.9% 34 / 0 / 46
Functions: 50.0% 3 / 1 / 7
Branches: 76.5% 26 / 0 / 34

OMCompiler/Compiler/SimCode/SerializeSparsityPattern.mo
Line Branch Exec Source
1 /*
2 * This file is part of OpenModelica.
3 *
4 * Copyright (c) 1998-2026, Open Source Modelica Consortium (OSMC),
5 * c/o Linköpings universitet, Department of Computer and Information Science,
6 * SE-58183 Linköping, Sweden.
7 *
8 * All rights reserved.
9 *
10 * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF AGPL VERSION 3 LICENSE OR
11 * THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.8.
12 * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
13 * RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GNU AGPL
14 * VERSION 3, ACCORDING TO RECIPIENTS CHOICE.
15 *
16 * The OpenModelica software and the OSMC (Open Source Modelica Consortium)
17 * Public License (OSMC-PL) are obtained from OSMC, either from the above
18 * address, from the URLs:
19 * http://www.openmodelica.org or
20 * https://github.com/OpenModelica/ or
21 * http://www.ida.liu.se/projects/OpenModelica,
22 * and in the OpenModelica distribution.
23 *
24 * GNU AGPL version 3 is obtained from:
25 * https://www.gnu.org/licenses/licenses.html#GPL
26 *
27 * This program is distributed WITHOUT ANY WARRANTY; without
28 * even the implied warranty of MERCHANTABILITY or FITNESS
29 * FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
30 * IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
31 *
32 * See the full OSMC Public License conditions for more details.
33 *
34 */
35
36 encapsulated package SerializeSparsityPattern
37
38 import List;
39 import SimCode;
40 import Util;
41
42 function serialize
43 input SimCode.SimCode code;
44 output String dummy = "";
45 protected
46 array<Integer> columnPointers, rowIndices, columns;
47 String fname;
48 list<tuple<Integer, list<Integer>>> pattern;
49 list<list<Integer>> colorList;
50 algorithm
51
2/2
✓ Branch 0 taken 10337 times.
✓ Branch 1 taken 1204 times.
11541 for jac in code.jacobianMatrices loop
52 // NBackEnd jacobians carry sparsity in sparsityMatrix (SPARSITY) and use
53 // computeColumnColoring at runtime — no .bin file is needed for them.
54 // Old backend jacobians have sparsityMatrix = EMPTY() and store integer-indexed
55 // sparsity/coloring in sparsity/coloredCols/coloredRows, which we serialize here.
56 if match jac.sparsityMatrix case SimCode.Sparsity.EMPTY() then true; else false; end match then
57 // pick sparsity and coloring depending on isAdjoint boolean
58
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10171 times.
10171 if jac.isAdjoint then
59 ✗ pattern := jac.sparsityT;
60 // if adjoint then row coloring must exist else fail
61 ✗ if not listEmpty(jac.coloredRows) then
62 colorList := jac.coloredRows;
63 else
64 ✗ Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed because no row coloring for the adjoint jacobian exists."});
65 ✗ fail();
66 end if;
67 else
68 10171 pattern := jac.sparsity;
69 10171 colorList := jac.coloredCols;
70 end if;
71
72
2/2
✓ Branch 0 taken 1934 times.
✓ Branch 1 taken 8237 times.
10171 if not listEmpty(pattern) then
73 1934 fname := code.fileNamePrefix + "_Jac" + jac.matrixName + ".bin";
74
4/4
✓ Branch 0 taken 9294 times.
✓ Branch 1 taken 1934 times.
✓ Branch 2 taken 9294 times.
✓ Branch 3 taken 1934 times.
11228 columnPointers := listArray(0 :: list(listLength(Util.tuple22(column)) for column in pattern));
75
4/4
✓ Branch 0 taken 9294 times.
✓ Branch 1 taken 1934 times.
✓ Branch 2 taken 9294 times.
✓ Branch 3 taken 1934 times.
11228 rowIndices := listArray(List.flatten(list(Util.tuple22(column) for column in pattern)));
76 serializeJacobian(fname, arrayLength(columnPointers), arrayLength(rowIndices), columnPointers, rowIndices);
77
2/2
✓ Branch 0 taken 4415 times.
✓ Branch 1 taken 1934 times.
6349 for color in colorList loop
78 4415 columns := listArray(color);
79 serializeColor(fname, arrayLength(columns), columns);
80 end for;
81 end if;
82 end if;
83 end for;
84 end serialize;
85
86 // *********************
87 // write to binary stuff
88 // *********************
89
90 protected
91 3138 function serializeJacobian
92 input String name;
93 input Integer numCols;
94 input Integer nnz;
95 input array<Integer> colPtrs;
96 input array<Integer> rowInds;
97 external "C" serializeJ(name, numCols, nnz, colPtrs, rowInds) annotation(Include="
98 extern FILE* omc_fopen(const char *filename, const char *mode);
99 extern size_t omc_fwrite(void *buffer, size_t size, size_t count, FILE *stream);
100
101 1934 static void serializeJ(const char* name, int numCols, int nnz, modelica_metatype colPtrs, modelica_metatype rowInds)
102 {
103 unsigned int i, j;
104 size_t count;
105 1934 FILE* pFile = omc_fopen(name, \"wb\");
106
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1934 times.
1934 if (pFile == NULL) {
107 ✗ throwStreamPrint(NULL, \"Could not open sparsity pattern file %s.\", name);
108 }
109
110 /* compute and write sparsePattern->leadindex */
111 1934 j = 0;
112
2/2
✓ Branch 0 taken 11228 times.
✓ Branch 1 taken 1934 times.
13162 for (i = 0; i < numCols; i++) {
113 11228 j += (unsigned int) MMC_UNTAGFIXNUM(MMC_STRUCTDATA(colPtrs)[i]);
114 11228 count = omc_fwrite(&j, sizeof(unsigned int), 1, pFile);
115
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11228 times.
11228 if (count != 1) {
116 ✗ throwStreamPrint(NULL, \"Error while writing sparsePattern->leadindex. Expected %d, got %zu\", 1, count);
117 }
118 }
119
120 /* write sparsePattern->index */
121
2/2
✓ Branch 0 taken 47014 times.
✓ Branch 1 taken 1934 times.
48948 for (i = 0; i < nnz; i++) {
122 47014 j = (unsigned int) MMC_UNTAGFIXNUM(MMC_STRUCTDATA(rowInds)[i]);
123 47014 count = omc_fwrite(&j, sizeof(unsigned int), 1, pFile);
124
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47014 times.
47014 if (count != 1) {
125 ✗ throwStreamPrint(NULL, \"Error while writing sparsePattern->index. Expected %d, got %zu\", 1, count);
126 }
127 }
128
129 1934 fclose(pFile);
130 1934 }
131 ");
132 end serializeJacobian;
133
134 ✗ function serializeColor
135 input String name;
136 input Integer size;
137 input array<Integer> columns;
138 external "C" serializeC(name, size, columns) annotation(Include="
139 extern FILE* omc_fopen(const char *filename, const char *mode);
140 extern size_t omc_fwrite(void *buffer, size_t size, size_t count, FILE *stream);
141
142 4415 static void serializeC(const char* name, int size, modelica_metatype columns)
143 {
144 unsigned int i, j;
145 size_t count;
146 4415 FILE* pFile = fopen(name, \"ab\");
147
1/2
✓ Branch 0 taken 4415 times.
✗ Branch 1 not taken.
4415 if (pFile == NULL) {
148 ✗ throwStreamPrint(NULL, \"Could not open sparsity pattern file %s.\", name);
149 }
150
151 /* write sparsePattern->colorCols */
152
2/2
✓ Branch 0 taken 9294 times.
✓ Branch 1 taken 4415 times.
13709 for (i = 0; i < size; i++) {
153 9294 j = (unsigned int) MMC_UNTAGFIXNUM(MMC_STRUCTDATA(columns)[i]);
154 9294 count = omc_fwrite(&j, sizeof(unsigned int), 1, pFile);
155
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9294 times.
9294 if (count != 1) {
156 ✗ throwStreamPrint(NULL, \"Error while writing sparsePattern->colorCols. Expected %d, got %zu\", 1, count);
157 }
158 }
159
160 4415 fclose(pFile);
161 4415 }
162 ");
163 end serializeColor;
164
165 annotation(__OpenModelica_Interface="backend_tools");
166 end SerializeSparsityPattern;
167