Skip to content

Commit 174b9a4

Browse files
move DeltaDelayModel::read and DeltaDelayModel::write to its own file
1 parent b27ec50 commit 174b9a4

File tree

2 files changed

+87
-67
lines changed

2 files changed

+87
-67
lines changed

vpr/src/place/timing/delay_model/delta_delay_model.cpp

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33

44
#include "compute_delta_delays_utils.h"
55

6+
#ifdef VTR_ENABLE_CAPNPROTO
7+
# include "capnp/serialize.h"
8+
# include "place_delay_model.capnp.h"
9+
# include "ndmatrix_serdes.h"
10+
# include "mmap_file.h"
11+
# include "serdes_utils.h"
12+
#endif // VTR_ENABLE_CAPNPROTO
13+
614
void DeltaDelayModel::compute(RouterDelayProfiler& route_profiler,
715
const t_placer_opts& placer_opts,
816
const t_router_opts& router_opts,
@@ -46,3 +54,82 @@ void DeltaDelayModel::dump_echo(std::string filepath) const {
4654
vtr::fclose(f);
4755
}
4856

57+
void DeltaDelayModel::read(const std::string& file) {
58+
#ifndef VTR_ENABLE_CAPNPROTO
59+
VPR_THROW(VPR_ERROR_PLACE,
60+
"OverrideDelayModel::read is disabled because VTR_ENABLE_CAPNPROTO=OFF. "
61+
"Re-compile with CMake option VTR_ENABLE_CAPNPROTO=ON to enable.";
62+
#else
63+
64+
// MmapFile object creates an mmap of the specified path, and will munmap
65+
// when the object leaves scope.
66+
MmapFile f(file);
67+
68+
/* Increase reader limit to 1G words to allow for large files. */
69+
::capnp::ReaderOptions opts = default_large_capnp_opts();
70+
71+
// FlatArrayMessageReader is used to read the message from the data array
72+
// provided by MmapFile.
73+
::capnp::FlatArrayMessageReader reader(f.getData(), opts);
74+
75+
// When reading capnproto files the Reader object to use is named
76+
// <schema name>::Reader.
77+
//
78+
// Initially this object is an empty VprDeltaDelayModel.
79+
VprDeltaDelayModel::Reader model;
80+
81+
// The reader.getRoot performs a cast from the generic capnproto to fit
82+
// with the specified schema.
83+
//
84+
// Note that capnproto does not validate that the incoming data matches the
85+
// schema. If this property is required, some form of check would be
86+
// required.
87+
model = reader.getRoot<VprDeltaDelayModel>();
88+
89+
auto toFloat = [](float* out, const VprFloatEntry::Reader& in) -> void {
90+
*out = in.getValue();
91+
};
92+
93+
// ToNdMatrix is a generic function for converting a Matrix capnproto
94+
// to a vtr::NdMatrix.
95+
//
96+
// The user must supply the matrix dimension (2 in this case), the source
97+
// capnproto type (VprFloatEntry),
98+
// target C++ type (flat), and a function to convert from the source capnproto
99+
// type to the target C++ type (ToFloat).
100+
//
101+
// The second argument should be of type Matrix<X>::Reader where X is the
102+
// capnproto element type.
103+
ToNdMatrix<4, VprFloatEntry, float>(&delays_, model.getDelays(), toFloat);
104+
#endif
105+
}
106+
107+
void DeltaDelayModel::write(const std::string& file) const {
108+
#ifndef VTR_ENABLE_CAPNPROTO
109+
VPR_THROW(VPR_ERROR_PLACE,
110+
"DeltaDelayModel::write is disabled because VTR_ENABLE_CAPNPROTO=OFF. "
111+
"Re-compile with CMake option VTR_ENABLE_CAPNPROTO=ON to enable.";
112+
#else
113+
114+
// MallocMessageBuilder object is the generate capnproto message builder,
115+
// using malloc for buffer allocation.
116+
::capnp::MallocMessageBuilder builder;
117+
118+
// initRoot<X> returns a X::Builder object that can be used to set the
119+
// fields in the message.
120+
auto model = builder.initRoot<VprDeltaDelayModel>();
121+
122+
auto fromFloat = [](VprFloatEntry::Builder* out, const float& in) -> void {
123+
out->setValue(in);
124+
};
125+
126+
// FromNdMatrix is a generic function for converting a vtr::NdMatrix to a
127+
// Matrix message. It is the mirror function of ToNdMatrix described in
128+
// read above.
129+
auto delay_values = model.getDelays();
130+
FromNdMatrix<4, VprFloatEntry, float>(&delay_values, delays_, fromFloat);
131+
132+
// writeMessageToFile writes message to the specified file.
133+
writeMessageToFile(file, &builder);
134+
#endif
135+
}

vpr/src/place/timing/delay_model/override_delay_model.cpp

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -204,19 +204,10 @@ void OverrideDelayModel::set_base_delay_model(std::unique_ptr<DeltaDelayModel> b
204204
* VTR_ENABLE_CAPNPROTO=OFF. Generally this means throwing an exception instead.
205205
*/
206206
#ifndef VTR_ENABLE_CAPNPROTO
207-
208207
# define DISABLE_ERROR \
209208
"is disable because VTR_ENABLE_CAPNPROTO=OFF." \
210209
"Re-compile with CMake option VTR_ENABLE_CAPNPROTO=ON to enable."
211210

212-
void DeltaDelayModel::read(const std::string& /*file*/) {
213-
VPR_THROW(VPR_ERROR_PLACE, "DeltaDelayModel::read " DISABLE_ERROR);
214-
}
215-
216-
void DeltaDelayModel::write(const std::string& /*file*/) const {
217-
VPR_THROW(VPR_ERROR_PLACE, "DeltaDelayModel::write " DISABLE_ERROR);
218-
}
219-
220211
void OverrideDelayModel::read(const std::string& /*file*/) {
221212
VPR_THROW(VPR_ERROR_PLACE, "OverrideDelayModel::read " DISABLE_ERROR);
222213
}
@@ -237,64 +228,6 @@ static void FromFloat(VprFloatEntry::Builder* out, const float& in) {
237228
out->setValue(in);
238229
}
239230

240-
void DeltaDelayModel::read(const std::string& file) {
241-
// MmapFile object creates an mmap of the specified path, and will munmap
242-
// when the object leaves scope.
243-
MmapFile f(file);
244-
245-
/* Increase reader limit to 1G words to allow for large files. */
246-
::capnp::ReaderOptions opts = default_large_capnp_opts();
247-
248-
// FlatArrayMessageReader is used to read the message from the data array
249-
// provided by MmapFile.
250-
::capnp::FlatArrayMessageReader reader(f.getData(), opts);
251-
252-
// When reading capnproto files the Reader object to use is named
253-
// <schema name>::Reader.
254-
//
255-
// Initially this object is an empty VprDeltaDelayModel.
256-
VprDeltaDelayModel::Reader model;
257-
258-
// The reader.getRoot performs a cast from the generic capnproto to fit
259-
// with the specified schema.
260-
//
261-
// Note that capnproto does not validate that the incoming data matches the
262-
// schema. If this property is required, some form of check would be
263-
// required.
264-
model = reader.getRoot<VprDeltaDelayModel>();
265-
266-
// ToNdMatrix is a generic function for converting a Matrix capnproto
267-
// to a vtr::NdMatrix.
268-
//
269-
// The user must supply the matrix dimension (2 in this case), the source
270-
// capnproto type (VprFloatEntry),
271-
// target C++ type (flat), and a function to convert from the source capnproto
272-
// type to the target C++ type (ToFloat).
273-
//
274-
// The second argument should be of type Matrix<X>::Reader where X is the
275-
// capnproto element type.
276-
ToNdMatrix<4, VprFloatEntry, float>(&delays_, model.getDelays(), ToFloat);
277-
}
278-
279-
void DeltaDelayModel::write(const std::string& file) const {
280-
// MallocMessageBuilder object is the generate capnproto message builder,
281-
// using malloc for buffer allocation.
282-
::capnp::MallocMessageBuilder builder;
283-
284-
// initRoot<X> returns a X::Builder object that can be used to set the
285-
// fields in the message.
286-
auto model = builder.initRoot<VprDeltaDelayModel>();
287-
288-
// FromNdMatrix is a generic function for converting a vtr::NdMatrix to a
289-
// Matrix message. It is the mirror function of ToNdMatrix described in
290-
// read above.
291-
auto delay_values = model.getDelays();
292-
FromNdMatrix<4, VprFloatEntry, float>(&delay_values, delays_, FromFloat);
293-
294-
// writeMessageToFile writes message to the specified file.
295-
writeMessageToFile(file, &builder);
296-
}
297-
298231
void OverrideDelayModel::read(const std::string& file) {
299232
MmapFile f(file);
300233

0 commit comments

Comments
 (0)