| 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 | // Authors: [email protected] (Dmitriy Korchemkin) |
| 30 | |
| 31 | #include "ceres/cuda_kernels_bsm_to_crs.h" |
| 32 | |
| 33 | #include <cuda_runtime.h> |
| 34 | #include <thrust/execution_policy.h> |
| 35 | #include <thrust/scan.h> |
| 36 | |
| 37 | #include "ceres/block_structure.h" |
| 38 | #include "ceres/cuda_kernels_utils.h" |
| 39 | |
| 40 | namespace ceres { |
| 41 | namespace internal { |
| 42 | |
| 43 | namespace { |
| 44 | inline auto ThrustCudaStreamExecutionPolicy(cudaStream_t stream) { |
| 45 | // par_nosync execution policy was added in Thrust 1.16 |
| 46 | // https://github.com/NVIDIA/thrust/blob/main/CHANGELOG.md#thrust-1160 |
| 47 | #if THRUST_VERSION < 101700 |
| 48 | return thrust::cuda::par.on(stream); |
| 49 | #else |
| 50 | return thrust::cuda::par_nosync.on(stream); |
| 51 | #endif |
| 52 | } |
| 53 | |
| 54 | void* CudaMalloc(size_t size, |
| 55 | cudaStream_t stream, |
| 56 | bool memory_pools_supported) { |
| 57 | void* data = nullptr; |
| 58 | // Stream-ordered alloaction API is available since CUDA 11.2, but might be |
| 59 | // not implemented by particular device |
| 60 | #if CUDART_VERSION < 11020 |
| 61 | #warning \ |
| 62 | "Stream-ordered allocations are unavailable, consider updating CUDA toolkit to version 11.2+" |
| 63 | cudaMalloc(&data, size); |
| 64 | #else |
| 65 | if (memory_pools_supported) { |
| 66 | cudaMallocAsync(&data, size, stream); |
| 67 | } else { |
| 68 | cudaMalloc(&data, size); |
| 69 | } |
| 70 | #endif |
| 71 | return data; |
| 72 | } |
| 73 | |
| 74 | void CudaFree(void* data, cudaStream_t stream, bool memory_pools_supported) { |
| 75 | // Stream-ordered alloaction API is available since CUDA 11.2, but might be |
| 76 | // not implemented by particular device |
| 77 | #if CUDART_VERSION < 11020 |
| 78 | #warning \ |
| 79 | "Stream-ordered allocations are unavailable, consider updating CUDA toolkit to version 11.2+" |
| 80 | cudaSuccess, cudaFree(data); |
| 81 | #else |
| 82 | if (memory_pools_supported) { |
| 83 | cudaFreeAsync(data, stream); |
| 84 | } else { |
| 85 | cudaFree(data); |
| 86 | } |
| 87 | #endif |
| 88 | } |
| 89 | template <typename T> |
| 90 | T* CudaAllocate(size_t num_elements, |
| 91 | cudaStream_t stream, |
| 92 | bool memory_pools_supported) { |
| 93 | T* data = static_cast<T*>( |
| 94 | CudaMalloc(num_elements * sizeof(T), stream, memory_pools_supported)); |
| 95 | return data; |
| 96 | } |
| 97 | } // namespace |
| 98 | |
| 99 | // Fill row block id and nnz for each row using block-sparse structure |
| 100 | // represented by a set of flat arrays. |
| 101 | // Inputs: |
| 102 | // - num_row_blocks: number of row-blocks in block-sparse structure |
| 103 | // - first_cell_in_row_block: index of the first cell of the row-block; size: |
| 104 | // num_row_blocks + 1 |
| 105 | // - cells: cells of block-sparse structure as a continuous array |
| 106 | // - row_blocks: row blocks of block-sparse structure stored sequentially |
| 107 | // - col_blocks: column blocks of block-sparse structure stored sequentially |
| 108 | // Outputs: |
| 109 | // - rows: rows[i + 1] will contain number of non-zeros in i-th row, rows[0] |
| 110 | // will be set to 0; rows are filled with a shift by one element in order |
| 111 | // to obtain row-index array of CRS matrix with a inclusive scan afterwards |
| 112 | // - row_block_ids: row_block_ids[i] will be set to index of row-block that |
| 113 | // contains i-th row. |
| 114 | // Computation is perform row-block-wise |
| 115 | template <bool partitioned = false> |
| 116 | __global__ void RowBlockIdAndNNZ( |
| 117 | const int num_row_blocks, |
| 118 | const int num_col_blocks_e, |
| 119 | const int num_row_blocks_e, |
| 120 | const int* __restrict__ first_cell_in_row_block, |
| 121 | const Cell* __restrict__ cells, |
| 122 | const Block* __restrict__ row_blocks, |
| 123 | const Block* __restrict__ col_blocks, |
| 124 | int* __restrict__ rows_e, |
| 125 | int* __restrict__ rows_f, |
| 126 | int* __restrict__ row_block_ids) { |
| 127 | const int row_block_id = blockIdx.x * blockDim.x + threadIdx.x; |
| 128 | if (row_block_id > num_row_blocks) { |
| 129 | // No synchronization is performed in this kernel, thus it is safe to return |
| 130 | return; |
| 131 | } |
| 132 | if (row_block_id == num_row_blocks) { |
| 133 | // one extra thread sets the first element |
| 134 | rows_f[0] = 0; |
| 135 | if constexpr (partitioned) { |
| 136 | rows_e[0] = 0; |
| 137 | } |
| 138 | return; |
| 139 | } |
| 140 | const auto& row_block = row_blocks[row_block_id]; |
| 141 | auto first_cell = cells + first_cell_in_row_block[row_block_id]; |
| 142 | const auto last_cell = cells + first_cell_in_row_block[row_block_id + 1]; |
| 143 | int row_nnz_e = 0; |
| 144 | if (partitioned && row_block_id < num_row_blocks_e) { |
| 145 | // First cell is a cell from E |
| 146 | row_nnz_e = col_blocks[first_cell->block_id].size; |
| 147 | ++first_cell; |
| 148 | } |
| 149 | int row_nnz_f = 0; |
| 150 | for (auto cell = first_cell; cell < last_cell; ++cell) { |
| 151 | row_nnz_f += col_blocks[cell->block_id].size; |
| 152 | } |
| 153 | const int first_row = row_block.position; |
| 154 | const int last_row = first_row + row_block.size; |
| 155 | for (int i = first_row; i < last_row; ++i) { |
| 156 | if constexpr (partitioned) { |
| 157 | rows_e[i + 1] = row_nnz_e; |
| 158 | } |
| 159 | rows_f[i + 1] = row_nnz_f; |
| 160 | row_block_ids[i] = row_block_id; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | // Row-wise creation of CRS structure |
| 165 | // Inputs: |
| 166 | // - num_rows: number of rows in matrix |
| 167 | // - first_cell_in_row_block: index of the first cell of the row-block; size: |
| 168 | // num_row_blocks + 1 |
| 169 | // - cells: cells of block-sparse structure as a continuous array |
| 170 | // - row_blocks: row blocks of block-sparse structure stored sequentially |
| 171 | // - col_blocks: column blocks of block-sparse structure stored sequentially |
| 172 | // - row_block_ids: index of row-block that corresponds to row |
| 173 | // - rows: row-index array of CRS structure |
| 174 | // Outputs: |
| 175 | // - cols: column-index array of CRS structure |
| 176 | // Computaion is perform row-wise |
| 177 | template <bool partitioned> |
| 178 | __global__ void ComputeColumns(const int num_rows, |
| 179 | const int num_row_blocks_e, |
| 180 | const int num_col_blocks_e, |
| 181 | const int* __restrict__ first_cell_in_row_block, |
| 182 | const Cell* __restrict__ cells, |
| 183 | const Block* __restrict__ row_blocks, |
| 184 | const Block* __restrict__ col_blocks, |
| 185 | const int* __restrict__ row_block_ids, |
| 186 | const int* __restrict__ rows_e, |
| 187 | int* __restrict__ cols_e, |
| 188 | const int* __restrict__ rows_f, |
| 189 | int* __restrict__ cols_f) { |
| 190 | const int row = blockIdx.x * blockDim.x + threadIdx.x; |
| 191 | if (row >= num_rows) { |
| 192 | // No synchronization is performed in this kernel, thus it is safe to return |
| 193 | return; |
| 194 | } |
| 195 | const int row_block_id = row_block_ids[row]; |
| 196 | // position in crs matrix |
| 197 | auto first_cell = cells + first_cell_in_row_block[row_block_id]; |
| 198 | const auto last_cell = cells + first_cell_in_row_block[row_block_id + 1]; |
| 199 | const int num_cols_e = col_blocks[num_col_blocks_e].position; |
| 200 | // For reach cell of row-block only current row is being filled |
| 201 | if (partitioned && row_block_id < num_row_blocks_e) { |
| 202 | // The first cell is cell from E |
| 203 | const auto& col_block = col_blocks[first_cell->block_id]; |
| 204 | const int col_block_size = col_block.size; |
| 205 | int column_idx = col_block.position; |
| 206 | int crs_position_e = rows_e[row]; |
| 207 | // Column indices for each element of row_in_block row of current cell |
| 208 | for (int i = 0; i < col_block_size; ++i, ++crs_position_e) { |
| 209 | cols_e[crs_position_e] = column_idx++; |
| 210 | } |
| 211 | ++first_cell; |
| 212 | } |
| 213 | int crs_position_f = rows_f[row]; |
| 214 | for (auto cell = first_cell; cell < last_cell; ++cell) { |
| 215 | const auto& col_block = col_blocks[cell->block_id]; |
| 216 | const int col_block_size = col_block.size; |
| 217 | int column_idx = col_block.position - num_cols_e; |
| 218 | // Column indices for each element of row_in_block row of current cell |
| 219 | for (int i = 0; i < col_block_size; ++i, ++crs_position_f) { |
| 220 | cols_f[crs_position_f] = column_idx++; |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | void FillCRSStructure(const int num_row_blocks, |
| 226 | const int num_rows, |
| 227 | const int* first_cell_in_row_block, |
| 228 | const Cell* cells, |
| 229 | const Block* row_blocks, |
| 230 | const Block* col_blocks, |
| 231 | int* rows, |
| 232 | int* cols, |
| 233 | cudaStream_t stream, |
| 234 | bool memory_pools_supported) { |
| 235 | // Set number of non-zeros per row in rows array and row to row-block map in |
| 236 | // row_block_ids array |
| 237 | int* row_block_ids = |
| 238 | CudaAllocate<int>(num_rows, stream, memory_pools_supported); |
| 239 | const int num_blocks_blockwise = NumBlocksInGrid(num_row_blocks + 1); |
| 240 | RowBlockIdAndNNZ<false><<<num_blocks_blockwise, kCudaBlockSize, 0, stream>>>( |
| 241 | num_row_blocks, |
| 242 | 0, |
| 243 | 0, |
| 244 | first_cell_in_row_block, |
| 245 | cells, |
| 246 | row_blocks, |
| 247 | col_blocks, |
| 248 | nullptr, |
| 249 | rows, |
| 250 | row_block_ids); |
| 251 | // Finalize row-index array of CRS strucure by computing prefix sum |
| 252 | thrust::inclusive_scan( |
| 253 | ThrustCudaStreamExecutionPolicy(stream), rows, rows + num_rows + 1, rows); |
| 254 | |
| 255 | // Fill cols array of CRS structure |
| 256 | const int num_blocks_rowwise = NumBlocksInGrid(num_rows); |
| 257 | ComputeColumns<false><<<num_blocks_rowwise, kCudaBlockSize, 0, stream>>>( |
| 258 | num_rows, |
| 259 | 0, |
| 260 | 0, |
| 261 | first_cell_in_row_block, |
| 262 | cells, |
| 263 | row_blocks, |
| 264 | col_blocks, |
| 265 | row_block_ids, |
| 266 | nullptr, |
| 267 | nullptr, |
| 268 | rows, |
| 269 | cols); |
| 270 | CudaFree(row_block_ids, stream, memory_pools_supported); |
| 271 | } |
| 272 | |
| 273 | void FillCRSStructurePartitioned(const int num_row_blocks, |
| 274 | const int num_rows, |
| 275 | const int num_row_blocks_e, |
| 276 | const int num_col_blocks_e, |
| 277 | const int num_nonzeros_e, |
| 278 | const int* first_cell_in_row_block, |
| 279 | const Cell* cells, |
| 280 | const Block* row_blocks, |
| 281 | const Block* col_blocks, |
| 282 | int* rows_e, |
| 283 | int* cols_e, |
| 284 | int* rows_f, |
| 285 | int* cols_f, |
| 286 | cudaStream_t stream, |
| 287 | bool memory_pools_supported) { |
| 288 | // Set number of non-zeros per row in rows array and row to row-block map in |
| 289 | // row_block_ids array |
| 290 | int* row_block_ids = |
| 291 | CudaAllocate<int>(num_rows, stream, memory_pools_supported); |
| 292 | const int num_blocks_blockwise = NumBlocksInGrid(num_row_blocks + 1); |
| 293 | RowBlockIdAndNNZ<true><<<num_blocks_blockwise, kCudaBlockSize, 0, stream>>>( |
| 294 | num_row_blocks, |
| 295 | num_col_blocks_e, |
| 296 | num_row_blocks_e, |
| 297 | first_cell_in_row_block, |
| 298 | cells, |
| 299 | row_blocks, |
| 300 | col_blocks, |
| 301 | rows_e, |
| 302 | rows_f, |
| 303 | row_block_ids); |
| 304 | // Finalize row-index array of CRS strucure by computing prefix sum |
| 305 | thrust::inclusive_scan(ThrustCudaStreamExecutionPolicy(stream), |
| 306 | rows_e, |
| 307 | rows_e + num_rows + 1, |
| 308 | rows_e); |
| 309 | thrust::inclusive_scan(ThrustCudaStreamExecutionPolicy(stream), |
| 310 | rows_f, |
| 311 | rows_f + num_rows + 1, |
| 312 | rows_f); |
| 313 | |
| 314 | // Fill cols array of CRS structure |
| 315 | const int num_blocks_rowwise = NumBlocksInGrid(num_rows); |
| 316 | ComputeColumns<true><<<num_blocks_rowwise, kCudaBlockSize, 0, stream>>>( |
| 317 | num_rows, |
| 318 | num_row_blocks_e, |
| 319 | num_col_blocks_e, |
| 320 | first_cell_in_row_block, |
| 321 | cells, |
| 322 | row_blocks, |
| 323 | col_blocks, |
| 324 | row_block_ids, |
| 325 | rows_e, |
| 326 | cols_e, |
| 327 | rows_f, |
| 328 | cols_f); |
| 329 | CudaFree(row_block_ids, stream, memory_pools_supported); |
| 330 | } |
| 331 | |
| 332 | template <typename T, typename Predicate> |
| 333 | __device__ int PartitionPoint(const T* data, |
| 334 | int first, |
| 335 | int last, |
| 336 | Predicate&& predicate) { |
| 337 | if (!predicate(data[first])) { |
| 338 | return first; |
| 339 | } |
| 340 | while (last - first > 1) { |
| 341 | const auto midpoint = first + (last - first) / 2; |
| 342 | if (predicate(data[midpoint])) { |
| 343 | first = midpoint; |
| 344 | } else { |
| 345 | last = midpoint; |
| 346 | } |
| 347 | } |
| 348 | return last; |
| 349 | } |
| 350 | |
| 351 | // Element-wise reordering of block-sparse values |
| 352 | // - first_cell_in_row_block - position of the first cell of row-block |
| 353 | // - block_sparse_values - segment of block-sparse values starting from |
| 354 | // block_sparse_offset, containing num_values |
| 355 | template <bool partitioned> |
| 356 | __global__ void PermuteToCrsKernel( |
| 357 | const int block_sparse_offset, |
| 358 | const int num_values, |
| 359 | const int num_row_blocks, |
| 360 | const int num_row_blocks_e, |
| 361 | const int* __restrict__ first_cell_in_row_block, |
| 362 | const int* __restrict__ value_offset_row_block_f, |
| 363 | const Cell* __restrict__ cells, |
| 364 | const Block* __restrict__ row_blocks, |
| 365 | const Block* __restrict__ col_blocks, |
| 366 | const int* __restrict__ crs_rows, |
| 367 | const double* __restrict__ block_sparse_values, |
| 368 | double* __restrict__ crs_values) { |
| 369 | const int value_id = blockIdx.x * blockDim.x + threadIdx.x; |
| 370 | if (value_id >= num_values) { |
| 371 | return; |
| 372 | } |
| 373 | const int block_sparse_value_id = value_id + block_sparse_offset; |
| 374 | // Find the corresponding row-block with a binary search |
| 375 | const int row_block_id = |
| 376 | (partitioned |
| 377 | ? PartitionPoint(value_offset_row_block_f, |
| 378 | 0, |
| 379 | num_row_blocks, |
| 380 | [block_sparse_value_id] __device__( |
| 381 | const int row_block_offset) { |
| 382 | return row_block_offset <= block_sparse_value_id; |
| 383 | }) |
| 384 | : PartitionPoint(first_cell_in_row_block, |
| 385 | 0, |
| 386 | num_row_blocks, |
| 387 | [cells, block_sparse_value_id] __device__( |
| 388 | const int row_block_offset) { |
| 389 | return cells[row_block_offset].position <= |
| 390 | block_sparse_value_id; |
| 391 | })) - |
| 392 | 1; |
| 393 | // Find cell and calculate offset within the row with a linear scan |
| 394 | const auto& row_block = row_blocks[row_block_id]; |
| 395 | auto first_cell = cells + first_cell_in_row_block[row_block_id]; |
| 396 | const auto last_cell = cells + first_cell_in_row_block[row_block_id + 1]; |
| 397 | const int row_block_size = row_block.size; |
| 398 | int num_cols_before = 0; |
| 399 | if (partitioned && row_block_id < num_row_blocks_e) { |
| 400 | ++first_cell; |
| 401 | } |
| 402 | for (const Cell* cell = first_cell; cell < last_cell; ++cell) { |
| 403 | const auto& col_block = col_blocks[cell->block_id]; |
| 404 | const int col_block_size = col_block.size; |
| 405 | const int cell_size = row_block_size * col_block_size; |
| 406 | if (cell->position + cell_size > block_sparse_value_id) { |
| 407 | const int pos_in_cell = block_sparse_value_id - cell->position; |
| 408 | const int row_in_cell = pos_in_cell / col_block_size; |
| 409 | const int col_in_cell = pos_in_cell % col_block_size; |
| 410 | const int row = row_in_cell + row_block.position; |
| 411 | crs_values[crs_rows[row] + num_cols_before + col_in_cell] = |
| 412 | block_sparse_values[value_id]; |
| 413 | break; |
| 414 | } |
| 415 | num_cols_before += col_block_size; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | void PermuteToCRS(const int block_sparse_offset, |
| 420 | const int num_values, |
| 421 | const int num_row_blocks, |
| 422 | const int* first_cell_in_row_block, |
| 423 | const Cell* cells, |
| 424 | const Block* row_blocks, |
| 425 | const Block* col_blocks, |
| 426 | const int* crs_rows, |
| 427 | const double* block_sparse_values, |
| 428 | double* crs_values, |
| 429 | cudaStream_t stream) { |
| 430 | const int num_blocks_valuewise = NumBlocksInGrid(num_values); |
| 431 | PermuteToCrsKernel<false> |
| 432 | <<<num_blocks_valuewise, kCudaBlockSize, 0, stream>>>( |
| 433 | block_sparse_offset, |
| 434 | num_values, |
| 435 | num_row_blocks, |
| 436 | 0, |
| 437 | first_cell_in_row_block, |
| 438 | nullptr, |
| 439 | cells, |
| 440 | row_blocks, |
| 441 | col_blocks, |
| 442 | crs_rows, |
| 443 | block_sparse_values, |
| 444 | crs_values); |
| 445 | } |
| 446 | |
| 447 | void PermuteToCRSPartitionedF(const int block_sparse_offset, |
| 448 | const int num_values, |
| 449 | const int num_row_blocks, |
| 450 | const int num_row_blocks_e, |
| 451 | const int* first_cell_in_row_block, |
| 452 | const int* value_offset_row_block_f, |
| 453 | const Cell* cells, |
| 454 | const Block* row_blocks, |
| 455 | const Block* col_blocks, |
| 456 | const int* crs_rows, |
| 457 | const double* block_sparse_values, |
| 458 | double* crs_values, |
| 459 | cudaStream_t stream) { |
| 460 | const int num_blocks_valuewise = NumBlocksInGrid(num_values); |
| 461 | PermuteToCrsKernel<true><<<num_blocks_valuewise, kCudaBlockSize, 0, stream>>>( |
| 462 | block_sparse_offset, |
| 463 | num_values, |
| 464 | num_row_blocks, |
| 465 | num_row_blocks_e, |
| 466 | first_cell_in_row_block, |
| 467 | value_offset_row_block_f, |
| 468 | cells, |
| 469 | row_blocks, |
| 470 | col_blocks, |
| 471 | crs_rows, |
| 472 | block_sparse_values, |
| 473 | crs_values); |
| 474 | } |
| 475 | |
| 476 | } // namespace internal |
| 477 | } // namespace ceres |