Thread: How to display data after Scatter

  1. #1
    Registered User
    Join Date
    May 2019
    Posts
    1

    How to display data after Scatter

    Hi,
    I have used MPI_Scatter to dsitribute the data but I am not sure if the data has arrived at all the processes or not. I am trying to print the data at other processes, but I am not getting any value


    Code:
    #include <iostream>
    #include <cmath>
    #include <mpi.h>
    #include <fstream>
    #include <ctime>
    #include <vector>
    #define n  16
    int rank, size;
    
    //void GaussianElimination(double **,double *b ,double *y);
    int main(int argc, char * argv[])
    {
      double sTime, eTime, rTime;
      std::ifstream inFile;
      int num_rows = 4;
      int num_cols = 4;
      int num_processors =4;
      int cur_control = 0;
      double * send_buffer = NULL;
      double * recv_buffer = NULL;
      double ** data = NULL;
      double determinant;
      int irow =0; int icol=0; int iIndex =0;
      std::vector<double> file_buffer;
       
    
    
      double **M_A, *I_A, *I_B, *I_Y, *ARecv, *BRecv;
      double *Output, Pivot;
      I_B = NULL;
      I_B = new double[n];
      if(I_B == NULL){
        std::cout<< " I_A can't be allocated memory";
        return -2;
      }
      I_A = NULL;
      I_A = new double[n];
      if(I_A == NULL){
        std::cout<< " I_A can't be allocated memory";
        return -2;
      }
      I_Y = NULL;
      I_Y = new double[n];
      if(I_Y== NULL){
        std::cout<< " I_Y can't be allocated memory";
        return -2;
      }
      
     recv_buffer = new double[n];
     if(recv_buffer== NULL){
        std::cout<< " recv_buffer can't be allocated memory";
        return -2;
      }
    
      M_A = new double*[num_cols];
      for(int i = 0; i < num_cols; i++){
        M_A[i] = new double[num_rows];
        if(M_A[i]==NULL){
           std::cout<<"M_A can't be allocated";
           MPI_Finalize();
           return 0;
        }
      }
      for(int i = 0; i < num_cols; i++)
      {
        for(int j = 0; j < num_rows; j++)
          M_A[i][j] = 0;
      }
    
      // Just get the initialization of the program going.
      MPI_Init(&argc, &argv);
      MPI_Comm_rank(MPI_COMM_WORLD, &rank);
      MPI_Comm_size(MPI_COMM_WORLD, &size);
       if(!rank)
      { M_A[0][0] =2.0;M_A[0][1] =1.0; M_A[0][2] = -1.0; M_A[0][3] =2.0;  //A[0][3] = 12.0;
           M_A[1][0] =4.0;M_A[1][1] =5.0; M_A[1][2] =-3.0;   M_A[1][3] = 6.0;//A[1][3] = 0.0;
           M_A[2][0] =-2.0;M_A[2][1] =5.0; M_A[2][2] = -2.0; M_A[2][3]=6.0;//A[0][3] = -9;
           M_A[3][0] =4.0;M_A[3][1] =11.0; M_A[3][2] = -4.0; M_A[3][3]=8.0;//A[0][3] = -9;
           I_B[0] = 5; I_B[1] = 9; I_B[2] = 4; I_B[3]=2;
           for(irow=0; irow<num_rows; irow++)
                for(icol=0; icol<num_cols; icol++)
                  I_A[iIndex++] = M_A[irow][icol];
    
      }
      MPI_Bcast (&num_rows, 1, MPI_INT, 0, MPI_COMM_WORLD);
      int iChunkSize = num_rows/num_processors;
      MPI_Scatter(send_buffer, num_cols, MPI_DOUBLE, recv_buffer, num_cols, MPI_DOUBLE, 0, MPI_COMM_WORLD);//data goes to each process
      /*for(int i=0;i<num_rows;i++)
      {
         MPI_Bcast((int **)&(M_A[i][0]),num_rows,MPI_INT,0,MPI_COMM_WORLD);
      }*/
    
     if(rank!=0) {
     for (int i= 0; i <num_rows;i++)
        {
           std::cout<<recv_buffer[i];
            printf("\n");
        }
    }
       // MPI_Barrier(MPI_COMM_WORLD);*/
    
        delete [] I_Y;
        delete [ ]I_B;
        for(int i = 0; i < n; i++)
         delete [] M_A[i];
        delete [ ] M_A;// No need to delete A because A is not created dynamically
        MPI_Finalize();
        return 0;
    }
    Somebody please guide me what is the problem with my printing code. I am not getting any error at compile or run-time.

    Zulfi.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,490
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Allocating Scatter Gather in one call
    By expl0ited in forum C Programming
    Replies: 6
    Last Post: 10-08-2015, 02:27 PM
  2. MPI Scatter
    By johngoodman in forum C Programming
    Replies: 8
    Last Post: 05-02-2013, 09:48 PM
  3. Data Display Debugger-(ddd)
    By ggcook1 in forum Linux Programming
    Replies: 1
    Last Post: 12-31-2012, 12:49 AM
  4. draw a scatter plot?
    By nano_nasa in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2002, 09:00 AM
  5. How do I display data in the Right Window in MFC.
    By Jasonc in forum Windows Programming
    Replies: 2
    Last Post: 09-07-2002, 06:31 AM

Tags for this Thread