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