AmpTools
GPUManager.h
Go to the documentation of this file.
1 //******************************************************************************
2 // This file is part of AmpTools, a package for performing Amplitude Analysis
3 //
4 // Copyright Trustees of Indiana University 2010, all rights reserved
5 //
6 // This software written by Matthew Shepherd, Ryan Mitchell, and
7 // Hrayr Matevosyan at Indiana University, Bloomington
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions
11 // are met:
12 // 1. Redistributions of source code must retain the above copyright
13 // notice and author attribution, this list of conditions and the
14 // following disclaimer.
15 // 2. Redistributions in binary form must reproduce the above copyright
16 // notice and author attribution, this list of conditions and the
17 // following disclaimer in the documentation and/or other materials
18 // provided with the distribution.
19 // 3. Neither the name of the University nor the names of its contributors
20 // may be used to endorse or promote products derived from this software
21 // without specific prior written permission.
22 //
23 // Creation of derivative forms of this software for commercial
24 // utilization may be subject to restriction; written permission may be
25 // obtained from the Trustees of Indiana University.
26 //
27 // INDIANA UNIVERSITY AND THE AUTHORS MAKE NO REPRESENTATIONS OR WARRANTIES,
28 // EXPRESS OR IMPLIED. By way of example, but not limitation, INDIANA
29 // UNIVERSITY MAKES NO REPRESENTATIONS OR WARRANTIES OF MERCANTABILITY OR
30 // FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF THIS SOFTWARE OR
31 // DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS,
32 // OR OTHER RIGHTS. Neither Indiana University nor the authors shall be
33 // held liable for any liability with respect to any claim by the user or
34 // any other party arising from use of the program.
35 //******************************************************************************
36 
37 #ifndef __GPU_MANAGER_H__
38 #define __GPU_MANAGER_H__
39 
40 #include <vector>
41 #include <complex>
42 #include <map>
43 #include <cassert>
44 
45 #include <stdlib.h>
46 #include <stdio.h>
47 
49 
50 #include "cuda_runtime.h"
51 
52 #define gpuErrChk(ans) { gpuAssert((ans), __FILE__, __LINE__); }
53 
54 inline void gpuAssert(cudaError_t code, const char *file, int line, bool abort=true)
55 {
56  if (code != cudaSuccess)
57  {
58  fprintf(stderr,"GPU ERROR: %s %s %d\n", cudaGetErrorString(code), file, line);
59  if (abort) exit(code);
60  }
61 }
62 
63 using namespace std;
64 // using namespace __gnu_cxx;
65 
66 class Kinematics;
67 class AmplitudeManager;
68 class AmpVecs;
69 class Amplitude;
70 
72 {
73 
74 public:
75 
76  GPUManager();
77  GPUManager( const AmpVecs& a );
78  ~GPUManager();
79 
80  void clearAll();
81  void clearAmpCalc();
82  void clearLikeCalc();
83 
84  void init( const AmpVecs& a );
85 
86  // Interface Utils
87  // First Amplitude calculation interface
88  void copyDataToGPU( const AmpVecs& a );
89 
90  void calcAmplitudeAll( const Amplitude* amp, unsigned long long offset,
91  const vector< vector< int > >* pvPermutations );
92 
93  void assembleTerms( int iAmpInd, int nFact, int nPerm );
94 
95  void copyAmpsFromGPU( AmpVecs& a );
96 
97  // Now the intensity calculator
98  double calcSumLogIntensity( const vector< complex< double > >& prodCoef,
99  const vector< vector< bool > >& cohMtx );
100 
101  void calcIntegral( GDouble* result, int iAmp, int jAmp, int iNGenEvents );
102 
103  // General utils:
104  static int calcNEventsGPU( int iNEvents ){
105 
106  //Should be a power of 2 for reduction to work, also multiple of GPU_BLOCK_SIZE_SQ
107  int iPow = 0;
108  while( ( 1 << iPow ) < iNEvents ) iPow++;
109  return 1 << iPow;
110  }
111 
112 private:
113 
114  static bool m_cudaDisplay;
115 
116  // array dimensions
117  unsigned int m_iNParticles;
118  unsigned long long m_iNEvents;
119  unsigned long long m_iNTrueEvents;
120  unsigned int m_iNAmps;
121 
122  // array sizes
123  unsigned long long m_iEventArrSize;
124  unsigned long long m_iTrueEventArrSize;
125  unsigned long long m_iAmpArrSize;
126  unsigned int m_iVArrSize;
127 
128  //Host Arrays
129  GDouble* m_pcCalcAmp;
130 
131  GDouble* m_pfVVStar;
132  GDouble* m_pfRes;
133 
134  //Device Arrays
135  GDouble* m_pfDevData;
136  GDouble* m_pfDevWeights;
137  GDouble* m_pcDevCalcAmp;
138  int* m_piDevPerm;
139 
140  GDouble* m_pfDevAmps;
141  GDouble* m_pfDevVVStar;
142 
143  GDouble* m_pfDevResRe;
144  GDouble* m_pfDevResIm;
145  GDouble* m_pfDevREDUCE;
146 
147  // CUDA Thread and Grid sizes
148  unsigned int m_iDimGridX;
149  unsigned int m_iDimGridY;
150  unsigned int m_iDimThreadX;
151  unsigned int m_iDimThreadY;
152 
153  unsigned int m_iNBlocks;
154  unsigned int m_iNThreads;
155 
156  // Internal Utils
157 
158  unsigned int m_devProp_major;
159 
160  void calcCUDADims();
161 
162 };
163 
164 #endif //__GPU_MANAGER_H__
double GDouble
void gpuAssert(cudaError_t code, const char *file, int line, bool abort=true)
Definition: GPUManager.h:54
static int calcNEventsGPU(int iNEvents)
Definition: GPUManager.h:104