CudaMem is a C++ library that provides smart pointers for managing memory on CUDA-enabled devices. It includes implementations of both unique and shared pointers, allowing you to efficiently allocate, deallocate, and transfer data between the host and device memory without using cudaMalloc
or cudaFree
.
Tested with:
- CUDA 11.1
- GCC 7.5
To install CudaSmartPointers, follow these steps:
-
Clone the repository:
git clone https://github.com/ahmedaradwan/CudaMem.git
-
Navigate to the project directory:
cd CudaMem
-
Create a build directory:
mkdir build cd build
-
Configure the project with CMake:
cmake ..
-
Build the project:
make
-
Optionally, install the library:
sudo make install
Here's a simple example demonstrating how to use the unique pointer from the CudaSmartPointers library:
#include "CudaMem.h"
#include <memory>
#include <vector>
#include <iostream>
#include <random>
int main(){
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<long> dis(1, 1000000);
std::vector<long> host_indexes;
const int num_elements = 100;
for (int i = 0; i < num_elements; ++i) {
host_indexes.push_back(dis(gen));
}
CudaMem::shared_ptr<long> device_indexes;
device_indexes.copyToDevice(host_indexes);
std::vector<long> host_indexes_2(device_indexes.getSize());
device_indexes.copyToHost(host_indexes_2);
for (const auto& i : host_indexes_2){
std::cout << "Vector element: " << i << std::endl;
}
return 0;
}