編寫乙個mpi程式,分別採用樹形和蝶形通訊結構計算全域性總和。首先計算通訊域comm_sz的程序數是2的冪的特殊情況,若能夠正確執行,改變該程式使其適用於comm_sz中任意程序數目的值。
與樹形結構不同的一點是,最後求得的和在每個執行緒中都存在#define _crt_secure_no_warnings
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using
namespace
std;
const
int numall = 100000; //要求和的數的數目
int ceil_log2(int n)
int tree_add(int argc, char **argv)
//mpi multi thread
int comm_sz;
int my_rank;
mpi_init(null, null);
mpi_comm_size(mpi_comm_world, &comm_sz);
mpi_comm_rank(mpi_comm_world, &my_rank);
//序列求和,用於驗證是否正確
if (my_rank == 0)
cout
<< "求得的和應為: "
<< test_sum << endl;
}//分派數字
int local_n = numall / comm_sz; //每個執行緒被分配到的數字的數目
if (numall %comm_sz > my_rank) local_n += 1;
int local_init_index = my_rank * local_n;
if (my_rank == numall % comm_sz) local_init_index = my_rank * (local_n + 1);
if (numall%comm_sz < my_rank) local_init_index = (numall / comm_sz + 1)*(numall%comm_sz) + (my_rank - numall % comm_sz)*local_n;
//對每個執行緒分配的數進行求和
int local_sum = 0;
for (int i = 0; i < local_n; i++)
//cout << my_rank << ": " << local_sum << endl;
mpi_barrier(mpi_comm_world);
//將每個執行緒求得的區域性和進行求和
int k = ceil_log2(comm_sz);
for (int i = 0; i < k; i++)
else
if(my_rank==send_p)
}mpi_barrier(mpi_comm_world);
}if (my_rank == 0) cout
<< local_sum << endl;
mpi_finalize();
return
0;}
蝶形結構主要用於分布式系統中,每個程序都希望獲得所有數的和,即每個程序都希望能得到最後的結果。而樹形結構的話只是將部分和集中,最後只有0號程序能獲得所有數字的總和。#define _crt_secure_no_warnings
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using
namespace
std;
const
int numall = 8; //要求和的數的數目
int ceil_log2(int n)
int main(int argc, char **argv) ;
//for (int i = 0; i < numall; i++)
//mpi multi thread
int comm_sz;
int my_rank;
mpi_init(null, null);
mpi_comm_size(mpi_comm_world, &comm_sz);
mpi_comm_rank(mpi_comm_world, &my_rank);
//序列求和,用於驗證是否正確
if (my_rank == 0)
cout
<< "求得的和應為: "
<< test_sum << endl;
}//分派數字
int local_n = numall / comm_sz; //每個執行緒被分配到的數字的數目
if (numall %comm_sz > my_rank) local_n += 1;
int local_init_index = my_rank * local_n;
if (my_rank == numall % comm_sz) local_init_index = my_rank * (local_n + 1);
if (numall%comm_sz < my_rank) local_init_index = (numall / comm_sz + 1)*(numall%comm_sz) + (my_rank - numall % comm_sz)*local_n;
//對每個執行緒分配的數進行求和
int local_sum = 0;
for (int i = 0; i < local_n; i++)
//cout << my_rank << ": " << local_sum << endl;
mpi_barrier(mpi_comm_world);
//將每個執行緒求得的區域性和進行求和,蝶形
int k = ceil_log2(comm_sz);
for (int i = 0; i < k; i++)
else
if (py == my_rank) }}
cout
<< my_rank << ": "
<< local_sum << endl;
mpi_barrier(mpi_comm_world);
}//cout << my_rank << ": " << local_sum << endl;
if (my_rank == 0) cout
<< local_sum << endl;
mpi_finalize();
return
0;}
MPI計算全域性總和(樹形與蝶形)
編寫乙個mpi程式,分別採用樹形和蝶形通訊結構計算全域性總和。首先計算通訊域comm sz的程序數是2的冪的特殊情況,若能夠正確執行,改變該程式使其適用於comm sz中任意程序數目的值。define crt secure no warnings include include include in...
雲計算 MPI程式設計
mpi是一種平行計算架構,mpich是mpi的一種實現,本集群使用虛擬機器安裝,作業系統是ubuntu14.04,使用三颱機器,使用者名稱都是ubuntu,機器名分別是ub0,ub1,ub2 測試程式 include mpi.h include include double f double dou...
平行計算MPI 二 MPI阻塞通訊
阻塞通訊 訊息傳送方的send需要接收方的recv配合才能完成。mpi對阻塞通訊一些底層函式進行了一些封裝,方便我們呼叫。1.標準通訊模式 mpi send buf,buf size,mpi char,process1,tag,mpi comm world mpi recv buf,buf size...