FZGPUModules 2.0
GPU-accelerated modular compression pipelines
Loading...
Searching...
No Matches
algorithms.h
Go to the documentation of this file.
1#pragma once
2
31#include "backend/api.h"
32#include "backend/types.h"
33#include "cuda_check.h"
34#include "mem/mempool.h"
35
36#if defined(FZGMOD_BACKEND_SYCL)
37
38#error "FZGMOD_BACKEND=SYCL is not implemented yet"
39
40#endif
41
42#include <thrust/device_ptr.h>
43#include <thrust/execution_policy.h>
44#include <thrust/scan.h>
45
46#include <cstddef>
47
48namespace fz {
49namespace backend {
50
51namespace detail {
52
54inline auto parOn(fz::stream_t stream) {
55#if defined(FZGMOD_BACKEND_HIP)
56 return thrust::hip::par.on(stream);
57#else
58 return thrust::cuda::par.on(stream);
59#endif
60}
61
62} // namespace detail
63
73 void* ptr = nullptr;
74 bool from_pool = false;
75};
76
89template<typename Fn>
90TempStorage withTempStorage(MemoryPool* pool, fz::stream_t stream, const char* tag, Fn&& fn) {
91 size_t bytes = 0;
92 fn(nullptr, bytes);
93 void* d_temp = pool ? pool->allocate(bytes, stream, tag) : nullptr;
94 bool from_pool = (d_temp != nullptr);
95 if (!d_temp && bytes > 0) {
96 FZ_CUDA_CHECK(cudaMalloc(&d_temp, bytes));
97 from_pool = false;
98 }
99 fn(d_temp, bytes);
100 return TempStorage{d_temp, from_pool};
101}
102
104inline void freeTempStorage(MemoryPool* pool, const TempStorage& temp, fz::stream_t stream) {
105 if (!temp.ptr) return;
106 if (temp.from_pool && pool) {
107 pool->free(temp.ptr, stream);
108 } else {
109 FZ_CUDA_CHECK_WARN(cudaStreamSynchronize(stream));
110 FZ_CUDA_CHECK_WARN(cudaFree(temp.ptr));
111 }
112}
113
115template<typename T>
116void exclusiveScan(fz::stream_t stream, T* d_in, T* d_out, size_t n) {
117 thrust::device_ptr<T> in_ptr(d_in);
118 thrust::device_ptr<T> out_ptr(d_out);
119 thrust::exclusive_scan(detail::parOn(stream), in_ptr, in_ptr + n, out_ptr);
120}
121
122} // namespace backend
123} // namespace fz
TempStorage withTempStorage(MemoryPool *pool, fz::stream_t stream, const char *tag, Fn &&fn)
Definition algorithms.h:90
void freeTempStorage(MemoryPool *pool, const TempStorage &temp, fz::stream_t stream)
Definition algorithms.h:104
void exclusiveScan(fz::stream_t stream, T *d_in, T *d_out, size_t n)
Definition algorithms.h:116
auto parOn(fz::stream_t stream)
Definition algorithms.h:54
Backend-neutral spelling of the host-side GPU runtime API.
Definition mempool.h:82
void free(void *ptr, fz::stream_t stream)
void * allocate(size_t size, fz::stream_t stream, const std::string &tag="", bool persistent=false)
GPU backend API error-checking macros.
Stream-ordered CUDA memory pool for pipeline buffer management.
Definition algorithms.h:48
Definition algorithms.h:72
Backend-neutral GPU type aliases.