blob: c6ab573fa4b74bbc0aa63db1f533dd8e4a1ec5b9 [file] [log] [blame]
Sameer Agarwal5a161a22013-10-29 22:08:15 -07001# Ceres Solver - A fast non-linear least squares minimizer
2# Copyright 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@google.com (Sameer Agarwal)
30#
31# Script for explicitly generating template specialization of the
32# PartitionedMatrixView class. Explicitly generating these
33# instantiations in separate .cc files breaks the compilation into
34# separate compilation unit rather than one large cc file.
35#
36# This script creates two sets of files.
37#
38# 1. partitioned_matrix_view_x_x_x.cc
Sameer Agarwal9ba0b352013-11-05 13:04:56 -080039# where the x indicates the template parameters and
Sameer Agarwal5a161a22013-10-29 22:08:15 -070040#
41# 2. partitioned_matrix_view.cc
42#
43# that contains a factory function for instantiating these classes
44# based on runtime parameters.
45#
46# The list of tuples, specializations indicates the set of
47# specializations that is generated.
48
49# Set of template specializations to generate
50SPECIALIZATIONS = [(2, 2, 2),
51 (2, 2, 3),
52 (2, 2, 4),
53 (2, 2, "Eigen::Dynamic"),
54 (2, 3, 3),
55 (2, 3, 4),
56 (2, 3, 9),
57 (2, 3, "Eigen::Dynamic"),
58 (2, 4, 3),
59 (2, 4, 4),
60 (2, 4, "Eigen::Dynamic"),
61 (2, "Eigen::Dynamic", "Eigen::Dynamic"),
62 (4, 4, 2),
63 (4, 4, 3),
64 (4, 4, 4),
65 (4, 4, "Eigen::Dynamic"),
66 ("Eigen::Dynamic", "Eigen::Dynamic", "Eigen::Dynamic")]
67HEADER = """// Ceres Solver - A fast non-linear least squares minimizer
68// Copyright 2013 Google Inc. All rights reserved.
69// http://code.google.com/p/ceres-solver/
70//
71// Redistribution and use in source and binary forms, with or without
72// modification, are permitted provided that the following conditions are met:
73//
74// * Redistributions of source code must retain the above copyright notice,
75// this list of conditions and the following disclaimer.
76// * Redistributions in binary form must reproduce the above copyright notice,
77// this list of conditions and the following disclaimer in the documentation
78// and/or other materials provided with the distribution.
79// * Neither the name of Google Inc. nor the names of its contributors may be
80// used to endorse or promote products derived from this software without
81// specific prior written permission.
82//
83// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
84// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
85// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
86// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
87// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
88// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
89// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
90// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
91// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
92// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
93// POSSIBILITY OF SUCH DAMAGE.
94//
95// Author: sameeragarwal@google.com (Sameer Agarwal)
96//
97// Template specialization of PartitionedMatrixView.
98//
99// ========================================
100// THIS FILE IS AUTOGENERATED. DO NOT EDIT.
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//=========================================
105//
106// This file is generated using generate_eliminator_specialization.py.
107// Editing it manually is not recommended.
108"""
109
110DYNAMIC_FILE = """
111
112#include "ceres/partitioned_matrix_view_impl.h"
113#include "ceres/internal/eigen.h"
114
115namespace ceres {
116namespace internal {
117
118template class PartitionedMatrixView<%s, %s, %s>;
119
120} // namespace internal
121} // namespace ceres
122"""
123
124SPECIALIZATION_FILE = """
125#ifndef CERES_RESTRICT_SCHUR_SPECIALIZATION
126
127#include "ceres/partitioned_matrix_view_impl.h"
128#include "ceres/internal/eigen.h"
129
130namespace ceres {
131namespace internal {
132
133template class PartitionedMatrixView<%s, %s, %s>;
134
135} // namespace internal
136} // namespace ceres
137
138#endif // CERES_RESTRICT_SCHUR_SPECIALIZATION
139"""
140
141FACTORY_FILE_HEADER = """
142#include "ceres/linear_solver.h"
143#include "ceres/partitioned_matrix_view.h"
144#include "ceres/internal/eigen.h"
145
146namespace ceres {
147namespace internal {
148
149PartitionedMatrixViewBase*
150PartitionedMatrixViewBase::Create(const LinearSolver::Options& options,
151 const BlockSparseMatrix& matrix) {
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 PartitionedMatrixView<%s, %s, %s>(
159 matrix, options.elimination_groups[0]);
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 << ">";
169 return new PartitionedMatrixView<Eigen::Dynamic, Eigen::Dynamic, Eigen::Dynamic>(
170 matrix, options.elimination_groups[0]);
171};
172
173} // namespace internal
174} // namespace ceres
175"""
176
177
178def SuffixForSize(size):
179 if size == "Eigen::Dynamic":
180 return "d"
181 return str(size)
182
183
184def SpecializationFilename(prefix, row_block_size, e_block_size, f_block_size):
185 return "_".join([prefix] + map(SuffixForSize, (row_block_size,
186 e_block_size,
187 f_block_size)))
188
189
190def Specialize():
191 """
192 Generate specialization code and the conditionals to instantiate it.
193 """
194 f = open("partitioned_matrix_view.cc", "w")
195 f.write(HEADER)
196 f.write(FACTORY_FILE_HEADER)
197
198 for row_block_size, e_block_size, f_block_size in SPECIALIZATIONS:
199 output = SpecializationFilename("generated/partitioned_matrix_view",
200 row_block_size,
201 e_block_size,
202 f_block_size) + ".cc"
203 fptr = open(output, "w")
204 fptr.write(HEADER)
205
206 template = SPECIALIZATION_FILE
207 if (row_block_size == "Eigen::Dynamic" and
208 e_block_size == "Eigen::Dynamic" and
209 f_block_size == "Eigen::Dynamic"):
210 template = DYNAMIC_FILE
211
212 fptr.write(template % (row_block_size, e_block_size, f_block_size))
213 fptr.close()
214
215 f.write(FACTORY_CONDITIONAL % (row_block_size,
216 e_block_size,
217 f_block_size,
218 row_block_size,
219 e_block_size,
220 f_block_size))
221 f.write(FACTORY_FOOTER)
222 f.close()
223
224
225if __name__ == "__main__":
226 Specialize()