[cuda-battery] Programming in CUDA with the C++ standard library

Hello folks,

Last June, I gave an introduction to CUDA programming at the HPC summer school, available here. I have developed a library, named cuda-battery that can help you to program more easily in CUDA. The main idea is to get the C++ standard library accessible inside kernels, so you can use something like vector<int> in the kernel instead of a C-like array int*.

I also wrote a tutorial to this library which should be considered as a follow-up to the HPC school tutorial. It is available here.

Not exactly a challenge, but can be useful to solve some challenges maybe :wink:

Best,
Pierre Talbot

Example

Quick example to transfer a std::vector on CPU to a battery::vector on GPU (notice you don’t need to do any manual memory allocation or deallocation):

#include <vector>
#include "battery/vector.hpp"
#include "battery/unique_ptr.hpp"
#include "battery/allocator.hpp"

using mvector = battery::vector<int, battery::managed_allocator>;

__global__ void kernel(mvector* v_ptr) {
  mvector& v = *v_ptr;
  // ... Compute on `v` in parallel.
}

int main(int argc, char** argv) {
  std::vector<int> v(10000, 42);
  // Transfer from CPU vector to GPU vector.
  auto gpu_v = battery::make_unique<mvector, battery::managed_allocator>(v);
  kernel<<<256, 256>>>(gpu_v.get());
  CUDAEX(cudaDeviceSynchronize());
  // Transfering the new data to the initial vector.
  for(int i = 0; i < v.size(); ++i) {
    v[i] = (*gpu_v)[i];
  }
  return 0;
}

Hello Pierre,

Thanks for posting cuda-battery.

To make it even more accessible for users, would you consider providing a code example that demonstrates how cuda-battery can simplify a particular CUDA code? A well-chosen example often speaks louder than pages of documentation.

Regards,

Pierrick

Alright! I’ve updated the first post with a simple example, what do you think?

1 Like