context_impl.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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: vitus@google.com (Michael Vitus)
  30. #ifndef CERES_INTERNAL_CONTEXT_IMPL_H_
  31. #define CERES_INTERNAL_CONTEXT_IMPL_H_
  32. // This include must come before any #ifndef check on Ceres compile options.
  33. // clang-format off
  34. #include "ceres/internal/config.h"
  35. // clang-format on
  36. #include <string>
  37. #include "ceres/context.h"
  38. #include "ceres/internal/disable_warnings.h"
  39. #include "ceres/internal/export.h"
  40. #ifndef CERES_NO_CUDA
  41. #include "cublas_v2.h"
  42. #include "cuda_runtime.h"
  43. #include "cusolverDn.h"
  44. #include "cusparse.h"
  45. #endif // CERES_NO_CUDA
  46. #include "ceres/thread_pool.h"
  47. namespace ceres::internal {
  48. class CERES_NO_EXPORT ContextImpl final : public Context {
  49. public:
  50. ContextImpl();
  51. ~ContextImpl() override;
  52. ContextImpl(const ContextImpl&) = delete;
  53. void operator=(const ContextImpl&) = delete;
  54. // When compiled with C++ threading support, resize the thread pool to have
  55. // at min(num_thread, num_hardware_threads) where num_hardware_threads is
  56. // defined by the hardware. Otherwise this call is a no-op.
  57. void EnsureMinimumThreads(int num_threads);
  58. ThreadPool thread_pool;
  59. #ifndef CERES_NO_CUDA
  60. // Note on Ceres' use of CUDA Devices on multi-GPU systems:
  61. // 1. On a multi-GPU system, if nothing special is done, the "default" CUDA
  62. // device will be used, which is device 0.
  63. // 2. If the user masks out GPUs using the CUDA_VISIBLE_DEVICES environment
  64. // variable, Ceres will still use device 0 visible to the program, but
  65. // device 0 will be the first GPU indicated in the environment variable.
  66. // 3. If the user explicitly selects a GPU in the host process before calling
  67. // Ceres, Ceres will use that GPU.
  68. // Note on Ceres' use of CUDA Streams:
  69. // Most of operations on the GPU are performed using a single stream. In
  70. // those cases DefaultStream() should be used. This ensures that operations
  71. // are stream-ordered, and might be concurrent with cpu processing with no
  72. // additional efforts.
  73. //
  74. // a. Single-stream workloads
  75. // - Only use default stream
  76. // - Return control to the callee without synchronization whenever possible
  77. // - Stream synchronization occurs only after GPU to CPU transfers, and is
  78. // handled by CudaBuffer
  79. //
  80. // b. Multi-stream workloads
  81. // Multi-stream workloads are more restricted in order to make it harder to
  82. // get a race-condition.
  83. // - Should always synchronize the default stream on entry
  84. // - Should always synchronize all utilized streams on exit
  85. // - Should not make any assumptions on one of streams_[] being default
  86. //
  87. // With those rules in place
  88. // - All single-stream asynchronous workloads are serialized using default
  89. // stream
  90. // - Multiple-stream workloads always wait single-stream workloads to finish
  91. // and leave no running computations on exit.
  92. // This slightly penalizes multi-stream workloads, but makes it easier to
  93. // avoid race conditions when multiple-stream workload depends on results of
  94. // any preceeding gpu computations.
  95. // Initializes cuBLAS, cuSOLVER, and cuSPARSE contexts, creates an
  96. // asynchronous CUDA stream, and associates the stream with the contexts.
  97. // Returns true iff initialization was successful, else it returns false and a
  98. // human-readable error message is returned.
  99. bool InitCuda(std::string* message);
  100. void TearDown();
  101. inline bool IsCudaInitialized() const { return is_cuda_initialized_; }
  102. // Returns a human-readable string describing the capabilities of the current
  103. // CUDA device. CudaConfigAsString can only be called after InitCuda has been
  104. // called.
  105. std::string CudaConfigAsString() const;
  106. // Returns the number of bytes of available global memory on the current CUDA
  107. // device. If it is called before InitCuda, it returns 0.
  108. size_t GpuMemoryAvailable() const;
  109. cusolverDnHandle_t cusolver_handle_ = nullptr;
  110. cublasHandle_t cublas_handle_ = nullptr;
  111. // Default stream.
  112. // Kernel invocations and memory copies on this stream can be left without
  113. // synchronization.
  114. cudaStream_t DefaultStream() { return streams_[0]; }
  115. static constexpr int kNumCudaStreams = 2;
  116. cudaStream_t streams_[kNumCudaStreams] = {0};
  117. cusparseHandle_t cusparse_handle_ = nullptr;
  118. bool is_cuda_initialized_ = false;
  119. int gpu_device_id_in_use_ = -1;
  120. cudaDeviceProp gpu_device_properties_;
  121. bool is_cuda_memory_pools_supported_ = false;
  122. int cuda_version_major_ = 0;
  123. int cuda_version_minor_ = 0;
  124. #endif // CERES_NO_CUDA
  125. };
  126. } // namespace ceres::internal
  127. #include "ceres/internal/reenable_warnings.h"
  128. #endif // CERES_INTERNAL_CONTEXT_IMPL_H_