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 8936441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:12:48+00:00 2026-06-15T10:12:48+00:00

I’m trying to send from process with rank = 0 to all other process

  • 0

I’m trying to send from process with rank = 0 to all other process (1,2,3,4) a value from the vector n_pointsBuffer. The problem is that just process 0 gets its value but the others don’t. Can anyone explain to me if it’s possible what I’m trying to do, if so, how? It’s the first time I use MPI.

#include <mpi.h>
#include <iostream>
#include <cstdlib>
#include <cmath>

using namespace std;

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

MPI::Init(argc, argv);

int num_procs = MPI::COMM_WORLD.Get_size();
int rank = MPI::COMM_WORLD.Get_rank();

srand(getpid());
int n_points;

if (rank == 0) {
    int n_pointsBuffer[] = { 1000000, 1203100, 1231230, 1231000, 1312322 };
    MPI::COMM_WORLD.Scatter(n_pointsBuffer, 1, MPI::INT, &n_points, 1,
            MPI::INT, 0);
}

cout << "Rank = " << rank << ", n_points = " << n_points << "\n";

double sum = 0;

for (int i = 0; i < n_points; i++) {
    double x = rand() / ((double) (RAND_MAX));
    double f = 1.0 / (1.0 + x * x);
    sum += f;
}

double avg_sum = 0;
MPI::COMM_WORLD.Reduce(&sum, &avg_sum, 1, MPI::DOUBLE, MPI::SUM, 0);

if (rank == 0) {
    double pi = 4.0 * (avg_sum / num_procs / ((double) (n_points)));
    cout << "Pi is approx " << pi << " with error " << pi - M_PI << ".\n";
}

MPI::Finalize();

return 0;
}
  • 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-15T10:12:49+00:00Added an answer on June 15, 2026 at 10:12 am

    This is because everyone has to call Scatter(). Not just the root. Quoting from another Answer :

    The MPI_Scatter function contains both send and recieve logic. The
    root process (specified here as 0) sends out the data, and all the
    recievers recieve; everyone participating has to call the routine.
    Scatter is an example of an MPI Collective Operation, where all tasks
    in the communicator have to call the same routine. Broadcast, barrier,
    reduction operations, and gather operations are other examples.

    Please note that the same holds for every collective operation. An example of using Broadcast is here (You can also find some other programs in the same repo, these were written by me to test my MPI cluster made of dumped machines) .

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I know there's a lot of other questions out there that deal with this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I am trying to understand how to use SyndicationItem to display feed which is

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.