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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:40:18+00:00 2026-05-22T17:40:18+00:00

In my parallel programming book, I came across this code that says the slaves

  • 0

In my parallel programming book, I came across this code that says the slaves generate the data set, however, I think the master acutally generates the data set.

This line in particular is why I believe that master generates the data set.

for (i=0; i < ARRAY_SIZE; i++) 
        numbers[i] = i; 

Can someone confirm if master or slaves generate the data set?

#include "mpi.h" 
#include <stdio.h> 
#include <math.h> 
#include <stdlib.h> 

#define TRIALS 20 
#define ARRAY_SIZE 1000000 

int main(int argc, char *argv[]) 
{ 
    int myid, numprocs; 
    double startwtime, endwtime; 
    int namelen; 
    int* numbers = new int[ARRAY_SIZE]; 
    int i, j, sum, part_sum; 
    int s, s0, startIndex, endIndex; 
    double totalTime; 

    char processor_name[MPI_MAX_PROCESSOR_NAME]; 

    MPI_Init(&argc,&argv); 
    MPI_Comm_size(MPI_COMM_WORLD,&numprocs); 
    MPI_Comm_rank(MPI_COMM_WORLD,&myid); 
    MPI_Get_processor_name(processor_name,&namelen); 

    fprintf(stderr,"Process %d on %s\n", myid, processor_name); 
    fflush(stderr); 

    for (i=0; i < ARRAY_SIZE; i++) 
        numbers[i] = i; 

    if (myid == 0) 
    { 
        s = (int) floor(ARRAY_SIZE/numprocs); 
        s0 = s + ARRAY_SIZE%numprocs; 
        //printf("s=%d , s0= %d\n", s, s0); 
    } 

    MPI_Bcast(&s, 1, MPI_INT, 0, MPI_COMM_WORLD); 
    MPI_Bcast(&s0, 1, MPI_INT, 0, MPI_COMM_WORLD); 

    startIndex = s0 + (myid - 1)*s; 
    endIndex = startIndex + s; 

    totalTime = 0; 

    for (j = 1; j <= TRIALS; j++) 
    { 
        if (myid == 0) 
        { 
            startwtime = MPI_Wtime(); 
        } 

        sum = 0; 
        part_sum = 0; 

        if (myid == 0) // master 
        { 
            // compute sum of master's numbers 
            for (i = 0; i < s0; i++) 
            { 
                part_sum += numbers[i]; 
            } 
        } 
        else 
        { 
            for (i = startIndex; i < endIndex; i++) 
            { 
                part_sum += numbers[i]; 
            } 
        } 
        MPI_Reduce(&part_sum, &sum, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD); 
        if (myid == 0) 
        { 
            double runTime; 
            endwtime = MPI_Wtime(); 
            runTime = endwtime - startwtime; 

            printf("Trial %d : Execution time (sec) = %f\n", j, runTime); 
            printf("Sum = %d \n", sum); 
            totalTime += runTime; 
        } 
    } // end for 
    if (myid == 0) 
        printf("Average time for %d trials = %f", TRIALS, totalTime/TRIALS); 

    MPI_Finalize(); 
} 
  • 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-22T17:40:19+00:00Added an answer on May 22, 2026 at 5:40 pm

    Both the master and the slaves generate the entire array. You have to remember that your program runs on all nodes and the part of the code in question doesn’t distinguish between master/slave. So the wording of your book isn’t wrong, but it could be clarified. 🙂

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

Sidebar

Related Questions

I'm reading a book about parallel programming an it says that it's not thread
I copied this code exactly out of my parallel programming book. When I tried
I'm reading Multithreaded, Parallel, and Distributed Programming by Gregory Andrews, and in this book
I read this story on slashdot today where they announce a new parallel programming
I worked on an example in the text book about parallel programming in C#.
There has been a significant shift towards data-parallel programming via systems like OpenCL and
In the book Programming Massively Parallel Processors the number of gflops is used to
How do you write pseudo-code for parallel programming? Especially, how do you differentiate local
I'm reading a book called Pro .NET 4 Parallel Programming in C# by Adam
I am learning parallel programming by myself. I wonder if distributed memory is always

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.