Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/harrism/ranger
Generate simple index ranges in C++ and CUDA C++
https://github.com/harrism/ranger
cpp cuda loops ranges
Last synced: 3 months ago
JSON representation
Generate simple index ranges in C++ and CUDA C++
- Host: GitHub
- URL: https://github.com/harrism/ranger
- Owner: harrism
- License: apache-2.0
- Created: 2022-03-23T02:41:24.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2023-06-14T00:39:12.000Z (over 1 year ago)
- Last Synced: 2024-10-14T14:39:17.934Z (4 months ago)
- Topics: cpp, cuda, loops, ranges
- Language: C++
- Homepage:
- Size: 17.6 KB
- Stars: 39
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Ranger: CUDA-enabled range helpers for C++ range-based for loops
Facilities for generating simple index ranges for C++ range-based for loops. Includes support for
CUDA grid-stride ranges.## Examples:
```
// generate values from 0 to N
for (auto i : range(N)) {
std::cout << i << std::endl;
}
``````
// generate values from `begin` to `end`
for (auto i : range(begin, end)) {
std::cout << i << std::endl;
}
``````
// generate values stepping by `step` from `begin` to `end`
for (auto i : range(begin, end, step)) {
std::cout << i << std::endl;
}
``````
// generate values from 0 to N in a kernel
__global__ void size_kernel(int N, int* out)
{
for (auto i : grid_stride_range(N)) {
out[i] = i;
}
}
``````
// generate values from begin to N in a kernel
__global__ void begin_end_kernel(int begin, end, int* out)
{
for (auto i : grid_stride_range(begin, end)) {
out[i-begin] = i;
}
}
``````
// generate values stepping by `step` from 0 to N in a kernel
__global__ void step_kernel(int N, int step, int* out)
{
for (auto i : grid_stride_range(0, N, step)) {
out[i / step] = i;
}
}
``````
// This version of grid_stride_range returns an index and an active_mask that excludes
// threads that step outside the range
template
__global__ void valid_if_kernel(active_mask_type* output, thread_index_type size, Predicate pred)
{
constexpr std::int32_t leader_lane{0};
constexpr std::int32_t warp_size{32};
thread_index_type const lane_id{threadIdx.x % warp_size};active_mask_type initial_active_mask = 0xFFFF'FFFF;
for (auto [i, active_mask] : grid_stride_range(size, initial_active_mask)) {
active_mask_type ballot = __ballot_sync(active_mask, pred(i));
if (lane_id == leader_lane) { output[i / warp_size] = ballot; }
}
}
```