无法使用MPI_Scatter和MPI_Reduce运行直方图

时间:2013-12-15 00:32:09

标签: c mpi

在使用MPI_Scatter和MPI_Reduce并行时,在程序运行时遇到错误。当数据不属于特定的bin时,它会退出。但它每次都在发生。我一直运行程序,并通过抛出“数据不属于bin”的错误退出。我不确定是因为MPI_Scatter或MPI_Reduce的错误位置。关于这个c代码有什么问题请帮帮我?我只是把它放在这里主程序。

int main(int argc, char* argv[]) {   
   int bin_count = 5;   
   int i, bin;  
   float min_meas = 1.00;  
   float max_meas = sizeof(float);  
   float* bin_maxes;  
   int* bin_counts;  
   int* bin_result;  
   int data_count = 100;  
   float* data;  
   int size = 1;  
   int rank = 0;  

  MPI_Init(&argc, &argv);  
 // note that argc and argv are passed by address  

  MPI_Comm_rank(MPI_COMM_WORLD,&rank);  
  MPI_Comm_size(MPI_COMM_WORLD,&size);  

 /* Allocate arrays needed */  
  bin_maxes = malloc(bin_count*sizeof(float));     
  bin_counts = malloc(bin_count*sizeof(int));  
  bin_result = malloc(bin_count*sizeof(int));  
  data = malloc(data_count*sizeof(float));  
  int count = (int)data_count/size;  

  if (rank == 0){  
  /* Generate the random data */  
  Gen_data(min_meas, max_meas, data, data_count);   

  }  

  MPI_Scatter(data,count,MPI_FLOAT,data,data_count,MPI_FLOAT,0,MPI_COMM_WORLD);  

 /* Create bins for storing counts */  
  Gen_bins(min_meas, max_meas, bin_maxes, bin_counts, bin_count);  

 /* Count number of values in each bin */  
 for (i = 0; i < count; i++) {  
    bin = Which_bin(data[i], bin_maxes, bin_count, min_meas); //decide data is belong to which bin  
    bin_counts[bin]++; //increments bin    
 }

 MPI_Reduce(bin_counts,bin_result,bin_count,MPI_INT,MPI_SUM,0,MPI_COMM_WORLD);  

 if(rank == 0) {  
    /* Print the histogram */  
    Print_histo(bin_maxes, bin_result, bin_count, min_meas); // prints the result  
 }  

 free(data);  
 free(bin_maxes);  
 free(bin_counts);  
 free(bin_result);  

 MPI_Finalize();  

 return 0;  

}  

0 个答案:

没有答案