## Developing MPI applications in Qt Creator

I have grown very fond of the ease and usability of Qt Creator lately, making it my main tool for developing anything in C and C++. Recently I started learning the MPI framework for doing large scale parallel programming and figured I wanted to try to make MPI play along with Qt Creator.

The first thing I noticed is that there is no problem using Qt Creator as an editor while compiling the MPI executable and running it from the command line. However, for the quick and easy testing of minor changes, running by pressing Ctrl + R in Qt Creator quickly came to a high place on my wish list.

After a quick search around the Internet, I found the necessary adjustments to make this exact thing happen. The steps are as follows:

### Make Qt Creator use mpicxx instead of g++

To change the compiler in Qt Creator, you simply have to edit the .pro file of your project. Add the following lines, and Qt Creator will call mpicxx instead of g++:

# MPI Settings
QMAKE_CXX = mpicxx
QMAKE_CXX_RELEASE = $$QMAKE_CXX QMAKE_CXX_DEBUG =$$QMAKE_CXX
QMAKE_LINK = $$QMAKE_CXX QMAKE_CC = mpicc QMAKE_CFLAGS +=$$system(mpicc --showme:compile)
QMAKE_LFLAGS += $$system(mpicxx --showme:link) QMAKE_CXXFLAGS +=$$system(mpicxx --showme:compile) -DMPICH_IGNORE_CXX_SEEK
QMAKE_CXXFLAGS_RELEASE += system(mpicxx --showme:compile) -DMPICH_IGNORE_CXX_SEEK

This also sets the correct compile and link flags in addition to changing the linker to mpicxx as well. The DMPICH_IGNORE_CXX_SEEK flag is added to avoid some errors I experienced on Red Hat Linux 5.

### Running the application with mpirun

Usually Qt Creator will simply run the generated executable after compiling, but to make use of multiple processors (which kind of is the whole point with MPI), you have to change the run settings of your project to a custom executable. This is done in the project configuration in Qt Creator under the Run tab for your project.

Add a Custom Executable run configuration and set the executable to /usr/bin/mpirun or any other path where mpirun resides on your system (run ‘which mpirun’ in a terminal to find it if you don’t know where it is). Then, set the arguments to

-n 2 yourexecutable

to run yourexecutable with 2 processors. Of course you’ll have to change yourexecutable to whatever your executable is named.

### Adding a GUI to your MPI program

I advice you to not use MPI together with Qt GUI libraries. The reasons for this is that Qt has its own threading classes that are easier to use together with GUI elements in your application if you need them and the fact that if you want to run your app on any large scale cluster you might not be able to use Qt at all, depending on what packages are available on this cluster. A much better solution is to load config settings from a file in the INI format, using for instance the lightweight inih library and make this settings file writable from a Qt GUI application using QSettings to write this file.

The downside of this approach is that you won’t be able to implement any communication directly with the MPI process, but this could be worked around by for instance reading and writing to a progress status file if this is what you need.

However, it is not impossible to make Qt GUI applications play along with MPI, even though you might have to juggle a bit around with stuff to make it work flawlessly. Passing MPI broadcasts and such to all the other processes is definitely possible, and a relatively small example of how to do this is shown in this post by Tiffany Chong and Valencia Hare.