C++ Wrapper for QCDNUM
The code below makes it possible to use the fortran DGLAP evolution program QCDNUM, by Michiel Botje, from C++ programs .
The general principle I have followed is to produce a wrapper that as far as possible is used in the same way as the fortran original. Thus for each fortran subroutine or function in the original, the wrapper has a C++ method that is used in a very similar way: i.e. it has the same name (but is in namespace QCDnum) with the same number and order of arguments, which are generally taken by reference, and it returns a value of comparible type if the original was a function. C++ String
is used where the fortran was a character array. Thus to understand how to use QCDNUM from C++, a user will mostly need to refer to the original QCDNUM manual.
First some warnings:
- C++/Fortran interfacing is compiler dependent. I use the gcc (g++, f77) compilers for which it works well (except gcc2.96 which has a bug that interferes with the wrapper, this is corrected in later gcc versions (3.2 and later are certainly OK in this respect)). I also tried it with the Sun compilers some time ago and got it working reasonably at the time. If you are using different compilers and find you have problems, you may need to make some adjustments specific to how your compilers work.
- This wrapper was written to work with QCDNUM version 16.12. If you are working with a different version you may need to add or subtract methods/functions from the wrapper.
- This wrapper is incomplete. I've only wrapped those QCDNUM functions that I've needed in my work. The .cpp file documents those methods that need to be written to complete the wrapper, if you require any of these it should be simple to see how to do this from similar methods already written (pay attention to which arguments of fortran subroutine are used for output).
How to use:
You will need to have already downloaded QCDNUM from www.nikhef.nl/~h24/qcdnum/. There is also an excellent manual there to accompany QCDNUM, which includes a test program (quprog.f). It may be wise to get the fortran test program working first so if you have any trouble using the wrapper below you know it's a C++/Fortran interfacing issue.
- Download the three wrapper files: QCDnum.cpp, QCDnum.h and wrapperHelper.f
- Download the C++ test program: quprog.cpp. This performs the same task as Botje's original test program.
- In your makefile you need to arrange things so that qcdnum1612.f and wrapperHelper.f are compiled with the fortran compiler, that QCDnum.cpp and your C++ program are compiled with your C++ compiler and then both sets of compiler output files are combined together at the linking stage (gcc requires the -lg2c option for linking). Here is my GNUmakefile for the test program.
- Make and run the the C++ test program. Compare the program output to that from the fortran test program. Small numerical differences are to be expected because of the different ways in which fortran and C++ programs internally represent floating point numbers.
Signs that the wrapper is not working correctly include:
- Seg fault, predictable. A seg fault occurrs while the program flow is in the wrapper or just after crossing from part of the program written in one language to a part written in another. Trivial changes to the main program (eg quprog.cpp) do not change when and where the seg fault occurrs and the seg fault can be reproduced by rerunning the program. Generally the problem will be in the wrapper method.
- Seg fault, erratic. A seg fault occurrs anywhere in the program, not necessarily anywhere near the wrapper or where flow crosses the boundary between code written in different languages. Trivial changes to the main program (eg quprog.cpp) may alter when and where the seg fault occurrs and the seg fault may not happen in the same place every time the program is run. Generally this indicates a memory problem arising from the wrapper. Perhaps a fortran subroutine makes use of a variable orignally defined in the C++ code, but which has gone out of scope. Diagnostic tools such as Valgrind Memcheck may help locate the problem.
go to Clare's homepage
Clare Quarman
Last modified: Fri Sep 29 16:04:35 BST 2006