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

  • Home
  • SEARCH
  • 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 9246461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:29:40+00:00 2026-06-18T09:29:40+00:00

I have two programs. The master which spawns workers which perform some calculations and

  • 0

I have two programs. The “master” which spawns “workers” which perform some calculations and I want the master to get the results from the workers and store the sum. I am trying to use MPI_Reduce to collect the results from the workers, and the workers use MPI_Reduce to send to the masters MPI_Comm. I am not sure if that is correct. Here are my programs:

Master:

#include <mpi.h>
#include <iostream>
using namespace std;

int main(int argc, char *argv[]) { 
    int world_size, universe_size, *universe_sizep, flag; 

    int rc, send, recv;

    // intercommunicator
    MPI_Comm everyone;

    MPI_Init(&argc, &argv); 
    MPI_Comm_size(MPI_COMM_WORLD, &world_size); 

    if (world_size != 1) {
        cout << "Top heavy with management" << endl;
    } 

    MPI_Attr_get(MPI_COMM_WORLD, MPI_UNIVERSE_SIZE, &universe_sizep, &flag);  
    if (!flag) { 
        cout << "This MPI does not support UNIVERSE_SIZE. How many processes total?";
        cout << "Enter the universe size: ";
        cin >> universe_size; 
    } else {
        universe_size = *universe_sizep;
    }
    if (universe_size == 1) {
        cout << "No room to start workers" << endl;
    }

    MPI_Comm_spawn("so_worker", MPI_ARGV_NULL, universe_size-1,  
             MPI_INFO_NULL, 0, MPI_COMM_SELF, &everyone,  
             MPI_ERRCODES_IGNORE);

    send = 0;

    rc = MPI_Reduce(&send, &recv, 1, MPI_INT, MPI_SUM, 0, everyone);

    // store result of recv ...
    // other calculations here
    cout << "From spawned workers recv: " << recv << endl;

    MPI_Finalize(); 
    return 0; 
}

Worker:

#include <mpi.h>
#include <iostream>
using namespace std;

int main(int argc, char *argv[]) { 

    int rc, send,recv;


    int parent_size, parent_id, my_id, numprocs; 
    // parent intercomm
    MPI_Comm parent; 
    MPI_Init(&argc, &argv); 

    MPI_Comm_get_parent(&parent); 
    if (parent == MPI_COMM_NULL) {
        cout << "No parent!" << endl;
    }
    MPI_Comm_remote_size(parent, &parent_size); 
    MPI_Comm_rank(parent, &parent_id) ; 
    //cout << "Parent is of size: " << size << endl;
    if (parent_size != 1) {
        cout << "Something's wrong with the parent" << endl;
    }

    MPI_Comm_rank(MPI_COMM_WORLD, &my_id) ;     
    MPI_Comm_size(MPI_COMM_WORLD, &numprocs) ;  

    cout << "I'm child process rank "<< my_id << " and we are " << numprocs << endl;
    cout << "The parent process rank "<< parent_id << " and we are " << parent_size << endl;

    // get value of send
    send = 7; // just an example
    recv = 0;

    rc = MPI_Reduce(&send, &recv, 1, MPI_INT, MPI_SUM, parent_id, parent);
    if (rc != MPI_SUCCESS)
        cout << my_id << " failure on mpi_reduce in WORKER" << endl;

    MPI_Finalize(); 
    return 0; 
} 

I compiled both and execute like this (mpic++ for osx):

mpic++ so_worker.cpp -o so_worker
mpic++ so_master.cpp -o so_master
mpirun -n 1 so_master

Is this the correct way to run a master that spawns the workers?

In the master I always get 0 back from the MPI_Reduce. Can I use MPI_reduce from intercommunicators or should I use MPI_Send from workers and MPI_Recv from master? I’m really not sure why it’s not working.

Any help would be appreciated. Thanks!

  • 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-18T09:29:41+00:00Added an answer on June 18, 2026 at 9:29 am

    MPI_Comm_get_parent returns the parent intercommunicator that encompasses the original process and all the spawned ones. In this case calling MPI_Comm_rank(parent, &parent_id) does not return the rank of the parent but rather the rank of the current process in the local group of the intercommunicator:

    I'm child process rank 0 and we are 3
    The parent process **rank 0** and we are 1
    I'm child process rank 1 and we are 3
    The parent process **rank 1** and we are 1
    I'm child process rank 2 and we are 3
    The parent process **rank 2** and we are 1
    

    (observe how the highlighted values differ – one would expect that the rank of the parent process should be the same, shouldn’t it?)

    That’s why the MPI_Reduce() call would not succeed as all worker processes specify different values for the root rank. Since originally there was one master process, its rank in remote group of parent would be 0 and hence all workers should specify 0 as the root to MPI_Reduce:

    //
    // Worker code
    //
    rc = MPI_Reduce(&send, &recv, 1, MPI_INT, MPI_SUM, 0, parent);
    

    This is only half of the problem. The other half is that rooted collective operations (e.g. MPI_REDUCE) operate a bit different with intercommunicators. One first has to decide which of the two groups would host the root. Once the root group is identified, the root process has to pass MPI_ROOT as the value of root in MPI_REDUCE and all other processes in the root group must pass MPI_PROC_NULL. That is the processes in the receiving group do not take part in the rooted collective operation at all. Since the master code is written so that there could be only one process in the master’s group, then it would suffice to change the call to MPI_Reduce in the master code to:

    //
    // Master code
    //
    rc = MPI_Reduce(&send, &recv, 1, MPI_INT, MPI_SUM, MPI_ROOT, everyone);
    

    Note that the master also does not participate in the reduction operation itself, e.g. the value of sendbuf (&send in this case) is irrelevant as the root would not be sending data to be reduced – it merely collects the result of the reduction performed over the values from the processes in the remote group.

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

Sidebar

Related Questions

I have two programs, Writer and Reader. I have a FIFO from Writer to
I have two programs, the first is a producer: public class Producer { public
I have two programs: one CLI program, and one GUI. The GUI is a
I have two begininer programs, both using the 'while' function, one works correctly, and
I'm going to have two independent programs (using SqlAlchemy / ORM / Declarative) that
I have two very similar programs each trying to run two threads OddThread and
I have the following two programs: long startTime = System.currentTimeMillis(); for (int i =
I need an XML-serializable dictionary. Actually, I now have two quite different programs that
I've got a table recording views of programs. Each program can have two different
I am developing two java programs that run in separate VM's that have a

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.