DKSOPAL.cpp 8.64 KB
Newer Older
1 2 3 4 5 6 7 8
#include "DKSOPAL.h"

DKSOPAL::DKSOPAL() {
  dksfft = nullptr;
  dkscol = nullptr;
  dksgreens = nullptr;
}

9 10 11 12 13
DKSOPAL::DKSOPAL(const char* api_name, const char* device_name) {
  setAPI(api_name, strlen(api_name));
  setDevice(device_name, strlen(device_name));
}

14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
DKSOPAL::~DKSOPAL() {
  delete dksfft;
  delete dkscol;
  delete dksgreens;
}

int DKSOPAL::setupOPAL() {
  int ierr = DKS_ERROR;
  if (apiOpenCL()) {
    ierr = OPENCL_SAFECALL( DKS_SUCCESS );
    //TODO: only enable if AMD libraries are available
    dksfft = OPENCL_SAFEINIT_AMD( new OpenCLFFT(getOpenCLBase()) );
    dkscol = OPENCL_SAFEINIT_AMD( new OpenCLCollimatorPhysics(getOpenCLBase()) );
    dksgreens = OPENCL_SAFEINIT_AMD( new OpenCLGreensFunction(getOpenCLBase()) );
  } else if (apiCuda()) {
    ierr = CUDA_SAFECALL( DKS_SUCCESS );
    dksfft = CUDA_SAFEINIT( new CudaFFT(getCudaBase()) );
    dkscol = CUDA_SAFEINIT( new CudaCollimatorPhysics(getCudaBase()) );
    dksgreens = CUDA_SAFEINIT( new CudaGreensFunction(getCudaBase()) );
  } else if (apiOpenMP()) {
    ierr = MIC_SAFECALL( DKS_SUCCESS );
    dksfft = MIC_SAFEINIT( new MICFFT(getMICBase()) );
    dkscol = MIC_SAFEINIT( new MICCollimatorPhysics(getMICBase()) );
    dksgreens = MIC_SAFEINIT( new MICGreensFunction(getMICBase()) );
  } else {
    ierr = DKS_ERROR;
  }

  return ierr;
}

int DKSOPAL::initDevice() {
  int ierr = setupDevice();
47
  if (ierr == DKS_SUCCESS)
48 49
    ierr = setupOPAL();
  return ierr;
50

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
}

/* setup fft plans to reuse if multiple ffts of same size are needed */
int DKSOPAL::setupFFT(int ndim, int N[3]) {

  if (apiCuda()) {
    return dksfft->setupFFT(ndim, N);
  } else if (apiOpenCL()) {
    int ierr1 = dksfft->setupFFT(ndim, N);
    int ierr2 = dksfft->setupFFTRC(ndim, N);
    int ierr3 = dksfft->setupFFTCR(ndim, N);
    if (ierr1 != DKS_SUCCESS || ierr2 != DKS_SUCCESS || ierr3 != DKS_SUCCESS)
      return DKS_ERROR;

    return DKS_SUCCESS;
  } else if (apiOpenMP()) {
    //micbase.mic_setupFFT(ndim, N);
    //BENI: setting up RC and CR transformations on MIC
    int ierr1 = dksfft->setupFFTRC(ndim, N, 1.);
    int ierr2 = dksfft->setupFFTCR(ndim, N, 1./(N[0]*N[1]*N[2]));
    if (ierr1 != DKS_SUCCESS)
      return ierr1;
    if (ierr2 != DKS_SUCCESS)
      return ierr2;
    return DKS_SUCCESS;
  }

  return DKS_ERROR;

}
//BENI:
int DKSOPAL::setupFFTRC(int ndim, int N[3], double scale) {

  if (apiCuda())
    return dksfft->setupFFT(ndim, N);
  if (apiOpenCL())
    return dksfft->setupFFTRC(ndim, N);
  else if (apiOpenMP())
    return dksfft->setupFFTRC(ndim, N, scale);

  return DKS_ERROR;

}

//BENI:
int DKSOPAL::setupFFTCR(int ndim, int N[3], double scale) {

  if (apiCuda())
    return dksfft->setupFFT(ndim, N);
  if (apiOpenCL())
    return dksfft->setupFFTCR(ndim, N);
  else if (apiOpenMP())
    return dksfft->setupFFTCR(ndim, N, scale);

  return DKS_ERROR;

}

/* call OpenCL FFT function for selected platform */
int DKSOPAL::callFFT(void * data_ptr, int ndim, int dimsize[3], int streamId) {

  if (apiOpenCL() || apiOpenMP()) 
    return dksfft->executeFFT(data_ptr, ndim, dimsize);
  else if (apiCuda())
    return dksfft->executeFFT(data_ptr, ndim, dimsize, streamId);

  DEBUG_MSG("No implementation for selected platform");
  return DKS_ERROR;
}

/* call OpenCL IFFT function for selected platform */
int DKSOPAL::callIFFT(void * data_ptr, int ndim, int dimsize[3], int streamId) {
  if (apiOpenCL() || apiOpenMP())
      return dksfft->executeIFFT(data_ptr, ndim, dimsize);
  else if (apiCuda()) 
    return dksfft->executeIFFT(data_ptr, ndim, dimsize, streamId);

  DEBUG_MSG("No implementation for selected platform");
  return DKS_ERROR;
}

/* call normalize FFT function for selected platform */
int DKSOPAL::callNormalizeFFT(void * data_ptr, int ndim, int dimsize[3], int streamId) {

  if (apiOpenCL()) {
    if ( loadOpenCLKernel("OpenCL/OpenCLKernels/OpenCLFFT.cl") == DKS_SUCCESS )
      return dksfft->normalizeFFT(data_ptr, ndim, dimsize);
    else 
      return DKS_ERROR;
  } else if (apiCuda()) {
    return dksfft->normalizeFFT(data_ptr, ndim, dimsize, streamId);
  } else if (apiOpenMP()) {
    return dksfft->normalizeFFT(data_ptr, ndim, dimsize);
  }

  DEBUG_MSG("No implementation for selected platform");
  return DKS_ERROR;
}

/* call real to complex FFT */
int DKSOPAL::callR2CFFT(void * real_ptr, void * comp_ptr, int ndim, int dimsize[3], int streamId) {

  if (apiCuda())
    return dksfft->executeRCFFT(real_ptr, comp_ptr, ndim, dimsize, streamId);
  else if (apiOpenCL() || apiOpenMP())
    return dksfft->executeRCFFT(real_ptr, comp_ptr, ndim, dimsize);

  DEBUG_MSG("No implementation for selected platform");
  return DKS_ERROR;
}

/* call complex to real FFT */
int DKSOPAL::callC2RFFT(void * real_ptr, void * comp_ptr, int ndim, int dimsize[3], int streamId) {
  if (apiCuda())
    return dksfft->executeCRFFT(real_ptr, comp_ptr, ndim, dimsize, streamId);
  else if (apiOpenCL() || apiOpenMP())
    return dksfft->executeCRFFT(real_ptr, comp_ptr, ndim, dimsize);

  DEBUG_MSG("No implementation for selected platform");
  return DKS_ERROR;
}

/* normalize complex to real iFFT */
int DKSOPAL::callNormalizeC2RFFT(void * real_ptr, int ndim, int dimsize[3], int streamId) {
  if (apiCuda())
    return dksfft->normalizeCRFFT(real_ptr, ndim, dimsize, streamId);
  else if (apiOpenCL())
    return DKS_ERROR;
  else if (apiOpenMP())
    return DKS_ERROR;

  DEBUG_MSG("No implementation for selected platform");
  return DKS_ERROR;
}

int DKSOPAL::callGreensIntegral(void *tmp_ptr, int I, int J, int K, int NI, int NJ, 
				double hz_m0, double hz_m1, double hz_m2, int streamId) {

    return dksgreens->greensIntegral(tmp_ptr, I, J, K, NI, NJ, 
				     hz_m0, hz_m1, hz_m2, streamId);

}

int DKSOPAL::callGreensIntegration(void *mem_ptr, void *tmp_ptr, 
				   int I, int J, int K, int streamId) {

  return dksgreens->integrationGreensFunction(mem_ptr, tmp_ptr, I, J, K, streamId);
}

int DKSOPAL::callMirrorRhoField(void *mem_ptr, int I, int J, int K, int streamId) {

  return dksgreens->mirrorRhoField(mem_ptr, I, J, K, streamId);  
}

int DKSOPAL::callMultiplyComplexFields(void *mem_ptr1, void *mem_ptr2, int size, int streamId) {
  
  return dksgreens->multiplyCompelxFields(mem_ptr1, mem_ptr2, size, streamId);
}

int DKSOPAL::callCollimatorPhysics(void *mem_ptr, void *par_ptr, 
				   int numparticles, int numparams,
				   int &numaddback, int &numdead) 
{

  return dkscol->CollimatorPhysics(mem_ptr, par_ptr, numparticles);

}


int DKSOPAL::callCollimatorPhysics2(void *mem_ptr, void *par_ptr, int numparticles) 
{

  return dkscol->CollimatorPhysics(mem_ptr, par_ptr, numparticles);
  
}

int DKSOPAL::callCollimatorPhysicsSoA(void *label_ptr, void *localID_ptr, 
				      void *rx_ptr, void *ry_ptr, void *rz_ptr, 
				      void *px_ptr, void *py_ptr, void *pz_ptr,
				      void *par_ptr, int numparticles)
{

  
    return dkscol->CollimatorPhysicsSoA(label_ptr, localID_ptr, 
					rx_ptr, ry_ptr, rz_ptr, 
					px_ptr, py_ptr, pz_ptr,
					par_ptr,  numparticles);

}


int DKSOPAL::callCollimatorPhysicsSort(void *mem_ptr, int numparticles, int &numaddback) 
{

  return dkscol->CollimatorPhysicsSort(mem_ptr, numparticles, numaddback);

}

int DKSOPAL::callCollimatorPhysicsSortSoA(void *label_ptr, void *localID_ptr, 
					  void *rx_ptr, void *ry_ptr, void *rz_ptr, 
					  void *px_ptr, void *py_ptr, void *pz_ptr,
					  void *par_ptr, int numparticles, int &numaddback) 
{

  return MIC_SAFECALL(dkscol->CollimatorPhysicsSortSoA(label_ptr, localID_ptr, 
						       rx_ptr, ry_ptr, rz_ptr, 
						       px_ptr, py_ptr, pz_ptr,
						       par_ptr,  numparticles, numaddback));

}


int DKSOPAL::callParallelTTrackerPush(void *r_ptr, void *p_ptr, int npart, 
				      void *dt_ptr, double dt, double c, 
				      bool usedt, int streamId) 
{

  return dkscol->ParallelTTrackerPush(r_ptr, p_ptr, npart, dt_ptr, dt, c, usedt, streamId);

}

272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
int DKSOPAL::callParallelTTrackerPush(void *r_ptr, void *p_ptr, void *dt_ptr, 
				      int npart, double c, int streamId) {
  
  return dkscol->ParallelTTrackerPush(r_ptr, p_ptr, npart, dt_ptr, 0, c, true, streamId);

}

int DKSOPAL::callParallelTTrackerKick(void *r_ptr, void *p_ptr, void *ef_ptr,
				      void *bf_ptr, void *dt_ptr, double charge, double mass,
				      int npart, double c, int streamId) 
{
  
  return dkscol->ParallelTTrackerKick(r_ptr, p_ptr, ef_ptr, bf_ptr, dt_ptr, 
				      charge, mass, npart, c, streamId);

}

289 290 291 292 293 294 295 296 297 298
int DKSOPAL::callParallelTTrackerPushTransform(void *x_ptr, void *p_ptr, 
					       void *lastSec_ptr, void *orient_ptr, 
					       int npart, int nsec, void *dt_ptr, double dt, 
					       double c, bool usedt, int streamId)
{

  return dkscol->ParallelTTrackerPushTransform(x_ptr, p_ptr, lastSec_ptr, orient_ptr,
					       npart, nsec, dt_ptr, dt, c, usedt, streamId);
  
}