Problem with conversion from 2D to 1D array using MPI

Hi,rray

I have written the following code to send the entire matrix to all processes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
#include <cmath>
#include <mpi.h>
#include <fstream>
#include <ctime>
#include <vector>
#define n  3
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_B 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;

      //2D to 1d array
       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;

  for(int i=0;i<num_rows;i++)
  {
     MPI_Bcast((int **)&(M_A[i][0]),num_rows,MPI_INT,0,MPI_COMM_WORLD);
  }*/
 /*for (int i= 0; i <num_rows;i++)
    {
        for(int j= 0; j < num_cols; j++)
            std::cout<<M_A[i][j];
        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;
}




However, I am getting following error. I found that this is related to the code performing 2D to 1D conversion.


mpic++ GES2.cpp
zulfi@lc2530hz:~/c programs/MPI_PROG/GE_SV$ ./a.out
[lc2] *** Process received signal ***
[lc2530] Signal: Segmentation fault (11)
[lc2530] Signal code: (128)
[lc2530] Failing at address: (nil)
[lc2530] [ 0] /lib/x86_64-linux-gnu/libc.so.6(+0x3ef20)[0x7ff6efdd0f20]
[lc2530] [ 1] ./a.out(+0xbe41)[0x559599e7be41]
[lc2530] [ 2] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xe7)[0x7ff6efdb3b97]
[lc2530] [ 3] ./a.out(+0xb78a)[0x559599e7b78a]
[lc2530] *** End of error message ***
Segmentation fault (core dumped)

Some body please guide me.

Zulfi.
Last edited on
This problem has been solved. value of 'n' was not correct.

Zulfi.
Registered users can post here. Sign in or register to post.