Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions symforce/caspar/source/runtime/shared_indices.cu
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <cooperative_groups/reduce.h>

#include "shared_indices.h"
#include "solver_tools.h"

namespace cg = cooperative_groups;

Expand Down Expand Up @@ -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<<<n_blocks, 1024>>>(indices, shared_indices_out, problem_size);
CASPAR_CUDA_CHECK(cudaGetLastError());
}

} // namespace caspar
19 changes: 11 additions & 8 deletions symforce/caspar/source/runtime/solver_tools.cu
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,34 @@ namespace caspar {
template <typename T>
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 <typename T>
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 <typename T>
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;
}

template <typename T>
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;
}

Expand All @@ -54,6 +55,7 @@ template <typename T>
void AlphaFromNumDenom(const T* alpha_numerator, const T* alpha_denominator, T* alpha,
T* neg_alpha) {
AlphaFromNumDenomKernel<T><<<1, 1>>>(alpha_numerator, alpha_denominator, alpha, neg_alpha);
CASPAR_CUDA_CHECK(cudaGetLastError());
}

template <typename T>
Expand All @@ -64,6 +66,7 @@ __global__ void BetaFromNumDenomKernel(const T* beta_num, const T* beta_denum, T
template <typename T>
void BetaFromNumDenom(const T* beta_num, const T* beta_denum, T* beta) {
BetaFromNumDenomKernel<T><<<1, 1>>>(beta_num, beta_denum, beta);
CASPAR_CUDA_CHECK(cudaGetLastError());
}

// Explicit instantiations
Expand Down
18 changes: 18 additions & 0 deletions symforce/caspar/source/runtime/solver_tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,26 @@

#pragma once

#include <stdexcept>
#include <string>

#include <cuda_runtime.h>

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 <typename T>
void Zero(T* start, T* end);

Expand Down
29 changes: 20 additions & 9 deletions symforce/caspar/source/runtime/sort_indices.cu
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <cub/cub.cuh>

#include "solver_tools.h"
#include "sort_indices.h"

namespace {
Expand All @@ -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<<<grid_size, 1024>>>(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<<<grid_size, 1024>>>(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,
Expand All @@ -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<<<grid_size, 1024>>>(input, selections, output, problem_size);
CASPAR_CUDA_CHECK(cudaGetLastError());
}

} // namespace caspar
6 changes: 6 additions & 0 deletions symforce/caspar/source/templates/caspar_mappings.cu.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -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<<<num_blocks, block_size>>>(stacked_data, cas_data, cas_stride, cas_offset, num_objects);
Expand All @@ -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<<<num_blocks, block_size>>>(cas_data, stacked_data, cas_stride, cas_offset, num_objects);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "caspar_mappings.h"
#include "pybind_array_tools.h"
#include "solver_tools.h"

namespace caspar {

Expand All @@ -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) {
Expand All @@ -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 %}
}
Expand Down
Loading