Add allocate_memory and free_memory

This commit is contained in:
Bram Veenboer
2025-09-02 11:55:33 +02:00
parent 716f323b26
commit 8df4bbf54e
4 changed files with 59 additions and 12 deletions

View File

@@ -19,9 +19,17 @@ struct GPUBackend::Impl {
}
}
void *allocate_memory(size_t bytes) const {
void *ptr;
cudaMallocHost(&ptr, bytes);
return ptr;
}
void free_memory(void *ptr) const { cudaFreeHost(ptr); }
void init(size_t n) {
const size_t bytes = n * sizeof(float);
cudaMallocHost(&h_x, bytes);
h_x = reinterpret_cast<float *>(allocate_memory(bytes));
cudaMalloc(&d_x, bytes);
}
@@ -71,6 +79,12 @@ GPUBackend::~GPUBackend() = default;
void GPUBackend::init(size_t n) { impl->init(n); }
void *GPUBackend::allocate_memory(size_t bytes) const {
return impl->allocate_memory(bytes);
}
void GPUBackend::free_memory(void *ptr) const { impl->free_memory(ptr); }
void GPUBackend::compute_sinf(size_t n, const float *x, float *s) const {
impl->compute_sinf(n, x, s);
}