Sameer Agarwal | 31730ef | 2013-02-28 11:20:28 -0800 | [diff] [blame] | 1 | # Ceres Solver - A fast non-linear least squares minimizer |
| 2 | # Copyright 2010, 2011, 2012, 2013 Google Inc. All rights reserved. |
| 3 | # http://code.google.com/p/ceres-solver/ |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 4 | # |
Sameer Agarwal | 31730ef | 2013-02-28 11:20:28 -0800 | [diff] [blame] | 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 | # |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 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 two sets of files. |
| 39 | # |
| 40 | # 1. schur_eliminator_x_x_x.cc |
| 41 | # where, the x indicates the template parameters and |
| 42 | # |
| 43 | # 2. schur_eliminator.cc |
| 44 | # |
| 45 | # that contains a factory function for instantiating these classes |
| 46 | # based on runtime parameters. |
| 47 | # |
| 48 | # The list of tuples, specializations indicates the set of |
| 49 | # specializations that is generated. |
| 50 | |
| 51 | # Set of template specializations to generate |
| 52 | SPECIALIZATIONS = [(2, 2, 2), |
| 53 | (2, 2, 3), |
| 54 | (2, 2, 4), |
Sameer Agarwal | 31730ef | 2013-02-28 11:20:28 -0800 | [diff] [blame] | 55 | (2, 2, "Eigen::Dynamic"), |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 56 | (2, 3, 3), |
| 57 | (2, 3, 4), |
| 58 | (2, 3, 9), |
Sameer Agarwal | 31730ef | 2013-02-28 11:20:28 -0800 | [diff] [blame] | 59 | (2, 3, "Eigen::Dynamic"), |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 60 | (2, 4, 3), |
| 61 | (2, 4, 4), |
Sameer Agarwal | 31730ef | 2013-02-28 11:20:28 -0800 | [diff] [blame] | 62 | (2, 4, "Eigen::Dynamic"), |
Sameer Agarwal | 63bcdff | 2013-10-27 21:34:13 -0700 | [diff] [blame] | 63 | (2, "Eigen::Dynamic", "Eigen::Dynamic"), |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 64 | (4, 4, 2), |
| 65 | (4, 4, 3), |
| 66 | (4, 4, 4), |
Sameer Agarwal | 31730ef | 2013-02-28 11:20:28 -0800 | [diff] [blame] | 67 | (4, 4, "Eigen::Dynamic"), |
| 68 | ("Eigen::Dynamic", "Eigen::Dynamic", "Eigen::Dynamic")] |
Sameer Agarwal | c59c1e4 | 2013-03-11 17:28:38 -0700 | [diff] [blame] | 69 | HEADER = """// Ceres Solver - A fast non-linear least squares minimizer |
Sameer Agarwal | 31730ef | 2013-02-28 11:20:28 -0800 | [diff] [blame] | 70 | // Copyright 2010, 2011, 2012, 2013 Google Inc. All rights reserved. |
| 71 | // http://code.google.com/p/ceres-solver/ |
| 72 | // |
| 73 | // Redistribution and use in source and binary forms, with or without |
| 74 | // modification, are permitted provided that the following conditions are met: |
| 75 | // |
| 76 | // * Redistributions of source code must retain the above copyright notice, |
| 77 | // this list of conditions and the following disclaimer. |
| 78 | // * Redistributions in binary form must reproduce the above copyright notice, |
| 79 | // this list of conditions and the following disclaimer in the documentation |
| 80 | // and/or other materials provided with the distribution. |
| 81 | // * Neither the name of Google Inc. nor the names of its contributors may be |
| 82 | // used to endorse or promote products derived from this software without |
| 83 | // specific prior written permission. |
| 84 | // |
| 85 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 86 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 87 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 88 | // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 89 | // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 90 | // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 91 | // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 92 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 93 | // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 94 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 95 | // POSSIBILITY OF SUCH DAMAGE. |
| 96 | // |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 97 | // Author: sameeragarwal@google.com (Sameer Agarwal) |
| 98 | // |
| 99 | // Template specialization of SchurEliminator. |
| 100 | // |
| 101 | // ======================================== |
| 102 | // THIS FILE IS AUTOGENERATED. DO NOT EDIT. |
| 103 | // THIS FILE IS AUTOGENERATED. DO NOT EDIT. |
| 104 | // THIS FILE IS AUTOGENERATED. DO NOT EDIT. |
| 105 | // THIS FILE IS AUTOGENERATED. DO NOT EDIT. |
| 106 | //========================================= |
| 107 | // |
Sameer Agarwal | 8140f0f | 2013-03-12 09:45:08 -0700 | [diff] [blame] | 108 | // This file is generated using generate_eliminator_specialization.py. |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 109 | // Editing it manually is not recommended. |
Sameer Agarwal | c59c1e4 | 2013-03-11 17:28:38 -0700 | [diff] [blame] | 110 | """ |
| 111 | |
| 112 | DYNAMIC_FILE = """ |
| 113 | |
| 114 | #include "ceres/schur_eliminator_impl.h" |
| 115 | #include "ceres/internal/eigen.h" |
| 116 | |
| 117 | namespace ceres { |
| 118 | namespace internal { |
| 119 | |
| 120 | template class SchurEliminator<%s, %s, %s>; |
| 121 | |
| 122 | } // namespace internal |
| 123 | } // namespace ceres |
| 124 | """ |
| 125 | |
| 126 | SPECIALIZATION_FILE = """ |
| 127 | #ifndef CERES_RESTRICT_SCHUR_SPECIALIZATION |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 128 | |
| 129 | #include "ceres/schur_eliminator_impl.h" |
| 130 | #include "ceres/internal/eigen.h" |
| 131 | |
| 132 | namespace ceres { |
| 133 | namespace internal { |
| 134 | |
| 135 | template class SchurEliminator<%s, %s, %s>; |
| 136 | |
| 137 | } // namespace internal |
| 138 | } // namespace ceres |
| 139 | |
Sameer Agarwal | c59c1e4 | 2013-03-11 17:28:38 -0700 | [diff] [blame] | 140 | #endif // CERES_RESTRICT_SCHUR_SPECIALIZATION |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 141 | """ |
| 142 | |
Sameer Agarwal | c59c1e4 | 2013-03-11 17:28:38 -0700 | [diff] [blame] | 143 | FACTORY_FILE_HEADER = """ |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 144 | #include "ceres/linear_solver.h" |
| 145 | #include "ceres/schur_eliminator.h" |
| 146 | #include "ceres/internal/eigen.h" |
| 147 | |
| 148 | namespace ceres { |
| 149 | namespace internal { |
| 150 | |
| 151 | SchurEliminatorBase* |
| 152 | SchurEliminatorBase::Create(const LinearSolver::Options& options) { |
| 153 | #ifndef CERES_RESTRICT_SCHUR_SPECIALIZATION |
| 154 | """ |
| 155 | |
| 156 | FACTORY_CONDITIONAL = """ if ((options.row_block_size == %s) && |
| 157 | (options.e_block_size == %s) && |
| 158 | (options.f_block_size == %s)) { |
| 159 | return new SchurEliminator<%s, %s, %s>(options); |
| 160 | } |
| 161 | """ |
| 162 | |
| 163 | FACTORY_FOOTER = """ |
| 164 | #endif |
| 165 | VLOG(1) << "Template specializations not found for <" |
| 166 | << options.row_block_size << "," |
| 167 | << options.e_block_size << "," |
| 168 | << options.f_block_size << ">"; |
Sameer Agarwal | 31730ef | 2013-02-28 11:20:28 -0800 | [diff] [blame] | 169 | return new SchurEliminator<Eigen::Dynamic, Eigen::Dynamic, Eigen::Dynamic>(options); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | } // namespace internal |
| 173 | } // namespace ceres |
| 174 | """ |
| 175 | |
| 176 | |
| 177 | def SuffixForSize(size): |
Sameer Agarwal | 31730ef | 2013-02-28 11:20:28 -0800 | [diff] [blame] | 178 | if size == "Eigen::Dynamic": |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 179 | return "d" |
| 180 | return str(size) |
| 181 | |
| 182 | |
| 183 | def SpecializationFilename(prefix, row_block_size, e_block_size, f_block_size): |
| 184 | return "_".join([prefix] + map(SuffixForSize, (row_block_size, |
| 185 | e_block_size, |
| 186 | f_block_size))) |
| 187 | |
| 188 | |
| 189 | def Specialize(): |
| 190 | """ |
| 191 | Generate specialization code and the conditionals to instantiate it. |
| 192 | """ |
| 193 | f = open("schur_eliminator.cc", "w") |
Sameer Agarwal | c59c1e4 | 2013-03-11 17:28:38 -0700 | [diff] [blame] | 194 | f.write(HEADER) |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 195 | f.write(FACTORY_FILE_HEADER) |
| 196 | |
| 197 | for row_block_size, e_block_size, f_block_size in SPECIALIZATIONS: |
| 198 | output = SpecializationFilename("generated/schur_eliminator", |
| 199 | row_block_size, |
| 200 | e_block_size, |
| 201 | f_block_size) + ".cc" |
| 202 | fptr = open(output, "w") |
Sameer Agarwal | c59c1e4 | 2013-03-11 17:28:38 -0700 | [diff] [blame] | 203 | fptr.write(HEADER) |
| 204 | |
| 205 | template = SPECIALIZATION_FILE |
| 206 | if (row_block_size == "Eigen::Dynamic" and |
| 207 | e_block_size == "Eigen::Dynamic" and |
Sameer Agarwal | 8140f0f | 2013-03-12 09:45:08 -0700 | [diff] [blame] | 208 | f_block_size == "Eigen::Dynamic"): |
Sameer Agarwal | c59c1e4 | 2013-03-11 17:28:38 -0700 | [diff] [blame] | 209 | template = DYNAMIC_FILE |
| 210 | |
Sameer Agarwal | 8140f0f | 2013-03-12 09:45:08 -0700 | [diff] [blame] | 211 | fptr.write(template % (row_block_size, e_block_size, f_block_size)) |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 212 | fptr.close() |
| 213 | |
| 214 | f.write(FACTORY_CONDITIONAL % (row_block_size, |
| 215 | e_block_size, |
| 216 | f_block_size, |
| 217 | row_block_size, |
| 218 | e_block_size, |
| 219 | f_block_size)) |
| 220 | f.write(FACTORY_FOOTER) |
| 221 | f.close() |
| 222 | |
| 223 | |
| 224 | if __name__ == "__main__": |
| 225 | Specialize() |