|
3 | 3 |
|
4 | 4 | #include "compute_delta_delays_utils.h"
|
5 | 5 |
|
| 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 | + |
6 | 14 | void DeltaDelayModel::compute(RouterDelayProfiler& route_profiler,
|
7 | 15 | const t_placer_opts& placer_opts,
|
8 | 16 | const t_router_opts& router_opts,
|
@@ -46,3 +54,82 @@ void DeltaDelayModel::dump_echo(std::string filepath) const {
|
46 | 54 | vtr::fclose(f);
|
47 | 55 | }
|
48 | 56 |
|
| 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 | +} |
0 commit comments