00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include <iostream>
00014 #include <string>
00015 #include <signal.h>
00016
00017 #ifdef USING_UP
00018 extern "C" {
00019 #include <liblrm.h>
00020 #include <lrmerrno.h>
00021 }
00022 #endif
00023
00024 #include "QMCManager.h"
00025
00026 static const bool showExtraHeaders = !false;
00027
00028 QMCInput globalInput;
00029
00030 #ifdef QMC_GPU
00031 #include "GPUGlobals.h"
00032
00033 CGcontext g_cgContext;
00034 CGprofile g_cgProfile;
00035
00036 void idle();
00037 void openGLBootStrap();
00038 void reshape(int w, int h);
00039 void cgErrorCallback();
00040
00041 #endif
00042
00043 int argc;
00044 char ** argv;
00045 void qmcbeaver();
00046 void atExitCallback();
00047 void atSignalCallback(int sig);
00048
00049
00050
00051
00052
00053
00054
00055 enum signalChannels { CHANNEL1 = SIGURG, CHANNEL2 = 40 };
00056
00057 using namespace std;
00058
00059 void printCompileInfo(ostream & strm)
00060 {
00061 strm << "QMcBeaver version " << VERSION << " was compiled in ";
00062
00063 #ifdef SINGLEPRECISION
00064 strm << "single";
00065 #else
00066 strm << "double";
00067 #endif
00068 strm << " precision." << endl;
00069
00070 #ifdef __DATE__
00071 strm << "Compiled on " << __DATE__ << " " << __TIME__ << endl;
00072 #endif
00073 #ifdef __VERSION__
00074 strm << "Compiler version " << __VERSION__ << endl;
00075 #endif
00076
00077 strm << "With the libraries:";
00078 #ifdef PARALLEL
00079 strm << " MPI";
00080 #endif
00081 #ifdef USEBLAS
00082 strm << " BLAS";
00083 #endif
00084 #ifdef USELAPACK
00085 strm << " LAPACK";
00086 #endif
00087 #ifdef USESPRNG
00088 strm << " SPRNG";
00089 #endif
00090 #ifdef USEHDF5
00091 strm << " HDF5";
00092 #endif
00093 #ifdef QMC_GPU
00094 strm << endl;
00095 GPUGlobals::printVersions(strm);
00096 #endif
00097 strm << endl;
00098 strm << "Signaling: CHANNEL1 = " << CHANNEL1 << " CHANNEL2 = " << CHANNEL2 << endl;
00099 }
00100
00101 int main(int argcTemp, char *argvTemp[])
00102 {
00103 argc = argcTemp;
00104 argv = argvTemp;
00105 atexit(atExitCallback);
00106
00107 #ifdef USING_UP
00108 int warn, status;
00109 long stoptime;
00110
00111
00112
00113
00114
00115 lrmwarn(CHANNEL1,60,&warn,&stoptime,&status);
00116 #endif
00117
00118 #ifndef QMC_DEBUG
00119 signal(CHANNEL1,atSignalCallback);
00120 signal(CHANNEL2,atSignalCallback);
00121 signal(SIGTERM,atSignalCallback);
00122 signal(SIGHUP,SIG_IGN);
00123 #endif
00124
00125 #ifdef PARALLEL
00126
00127
00128 if( MPI_Init(&argc,&argv) )
00129 {
00130 cerr << "ERROR: MPI_Init Error" << endl;
00131 exit(1);
00132 }
00133
00134 int rank = -1;
00135 MPI_Comm_rank( MPI_COMM_WORLD, &rank );
00136
00137 if( rank != 0 )
00138 {
00139
00140
00141
00142
00143 ofstream dump("/dev/null");
00144 if(dump.good())
00145 clog.rdbuf(dump.rdbuf());
00146 } else {
00147
00148
00149
00150 clog.rdbuf(cout.rdbuf());
00151 }
00152 #else
00153 clog.rdbuf(cout.rdbuf());
00154 #endif
00155
00156 #ifdef QMC_GPU
00157 openGLBootStrap();
00158 #else
00159 qmcbeaver();
00160 #endif
00161
00162 #ifdef PARALLEL
00163
00164 if( MPI_Finalize() )
00165 {
00166 cerr << "ERROR: MPI_Finalize Error" << endl;
00167 exit(1);
00168 }
00169
00170
00171 exit(0);
00172 #endif
00173
00174
00175
00176 return 0;
00177 }
00178
00179 void qmcbeaver()
00180 {
00181 Stopwatch timer;
00182 timer.start();
00183
00184 if(argc < 2)
00185 {
00186 cerr << "ERROR: No input file given" << endl;
00187 exit(1);
00188 }
00189
00190 if(argc == 1 || showExtraHeaders)
00191 {
00192 printCompileInfo(clog);
00193 clog << endl << endl;
00194 }
00195
00196 globalInput.read( string(argv[ 1 ]) );
00197
00198 QMCManager TheMan;
00199 TheMan.initialize( argc, argv);
00200
00201 if( globalInput.flags.zero_out_checkpoint_statistics )
00202 {
00203 TheMan.zeroOut();
00204 }
00205
00206 bool ok = TheMan.run(false);
00207 TheMan.writeRestart();
00208
00209 if( globalInput.flags.my_rank == 0 && globalInput.flags.set_debug == 0)
00210 {
00211 clog << "*************** Print Results (ok = " << ok << ")" << endl;
00212 clog << TheMan;
00213 *TheMan.getResultsOutputStream() << TheMan;
00214 }
00215
00216 int optloops = 1;
00217 while( globalInput.flags.optimize_Psi &&
00218 optloops <= globalInput.flags.max_optimize_Psi_steps &&
00219 ok)
00220 {
00221 TheMan.writeTimingData(clog);
00222 clog << "*************** Optimize iteration: " << optloops << ";" << endl;
00223
00224
00225 TheMan.resetTimers();
00226
00227 TheMan.optimize();
00228 TheMan.zeroOut();
00229 ok = TheMan.run(globalInput.flags.equilibrate_every_opt_step);
00230
00231 stringstream save_opt;
00232 save_opt << globalInput.flags.checkout_file_name << "/"
00233 << globalInput.flags.base_file_name << ".opt" << optloops << ".ckmf";
00234
00235 if(globalInput.flags.a_diag < 0)
00236 {
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247 if(optloops % 2 == 0)
00248 {
00249 TheMan.writeRestart(save_opt.str());
00250 TheMan.writeRestart();
00251 }
00252 }
00253 else
00254 {
00255 TheMan.writeRestart(save_opt.str());
00256 TheMan.writeRestart();
00257 }
00258
00259 if( globalInput.flags.my_rank == 0 )
00260 {
00261 clog << "*************** Print Results (ok = " << ok << ")" << endl;
00262
00263 clog << TheMan;
00264 *TheMan.getResultsOutputStream() << TheMan;
00265 }
00266
00267 optloops++;
00268 }
00269
00270 if(!ok)
00271 {
00272 clog << "Error: the calculation was terminated prematurely.\n";
00273
00274
00275
00276
00277
00278 }
00279
00280 if(globalInput.flags.set_debug == 0)
00281 clog << "*************** Finalize" << endl;
00282 TheMan.finalize();
00283
00284 timer.stop();
00285
00286 if( globalInput.flags.my_rank == 0 &&
00287 globalInput.flags.calculate_bf_density == 1)
00288 {
00289 TheMan.writeBFDensity();
00290 }
00291
00292 if( globalInput.flags.my_rank == 0 &&
00293 globalInput.flags.nuclear_derivatives != "none")
00294 {
00295 TheMan.writeForces();
00296 }
00297
00298 if( globalInput.flags.my_rank == 0)
00299 {
00300 TheMan.writeTimingData(clog);
00301
00302 if(globalInput.flags.set_debug == 0)
00303 clog << "Wallclock Time: " << timer << endl;
00304
00305 TheMan.writeTimingData( *TheMan.getResultsOutputStream() );
00306 *TheMan.getResultsOutputStream()
00307 << "Wallclock Time: " << timer << endl;
00308 }
00309 }
00310
00311 void atExitCallback()
00312 {
00313 #if defined(_WIN32) && !defined(__CYGWIN__)
00314 clog << "Press any key...\n";
00315 getchar();
00316 #endif
00317 }
00318
00319 void atSignalCallback(int signal)
00320 {
00321 signalType choice;
00322
00323 switch(signal){
00324 case CHANNEL1:
00325 choice = SIG_REDUCE;
00326 break;
00327 case CHANNEL2:
00328 choice = SIG_INCREASE;
00329 break;
00330 case SIGTERM:
00331
00332 choice = SIG_QUIT;
00333 break;
00334 default:
00335 cout << "No response available for trapped signal " << signal << "\n.";
00336 return;
00337 break;
00338 }
00339
00340 QMCManager::receiveSignal(choice);
00341 }
00342
00343 #ifdef QMC_GPU
00344 void openGLBootStrap()
00345 {
00346
00347 GPUGlobals::checkExtensions();
00348
00350 glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);
00351 glutInitWindowSize(200, 1);
00352 glutInitWindowPosition(500, 500);
00353 glutCreateWindow("Hello, QMC-GPU!");
00354 glutHideWindow();
00355
00356 glDrawBuffer(GL_BACK);
00357 glReadBuffer(GL_BACK);
00358 glDisable (GL_DEPTH_TEST);
00359 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
00360 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00361
00362 glDrawBuffer(GL_FRONT);
00363 glReadBuffer(GL_FRONT);
00364 glDisable(GL_DEPTH_TEST);
00365 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
00366 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00367
00368 int err = glewInit();
00369 if (GLEW_OK != err)
00370 {
00371
00372 fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
00373 exit(-1);
00374 }
00375
00376
00377
00378
00379
00380
00381 glutIdleFunc(idle);
00382 glutReshapeFunc(reshape);
00383 GPUGlobals::getOpenGLError(0);
00385
00387 cgSetErrorCallback(cgErrorCallback);
00388 g_cgContext = cgCreateContext();
00389
00390
00391 g_cgProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT);
00392 assert(g_cgProfile != CG_PROFILE_UNKNOWN);
00393 cgGLSetOptimalOptions(g_cgProfile);
00395
00396
00397
00398 glutMainLoop();
00399 }
00400
00401
00402 void cgErrorCallback()
00403 {
00404 CGerror lastError = cgGetError();
00405
00406 if(lastError)
00407 {
00408 printf("%s\n\n", cgGetErrorString(lastError));
00409 printf("%s\n", cgGetLastListing(g_cgContext));
00410 printf("Cg error!\n");
00411 }
00412 }
00413
00414
00415 void idle()
00416 {
00417
00418
00419 qmcbeaver();
00420 #ifdef PARALLEL
00421 if( MPI_Finalize() )
00422 {
00423 cerr << "ERROR: MPI_Finalize Error" << endl;
00424 exit(1);
00425 }
00426 #endif
00427 exit(0);
00428 }
00429
00430
00431 void reshape(int w, int h)
00432 {
00433 glViewport(0, 0, (GLsizei)(w), (GLsizei)(h));
00434 glMatrixMode(GL_PROJECTION);
00435 glLoadIdentity();
00436 gluOrtho2D(-1, 1, -1, 1);
00437 glMatrixMode(GL_MODELVIEW);
00438 glLoadIdentity();
00439 }
00440 #endif
00441