I'm part of a Computational Science course and come from a non-programming background, so please forgive me my ignorance. I'm working on a set of code in C to numerically solve the Navier Stokes equations for a Computational Fluid Dynamics course. Anyway, as a part of our task, I have to write code that runs on a single processor (serially) as well as provide the choice to parallelize that same code with OpenMP and/or MPI (to utilize multiple processors, depending on user input at the beginning of run time.

I was hoping for advice from the community on doing this and I have a few questions:

  1. Should I be thinking of an if/else structure for my program? (The program would need to branch towards serial code, or MPI enabled code, or OpenMP code, or a hybrid of MPI+OpenMP, and this would mean that much of the code would be duplicated).
  2. Should I constructs the actions that would be duplicated to be separate functions and call these functions during run time where they are needed? (But from what I understand function calls would increase the overhead of my code in general, regardless of what choice is picked)
  3. Would there be a way to hide the pre-processor directives, or MPI-related functions (e.g. MPI_Init()) so that they would conveniently appear or disappear depending on the choice the user made at the beginning or run time?
  4. Finally, the purpose of this task is to measure the performance benefits of using serial code, vs. OpenMP, vs. MPI, vs. MPI+OpenMP. Does it make sense that I am trying to avoid code duplication since combining the code (to increase readability) would unnecessarily add overhead to my serial code? Such as MPI-related function calls, potentially many if/else conditions, which would skew the performance measurements of my serial code, versus an if/else structure at the beginning of the program which directs it down a completely separate path (as in Q1).

I hope I've asked a clear question and have abided by the community's standards. Thank you kindly.


I am wondering why you want to combine all the methods(serial, MPI, OpenMP) into one C program. I think a better way is to separate them into 3 programs, and call them by a script(like shell). Anyway, they are different, considering the code and compiling.

To your first question, if you insist on one C program, of course you have to figure out a way to combine them together. Maybe if/else is not enough, as I know, MPI program is called by "mpirun" command. It seems to be a little hard using it in C.

To your second question, I suggest you convert your serial program into OPenMP and MPI first, and then think about avoiding duplicates. There is a great difference between serial and parallel program.

To your third question, MPI_init is a part of MPI program, as I know, it is necessary because they will do some pre-processing jobs.

To your last question, combining them together would bring some side effects unavoidably since you may use "mpicc" to compile serial and OpenMP program. I think it can be ignored because it's a tiny part for your entire program.

Still, I suggest you separate them and use a script.

  • $\begingroup$ You often want to combine all three. Especially when you write code to be reused. Take FFTW3 as example, it supports serial, OpenMP, pthreads and MPI. $\endgroup$
    – Vladimir F
    Jun 21 '14 at 17:28
  • $\begingroup$ Thank you for your response. I will pursue my answer in the above-mentioned appropriate forum. $\endgroup$
    – kfkhalili
    Jun 21 '14 at 17:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.