MPI筆記(一)環境
MPI筆記(二)點對點通信
MPI筆記(三)集合通信
MPI筆記(四)資料類型和派生資料類型
MPI筆記(五)組和通信因子
MPI筆記(六)虛拟拓撲
MPI筆記(七)計算圓周率
/*
組和通信因子:
MPI_COMM_WORLD 是全局的通信因子
MPI_Comm_group (comm,*group) : 擷取與通信因子關聯的組句柄
MPI_Group_rank (group,*rank) : 傳回該程序在指定組中的秩
MPI_Group_size (group,*size) : 傳回該組的程序數
MPI_Group_excl (group,n,*ranks,*newgroup) : 從指定的組中淘汰一些程序後建立新組
MPI_Group_incl (group,n,*ranks,*newgroup) :從指定的組中提取程序産生新的組
MPI_Group_intersection (group1,group2,*newgroup) :擷取兩個組的交集程序,生成新的組
MPI_Group_union (group1,group2,*newgroup) :擷取兩個組的并集程序,生成新的組
MPI_Group_difference (group1,group2,*newgroup) :擷取兩個程序組中的不同程序,産生新的組
MPI_Group_compare (group1,group2,*result) :比較兩個組,如果兩個組中的成員和秩都一樣則傳回 MPI_IDENT,隻成員一樣傳回MPI_SIMILAR,否則傳回MPI_UNEQUAL
MPI_Group_free (group) :釋放一個使用者建立的組
MPI_Comm_create (comm,group,*newcomm) : 根據組和通信因子,建立一個新的通信因子
MPI_Comm_dup (comm,*newcomm) : 複制一個新的通信因子
MPI_Comm_compare (comm1,comm2,*result) : 該函數比較兩個通信因子, 找出兩個組内通信因子之間的關系。 如果兩個通信因子中的 上下文關系群組都一樣,則傳回 MPI_IDENT ,如果上下文關系相同,而組不相同則傳回 MPI_CONGRUENT ,如果上下文關系不相同,而組相同則傳回 MPI_SIMILAR。否則結果為 MPI_UNEQUEL
MPI_Comm_free (*comm) :釋放使用者建立的通信因子
*/
#include <iostream>
#include "mpi.h"
#include <Windows.h>
using namespace std;
#define NPROCS 8
int main_comm(int argc, char *argv[])
{
int rank, new_rank, sendbuf, recvbuf, numtasks,
ranks1[4] = { 0,1,2,3 }, ranks2[4] = { 4,5,6,7 };
MPI_Group orig_group, new_group;
MPI_Comm new_comm;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
if (numtasks != NPROCS) {
printf("Must specify MP_PROCS= %d. Terminating.\n", NPROCS);
MPI_Finalize();
exit(0);
}
sendbuf = rank;
/* Extract the original group handle */
MPI_Comm_group(MPI_COMM_WORLD, &orig_group); // 擷取與通信因子關聯的組句柄
/* Divide tasks into two distinct groups based upon rank */
if (rank < NPROCS / 2) {
MPI_Group_incl(orig_group, NPROCS / 2, ranks1, &new_group); // 從組中提取程序,産生新的組
}
else {
MPI_Group_incl(orig_group, NPROCS / 2, ranks2, &new_group);
}
/* Create new new communicator and then perform collective communications */
MPI_Comm_create(MPI_COMM_WORLD, new_group, &new_comm); // 根據通信因子群組生成新的通信因子
MPI_Allreduce(&sendbuf, &recvbuf, 1, MPI_INT, MPI_SUM, new_comm); // 發送資料到指定的通信因子
MPI_Group_rank(new_group, &new_rank); // 擷取目前程序在組中的秩
printf("rank= %d newrank= %d recvbuf= %d\n", rank, new_rank, recvbuf);
MPI_Finalize();
}