Skip to content

Commit

Permalink
Reorganize CUDA dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremykubica committed Mar 11, 2024
1 parent 185d299 commit c134561
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 74 deletions.
5 changes: 3 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ target_link_libraries(search PRIVATE
if(HAVE_CUDA)
message(STATUS "Building CUDA Libraries")
add_library(searchcu STATIC
src/kbmod/search/image_kernels.cu
src/kbmod/search/kernels.cu
src/kbmod/search/kernels/image_kernels.cu
src/kbmod/search/kernels/kernel_memory.cu
src/kbmod/search/kernels/kernels.cu
)

set_target_properties(searchcu PROPERTIES
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#define IMAGE_KERNELS_CU_

#include <assert.h>
#include "common.h"
#include "../common.h"
#include "cuda_errors.h"
#include <stdio.h>
#include <float.h>
Expand Down
109 changes: 109 additions & 0 deletions src/kbmod/search/kernels/kernel_memory.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* kernel_memory.cu
*
* Helper functions for transfering KBMOD data to/from GPU.
*/

#ifndef KERNELS_MEMORY_CU_
#define KERNELS_MEMORY_CU_

#include <stdexcept>
#include <float.h>
#include <vector>

#include "cuda_errors.h"

#include "../trajectory_list.h"

namespace search {


// ---------------------------------------
// --- Basic Memory Functions ------------
// ---------------------------------------

extern "C" void* allocate_gpu_block(unsigned long memory_size) {
void *gpu_ptr;
checkCudaErrors(cudaMalloc((void **)&gpu_ptr, memory_size));
if (gpu_ptr == nullptr) throw std::runtime_error("Unable to allocate GPU memory.");
return gpu_ptr;
}

extern "C" void free_gpu_block(void* gpu_ptr) {
if (gpu_ptr == nullptr) throw std::runtime_error("Trying to free nullptr.");
checkCudaErrors(cudaFree(gpu_ptr));
}

extern "C" void copy_block_to_gpu(void* cpu_ptr, void* gpu_ptr, unsigned long memory_size) {
if (cpu_ptr == nullptr) throw std::runtime_error("Invalid CPU pointer");
if (gpu_ptr == nullptr) throw std::runtime_error("Invalid GPU pointer");

checkCudaErrors(cudaMemcpy(gpu_ptr, cpu_ptr, memory_size, cudaMemcpyHostToDevice));
}

extern "C" void copy_block_to_cpu(void* cpu_ptr, void* gpu_ptr, unsigned long memory_size) {
if (cpu_ptr == nullptr) throw std::runtime_error("Invalid CPU pointer");
if (gpu_ptr == nullptr) throw std::runtime_error("Invalid GPU pointer");

checkCudaErrors(cudaMemcpy(cpu_ptr, gpu_ptr, memory_size, cudaMemcpyDeviceToHost));
}

// ---------------------------------------
// --- Memory Functions ------------------
// ---------------------------------------


extern "C" float *move_floats_to_gpu(std::vector<float> &data) {
unsigned long memory_size = data.size() * sizeof(float);

float *gpu_ptr;
checkCudaErrors(cudaMalloc((void **)&gpu_ptr, memory_size));
checkCudaErrors(cudaMemcpy(gpu_ptr, data.data(), memory_size, cudaMemcpyHostToDevice));

return gpu_ptr;
}

extern "C" void free_gpu_float_array(float *gpu_ptr) {
if (gpu_ptr == nullptr) throw std::runtime_error("Trying to free nullptr.");
checkCudaErrors(cudaFree(gpu_ptr));
}

extern "C" void *move_void_array_to_gpu(void *data_array, long unsigned memory_size) {
if (data_array == nullptr) throw std::runtime_error("No data given.");
if (memory_size == 0) throw std::runtime_error("Invalid size.");

void *gpu_ptr;
checkCudaErrors(cudaMalloc((void **)&gpu_ptr, memory_size));
checkCudaErrors(cudaMemcpy(gpu_ptr, data_array, memory_size, cudaMemcpyHostToDevice));

return gpu_ptr;
}

extern "C" void free_gpu_void_array(void *gpu_ptr) {
if (gpu_ptr == nullptr) throw std::runtime_error("Trying to free nullptr.");
checkCudaErrors(cudaFree(gpu_ptr));
}

extern "C" Trajectory *allocate_gpu_trajectory_list(long unsigned num_trj) {
Trajectory *gpu_ptr;
checkCudaErrors(cudaMalloc((void **)&gpu_ptr, num_trj * sizeof(Trajectory)));
return gpu_ptr;
}

extern "C" void free_gpu_trajectory_list(Trajectory *gpu_ptr) { checkCudaErrors(cudaFree(gpu_ptr)); }

extern "C" void copy_trajectory_list(Trajectory *cpu_ptr, Trajectory *gpu_ptr, long unsigned num_trj,
bool to_gpu) {
if ((cpu_ptr == nullptr) || (gpu_ptr == nullptr)) throw std::runtime_error("Invalid pointer.");
long unsigned memory_size = num_trj * sizeof(Trajectory);

if (to_gpu) {
checkCudaErrors(cudaMemcpy(gpu_ptr, cpu_ptr, memory_size, cudaMemcpyHostToDevice));
} else {
checkCudaErrors(cudaMemcpy(cpu_ptr, gpu_ptr, memory_size, cudaMemcpyDeviceToHost));
}
}

} /* namespace search */

#endif /* KERNELS_MEMORY_CU_ */
44 changes: 44 additions & 0 deletions src/kbmod/search/kernels/kernel_memory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* kernel_memory.h
*
* Helper functions for transfering KBMOD data to/from GPU.
*/

#ifndef KERNELS_MEMORY_H_
#define KERNELS_MEMORY_H_

#include "../trajectory_list.h"

namespace search {

// ---------------------------------------
// --- Basic Memory Functions ------------
// ---------------------------------------

extern "C" void* allocate_gpu_block(unsigned long memory_size);

extern "C" void free_gpu_block(void* gpu_ptr);

extern "C" void copy_block_to_gpu(void* cpu_ptr, void* gpu_ptr, unsigned long memory_size);

extern "C" void copy_block_to_cpu(void* cpu_ptr, void* gpu_ptr, unsigned long memory_size);

// ---------------------------------------
// --- Memory Functions ------------------
// ---------------------------------------

extern "C" float *move_floats_to_gpu(std::vector<float> &data);
extern "C" void free_gpu_float_array(float *gpu_ptr);

extern "C" void *move_void_array_to_gpu(void *data_array, long unsigned memory_size);
extern "C" void free_gpu_void_array(void *gpu_ptr);

extern "C" Trajectory *allocate_gpu_trajectory_list(long unsigned num_trj);
extern "C" void free_gpu_trajectory_list(Trajectory *gpu_ptr);
extern "C" void copy_trajectory_list(Trajectory *cpu_ptr, Trajectory *gpu_ptr, long unsigned num_trj,
bool to_gpu);


} /* namespace search */

#endif /* KERNELS_MEMORY_CU_ */
Original file line number Diff line number Diff line change
Expand Up @@ -16,67 +16,14 @@
#include <stdio.h>
#include <float.h>

#include "common.h"
#include "../common.h"
#include "cuda_errors.h"
#include "psi_phi_array_ds.h"
#include "trajectory_list.h"
#include "../psi_phi_array_ds.h"
#include "../trajectory_list.h"

namespace search {

// ---------------------------------------
// --- Memory Functions ------------------
// ---------------------------------------

extern "C" float *move_floats_to_gpu(std::vector<float> &data) {
unsigned long memory_size = data.size() * sizeof(float);

float *gpu_ptr;
checkCudaErrors(cudaMalloc((void **)&gpu_ptr, memory_size));
checkCudaErrors(cudaMemcpy(gpu_ptr, data.data(), memory_size, cudaMemcpyHostToDevice));

return gpu_ptr;
}

extern "C" void free_gpu_float_array(float *gpu_ptr) {
if (gpu_ptr == nullptr) throw std::runtime_error("Trying to free nullptr.");
checkCudaErrors(cudaFree(gpu_ptr));
}

extern "C" void *move_void_array_to_gpu(void *data_array, long unsigned memory_size) {
if (data_array == nullptr) throw std::runtime_error("No data given.");
if (memory_size == 0) throw std::runtime_error("Invalid size.");

void *gpu_ptr;
checkCudaErrors(cudaMalloc((void **)&gpu_ptr, memory_size));
checkCudaErrors(cudaMemcpy(gpu_ptr, data_array, memory_size, cudaMemcpyHostToDevice));
#include "kernel_memory.h"

return gpu_ptr;
}

extern "C" void free_gpu_void_array(void *gpu_ptr) {
if (gpu_ptr == nullptr) throw std::runtime_error("Trying to free nullptr.");
checkCudaErrors(cudaFree(gpu_ptr));
}

extern "C" Trajectory *allocate_gpu_trajectory_list(long unsigned num_trj) {
Trajectory *gpu_ptr;
checkCudaErrors(cudaMalloc((void **)&gpu_ptr, num_trj * sizeof(Trajectory)));
return gpu_ptr;
}

extern "C" void free_gpu_trajectory_list(Trajectory *gpu_ptr) { checkCudaErrors(cudaFree(gpu_ptr)); }

extern "C" void copy_trajectory_list(Trajectory *cpu_ptr, Trajectory *gpu_ptr, long unsigned num_trj,
bool to_gpu) {
if ((cpu_ptr == nullptr) || (gpu_ptr == nullptr)) throw std::runtime_error("Invalid pointer.");
long unsigned memory_size = num_trj * sizeof(Trajectory);

if (to_gpu) {
checkCudaErrors(cudaMemcpy(gpu_ptr, cpu_ptr, memory_size, cudaMemcpyHostToDevice));
} else {
checkCudaErrors(cudaMemcpy(cpu_ptr, gpu_ptr, memory_size, cudaMemcpyDeviceToHost));
}
}
namespace search {

// ---------------------------------------
// --- Data Access Functions -------------
Expand Down
9 changes: 3 additions & 6 deletions src/kbmod/search/psi_phi_array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@
#include "psi_phi_array_utils.h"
#include "pydocs/psi_phi_array_docs.h"

namespace search {

// Declaration of CUDA functions that will be linked in.
#ifdef HAVE_CUDA
extern "C" float* move_floats_to_gpu(std::vector<float>& data);
extern "C" void free_gpu_float_array(float* gpu_ptr);
extern "C" void* move_void_array_to_gpu(void* data_array, long unsigned memory_size);
extern "C" void free_gpu_void_array(void* gpu_ptr);
#include "kernels/kernel_memory.h"
#endif

namespace search {

// -------------------------------------------------------
// --- Implementation of core data structure functions ---
// -------------------------------------------------------
Expand Down
10 changes: 3 additions & 7 deletions src/kbmod/search/trajectory_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@
#include <parallel/algorithm>
#include <algorithm>

namespace search {

// Declaration of CUDA functions that will be linked in.
#ifdef HAVE_CUDA
extern "C" Trajectory *allocate_gpu_trajectory_list(long unsigned num_trj);
#include "kernels/kernel_memory.h"
#endif

extern "C" void free_gpu_trajectory_list(Trajectory *gpu_ptr);

extern "C" void copy_trajectory_list(Trajectory *cpu_ptr, Trajectory *gpu_ptr, long unsigned num_trj,
bool to_gpu);
#endif
namespace search {

// -------------------------------------------------------
// --- Implementation of core data structure functions ---
Expand Down

0 comments on commit c134561

Please sign in to comment.