Tuesday, 7 March 2017

Compiling xdrfile library by using PGI C compiler (fast and dirty way)


1. Introduction.

2. Downloading the code of xdrfile library.

3. Compiling and installing the code.

4. Testing the compilation.

The xdrfile library is a side-project of GROMACS and provides an easy to use universal interface for reading XTC and TRR trajectories from within python, C/C++, and Fortran code based applications.

The library code could be easily compiled by using GNU C and Intel C compilers, but the attempt to use PGI compilers collection through the configure script will fail. One of the reasons for that failure is that PGI compilers do not support invocation with GNU style parameters. Of course, that might be solved by changing the configure script and thus matching the PGI C compiler set of input parameter, but unless you wanna pack the compilation as RPM, DEB, or other package, it is not worth to do it. The code might be compiled directly from command line without any automation in seconds, and the goal of this document is to show how to.


The source code tarball of xdrfile library could be downloaded from the download page of GROMACS project (scroll down the page - the xdrfile links are in the bottom of the page):


The current version is 1.1.4. Create a separate folder, download and unzip the tarball into it, and enter the folder:

$ mkdir ~/tmp/xdrfile
$ cd ~/tmp/xdrfile
$ wget ftp://ftp.gromacs.org/contrib/xdrfile-1.1.4.tar.gz
$ tar zxvf xdrfile-1.1.4.tar.gz
$ cd xdrfile-1.1.4/src


Before start compiling the code one need to realize that both shared or static library might be needed build because some of the applications which will use the library might follow different model of compilation - some of them might need the shared version of the library, another ones might need the static version. So the example bellow shows how to compile both of them:

The compilation of shared library (using position independent code (PIC) model):

$ cd ~/tmp/xdrfile/xdrfile-1.1.4/src
$ pgcc -fastsse -fPIC -c xdrfile_xtc.c xdrfile_trr.c xdrfile.c -I../include
$ pgcc -fastsse -fPIC -shared -o libxdrfile.so xdrfile.o xdrfile_trr.o xdrfile_xtc.o
$ sudo cp ~/tmp/xdrfile/xdrfile-1.1.4/src/libxdrfile.so /usr/local/lib

To compile the static version of the library, create first the object files (as shown above) and use the GNU ar tool to pack them:

$ cd ~/tmp/xdrfile/xdrfile-1.1.4/src
$ pgcc -fastsse -fPIC -c xdrfile_xtc.c xdrfile_trr.c xdrfile.c -I../include
$ ar rcs libxdrfile.a xdrfile_xtc.o xdrfile_trr.o xdrfile.o
$ sudo cp ~/tmp/xdrfile/xdrfile-1.1.4/src/libxdrfile.a /usr/local/lib

Note that using the static version of the library is not recommended, unless you are very certain about what you want to achieve by compiling your code statically.


The xdrfile library is supplied with two test tools - one written in C and another one - in Python. Because the test requires some TRR and XTC trajectories to exist, the C tool should be run first to generate them, after its successful execution, of course.

To execute xdrfile_c_test we need to have it compiled from the C-source. The compilation with respect the shared library libxdrfile.so (for the example it is installed locally in /usr/local/bin) follows the recipe:

$ cd ~/tmp/xdrfile/xdrfile-1.1.4/src
$ pgcc -fastsse -o xdrfile_c_test xdrfile_c_test.c -L/usr/local/lib -lxdrfile -I../include

To check the if the shared library compiled before works as expected and generate sample TRR and XTC trajectories to test the tools on, execute xdrfile_c_test (presume the libxdrfile.so is in /usr/local/bin):

$ cd ~/tmp/xdrfile/xdrfile-1.1.4/src
$ export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
$ ./xdrfile_c_test

If the library libxdrfile.so is successfully compiled, loaded, and works as expected the following output will appear on the display:

Testing basic xdrfile library: PASSED
Testing xtc functionality: PASSED
Testing trr functionality: PASSED

and these new trajectory files will be created:

$ ~/tmp/xdrfile/xdrfile-1.1.4/src/test.trr
$ ~/tmp/xdrfile/xdrfile-1.1.4/src/test.xtc

Execute the tool xdrfile_test.py (have Python 2.7, presume the libxdrfile.so is in /usr/local/bin):

$ cd ~/tmp/xdrfile/xdrfile-1.1.4/src/python
$ export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
$ ./xdrfile_test.py

If the execution is successful the displayed result will look like:

../test.trr OK
../test.xtc OK

Compile trr2xtc from its C-source (for the example the shared library libxdrfile.so is installed locally in /usr/local/bin):

$ cd ~/tmp/xdrfile/xdrfile-1.1.4/src/python
$ pgcc -fastsse -o trr2xtc trr2xtc.c -L/usr/local/lib -lxdrfile -I../include

Execute it to convert the sample TRR trajectory into XTC one (if libxdrfile.so is in /usr/local/bin):

$ cd ~/tmp/xdrfile/xdrfile-1.1.4/src/python
$ export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
$ ./trr2xtc -i test.trr -o converted.xtc

To be sure that the conversion is successful, compare the SHA256 checksums of the produced converted.xtc and the sample test.xtc (that will work in case test.xtc has not been modified after its creation):

$ cd ~/tmp/xdrfile/xdrfile-1.1.4/src/python
$ sha256sum converted.xtc
$ sha256sum test.xtc

They must match!