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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:05:37+00:00 2026-05-20T10:05:37+00:00

I have declared int value in my main, and all of the processes has

  • 0

I have declared int value in my main, and all of the processes has inicialized this value. All of them are storing value, which I want to write on the screen after computing is finished. Is Broadcast a solution? E.g. how to implement it?

int i;
int value;
  MPI_Status status;
  MPI_Init(&argc, &argv);
  MPI_Comm_size(MPI_COMM_WORLD,&numtasks);
  MPI_Comm_rank(MPI_COMM_WORLD;&myrank);

  left =  (myrank - 1); if (left < 0) left = numtasks-1;
  right = (myrank + 1); if (right >= numtasks) right = 0;

if(myrank==0){
     value=value+myrank;
      MPI_Send(value,MPI_INT,right,99,MPI_COMM_WORLD);
      MPI_Recv(value,MPI_INT,left,99,MPI_COMM_WORLD,&status);

  }
  else if(myrank==(numtasks-1)){

      MPI_Recv(value,MPI_INT,left,99,MPI_COMM_WORLD,&status);
      value=value+myrank;
      MPI_Send(value,MPI_INT,right,99,MPI_COMM_WORLD);


  }
  else{
      MPI_Recv(value,MPI_INT,left,99,MPI_COMM_WORLD,&status);
      value=value+myrank;
      MPI_Send(value,MPI_INT,right,99,MPI_COMM_WORLD);
  }

Theese should make logical circle. I do one computing (sum of all ranks), and in process 0 I get result. This result (for 4 processes it will be 6) I want to be printed by each of the processes after this computing. But I don’t see how to use barrier exactly and where.

There is also one thing, after all N-1 sendings (where N is number of processes) I should have sum of all ranks in each of processes. In my code I get this sum only into process 0… It might be a bad approach 🙁

  • 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-05-20T10:05:37+00:00Added an answer on May 20, 2026 at 10:05 am

    Some more detail about the structure of your code would help, but it sounds like you can just use MPI_Barrier. Your processes don’t need to exchange any data, they just have to wait until everyone reached the point in your code where you want the printing to happen, which is exactly what Barrier does.


    EDIT: In the code you posted, the Barrier would go at the very end (after the if statement), followed by printf(value).

    However, your code will not compute the total sum of all ranks in all nodes, since process i only receives the summed ranks of the first i-1 processes. If you want each process to have the total sum at the end, then replacing Barrier with Broadcast is indeed the best option. (In fact, the entire if statement AND the Broadcast could be replaced by a single MPI_Reduce() call, but that wouldn’t really help you learn MPI. 🙂 )

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

Sidebar

Related Questions

I have a scenario where a value is declared as int[] rate = new
I have a int variable witch holds the old battery value it is declared
I have declared a variable like this @{ int i = 1; } Now,
In C, I have declared a memory area like this: int cells = 512;
I have a Func in my code that's declared like this: Func<string, int, bool>
I have a class which should be declared globally from main() and accessed from
I have something like this: #include <iostream> #include <map> int main() { std::map<int, int*>
I have declared the following struct: typedef struct _RECOGNITIONRESULT { int begin_time_ms, end_time_ms; char*
I have declared: queue<int, list<int> > Q After a series of calls: Q.push(37); Q.pop();
I have declared three variables namely TemplateData tData; TaskInstance tInstance;int tID; in my program.

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.