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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:33:00+00:00 2026-06-03T20:33:00+00:00

I have a 9×9 2D array. I fill the array with random numbers and

  • 0

I have a 9×9 2D array. I fill the array with random numbers and then send the 1st 3 rows of the array to process with rank=1, 2nd 3 rows of the array to process with rank=2 and at last, the 3rd 3 rows of the array to process with rank=3. I mean each process will get 3 rows of the array.

After the respective rows of the main array are received by the process, it will calculate the histogram of the 3 rows which it received from the root process. Then return the histogram back to the root process. All the 3 processes will do this task.

The problem is that the process with rank=3 can not receive the rows which are sent by the root process. I could not figure it out why?
I would be very grateful if someone have a look at my code and find a solution for my problem.

Thanks in advance.

I am using MPI with c. here is my code

#include "mpi.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <time.h>

int main(int argc, char* argv[])
{
int my_rank;
int p;
int i;
int k;
int m;
int tag=0;
int array[9][9];
int sub_array[3][9];    // 3 rows and 9 columns for each process
int c[20];

MPI_Status status;
//  MPI_Datatype MPI_INT;

MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&my_rank);
MPI_Comm_size(MPI_COMM_WORLD,&p);

//  MPI_Type_contiguous(9,MPI_INT,&MPI_INT);
//  MPI_Type_commit(&MPI_INT);
srand(time(NULL));

for(i=0; i<20 ; i++)
            c[i] = 0;


    if(my_rank == 0)
    {


    for(i=0; i<9 ; i++)

        for(k=0; k<9 ; k++)
        {
                array[i][k] = rand()%20+1;  // fill the array with random numbers from 1 to 20;
        }

        for(i=0; i<9 ; i++)
        {
            printf("\n");
            for(k=0; k<9 ; k++)
        {
                printf("%d ",array[i][k]); // prints the array here
        }
        }

        // here each process will be sent 3 rows of the array;
        for(i=0; i<3 ; i++)
                MPI_Send(&(array[i][0]),9,MPI_INT,1,tag,MPI_COMM_WORLD); // 1st 3 rows of the array are sent to process 1
        for(i=3; i<6 ; i++)
                MPI_Send(&(array[i][0]),9,MPI_INT,2,tag,MPI_COMM_WORLD); // 2nd 3 rows of the array are sent to process 2
        for(i=6; i<9 ; i++)
                MPI_Send(&(array[i][0]),9,MPI_INT,3,tag,MPI_COMM_WORLD); // 3rd 3 rows of the array are sent to process 3

        for (i=1;i<=3;i++)
        MPI_Recv(&sub_array, 9, MPI_INT,i, 0, MPI_COMM_WORLD, &status);  // root receives the subarrays from each node;
    }


if(my_rank != 0)
{
    // for the process with rank=1;
        for(i=0; i<3 ; i++)
        {
            MPI_Recv(&(sub_array[i][0]),9,MPI_INT,0,tag,MPI_COMM_WORLD,&status);

                for(k=0; k<9 ; k++)
                {
                    for(m=0 ; m<20 ; m++)       // here the process with rank=1 calculates the histogram
                    {
                        if(sub_array[i][k] == m)
                            c[m]++;
                    }
                }
        }       

        // for the process with rank=2
        for(i=3; i<6 ; i++)
        {
            MPI_Recv(&(sub_array[i][0]),9,MPI_INT,0,tag,MPI_COMM_WORLD,&status);

                for(k=0; k<9 ; k++)
                {
                    for(m=0 ; m<20 ; m++)  // here the process with rank=2 calculates the histogram
                    {
                        if(sub_array[i][k] == m)
                            c[m]++;
                    }
                }
        }
        // for the process with rank=3
        for(i=6; i<9 ; i++)
        {
            MPI_Recv(&(sub_array[i][0]),9,MPI_INT,0,tag,MPI_COMM_WORLD,&status);
            for(k=0; k<9 ; k++)

                {
                    for(m=0 ; m<20 ; m++)   // here the process with rank=3 calculates the histogram
                    {
                        if(sub_array[i][k] == m)
                            c[m]++;
                    }
                }
        }
}




    // here the histogram must be printed.
    for(k=0; k<20 ; k++)
    {
        printf("\n");
        printf("%2d : ",k);
        for(i=0 ; i<c[k] ; i++)
    {
        printf("=");
    }
    }

    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-03T20:33:02+00:00Added an answer on June 3, 2026 at 8:33 pm
    #include <stdio.h>
    #include <conio.h>
    #include <time.h>
    
    int main(int argc, char* argv)
    {
    int my_rank;
    int p;
    int Array[9];
    int localArray[3];
    int i;
    int k;
    int m;
    int a[9][9];
    int b[10][50];
    int c[20];
    
    MPI_Status status;
    MPI_Datatype my_type;
    
    MPI_Init(&argc,&argv);
    MPI_Comm_rank(MPI_COMM_WORLD,&my_rank);
    MPI_Comm_size(MPI_COMM_WORLD,&p);
    
    MPI_Type_contiguous(9,MPI_INT,&my_type);
    MPI_Type_commit(&my_type);
    srand(time(NULL));
    
        for(i=0; i<20 ; i++)
        c[i] = 0;
    
        if(my_rank == 0)
        {
    
        for(i=0; i<9 ; i++)
            for(k=0; k<9 ; k++)
            {
                    a[i][k] = rand()%20;
            }
    
            for(i=0; i<9 ; i++)
            {
                printf("\n");
                for(k=0; k<9 ; k++)
            {
                    printf("%d ",a[i][k]);
            }
            }
    
            for(i=0; i<3 ; i++)
                    MPI_Send(&(a[i][0]),1,my_type,1,0,MPI_COMM_WORLD);
            for(i=3; i<6 ; i++)
                    MPI_Send(&(a[i][0]),1,my_type,2,0,MPI_COMM_WORLD);
            for(i=6; i<9 ; i++)
                    MPI_Send(&(a[i][0]),1,my_type,3,0,MPI_COMM_WORLD);
        }
    
    
    if(my_rank != 0)
    {
            for(i=0; i<3 ; i++)
            {
                MPI_Recv(&(b[i][0]),1,my_type,0,0,MPI_COMM_WORLD,&status);
            }
            for(i=0; i<3 ; i++)
                for(k=0; k<9 ; k++)
                {
                    for(m=0 ; m<20 ; m++)
                    {
                        if(b[i][k] == m)
                            c[m]++;
                    }
                }
    
                printf("\n \n ");
                for(m=0 ; m<20 ; m++)
                    printf("%d ",c[m]);
    
    
    
    
    
            for(i=3; i<6 ; i++)
            {
                MPI_Recv(&(b[i][0]),1,my_type,0,0,MPI_COMM_WORLD,&status);
            }
    
            for(i=0; i<3 ; i++)
                for(k=0; k<9 ; k++)
                {
                    for(m=0 ; m<20 ; m++)
                    {
                        if(b[i][k] == m)
                            c[m]++;
                    }
                }
    
                printf("\n \n ");
                for(m=0 ; m<20 ; m++)
                    printf("%d ",c[m]);
    
            for(i=0; i<3 ; i++)
            {
                MPI_Recv(&(b[i][0]),1,my_type,0,0,MPI_COMM_WORLD,&status);
            }
            for(i=0; i<3 ; i++)
                for(k=0; k<9 ; k++)
                {
                    for(m=0 ; m<20 ; m++)
                    {
                        if(b[i][k] == m)
                            c[m]++;
                    }
                }
    
                printf("\n \n ");
                for(m=0 ; m<20 ; m++)
                    printf("%d ",c[m]);
    }
    
    
    
    
    
        for(k=0; k<20 ; k++)
        {
            printf("\n");
            printf("%2d : ",k);
            for(i=0 ; i<c[k] ; i++)
        {
            printf("=");
        }
        }
    
        MPI_Finalize();
     return 0;
    }
    

    @Colin D I edited the code above and it’s working now BUT the problem is that 3.processor doesn’t receive or 0.processor couldn’t send the rows.I didn’t achieve that.

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

Sidebar

Related Questions

I have got the following pulled from an array. [TheBeautifulSouth/2-15 One Last Love Song.m4a]
Have dict like: mydict= {'a':[],'b':[],'c':[],'d':[]} list like: log = [['a',917],['b', 312],['c',303],['d',212],['a',215],['b',212].['c',213],['d',202]] How do i
I have the following numpy array: # A B C Y my_arr = np.array([
suppose that we have three array int a[]=new int[]{4,6,8,9,11,12}; int b[]=new int[]{3,5,7,13,14}; int c[]=new
I have a column filled with data in those rows and i would like
I have a PHP array that I need to sort. I have included the
This is a php question. I have an array of arrays: Array ( [1]
I have a php array like this: [ ['url_id' => 2191238, 'time_spent' => 41],
I have a javascript code, containing an array with trailing comma items:[{ id: 'col-1',
I have a form and I need to fill in the options of a

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.