C++ MPI:从从进程过滤子阵列以更新根/主进程中的主阵列
很抱歉代码太长了。我是MPI新手,在尝试并行排序算法时遇到了一些问题。我基本上有一个巨大的整数数组,我必须通过将它们分成相等的部分来从进程,从而加快排序速度。从属进程完成后,它们必须将其子数组返回到根进程(合并),根进程可以执行进一步的处理。 因此:解决方案必须是用半排序子数组修补的全局数组。 我已经试着在论坛上看了以前的问题,但是一整天我都在原地踏步。请不要对我太苛刻,因为我还是MPI新手C++ MPI:从从进程过滤子阵列以更新根/主进程中的主阵列,c++,parallel-processing,mpi,openmpi,C++,Parallel Processing,Mpi,Openmpi,很抱歉代码太长了。我是MPI新手,在尝试并行排序算法时遇到了一些问题。我基本上有一个巨大的整数数组,我必须通过将它们分成相等的部分来从进程,从而加快排序速度。从属进程完成后,它们必须将其子数组返回到根进程(合并),根进程可以执行进一步的处理。 因此:解决方案必须是用半排序子数组修补的全局数组。 我已经试着在论坛上看了以前的问题,但是一整天我都在原地踏步。请不要对我太苛刻,因为我还是MPI新手 void Batcher( char *inputFile, string outputFile,
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 ;
}
从您的代码中,我可以看到当
秩
等于根
时,您正在从从属进程接收排序的数组。问题在于您的for
循环。您正在尝试从i+1
到N
的所有进程接收,而您的通信器只有N
进程(这是因为您已分配N=world\u size
)。因此,您的从机是1..N-1
。因此,您需要将for
语句更改如下:
for ( int i= 0; i< (N-1); i++){ //To receive data from process i=1 to i=N-1
for(int i=0;i<(N-1);i++){//从进程i=1到i=N-1接收数据
另一则注释:你的代码很难调试和管理。你会发现使用<代码> MPIX散射和
和'MPI_聚集`做了我所需要的一切。你建议的教程类似于@Pooja建议:()谢谢,我似乎在重新发明轮子,做我自己的MPI_分散
和“MPI\u聚集”。尽管那个教程帮了我很大的忙,我还是去试试。
MPI\u散布
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).
--------------------------------------------------------------------------
for ( int i= 0; i< (N-1); i++){ //To receive data from process i=1 to i=N-1