LAM/MPI logo

LAM/MPI General User's Mailing List Archives

  |   Home   |   Download   |   Documentation   |   FAQ   |   all just in this list

From: Yeliang Zhang (zhang_at_[hidden])
Date: 2005-11-02 04:53:02


Hi,

 

Is there a way to send a 2-D array at once instead of send one row by one
row? I wrote a code with 2 processors, processor 0 generates 2-D array and
send half of the rows to processor 1. I tested with 2x2 array, the result is
fine. But when I tested with 4x4 array. The last two numbers received in
processor 1 are 0.

 

I attach the code as following:

 

#include <stdio.h>

#include "mpi.h"

#include <stdlib.h>

#include <math.h>

#include <time.h>

 

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

 

 

int rank; /* Rank of process */

int size; /* Number of processes */

int source; /* Rank of sender */

int dest; /* Rank of receiver */

int tag = 51; /* Tag for messages */

 

int m,n,g;

int distr,cust;

int **in_set;

/*int in_set[20][20];*/

int sum,sum1;

int **R;

/*int R[20][20]*/

int x,y,xx,yy,z;

int tempx, tempy;

 int count;

 

int x1,y1,xx1,yy1,z1;

int tempx1, tempy1;

 

MPI_Status status; /* Return status for wait*/

MPI_Request request; /* Return request for send/receive*/

 

 

 MPI_Init(&argc, &argv);

 MPI_Comm_rank(MPI_COMM_WORLD, &rank);

 MPI_Comm_size(MPI_COMM_WORLD, &size);

 srand ( time(NULL) ); /*random seed used originally to get the input
file*/

 

 

 if (rank==0){

   

   printf("Enter number of rows x = ");

   scanf("%d",&n);

 

   while ((n%size) != 0) {

     printf("\nNumber of columns should be equally divided among
processors\n");

     printf("Please enter a new x: ");

     scanf("%d",&n);

   }

 

   printf("Enter number of columns y = ");

   scanf("%d",&m);

   

   

   distr = n / size;

 

   in_set = (int **)malloc(n * sizeof(int *));

   for(x=0; x < n; x++) {

     in_set[x] = (int *)malloc(m * sizeof(int));

   }

 

   for (x=0;x<n;x++){

     for (y=0;y<m;y++){

       in_set[x][y] = 1+ rand()/(((double)RAND_MAX + 1) / 8);

       printf("[%d][%d] = %d\n",x,y, in_set[x][y]);

     }

   }

   

   MPI_Send(&distr, 1, MPI_INT, 1,tag+29, MPI_COMM_WORLD);

   MPI_Send(&m, 1, MPI_INT, 1,tag+2, MPI_COMM_WORLD);

   MPI_Send(in_set[distr], (distr)*m, MPI_INT, 1,tag, MPI_COMM_WORLD);

 }

 

 if (rank!=0) {

   

   MPI_Recv(&distr, 1, MPI_INT, 0, tag+29, MPI_COMM_WORLD, &status);

   MPI_Recv(&m, 1, MPI_INT, 0, tag+2, MPI_COMM_WORLD, &status);

   

   R = (int **)malloc((int)distr * sizeof(int *));

 

   for(x1=0; x1<distr; x1++) {

     R[x1] = (int *)malloc((int)m * sizeof(int));

   }

 

   printf("myid = %d\tdistr = %d m = %d\n",rank, distr, m);

   

   MPI_Recv(*R, (distr)*m, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);

   MPI_Get_count(&status, MPI_INT, &count);

 

   printf("count = %d\n", count);

   

   printf("RECEIVED IN %d\n", rank);

   for (x1=0;x1<distr;x1++){

     for (y1=0;y1<m;y1++){

       printf("[%d][%d] = %d", x1, y1, R[x1][y1]);

     }

     printf("\n");

   }

   

 }

 

 MPI_Finalize();

 

}

 

The 4x4 array result is:

 

Enter number of rows x = 4

Enter number of columns y = 4

[0][0] = 2

[0][1] = 1

[0][2] = 7

[0][3] = 7

[1][0] = 5

[1][1] = 3

[1][2] = 7

[1][3] = 3

[2][0] = 4

[2][1] = 2

[2][2] = 1

[2][3] = 8

[3][0] = 5

[3][1] = 1

[3][2] = 6

[3][3] = 3

myid = 1 distr = 2 m = 4

count = 8

RECEIVED IN 1

[0][0] = 4[0][1] = 2[0][2] = 1[0][3] = 8

[1][0] = 5[1][1] = 1[1][2] = 0[1][3] = 0

 

Best wishes,

 

Yeliang Zhang