|
| 1 | +Capnproto usage in VTR |
| 2 | +====================== |
| 3 | + |
| 4 | +Capnproto is a data serialization framework designed for portabliity and speed. |
| 5 | +In VPR, capnproto is used to provide binary formats for internal data |
| 6 | +structures that can be computed once, and used many times. Specific examples: |
| 7 | + - rrgraph |
| 8 | + - Router lookahead data |
| 9 | + - Place matrix delay estimates |
| 10 | + |
| 11 | +What is capnproto? |
| 12 | +================== |
| 13 | + |
| 14 | +capnproto can be broken down into 3 parts: |
| 15 | + - A schema language |
| 16 | + - A code generator |
| 17 | + - A library |
| 18 | + |
| 19 | +The schema language is used to define messages. Each message must have an |
| 20 | +explcit capnproto schema, which are stored in files suffixed with ".capnp". |
| 21 | +The capnproto documentation for how to write these schema files can be found |
| 22 | +here: https://capnproto.org/language.html |
| 23 | + |
| 24 | +The schema by itself is not especially useful. In order to read and write |
| 25 | +messages defined by the schema in a target language (e.g. C++), a code |
| 26 | +generation step is required. Capnproto provides a cmake function for this |
| 27 | +purpose, `capnp_generate_cpp`. This generates C++ source and header files. |
| 28 | +These source and header files combined with the capnproto C++ library, enables |
| 29 | +C++ code to read and write the messages matching a particular schema. The C++ |
| 30 | +library API can be found here: https://capnproto.org/cxx.html |
| 31 | + |
| 32 | +Contents of libvtrcapnproto |
| 33 | +=========================== |
| 34 | + |
| 35 | +libvtrcapnproto should contain two elements: |
| 36 | + - Utilities for working capnproto messages in VTR |
| 37 | + - Generate source and header files of all capnproto messages used in VTR |
| 38 | + |
| 39 | +I/O Utilities |
| 40 | +------------- |
| 41 | + |
| 42 | +Capnproto does not provide IO support, instead it works from arrays (or file |
| 43 | +descriptors). To avoid re-writing this code, libvtrcapnproto provides two |
| 44 | +utilities that should be used whenever reading or writing capnproto message to |
| 45 | +disk: |
| 46 | + - `serdes_utils.h` provides the writeMessageToFile function - Writes a |
| 47 | + capnproto message to disk. |
| 48 | + - `mmap_file.h` provides MmapFile object - Maps a capnproto message from the |
| 49 | + disk as a flat array. |
| 50 | + |
| 51 | +NdMatrix Utilities |
| 52 | +------------------ |
| 53 | + |
| 54 | +A common datatype which appears in many data structures that VPR might want to |
| 55 | +serialize is the generic type `vtr::NdMatrix`. `ndmatrix_serdes.h` provides |
| 56 | +generic functions ToNdMatrix and FromNdMatrix, which can be used to generically |
| 57 | +convert between the provideid capnproto message `Matrix` and `vtr::NdMatrix`. |
| 58 | + |
| 59 | +Capnproto schemas |
| 60 | +----------------- |
| 61 | + |
| 62 | +libvtrcapnproto should contain all capnproto schema definitions used within |
| 63 | +VTR. To add a new schema: |
| 64 | +1. Add the schema to git in `libs/libvtrcapnproto/` |
| 65 | +2. Add the schema file name to `capnp_generate_cpp` invocation in |
| 66 | + `libs/libvtrcapnproto/CMakeLists.txt`. |
| 67 | + |
| 68 | +The schema will be available in the header file `schema filename>.h`. The |
| 69 | +actual header file will appear in the CMake build directory |
| 70 | +`libs/libvtrcapnproto` after `libvtrcapnproto` has been rebuilt. |
| 71 | + |
| 72 | +Writing capnproto binary files to text |
| 73 | +====================================== |
| 74 | + |
| 75 | +The `capnp` tool (found in the CMake build directiory |
| 76 | +`libs/EXTERNAL/capnproto/c++/src/capnp`) can be used to convert from a binary |
| 77 | +capnp message to a textual form. |
| 78 | + |
| 79 | +Example converting VprOverrideDelayModel from binary to text: |
| 80 | + |
| 81 | +``` |
| 82 | +capnp convert binary:text place_delay_model.capnp VprOverrideDelayModel \ |
| 83 | + < place_delay.bin > place_delay.txt |
| 84 | +``` |
0 commit comments