-
Notifications
You must be signed in to change notification settings - Fork 0
/
propagate.cu
362 lines (225 loc) · 11.2 KB
/
propagate.cu
1
2
3
4
5
6
7
8
9
10
11
12
13
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
47
48
49
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
propagate.cu
Main file for matrix exponential propagation
Nicolas Sawaya
2013
*/
#include <includeFiles.cu>
int main( int argc, char *argv[] ) {
//For error handling
//bool errorThrown = false;
//Read in which device being used
if(argc<3) {
cout << "ERROR. Must include device being used in command line. Usage:";
cout << endl << "./executable {devicenumber}.";
exit(1);
}
int deviceUsed = atoi(argv[2]);
cout << "Setting device to " << deviceUsed << "." << endl << endl;
if(cudaSetDevice(deviceUsed)!=cudaSuccess) {
cout << "ERROR setting device to " << deviceUsed;
cout << "." << endl;
return false;
}
//Set up CULA, cuBLAS, cuSPARSE
if( ! initCula() ) exit(1);
cusparseHandle_t cusparseHandle=0;
if( ! initCusparse(&cusparseHandle) ) exit(1);
cublasHandle_t cublasHandle=0;
if( ! initCublas(&cublasHandle) ) exit(1);
//cuSPARSE
//cusparseStatus_t csStatus;
//cusparseHandle_t cusparseHandle = 0;
//cusparseMatDescr_t matDescr_A = 0;
cout << endl;
cout << " ( \n"
" )\\ ) ( ) \n"
"(()/( ( ) ( )\\ ( ) ( /( \n"
" /(_)) )\\ ) ( ( /( )((_) ))\\ ( /( ( )\\()) \n"
"(_))_ (()/( )\\ ) )(_))((_)_ /((_) )(_)) )\\ ) (_))/ \n"
" | \\ )(_)) _(_/( ((_)_ / _ \\ (_))( ((_)_ _(_/( | |_ \n"
" | |) || || || ' \\))/ _` || (_) || || |/ _` || ' \\))| _| \n"
" |___/ \\_, ||_||_| \\__,_| \\__\\_\\ \\_,_|\\__,_||_||_| \\__| \n"
" |__/ \n";
cout << endl << endl;
cout << "DynaQuant (beta)" << endl;
cout << endl;
cout << "If you use the code, please consider citing Sawaya, et al., Nano Lett., 2015, 15 (3), 1722";
cout << endl << endl;
//Set up timer
time_t timeStart, timeNow;
time(&timeStart);
//Declare
systemClass<typeMat,typeState,typeReal> sysObj;//(/*SIM_TYPE_CONST_HAM*/);
sysObj.setCusparseHandle(&cusparseHandle);
sysObj.setCublasHandle(&cublasHandle);
initCula(); //Initialize CULA
//Parse in-file
if( ! sysObj.parseInFile(argv[1]) ) exit(1);
//Initialize state
if( ! sysObj.setInitState() ) exit(1);
// if( ! sysObj.initStateAsBasisVector(sysObj.initialState) ) exit(1);
//Get exponential coefficient. This will include dt, equaling dt*i*(-1/hbar)
typeMat h_expCoeff;
typeMat *d_expCoeff;
h_expCoeff = get_neg_inv_hbar_imag();
h_expCoeff.y = h_expCoeff.y * sysObj.dt;
cout << "h_expCoeff.{x,y} = " << h_expCoeff.x << ", " << h_expCoeff.y << endl;
cudaError_t cuErr;
cuErr = cudaMalloc((void**)&d_expCoeff, sizeof(typeMat));
cuErr = cudaMemcpy( d_expCoeff, &h_expCoeff, sizeof(typeMat), cudaMemcpyHostToDevice );
if(cuErr!=cudaSuccess) {
cout << "ERROR copying (or maybe allocating) expCoeff to device." << endl;
exit(1);
return false;
}
//Print this first state
// sysObj.printSingleState(0);
//Declare pointers. They have to be pointers if I'm not sure that they will become objects.
lanczosClass<typeMat,typeState,typeReal> *lanczosObjPtr;
padeClass<typeMat,typeState,typeReal> *padeObjPtr;
taylorKrylovClass<typeMat,typeState,typeReal> *tkObjPtr;
//Declared objects depend on propagation mode
if(sysObj.propMode==PROP_MODE_LANCZOS) {
cout << "propMode " << "PROP_MODE_LANCZOS" << endl;
//Initialize lanczos object
lanczosObjPtr = new lanczosClass<typeMat,typeState,typeReal>( &sysObj,
sysObj.m, &(sysObj.d_hamCsr) );
//Initialize pade object
padeObjPtr = new padeClass<typeMat,typeState,typeReal>( &sysObj , lanczosObjPtr , d_expCoeff );
} else if(sysObj.propMode==PROP_MODE_TAYLOR_LANCZOS) {
cout << "propMode " << "PROP_MODE_TAYLOR_LANCZOS" << endl;
//Initialize Taylor-Krylov object
tkObjPtr = new
taylorKrylovClass<typeMat,typeState,typeReal>(
&sysObj,
&(sysObj.d_hamCsr),
sysObj.tkMatFileName,
sysObj.pTaylorKrylov,
sysObj.mMatBTaylorKrylov,
h_expCoeff,
d_expCoeff
);
}
//Get number of ensembles to run
int numEnsembleRuns=1;
if(sysObj.ensembleAvgMode != ENS_AVG_MODE_NONE ) {
numEnsembleRuns = sysObj.numEnsembleRuns;
}
cout << "numEnsembleRuns = " << numEnsembleRuns << "." << endl << endl;
//Get number of initial conditions to run (e.g. for abs spec, it's 3)
int numInitConditions;
numInitConditions = sysObj.getNumInitConditions();
cout << "numInitConditions = " << numInitConditions << endl;
//Show time
time(&timeNow);
cout << "Starting simulation. Elapsed time is " << (timeNow-timeStart) << endl;
//Get number of inner loops you will do
int totalLoops = sysObj.totalLoops;
//Loop over different initial conditions.
for(int iterInitCond=0;iterInitCond<numInitConditions;iterInitCond++){
//Set next initial state
cout << "Setting initial state #" << iterInitCond << endl;
sysObj.prepNextInitState(iterInitCond);
//Loop over different ensemble runs
for(int run=0; run<numEnsembleRuns; run++) {
//cout << "Starting run=" << run << endl;
//cout << "Setting initial vector." << endl;
//Reset the inital basis vector
//if( ! sysObj.resetDeviceToInitialState() ) exit(1);
//Prepping for next run (including re-randomizing ZZReal Hamiltonian)
if( !sysObj.prepAnalysesForNextRun() ) return false;
//Do several "loops" (this saves memory)
int currentStep = 0;
for(int loopNum=0; loopNum<totalLoops; loopNum++) {
//cout << "Doing getNumStepsThisLoop()" << endl;
//Get number of steps in this loop. Prevents against overshooting end of array.
int numStepsThisLoop = sysObj.getNumStepsThisLoop(currentStep);
//cout << "Starting loop" << endl;
//Complete one loop
int destVecNum, sourceVecNum;
for(int step=0; step<numStepsThisLoop; step++) {
//Get dest and source vectors, i.e. dest=exp(-iH*dt)*source
//Depends on whether this is the first loop or not
sourceVecNum = (step==0&&loopNum!=0) ? (sysObj.stepsPerLoop) : (step);
destVecNum = step + 1;
//Calculate and store inv. partic. ratio, if specified
if(! sysObj.calcInvPatricRatio(sourceVecNum, currentStep) ) break;
//Update the Hamiltonian (with e.g. stochastic process)
if(! sysObj.updateHamiltonian( currentStep*sysObj.dt ) ) return false;
//Debug flag for Hamiltonian
if(sysObj.debugDiags) {
//Copy Hamiltonian back
sysObj.h_hamCoo.copyFromDevice(sysObj.d_hamCoo);
// sysObj.fDiagDebugs << "** step " << step << " **" << endl;
for(int elem=0;elem<sysObj.h_hamCoo.nnz;elem++) {
// sysObj.fDiagDebugs << sysObj.h_hamCoo.cooRowIndA[elem] << " " << sysObj.h_hamCoo.cooColIndA[elem] << endl;
if(sysObj.h_hamCoo.cooRowIndA[elem]==sysObj.h_hamCoo.cooColIndA[elem])
sysObj.fDiagDebugs << sysObj.h_hamCoo.cooValA[elem].x << endl;
}
}
//Branch based on propagation mode
if(sysObj.propMode==PROP_MODE_LANCZOS) {
//Do the lanczos decomposition
if(! lanczosObjPtr->doLanczos( sysObj.getStateDevPtr(sourceVecNum) ) ) return false;
//Do the pade exponentiation
if(! padeObjPtr->doPade() ) break;
//Propagate exp(-i/hbar * tau * H)*vec = beta*Q*exp(-i/hbar *tau*H)*e1;
if(! propagateSystem(&sysObj, lanczosObjPtr, padeObjPtr, destVecNum) ) break;
} else if(sysObj.propMode==PROP_MODE_TAYLOR_LANCZOS) {
//Propagate using Taylor-Krylov method
if(! tkObjPtr->propagateWithTaylorKrylov(
sysObj.getStateDevPtr(sourceVecNum), sysObj.getStateDevPtr(destVecNum)
) ) return false;
}
//Renormalize (partic ratio calculated here)
if(! sysObj.renormalizeAndStoreNorm(destVecNum) ) break;
//Calculate Error
if(!
sysObj.calculateErrorEstimate(lanczosObjPtr,padeObjPtr,tkObjPtr,sysObj.h_norm)
) return false;
//Increment
currentStep++;
} //end of one "loop"
cout << "currentStep = " << currentStep << endl;
//Run extra routines as necessary
cout << "run = " << run << endl;
if(! sysObj.extraAnalysisAfterSingleLoop(loopNum,currentStep,numStepsThisLoop) ) exit(1);
} //end of single ensemble run
//Run extra routines as necessary, e.g. routines required for spectra
if(! sysObj.extraAnalysisAfterSingleRun(run) ) exit(1);
//Print out total accumulated error for the run
sysObj.printTotalError();
//Show time
time(&timeNow);
//cout << "Finished run " << run+1 << ". Elapsed time is ";
cout << (timeNow-timeStart) << " seconds elapsed." << endl;
//Computes populations, adds to average
// if(! sysObj.addPopToEnsembleAverage( run, lanczosObj.d_one ) ) break;
} //end of loop over ensemble runs
//Average out ensemble if in ensemble-mode
// sysObj.calcEnsembleAverage();
//Run extra routines as necessary, e.g. routines required for spectra
if(! sysObj.extraAnalysisAfterEnsemble(iterInitCond) ) exit(1);
} //end of loop over different initial conditions
//Run extra routines as necessary, e.g. routines required for spectra
if(! sysObj.extraAnalysisEnd() ) exit(1);
//Copy that state back to host
//Maybe don't always include
//sysobj.matOfStates.
//Copy all states back to host
// sysObj.matOfStates.copyThisDeviceToThisHost();
//Print out the states
//////sysObj.matOfStates.printHostMat();
// sysObj.printAllStates();
// sysObj.printAllStatesNormSq();
//Copy all populations averages back to host
// sysObj.matEnsembleAverage.copyThisDeviceToThisHost();
// sysObj.printOneSiteEnsembleAvgPopAllSteps(300);
//sysObj.printOneSiteEnsembleAvgPopAllSteps(301);
//sysObj.printOneSiteEnsembleAvgPopAllSteps(359);
//Show time
time(&timeNow);
cout << endl << "Closing. Elapsed time is " << (timeNow-timeStart);
cout << " seconds." << endl;
}