博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++分布式计算(一)MPI主节点和子节点master&slaver计算
阅读量:7203 次
发布时间:2019-06-29

本文共 3789 字,大约阅读时间需要 12 分钟。

  hot3.png

#include 
#define WORKTAG 1#define DIETAG 2/* Local functions */static void master(void);static void slave(void);static unit_of_work_t get_next_work_item(void);static void process_results(unit_result_t result);static unit_result_t do_work(unit_of_work_t work);intmain(int argc, char **argv){ int myrank; /* Initialize MPI */ MPI_Init(&argc, &argv); /* Find out my identity in the default communicator */ MPI_Comm_rank(MPI_COMM_WORLD, &myrank); if (myrank == 0) { master(); } else { slave(); } /* Shut down MPI */ MPI_Finalize(); return 0;}static voidmaster(void){ int ntasks, rank; unit_of_work_t work; unit_result_t result; MPI_Status status; /* Find out how many processes there are in the default communicator */ MPI_Comm_size(MPI_COMM_WORLD, &ntasks); /* Seed the slaves; send one unit of work to each slave. */ for (rank = 1; rank < ntasks; ++rank) { /* Find the next item of work to do */ work = get_next_work_item(); /* Send it to each rank */ MPI_Send(&work, /* message buffer */ 1, /* one data item */ MPI_INT, /* data item is an integer */ rank, /* destination process rank */ WORKTAG, /* user chosen message tag */ MPI_COMM_WORLD); /* default communicator */ } /* Loop over getting new work requests until there is no more work to be done */ work = get_next_work_item(); while (work != NULL) { /* Receive results from a slave */ MPI_Recv(&result, /* message buffer */ 1, /* one data item */ MPI_DOUBLE, /* of type double real */ MPI_ANY_SOURCE, /* receive from any sender */ MPI_ANY_TAG, /* any type of message */ MPI_COMM_WORLD, /* default communicator */ &status); /* info about the received message */ /* Send the slave a new work unit */ MPI_Send(&work, /* message buffer */ 1, /* one data item */ MPI_INT, /* data item is an integer */ status.MPI_SOURCE, /* to who we just received from */ WORKTAG, /* user chosen message tag */ MPI_COMM_WORLD); /* default communicator */ /* Get the next unit of work to be done */ work = get_next_work_item(); } /* There's no more work to be done, so receive all the outstanding results from the slaves. */ for (rank = 1; rank < ntasks; ++rank) { MPI_Recv(&result, 1, MPI_DOUBLE, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status); } /* Tell all the slaves to exit by sending an empty message with the DIETAG. */ for (rank = 1; rank < ntasks; ++rank) { MPI_Send(0, 0, MPI_INT, rank, DIETAG, MPI_COMM_WORLD); }}static void slave(void){ unit_of_work_t work; unit_result_t results; MPI_Status status; while (1) { /* Receive a message from the master */ MPI_Recv(&work, 1, MPI_INT, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status); /* Check the tag of the received message. */ if (status.MPI_TAG == DIETAG) { return; } /* Do the work */ result = do_work(work); /* Send the result back */ MPI_Send(&result, 1, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD); }}static unit_of_work_t get_next_work_item(void){ /* Fill in with whatever is relevant to obtain a new unit of work suitable to be given to a slave. */}static void process_results(unit_result_t result){ /* Fill in with whatever is relevant to process the results returned by the slave */}static unit_result_tdo_work(unit_of_work_t work){ /* Fill in with whatever is necessary to process the work and generate a result */}

转载于:https://my.oschina.net/VenusV/blog/2874742

你可能感兴趣的文章
android 学习随笔二十五(动画:补间动画)
查看>>
如何查看IIS并发连接数
查看>>
Thinkphp 框架基础
查看>>
zabbix安装配置agent程序之agent配置文件详解
查看>>
团队绩效表
查看>>
python栈的两个应用
查看>>
(三)maven之一个基本的pom.xml
查看>>
maven第一个HelloWorld
查看>>
联想壁纸下载
查看>>
iOS导入第三方库步骤-CocoaPods
查看>>
Unique Encryption Keys
查看>>
hdu4311 n个平面点选取一个使距离该点曼哈顿距离最小 枚举/快速计算曼哈顿距离...
查看>>
Bow & Arrow 学习
查看>>
jQuery 取选中的radio的值方法
查看>>
[AaronYang]C#人爱学不学[7]
查看>>
一个程序员对微信小程序的看法
查看>>
非常好的理解遗传算法的例子
查看>>
ClassNotFoundException处理
查看>>
springboot文件的上传与下载
查看>>
代码之谜(零) - 其实,你不懂代码
查看>>