Skip to content

Commit

Permalink
Removed cuda_ml_optimiser from gpu_utils
Browse files Browse the repository at this point in the history
  • Loading branch information
jahooker committed Jan 17, 2023
1 parent 1dc1354 commit 325b529
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 3,823 deletions.
232 changes: 124 additions & 108 deletions src/acc/cuda/cuda_ml_optimiser.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,132 +15,148 @@
#include "src/acc/acc_ml_optimiser.h"
#include "src/acc/acc_ptr.h"

class MlDeviceBundle
{
public:
//#ifdef DEBUG_CUDA
//#define HANDLE_CUFFT_ERROR( err ) (CufftHandleError( err, __FILE__, __LINE__ ))
//#else
//#define HANDLE_CUFFT_ERROR( err ) (err) //Do nothing
//#endif
//static void CufftHandleError( cufftResult err, const char *file, int line )
//{
// if (err != CUFFT_SUCCESS)
// {
// fprintf(stderr, "Cufft error in file '%s' in line %i : %s.\n",
// __FILE__, __LINE__, "error" );
// raise(SIGSEGV);
// }
//}

/*
* Bundle of device-objects
*/
class MlDeviceBundle {

public:

//The CUDA accelerated projector set
std::vector< AccProjector > projectors;

//The CUDA accelerated back-projector set
std::vector< AccBackprojector > backprojectors;

//Used for precalculations of projection setup
CudaCustomAllocator *allocator;

//Used for precalculations of projection setup
bool generateProjectionPlanOnTheFly;
std::vector<AccProjectorPlan> coarseProjectionPlans;

MlOptimiser *baseMLO;

int device_id;

int rank_shared_count;

bool haveWarnedRefinementMem;

MlDeviceBundle(MlOptimiser *baseMLOptimiser):
baseMLO(baseMLOptimiser),
generateProjectionPlanOnTheFly(false),
rank_shared_count(1),
device_id(-1),
haveWarnedRefinementMem(false),
allocator(NULL)
{};

void setDevice(int did) {
device_id = did;
}

size_t checkFixedSizedObjects(int shares);
void setupFixedSizedObjects();
void setupTunableSizedObjects(size_t allocationSize);

void syncAllBackprojects() {
DEBUG_HANDLE_ERROR(cudaDeviceSynchronize());
}

~MlDeviceBundle() {
projectors.clear();
backprojectors.clear();
coarseProjectionPlans.clear();
// Delete this lastly
delete allocator;
HANDLE_ERROR(cudaSetDevice(device_id));
HANDLE_ERROR(cudaDeviceReset());
}

//The CUDA accelerated projector set
std::vector< AccProjector > projectors;

//The CUDA accelerated back-projector set
std::vector< AccBackprojector > backprojectors;

//Used for precalculations of projection setup
CudaCustomAllocator *allocator;

//Used for precalculations of projection setup
bool generateProjectionPlanOnTheFly;
std::vector<AccProjectorPlan> coarseProjectionPlans;

MlOptimiser *baseMLO;

int device_id;

int rank_shared_count;

bool haveWarnedRefinementMem;

MlDeviceBundle(MlOptimiser *baseMLOptimiser):
baseMLO(baseMLOptimiser),
generateProjectionPlanOnTheFly(false),
rank_shared_count(1),
device_id(-1),
haveWarnedRefinementMem(false),
allocator(NULL)
{};

void setDevice(int did)
{
device_id = did;
}

size_t checkFixedSizedObjects(int shares);
void setupFixedSizedObjects();
void setupTunableSizedObjects(size_t allocationSize);
};

void syncAllBackprojects()
{
DEBUG_HANDLE_ERROR(cudaDeviceSynchronize());
}
class MlOptimiserCuda {

public:

~MlDeviceBundle()
{
projectors.clear();
backprojectors.clear();
coarseProjectionPlans.clear();
//Delete this lastly
delete allocator;
HANDLE_ERROR(cudaSetDevice(device_id));
HANDLE_ERROR(cudaDeviceReset());
}

};
class MlOptimiserCuda
{
public:
// transformer as holder for reuse of fftw_plans
FourierTransformer transformer;
// transformer as holder for reuse of fftw_plans
FourierTransformer transformer;

//Class streams ( for concurrent scheduling of class-specific kernels)
std::vector<cudaStream_t> classStreams;
cudaError_t errorStatus;
std::vector<cudaStream_t> classStreams;
cudaError_t errorStatus;

CudaFFT transformer1;
CudaFFT transformer2;
CudaFFT transformer1;
CudaFFT transformer2;

MlOptimiser *baseMLO;
MlOptimiser *baseMLO;

bool refIs3D;
bool dataIs3D;
bool refIs3D;
bool dataIs3D;

int device_id;
int device_id;

MlDeviceBundle *bundle;
MlDeviceBundle *bundle;

//Used for precalculations of projection setup
CudaCustomAllocator *allocator;
//Used for precalculations of projection setup
CudaCustomAllocator *allocator;

//Used for precalculations of projection setup
bool generateProjectionPlanOnTheFly;
//Used for precalculations of projection setup
bool generateProjectionPlanOnTheFly;


#ifdef TIMING_FILES
relion_timer timer;
relion_timer timer;
#endif

MlOptimiserCuda(MlOptimiser *baseMLOptimiser, MlDeviceBundle* bundle, const char * timing_fnm) :
baseMLO(baseMLOptimiser),
transformer1(cudaStreamPerThread, bundle->allocator, baseMLOptimiser->mymodel.data_dim),
transformer2(cudaStreamPerThread, bundle->allocator, baseMLOptimiser->mymodel.data_dim),
refIs3D(baseMLO->mymodel.ref_dim == 3),
dataIs3D(baseMLO->mymodel.data_dim == 3),
bundle(bundle),
device_id(bundle->device_id),
#ifdef TIMING_FILES
timer(timing_fnm),
#endif
errorStatus((cudaError_t)0),
allocator(bundle->allocator),
generateProjectionPlanOnTheFly(bundle->generateProjectionPlanOnTheFly)
{};

void resetData();

void doThreadExpectationSomeParticles(int thread_id);

~MlOptimiserCuda()
{
for (int i = 0; i < classStreams.size(); i++)
if (classStreams[i] != NULL)
HANDLE_ERROR(cudaStreamDestroy(classStreams[i]));
}

CudaCustomAllocator *getAllocator()
{
return (bundle->allocator);
};
MlOptimiserCuda(MlOptimiser *baseMLOptimiser, MlDeviceBundle* bundle, const char * timing_fnm) :
baseMLO(baseMLOptimiser),
transformer1(cudaStreamPerThread, bundle->allocator, baseMLOptimiser->mymodel.data_dim),
transformer2(cudaStreamPerThread, bundle->allocator, baseMLOptimiser->mymodel.data_dim),
refIs3D(baseMLO->mymodel.ref_dim == 3),
dataIs3D(baseMLO->mymodel.data_dim == 3),
bundle(bundle),
device_id(bundle->device_id),
#ifdef TIMING_FILES
timer(timing_fnm),
#endif
errorStatus((cudaError_t)0),
allocator(bundle->allocator),
generateProjectionPlanOnTheFly(bundle->generateProjectionPlanOnTheFly)
{};

void resetData();

void doThreadExpectationSomeParticles(int thread_id);

~MlOptimiserCuda()
{
for (int i = 0; i < classStreams.size(); i++)
if (classStreams[i] != NULL)
HANDLE_ERROR(cudaStreamDestroy(classStreams[i]));
}

CudaCustomAllocator *getAllocator()
{
return (bundle->allocator);
};
};

#endif
Loading

0 comments on commit 325b529

Please sign in to comment.