blob: caeca69fb80b17d47f96ed7442025389898c8f9b [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"),
Keir Mierle8ebb0732012-04-30 23:09:08 -070063 (4, 4, 2),
64 (4, 4, 3),
65 (4, 4, 4),
Sameer Agarwal31730ef2013-02-28 11:20:28 -080066 (4, 4, "Eigen::Dynamic"),
67 ("Eigen::Dynamic", "Eigen::Dynamic", "Eigen::Dynamic")]
Sameer Agarwalc59c1e42013-03-11 17:28:38 -070068HEADER = """// Ceres Solver - A fast non-linear least squares minimizer
Sameer Agarwal31730ef2013-02-28 11:20:28 -080069// 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//
Keir Mierle8ebb0732012-04-30 23:09:08 -070096// Author: sameeragarwal@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//
Sameer Agarwal8140f0f2013-03-12 09:45:08 -0700107// This file is generated using generate_eliminator_specialization.py.
Keir Mierle8ebb0732012-04-30 23:09:08 -0700108// Editing it manually is not recommended.
Sameer Agarwalc59c1e42013-03-11 17:28:38 -0700109"""
110
111DYNAMIC_FILE = """
112
113#include "ceres/schur_eliminator_impl.h"
114#include "ceres/internal/eigen.h"
115
116namespace ceres {
117namespace internal {
118
119template class SchurEliminator<%s, %s, %s>;
120
121} // namespace internal
122} // namespace ceres
123"""
124
125SPECIALIZATION_FILE = """
126#ifndef CERES_RESTRICT_SCHUR_SPECIALIZATION
Keir Mierle8ebb0732012-04-30 23:09:08 -0700127
128#include "ceres/schur_eliminator_impl.h"
129#include "ceres/internal/eigen.h"
130
131namespace ceres {
132namespace internal {
133
134template class SchurEliminator<%s, %s, %s>;
135
136} // namespace internal
137} // namespace ceres
138
Sameer Agarwalc59c1e42013-03-11 17:28:38 -0700139#endif // CERES_RESTRICT_SCHUR_SPECIALIZATION
Keir Mierle8ebb0732012-04-30 23:09:08 -0700140"""
141
Sameer Agarwalc59c1e42013-03-11 17:28:38 -0700142FACTORY_FILE_HEADER = """
Keir Mierle8ebb0732012-04-30 23:09:08 -0700143#include "ceres/linear_solver.h"
144#include "ceres/schur_eliminator.h"
145#include "ceres/internal/eigen.h"
146
147namespace ceres {
148namespace internal {
149
150SchurEliminatorBase*
151SchurEliminatorBase::Create(const LinearSolver::Options& options) {
152#ifndef CERES_RESTRICT_SCHUR_SPECIALIZATION
153"""
154
155FACTORY_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
162FACTORY_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 << ">";
Sameer Agarwal31730ef2013-02-28 11:20:28 -0800168 return new SchurEliminator<Eigen::Dynamic, Eigen::Dynamic, Eigen::Dynamic>(options);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700169}
170
171} // namespace internal
172} // namespace ceres
173"""
174
175
176def SuffixForSize(size):
Sameer Agarwal31730ef2013-02-28 11:20:28 -0800177 if size == "Eigen::Dynamic":
Keir Mierle8ebb0732012-04-30 23:09:08 -0700178 return "d"
179 return str(size)
180
181
182def 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
188def Specialize():
189 """
190 Generate specialization code and the conditionals to instantiate it.
191 """
192 f = open("schur_eliminator.cc", "w")
Sameer Agarwalc59c1e42013-03-11 17:28:38 -0700193 f.write(HEADER)
Keir Mierle8ebb0732012-04-30 23:09:08 -0700194 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")
Sameer Agarwalc59c1e42013-03-11 17:28:38 -0700202 fptr.write(HEADER)
203
204 template = SPECIALIZATION_FILE
205 if (row_block_size == "Eigen::Dynamic" and
206 e_block_size == "Eigen::Dynamic" and
Sameer Agarwal8140f0f2013-03-12 09:45:08 -0700207 f_block_size == "Eigen::Dynamic"):
Sameer Agarwalc59c1e42013-03-11 17:28:38 -0700208 template = DYNAMIC_FILE
209
Sameer Agarwal8140f0f2013-03-12 09:45:08 -0700210 fptr.write(template % (row_block_size, e_block_size, f_block_size))
Keir Mierle8ebb0732012-04-30 23:09:08 -0700211 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
223if __name__ == "__main__":
224 Specialize()