@@ -170,6 +170,14 @@ float SimpleDelayModel::delay(const t_physical_tile_loc& from_loc, int /*from_pi
170
170
" is disable because VTR_ENABLE_CAPNPROTO=OFF." \
171
171
" Re-compile with CMake option VTR_ENABLE_CAPNPROTO=ON to enable."
172
172
173
+ void SimpleDelayModel::read (const std::string& /* file*/ ) {
174
+ VPR_THROW (VPR_ERROR_PLACE, " SimpleDelayModel::read " DISABLE_ERROR);
175
+ }
176
+
177
+ void SimpleDelayModel::write (const std::string& /* file*/ ) const {
178
+ VPR_THROW (VPR_ERROR_PLACE, " SimpleDelayModel::write " DISABLE_ERROR);
179
+ }
180
+
173
181
void DeltaDelayModel::read (const std::string& /* file*/ ) {
174
182
VPR_THROW (VPR_ERROR_PLACE, " DeltaDelayModel::read " DISABLE_ERROR);
175
183
}
@@ -198,6 +206,64 @@ static void FromFloat(VprFloatEntry::Builder* out, const float& in) {
198
206
out->setValue (in);
199
207
}
200
208
209
+ void SimpleDelayModel::read (const std::string& file) {
210
+ // MmapFile object creates an mmap of the specified path, and will munmap
211
+ // when the object leaves scope.
212
+ MmapFile f (file);
213
+
214
+ /* Increase reader limit to 1G words to allow for large files. */
215
+ ::capnp::ReaderOptions opts = default_large_capnp_opts ();
216
+
217
+ // FlatArrayMessageReader is used to read the message from the data array
218
+ // provided by MmapFile.
219
+ ::capnp::FlatArrayMessageReader reader (f.getData (), opts);
220
+
221
+ // When reading capnproto files the Reader object to use is named
222
+ // <schema name>::Reader.
223
+ //
224
+ // Initially this object is an empty VprDeltaDelayModel.
225
+ VprDeltaDelayModel::Reader model;
226
+
227
+ // The reader.getRoot performs a cast from the generic capnproto to fit
228
+ // with the specified schema.
229
+ //
230
+ // Note that capnproto does not validate that the incoming data matches the
231
+ // schema. If this property is required, some form of check would be
232
+ // required.
233
+ model = reader.getRoot <VprDeltaDelayModel>();
234
+
235
+ // ToNdMatrix is a generic function for converting a Matrix capnproto
236
+ // to a vtr::NdMatrix.
237
+ //
238
+ // The user must supply the matrix dimension (5 in this case), the source
239
+ // capnproto type (VprFloatEntry),
240
+ // target C++ type (flat), and a function to convert from the source capnproto
241
+ // type to the target C++ type (ToFloat).
242
+ //
243
+ // The second argument should be of type Matrix<X>::Reader where X is the
244
+ // capnproto element type.
245
+ ToNdMatrix<5 , VprFloatEntry, float >(&delays_, model.getDelays (), ToFloat);
246
+ }
247
+
248
+ void SimpleDelayModel::write (const std::string& file) const {
249
+ // MallocMessageBuilder object generates capnproto message builder,
250
+ // using malloc for buffer allocation.
251
+ ::capnp::MallocMessageBuilder builder;
252
+
253
+ // initRoot<X> returns a X::Builder object that can be used to set the
254
+ // fields in the message.
255
+ auto model = builder.initRoot <VprDeltaDelayModel>();
256
+
257
+ // FromNdMatrix is a generic function for converting a vtr::NdMatrix to a
258
+ // Matrix message. It is the mirror function of ToNdMatrix described in
259
+ // read above.
260
+ auto delay_values = model.getDelays ();
261
+ FromNdMatrix<5 , VprFloatEntry, float >(&delay_values, delays_, FromFloat);
262
+
263
+ // writeMessageToFile writes message to the specified file.
264
+ writeMessageToFile (file, &builder);
265
+ }
266
+
201
267
void DeltaDelayModel::read (const std::string& file) {
202
268
// MmapFile object creates an mmap of the specified path, and will munmap
203
269
// when the object leaves scope.
0 commit comments