#ifndef __DTrajectoryPool__ #define __DTrajectoryPool__ #include #define NTrajectoryBlockCount 3*11 #include "DTrajectory.cu" using namespace std; class DTrajectoryPool { public: DTrajectoryPool(int Nblocks); ~DTrajectoryPool(); int getBlock(void); void returnBlock(int); private: DTrajectoryPool(){} DTrajectory *base_d; bool *available_h; int Nblocks; }; //------------------------------- // DTrajectoryPool (constructor) //------------------------------- DTrajectoryPool::DTrajectoryPool(int Nblocks) { base_d = NULL; available_h = NULL; this->Nblocks = Nblocks; int size = Nblocks * NTrajectoryBlockCount * sizeof(DTrajectory); cudaError_t err = cudaMalloc((void **) &base_d, size); if(!cudaSuccess) { cerr << "Error allocating CUDA memory for pool with size " << size << endl; exit(-1); } available_h = new bool[Nblocks]; for(int i = 0; i < Nblocks; i++) { available_h[i] = true; } } //------------------------------- // ~DTrajectoryPool (destructor) //------------------------------- DTrajectoryPool::~DTrajectoryPool() { if(base_d != NULL) cudaFree(base_d); if(available_h!= NULL) delete[] available_h; } //------------------------------- // getBlock //------------------------------- int DTrajectoryPool::getBlock(void) { for(int i = 0; i < Nblocks; i++) { if(available_h[i]) { available_h[i] = false; return i; } } throw int (-1); } //------------------------------- // returnBlock //------------------------------- void DTrajectoryPool::returnBlock(int N) { if(N<0 || N>=Nblocks){ cerr << "Returning a trajectory block outside of range with N " << N << endl; throw int (-1); } available_h[N] = true; } #endif