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