su2hmc
Loading...
Searching...
No Matches
/github/workspace/Extract_Obs.c
1
15#include <assert.h>
16#include <clover.h>
17#include <matrices.h>
18#ifdef USE_GPU
19#include <cuda_runtime.h>
20cublasHandle_t cublas_handle;
21cublasStatus_t cublas_status;
22cudaMemPool_t mempool;
23//Fix this later
24#endif
25
26int main(int argc, char *argv[]){
27 const char funcname[] = "main";
28 //Prevent any issues with MPI being initialised needlessly in Par_begin
29#if(nproc>1)
30#error nproc>1 in sizes.h. Ensure npx=npy=npt=npt=1, and then compile.
31#endif
32 //Lattice layout setup.
33 Par_begin(argc,argv);
34#ifdef USE_GPU
35 //CUBLAS Handle
36 cublasCreate(&cublas_handle);
37 //Set up grid and blocks
39 //CUDA device
40 int device=-1;
41 cudaGetDevice(&device);
42 //For asynchronous memory, when CUDA syncs any unused memory in the pool is released back to the OS
43 //unless a threshold is given. We'll base our threshold off of Congradp
44 //12*kvol for the clover and 4*16*kvolHalo for the fermion fields
45 //Factor of 1.5 because we need it in single and double precsion should be plenty without being excessive.
46 //Not everything has a halo so larger halos give us more headroom too.
47 cudaDeviceGetDefaultMemPool(&mempool, device);
48 int threshold=8*kfermHalo*sizeof(Complex);
49 cudaMemPoolSetAttribute(mempool, cudaMemPoolAttrReleaseThreshold, &threshold);
50#endif
51 FILE *midout;
52 const char *filename = (argc!=2) ?"midout":argv[1];
53 char *fileop = "r";
54 if( !(midout = fopen(filename, fileop) ) ){
55 fprintf(stderr, "Error %i in %s: Failed to open file %s for %s.\nExiting\n\n",\
56 OPENERROR, funcname, filename, fileop);
57 exit(OPENERROR);
58 }
59
60 float dt=0.004;//HMC Step size to keep the existing parser happy
61 float beta = 1.7f; //Inverse gauge coupling
62 float kappa = 0.1780f;//Hopping parameter
63 float jqq = 0.0;//Diquark source
64 float c_sw = 0.0;//Clover coefficient
65 float mu = 0.0f; //Chemical potential
66 float delb=0; //Legacy. Not used
67 int istart = 1;//Start type (hot or cold). Just to keep the existing parser happy
68 int icheck = 5; //How often are configurations saved (or read in this case)
69 int iread = 0; //First config number to read in
70 int stepl = 250;//HMC Step length
71 int ntraj = 10;//Number of trajectories (NOT CONFIGS).
72 fscanf(midout, "%f %f %f %f %f %f %f %d %d %d %d %d", &dt, &beta, &kappa,\
73 &jqq, &c_sw, &mu, &delb, &stepl, &ntraj, &istart, &icheck, &iread);
74 fclose(midout);
75 assert(stepl>0); assert(ntraj>0); assert(istart>=0); assert(icheck>0); assert(iread>=0);
76 //Boundary condition
77 const int ibound = -1;
78 //How many configs to read
79#ifdef _DEBUG
80 printf("Reading %d configs\n",ntraj);
81#endif
82
83 //Gauge, trial and momentum fields
84 //You'll notice that there are two different allocation/free statements
85 //One for CUDA and one for everything else depending on what's
86 //being used
87 /*** Let's take a quick moment to compare this to the analysis code.
88 * The analysis code stores the gauge field as a 4 component real valued vector, whereas the produciton code
89 * used two complex numbers.
90 *
91 * Analysis code: u=(Re(u[0]),Im(u[1]),Re(u[1]),Im(u[0]))
92 * Production code: u[0]=u[0]+I*u[3] u[1]=u[2]+I*u[1]
93 *
94 */
95 Complex *u[2], *ut[2],*Phi;
96 Complex_f *ut_f[2];
97 double *dk[2];
98 float *dk_f[2];
99 //Halo index arrays
100 unsigned int *iu, *id;
101 //And clover arrays. These only get assigned if @f$c_\text{SW}>0@f$
102 Complex *sigval; Complex_f *sigval_f; unsigned short *sigin;
103#ifdef USE_GPU
104 //Managed here because it's easier to fill them on CPU
105 cudaMallocManaged((void**)&iu,ndim*kvol*sizeof(int),cudaMemAttachGlobal);
106 cudaMallocManaged((void**)&id,ndim*kvol*sizeof(int),cudaMemAttachGlobal);
107
108 cudaMallocManaged((void **)&dk[0],(kvolHalo)*sizeof(double),cudaMemAttachGlobal);
109 cudaMallocManaged((void **)&dk[1],(kvolHalo)*sizeof(double),cudaMemAttachGlobal);
110#ifdef _DEBUG
111 cudaMallocManaged((void **)&dk_f[0],(kvolHalo)*sizeof(float),cudaMemAttachGlobal);
112 cudaMallocManaged((void **)&dk_f[1],(kvolHalo)*sizeof(float),cudaMemAttachGlobal);
113#else
114 cudaMalloc((void **)&dk_f[0],(kvolHalo)*sizeof(float));
115 cudaMalloc((void **)&dk_f[1],(kvolHalo)*sizeof(float));
116#endif
117
118 unsigned short *gamin; Complex *gamval; Complex_f *gamval_f;
119 cudaMallocManaged((void **)&gamin,4*4*sizeof(short),cudaMemAttachGlobal);
120 cudaMallocManaged((void **)&gamval,5*4*sizeof(Complex),cudaMemAttachGlobal);
121 cudaMallocManaged((void **)&gamval_f,5*4*sizeof(Complex_f),cudaMemAttachGlobal);
122
123 cudaMallocManaged((void **)&u[0],ndim*kvol*sizeof(Complex),cudaMemAttachGlobal);
124 cudaMallocManaged((void **)&u[1],ndim*kvol*sizeof(Complex),cudaMemAttachGlobal);
125 //Needs to be managed as fermionic.c still used them on CPU
126 cudaMallocManaged((void **)&ut[0],ndim*(kvolHalo)*sizeof(Complex),cudaMemAttachGlobal);
127 cudaMallocManaged((void **)&ut[1],ndim*(kvolHalo)*sizeof(Complex),cudaMemAttachGlobal);
128#ifdef _DEBUG
129 cudaMallocManaged((void **)&ut_f[0],ndim*(kvolHalo)*sizeof(Complex_f),cudaMemAttachGlobal);
130 cudaMallocManaged((void **)&ut_f[1],ndim*(kvolHalo)*sizeof(Complex_f),cudaMemAttachGlobal);
131 cudaMallocManaged((void **)&Phi, nf*kferm*sizeof(Complex),cudaMemAttachGlobal);
132#else
133 cudaMalloc((void **)&ut_f[0],ndim*(kvolHalo)*sizeof(Complex_f));
134 cudaMalloc((void **)&ut_f[1],ndim*(kvolHalo)*sizeof(Complex_f));
135 cudaMalloc((void **)&Phi, nf*kferm*sizeof(Complex));
136#endif
137#else
138 id = (unsigned int*)aligned_alloc(AVX,ndim*kvol*sizeof(int));
139 iu = (unsigned int*)aligned_alloc(AVX,ndim*kvol*sizeof(int));
140
141 alignas(AVX) unsigned short gamin[16]; alignas(AVX) Complex gamval[20]; alignas(AVX) Complex_f gamval_f[20];
142
143 dk[0] = (double *)aligned_alloc(AVX,(kvolHalo)*sizeof(double));
144 dk[1] = (double *)aligned_alloc(AVX,(kvolHalo)*sizeof(double));
145 dk_f[0] = (float *)aligned_alloc(AVX,(kvolHalo)*sizeof(float));
146 dk_f[1] = (float *)aligned_alloc(AVX,(kvolHalo)*sizeof(float));
147
148 u[0] = (Complex *)aligned_alloc(AVX,ndim*kvol*sizeof(Complex));
149 u[1] = (Complex *)aligned_alloc(AVX,ndim*kvol*sizeof(Complex));
150 ut[0] = (Complex *)aligned_alloc(AVX,ndim*(kvolHalo)*sizeof(Complex));
151 ut[1] = (Complex *)aligned_alloc(AVX,ndim*(kvolHalo)*sizeof(Complex));
152 ut_f[0] = (Complex_f *)aligned_alloc(AVX,ndim*(kvolHalo)*sizeof(Complex_f));
153 ut_f[1] = (Complex_f *)aligned_alloc(AVX,ndim*(kvolHalo)*sizeof(Complex_f));
154#endif
155 //Get nearest neigbour addresses
156 Addrc(iu, id);
157 //And confirm they're legit
160 //Free unneccessary arrays from lattice setup.
161 free(hd); free(hu); free(pcoord);
162
176 Init(istart,ibound,iread,beta,mu,kappa,jqq,c_sw,u,ut,ut_f,gamval,gamval_f,gamin,dk,dk_f,iu,id);
177 if(c_sw)
178 Init_clover(&sigval,&sigval_f,&sigin,c_sw);
179
181#ifdef USE_GPU
182 //GPU Initialisation stuff
183 Init_CUDA(ut[0],ut[1],gamval,gamval_f,gamin,dk[0],dk[1],iu,id);//&dimBlock,&dimGrid);
184#endif
185 //Send trials to accelerator for reunitarisation
186 Reunitarise(ut);
187 //Get trials back
188#ifdef USE_GPU
189#if(nproc>1) //Memcpy routines need to be strided if there is a halo since the lattice is not contiguous in memory
190 for(unsigned short mu=0;mu<ndim;mu++){
191 cudaMemcpyAsync(u[0]+kvol*mu, ut[0]+kvolHalo*mu, kvol*sizeof(Complex),cudaMemcpyDefault,streams[mu]);
192 cudaMemcpyAsync(u[1]+kvol*mu, ut[1]+kvolHalo*mu, kvol*sizeof(Complex),cudaMemcpyDefault,streams[mu]);
193 }
194#else
195 cudaMemcpyAsync(u[0], ut[0], ndim*kvol*sizeof(Complex),cudaMemcpyDefault,streams[0]);
196 cudaMemcpyAsync(u[1], ut[1], ndim*kvol*sizeof(Complex),cudaMemcpyDefault,streams[1]);
197#endif
198#else
199 for(unsigned short mu=0;mu<ndim;mu++){
200 memcpy(u[0]+kvol*mu, ut[0]+kvolHalo*mu, kvol*sizeof(Complex));
201 memcpy(u[1]+kvol*mu, ut[1]+kvolHalo*mu, kvol*sizeof(Complex));
202 }
203#endif
204#ifdef USE_GPU
206#endif
207 //Prepare file names
208 char suffix[FILELEN]="";
209 int buffer; char buff2[7];
210 //Add script for extracting correct mu, j etc.
211 buffer = (int)round(100*beta);
212 sprintf(buff2,"b%03d",buffer);
213 strcat(suffix,buff2);
214 //@f$\kappa@f$
215 buffer = (int)round(10000*kappa);
216 sprintf(buff2,"k%04d",buffer);
217 strcat(suffix,buff2);
218 //@f$\mu@f$
219 buffer = (int)round(1000*mu);
220 sprintf(buff2,"mu%04d",buffer);
221 strcat(suffix,buff2);
222 //J
223 buffer = (int)round(1000*jqq);
224 sprintf(buff2,"j%03d",buffer);
225 strcat(suffix,buff2);
226 //c_sw
227 if(c_sw){
228 buffer = (int)round(100*c_sw);
229 sprintf(buff2,"c%03d",buffer);
230 strcat(suffix,buff2);
231 }
232 //nx
233 sprintf(buff2,"s%02d",nx);
234 strcat(suffix,buff2);
235 //nt
236 sprintf(buff2,"t%02d",nt);
237 strcat(suffix,buff2);
238 for(unsigned int i=iread;i<=ntraj;i+=icheck){
239 const unsigned int conf = i;
240
241 printf("Reading config %d\n",conf);
242 Par_sread(conf, beta, mu, kappa, jqq,c_sw,u[0],u[1],ut[0],ut[1]);
243 Reunitarise(ut);
244 Trial_Exchange(ut,ut_f);
245#ifdef USE_GPU
247#endif
248
249 //Polyakov loop
250 double poly = Polyakov(ut_f);
251 double hg, avplaqs, avplaqt;
252 //Plaquettes
253 Average_Plaquette(&hg,&avplaqs,&avplaqt,ut_f,iu,beta);
254 //Fermionic observable setup
255 double pbp=0; Complex qq=0; Complex qbqb=0;
256 double endenf=0, denf=0;
257 //Fun facts
258 int itercg=0;
259 int measure_check=Measure(&pbp,&endenf,&denf,&qq,&qbqb,respbp,&itercg,ut,ut_f,iu,id,\
260 gamval,gamval_f,gamin,sigval,sigval_f,sigin,dk,dk_f,jqq,kappa,c_sw,Phi);
261#pragma omp parallel for
262 for(unsigned short j=0; j<3; j++)
263 switch(j)
264 {
265 case(0):
266 {
267 FILE *fortout;
268 char fortname[FILELEN] = "ext_fermi.";
269 strcat(fortname,suffix);
270 const char *fortop= (i==0) ? "w" : "a";
271 if(!(fortout=fopen(fortname, fortop) )){
272 fprintf(stderr, "Error %i in %s: Failed to open file %s for %s.\nExiting\n\n",\
273 OPENERROR, funcname, fortname, fortop);
274#if(nproc>1)
275 MPI_Abort(comm,OPENERROR);
276#else
277 exit(OPENERROR);
278#endif
279 }
280 if(i==0)
281 fprintf(fortout, "pbp\tendenf\tdenf\n");
282 if(measure_check)
283 fprintf(fortout, "%e\t%e\t%e\n", NAN, NAN, NAN);
284 else
285 fprintf(fortout, "%e\t%e\t%e\n", pbp, endenf, denf);
286 fclose(fortout);
287 break;
288 }
289 case(1):
290 //The original code implicitly created these files with the name
291 //fort.XX where XX is the file label
292 //from FORTRAN. This was fort.12
293 {
294 FILE *fortout;
295 char fortname[FILELEN] = "ext_bose.";
296 strcat(fortname,suffix);
297 const char *fortop= (i==0) ? "w" : "a";
298 if(!(fortout=fopen(fortname, fortop) )){
299 fprintf(stderr, "Error %i in %s: Failed to open file %s for %s.\nExiting\n\n",\
300 OPENERROR, funcname, fortname, fortop);
301 }
302 if(i==0)
303 fprintf(fortout, "avplaqs\tavplaqt\tpoly\n");
304 fprintf(fortout, "%e\t%e\t%e\n", avplaqs, avplaqt, poly);
305 fclose(fortout);
306 break;
307 }
308 case(2):
309 {
310 FILE *fortout;
311 char fortname[FILELEN] = "ext_diq.";
312 strcat(fortname,suffix);
313 const char *fortop= (i==0) ? "w" : "a";
314 if(!(fortout=fopen(fortname, fortop) )){
315 fprintf(stderr, "Error %i in %s: Failed to open file %s for %s.\nExiting\n\n",\
316 OPENERROR, funcname, fortname, fortop);
317#if(nproc>1)
318 MPI_Abort(comm,OPENERROR);
319#else
320 exit(OPENERROR);
321#endif
322 }
323 if(i==0)
324 fprintf(fortout, "Re(qq)\n");
325 if(measure_check)
326 fprintf(fortout, "%e\n", NAN);
327 else
328 fprintf(fortout, "%e\n", creal(qq));
329 fclose(fortout);
330 break;
331 }
332 default: break;
333 }
334 }
335#ifdef USE_GPU
336 //Make a routine that does this for us
337 cudaFree(dk[0]); cudaFree(dk[1]);
338 cudaFree(Phi); cudaFree(ut[0]); cudaFree(ut[1]);
339 cudaFree(u[0]); cudaFree(u[1]);
340 cudaFree(id); cudaFree(iu);
341 cudaFree(dk_f[0]); cudaFree(dk_f[1]); cudaFree(ut_f[0]); cudaFree(ut_f[1]);
342 cudaFree(gamin); cudaFree(gamval); cudaFree(gamval_f);
343 if(c_sw){
344 cudaFree(sigval); cudaFree(sigval_f); cudaFree(sigin);
345 }
346 cublasDestroy(cublas_handle);
347#else
348 free(dk[0]); free(dk[1]);
349 free(Phi); free(ut[0]); free(ut[1]);
350 free(u[0]); free(u[1]);
351 free(id); free(iu);
352 free(dk_f[0]); free(dk_f[1]); free(ut_f[0]); free(ut_f[1]);
353 if(c_sw){
354 free(sigval); free(sigval_f); free(sigin);
355 }
356#endif
357#ifdef __RANLUX__
358 gsl_rng_free(ranlux_instd);
359#endif
360
361 return 0;
362}
Routines needed for Clover improved wilson fermions.
unsigned int * hd
Down halo indices.
Definition coord.c:11
unsigned int * hu
Up halo indices.
Definition coord.c:11
#define OPENERROR
Error opening file.
Definition errorcodes.h:32
double Polyakov(Complex_f *ut[2])
Calculate the Polyakov loop (no prizes for guessing that one...).
Definition bosonic.c:74
int Average_Plaquette(double *hg, double *avplaqs, double *avplaqt, Complex_f *ut[2], unsigned int *iu, float beta)
Calculates the gauge action using new (how new?) lookup table Follows a routine called qedplaq in som...
Definition bosonic.c:14
int Init_clover(Complex **sigval, Complex_f **sigval_f, unsigned short **sigin, float c_sw)
Initialise values needed for the clover terms.
Definition clover.c:696
int Measure(double *pbp, double *endenf, double *denf, Complex *qq, Complex *qbqb, double res, int *itercg, Complex *ut[2], Complex_f *ut_f[2], unsigned int *iu, unsigned int *id, Complex gamval[20], Complex_f gamval_f[20], const unsigned short gamin[16], Complex *sigval, Complex_f *sigval_f, unsigned short *sigin, double *dk[2], float *dk_f[2], Complex_f jqq, float akappa, float c_sw, Complex *Phi)
Calculate fermion expectation values via a noisy estimator.
Definition fermionic.c:7
void blockInit(int x, int y, int z, int t, dim3 *dimBlock, dim3 *dimGrid)
Initialises the CUDA grid and block size for a given lattice.
Definition cusu2hmc.cu:208
void Init_CUDA(Complex *u11t, Complex *u12t, Complex gamval[20], Complex_f gamval_f[20], unsigned short gamin[16], double *dk4m, double *dk4p, unsigned int *iu, unsigned int *id)
Initialise CUDA cuInit was taken already by CUDA (unsurprisingly).
Definition cusu2hmc.cu:254
int Init(const int istart, const int ibound, const int iread, const float beta, const float fmu, const float akappa, const Complex_f ajq, const float c_sw, Complex *u[2], Complex *ut[2], Complex_f *ut_f[2], Complex gamval[20], Complex_f gamval_f[20], unsigned short gamin[16], double *dk[2], float *dk_f[2], unsigned int *iu, unsigned int *id)
Initialises the system.
Definition su2hmc.c:10
int Reunitarise(Complex *ut[2])
Reunitarises u11t and u12t as in conj(u11t[i])*u11t[i]+conj(u12t[i])*u12t[i]=1.
Definition su2hmc.c:342
int Check_addr(unsigned int *table, int lns, int lnt, int imin, int imax)
Checks that the addresses are within bounds before an update.
Definition coord.c:305
int Addrc(unsigned int *iu, unsigned int *id)
Loads the addresses required during the update.
Definition coord.c:13
int Par_begin(int argc, char *argv[])
Initialises the MPI configuration.
Definition par_mpi.c:24
int Trial_Exchange(Complex *ut[2], Complex_f *ut_f[2])
Exchanges the trial fields.
Definition par_mpi.c:1043
int Par_sread(const int iread, const float beta, const float fmu, const float akappa, const Complex_f ajq, const float c_sw, Complex *u11, Complex *u12, Complex *u11t, Complex *u12t)
Reads and assigns the gauges from file.
Definition par_mpi.c:114
Matrix multiplication and related declarations.
int * pcoord
The processor grid.
Definition par_mpi.c:17
gsl_rng * ranlux_instd
RANLUX instance.
Definition random.c:16
#define AVX
Alignment of arrays. 64 for AVX-512, 32 for AVX/AVX2. 16 for SSE. Since AVX is standard on modern x86...
Definition sizes.h:283
#define nt
Lattice temporal extent. This also corresponds to the inverse temperature.
Definition sizes.h:96
#define nx
Lattice x extent.
Definition sizes.h:76
#define ksizet
Sublattice t extent.
Definition sizes.h:162
#define kvol
Sublattice volume.
Definition sizes.h:167
#define Complex
Double precision complex number.
Definition sizes.h:68
#define kferm
sublattice size including Gor'kov indices
Definition sizes.h:199
#define ksize
Sublattice spatial extent for a cubic lattice.
Definition sizes.h:159
#define nf
Fermion flavours (double it).
Definition sizes.h:164
#define cudaDeviceSynchronise()
Get rid of that bastardised yankee English.
Definition sizes.h:57
#define respbp
Conjugate gradient residue for .
Definition sizes.h:253
#define FILELEN
Default file name length.
Definition sizes.h:72
#define Complex_f
Single precision complex number.
Definition sizes.h:66
dim3 dimGrid
Default grid size. First component is normally nt. Second and third depend whatever is needed to get ...
Definition cusu2hmc.cu:27
#define ndim
Dimensions.
Definition sizes.h:192
#define kvolHalo
Subvolume + halo size.
Definition sizes.h:238
#define kfermHalo
Gor'kov lattice and halo.
Definition sizes.h:240
#define nz
Lattice z extent. We normally use cubic lattices so this is the same as nx.
Definition sizes.h:90
#define ny
Lattice y extent. We normally use cubic lattices so this is the same as nx.
Definition sizes.h:84
dim3 dimBlock
Default block size. Usually 128.
Definition cusu2hmc.cu:25
cudaStream_t streams[ndirac *ndim *nadj]
An array of concurrent GPU streams to keep it busy.
Definition cusu2hmc.cu:29
#define creal(z)
Extract Real Component using C standard notation.