From 69b2432259e7b9f9e3d8d55dfb0819b36cb254be Mon Sep 17 00:00:00 2001 From: Paul-Edouard Sarlin Date: Fri, 4 Dec 2026 11:23:38 +0000 Subject: [PATCH] [Caspar] Check CUDA errors in the generated solver and runtime The generated GraphSolver and the Caspar runtime ignored the results of CUDA runtime calls, CUB calls, runtime kernel launches, and the StackedToCaspar/CasparToStacked mappings. A failed cudaMalloc, for example, left the solver with invalid device pointers: the following kernels then failed with a sticky cudaErrorIllegalAddress that broke every later solve in the process, and host read-backs silently returned zeros. Add CASPAR_CUDA_CHECK in solver_tools.h, which throws a std::runtime_error naming the failed expression, its location, and the CUDA error, and use it for all such calls in the solver, the runtime, and the Python mapping bindings. finish_indices() and solve() also synchronize and check for errors before returning. Return early on empty inputs from the runtime functions and mappings that launch kernels, which would otherwise launch zero blocks and fail the new checks. Signed-off-by: Paul-Edouard Sarlin --- .../caspar/source/runtime/shared_indices.cu | 5 + .../caspar/source/runtime/solver_tools.cu | 19 +- symforce/caspar/source/runtime/solver_tools.h | 18 ++ .../caspar/source/runtime/sort_indices.cu | 29 ++- .../source/templates/caspar_mappings.cu.jinja | 6 + .../caspar_mappings_pybinding.h.jinja | 13 +- .../caspar/source/templates/solver.cc.jinja | 97 +++---- test/caspar_solver_cuda_test.cc | 41 +++ .../caspar_mappings.cu | 6 + .../symforce_caspar_solver_test/solver.cc | 60 +++-- .../caspar_mappings.cu | 42 +++ .../solver.cc | 242 ++++++++++-------- 12 files changed, 380 insertions(+), 198 deletions(-) diff --git a/symforce/caspar/source/runtime/shared_indices.cu b/symforce/caspar/source/runtime/shared_indices.cu index f3d20c61..29070de0 100644 --- a/symforce/caspar/source/runtime/shared_indices.cu +++ b/symforce/caspar/source/runtime/shared_indices.cu @@ -8,6 +8,7 @@ #include #include "shared_indices.h" +#include "solver_tools.h" namespace cg = cooperative_groups; @@ -148,8 +149,12 @@ __global__ void SharedIndicesKernel(const uint* const __restrict__ indices, void SharedIndices(const uint* const indices, SharedIndex* const shared_indices_out, const uint problem_size) { + if (problem_size == 0) { + return; + } const int n_blocks = (problem_size + 1024 - 1) / 1024; SharedIndicesKernel<<>>(indices, shared_indices_out, problem_size); + CASPAR_CUDA_CHECK(cudaGetLastError()); } } // namespace caspar diff --git a/symforce/caspar/source/runtime/solver_tools.cu b/symforce/caspar/source/runtime/solver_tools.cu index 5954fb04..8c7ad4cf 100644 --- a/symforce/caspar/source/runtime/solver_tools.cu +++ b/symforce/caspar/source/runtime/solver_tools.cu @@ -13,25 +13,26 @@ namespace caspar { template void Zero(T* start, T* end) { const size_t num_bytes = (end - start) * sizeof(T); - cudaMemset(start, 0, num_bytes); + CASPAR_CUDA_CHECK(cudaMemset(start, 0, num_bytes)); } template void Copy(const T* start, const T* end, T* target) { const size_t num_bytes = (end - start) * sizeof(T); - cudaMemcpy(target, start, num_bytes, cudaMemcpyDeviceToDevice); + CASPAR_CUDA_CHECK(cudaMemcpy(target, start, num_bytes, cudaMemcpyDeviceToDevice)); } template T Sum(const T* start, const T* end, T* target_ptr, T* scratch_ptr, const bool copy_to_host) { const size_t num_el = (end - start); - size_t tmp_storage_bytes; - cudaMemset(target_ptr, 0, sizeof(T)); - cub::DeviceReduce::Sum(nullptr, tmp_storage_bytes, start, target_ptr, num_el); - cub::DeviceReduce::Sum(scratch_ptr, tmp_storage_bytes, start, target_ptr, num_el); + size_t tmp_storage_bytes = 0; + CASPAR_CUDA_CHECK(cudaMemset(target_ptr, 0, sizeof(T))); + CASPAR_CUDA_CHECK(cub::DeviceReduce::Sum(nullptr, tmp_storage_bytes, start, target_ptr, num_el)); + CASPAR_CUDA_CHECK( + cub::DeviceReduce::Sum(scratch_ptr, tmp_storage_bytes, start, target_ptr, num_el)); T result = T{0}; if (copy_to_host) { - cudaMemcpy(&result, target_ptr, sizeof(T), cudaMemcpyDeviceToHost); + CASPAR_CUDA_CHECK(cudaMemcpy(&result, target_ptr, sizeof(T), cudaMemcpyDeviceToHost)); } return result; } @@ -39,7 +40,7 @@ T Sum(const T* start, const T* end, T* target_ptr, T* scratch_ptr, const bool co template T ReadCuMem(const T* const data) { T result; - cudaMemcpy(&result, data, sizeof(T), cudaMemcpyDeviceToHost); + CASPAR_CUDA_CHECK(cudaMemcpy(&result, data, sizeof(T), cudaMemcpyDeviceToHost)); return result; } @@ -54,6 +55,7 @@ template void AlphaFromNumDenom(const T* alpha_numerator, const T* alpha_denominator, T* alpha, T* neg_alpha) { AlphaFromNumDenomKernel<<<1, 1>>>(alpha_numerator, alpha_denominator, alpha, neg_alpha); + CASPAR_CUDA_CHECK(cudaGetLastError()); } template @@ -64,6 +66,7 @@ __global__ void BetaFromNumDenomKernel(const T* beta_num, const T* beta_denum, T template void BetaFromNumDenom(const T* beta_num, const T* beta_denum, T* beta) { BetaFromNumDenomKernel<<<1, 1>>>(beta_num, beta_denum, beta); + CASPAR_CUDA_CHECK(cudaGetLastError()); } // Explicit instantiations diff --git a/symforce/caspar/source/runtime/solver_tools.h b/symforce/caspar/source/runtime/solver_tools.h index 7a7a649e..f263672f 100644 --- a/symforce/caspar/source/runtime/solver_tools.h +++ b/symforce/caspar/source/runtime/solver_tools.h @@ -5,8 +5,26 @@ #pragma once +#include +#include + +#include + namespace caspar { +inline void CheckCudaError(const cudaError_t err, const char* expr, const char* file, + const int line) { + if (err != cudaSuccess) { + // Clear the thread-local error state so recoverable errors (e.g. OOM) don't poison later calls. + (void)cudaGetLastError(); + throw std::runtime_error(std::string("Caspar CUDA error in ") + expr + " at " + file + ":" + + std::to_string(line) + ": " + cudaGetErrorName(err) + " (" + + cudaGetErrorString(err) + ")"); + } +} + +#define CASPAR_CUDA_CHECK(expr) ::caspar::CheckCudaError((expr), #expr, __FILE__, __LINE__) + template void Zero(T* start, T* end); diff --git a/symforce/caspar/source/runtime/sort_indices.cu b/symforce/caspar/source/runtime/sort_indices.cu index 0e1356ec..a6fd1b22 100644 --- a/symforce/caspar/source/runtime/sort_indices.cu +++ b/symforce/caspar/source/runtime/sort_indices.cu @@ -5,6 +5,7 @@ #include +#include "solver_tools.h" #include "sort_indices.h" namespace { @@ -31,32 +32,38 @@ namespace caspar { size_t SortIndicesGetTmpNbytes(size_t problem_size) { size_t tmp_storage_bytes = 0; - auto err = - cub::DeviceRadixSort::SortPairs(nullptr, tmp_storage_bytes, (uint*)nullptr, (uint*)nullptr, - (uint*)nullptr, (uint*)nullptr, (uint)problem_size); + CASPAR_CUDA_CHECK(cub::DeviceRadixSort::SortPairs(nullptr, tmp_storage_bytes, (uint*)nullptr, + (uint*)nullptr, (uint*)nullptr, (uint*)nullptr, + (uint)problem_size)); return tmp_storage_bytes; } void SortIndices(void* tmp_storage, size_t tmp_storage_bytes, const uint* keys_in, uint* sorted_out, uint* target_out, uint* argsort_out, size_t problem_size) { + if (problem_size == 0) { + return; + } const size_t grid_size = (problem_size + 1023) / 1024; Arange<<>>(target_out, problem_size); - cub::DeviceRadixSort::SortPairs(tmp_storage, tmp_storage_bytes, keys_in, sorted_out, target_out, - argsort_out, (uint)problem_size); + CASPAR_CUDA_CHECK(cudaGetLastError()); + CASPAR_CUDA_CHECK(cub::DeviceRadixSort::SortPairs(tmp_storage, tmp_storage_bytes, keys_in, + sorted_out, target_out, argsort_out, + (uint)problem_size)); TargetFromArgsort<<>>(argsort_out, target_out, problem_size); + CASPAR_CUDA_CHECK(cudaGetLastError()); } size_t SortKeysGetTmpNbytes(size_t problem_size) { size_t tmp_storage_bytes = 0; - auto err = cub::DeviceRadixSort::SortKeys(nullptr, tmp_storage_bytes, (uint*)nullptr, - (uint*)nullptr, (uint)problem_size); + CASPAR_CUDA_CHECK(cub::DeviceRadixSort::SortKeys(nullptr, tmp_storage_bytes, (uint*)nullptr, + (uint*)nullptr, (uint)problem_size)); return tmp_storage_bytes; } void SortKeys(void* tmp_storage, size_t tmp_storage_bytes, const uint* keys_in, uint* sorted_out, size_t problem_size) { - cub::DeviceRadixSort::SortKeys(tmp_storage, tmp_storage_bytes, keys_in, sorted_out, - (uint)problem_size); + CASPAR_CUDA_CHECK(cub::DeviceRadixSort::SortKeys(tmp_storage, tmp_storage_bytes, keys_in, + sorted_out, (uint)problem_size)); } __global__ void SelectIndexKernel(const uint* const input, const uint* const selections, @@ -70,8 +77,12 @@ __global__ void SelectIndexKernel(const uint* const input, const uint* const sel void SelectIndex(const uint* const input, const uint* const selections, uint* const output, size_t problem_size) { + if (problem_size == 0) { + return; + } const size_t grid_size = (problem_size + 1023) / 1024; SelectIndexKernel<<>>(input, selections, output, problem_size); + CASPAR_CUDA_CHECK(cudaGetLastError()); } } // namespace caspar diff --git a/symforce/caspar/source/templates/caspar_mappings.cu.jinja b/symforce/caspar/source/templates/caspar_mappings.cu.jinja index eb398676..8a6e7df9 100644 --- a/symforce/caspar/source/templates/caspar_mappings.cu.jinja +++ b/symforce/caspar/source/templates/caspar_mappings.cu.jinja @@ -116,6 +116,9 @@ cudaError_t {{nodetype.__name__}}StackedToCaspar(const {{caslib.storage_t}}* sta const unsigned int cas_offset, const unsigned int num_objects ) { + if (num_objects == 0) { + return cudaSuccess; + } const int num_blocks = (num_objects + block_size - 1) / block_size; {{nodetype.__name__}}StackedToCaspar_kernel<<>>(stacked_data, cas_data, cas_stride, cas_offset, num_objects); @@ -128,6 +131,9 @@ cudaError_t {{nodetype.__name__}}CasparToStacked(const {{caslib.storage_t}}* cas const unsigned int cas_stride, const unsigned int cas_offset, const unsigned int num_objects) { + if (num_objects == 0) { + return cudaSuccess; + } const int num_blocks = (num_objects + block_size - 1) / block_size; {{nodetype.__name__}}CasparToStacked_kernel<<>>(cas_data, stacked_data, cas_stride, cas_offset, num_objects); diff --git a/symforce/caspar/source/templates/caspar_mappings_pybinding.h.jinja b/symforce/caspar/source/templates/caspar_mappings_pybinding.h.jinja index 05f250c9..14dadf07 100644 --- a/symforce/caspar/source/templates/caspar_mappings_pybinding.h.jinja +++ b/symforce/caspar/source/templates/caspar_mappings_pybinding.h.jinja @@ -6,6 +6,7 @@ #include "caspar_mappings.h" #include "pybind_array_tools.h" +#include "solver_tools.h" namespace caspar { @@ -27,9 +28,9 @@ void add_casmappings_pybindings(pybind11::module_ module) { throw std::runtime_error( "The caspar data must have at least as many columns as stacked_data has rows."); } - cudaSetDevice(GetDeviceId(stacked_data)); - {{nodetype.__name__}}StackedToCaspar( - As{{caslib.storage_t.capitalize()}}Ptr(stacked_data), As{{caslib.storage_t.capitalize()}}Ptr(cas_data), cas_stride, 0, num_objects); + CASPAR_CUDA_CHECK(cudaSetDevice(GetDeviceId(stacked_data))); + CASPAR_CUDA_CHECK({{nodetype.__name__}}StackedToCaspar( + As{{caslib.storage_t.capitalize()}}Ptr(stacked_data), As{{caslib.storage_t.capitalize()}}Ptr(cas_data), cas_stride, 0, num_objects)); }); module.def("{{camel_to_snake(nodetype.__name__)}}_caspar_to_stacked", [](pybind11::object cas_data, pybind11::object stacked_data) { @@ -46,9 +47,9 @@ void add_casmappings_pybindings(pybind11::module_ module) { throw std::runtime_error( "The caspar data must have at least as many columns as stacked_data has rows."); } - cudaSetDevice(GetDeviceId(cas_data)); - {{nodetype.__name__}}CasparToStacked( - As{{caslib.storage_t.capitalize()}}Ptr(cas_data), As{{caslib.storage_t.capitalize()}}Ptr(stacked_data), cas_stride, 0, num_objects); + CASPAR_CUDA_CHECK(cudaSetDevice(GetDeviceId(cas_data))); + CASPAR_CUDA_CHECK({{nodetype.__name__}}CasparToStacked( + As{{caslib.storage_t.capitalize()}}Ptr(cas_data), As{{caslib.storage_t.capitalize()}}Ptr(stacked_data), cas_stride, 0, num_objects)); }); {% endfor %} } diff --git a/symforce/caspar/source/templates/solver.cc.jinja b/symforce/caspar/source/templates/solver.cc.jinja index 87c21613..ba1745dc 100644 --- a/symforce/caspar/source/templates/solver.cc.jinja +++ b/symforce/caspar/source/templates/solver.cc.jinja @@ -97,16 +97,16 @@ namespace caspar { } if (device_id_ != 0) { int deviceCount; - cudaGetDeviceCount(&deviceCount); + CASPAR_CUDA_CHECK(cudaGetDeviceCount(&deviceCount)); if (deviceCount <= device_id_) { throw std::runtime_error("CUDA detected " + std::to_string(deviceCount) + " devices, but device " + std::to_string(device_id_) + " was requested (0-indexed)"); } } - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); uint8_t* origin_ptr = nullptr; - cudaMalloc(&origin_ptr, allocation_size_); + CASPAR_CUDA_CHECK(cudaMalloc(&origin_ptr, allocation_size_)); origin_ptr_.reset(origin_ptr); size_t offset = 0; @@ -128,13 +128,13 @@ size_t {{ solver.struct_name }}::get_allocation_size(){ SolveResult {{ solver.struct_name }}::solve(bool print_progress, bool verbose_logging) { - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); SolveResult result; result.exit_reason = ExitReason::MAX_ITERATIONS; {{solver.linear_t}} score_best; {{solver.linear_t}} score_best_pcg; {{solver.linear_t}} diag = params_.diag_init; - cudaMemcpy({{solver_key("current_diag")}}, &diag, sizeof({{solver.linear_t}}), cudaMemcpyHostToDevice); + CASPAR_CUDA_CHECK(cudaMemcpy({{solver_key("current_diag")}}, &diag, sizeof({{solver.linear_t}}), cudaMemcpyHostToDevice)); {{solver.linear_t}} up_scale = params_.diag_scaling_up; {{solver.linear_t}} quality; @@ -211,7 +211,7 @@ SolveResult {{ solver.struct_name }}::solve(bool print_progress, bool verbose_lo {{solver.linear_t}} scale = std::max(params_.diag_scaling_down, 1.0f - quality_tmp * quality_tmp * quality_tmp); diag = std::max(params_.diag_min, diag * scale); - cudaMemcpy({{solver_key("current_diag")}}, &diag, sizeof({{solver.linear_t}}), cudaMemcpyHostToDevice); + CASPAR_CUDA_CHECK(cudaMemcpy({{solver_key("current_diag")}}, &diag, sizeof({{solver.linear_t}}), cudaMemcpyHostToDevice)); up_scale = params_.diag_scaling_up; score_best = score_best_pcg; step_accepted = true; @@ -227,7 +227,7 @@ SolveResult {{ solver.struct_name }}::solve(bool print_progress, bool verbose_lo result.exit_reason = ExitReason::CONVERGED_DIAG_EXIT; break; } - cudaMemcpy({{solver_key("current_diag")}}, &diag, sizeof({{solver.linear_t}}), cudaMemcpyHostToDevice); + CASPAR_CUDA_CHECK(cudaMemcpy({{solver_key("current_diag")}}, &diag, sizeof({{solver.linear_t}}), cudaMemcpyHostToDevice)); up_scale *= 2; } const auto t_now = std::chrono::steady_clock::now(); @@ -266,6 +266,8 @@ SolveResult {{ solver.struct_name }}::solve(bool print_progress, bool verbose_lo } } + CASPAR_CUDA_CHECK(cudaGetLastError()); + CASPAR_CUDA_CHECK(cudaStreamSynchronize(nullptr)); const auto t_final = std::chrono::steady_clock::now(); result.final_score = score_best; result.iteration_count = solver_iter_; @@ -664,12 +666,15 @@ void {{ solver.struct_name }}::finish_indices() { {% endfor %} {% endif %} {% endfor %} #} + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); + CASPAR_CUDA_CHECK(cudaGetLastError()); + CASPAR_CUDA_CHECK(cudaStreamSynchronize(nullptr)); indices_valid_ = true; } {% for nodetype in solver.node_types %} void {{ solver.struct_name }}::Set{{nodetype.__name__}}Num(const size_t num) { - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (num > {{num_max_key(nodetype)}}) { throw std::runtime_error(std::to_string(num) + " > {{num_max_key(nodetype)}}"); } @@ -678,48 +683,48 @@ void {{ solver.struct_name }}::Set{{nodetype.__name__}}Num(const size_t num) { void {{ solver.struct_name }}::Set{{nodetype.__name__}}NodesFromStackedHost( const {{solver.storage_t}}* const data, const size_t offset, const size_t num) { - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (offset + num > {{num_key(nodetype)}}){ throw std::runtime_error(std::to_string(offset + num) + " > {{num_key(nodetype)}}"); } - cudaMemcpy(marker__scratch_inout_, data, {{Ops.storage_dim(nodetype)}} * num * sizeof({{solver.storage_t}}), - cudaMemcpyHostToDevice); - {{nodetype.__name__}}StackedToCaspar(marker__scratch_inout_, + CASPAR_CUDA_CHECK(cudaMemcpy(marker__scratch_inout_, data, {{Ops.storage_dim(nodetype)}} * num * sizeof({{solver.storage_t}}), + cudaMemcpyHostToDevice)); + CASPAR_CUDA_CHECK({{nodetype.__name__}}StackedToCaspar(marker__scratch_inout_, {{node_key(nodetype, "storage_current")}}, - {{num_max_key(nodetype)}}, offset, num); + {{num_max_key(nodetype)}}, offset, num)); } void {{ solver.struct_name }}::Set{{nodetype.__name__}}NodesFromStackedDevice( const {{solver.storage_t}}* const data, const size_t offset, const size_t num) { - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (offset + num > {{num_key(nodetype)}}){ throw std::runtime_error(std::to_string(offset + num) + " > {{num_key(nodetype)}}"); } - {{nodetype.__name__}}StackedToCaspar( - data, {{node_key(nodetype, "storage_current")}}, {{num_max_key(nodetype)}}, offset, num); + CASPAR_CUDA_CHECK({{nodetype.__name__}}StackedToCaspar( + data, {{node_key(nodetype, "storage_current")}}, {{num_max_key(nodetype)}}, offset, num)); } void {{solver.struct_name}}::Get{{nodetype.__name__}}NodesToStackedHost( {{solver.storage_t}}* const data, const size_t offset, const size_t num) { - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (offset + num > {{num_key(nodetype)}}){ throw std::runtime_error(std::to_string(offset + num) + " > {{num_key(nodetype)}}"); } - {{nodetype.__name__}}CasparToStacked({{node_key(nodetype, "storage_current")}}, + CASPAR_CUDA_CHECK({{nodetype.__name__}}CasparToStacked({{node_key(nodetype, "storage_current")}}, marker__scratch_inout_, {{num_max_key(nodetype)}}, - offset, num); - cudaMemcpy(data, marker__scratch_inout_, {{Ops.storage_dim(nodetype)}} * num * sizeof({{solver.storage_t}}), - cudaMemcpyDeviceToHost); + offset, num)); + CASPAR_CUDA_CHECK(cudaMemcpy(data, marker__scratch_inout_, {{Ops.storage_dim(nodetype)}} * num * sizeof({{solver.storage_t}}), + cudaMemcpyDeviceToHost)); } void {{solver.struct_name}}::Get{{nodetype.__name__}}NodesToStackedDevice( {{solver.storage_t}}* const data, const size_t offset, const size_t num) { - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (offset + num > {{num_key(nodetype)}}){ throw std::runtime_error(std::to_string(offset + num) + " > {{num_key(nodetype)}}"); } - {{nodetype.__name__}}CasparToStacked({{node_key(nodetype, "storage_current")}}, data, - {{num_max_key(nodetype)}}, offset, num); + CASPAR_CUDA_CHECK({{nodetype.__name__}}CasparToStacked({{node_key(nodetype, "storage_current")}}, data, + {{num_max_key(nodetype)}}, offset, num)); } {% endfor%} @@ -735,21 +740,21 @@ void {{solver.struct_name}}::Get{{nodetype.__name__}}NodesToStackedDevice( {% if fac.isnodeshared[arg] %} void {{ solver.struct_name }}::Set{{parts_to_pascal(fac.name)}}{{parts_to_pascal(arg)}}IndicesFromHost( const unsigned int* const indices, size_t num) { - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (num != {{num_key(fac)}}){ throw std::runtime_error( std::to_string(num) + " != {{num_key(fac)}}. Use Set{{fac.name }}Num before setting indices."); } - cudaMemcpy((unsigned int*)marker__scratch_inout_, indices, num * sizeof(unsigned int), - cudaMemcpyHostToDevice); + CASPAR_CUDA_CHECK(cudaMemcpy((unsigned int*)marker__scratch_inout_, indices, num * sizeof(unsigned int), + cudaMemcpyHostToDevice)); Set{{parts_to_pascal(fac.name)}}{{parts_to_pascal(arg)}}IndicesFromDevice((unsigned int*)marker__scratch_inout_, num); } void {{ solver.struct_name }}::Set{{parts_to_pascal(fac.name)}}{{parts_to_pascal(arg)}}IndicesFromDevice( const unsigned int* const indices, size_t num) { indices_valid_ = false; - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (num != {{num_key(fac)}}){ throw std::runtime_error( @@ -779,7 +784,7 @@ void {{solver.struct_name}}::Get{{nodetype.__name__}}NodesToStackedDevice( const {{solver.storage_t}}* const data, size_t offset, size_t num {% endif %} ) { - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); {% if fac.isconstuniq[arg] %} const size_t offset = 0; const size_t num = 1; @@ -787,9 +792,9 @@ void {{solver.struct_name}}::Get{{nodetype.__name__}}NodesToStackedDevice( if (offset + num > {{num_max_key(fac)}}) { throw std::runtime_error(std::to_string(offset + num) + " > {{num_max_key(fac)}}"); } - cudaMemcpy(marker__scratch_inout_, data, {{Ops.storage_dim(argtype)}} * num * sizeof({{solver.storage_t}}), - cudaMemcpyHostToDevice); - {{argtype.__name__}}StackedToCaspar(marker__scratch_inout_, {{arg_key(fac, arg, "data")}}, + CASPAR_CUDA_CHECK(cudaMemcpy(marker__scratch_inout_, data, {{Ops.storage_dim(argtype)}} * num * sizeof({{solver.storage_t}}), + cudaMemcpyHostToDevice)); + CASPAR_CUDA_CHECK({{argtype.__name__}}StackedToCaspar(marker__scratch_inout_, {{arg_key(fac, arg, "data")}}, {% if fac.isconstseq[arg] %} {{num_max_key(fac)}}, {% elif fac.isconstuniq[arg] %} @@ -799,7 +804,7 @@ void {{solver.struct_name}}::Get{{nodetype.__name__}}NodesToStackedDevice( {% elif fac.isconstindexed[arg] %} {{num_max_key(fac)}}, {% endif %} - offset, num); + offset, num)); } void {{ solver.struct_name }}::Set{{parts_to_pascal(fac.name)}}{{parts_to_pascal(arg)}}DataFromStackedDevice( @@ -812,7 +817,7 @@ void {{solver.struct_name}}::Get{{nodetype.__name__}}NodesToStackedDevice( {% elif fac.isconstindexed[arg] %} const {{solver.storage_t}}* const data, size_t offset, size_t num {% endif %} ) { - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); {% if fac.isconstuniq[arg] %} const size_t offset = 0; const size_t num = 1; @@ -820,7 +825,7 @@ void {{solver.struct_name}}::Get{{nodetype.__name__}}NodesToStackedDevice( if (offset + num > {{num_max_key(fac)}}){ throw std::runtime_error(std::to_string(offset + num) + " > {{num_max_key(fac)}}"); } - {{argtype.__name__}}StackedToCaspar(data, {{arg_key(fac, arg, "data")}}, + CASPAR_CUDA_CHECK({{argtype.__name__}}StackedToCaspar(data, {{arg_key(fac, arg, "data")}}, {% if fac.isconstseq[arg] %} {{num_max_key(fac)}}, {% elif fac.isconstuniq[arg] %} @@ -830,26 +835,26 @@ void {{solver.struct_name}}::Get{{nodetype.__name__}}NodesToStackedDevice( {% elif fac.isconstindexed[arg] %} {{num_max_key(fac)}}, {% endif %} - offset, num); + offset, num)); } {% if fac.isconstshared[arg] %} void {{ solver.struct_name }}::Set{{parts_to_pascal(fac.name)}}{{parts_to_pascal(arg)}}IndicesFromHost( const unsigned int* const indices, size_t num) { - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (num != {{num_key(fac)}}){ throw std::runtime_error( std::to_string(num) + " != {{num_key(fac)}}. Use Set{{fac.name }}Num before setting indices."); } - cudaMemcpy((unsigned int*)marker__scratch_inout_, indices, num * sizeof(unsigned int), - cudaMemcpyHostToDevice); + CASPAR_CUDA_CHECK(cudaMemcpy((unsigned int*)marker__scratch_inout_, indices, num * sizeof(unsigned int), + cudaMemcpyHostToDevice)); Set{{parts_to_pascal(fac.name)}}{{parts_to_pascal(arg)}}IndicesFromDevice((unsigned int*)marker__scratch_inout_, num); } void {{ solver.struct_name }}::Set{{parts_to_pascal(fac.name)}}{{parts_to_pascal(arg)}}IndicesFromDevice( const unsigned int* const indices, size_t num) { indices_valid_ = false; - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (num != {{num_key(fac)}}){ throw std::runtime_error( std::to_string(num) @@ -869,27 +874,27 @@ void {{solver.struct_name}}::Get{{nodetype.__name__}}NodesToStackedDevice( void {{ solver.struct_name }}::Set{{parts_to_pascal(fac.name)}}{{parts_to_pascal(arg)}}IndicesFromHost( const unsigned int* const indices, size_t num) { indices_valid_ = false; - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (num != {{num_key(fac)}}){ throw std::runtime_error( std::to_string(num) + " != {{num_key(fac)}}. Use Set{{fac.name }}Num before setting indices."); } - cudaMemcpy({{arg_key(fac, arg, "idx")}}, indices, num * sizeof(unsigned int), - cudaMemcpyHostToDevice); + CASPAR_CUDA_CHECK(cudaMemcpy({{arg_key(fac, arg, "idx")}}, indices, num * sizeof(unsigned int), + cudaMemcpyHostToDevice)); } void {{ solver.struct_name }}::Set{{parts_to_pascal(fac.name)}}{{parts_to_pascal(arg)}}IndicesFromDevice( const unsigned int* const indices, size_t num) { indices_valid_ = false; - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (num != {{num_key(fac)}}){ throw std::runtime_error( std::to_string(num) + " != {{num_key(fac)}}. Use Set{{fac.name }}Num before setting indices."); } - cudaMemcpy({{arg_key(fac, arg, "idx")}}, indices, num * sizeof(unsigned int), - cudaMemcpyDeviceToDevice); + CASPAR_CUDA_CHECK(cudaMemcpy({{arg_key(fac, arg, "idx")}}, indices, num * sizeof(unsigned int), + cudaMemcpyDeviceToDevice)); } {% endif%} diff --git a/test/caspar_solver_cuda_test.cc b/test/caspar_solver_cuda_test.cc index e1f34914..8fa4cb19 100644 --- a/test/caspar_solver_cuda_test.cc +++ b/test/caspar_solver_cuda_test.cc @@ -12,6 +12,7 @@ #include #include +#include #include #include "solver.h" @@ -225,3 +226,43 @@ TEST_CASE("GraphSolver can be move-constructed and move-assigned without double- check_matches_baseline(Run(params)); CHECK(cudaGetLastError() == cudaSuccess); } + +TEST_CASE("Failed device allocation throws and leaves CUDA usable", "[caspar_solver_cuda_test]") { + REQUIRE(HaveDevice()); + + const caspar::SolverParams params = ParamsWithIterations(10); + // Hundreds of TB: more than any GPU's memory, yet far below size_t overflow in get_nbytes(). + constexpr size_t kExcessiveNodes = size_t{1} << 40; + REQUIRE_THROWS_WITH(caspar::GraphSolver(params, kExcessiveNodes, kExcessiveNodes), + Catch::Matchers::ContainsSubstring("cudaErrorMemoryAllocation")); + CHECK(cudaGetLastError() == cudaSuccess); + + const Solve solve = Run(params); + CHECK(cudaGetLastError() == cudaSuccess); + CHECK(std::isfinite(solve.result.final_score)); + CHECK(solve.result.final_score < solve.result.initial_score); + CHECK(ScoreOf(solve.x) == Catch::Approx(solve.result.final_score).epsilon(1e-9)); +} + +TEST_CASE("Empty problem does not throw or trigger CUDA launch errors", + "[caspar_solver_cuda_test]") { + REQUIRE(HaveDevice()); + REQUIRE(cudaGetLastError() == cudaSuccess); + + const caspar::SolverParams params = ParamsWithIterations(5); + caspar::GraphSolver solver(params, /* Matrix21_num_max */ 0, /* rosenbrock_num_max */ 0); + + REQUIRE_NOTHROW(solver.SetRosenbrockXIndicesFromHost(nullptr, 0)); + CHECK(cudaGetLastError() == cudaSuccess); + REQUIRE_NOTHROW(solver.SetMatrix21NodesFromStackedHost(nullptr, 0, 0)); + REQUIRE_NOTHROW(solver.finish_indices()); + + caspar::SolveResult result; + REQUIRE_NOTHROW(result = solver.solve(/* print_progress */ false, /* verbose_logging */ true)); + REQUIRE_NOTHROW(solver.GetMatrix21NodesToStackedHost(nullptr, 0, 0)); + + CHECK(cudaGetLastError() == cudaSuccess); + CHECK(result.initial_score == 0.0); + CHECK(result.final_score == 0.0); + CHECK(result.exit_reason == caspar::ExitReason::CONVERGED_SCORE_THRESHOLD); +} diff --git a/test/symforce_function_codegen_test_data/symengine/symforce_caspar_solver_test/caspar_mappings.cu b/test/symforce_function_codegen_test_data/symengine/symforce_caspar_solver_test/caspar_mappings.cu index 7edd0f2b..cb24140e 100644 --- a/test/symforce_function_codegen_test_data/symengine/symforce_caspar_solver_test/caspar_mappings.cu +++ b/test/symforce_function_codegen_test_data/symengine/symforce_caspar_solver_test/caspar_mappings.cu @@ -65,6 +65,9 @@ __global__ __launch_bounds__(block_size, 1) void Matrix21CasparToStacked_kernel( cudaError_t Matrix21StackedToCaspar(const double* stacked_data, double* cas_data, const unsigned int cas_stride, const unsigned int cas_offset, const unsigned int num_objects) { + if (num_objects == 0) { + return cudaSuccess; + } const int num_blocks = (num_objects + block_size - 1) / block_size; Matrix21StackedToCaspar_kernel<<>>(stacked_data, cas_data, cas_stride, @@ -76,6 +79,9 @@ cudaError_t Matrix21StackedToCaspar(const double* stacked_data, double* cas_data cudaError_t Matrix21CasparToStacked(const double* cas_data, double* stacked_data, const unsigned int cas_stride, const unsigned int cas_offset, const unsigned int num_objects) { + if (num_objects == 0) { + return cudaSuccess; + } const int num_blocks = (num_objects + block_size - 1) / block_size; Matrix21CasparToStacked_kernel<<>>(cas_data, stacked_data, cas_stride, diff --git a/test/symforce_function_codegen_test_data/symengine/symforce_caspar_solver_test/solver.cc b/test/symforce_function_codegen_test_data/symengine/symforce_caspar_solver_test/solver.cc index 0ec19c5f..fba486e0 100644 --- a/test/symforce_function_codegen_test_data/symengine/symforce_caspar_solver_test/solver.cc +++ b/test/symforce_function_codegen_test_data/symengine/symforce_caspar_solver_test/solver.cc @@ -77,16 +77,16 @@ GraphSolver::GraphSolver(const SolverParams& params, size_t Matrix21_num } if (device_id_ != 0) { int deviceCount; - cudaGetDeviceCount(&deviceCount); + CASPAR_CUDA_CHECK(cudaGetDeviceCount(&deviceCount)); if (deviceCount <= device_id_) { throw std::runtime_error("CUDA detected " + std::to_string(deviceCount) + " devices, but device " + std::to_string(device_id_) + " was requested (0-indexed)"); } } - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); uint8_t* origin_ptr = nullptr; - cudaMalloc(&origin_ptr, allocation_size_); + CASPAR_CUDA_CHECK(cudaMalloc(&origin_ptr, allocation_size_)); origin_ptr_.reset(origin_ptr); size_t offset = 0; @@ -163,13 +163,14 @@ size_t GraphSolver::get_allocation_size() { } SolveResult GraphSolver::solve(bool print_progress, bool verbose_logging) { - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); SolveResult result; result.exit_reason = ExitReason::MAX_ITERATIONS; double score_best; double score_best_pcg; double diag = params_.diag_init; - cudaMemcpy(solver__current_diag_, &diag, sizeof(double), cudaMemcpyHostToDevice); + CASPAR_CUDA_CHECK( + cudaMemcpy(solver__current_diag_, &diag, sizeof(double), cudaMemcpyHostToDevice)); double up_scale = params_.diag_scaling_up; double quality; @@ -240,7 +241,8 @@ SolveResult GraphSolver::solve(bool print_progress, bool verbose_logging) { double scale = std::max(params_.diag_scaling_down, 1.0f - quality_tmp * quality_tmp * quality_tmp); diag = std::max(params_.diag_min, diag * scale); - cudaMemcpy(solver__current_diag_, &diag, sizeof(double), cudaMemcpyHostToDevice); + CASPAR_CUDA_CHECK( + cudaMemcpy(solver__current_diag_, &diag, sizeof(double), cudaMemcpyHostToDevice)); up_scale = params_.diag_scaling_up; score_best = score_best_pcg; step_accepted = true; @@ -253,7 +255,8 @@ SolveResult GraphSolver::solve(bool print_progress, bool verbose_logging) { result.exit_reason = ExitReason::CONVERGED_DIAG_EXIT; break; } - cudaMemcpy(solver__current_diag_, &diag, sizeof(double), cudaMemcpyHostToDevice); + CASPAR_CUDA_CHECK( + cudaMemcpy(solver__current_diag_, &diag, sizeof(double), cudaMemcpyHostToDevice)); up_scale *= 2; } const auto t_now = std::chrono::steady_clock::now(); @@ -292,6 +295,8 @@ SolveResult GraphSolver::solve(bool print_progress, bool verbose_logging) { } } + CASPAR_CUDA_CHECK(cudaGetLastError()); + CASPAR_CUDA_CHECK(cudaStreamSynchronize(nullptr)); const auto t_final = std::chrono::steady_clock::now(); result.final_score = score_best; result.iteration_count = solver_iter_; @@ -425,11 +430,14 @@ double GraphSolver::GetPredDecrease() { } void GraphSolver::finish_indices() { + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); + CASPAR_CUDA_CHECK(cudaGetLastError()); + CASPAR_CUDA_CHECK(cudaStreamSynchronize(nullptr)); indices_valid_ = true; } void GraphSolver::SetMatrix21Num(const size_t num) { - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (num > Matrix21_num_max_) { throw std::runtime_error(std::to_string(num) + " > Matrix21_num_max_"); } @@ -438,42 +446,46 @@ void GraphSolver::SetMatrix21Num(const size_t num) { void GraphSolver::SetMatrix21NodesFromStackedHost(const double* const data, const size_t offset, const size_t num) { - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (offset + num > Matrix21_num_) { throw std::runtime_error(std::to_string(offset + num) + " > Matrix21_num_"); } - cudaMemcpy(marker__scratch_inout_, data, 2 * num * sizeof(double), cudaMemcpyHostToDevice); - Matrix21StackedToCaspar(marker__scratch_inout_, nodes__Matrix21__storage_current_, - Matrix21_num_max_, offset, num); + CASPAR_CUDA_CHECK( + cudaMemcpy(marker__scratch_inout_, data, 2 * num * sizeof(double), cudaMemcpyHostToDevice)); + CASPAR_CUDA_CHECK(Matrix21StackedToCaspar( + marker__scratch_inout_, nodes__Matrix21__storage_current_, Matrix21_num_max_, offset, num)); } void GraphSolver::SetMatrix21NodesFromStackedDevice(const double* const data, const size_t offset, const size_t num) { - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (offset + num > Matrix21_num_) { throw std::runtime_error(std::to_string(offset + num) + " > Matrix21_num_"); } - Matrix21StackedToCaspar(data, nodes__Matrix21__storage_current_, Matrix21_num_max_, offset, num); + CASPAR_CUDA_CHECK(Matrix21StackedToCaspar(data, nodes__Matrix21__storage_current_, + Matrix21_num_max_, offset, num)); } void GraphSolver::GetMatrix21NodesToStackedHost(double* const data, const size_t offset, const size_t num) { - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (offset + num > Matrix21_num_) { throw std::runtime_error(std::to_string(offset + num) + " > Matrix21_num_"); } - Matrix21CasparToStacked(nodes__Matrix21__storage_current_, marker__scratch_inout_, - Matrix21_num_max_, offset, num); - cudaMemcpy(data, marker__scratch_inout_, 2 * num * sizeof(double), cudaMemcpyDeviceToHost); + CASPAR_CUDA_CHECK(Matrix21CasparToStacked( + nodes__Matrix21__storage_current_, marker__scratch_inout_, Matrix21_num_max_, offset, num)); + CASPAR_CUDA_CHECK( + cudaMemcpy(data, marker__scratch_inout_, 2 * num * sizeof(double), cudaMemcpyDeviceToHost)); } void GraphSolver::GetMatrix21NodesToStackedDevice(double* const data, const size_t offset, const size_t num) { - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (offset + num > Matrix21_num_) { throw std::runtime_error(std::to_string(offset + num) + " > Matrix21_num_"); } - Matrix21CasparToStacked(nodes__Matrix21__storage_current_, data, Matrix21_num_max_, offset, num); + CASPAR_CUDA_CHECK(Matrix21CasparToStacked(nodes__Matrix21__storage_current_, data, + Matrix21_num_max_, offset, num)); } void GraphSolver::SetRosenbrockNum(const size_t num) { @@ -483,19 +495,19 @@ void GraphSolver::SetRosenbrockNum(const size_t num) { rosenbrock_num_ = num; } void GraphSolver::SetRosenbrockXIndicesFromHost(const unsigned int* const indices, size_t num) { - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (num != rosenbrock_num_) { throw std::runtime_error(std::to_string(num) + " != rosenbrock_num_. Use SetrosenbrockNum before setting indices."); } - cudaMemcpy((unsigned int*)marker__scratch_inout_, indices, num * sizeof(unsigned int), - cudaMemcpyHostToDevice); + CASPAR_CUDA_CHECK(cudaMemcpy((unsigned int*)marker__scratch_inout_, indices, + num * sizeof(unsigned int), cudaMemcpyHostToDevice)); SetRosenbrockXIndicesFromDevice((unsigned int*)marker__scratch_inout_, num); } void GraphSolver::SetRosenbrockXIndicesFromDevice(const unsigned int* const indices, size_t num) { indices_valid_ = false; - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (num != rosenbrock_num_) { throw std::runtime_error(std::to_string(num) + diff --git a/test/symforce_function_codegen_test_data/symengine/symforce_caspar_tridiagonal_test/caspar_mappings.cu b/test/symforce_function_codegen_test_data/symengine/symforce_caspar_tridiagonal_test/caspar_mappings.cu index 0b2a5761..56f61303 100644 --- a/test/symforce_function_codegen_test_data/symengine/symforce_caspar_tridiagonal_test/caspar_mappings.cu +++ b/test/symforce_function_codegen_test_data/symengine/symforce_caspar_tridiagonal_test/caspar_mappings.cu @@ -65,6 +65,9 @@ __global__ __launch_bounds__(block_size, 1) void ChainACasparToStacked_kernel( cudaError_t ChainAStackedToCaspar(const double* stacked_data, double* cas_data, const unsigned int cas_stride, const unsigned int cas_offset, const unsigned int num_objects) { + if (num_objects == 0) { + return cudaSuccess; + } const int num_blocks = (num_objects + block_size - 1) / block_size; ChainAStackedToCaspar_kernel<<>>(stacked_data, cas_data, cas_stride, @@ -76,6 +79,9 @@ cudaError_t ChainAStackedToCaspar(const double* stacked_data, double* cas_data, cudaError_t ChainACasparToStacked(const double* cas_data, double* stacked_data, const unsigned int cas_stride, const unsigned int cas_offset, const unsigned int num_objects) { + if (num_objects == 0) { + return cudaSuccess; + } const int num_blocks = (num_objects + block_size - 1) / block_size; ChainACasparToStacked_kernel<<>>(cas_data, stacked_data, cas_stride, @@ -143,6 +149,9 @@ __global__ __launch_bounds__(block_size, 1) void ChainBCasparToStacked_kernel( cudaError_t ChainBStackedToCaspar(const double* stacked_data, double* cas_data, const unsigned int cas_stride, const unsigned int cas_offset, const unsigned int num_objects) { + if (num_objects == 0) { + return cudaSuccess; + } const int num_blocks = (num_objects + block_size - 1) / block_size; ChainBStackedToCaspar_kernel<<>>(stacked_data, cas_data, cas_stride, @@ -154,6 +163,9 @@ cudaError_t ChainBStackedToCaspar(const double* stacked_data, double* cas_data, cudaError_t ChainBCasparToStacked(const double* cas_data, double* stacked_data, const unsigned int cas_stride, const unsigned int cas_offset, const unsigned int num_objects) { + if (num_objects == 0) { + return cudaSuccess; + } const int num_blocks = (num_objects + block_size - 1) / block_size; ChainBCasparToStacked_kernel<<>>(cas_data, stacked_data, cas_stride, @@ -214,6 +226,9 @@ __global__ __launch_bounds__(block_size, 1) void Matrix21CasparToStacked_kernel( cudaError_t Matrix21StackedToCaspar(const double* stacked_data, double* cas_data, const unsigned int cas_stride, const unsigned int cas_offset, const unsigned int num_objects) { + if (num_objects == 0) { + return cudaSuccess; + } const int num_blocks = (num_objects + block_size - 1) / block_size; Matrix21StackedToCaspar_kernel<<>>(stacked_data, cas_data, cas_stride, @@ -225,6 +240,9 @@ cudaError_t Matrix21StackedToCaspar(const double* stacked_data, double* cas_data cudaError_t Matrix21CasparToStacked(const double* cas_data, double* stacked_data, const unsigned int cas_stride, const unsigned int cas_offset, const unsigned int num_objects) { + if (num_objects == 0) { + return cudaSuccess; + } const int num_blocks = (num_objects + block_size - 1) / block_size; Matrix21CasparToStacked_kernel<<>>(cas_data, stacked_data, cas_stride, @@ -294,6 +312,9 @@ __global__ __launch_bounds__(block_size, 1) void Matrix22CasparToStacked_kernel( cudaError_t Matrix22StackedToCaspar(const double* stacked_data, double* cas_data, const unsigned int cas_stride, const unsigned int cas_offset, const unsigned int num_objects) { + if (num_objects == 0) { + return cudaSuccess; + } const int num_blocks = (num_objects + block_size - 1) / block_size; Matrix22StackedToCaspar_kernel<<>>(stacked_data, cas_data, cas_stride, @@ -305,6 +326,9 @@ cudaError_t Matrix22StackedToCaspar(const double* stacked_data, double* cas_data cudaError_t Matrix22CasparToStacked(const double* cas_data, double* stacked_data, const unsigned int cas_stride, const unsigned int cas_offset, const unsigned int num_objects) { + if (num_objects == 0) { + return cudaSuccess; + } const int num_blocks = (num_objects + block_size - 1) / block_size; Matrix22CasparToStacked_kernel<<>>(cas_data, stacked_data, cas_stride, @@ -383,6 +407,9 @@ __global__ __launch_bounds__(block_size, 1) void Matrix23CasparToStacked_kernel( cudaError_t Matrix23StackedToCaspar(const double* stacked_data, double* cas_data, const unsigned int cas_stride, const unsigned int cas_offset, const unsigned int num_objects) { + if (num_objects == 0) { + return cudaSuccess; + } const int num_blocks = (num_objects + block_size - 1) / block_size; Matrix23StackedToCaspar_kernel<<>>(stacked_data, cas_data, cas_stride, @@ -394,6 +421,9 @@ cudaError_t Matrix23StackedToCaspar(const double* stacked_data, double* cas_data cudaError_t Matrix23CasparToStacked(const double* cas_data, double* stacked_data, const unsigned int cas_stride, const unsigned int cas_offset, const unsigned int num_objects) { + if (num_objects == 0) { + return cudaSuccess; + } const int num_blocks = (num_objects + block_size - 1) / block_size; Matrix23CasparToStacked_kernel<<>>(cas_data, stacked_data, cas_stride, @@ -461,6 +491,9 @@ __global__ __launch_bounds__(block_size, 1) void Matrix31CasparToStacked_kernel( cudaError_t Matrix31StackedToCaspar(const double* stacked_data, double* cas_data, const unsigned int cas_stride, const unsigned int cas_offset, const unsigned int num_objects) { + if (num_objects == 0) { + return cudaSuccess; + } const int num_blocks = (num_objects + block_size - 1) / block_size; Matrix31StackedToCaspar_kernel<<>>(stacked_data, cas_data, cas_stride, @@ -472,6 +505,9 @@ cudaError_t Matrix31StackedToCaspar(const double* stacked_data, double* cas_data cudaError_t Matrix31CasparToStacked(const double* cas_data, double* stacked_data, const unsigned int cas_stride, const unsigned int cas_offset, const unsigned int num_objects) { + if (num_objects == 0) { + return cudaSuccess; + } const int num_blocks = (num_objects + block_size - 1) / block_size; Matrix31CasparToStacked_kernel<<>>(cas_data, stacked_data, cas_stride, @@ -566,6 +602,9 @@ __global__ __launch_bounds__(block_size, 1) void Matrix33CasparToStacked_kernel( cudaError_t Matrix33StackedToCaspar(const double* stacked_data, double* cas_data, const unsigned int cas_stride, const unsigned int cas_offset, const unsigned int num_objects) { + if (num_objects == 0) { + return cudaSuccess; + } const int num_blocks = (num_objects + block_size - 1) / block_size; Matrix33StackedToCaspar_kernel<<>>(stacked_data, cas_data, cas_stride, @@ -577,6 +616,9 @@ cudaError_t Matrix33StackedToCaspar(const double* stacked_data, double* cas_data cudaError_t Matrix33CasparToStacked(const double* cas_data, double* stacked_data, const unsigned int cas_stride, const unsigned int cas_offset, const unsigned int num_objects) { + if (num_objects == 0) { + return cudaSuccess; + } const int num_blocks = (num_objects + block_size - 1) / block_size; Matrix33CasparToStacked_kernel<<>>(cas_data, stacked_data, cas_stride, diff --git a/test/symforce_function_codegen_test_data/symengine/symforce_caspar_tridiagonal_test/solver.cc b/test/symforce_function_codegen_test_data/symengine/symforce_caspar_tridiagonal_test/solver.cc index 3b27e4e5..6ba7882c 100644 --- a/test/symforce_function_codegen_test_data/symengine/symforce_caspar_tridiagonal_test/solver.cc +++ b/test/symforce_function_codegen_test_data/symengine/symforce_caspar_tridiagonal_test/solver.cc @@ -126,16 +126,16 @@ GraphSolver::GraphSolver(const SolverParams& params, size_t ChainA_num_m } if (device_id_ != 0) { int deviceCount; - cudaGetDeviceCount(&deviceCount); + CASPAR_CUDA_CHECK(cudaGetDeviceCount(&deviceCount)); if (deviceCount <= device_id_) { throw std::runtime_error("CUDA detected " + std::to_string(deviceCount) + " devices, but device " + std::to_string(device_id_) + " was requested (0-indexed)"); } } - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); uint8_t* origin_ptr = nullptr; - cudaMalloc(&origin_ptr, allocation_size_); + CASPAR_CUDA_CHECK(cudaMalloc(&origin_ptr, allocation_size_)); origin_ptr_.reset(origin_ptr); size_t offset = 0; @@ -291,13 +291,14 @@ size_t GraphSolver::get_allocation_size() { } SolveResult GraphSolver::solve(bool print_progress, bool verbose_logging) { - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); SolveResult result; result.exit_reason = ExitReason::MAX_ITERATIONS; double score_best; double score_best_pcg; double diag = params_.diag_init; - cudaMemcpy(solver__current_diag_, &diag, sizeof(double), cudaMemcpyHostToDevice); + CASPAR_CUDA_CHECK( + cudaMemcpy(solver__current_diag_, &diag, sizeof(double), cudaMemcpyHostToDevice)); double up_scale = params_.diag_scaling_up; double quality; @@ -370,7 +371,8 @@ SolveResult GraphSolver::solve(bool print_progress, bool verbose_logging) { double scale = std::max(params_.diag_scaling_down, 1.0f - quality_tmp * quality_tmp * quality_tmp); diag = std::max(params_.diag_min, diag * scale); - cudaMemcpy(solver__current_diag_, &diag, sizeof(double), cudaMemcpyHostToDevice); + CASPAR_CUDA_CHECK( + cudaMemcpy(solver__current_diag_, &diag, sizeof(double), cudaMemcpyHostToDevice)); up_scale = params_.diag_scaling_up; score_best = score_best_pcg; step_accepted = true; @@ -384,7 +386,8 @@ SolveResult GraphSolver::solve(bool print_progress, bool verbose_logging) { result.exit_reason = ExitReason::CONVERGED_DIAG_EXIT; break; } - cudaMemcpy(solver__current_diag_, &diag, sizeof(double), cudaMemcpyHostToDevice); + CASPAR_CUDA_CHECK( + cudaMemcpy(solver__current_diag_, &diag, sizeof(double), cudaMemcpyHostToDevice)); up_scale *= 2; } const auto t_now = std::chrono::steady_clock::now(); @@ -423,6 +426,8 @@ SolveResult GraphSolver::solve(bool print_progress, bool verbose_logging) { } } + CASPAR_CUDA_CHECK(cudaGetLastError()); + CASPAR_CUDA_CHECK(cudaStreamSynchronize(nullptr)); const auto t_final = std::chrono::steady_clock::now(); result.final_score = score_best; result.iteration_count = solver_iter_; @@ -771,11 +776,14 @@ double GraphSolver::GetPredDecrease() { } void GraphSolver::finish_indices() { + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); + CASPAR_CUDA_CHECK(cudaGetLastError()); + CASPAR_CUDA_CHECK(cudaStreamSynchronize(nullptr)); indices_valid_ = true; } void GraphSolver::SetChainANum(const size_t num) { - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (num > ChainA_num_max_) { throw std::runtime_error(std::to_string(num) + " > ChainA_num_max_"); } @@ -784,46 +792,50 @@ void GraphSolver::SetChainANum(const size_t num) { void GraphSolver::SetChainANodesFromStackedHost(const double* const data, const size_t offset, const size_t num) { - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (offset + num > ChainA_num_) { throw std::runtime_error(std::to_string(offset + num) + " > ChainA_num_"); } - cudaMemcpy(marker__scratch_inout_, data, 2 * num * sizeof(double), cudaMemcpyHostToDevice); - ChainAStackedToCaspar(marker__scratch_inout_, nodes__ChainA__storage_current_, ChainA_num_max_, - offset, num); + CASPAR_CUDA_CHECK( + cudaMemcpy(marker__scratch_inout_, data, 2 * num * sizeof(double), cudaMemcpyHostToDevice)); + CASPAR_CUDA_CHECK(ChainAStackedToCaspar(marker__scratch_inout_, nodes__ChainA__storage_current_, + ChainA_num_max_, offset, num)); } void GraphSolver::SetChainANodesFromStackedDevice(const double* const data, const size_t offset, const size_t num) { - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (offset + num > ChainA_num_) { throw std::runtime_error(std::to_string(offset + num) + " > ChainA_num_"); } - ChainAStackedToCaspar(data, nodes__ChainA__storage_current_, ChainA_num_max_, offset, num); + CASPAR_CUDA_CHECK( + ChainAStackedToCaspar(data, nodes__ChainA__storage_current_, ChainA_num_max_, offset, num)); } void GraphSolver::GetChainANodesToStackedHost(double* const data, const size_t offset, const size_t num) { - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (offset + num > ChainA_num_) { throw std::runtime_error(std::to_string(offset + num) + " > ChainA_num_"); } - ChainACasparToStacked(nodes__ChainA__storage_current_, marker__scratch_inout_, ChainA_num_max_, - offset, num); - cudaMemcpy(data, marker__scratch_inout_, 2 * num * sizeof(double), cudaMemcpyDeviceToHost); + CASPAR_CUDA_CHECK(ChainACasparToStacked(nodes__ChainA__storage_current_, marker__scratch_inout_, + ChainA_num_max_, offset, num)); + CASPAR_CUDA_CHECK( + cudaMemcpy(data, marker__scratch_inout_, 2 * num * sizeof(double), cudaMemcpyDeviceToHost)); } void GraphSolver::GetChainANodesToStackedDevice(double* const data, const size_t offset, const size_t num) { - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (offset + num > ChainA_num_) { throw std::runtime_error(std::to_string(offset + num) + " > ChainA_num_"); } - ChainACasparToStacked(nodes__ChainA__storage_current_, data, ChainA_num_max_, offset, num); + CASPAR_CUDA_CHECK( + ChainACasparToStacked(nodes__ChainA__storage_current_, data, ChainA_num_max_, offset, num)); } void GraphSolver::SetChainBNum(const size_t num) { - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (num > ChainB_num_max_) { throw std::runtime_error(std::to_string(num) + " > ChainB_num_max_"); } @@ -832,42 +844,46 @@ void GraphSolver::SetChainBNum(const size_t num) { void GraphSolver::SetChainBNodesFromStackedHost(const double* const data, const size_t offset, const size_t num) { - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (offset + num > ChainB_num_) { throw std::runtime_error(std::to_string(offset + num) + " > ChainB_num_"); } - cudaMemcpy(marker__scratch_inout_, data, 3 * num * sizeof(double), cudaMemcpyHostToDevice); - ChainBStackedToCaspar(marker__scratch_inout_, nodes__ChainB__storage_current_, ChainB_num_max_, - offset, num); + CASPAR_CUDA_CHECK( + cudaMemcpy(marker__scratch_inout_, data, 3 * num * sizeof(double), cudaMemcpyHostToDevice)); + CASPAR_CUDA_CHECK(ChainBStackedToCaspar(marker__scratch_inout_, nodes__ChainB__storage_current_, + ChainB_num_max_, offset, num)); } void GraphSolver::SetChainBNodesFromStackedDevice(const double* const data, const size_t offset, const size_t num) { - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (offset + num > ChainB_num_) { throw std::runtime_error(std::to_string(offset + num) + " > ChainB_num_"); } - ChainBStackedToCaspar(data, nodes__ChainB__storage_current_, ChainB_num_max_, offset, num); + CASPAR_CUDA_CHECK( + ChainBStackedToCaspar(data, nodes__ChainB__storage_current_, ChainB_num_max_, offset, num)); } void GraphSolver::GetChainBNodesToStackedHost(double* const data, const size_t offset, const size_t num) { - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (offset + num > ChainB_num_) { throw std::runtime_error(std::to_string(offset + num) + " > ChainB_num_"); } - ChainBCasparToStacked(nodes__ChainB__storage_current_, marker__scratch_inout_, ChainB_num_max_, - offset, num); - cudaMemcpy(data, marker__scratch_inout_, 3 * num * sizeof(double), cudaMemcpyDeviceToHost); + CASPAR_CUDA_CHECK(ChainBCasparToStacked(nodes__ChainB__storage_current_, marker__scratch_inout_, + ChainB_num_max_, offset, num)); + CASPAR_CUDA_CHECK( + cudaMemcpy(data, marker__scratch_inout_, 3 * num * sizeof(double), cudaMemcpyDeviceToHost)); } void GraphSolver::GetChainBNodesToStackedDevice(double* const data, const size_t offset, const size_t num) { - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (offset + num > ChainB_num_) { throw std::runtime_error(std::to_string(offset + num) + " > ChainB_num_"); } - ChainBCasparToStacked(nodes__ChainB__storage_current_, data, ChainB_num_max_, offset, num); + CASPAR_CUDA_CHECK( + ChainBCasparToStacked(nodes__ChainB__storage_current_, data, ChainB_num_max_, offset, num)); } void GraphSolver::SetChainALinkNum(const size_t num) { @@ -878,43 +894,47 @@ void GraphSolver::SetChainALinkNum(const size_t num) { } void GraphSolver::SetChainALinkWeightDataFromStackedHost(const double* const data, size_t offset, size_t num) { - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (offset + num > chain_a_link_num_max_) { throw std::runtime_error(std::to_string(offset + num) + " > chain_a_link_num_max_"); } - cudaMemcpy(marker__scratch_inout_, data, 4 * num * sizeof(double), cudaMemcpyHostToDevice); - Matrix22StackedToCaspar(marker__scratch_inout_, facs__chain_a_link__args__weight__data_, - chain_a_link_num_max_, offset, num); + CASPAR_CUDA_CHECK( + cudaMemcpy(marker__scratch_inout_, data, 4 * num * sizeof(double), cudaMemcpyHostToDevice)); + CASPAR_CUDA_CHECK(Matrix22StackedToCaspar(marker__scratch_inout_, + facs__chain_a_link__args__weight__data_, + chain_a_link_num_max_, offset, num)); } void GraphSolver::SetChainALinkWeightDataFromStackedDevice(const double* const data, size_t offset, size_t num) { - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (offset + num > chain_a_link_num_max_) { throw std::runtime_error(std::to_string(offset + num) + " > chain_a_link_num_max_"); } - Matrix22StackedToCaspar(data, facs__chain_a_link__args__weight__data_, chain_a_link_num_max_, - offset, num); + CASPAR_CUDA_CHECK(Matrix22StackedToCaspar(data, facs__chain_a_link__args__weight__data_, + chain_a_link_num_max_, offset, num)); } void GraphSolver::SetChainALinkOffsetDataFromStackedHost(const double* const data, size_t offset, size_t num) { - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (offset + num > chain_a_link_num_max_) { throw std::runtime_error(std::to_string(offset + num) + " > chain_a_link_num_max_"); } - cudaMemcpy(marker__scratch_inout_, data, 2 * num * sizeof(double), cudaMemcpyHostToDevice); - Matrix21StackedToCaspar(marker__scratch_inout_, facs__chain_a_link__args__offset__data_, - chain_a_link_num_max_, offset, num); + CASPAR_CUDA_CHECK( + cudaMemcpy(marker__scratch_inout_, data, 2 * num * sizeof(double), cudaMemcpyHostToDevice)); + CASPAR_CUDA_CHECK(Matrix21StackedToCaspar(marker__scratch_inout_, + facs__chain_a_link__args__offset__data_, + chain_a_link_num_max_, offset, num)); } void GraphSolver::SetChainALinkOffsetDataFromStackedDevice(const double* const data, size_t offset, size_t num) { - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (offset + num > chain_a_link_num_max_) { throw std::runtime_error(std::to_string(offset + num) + " > chain_a_link_num_max_"); } - Matrix21StackedToCaspar(data, facs__chain_a_link__args__offset__data_, chain_a_link_num_max_, - offset, num); + CASPAR_CUDA_CHECK(Matrix21StackedToCaspar(data, facs__chain_a_link__args__offset__data_, + chain_a_link_num_max_, offset, num)); } void GraphSolver::SetChainAPriorNum(const size_t num) { if (num > chain_a_prior_num_max_) { @@ -923,20 +943,20 @@ void GraphSolver::SetChainAPriorNum(const size_t num) { chain_a_prior_num_ = num; } void GraphSolver::SetChainAPriorAIndicesFromHost(const unsigned int* const indices, size_t num) { - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (num != chain_a_prior_num_) { throw std::runtime_error(std::to_string(num) + " != chain_a_prior_num_. Use Setchain_a_priorNum " "before setting indices."); } - cudaMemcpy((unsigned int*)marker__scratch_inout_, indices, num * sizeof(unsigned int), - cudaMemcpyHostToDevice); + CASPAR_CUDA_CHECK(cudaMemcpy((unsigned int*)marker__scratch_inout_, indices, + num * sizeof(unsigned int), cudaMemcpyHostToDevice)); SetChainAPriorAIndicesFromDevice((unsigned int*)marker__scratch_inout_, num); } void GraphSolver::SetChainAPriorAIndicesFromDevice(const unsigned int* const indices, size_t num) { indices_valid_ = false; - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (num != chain_a_prior_num_) { throw std::runtime_error(std::to_string(num) + @@ -954,23 +974,25 @@ void GraphSolver::SetChainAPriorAIndicesFromDevice(const unsigned int* const ind } void GraphSolver::SetChainAPriorTargetDataFromStackedHost(const double* const data, size_t offset, size_t num) { - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (offset + num > chain_a_prior_num_max_) { throw std::runtime_error(std::to_string(offset + num) + " > chain_a_prior_num_max_"); } - cudaMemcpy(marker__scratch_inout_, data, 2 * num * sizeof(double), cudaMemcpyHostToDevice); - Matrix21StackedToCaspar(marker__scratch_inout_, facs__chain_a_prior__args__target__data_, - chain_a_prior_num_max_, offset, num); + CASPAR_CUDA_CHECK( + cudaMemcpy(marker__scratch_inout_, data, 2 * num * sizeof(double), cudaMemcpyHostToDevice)); + CASPAR_CUDA_CHECK(Matrix21StackedToCaspar(marker__scratch_inout_, + facs__chain_a_prior__args__target__data_, + chain_a_prior_num_max_, offset, num)); } void GraphSolver::SetChainAPriorTargetDataFromStackedDevice(const double* const data, size_t offset, size_t num) { - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (offset + num > chain_a_prior_num_max_) { throw std::runtime_error(std::to_string(offset + num) + " > chain_a_prior_num_max_"); } - Matrix21StackedToCaspar(data, facs__chain_a_prior__args__target__data_, chain_a_prior_num_max_, - offset, num); + CASPAR_CUDA_CHECK(Matrix21StackedToCaspar(data, facs__chain_a_prior__args__target__data_, + chain_a_prior_num_max_, offset, num)); } void GraphSolver::SetChainBLinkNum(const size_t num) { if (num > chain_b_link_num_max_) { @@ -980,43 +1002,47 @@ void GraphSolver::SetChainBLinkNum(const size_t num) { } void GraphSolver::SetChainBLinkWeightDataFromStackedHost(const double* const data, size_t offset, size_t num) { - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (offset + num > chain_b_link_num_max_) { throw std::runtime_error(std::to_string(offset + num) + " > chain_b_link_num_max_"); } - cudaMemcpy(marker__scratch_inout_, data, 9 * num * sizeof(double), cudaMemcpyHostToDevice); - Matrix33StackedToCaspar(marker__scratch_inout_, facs__chain_b_link__args__weight__data_, - chain_b_link_num_max_, offset, num); + CASPAR_CUDA_CHECK( + cudaMemcpy(marker__scratch_inout_, data, 9 * num * sizeof(double), cudaMemcpyHostToDevice)); + CASPAR_CUDA_CHECK(Matrix33StackedToCaspar(marker__scratch_inout_, + facs__chain_b_link__args__weight__data_, + chain_b_link_num_max_, offset, num)); } void GraphSolver::SetChainBLinkWeightDataFromStackedDevice(const double* const data, size_t offset, size_t num) { - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (offset + num > chain_b_link_num_max_) { throw std::runtime_error(std::to_string(offset + num) + " > chain_b_link_num_max_"); } - Matrix33StackedToCaspar(data, facs__chain_b_link__args__weight__data_, chain_b_link_num_max_, - offset, num); + CASPAR_CUDA_CHECK(Matrix33StackedToCaspar(data, facs__chain_b_link__args__weight__data_, + chain_b_link_num_max_, offset, num)); } void GraphSolver::SetChainBLinkOffsetDataFromStackedHost(const double* const data, size_t offset, size_t num) { - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (offset + num > chain_b_link_num_max_) { throw std::runtime_error(std::to_string(offset + num) + " > chain_b_link_num_max_"); } - cudaMemcpy(marker__scratch_inout_, data, 3 * num * sizeof(double), cudaMemcpyHostToDevice); - Matrix31StackedToCaspar(marker__scratch_inout_, facs__chain_b_link__args__offset__data_, - chain_b_link_num_max_, offset, num); + CASPAR_CUDA_CHECK( + cudaMemcpy(marker__scratch_inout_, data, 3 * num * sizeof(double), cudaMemcpyHostToDevice)); + CASPAR_CUDA_CHECK(Matrix31StackedToCaspar(marker__scratch_inout_, + facs__chain_b_link__args__offset__data_, + chain_b_link_num_max_, offset, num)); } void GraphSolver::SetChainBLinkOffsetDataFromStackedDevice(const double* const data, size_t offset, size_t num) { - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (offset + num > chain_b_link_num_max_) { throw std::runtime_error(std::to_string(offset + num) + " > chain_b_link_num_max_"); } - Matrix31StackedToCaspar(data, facs__chain_b_link__args__offset__data_, chain_b_link_num_max_, - offset, num); + CASPAR_CUDA_CHECK(Matrix31StackedToCaspar(data, facs__chain_b_link__args__offset__data_, + chain_b_link_num_max_, offset, num)); } void GraphSolver::SetChainBPriorNum(const size_t num) { if (num > chain_b_prior_num_max_) { @@ -1025,20 +1051,20 @@ void GraphSolver::SetChainBPriorNum(const size_t num) { chain_b_prior_num_ = num; } void GraphSolver::SetChainBPriorBIndicesFromHost(const unsigned int* const indices, size_t num) { - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (num != chain_b_prior_num_) { throw std::runtime_error(std::to_string(num) + " != chain_b_prior_num_. Use Setchain_b_priorNum " "before setting indices."); } - cudaMemcpy((unsigned int*)marker__scratch_inout_, indices, num * sizeof(unsigned int), - cudaMemcpyHostToDevice); + CASPAR_CUDA_CHECK(cudaMemcpy((unsigned int*)marker__scratch_inout_, indices, + num * sizeof(unsigned int), cudaMemcpyHostToDevice)); SetChainBPriorBIndicesFromDevice((unsigned int*)marker__scratch_inout_, num); } void GraphSolver::SetChainBPriorBIndicesFromDevice(const unsigned int* const indices, size_t num) { indices_valid_ = false; - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (num != chain_b_prior_num_) { throw std::runtime_error(std::to_string(num) + @@ -1056,23 +1082,25 @@ void GraphSolver::SetChainBPriorBIndicesFromDevice(const unsigned int* const ind } void GraphSolver::SetChainBPriorTargetDataFromStackedHost(const double* const data, size_t offset, size_t num) { - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (offset + num > chain_b_prior_num_max_) { throw std::runtime_error(std::to_string(offset + num) + " > chain_b_prior_num_max_"); } - cudaMemcpy(marker__scratch_inout_, data, 3 * num * sizeof(double), cudaMemcpyHostToDevice); - Matrix31StackedToCaspar(marker__scratch_inout_, facs__chain_b_prior__args__target__data_, - chain_b_prior_num_max_, offset, num); + CASPAR_CUDA_CHECK( + cudaMemcpy(marker__scratch_inout_, data, 3 * num * sizeof(double), cudaMemcpyHostToDevice)); + CASPAR_CUDA_CHECK(Matrix31StackedToCaspar(marker__scratch_inout_, + facs__chain_b_prior__args__target__data_, + chain_b_prior_num_max_, offset, num)); } void GraphSolver::SetChainBPriorTargetDataFromStackedDevice(const double* const data, size_t offset, size_t num) { - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (offset + num > chain_b_prior_num_max_) { throw std::runtime_error(std::to_string(offset + num) + " > chain_b_prior_num_max_"); } - Matrix31StackedToCaspar(data, facs__chain_b_prior__args__target__data_, chain_b_prior_num_max_, - offset, num); + CASPAR_CUDA_CHECK(Matrix31StackedToCaspar(data, facs__chain_b_prior__args__target__data_, + chain_b_prior_num_max_, offset, num)); } void GraphSolver::SetChainCouplingNum(const size_t num) { if (num > chain_coupling_num_max_) { @@ -1081,21 +1109,21 @@ void GraphSolver::SetChainCouplingNum(const size_t num) { chain_coupling_num_ = num; } void GraphSolver::SetChainCouplingAIndicesFromHost(const unsigned int* const indices, size_t num) { - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (num != chain_coupling_num_) { throw std::runtime_error(std::to_string(num) + " != chain_coupling_num_. Use " "Setchain_couplingNum before setting indices."); } - cudaMemcpy((unsigned int*)marker__scratch_inout_, indices, num * sizeof(unsigned int), - cudaMemcpyHostToDevice); + CASPAR_CUDA_CHECK(cudaMemcpy((unsigned int*)marker__scratch_inout_, indices, + num * sizeof(unsigned int), cudaMemcpyHostToDevice)); SetChainCouplingAIndicesFromDevice((unsigned int*)marker__scratch_inout_, num); } void GraphSolver::SetChainCouplingAIndicesFromDevice(const unsigned int* const indices, size_t num) { indices_valid_ = false; - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (num != chain_coupling_num_) { throw std::runtime_error(std::to_string(num) + @@ -1112,21 +1140,21 @@ void GraphSolver::SetChainCouplingAIndicesFromDevice(const unsigned int* const i SharedIndices(indices, facs__chain_coupling__args__a__idx_shared_, num); } void GraphSolver::SetChainCouplingBIndicesFromHost(const unsigned int* const indices, size_t num) { - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (num != chain_coupling_num_) { throw std::runtime_error(std::to_string(num) + " != chain_coupling_num_. Use " "Setchain_couplingNum before setting indices."); } - cudaMemcpy((unsigned int*)marker__scratch_inout_, indices, num * sizeof(unsigned int), - cudaMemcpyHostToDevice); + CASPAR_CUDA_CHECK(cudaMemcpy((unsigned int*)marker__scratch_inout_, indices, + num * sizeof(unsigned int), cudaMemcpyHostToDevice)); SetChainCouplingBIndicesFromDevice((unsigned int*)marker__scratch_inout_, num); } void GraphSolver::SetChainCouplingBIndicesFromDevice(const unsigned int* const indices, size_t num) { indices_valid_ = false; - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (num != chain_coupling_num_) { throw std::runtime_error(std::to_string(num) + @@ -1144,43 +1172,47 @@ void GraphSolver::SetChainCouplingBIndicesFromDevice(const unsigned int* const i } void GraphSolver::SetChainCouplingWeightDataFromStackedHost(const double* const data, size_t offset, size_t num) { - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (offset + num > chain_coupling_num_max_) { throw std::runtime_error(std::to_string(offset + num) + " > chain_coupling_num_max_"); } - cudaMemcpy(marker__scratch_inout_, data, 6 * num * sizeof(double), cudaMemcpyHostToDevice); - Matrix23StackedToCaspar(marker__scratch_inout_, facs__chain_coupling__args__weight__data_, - chain_coupling_num_max_, offset, num); + CASPAR_CUDA_CHECK( + cudaMemcpy(marker__scratch_inout_, data, 6 * num * sizeof(double), cudaMemcpyHostToDevice)); + CASPAR_CUDA_CHECK(Matrix23StackedToCaspar(marker__scratch_inout_, + facs__chain_coupling__args__weight__data_, + chain_coupling_num_max_, offset, num)); } void GraphSolver::SetChainCouplingWeightDataFromStackedDevice(const double* const data, size_t offset, size_t num) { - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (offset + num > chain_coupling_num_max_) { throw std::runtime_error(std::to_string(offset + num) + " > chain_coupling_num_max_"); } - Matrix23StackedToCaspar(data, facs__chain_coupling__args__weight__data_, chain_coupling_num_max_, - offset, num); + CASPAR_CUDA_CHECK(Matrix23StackedToCaspar(data, facs__chain_coupling__args__weight__data_, + chain_coupling_num_max_, offset, num)); } void GraphSolver::SetChainCouplingOffsetDataFromStackedHost(const double* const data, size_t offset, size_t num) { - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (offset + num > chain_coupling_num_max_) { throw std::runtime_error(std::to_string(offset + num) + " > chain_coupling_num_max_"); } - cudaMemcpy(marker__scratch_inout_, data, 2 * num * sizeof(double), cudaMemcpyHostToDevice); - Matrix21StackedToCaspar(marker__scratch_inout_, facs__chain_coupling__args__offset__data_, - chain_coupling_num_max_, offset, num); + CASPAR_CUDA_CHECK( + cudaMemcpy(marker__scratch_inout_, data, 2 * num * sizeof(double), cudaMemcpyHostToDevice)); + CASPAR_CUDA_CHECK(Matrix21StackedToCaspar(marker__scratch_inout_, + facs__chain_coupling__args__offset__data_, + chain_coupling_num_max_, offset, num)); } void GraphSolver::SetChainCouplingOffsetDataFromStackedDevice(const double* const data, size_t offset, size_t num) { - cudaSetDevice(device_id_); + CASPAR_CUDA_CHECK(cudaSetDevice(device_id_)); if (offset + num > chain_coupling_num_max_) { throw std::runtime_error(std::to_string(offset + num) + " > chain_coupling_num_max_"); } - Matrix21StackedToCaspar(data, facs__chain_coupling__args__offset__data_, chain_coupling_num_max_, - offset, num); + CASPAR_CUDA_CHECK(Matrix21StackedToCaspar(data, facs__chain_coupling__args__offset__data_, + chain_coupling_num_max_, offset, num)); } size_t GraphSolver::get_nbytes() {