MPI:从子进程渗透子数组以更新根/主进程中的主数组

时间:2015-09-06 22:57:07

标签: c++ parallel-processing mpi openmpi

我为相当长的代码道歉。我是MPI的新手,当我尝试并行化排序算法时遇到问题。我基本上有一大堆的整数,我必须通过划分为与从属进程相等的部分来更快地排序。完成从属进程后,他们必须将其子数组返回到根进程(已合并),这可以进行进一步处理。 因此:解决方案必须是使用半分类子阵列修补的全局数组。  我已经尝试在论坛中查看以前的问题,但我仍然坚持了一整天。请不要对我太过刻苦,因为我仍然是新的MPI。

void Batcher( char *inputFile,   string outputFile, int N  ){

   int initialised;
   MPI_Initialized(&initialised);
   //int rank;
   if (!initialised)
   {
      MPI_Init(NULL, NULL);
      atexit(finalized);
   }
   //
   //BEGIN : FILE READING FOR ARRAY
   //
   //Get number of processes
   int world_size;
   int root = 0;
   MPI_Comm_size(MPI_COMM_WORLD, &world_size);
   N=world_size;
   //Get the rank of the process
   int rank;
   MPI_Comm_rank(MPI_COMM_WORLD , &rank);

   int *global_array;
   int *sub_array_per_process;
   int element_count;

   if ( rank == root ){
       // ifstream input;
       ofstream output;
       std::ifstream input( inputFile, std::ifstream::in);
       //get number of integers in the file  so they may be populates to array
       int counter=0;
       string line;
       while( std::getline(input,line)){
          if ( line !="")          ++counter;
       }

       int numbers[counter];
       global_array = new int [counter];
       global_array = &numbers[0];


       int current;
       int index = 0;

       //reset read pointer to beginning of input file
       input.clear();
       input.seekg(0, ios::beg);
      //get number from inputfile and add to array numbers 
       while( input >> current){
          numbers[index]=current;
          index++;
          // cout<<((int) a);
       }
      global_array = numbers;

      for(int i=0; i<counter; i++)
          global_array[i]=numbers[i];//<<endl;

       for(int i=0; i<counter; i++)
          cout<<"global "<< global_array[i]<< " numbers " <<numbers[i] <<endl;


       element_count = counter; 
       input.close();


       /* 
       Send tasks to slaves  */

        int NON = element_count / (N - 1 );
        for(int i=root+1; i< world_size ; i++){
           int start = get_preceeding_ranks(i )*NON;
           //     cout<<"start is "<< start <<endl;
           MPI_Send(&global_array[start], NON, MPI_INT,i, 1 ,MPI_COMM_WORLD);

       }


      MPI_Status status;
      int temp[counter];

   } // End root process operation

    MPI_Bcast(&element_count, 1, MPI_INT,root,MPI_COMM_WORLD );

    int NON = element_count / (N - 1 );
     //Recieve local su-job from root
    if ( rank != root ){
       MPI_Status status;
       MPI_Recv(sub_array_per_process,NON, MPI_INT , root, 1 , MPI_COMM_WORLD ,&status );

    }
    int n_per_small_chunk = sizeof(sub_array_per_process) / sizeof(sub_array_per_process[0]) ;

    oddEvenMergeSort(sub_array_per_process,0, n_per_small_chunk);

    cout<<"After sorting processwise sorting.... "<<endl;
    if ( rank != root ){
         for(int i=0;i<NON;i++)
          cout<<"rank  : "<< rank <<"  data = "<< sub_array_per_process[ i] << endl;
    }
 //   MPI_Bcast(global_array, element_count , MPI_INT,root,MPI_COMM_WORLD );
 //sub_array_per_process = new int[2];

    MPI_Barrier(MPI_COMM_WORLD  ) ;

   if (rank == root ){
       int start ;
       int sender = -1;
       for ( int i= 0; i< N; i++){
            start = get_preceeding_ranks(i+1 )*NON;
             MPI_Status status;
       cout<<" start = "<<start<<endl;
       sender = i+1;
           MPI_Recv(&global_array[start], NON  , MPI_INT , sender , 1, MPI_COMM_WORLD ,&status );
           cout<<" Received  " << global_array[start] <<" from " << sender <<   endl
           ;
           // MPI_Bcast(global_array, elem , MPI_INT,root,MPI_COMM_WORLD );

        }
         for ( int j=0; j< element_count; j++ )  
             cout<<" Received  " << global_array[j] <<" from " <<sender <<   endl;

   }
   else    {  //Send to root your sub-array..
     // for (int j=1 ; j < N; i++ )
         for ( int i=0;i<NON; i++)
             MPI_Send(&sub_array_per_process[i], NON , MPI_INT,0, 1 ,MPI_COMM_WORLD);
  }

   MPI_Barrier(MPI_COMM_WORLD  ) ;

   for ( int j=0; j< element_count; j++ )  
     cout<<" iOutside  " << global_array[j] <<" from "<<   endl;



   MPI_Finalize();

}

int main() {
  string output;//Dummy

  char* inputFile ="inputsmall.txt";
  int N=0; //Dummy


  Batcher( inputFile,  output, N  );

  return 0 ;
}

由于代码很长,特别是我被困在这里: -

   if (rank == root ){
           int start ;
           int sender = -1;
           for ( int i= 0; i< N; i++){ //get all submitted sub-arrays from slaves. 
                start = get_preceeding_ranks(i+1 )*NON;
                 MPI_Status status;
           cout<<" start = "<<start<<endl;
           sender = i+1;
               MPI_Recv(&global_array[start], NON  , MPI_INT , sender , 1, MPI_COMM_WORLD ,&status );
               cout<<" Received  " << global_array[start] <<" from " << sender <<   endl
               ;


            }
             for ( int j=0; j< element_count; j++ )  
                 cout<<" Received  " << global_array[j] <<" from " <<sender <<   endl;

       }
       else    {  //Send to root your sub-array..
                for ( int i=0;i<NON; i++)
                 MPI_Send(&sub_array_per_process[i], NON , MPI_INT,0, 1 ,MPI_COMM_WORLD);
      }

我得到的输出是: -

     start = 2
 Received  2 from 1
 start = 4
 Received  3 from 2
 start = 6
 Received  4 from 3
 start = 8
[tux2:25061] *** Process received signal ***
[tux2:25061] Signal: Segmentation fault (11)
[tux2:25061] Signal code:  (128)
[tux2:25061] Failing at address: (nil)
--------------------------------------------------------------------------
mpirun noticed that process rank 0 with PID 25061 on node tux2 exited on signal 11 (Segmentation fault).
--------------------------------------------------------------------------

1 个答案:

答案 0 :(得分:3)

从您的代码中我可以看到,当rank等于root时,您将从从属进程接收已排序的数组。问题出在你的for循环中。您尝试从i+1N的所有流程接收,而您的Communicator只有N个进程(这是因为您已分配N = world_size)。因此,您的奴隶是1..N-1。因此,您需要更改for语句,如下所示:

for ( int i= 0; i< (N-1); i++){ //To receive data from process i=1 to i=N-1

另一方面,您的代码很难调试和管理。您会发现使用MPI_ScatterMPI_Gather很容易。看看this tutorial