MPQC
3.0.0-alpha
|
In addition to the executables, the MPQC toolkit libraries and include files can be installed on your machine. This is described in the Compiling section of this manual.
The mpqc-config
program can be use to obtain the compilers, compiler options, and libraries needed to use the MPQC toolkit from your program. This utility is discussed below, along with how the MPQC toolkit must be initialized in your main
subroutine.
The mpqc-config program returns information about how MPQC was compiled and installed. See The mpqc-config Program for more information.
A variety of initializations must be done when a program using the MPQC classes is started. This includes finding out what sc::MessageGrp to be used for communication, what sc::ThreadGrp is to be used for multi-threading, what default sc::Integral factory should be used, reading the input file in a way that is efficient on a parallel machine, setting up the output, and so on. The sc::MPQCInit class simplifies this setup. It is used by first initializing an sc::GetLongOpt object. This is used to retrieve flags specified on the command line. The programmer enrolls any desired options into the sc::GetLongOpt object and then passes this, along with argc
and argv
, to the constructor for sc::MPQCInit. The sc::MPQCInit class will enroll the options it requires: –messagegrp
, –threadgrp
, –memory
, –resources
, and –integral
. Then the programmer can parse the options and retrieve the value of any flag or obtain anything that was specified on the command line that was not associated with a flag.
Below is a code fragment illustrating the use of sc::MPQCInit.
sc::GetLongOpt opt; opt.usage("[options] [filename]"); opt.enroll("verbose", GetLongOpt::NoValue, "enable extra printing", 0);
sc::MPQCInit init(opt,argc,argv);
int optind = opt.parse(argc, argv); std::string inputfile; if (argc - optind == 0) { inputfile = "input.in"; } else if (argc - optind == 1) { inputfile = argv[optind]; } else { opt.usage(std::cout); return 1; }
sc::Ref<sc::KeyVal> keyval = init.init(inputfile);
This section illustrates how to add a new quantum chemistry method implementation to MPQC.
This example code illustrates a complete MP2 energy implementation using the MPQC Toolkit. First an MP2 class is declared and the necesary base class member functions are provided. Next a ClassDesc is defined. Finally, the member functions are defined.
Note that no main routine is provided. This is because this file is designed to be used to extend the functionality of the mpqc executable. To generate a new mpqc executable with the new class available for use, see the MP2 Implementation Example: Makefile section.
This example Makefile demonstrates how to link in a new class to form a new mpqc executable, here named mp2. The code is given in the MP2 Implementation Example: Source section. The mpqc-config command is used to obtain information about how the MPQC toolkit was compiled and installed. The library specified with -lmpqc provides the main routine from mpqc.
This input file can be used with the program illustrated in the MP2 Implementation Example: Source section. It will compute the MP2 energy using the new MP2 class. Note that only the object-oriented input format can be used with user provided classes.
The MP2 sample program above used the MPQC main routine. This does not always provide sufficient flexibility, and a user-provide main routine is sometimes more appropriate. However, quite a few steps are need to prepare for a run, most of which have to do with initializing various libraries to prepare for a parallel run. The HF example illustrates how an sc::MPQCInit object can be used to prepare for a computation. It implements a complete closed-shell Hartree-Fock program using various MPQC functionality, without provide a new MPQC sc::Wavefunction implementation as done in the previous example (Using MPQC's main: MP2 Implementation Example).
This example program can be found in the doc/devsamp/hf
subdirectory of the MPQC source distribution and is shown here in its entirety.
This example Makefile will build an executable named hf. The mpqc-config command is used to obtain information about how the MPQC toolkit was compiled and installed. The library specified with -lmpqcinit provides the initialization routines from mpqc.
This input will run a Hartree-Fock calculation on a water molecule. The section beginning mpqc:
provides the input needed to run the mpqc
program with the same basis set and geometry as a check. The hf
program only reads the occ
and basis
keywords at the beginning of the sample input.
The development of MPQC began before exception handling was available in C++. A retrofit of the code to use exceptions is in progress. It is difficult to retrofit a code, especially a parallel code, to do exception handling. There will be some limitations: exception handling will not work well for parallel jobs, objects whose members throw might be left in a questionable state, etc. However, it is intended that MPQC objects will be usable in an interactive environment. It is also planned that exceptions be used internally to facilitate recover from certain problems.
All new code should use exceptions instead of exit or abort and allocate resources in such a way that, if an exception occurs, all resources such as memory or locks are released. A hierarchy of exception classes has been created that maps better to scientific computing than the standard exceptions. More information is below, as well as in the documentation for the SCException class and its derivatives.
Consider the following code fragment:
Object *obj = new Object; double *array = new double[n];
f(obj, array, mol);
delete obj; delete[] array;
If an exception is thrown in the function f(), then storage for array and obj will not be released. The standard C++ library provides a class, auto_ptr, to deal with obj, and the MPQC toolkit provides a class, auto_vec, to deal with array.
The include files for these two classes are:
#include <memory> #include <util/misc/autovec.h>
the code would be modified as follows:
std::auto_ptr<Object> obj(new Object); sc::auto_vec<double> array(new double[n]);
f(obj.get(), array.get());
obj.release(); // or just let the destructor release it array.release(); // or just let the destructor release it
Note that when sc::Ref is used to store pointers, the storage will automatically be released when necessary. No special treatment is needed to deal with exceptions.
Consider the following code fragment:
g(const sc::Ref<sc::ThreadLock> &lock) { lock->lock(); f(); lock->unlock(); }
If f() throws, then the lock is never released. The ThreadLock lock() and unlock() members should not be used anymore. Now do the following:
g(const sc::Ref<sc::ThreadLock> &lock) { sc::ThreadLockHolder lockholder(lock); f(); lockholder->unlock(); // or let the destructor unlock it }
Consider the following code fragment:
g(const sc::Ref<sc::RegionTimer> ®tim) { regtim->enter("f()"); f(); regtim->exit(); }
If f() throws, then the "f()" timing region is never exited. Instead use the following:
g(const sc::Ref<sc::RegionTimer> ®tim) { sc::Timer timer(regtim, "f()"); f(); timer.reset(); // or let the destructor exit the region }
The MPQC exceptions provide information that can be used into two ways. First, text information is provided so that if the exception is not caught at a lower level, then the mpqc executable will catch it and write information about the problem to the terminal or an output file. Second, information about the nature of the problem is provided, to permit developers to catch the exception and deal with it in some way. The documentation for sc::SCException and all of its derivatives gives more information about the exceptions that are available. As an example, consider the following loop, where a maximum number of iterations is permitted:
XYZ::update() { for (int i=0; i<maxiter; i++) { // ... compute xyz update ... if (residual < threshold) return; } throw MaxIterExceeded("too many iterations xyz computation", __FILE__, __LINE__, maxiter, class_desc()); }
The first argument to the exception class is a brief description of the error. Additional information can be provided, see SCException::elaborate() description below. The next two arguments are the filename and line number. The C preprocessor provides these for you with the FILE and LINE macros. The next argument is specific to the MaxIterExceeded exception; it is the maximum number of iterations. Finally, a ClassDesc* can be given, which will be used to print out the class name of the object that failed. All of these arguments are optional; however, the first three should always be given.
It is possible to provide additional information using the SCException::elaborate() member. This will return a ostream, and the additional information can be written to this stream. However, if for some reason it is not possible to write to this stream (say, there wasn't enough memory to allocate it), then an exception will be thrown. For this reason, the string description given as the first argument should be informative since the additional information might not be available, and attempts to use elaborate() should be in a try block. So, for example, the elaborate() member could be used in the above example as follows:
XYZ::update() { for (int i=0; i<maxiter; i++) { // ... compute xyz update ... if (residual < threshold) return; } MaxIterExceeded ex("too many iterations in xyz computation", __FILE__, __LINE__, maxiter, class_desc()); try { ex.elaborate() << "this can happen when the stepsize is too small" << std::endl << "the stepsize is " << stepsize << std::endl; } catch (...) {} throw ex; }
Note that writing to stream returned by elaborate() won't necessarily cause anything to get written to the terminal or an output file. The information will be available when the what() member is called, if writing to the stream succeeds. If the exception is caught by the mpqc main routine, then it will be printed for the user to see. If the program catches the exception and determines that it is possible to proceed in a different way, then the user will never see the text.
Usually, exceptions are not the desired behaviour in a program, and it is necessary to debug a program that throws an exception. This was easy when abort was called, because abort would raise a signal that was caught by the debugger and the code is stopped at the appropriate place. With exceptions the matter is more complex, because the stack is unwound when an exception is thrown and most debugging information is lost. To work around this problem, a breakpoint can be set in code that will be reached only in an exception, and will be run before the stack unwind begins. A useful place to do this when GCC is used as the compiler is in the routine __cxa_allocate_exception(). So, in gdb, the following could be done:
$ gdb ./scextest (gdb) b main (gdb) run Breakpoint 1, main () at /home/cljanss/src/SC/src/lib/util/misc/scextest.cc:172 172 f(); (gdb) b __cxa_allocate_exception (gdb) cont Breakpoint 2, 0x40582d46 in __cxa_allocate_exception () from /usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.5/libstdc++.so.5 (gdb) where #0 0x40582d46 in __cxa_allocate_exception () from /usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.5/libstdc++.so.5 #1 0x0804b3f7 in f () at /home/cljanss/src/SC/src/lib/util/misc/scextest.cc:60 #2 0x0804b9e9 in main () at /home/cljanss/src/SC/src/lib/util/misc/scextest.cc:172
Giving gdb "b main" followed by "run" was required before gdb could find the __cxa_allocate_exception symbol.
There are two ways to test an MPQC build. The testbuild
and testrun
make targets can be used to run test programs in various library directories, and the check
and related make targets can be used to run MPQC on sets of input files. See Validating MPQC for more information about how to run the tests.
Test programs can be added to the library directories by providing a source file with a main routine. The set of test programs that is to be built and run by testbuild
and testrun
, respectively, is given by the TESTPROGS
variable in the library's Makefile
. It may be necessary for an explicit rule to be given for building the test program to ensure that necessary libraries are linked in. If a file named after the test program with a .out
suffix is found in the source directory, then testrun
fail if the command's output differs from that file. Care must be taken to ensure that the output is architecture independent in this case. Otherwise, testrun
will fail only if running the command results in a nonzero return code.
Additional MPQC test inputs can be added in the test/ref
directory. These inputs can be provided in one of two ways. An input which is used to automatically generate multiple test cases can be written (with a .qci
suffix), or a subdirectory with each input can be made. See Makefile
, basis1.qci
, and input
in the test/ref
directory for examples.
After you have added new inputs and modified the Makefile, change into the test/ref
subdirectory of your object directory (where you compiled MPQC) and type make inputs
. This will create a input
subdirectory containing MPQC input files with a .in
suffix. Files ending with a .qci
suffix will also be placed in the input
directory. These contain a description of the calculation that is used by the utility program that checks the results of the validation suite. Both the .in
and .qci
files for the new test cases must be copied into the ref
directory in the source tree. Note that inputs that are not useful in your build environment are not created by make inputs
.