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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:23:55+00:00 2026-06-13T06:23:55+00:00

Why can’t I access muhray 8 lines from the bottom? The print lines that

  • 0

Why can’t I access muhray 8 lines from the bottom? The print lines that start with “!!” work correctly but I can’t seem to get the right values at the very end.

Here is my output:

[computer@node01 ~]$ mpiexec -n 8 ./presum 1000
!! proc0's array is size 125 and goes from 1 to 1
proc0's array is size 125 and goes from 4693173 to 1819307369
!! proc2's array is size 125 and goes from 1 to 1
proc2's array is size 125 and goes from 4693173 to 1819307369
!! proc3's array is size 125 and goes from 1 to 1
proc3's array is size 125 and goes from 4693173 to 1819307369
!! proc1's array is size 125 and goes from 1 to 1
proc1's array is size 125 and goes from 4693173 to 1819307369
!! proc4's array is size 125 and goes from 1 to 1
proc4's array is size 125 and goes from 4693173 to 1819307369
!! proc5's array is size 125 and goes from 1 to 1
proc5's array is size 125 and goes from 4693173 to 1819307369
!! proc6's array is size 125 and goes from 1 to 1
proc6's array is size 125 and goes from 4693173 to 1819307369
!! proc7's array is size 125 and goes from 1 to 1
proc7's array is size 125 and goes from 4693173 to 1819307369

Here is the code in question:

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

    //max size of the data array to split up
    #define MAXSIZE 1000000

    //methods
    int checkInput(int nprocs, int argc, char *argv[], int id);

    //mpi send & rec tags
    int ARSIZE = 0;     //array size
    int ARR = 1;        //array
    int MSM = 2;        //slave sum

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

        int     ARsize;             /*size of the array to pre-sum*/
        int     id;                 /*process id number*/
        int     nprocs;             /*number of processors*/
        int     i, j, k;            /*counters*/
        int     muhsize;            /*size of personal array to calculate*/
        int     * muhray;           /**/

        //MPI framework
        MPI_Status status;
        MPI_Init(&argc, &argv);
        MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
        MPI_Comm_rank(MPI_COMM_WORLD, &id);
        MPI_Barrier(MPI_COMM_WORLD);

        //pull input, check values, return ARsize
        ARsize = checkInput(nprocs, argc, argv, id);

        //set up array, serial run, send out chunks
        if (!id) {      

            //variables only the zero node needs
            int     data[ARsize];               /*full original array of numbers*/
            int     chunkSize, upper, lower;    /*vars to determine cunksize to send out*/
            int     smoothCount = 0;            /*BOOL for uneven division chunksize*/

            //fill array with numbers
            for (i = 0; i < ARsize; i++) {
                data[i] = 1;
            }

            //sequential solution here      


            //determine chunkSize
            chunkSize = (int) (ARsize/nprocs);  
            if (ARsize % nprocs != 0) {
                chunkSize = chunkSize + 1;
                smoothCount = 1;
            }

            //send chunks of data to procs
            for (i = 0; i < nprocs; i++) {          
                lower = i * chunkSize;
                upper = ((i+1) * chunkSize) - 1;
                if (i == nprocs-1 && smoothCount == 1) {
                    upper = ARsize-1;
                }
                int intarray[(upper-lower)];
                for (k = lower, j = 0; k <= upper; k++, j++) {
                    intarray[j] = data[k];   
                    }
                if(i > 0) {
                    //send array size
                    MPI_Send(&j, 1, MPI_INT, i, ARSIZE, MPI_COMM_WORLD);
                    //send actual array
                    MPI_Send(intarray, j, MPI_INT, i, ARR, MPI_COMM_WORLD); 
                }
                //zero no send to self, this data used later for all nodes calc
                else {
                    muhsize = j;
                    int muhray[muhsize];
                    for (j = 0; j <= chunkSize; j++) {
                        muhray[j] = intarray[j];
                    }   
                    printf("!! proc%d's array is size %d and goes from %d to %d\n", id, muhsize, muhray[0], muhray[(muhsize-1)]);
                }
            }       
        }
        else {

            MPI_Recv(&muhsize, 1, MPI_INT, 0, ARSIZE, MPI_COMM_WORLD, &status);
            int muhray[muhsize];
            MPI_Recv(muhray, muhsize, MPI_INT, 0, ARR, MPI_COMM_WORLD, &status);
            printf("!! proc%d's array is size %d and goes from %d to %d\n", id, muhsize, muhray[0], muhray[(muhsize-1)]);
            fflush(stdout);
        }

        printf("proc%d's array is size %d and goes from %d to %d\n", id, muhsize, muhray[0], muhray[muhsize]);
        fflush(stdout);
        //MPI_Send(&muhsize, 1, MPI_INT, 0, MSM, MPI_COMM_WORLD);       


        MPI_Finalize();

    }

//pull input, check values, return ARsize
int checkInput(int nprocs, int argc, char *argv[], int id) {

    int size;

    if (nprocs % 2 != 0 || nprocs == 6 || nprocs > 8) {
        if (!id) printf("run with 2^k procs, (1 >= k <= 3)\n");  
        fflush(stdout);
        MPI_Finalize();
        exit(1);
    }
    if (argc != 2) {
        if (!id) printf("Usage: presum [array size (max: %d)]\n", MAXSIZE);
        fflush(stdout);
        MPI_Finalize();
        exit(1);        
    }
    size = atoi(argv[1]);
    if (size <= nprocs) {
        if (!id) printf("search range must be greater than processor count\n");
        fflush(stdout);
        MPI_Finalize();
        exit(1);
    }
    if (size > MAXSIZE) {
        if (!id) printf("array size must be less than or equal to %d\n", MAXSIZE);
        fflush(stdout);
        MPI_Finalize();
        exit(1);
    }
    return size;
}
  • 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-13T06:23:56+00:00Added an answer on June 13, 2026 at 6:23 am

    The problem you have is very likely with scopes of variables. For instance here:

    ...
    else {
        MPI_Recv(&muhsize, 1, MPI_INT, 0, ARSIZE, MPI_COMM_WORLD, &status);
        int muhray[muhsize];
        MPI_Recv(muhray, muhsize, MPI_INT, 0, ARR, MPI_COMM_WORLD, &status);
        printf("!! proc%d's array is size %d and goes from %d to %d\n", id, muhsize, muhray[0], muhray[(muhsize-1)]);
        fflush(stdout);
    }
    
    printf("proc%d's array is size %d and goes from %d to %d\n", id, muhsize, muhray[0], muhray[muhsize]);
    

    you declare int muhray[muhsize]; inside the scope of the else construct. When you exit this scope muhray is destroyed as it is a local variable. What you are using in the last printf seems to be an uninitialized int * muhray; declared immediately after the main.

    Note that while they have the same name these two are different variables.

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

Sidebar

Related Questions

Can I change the field public virtual ClassOne ClassOne { get; set; } to
Can any one tell, how to get the result of LINQ query contains group
Can I call select before recv_from on a socket that is blocking?
Can I have a project that has some parts written in c and other
Can some one confirm me that only one UIWindow instance is possible in any
Can we change the color of the divider? Apple documentations says, that we can
Can someone please explain why this doesn't work? MyClass myClass1 = new MyClass(); object
Can't seem to figure out what's wrong with the simple getJSON call below. It's
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Can I prevent an .apk from being installed if unknown sources is checked?

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.