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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:11:03+00:00 2026-06-02T00:11:03+00:00

#include <stdio.h> #include <math.h> #include <mpi.h> int main(int argc, char *argv[]) { int i,

  • 0
#include <stdio.h>
#include <math.h>
#include <mpi.h>

int main(int argc, char *argv[])
{
    int i, done = 0, n;
    double PI25DT = 3.141592653589793238462643;
    double pi, tmp, h, sum, x;

    int numprocs, rank;
    MPI_Status status;

    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);

    if (numprocs < 1)
         printf("numprocs = %d, should be run with numprocs > 1\n", numprocs);
    else {
        while (!done)
        {
            if (rank == 0) {
                printf("Enter the number of intervals: (0 quits) \n");
                scanf("%d",&n);
            }
            if (n == 0) break;

            MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);

            h   = 1.0 / (double) n;
            sum = 0.0;
            for (i = 1 + rank; i <= n; i+=numprocs) {
                x = h * ((double)i - 0.5);
                sum += 4.0 / (1.0 + x*x);
            }

            MPI_Reduce(&sum, &tmp, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
            if (rank == 0) {
                pi = h * tmp;
                printf("pi is approximately %.16f, Error is %.16f\n", pi, fabs(pi - PI25DT));
            }
        }
    }
    MPI_Finalize();
}

I run this program with number of processes = 10 and after typing 0 in scanf(“%d”, &n); the program doesn’t seem to finalize and I have to close it with Ctrl+C.

10 total processes killed (some possibly by mpirun during cleanup)
mpirun: clean termination accomplished

Appears in console after killing process.

  • 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-02T00:11:04+00:00Added an answer on June 2, 2026 at 12:11 am

    Well, sure. Look at what’s going on here:

            if (rank == 0) {
                printf("Enter the number of intervals: (0 quits) \n");
                scanf("%d",&n);
            }
            if (n == 0) break;
    
            MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
    

    If you enter ‘0’ on rank 0, then rank 0 breaks, but all the other ranks have whatever they had in n last time around (or, in the case of the first iteration, whatever random stuff was in that memory location). So ranks 1-(N-1) enter the MPI_Bcast, patiently waiting to participate in a broadcast to find out what n is, and meanwhile rank 0 has exited the loop and is sitting at MPI_Finalize wondering where everyone else is.

    You just need to flip those lines:

            MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
            if (n == 0) break;
    

    Now everyone decides whether or not to break out of the loop at the same time.

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

Sidebar

Related Questions

In C++ C: Output: 1610612736 #include <math.h> #include <stdio.h> int main(int argc, char** argv)
#include<stdio.h> #include<time.h> int main() { clock_t start; double d; long int n,i,j; scanf(%ld,&n); n=100000;
#include<stdio.h> #include<string.h> #include<stdlib.h> int main() { // int char str[40],ch; FILE*fp,*fp1,*fp2; fp=fopen(ide_input,w); fp1=fopen(error_log,w); fp2=fopen(lex_output,w);
#include<stdio.h> #include<math.h> int main () { FILE *fp; fp=fopen(output,w); float t,y=0,x=0,e=5,f=1,w=1; for (t=0;t<10;t=t+0.01) {
Below is my code: #include <stdio.h> #include <math.h> void main () { int my_data[4][500];
#include <stdio.h> #include <math.h> int main (void) { float inches; printf(Enter the number of
#include <stdio.h> #include <math.h> /* converts to binary using logs */ int main() {
#include <stdio.h> #include <math.h> /* converts to binary */ int main() { unsigned int
#include <stdio.h> #include <string.h> #include <math.h> #define NUM 8 int main() { int i,
#include stdafx.h #include stdio.h #include math.h int main() { float c; printf(Type c); scanf(%f,

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.