bioem_cuda_internal.h 6.77 KB
Newer Older
1 2 3 4 5
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   < BioEM software for Bayesian inference of Electron Microscopy images>
   Copyright (C) 2016 Pilar Cossio, David Rohr, Fabio Baruffa, Markus Rampp, 
        Volker Lindenstruth and Gerhard Hummer.
   Max Planck Institute of Biophysics, Frankfurt, Germany.
Pilar Cossio's avatar
Pilar Cossio committed
6 7
   Frankfurt Institute for Advanced Studies, Goethe University Frankfurt, Germany.
   Max Planck Computing and Data Facility, Garching, Germany. 
8

Pilar Cossio's avatar
Pilar Cossio committed
9
   Released under the GNU Public License, v3. 
10 11 12 13
   See license statement for terms of distribution.

   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

qon's avatar
qon committed
14 15 16 17
#ifndef BIOEM_CUDA_INTERNAL_H
#define BIOEM_CUDA_INTERNAL_H

#include <cuda.h>
18
#include <cufft.h>
qon's avatar
qon committed
19

20 21 22 23 24
//Hack to make nvcc compiler accept fftw.h, float128 is not used anyway
#define __float128 double
#include <fftw3.h>
#undef __float128

qon's avatar
qon committed
25 26 27 28 29
#include "bioem_cuda.h"

class bioem_cuda : public bioem
{
public:
30 31 32
	bioem_cuda();
	virtual ~bioem_cuda();

33
	virtual int compareRefMaps(int iOrient, int iConv, myfloat_t amp, myfloat_t pha, myfloat_t env, const myfloat_t* conv_map, mycomplex_t* localmultFFT, myfloat_t sumC, myfloat_t sumsquareC, const int startMap = 0);
34 35
	virtual void* malloc_device_host(size_t size);
	virtual void free_device_host(void* ptr);
36

qon's avatar
qon committed
37 38 39 40 41
protected:
	virtual int deviceInit();
	virtual int deviceStartRun();
	virtual int deviceFinishRun();
	int deviceExit();
David Rohr's avatar
David Rohr committed
42 43 44
	
private:
	int selectCudaDevice();
45

qon's avatar
qon committed
46
	int deviceInitialized;
47

48 49
	cudaStream_t cudaStream[3];
	cudaEvent_t cudaEvent[3];
50
	cudaEvent_t cudaFFTEvent[2];
qon's avatar
qon committed
51
	bioem_RefMap_Mod* pRefMap_device_Mod;
52
	bioem_RefMap* gpumap;
David Rohr's avatar
David Rohr committed
53
	bioem_Probability* pProb_host;
54
	bioem_Probability pProb_device;
David Rohr's avatar
David Rohr committed
55
	void* pProb_memory;
56
	myfloat_t* pConvMap_device[2];
57

58 59
	mycomplex_t* pRefMapsFFT;
	mycomplex_t* pConvMapFFT;
60
	mycomplex_t* pConvMapFFT_Host;
61 62 63
	mycuComplex_t* pFFTtmp2[2];
	myfloat_t* pFFTtmp[2];
	cufftHandle plan[2][2];
64

65 66
	myfloat_t *maps, *sum, *sumsquare;

67 68
	int GPUAlgo;		//GPU Algorithm to use, 0: parallelize over maps, 1: as 0 but work split in multiple kernels (better), 2: also parallelize over shifts (best)
	int GPUAsync;		//Run GPU Asynchronously, do the convolutions on the host in parallel.
69
	int GPUDualStream;	//Use two streams to improve paralelism
70
	int GPUWorkload;	//Percentage of workload to perform on GPU. Default 100. Rest is done on processor in parallel.
71

72
	int maxRef;
qon's avatar
qon committed
73 74
};

Luka Stanisic's avatar
Luka Stanisic committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
/* Handing CUDA Driver errors */
/* Inspired from: https://github.com/garymacindoe/cuda-cholesky */

// Expand and stringify argument
#define STRINGx(x) #x
#define STRING(x) STRINGx(x)

const char * cuGetError(CUresult result) {
  switch (result) {
    case CUDA_SUCCESS:                              return "No errors";
    case CUDA_ERROR_INVALID_VALUE:                  return "Invalid value";
    case CUDA_ERROR_OUT_OF_MEMORY:                  return "Out of memory";
    case CUDA_ERROR_NOT_INITIALIZED:                return "Driver not initialized";
    case CUDA_ERROR_DEINITIALIZED:                  return "Driver deinitialized";
    case CUDA_ERROR_PROFILER_DISABLED:              return "Profiler disabled";
    case CUDA_ERROR_PROFILER_NOT_INITIALIZED:       return "Profiler not initialized";
    case CUDA_ERROR_PROFILER_ALREADY_STARTED:       return "Profiler already started";
    case CUDA_ERROR_PROFILER_ALREADY_STOPPED:       return "Profiler already stopped";
    case CUDA_ERROR_NO_DEVICE:                      return "No CUDA-capable device available";
    case CUDA_ERROR_INVALID_DEVICE:                 return "Invalid device";
    case CUDA_ERROR_INVALID_IMAGE:                  return "Invalid kernel image";
    case CUDA_ERROR_INVALID_CONTEXT:                return "Invalid context";
    case CUDA_ERROR_CONTEXT_ALREADY_CURRENT:        return "Context already current";
    case CUDA_ERROR_MAP_FAILED:                     return "Map failed";
    case CUDA_ERROR_UNMAP_FAILED:                   return "Unmap failed";
    case CUDA_ERROR_ARRAY_IS_MAPPED:                return "Array is mapped";
    case CUDA_ERROR_ALREADY_MAPPED:                 return "Already mapped";
    case CUDA_ERROR_NO_BINARY_FOR_GPU:              return "No binary for GPU";
    case CUDA_ERROR_ALREADY_ACQUIRED:               return "Already acquired";
    case CUDA_ERROR_NOT_MAPPED:                     return "Not mapped";
    case CUDA_ERROR_NOT_MAPPED_AS_ARRAY:            return "Not mapped as array";
    case CUDA_ERROR_NOT_MAPPED_AS_POINTER:          return "Not mapped as pointer";
    case CUDA_ERROR_ECC_UNCORRECTABLE:              return "Uncorrectable ECC error";
    case CUDA_ERROR_UNSUPPORTED_LIMIT:              return "Unsupported CUlimit";
    case CUDA_ERROR_CONTEXT_ALREADY_IN_USE:         return "Context already in use";
    case CUDA_ERROR_INVALID_SOURCE:                 return "Invalid source";
    case CUDA_ERROR_FILE_NOT_FOUND:                 return "File not found";
    case CUDA_ERROR_SHARED_OBJECT_SYMBOL_NOT_FOUND: return "Shared object symbol not found";
    case CUDA_ERROR_SHARED_OBJECT_INIT_FAILED:      return "Shared object initialization failed";
    case CUDA_ERROR_OPERATING_SYSTEM:               return "Operating System call failed";
    case CUDA_ERROR_INVALID_HANDLE:                 return "Invalid handle";
    case CUDA_ERROR_NOT_FOUND:                      return "Not found";
    case CUDA_ERROR_NOT_READY:                      return "CUDA not ready";
    case CUDA_ERROR_LAUNCH_FAILED:                  return "Launch failed";
    case CUDA_ERROR_LAUNCH_OUT_OF_RESOURCES:        return "Launch exceeded resources";
    case CUDA_ERROR_LAUNCH_TIMEOUT:                 return "Launch exceeded timeout";
    case CUDA_ERROR_LAUNCH_INCOMPATIBLE_TEXTURING:  return "Launch with incompatible texturing";
    case CUDA_ERROR_PEER_ACCESS_ALREADY_ENABLED:    return "Peer access already enabled";
    case CUDA_ERROR_PEER_ACCESS_NOT_ENABLED:        return "Peer access not enabled";
    case CUDA_ERROR_PRIMARY_CONTEXT_ACTIVE:         return "Primary context active";
    case CUDA_ERROR_CONTEXT_IS_DESTROYED:           return "Context is destroyed";
    case CUDA_ERROR_ASSERT:                         return "Device assert failed";
    case CUDA_ERROR_TOO_MANY_PEERS:                 return "Too many peers";
    case CUDA_ERROR_HOST_MEMORY_ALREADY_REGISTERED: return "Host memory already registered";
    case CUDA_ERROR_HOST_MEMORY_NOT_REGISTERED:     return "Host memory not registered";
    case CUDA_ERROR_UNKNOWN:                        return "Unknown error";
    default:                                        return "Unknown error code";
  }
}

#define CU_ERROR_CHECK(call) \
  do { \
    CUresult __error__; \
    if ((__error__ = (call)) != CUDA_SUCCESS) { \
      printf(STRING(call), __func__, __FILE__, __LINE__, __error__, \
                     (const char * (*)(int))cuGetError); \
      return __error__; \
    } \
  } while (false)


qon's avatar
qon committed
146
#endif
147