M. Dennis
M. Dennis

Reputation: 53

When running MPI is there a way to execute sequential code first and then run the parallel code

I am writing code and I need to initialize variables and execute a series of tasks sequentially and only once. Following that, I am currently using pthreading to run parallel processes before returning to sequential code to sort the data and perform a binary search. The parallel processes are simply a many reads from multiple (roughly 35) sockets simultaneously. I have been asked to implement the same setup using MPI, and my current understanding is if I use MPI every single process will execute the entire program and the parallal processing is not confined to the lines where MPI_Init_thread and MPI_Finalize are placed. I my understanding correct? Or is there a way to execute sequential code before and after parallel code while using MPI?

Upvotes: 2

Views: 1735

Answers (1)

bembas
bembas

Reputation: 771

  • MPI_Init is called prior to any calls to other MPI routines. Its purpose is to initialize the MPI environment.
  • MPI_Finalize is called at the end of the computation, and it performs various clean-up tasks to terminate the MPI environment.
  • MPI_Init also strips off any MPI related command-line arguments.

If you want to execute sequential code you can do

 if (myrank == 0){ 
 //code
 }

and the main thread (with id = 0) will execute the code alone so you ll have no parralelism in that spot.

Upvotes: 3

Related Questions