
- •Блок-схема паралельного алгоритму Флойда
- •Лістинг паралельної програми для реалізації алгоритму Флойда
- •Int *source,*dest;
- •Int scount,rows_count;//кількість елементів для передачі
- •Int *my_d,*my_s,*my_Mas;//масиви для приймання, що міститимуть матриці d та s
- •Int *displs,*Scatter_scount;
- •Ifstream file("input.Txt");
- •If(!file){
- •Int *exeption1;
Int *displs,*Scatter_scount;
int my_k;
k_row = new int[n];
displs = new int[ProcNum];
Scatter_scount = new int[ProcNum];
//Процес обчислення матриць
for(int pcount=0;pcount<ProcNum;pcount++)
for(int k=0;k<rows_count;k++){
my_k=k+pcount*rows_count;
for(int i=0;i<ProcNum;i++){displs[i]=k*n;Scatter_scount[i]=n;}
MPI_Scatterv(my_D,Scatter_scount,displs,MPI_INT,k_row,
Scatter_scount[ProcRank],MPI_INT,pcount,cube_comm);
for(int i=0;i<rows_count;i++)
for(int j=0;j<n;j++){
if(my_D[i*n+my_k]>0 && k_row[j]>0){
Operator=my_D[i*n+my_k]+k_row[j];
if(my_D[i*n+j]!=-1 && Operator<my_D[i*n+j] || my_D[i*n+j]==0){
my_D[i*n+j]=Operator;
my_S[i*n+j]=my_k;}
}
}
}
delete(k_row);
delete(displs);
delete(Scatter_scount);
/*--------------------------------------------------------------------*/
/*-------ЧИТАННЯ З ФАЙЛА НОМЕРІВ ВЕРШИН ГРАФА,-------*/
/*----ДЛЯ ЯКИХ НЕОБХІДНО ЗНАЙТИ МІНІМАЛЬНИЙ ШЛЯХ----*/
Ifstream file("input.Txt");
char *tresh;
tresh=new char[11];//Допоміжна змінна для читання з фалу
//Якщо фала не існує, то необхідно його створити і вийти з програми
//адже користувач повинен заповнити цей файл
If(!file){
if(ProcRank==0){
cout<<"\n\nERROR: There are not file with improntent data(input.txt)\n";
cout<<"This file has been created now but you must input following data there:\n";
cout<<"Start node and end node\n";
cout<<"After that execute program again";
ofstream file1("input.txt");
file1<<"Start_node: 0\nEnd_node: 1\n\n";
file1.close();}
file.close();
MPI_Finalize();
return 1;}
//Читання з файлу необхідних даних
file>>tresh>>start_node>>tresh>>end_node;
//Врахуємо, що числення починається з нуля
start_node--;end_node--;
file.close();
if(start_node<0 || end_node<0 || start_node>=n || end_node>=n){
if(ProcRank==0){
cout<<"\n\nERROR: Start and end node must be in diapason 1.."<<n<<"\n";
cout<<"Please correct the values in the \"input.txt\" file”;
cout<<” and rerun program\n\n";
ofstream file1("input.txt");
file1<<"Start_node: 0\nEnd_node: 1\n\n";
file1.close();}
MPI_Finalize();
return 1;}
/*---------------------------------------------------*/
/*----УТВОРЕННЯ ПОВНИХ МАТИЦЬ ВІДСТАНЕЙ ТА ПОСЛІДОВНОСТЕЙ----*/
/*----------------ЗНАХОДЖЕННЯ НАЙКОРОТШОГО ШЛЯХУ-------------*/
//Збір всіх даних на 0-му процесі
MPI_Gather(my_D,scount,MPI_INT,D,scount,MPI_INT,root,cube_comm);
MPI_Gather(my_S,scount,MPI_INT,S,scount,MPI_INT,root,cube_comm);
delete(my_Mas);
delete(my_D);
delete(my_S);
//Оголошення необхідних змінних
Int *exeption1;
exeption1 = new int[1];
exeption1[0]=0;
//Перевірка на виникнення виняткових ситуацій:
//-користвувач ввів запит на пошук шляху між одним вузлом(вузол зєднує сам з //собою)
//-не існує шляху між вузлами(вони не звязані)
if(ProcRank==0){
if(D[start_node*n+end_node]==-1){
cout<<"\n\n["<<ProcRank<<"] ERROR: Nodes can't be connected with”; cout<<” himself.\n";
cout<<"Please retry with another nodes\n\n";
exeption1[0] = 1;}
else if(D[start_node*n+end_node]==0){
cout<<"\n\n["<<ProcRank<<"] INFO: Nodes are not connected\n\n";
exeption1[0] = 1;}
else{
cout<<"\n\n["<<ProcRank<<"] Min distance between node ("<<start_node+1;
cout<<") and node ("<<end_node+1<<") is "<<D[start_node*n+end_node];}
}
//Переривання програми у разі виникнення виняткової ситуації
MPI_Bcast(exeption1,1,MPI_INT,root,cube_comm);
if(exeption1[0]==1){MPI_Finalize();return 1;}
delete(exeption1);
//Пошук шляху між вузлами
if(ProcRank==0){
cout<<"\n["<<ProcRank<<"] Shortest path between nodes (in reverse order):";
int i=start_node;
int j=end_node;
while(1){
if(S[i*n+j]==j){
cout<<"\n"<<j+1<<"\n"<<i+1;
break;}
cout<<"\n"<<j+1;
j=S[i*n+j];}
time=MPI_Wtime()-time;
cout <<"\nTime of program processing - "<<time<<" s\n\n";
}
/*-----------------------------------------------------------*/
MPI_Finalize();
/*+++++++++++ЗАВЕРШЕННЯ ПАРАЛЕЛЬНОЇ ЧАСТИНИ++++++++++++++++++*/
return(0);
}