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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:13:41+00:00 2026-06-10T07:13:41+00:00

I have this serial code that I’m trying to convert to parallel using MPI.

  • 0

I have this serial code that I’m trying to convert to parallel using MPI. However I can’t seem to get the MPI_Scatter() function to work correctly without crashing. The function loops over an array called cells and modifies some of the values.

Below is the original serial code:

int accelerate_flow(const t_param params, t_speed* cells, int* obstacles)
{
  register int ii,jj;     /* generic counters */
  register float w1,w2;  /* weighting factors */
  /* compute weighting factors */
  w1 = params.density * params.accel * oneover9;
  w2 = params.density * params.accel * oneover36;

  int i;

  /* modify the first column of the grid */
  jj=0;

  for(ii=0;ii<params.ny;ii++)
  {

      if( !obstacles[ii*params.nx] && (cells[ii*params.nx].speeds[3] > w1 &&
          cells[ii*params.nx].speeds[6] > w2 && cells[ii*params.nx].speeds[7] > w2))  
      {
          /* increase 'east-side' densities */
          cells[ii*params.nx].speeds[1] += w1;
          cells[ii*params.nx].speeds[5] += w2;
          cells[ii*params.nx].speeds[8] += w2;
         /* decrease 'west-side' densities */
         cells[ii*params.nx].speeds[3] -= w1;
         cells[ii*params.nx].speeds[6] -= w2;
         cells[ii*params.nx].speeds[7] -= w2;
      }
  }

return EXIT_SUCCESS;

}

And here is my attempt at using MPI:

int accelerate_flow(const t_param params, t_speed* cells, int* obstacles, int myrank, int ntasks)
{
    register int ii,jj = 0;;     /* generic counters */
    register float w1,w2;  /* weighting factors */
    int recvSize;
    int cellsSendTag = 123, cellsRecvTag = 321;
    int size = params.ny / ntasks, i;
    MPI_Request* cellsSend, *cellsRecieve;
    MPI_Status *status;

    /* compute weighting factors */
    w1 = params.density * params.accel * oneover9;
    w2 = params.density * params.accel * oneover36;

    t_speed* recvCells = (t_speed*)malloc(size*sizeof(t_speed)*params.nx);

    MPI_Scatter(cells, sizeof(t_speed)*params.nx*params.ny, MPI_BYTE, recvCells, 
      size*sizeof(t_speed)*params.nx, MPI_BYTE, 0, MPI_COMM_WORLD);

    for(ii= 0;ii < size;ii++)
    {
        if( !obstacles[ii*params.nx] && (recvCells[ii*params.nx].speeds[3] > w1 &&
             recvCells[ii*params.nx].speeds[6] > w2 && recvCells[ii*params.nx].speeds[7] > w2))
        {

           /* increase 'east-side' densities */
           recvCells[ii*params.nx].speeds[1] += w1;
           recvCells[ii*params.nx].speeds[5] += w2;
           recvCells[ii*params.nx].speeds[8] += w2;
           /* decrease 'west-side' densities */
           recvCells[ii*params.nx].speeds[3] -= w1;
           recvCells[ii*params.nx].speeds[6] -= w2;
           recvCells[ii*params.nx].speeds[7] -= w2;
        }
   }

MPI_Gather(recvCells, size*sizeof(t_speed)*params.nx, MPI_BYTE, cells, params.ny*sizeof(t_speed)*params.nx, MPI_BYTE, 0, MPI_COMM_WORLD);

 return EXIT_SUCCESS;

}

And here is the t_speed structure:

typedef struct {
float speeds[NSPEEDS];
} t_speed;

params.nx = 300, params.ny = 200

Would greatly appreciate any help. Thanks.

  • 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-10T07:13:43+00:00Added an answer on June 10, 2026 at 7:13 am

    The first count argument to MPI_Scatter is the number of elements to send to each process, not in total. Here, the send count and the receive count will be the same, and will be nx*ny/ntasks; so you’d have something like

    int count=params.nx*params.ny/ntasks;
    
    MPI_Scatter(cells,    sizeof(t_speed)*count, MPI_BYTE, 
                recvCells,sizeof(t_speed)*count, MPI_BYTE, 0, MPI_COMM_WORLD);
    

    Note that this will only work when ntasks evenly divides nx*ny, otherwise you’ll have to use Scatterv.

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

Sidebar

Related Questions

I have some serial code like this that computes word concordances i.e. counting collocated
I have this code 'Open a file for reading 'Get a StreamReader class that
I have this code by how I can get data from a sqllite database
I have a table that looks like this: CREATE TABLE foobar ( id SERIAL
I have this set of code that adds a display:none css class to a
I have my code that reads and writes to a serial port written in
I have some SerialPort code that constantly needs to read data from a serial
I have the following python code that expects data coming from the serial port,
I have this code here where I am trying to save each field in
I am trying to setup a serial port on centos but can't get it

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.