Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame^] | 1 | # Ceres Solver - A fast non-linear least squares minimizer |
| 2 | # Copyright 2015 Google Inc. All rights reserved. |
| 3 | # http://ceres-solver.org/ |
| 4 | # |
| 5 | # Redistribution and use in source and binary forms, with or without |
| 6 | # modification, are permitted provided that the following conditions are met: |
| 7 | # |
| 8 | # * Redistributions of source code must retain the above copyright notice, |
| 9 | # this list of conditions and the following disclaimer. |
| 10 | # * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | # this list of conditions and the following disclaimer in the documentation |
| 12 | # and/or other materials provided with the distribution. |
| 13 | # * Neither the name of Google Inc. nor the names of its contributors may be |
| 14 | # used to endorse or promote products derived from this software without |
| 15 | # specific prior written permission. |
| 16 | # |
| 17 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 21 | # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 25 | # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 26 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 27 | # POSSIBILITY OF SUCH DAMAGE. |
| 28 | # |
| 29 | # Author: sameeragarwal@google.com (Sameer Agarwal) |
| 30 | # |
| 31 | # Script for explicitly generating template specialization of the |
| 32 | # SchurEliminator class. It is a rather large class |
| 33 | # and the number of explicit instantiations is also large. Explicitly |
| 34 | # generating these instantiations in separate .cc files breaks the |
| 35 | # compilation into separate compilation unit rather than one large cc |
| 36 | # file which takes 2+GB of RAM to compile. |
| 37 | # |
| 38 | # This script creates three sets of files. |
| 39 | # |
| 40 | # 1. schur_eliminator_x_x_x.cc and partitioned_matrix_view_x_x_x.cc |
| 41 | # where, the x indicates the template parameters and |
| 42 | # |
| 43 | # 2. schur_eliminator.cc & partitioned_matrix_view.cc |
| 44 | # |
| 45 | # that contains a factory function for instantiating these classes |
| 46 | # based on runtime parameters. |
| 47 | # |
| 48 | # 3. schur_templates.cc |
| 49 | # |
| 50 | # that contains a function which can be queried to determine what |
| 51 | # template specializations are available. |
| 52 | # |
| 53 | # The following list of tuples, specializations indicates the set of |
| 54 | # specializations that is generated. |
| 55 | SPECIALIZATIONS = [(2, 2, 2), |
| 56 | (2, 2, 3), |
| 57 | (2, 2, 4), |
| 58 | (2, 2, "Eigen::Dynamic"), |
| 59 | (2, 3, 3), |
| 60 | (2, 3, 4), |
| 61 | (2, 3, 6), |
| 62 | (2, 3, 9), |
| 63 | (2, 3, "Eigen::Dynamic"), |
| 64 | (2, 4, 3), |
| 65 | (2, 4, 4), |
| 66 | (2, 4, 6), |
| 67 | (2, 4, 8), |
| 68 | (2, 4, 9), |
| 69 | (2, 4, "Eigen::Dynamic"), |
| 70 | (2, "Eigen::Dynamic", "Eigen::Dynamic"), |
| 71 | (4, 4, 2), |
| 72 | (4, 4, 3), |
| 73 | (4, 4, 4), |
| 74 | (4, 4, "Eigen::Dynamic")] |
| 75 | |
| 76 | import schur_eliminator_template |
| 77 | import partitioned_matrix_view_template |
| 78 | import os |
| 79 | import glob |
| 80 | |
| 81 | def SuffixForSize(size): |
| 82 | if size == "Eigen::Dynamic": |
| 83 | return "d" |
| 84 | return str(size) |
| 85 | |
| 86 | def SpecializationFilename(prefix, row_block_size, e_block_size, f_block_size): |
| 87 | return "_".join([prefix] + map(SuffixForSize, (row_block_size, |
| 88 | e_block_size, |
| 89 | f_block_size))) |
| 90 | |
| 91 | def GenerateFactoryConditional(row_block_size, e_block_size, f_block_size): |
| 92 | conditionals = [] |
| 93 | if (row_block_size != "Eigen::Dynamic"): |
| 94 | conditionals.append("(options.row_block_size == %s)" % row_block_size) |
| 95 | if (e_block_size != "Eigen::Dynamic"): |
| 96 | conditionals.append("(options.e_block_size == %s)" % e_block_size) |
| 97 | if (f_block_size != "Eigen::Dynamic"): |
| 98 | conditionals.append("(options.f_block_size == %s)" % f_block_size) |
| 99 | if (len(conditionals) == 0): |
| 100 | return "%s" |
| 101 | |
| 102 | if (len(conditionals) == 1): |
| 103 | return " if " + conditionals[0] + "{\n %s\n }\n" |
| 104 | |
| 105 | return " if (" + " &&\n ".join(conditionals) + ") {\n %s\n }\n" |
| 106 | |
| 107 | def Specialize(name, data): |
| 108 | """ |
| 109 | Generate specialization code and the conditionals to instantiate it. |
| 110 | """ |
| 111 | |
| 112 | # Specialization files |
| 113 | for row_block_size, e_block_size, f_block_size in SPECIALIZATIONS: |
| 114 | output = SpecializationFilename("generated/" + name, |
| 115 | row_block_size, |
| 116 | e_block_size, |
| 117 | f_block_size) + ".cc" |
| 118 | |
| 119 | with open(output, "w") as f: |
| 120 | f.write(data["HEADER"]) |
| 121 | f.write(data["SPECIALIZATION_FILE"] % |
| 122 | (row_block_size, e_block_size, f_block_size)) |
| 123 | |
| 124 | # Generate the _d_d_d specialization. |
| 125 | output = SpecializationFilename("generated/" + name, |
| 126 | "Eigen::Dynamic", |
| 127 | "Eigen::Dynamic", |
| 128 | "Eigen::Dynamic") + ".cc" |
| 129 | with open(output, "w") as f: |
| 130 | f.write(data["HEADER"]) |
| 131 | f.write(data["DYNAMIC_FILE"] % |
| 132 | ("Eigen::Dynamic", "Eigen::Dynamic", "Eigen::Dynamic")) |
| 133 | |
| 134 | # Factory |
| 135 | with open(name + ".cc", "w") as f: |
| 136 | f.write(data["HEADER"]) |
| 137 | f.write(data["FACTORY_FILE_HEADER"]) |
| 138 | for row_block_size, e_block_size, f_block_size in SPECIALIZATIONS: |
| 139 | factory_conditional = GenerateFactoryConditional( |
| 140 | row_block_size, e_block_size, f_block_size) |
| 141 | factory = data["FACTORY"] % (row_block_size, e_block_size, f_block_size) |
| 142 | f.write(factory_conditional % factory); |
| 143 | f.write(data["FACTORY_FOOTER"]) |
| 144 | |
| 145 | QUERY_HEADER = """// Ceres Solver - A fast non-linear least squares minimizer |
| 146 | // Copyright 2017 Google Inc. All rights reserved. |
| 147 | // http://ceres-solver.org/ |
| 148 | // |
| 149 | // Redistribution and use in source and binary forms, with or without |
| 150 | // modification, are permitted provided that the following conditions are met: |
| 151 | // |
| 152 | // * Redistributions of source code must retain the above copyright notice, |
| 153 | // this list of conditions and the following disclaimer. |
| 154 | // * Redistributions in binary form must reproduce the above copyright notice, |
| 155 | // this list of conditions and the following disclaimer in the documentation |
| 156 | // and/or other materials provided with the distribution. |
| 157 | // * Neither the name of Google Inc. nor the names of its contributors may be |
| 158 | // used to endorse or promote products derived from this software without |
| 159 | // specific prior written permission. |
| 160 | // |
| 161 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 162 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 163 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 164 | // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 165 | // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 166 | // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 167 | // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 168 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 169 | // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 170 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 171 | // POSSIBILITY OF SUCH DAMAGE. |
| 172 | // |
| 173 | // Author: sameeragarwal@google.com (Sameer Agarwal) |
| 174 | // |
| 175 | // What template specializations are available. |
| 176 | // |
| 177 | // ======================================== |
| 178 | // THIS FILE IS AUTOGENERATED. DO NOT EDIT. |
| 179 | // THIS FILE IS AUTOGENERATED. DO NOT EDIT. |
| 180 | // THIS FILE IS AUTOGENERATED. DO NOT EDIT. |
| 181 | // THIS FILE IS AUTOGENERATED. DO NOT EDIT. |
| 182 | //========================================= |
| 183 | // |
| 184 | // This file is generated using generate_template_specializations.py. |
| 185 | """ |
| 186 | |
| 187 | QUERY_FILE_HEADER = """ |
| 188 | #include "ceres/internal/eigen.h" |
| 189 | #include "ceres/schur_templates.h" |
| 190 | |
| 191 | namespace ceres { |
| 192 | namespace internal { |
| 193 | |
| 194 | void GetBestSchurTemplateSpecialization(int* row_block_size, |
| 195 | int* e_block_size, |
| 196 | int* f_block_size) { |
| 197 | LinearSolver::Options options; |
| 198 | options.row_block_size = *row_block_size; |
| 199 | options.e_block_size = *e_block_size; |
| 200 | options.f_block_size = *f_block_size; |
| 201 | *row_block_size = Eigen::Dynamic; |
| 202 | *e_block_size = Eigen::Dynamic; |
| 203 | *f_block_size = Eigen::Dynamic; |
| 204 | #ifndef CERES_RESTRICT_SCHUR_SPECIALIZATION |
| 205 | """ |
| 206 | |
| 207 | QUERY_FOOTER = """ |
| 208 | #endif |
| 209 | return; |
| 210 | } |
| 211 | |
| 212 | } // namespace internal |
| 213 | } // namespace ceres |
| 214 | """ |
| 215 | |
| 216 | QUERY_ACTION = """ *row_block_size = %s; |
| 217 | *e_block_size = %s; |
| 218 | *f_block_size = %s; |
| 219 | return;""" |
| 220 | |
| 221 | def GenerateQueryFile(): |
| 222 | """ |
| 223 | Generate file that allows querying for available template specializations. |
| 224 | """ |
| 225 | |
| 226 | with open("schur_templates.cc", "w") as f: |
| 227 | f.write(QUERY_HEADER) |
| 228 | f.write(QUERY_FILE_HEADER) |
| 229 | for row_block_size, e_block_size, f_block_size in SPECIALIZATIONS: |
| 230 | factory_conditional = GenerateFactoryConditional( |
| 231 | row_block_size, e_block_size, f_block_size) |
| 232 | action = QUERY_ACTION % (row_block_size, e_block_size, f_block_size) |
| 233 | f.write(factory_conditional % action) |
| 234 | f.write(QUERY_FOOTER) |
| 235 | |
| 236 | |
| 237 | if __name__ == "__main__": |
| 238 | for f in glob.glob("generated/*"): |
| 239 | os.remove(f) |
| 240 | |
| 241 | Specialize("schur_eliminator", |
| 242 | schur_eliminator_template.__dict__) |
| 243 | Specialize("partitioned_matrix_view", |
| 244 | partitioned_matrix_view_template.__dict__) |
| 245 | GenerateQueryFile() |