Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8151385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:22:14+00:00 2026-06-06T15:22:14+00:00

I’m using gcc and OpenMPI . Usually I run MPI programs using the mpirun

  • 0

I’m using gcc and OpenMPI. Usually I run MPI programs using the mpirun wrapper — for example,

mpirun -np 4 myprogram

to start 4 processes.

However, I was wondering if it’s possible to easily generate a binary which will do that automatically (maybe with some hardcoded options like -np 4 above).

I know I can write a C wrapper that calls my program, such as the following:

#include <stdlib.h>
#include <unistd.h>

int main() {
        char *options[] = { "mpirun", "-np", "4", "myprogram" };

        execvp("mpirun", options);
        /* Ignoring return value to keep example simple */

        return EXIT_SUCCESS;
}

but this seems a bit clumsy and I end up with two executables instead of one.

I have tried to explicitly link the MPI libraries, like

gcc -o myprogram -I/usr/lib/openmpi/include/ \
    -lmpi -L/usr/lib/openmpi/lib/ myprogram.c

but the when I run resulting executable, MPI_Comm_size sets zero as the group size (as if I had given -np 0 as argument). Can I use an environment variable or something else to pass the group size? Or, is there another way to build a single-executable MPI program (using Linux and gcc)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T15:22:16+00:00Added an answer on June 6, 2026 at 3:22 pm

    If I get it correctly, you want a self-launching MPI executable. As I have written in my comment, you can go with a special option that makes your code execute mpirun if supplied, e.g. -launchmpi. With Open MPI it is even easier since it exports special environment variables to launched MPI processes, e.g. OMPI_COMM_WORLD_RANK. If this variable exists in the environment, then you know that the program was launched from mpirun and not directly. You can combine both methods in a single check this:

    int main (int argc, char **argv)
    {
        int perform_launch = 0;
        // Scan argv[] for special option like "-launchmpi"
        // and set perform_launch if found 
    
        if (perform_launch || getenv("OMPI_COMM_WORLD_RANK") == NULL)
        {
            // #args = argc + 3 ("mpirun -np 4" added) + NULL
            // #args should be reduced by one if "-launchmpi" is present
            char **args = (char **)calloc(
               argc + (perform_launch ? 3 : 4),
               sizeof(char *));
            args[0] = "mpirun";
            args[1] = "-np";
            args[2] = "4";
            // Copy the entire argv to the rest of args but skip "-launchmpi"
    
            execvp("mpirun", args);
    
            return EXIT_SUCCESS;
        }
    
        // Proceed as regular MPI code
        MPI_Init(&argc, &argv);
        ...
        // Magic happens here
        ...
        MPI_Finalize();
    
        return EXIT_SUCCESS;
    }
    

    If you’d like to control the number of processes in the MPI job, you can supply it as an additional arugment, e.g. -launchmpi 12, or in an environment variable and use its value instead of "4" in the above code.

    Note that MPI executables cannot be generally launched without mpirun. The latter is an integral part of the MPI run-time and it does much more that just launching multiple copies of the MPI executable. Also you are always linking explicitly to the MPI library when compiling with any of the MPI compiler wrappers (try mpicc -showme). Although you can link MPI libraries statically (not recommended, see here), you will still need mpirun in order to be able to run MPI jobs – AFAIK there is no way to embed mpirun functionality in your program, at least not in Open MPI.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using parse_ini_file to read the contents of a file however it is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Specifically, suppose I start with the string string =hello \'i am \' me And
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I would like to run a str_replace or preg_replace which looks for certain words
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.