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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T17:24:52+00:00 2026-05-19T17:24:52+00:00

In a MPI program I want to transfer info to the workers but I

  • 0

In a MPI program I want to transfer info to the workers but I get a problem:

    Number of worker tasks = 4
   sending 1-th element q=0.011000 to task 1
   received 1-th element q=0.000000 in task 108827872
   sending 2-th element q=0.012000 to task 2
   sending 3-th element q=0.013000 to task 3
   received 2-th element q=0.000000 in task 1353735488
   sending 4-th element q=0.014000 to task 4
   received 3-th element q=0.000000 in task -1900924208
   received 4-th element q=0.000000 in task -1215863168

It is very likely I am not doing the right message passing, do you know where the error could be?

#include "mpi.h"               /* required MPI library */
#include <stdio.h>
#include <math.h>

#define NRRR 16               /* number of rows in matrix A */
#define NLLL 16                 /* number of columns in matrix A */
#define MASTER 0               /* taskid of first task */
#define FROM_MASTER 1          /* setting a message type */
#define FROM_WORKER 2          /* setting a message type */

int main(argc,argv)
int argc;
char *argv[];
{
    int numtasks,              /* number of tasks in partition */
    taskid,                /* a task identifier */
    numworkers,            /* number of worker tasks */
    source,                /* task id of message source */
    dest,                  /* task id of message destination */
    mtype,
    i,j,
    rc;                 /* message type */
    double  qr[NRRR],
    ql[NLLL],
    element_r[NRRR][3],
    element_l[NLLL][3];           


    MPI_Status status;  
    rc = MPI_Init(&argc,&argv);
    rc|= MPI_Comm_size(MPI_COMM_WORLD,&numtasks);   
    rc|= MPI_Comm_rank(MPI_COMM_WORLD,&taskid); 

    if (rc != 0)
        printf ("error initializing MPI and obtaining task ID info\n");

    numworkers = numtasks-1;

    // MASTER
    if (taskid == MASTER)
    {
        printf("\n\n\n\nNumber of worker tasks = %d\n",numworkers);

        // init element_r and element_l
        for(j=0;j<NRRR;j++){
            element_r[j][0]=j;
            element_r[j][1]=j+1;
            element_r[j][2]=j+2;
            qr[j] = j*1e-4+1e-3;
        }

        for(i=0;i<NLLL;i++){
            element_l[i][0]=12000+i;
            element_l[i][1]=12000+i+1;
            element_l[i][2]=12000+i+2;
            ql[i] = i*1e-3 +1e-2 ;
        }

        mtype = FROM_MASTER;
        for (dest=1; dest<=numworkers; dest++)
        {
            printf("   sending %d-th element q=%f to task %d\n",dest,ql[dest],dest);
            MPI_Send(&ql[dest], 1, MPI_DOUBLE, dest, mtype, MPI_COMM_WORLD);                        
        }

    }

    // WORKER
    if (taskid > MASTER)
    {
        mtype = FROM_MASTER;        
        MPI_Recv(&ql, 1, MPI_DOUBLE, MASTER, mtype, MPI_COMM_WORLD, &status);
        printf("   received %d-th element q=%f in task %d\n",taskid,ql,taskid);     

    }

    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-19T17:24:53+00:00Added an answer on May 19, 2026 at 5:24 pm

    Looking at your sends and receives, I don’t see anything wrong. Master is sending ql[N], to each worker N, and this is received and stored in ql[0].

    It looks like you’re just not printing it out correctly.

    I get a sensible output when I replace the worker loop with:

    // WORKER
    if (taskid != MASTER)
    {
        mtype = FROM_MASTER;
        MPI_Recv(&ql, 1, MPI_DOUBLE, MASTER, mtype, MPI_COMM_WORLD, &status);
        printf("   received %d-th element q=%f in task %d\n", taskid, ql[0], taskid);
    
    }
    

    Example run:

    $ mpiexec.exe -np 5 ./a.exe
    
    Number of worker tasks = 4
       sending 1-th element q=0.011000 to task 1
       sending 2-th element q=0.012000 to task 2
       received 1-th element q=0.011000 in task 1
       sending 3-th element q=0.013000 to task 3
       received 2-th element q=0.012000 in task 2
       sending 4-th element q=0.014000 to task 4
       received 3-th element q=0.013000 in task 3
       received 4-th element q=0.014000 in task 4
    

    As an aside, what you’re currently doing is what MPI_Scatter was designed for. Examples here.

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

Sidebar

Related Questions

i'm working with supercomputer, using MPI. but problem in.. C++ have a program, which
I currently have an MPI program written in C and I want to use
I had a problem with a program that uses MPI and I have just
I have a strange problem freeing allocated memory in my mpi program: Here is
I have an MPI program which compiles and runs, but I would like to
How much tcp connections will be used for sending data by MPI program if
I want to run Open MPI program in Xcode. I follow http://www.macresearch.org/compiling-running-and-debugging-mpi-programs-xcode tutorial. I
I'm writing an MPI program (Visual Studio 2k8 + MSMPI) that uses Boost::thread to
Ok, this is a really weird one. I have an MPI program, where each
I'm writing an MPI C program. I have troubles debugging it, because whenever I

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.