/*
Matricefilenames:
  small matrix A.bin of dimension 100 × 50
  small matrix B.bin of dimension 50 × 100
  large matrix A.bin of dimension 1000 × 500
  large matrix B.bin of dimension 500 × 1000
An MPI program should be implemented such that it can
• accept two file names at run-time,
• let process 0 read the A and B matrices from the two data files,
• let process 0 distribute the pieces of A and B to all the other processes,
• involve all the processes to carry out the the chosen parallel algorithm
for matrix multiplication C = A * B ,
• let process 0 gather, from all the other processes, the different pieces
of C ,
• let process 0 write out the entire C matrix to a data file.
*/
int main(int argc, char *argv[]) {
  printf("Oblig 2 \n");
  double **matrixa;
  double **matrixb;
  int ma,na,my_ma,my_na;
  int mb,nb,my_mb,my_nb;
  int i,j,k;
  int myrank,numprocs;
  int konstanta,konstantb;
  MPI_Init(&argc,&argv);
  MPI_Comm_rank(MPI_COMM_WORLD,&myrank);
  MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
  if(myrank==0) {
    read_matrix_binaryformat ("small_matrix_A.bin", &matrixa, &ma, &na);
    read_matrix_binaryformat ("small_matrix_B.bin", &matrixb, &mb, &nb);
  }
  //mpi broadcast
  MPI_Bcast(&ma,1,MPI_INT,0,MPI_COMM_WORLD);
  MPI_Bcast(&mb,1,MPI_INT,0,MPI_COMM_WORLD);
  MPI_Bcast(&na,1,MPI_INT,0,MPI_COMM_WORLD);
  MPI_Bcast(&nb,1,MPI_INT,0,MPI_COMM_WORLD);
  fflush(stdout);
  int resta = ma % numprocs;//rest antall som har den største verdien
  //int restb = mb % numprocs;
  if (myrank == 0) {
    printf("ma : %d",ma);
    fflush(stdout);
    printf("mb : %d",mb);
    fflush(stdout); 
  } 
  MPI_Barrier(MPI_COMM_WORLD);
  if (resta == 0) {
    my_ma = ma / numprocs;
    printf("null rest\n ");
    fflush(stdout);
  } else {
    if (myrank < resta) {
      my_ma = ma / numprocs + 1;//husk + 1 
    } else {
      my_ma = ma / numprocs;    //heltalls divisjon gir nedre verdien !
    }
  }
  my_na = na;
  my_nb = nb;
  double **myblock = malloc(my_ma*sizeof(double*));
  for(i=0;i<na;i++) {
    myblock[i] = malloc(my_na*sizeof(double));
  }
  //send_cnt for scatterv
  //________________________________________________________________________________________________________________________________________________
  int* send_cnta = (int*)malloc(numprocs*sizeof(int));//array med antall elementer sendt til hver prosess array[i] = antall elementer , i er process
  int tot_elemsa = my_ma*my_na;
  MPI_Allgather(&tot_elemsa,1,MPI_INT,&send_cnta[0],1,MPI_INT,MPI_COMM_WORLD);//arrays i c må sendes &array[0]
  //send_disp for scatterv
  //__________________________________________________________________________________
    int* send_dispa = (int*)malloc(numprocs*sizeof(int)); //hvorfor trenger disp
    // int* send_dispb = (int*)malloc(numprocs*sizeof(int));
    //disp hvor i imagechars første element til hver prosess skal til
    fflush(stdout);
    if(resta==0) {
      send_dispa[myrank]=myrank*my_ma*my_na;
    } else if(myrank<=resta) {
      if(myrank<resta) {    
    send_dispa[myrank]=myrank*my_ma*my_na;
      } else {//my_rank == rest
    send_dispa[myrank]=myrank*(my_ma+1)*my_na;
    konstanta=myrank*(my_ma+1)*my_na;
      } 
    }
    MPI_Bcast(&konstanta,1,MPI_INT,resta,MPI_COMM_WORLD);
    if (myrank>resta){
      send_dispa[myrank]=((myrank-resta)*(my_ma*my_na))+konstanta;
    }
    MPI_Allgather(&send_dispa[myrank],1,MPI_INT,&send_dispa[0],1,MPI_INT,MPI_COMM_WORLD);
    //___________________________________________________________________________________
     printf("print2: %d" , myrank);
     fflush(stdout);
    //recv_buffer for scatterv
    double *recv_buffera=malloc((my_ma*my_na)*sizeof(double));
    MPI_Scatterv(&matrixa[0], &send_cnta[0], &send_dispa[0], MPI_UNSIGNED_CHAR, &recv_buffera[0], my_ma*my_na, MPI_UNSIGNED_CHAR, 0, MPI_COMM_WORLD);
    for(i=0; i<my_ma; i++) {
      for(j=0; j<my_na; j++) {
    myblock[i][j]=recv_buffera[i*my_na + j];
      }
    }
    MPI_Finalize();
    return 0;
}
OLD:我得到三种类型的错误。我可以得到 scatterv 计数错误、segmentationfault 11,或者进程卡住了。我得到的错误似乎是随机的。我每次使用 2 个 proc 运行代码。当它卡住时,它会在 printf("print2: %d" , myrank); 之前卡住。当我的朋友在他自己的计算机上运行代码时也使用两个程序,他没有通过第一个 MPI_Bcast。当他运行它时,什么都没有打印出来。这是我得到的错误的链接:http: //justpaste.it/zs0
更新的问题:现在我在 scatterv 调用之前在“ printf("print2: %d" , myrank); ”之后只得到一个分段错误。即使我在 printf 语句之后删除了所有代码,我也会遇到分段错误,但前提是我运行代码超过两个 procs。