generate_bundle_adjustment_tests.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. # Ceres Solver - A fast non-linear least squares minimizer
  2. # Copyright 2023 Google Inc. All rights reserved.
  3. # http://ceres-solver.org/
  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: keir@google.com (Keir Mierle)
  30. #
  31. # Generate bundle adjustment tests as separate binaries. Since the bundle
  32. # adjustment tests are fairly processing intensive, serializing them makes the
  33. # tests take forever to run. Splitting them into separate binaries makes it
  34. # easier to parallelize in continuous integration systems, and makes local
  35. # processing on multi-core workstations much faster.
  36. # Product of ORDERINGS, THREAD_CONFIGS, and SOLVER_CONFIGS is the full set of
  37. # tests to generate.
  38. ORDERINGS = ["kAutomaticOrdering", "kUserOrdering"]
  39. SINGLE_THREADED = "1"
  40. MULTI_THREADED = "4"
  41. THREAD_CONFIGS = [SINGLE_THREADED, MULTI_THREADED]
  42. DENSE_SOLVER_CONFIGS = [
  43. # Linear solver Dense backend
  44. ('DENSE_SCHUR', 'EIGEN'),
  45. ('DENSE_SCHUR', 'LAPACK'),
  46. ('DENSE_SCHUR', 'CUDA'),
  47. ]
  48. SPARSE_SOLVER_CONFIGS = [
  49. # Linear solver Sparse backend
  50. ('SPARSE_NORMAL_CHOLESKY', 'SUITE_SPARSE'),
  51. ('SPARSE_NORMAL_CHOLESKY', 'EIGEN_SPARSE'),
  52. ('SPARSE_NORMAL_CHOLESKY', 'ACCELERATE_SPARSE'),
  53. ('SPARSE_SCHUR', 'SUITE_SPARSE'),
  54. ('SPARSE_SCHUR', 'EIGEN_SPARSE'),
  55. ('SPARSE_SCHUR', 'ACCELERATE_SPARSE'),
  56. ]
  57. ITERATIVE_SOLVER_CONFIGS = [
  58. # Linear solver Sparse backend Preconditioner
  59. ('ITERATIVE_SCHUR', 'NO_SPARSE', 'JACOBI'),
  60. ('ITERATIVE_SCHUR', 'NO_SPARSE', 'SCHUR_JACOBI'),
  61. ('ITERATIVE_SCHUR', 'NO_SPARSE', 'SCHUR_POWER_SERIES_EXPANSION'),
  62. ('ITERATIVE_SCHUR', 'SUITE_SPARSE', 'CLUSTER_JACOBI'),
  63. ('ITERATIVE_SCHUR', 'EIGEN_SPARSE', 'CLUSTER_JACOBI'),
  64. ('ITERATIVE_SCHUR', 'ACCELERATE_SPARSE','CLUSTER_JACOBI'),
  65. ('ITERATIVE_SCHUR', 'SUITE_SPARSE', 'CLUSTER_TRIDIAGONAL'),
  66. ('ITERATIVE_SCHUR', 'EIGEN_SPARSE', 'CLUSTER_TRIDIAGONAL'),
  67. ('ITERATIVE_SCHUR', 'ACCELERATE_SPARSE','CLUSTER_TRIDIAGONAL'),
  68. ]
  69. FILENAME_SHORTENING_MAP = dict(
  70. DENSE_SCHUR='denseschur',
  71. ITERATIVE_SCHUR='iterschur',
  72. SPARSE_NORMAL_CHOLESKY='sparsecholesky',
  73. SPARSE_SCHUR='sparseschur',
  74. EIGEN='eigen',
  75. LAPACK='lapack',
  76. CUDA='cuda',
  77. NO_SPARSE='', # Omit sparse reference entirely for dense tests.
  78. SUITE_SPARSE='suitesparse',
  79. EIGEN_SPARSE='eigensparse',
  80. ACCELERATE_SPARSE='acceleratesparse',
  81. IDENTITY='identity',
  82. JACOBI='jacobi',
  83. SCHUR_JACOBI='schurjacobi',
  84. CLUSTER_JACOBI='clustjacobi',
  85. CLUSTER_TRIDIAGONAL='clusttri',
  86. SCHUR_POWER_SERIES_EXPANSION='spse',
  87. kAutomaticOrdering='auto',
  88. kUserOrdering='user',
  89. )
  90. COPYRIGHT_HEADER = (
  91. """// Ceres Solver - A fast non-linear least squares minimizer
  92. // Copyright 2023 Google Inc. All rights reserved.
  93. // http://ceres-solver.org/
  94. //
  95. // Redistribution and use in source and binary forms, with or without
  96. // modification, are permitted provided that the following conditions are met:
  97. //
  98. // * Redistributions of source code must retain the above copyright notice,
  99. // this list of conditions and the following disclaimer.
  100. // * Redistributions in binary form must reproduce the above copyright notice,
  101. // this list of conditions and the following disclaimer in the documentation
  102. // and/or other materials provided with the distribution.
  103. // * Neither the name of Google Inc. nor the names of its contributors may be
  104. // used to endorse or promote products derived from this software without
  105. // specific prior written permission.
  106. //
  107. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  108. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  109. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  110. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  111. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  112. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  113. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  114. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  115. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  116. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  117. // POSSIBILITY OF SUCH DAMAGE.
  118. //
  119. // ========================================
  120. // THIS FILE IS AUTOGENERATED. DO NOT EDIT.
  121. // THIS FILE IS AUTOGENERATED. DO NOT EDIT.
  122. // THIS FILE IS AUTOGENERATED. DO NOT EDIT.
  123. // THIS FILE IS AUTOGENERATED. DO NOT EDIT.
  124. // ========================================
  125. //
  126. // This file is generated using generate_bundle_adjustment_tests.py.""")
  127. BUNDLE_ADJUSTMENT_TEST_TEMPLATE = (COPYRIGHT_HEADER + """
  128. #include "ceres/bundle_adjustment_test_util.h"
  129. #include "ceres/internal/config.h"
  130. #include "gtest/gtest.h"
  131. %(preprocessor_conditions_begin)s
  132. namespace ceres::internal {
  133. TEST_F(BundleAdjustmentTest,
  134. %(test_class_name)s) { // NOLINT
  135. BundleAdjustmentProblem bundle_adjustment_problem;
  136. Solver::Options* options = bundle_adjustment_problem.mutable_solver_options();
  137. options->eta = 0.01;
  138. options->num_threads = %(num_threads)s;
  139. options->linear_solver_type = %(linear_solver)s;
  140. options->dense_linear_algebra_library_type = %(dense_backend)s;
  141. options->sparse_linear_algebra_library_type = %(sparse_backend)s;
  142. options->preconditioner_type = %(preconditioner)s;
  143. if (%(ordering)s) {
  144. options->linear_solver_ordering = nullptr;
  145. }
  146. Problem* problem = bundle_adjustment_problem.mutable_problem();
  147. RunSolverForConfigAndExpectResidualsMatch(*options, problem);
  148. }
  149. } // namespace ceres::internal
  150. %(preprocessor_conditions_end)s""")
  151. def camelcasify(token):
  152. """Convert capitalized underscore tokens to camel case"""
  153. return ''.join([x.lower().capitalize() for x in token.split('_')])
  154. def generate_bundle_test(linear_solver,
  155. dense_backend,
  156. sparse_backend,
  157. preconditioner,
  158. ordering,
  159. thread_config):
  160. """Generate a bundle adjustment test executable configured appropriately"""
  161. # Preconditioner only makes sense for iterative schur; drop it otherwise.
  162. preconditioner_tag = preconditioner
  163. if linear_solver != 'ITERATIVE_SCHUR':
  164. preconditioner_tag = ''
  165. dense_backend_tag = dense_backend
  166. if linear_solver != 'DENSE_SCHUR':
  167. dense_backend_tag=''
  168. # Omit references to the sparse backend when one is not in use.
  169. sparse_backend_tag = sparse_backend
  170. if sparse_backend == 'NO_SPARSE':
  171. sparse_backend_tag = ''
  172. # Use a double underscore; otherwise the names are harder to understand.
  173. test_class_name = '_'.join(filter(lambda x: x, [
  174. camelcasify(linear_solver),
  175. camelcasify(dense_backend_tag),
  176. camelcasify(sparse_backend_tag),
  177. camelcasify(preconditioner_tag),
  178. ordering[1:], # Strip 'k'
  179. 'Threads' if thread_config == MULTI_THREADED else '']))
  180. # Initial template parameters (augmented more below).
  181. template_parameters = dict(
  182. linear_solver=linear_solver,
  183. dense_backend=dense_backend,
  184. sparse_backend=sparse_backend,
  185. preconditioner=preconditioner,
  186. ordering=ordering,
  187. num_threads=thread_config,
  188. test_class_name=test_class_name)
  189. # Accumulate appropriate #ifdef/#ifndefs for the solver's sparse backend.
  190. preprocessor_conditions_begin = []
  191. preprocessor_conditions_end = []
  192. if sparse_backend == 'SUITE_SPARSE':
  193. preprocessor_conditions_begin.append('#ifndef CERES_NO_SUITESPARSE')
  194. preprocessor_conditions_end.insert(0, '#endif // CERES_NO_SUITESPARSE')
  195. elif sparse_backend == 'ACCELERATE_SPARSE':
  196. preprocessor_conditions_begin.append('#ifndef CERES_NO_ACCELERATE_SPARSE')
  197. preprocessor_conditions_end.insert(0, '#endif // CERES_NO_ACCELERATE_SPARSE')
  198. elif sparse_backend == 'EIGEN_SPARSE':
  199. preprocessor_conditions_begin.append('#ifdef CERES_USE_EIGEN_SPARSE')
  200. preprocessor_conditions_end.insert(0, '#endif // CERES_USE_EIGEN_SPARSE')
  201. if dense_backend == "LAPACK":
  202. preprocessor_conditions_begin.append('#ifndef CERES_NO_LAPACK')
  203. preprocessor_conditions_end.insert(0, '#endif // CERES_NO_LAPACK')
  204. elif dense_backend == "CUDA":
  205. preprocessor_conditions_begin.append('#ifndef CERES_NO_CUDA')
  206. preprocessor_conditions_end.insert(0, '#endif // CERES_NO_CUDA')
  207. # If there are #ifdefs, put newlines around them.
  208. if preprocessor_conditions_begin:
  209. preprocessor_conditions_begin.insert(0, '')
  210. preprocessor_conditions_begin.append('')
  211. preprocessor_conditions_end.insert(0, '')
  212. preprocessor_conditions_end.append('')
  213. # Put #ifdef/#ifndef stacks into the template parameters.
  214. template_parameters['preprocessor_conditions_begin'] = '\n'.join(
  215. preprocessor_conditions_begin)
  216. template_parameters['preprocessor_conditions_end'] = '\n'.join(
  217. preprocessor_conditions_end)
  218. # Substitute variables into the test template, and write the result to a file.
  219. filename_tag = '_'.join(FILENAME_SHORTENING_MAP.get(x) for x in [
  220. linear_solver,
  221. dense_backend_tag,
  222. sparse_backend_tag,
  223. preconditioner_tag,
  224. ordering]
  225. if FILENAME_SHORTENING_MAP.get(x))
  226. if (thread_config == MULTI_THREADED):
  227. filename_tag += '_threads'
  228. filename = ('generated_bundle_adjustment_tests/ba_%s_test.cc' %
  229. filename_tag.lower())
  230. with open(filename, 'w') as fd:
  231. fd.write(BUNDLE_ADJUSTMENT_TEST_TEMPLATE % template_parameters)
  232. # All done.
  233. print('Generated', filename)
  234. return filename
  235. if __name__ == '__main__':
  236. # Iterate over all the possible configurations and generate the tests.
  237. generated_files = []
  238. for ordering in ORDERINGS:
  239. for thread_config in THREAD_CONFIGS:
  240. for linear_solver, dense_backend in DENSE_SOLVER_CONFIGS:
  241. generated_files.append(
  242. generate_bundle_test(linear_solver,
  243. dense_backend,
  244. 'NO_SPARSE',
  245. 'IDENTITY',
  246. ordering,
  247. thread_config))
  248. for linear_solver, sparse_backend, in SPARSE_SOLVER_CONFIGS:
  249. generated_files.append(
  250. generate_bundle_test(linear_solver,
  251. 'EIGEN',
  252. sparse_backend,
  253. 'IDENTITY',
  254. ordering,
  255. thread_config))
  256. for linear_solver, sparse_backend, preconditioner, in ITERATIVE_SOLVER_CONFIGS:
  257. generated_files.append(
  258. generate_bundle_test(linear_solver,
  259. 'EIGEN',
  260. sparse_backend,
  261. preconditioner,
  262. ordering,
  263. thread_config))
  264. # Generate the CMakeLists.txt as well.
  265. with open('generated_bundle_adjustment_tests/CMakeLists.txt', 'w') as fd:
  266. fd.write(COPYRIGHT_HEADER.replace('//', '#').replace('http:#', 'http://'))
  267. fd.write('\n')
  268. fd.write('\n')
  269. for generated_file in generated_files:
  270. fd.write('ceres_test(%s)\n' %
  271. generated_file.split('/')[1].replace('_test.cc', ''))