1 // Ceres Solver - A fast non-linear least squares minimizer 2 // Copyright 2010, 2011, 2012 Google Inc. All rights reserved. 3 // http://code.google.com/p/ceres-solver/ 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 # 30 # Copyright 2011 Google Inc. All Rights Reserved. 31 # Author: sameeragarwal (at] google.com (Sameer Agarwal) 32 # 33 # Script for explicitly generating template specialization of the 34 # SchurEliminator class. It is a rather large class 35 # and the number of explicit instantiations is also large. Explicitly 36 # generating these instantiations in separate .cc files breaks the 37 # compilation into separate compilation unit rather than one large cc 38 # file which takes 2+GB of RAM to compile. 39 # 40 # This script creates two sets of files. 41 # 42 # 1. schur_eliminator_x_x_x.cc 43 # where, the x indicates the template parameters and 44 # 45 # 2. schur_eliminator.cc 46 # 47 # that contains a factory function for instantiating these classes 48 # based on runtime parameters. 49 # 50 # The list of tuples, specializations indicates the set of 51 # specializations that is generated. 52 53 # Set of template specializations to generate 54 SPECIALIZATIONS = [(2, 2, 2), 55 (2, 2, 3), 56 (2, 2, 4), 57 (2, 2, "Dynamic"), 58 (2, 3, 3), 59 (2, 3, 4), 60 (2, 3, 9), 61 (2, 3, "Dynamic"), 62 (2, 4, 3), 63 (2, 4, 4), 64 (2, 4, "Dynamic"), 65 (4, 4, 2), 66 (4, 4, 3), 67 (4, 4, 4), 68 (4, 4, "Dynamic"), 69 ("Dynamic", "Dynamic", "Dynamic")] 70 71 SPECIALIZATION_FILE = """// Copyright 2011 Google Inc. All Rights Reserved. 72 // Author: sameeragarwal (at] google.com (Sameer Agarwal) 73 // 74 // Template specialization of SchurEliminator. 75 // 76 // ======================================== 77 // THIS FILE IS AUTOGENERATED. DO NOT EDIT. 78 // THIS FILE IS AUTOGENERATED. DO NOT EDIT. 79 // THIS FILE IS AUTOGENERATED. DO NOT EDIT. 80 // THIS FILE IS AUTOGENERATED. DO NOT EDIT. 81 //========================================= 82 // 83 // This file is generated using generate_eliminator_specializations.py. 84 // Editing it manually is not recommended. 85 86 #include "ceres/schur_eliminator_impl.h" 87 #include "ceres/internal/eigen.h" 88 89 namespace ceres { 90 namespace internal { 91 92 template class SchurEliminator<%s, %s, %s>; 93 94 } // namespace internal 95 } // namespace ceres 96 97 """ 98 99 FACTORY_FILE_HEADER = """// Copyright 2011 Google Inc. All Rights Reserved. 100 // Author: sameeragarwal (at] google.com (Sameer Agarwal) 101 // 102 // ======================================== 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 // THIS FILE IS AUTOGENERATED. DO NOT EDIT. 107 //========================================= 108 // 109 // This file is generated using generate_template_specializations.py. 110 // Editing it manually is not recommended. 111 112 #include "ceres/linear_solver.h" 113 #include "ceres/schur_eliminator.h" 114 #include "ceres/internal/eigen.h" 115 116 namespace ceres { 117 namespace internal { 118 119 SchurEliminatorBase* 120 SchurEliminatorBase::Create(const LinearSolver::Options& options) { 121 #ifndef CERES_RESTRICT_SCHUR_SPECIALIZATION 122 """ 123 124 FACTORY_CONDITIONAL = """ if ((options.row_block_size == %s) && 125 (options.e_block_size == %s) && 126 (options.f_block_size == %s)) { 127 return new SchurEliminator<%s, %s, %s>(options); 128 } 129 """ 130 131 FACTORY_FOOTER = """ 132 #endif 133 VLOG(1) << "Template specializations not found for <" 134 << options.row_block_size << "," 135 << options.e_block_size << "," 136 << options.f_block_size << ">"; 137 return new SchurEliminator<Dynamic, Dynamic, Dynamic>(options); 138 } 139 140 } // namespace internal 141 } // namespace ceres 142 """ 143 144 145 def SuffixForSize(size): 146 if size == "Dynamic": 147 return "d" 148 return str(size) 149 150 151 def SpecializationFilename(prefix, row_block_size, e_block_size, f_block_size): 152 return "_".join([prefix] + map(SuffixForSize, (row_block_size, 153 e_block_size, 154 f_block_size))) 155 156 157 def Specialize(): 158 """ 159 Generate specialization code and the conditionals to instantiate it. 160 """ 161 f = open("schur_eliminator.cc", "w") 162 f.write(FACTORY_FILE_HEADER) 163 164 for row_block_size, e_block_size, f_block_size in SPECIALIZATIONS: 165 output = SpecializationFilename("generated/schur_eliminator", 166 row_block_size, 167 e_block_size, 168 f_block_size) + ".cc" 169 fptr = open(output, "w") 170 fptr.write(SPECIALIZATION_FILE % (row_block_size, 171 e_block_size, 172 f_block_size)) 173 fptr.close() 174 175 f.write(FACTORY_CONDITIONAL % (row_block_size, 176 e_block_size, 177 f_block_size, 178 row_block_size, 179 e_block_size, 180 f_block_size)) 181 f.write(FACTORY_FOOTER) 182 f.close() 183 184 185 if __name__ == "__main__": 186 Specialize() 187