blob: 78f779f2b7050ef816f03475f3c1f5e0ffaae0ba [file] [log] [blame]
Sameer Agarwal31730ef2013-02-28 11:20:28 -08001# 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 Mierle8ebb0732012-04-30 23:09:08 -07004#
Sameer Agarwal31730ef2013-02-28 11:20:28 -08005# 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 Mierle8ebb0732012-04-30 23:09:08 -070029# 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
52SPECIALIZATIONS = [(2, 2, 2),
53 (2, 2, 3),
54 (2, 2, 4),
Sameer Agarwal31730ef2013-02-28 11:20:28 -080055 (2, 2, "Eigen::Dynamic"),
Keir Mierle8ebb0732012-04-30 23:09:08 -070056 (2, 3, 3),
57 (2, 3, 4),
58 (2, 3, 9),
Sameer Agarwal31730ef2013-02-28 11:20:28 -080059 (2, 3, "Eigen::Dynamic"),
Keir Mierle8ebb0732012-04-30 23:09:08 -070060 (2, 4, 3),
61 (2, 4, 4),
Sameer Agarwal31730ef2013-02-28 11:20:28 -080062 (2, 4, "Eigen::Dynamic"),
Sameer Agarwal63bcdff2013-10-27 21:34:13 -070063 (2, "Eigen::Dynamic", "Eigen::Dynamic"),
Keir Mierle8ebb0732012-04-30 23:09:08 -070064 (4, 4, 2),
65 (4, 4, 3),
66 (4, 4, 4),
Sameer Agarwal31730ef2013-02-28 11:20:28 -080067 (4, 4, "Eigen::Dynamic"),
68 ("Eigen::Dynamic", "Eigen::Dynamic", "Eigen::Dynamic")]
Sameer Agarwalc59c1e42013-03-11 17:28:38 -070069HEADER = """// Ceres Solver - A fast non-linear least squares minimizer
Sameer Agarwal31730ef2013-02-28 11:20:28 -080070// 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 Mierle8ebb0732012-04-30 23:09:08 -070097// 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 Agarwal8140f0f2013-03-12 09:45:08 -0700108// This file is generated using generate_eliminator_specialization.py.
Keir Mierle8ebb0732012-04-30 23:09:08 -0700109// Editing it manually is not recommended.
Sameer Agarwalc59c1e42013-03-11 17:28:38 -0700110"""
111
112DYNAMIC_FILE = """
113
114#include "ceres/schur_eliminator_impl.h"
115#include "ceres/internal/eigen.h"
116
117namespace ceres {
118namespace internal {
119
120template class SchurEliminator<%s, %s, %s>;
121
122} // namespace internal
123} // namespace ceres
124"""
125
126SPECIALIZATION_FILE = """
127#ifndef CERES_RESTRICT_SCHUR_SPECIALIZATION
Keir Mierle8ebb0732012-04-30 23:09:08 -0700128
129#include "ceres/schur_eliminator_impl.h"
130#include "ceres/internal/eigen.h"
131
132namespace ceres {
133namespace internal {
134
135template class SchurEliminator<%s, %s, %s>;
136
137} // namespace internal
138} // namespace ceres
139
Sameer Agarwalc59c1e42013-03-11 17:28:38 -0700140#endif // CERES_RESTRICT_SCHUR_SPECIALIZATION
Keir Mierle8ebb0732012-04-30 23:09:08 -0700141"""
142
Sameer Agarwalc59c1e42013-03-11 17:28:38 -0700143FACTORY_FILE_HEADER = """
Keir Mierle8ebb0732012-04-30 23:09:08 -0700144#include "ceres/linear_solver.h"
145#include "ceres/schur_eliminator.h"
146#include "ceres/internal/eigen.h"
147
148namespace ceres {
149namespace internal {
150
151SchurEliminatorBase*
152SchurEliminatorBase::Create(const LinearSolver::Options& options) {
153#ifndef CERES_RESTRICT_SCHUR_SPECIALIZATION
154"""
155
156FACTORY_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
163FACTORY_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 Agarwal31730ef2013-02-28 11:20:28 -0800169 return new SchurEliminator<Eigen::Dynamic, Eigen::Dynamic, Eigen::Dynamic>(options);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700170}
171
172} // namespace internal
173} // namespace ceres
174"""
175
176
177def SuffixForSize(size):
Sameer Agarwal31730ef2013-02-28 11:20:28 -0800178 if size == "Eigen::Dynamic":
Keir Mierle8ebb0732012-04-30 23:09:08 -0700179 return "d"
180 return str(size)
181
182
183def 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
189def Specialize():
190 """
191 Generate specialization code and the conditionals to instantiate it.
192 """
193 f = open("schur_eliminator.cc", "w")
Sameer Agarwalc59c1e42013-03-11 17:28:38 -0700194 f.write(HEADER)
Keir Mierle8ebb0732012-04-30 23:09:08 -0700195 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 Agarwalc59c1e42013-03-11 17:28:38 -0700203 fptr.write(HEADER)
204
205 template = SPECIALIZATION_FILE
206 if (row_block_size == "Eigen::Dynamic" and
207 e_block_size == "Eigen::Dynamic" and
Sameer Agarwal8140f0f2013-03-12 09:45:08 -0700208 f_block_size == "Eigen::Dynamic"):
Sameer Agarwalc59c1e42013-03-11 17:28:38 -0700209 template = DYNAMIC_FILE
210
Sameer Agarwal8140f0f2013-03-12 09:45:08 -0700211 fptr.write(template % (row_block_size, e_block_size, f_block_size))
Keir Mierle8ebb0732012-04-30 23:09:08 -0700212 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
224if __name__ == "__main__":
225 Specialize()