kupl_shm_peer_fence
win上两个进程间的同步。
接口定义
int kupl_shm_peer_fence(kupl_shm_win_h win, int remote_rank);
参数
参数名 |
类型 |
描述 |
输入/输出 |
---|---|---|---|
win |
kupl_shm_win_h |
需要同步的window对象 |
输入 |
remote_rank |
int |
需要同步的对端进程号 |
输入 |
返回值
- 成功:返回KUPL_OK
- 失败:返回KUPL_ERROR
示例
该示例需要以2进程运行
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | #include <stdio.h> #include <mpi.h> #include <unistd.h> #include "kupl.h" #define ITERS 5 // 创建 kupl 通信域需要的回调函数 1 static int oob_barrier_callback(void *group) { return MPI_Barrier((MPI_Comm)group); } // 创建 kupl 通信域需要的回调函数 2 static int oob_allgather_callback(const void *sendbuf, void *recvbuf, int size, void *group, kupl_shm_datatype_t datatype) { switch (datatype) { case KUPL_SHM_DATATYPE_CHAR: return MPI_Allgather(sendbuf, size, MPI_CHAR, recvbuf, size, MPI_CHAR, (MPI_Comm)group); default: fprintf(stderr, "not support datatype"); return KUPL_ERROR; } } int main(int argc, char *argv[]) { // 初始化 MPI 环境 MPI_Init(&argc, &argv); MPI_Comm comm = MPI_COMM_WORLD; // 获取 MPI 通信域大小 int world_size; MPI_Comm_size(comm, &world_size); // peer fence 演示,使用2进程运行 if (world_size != 2) { fprintf(stderr, "This demo only support two procs\n"); MPI_Abort(comm, -1); } // 获取进程 rank 号 int world_rank; MPI_Comm_rank(comm, &world_rank); // 获取进程 pid 号 int pid = getpid(); // 创建 kupl 通信域 kupl_shm_oob_cb_t oob_cbs; kupl_shm_oob_cb_h oob_cbs_h = &oob_cbs; oob_cbs_h->oob_allgather = oob_allgather_callback; oob_cbs_h->oob_barrier = oob_barrier_callback; kupl_shm_comm_h kupl_comm; int ret = kupl_shm_comm_create(world_size, world_rank, pid, oob_cbs_h, (void *)comm, &kupl_comm); if (ret != KUPL_OK || kupl_comm == NULL) { fprintf(stderr, "kupl shm comm create failed: %d\n", ret); return -1; } kupl_shm_win_h win; void *baseptr; size_t bufsize = sizeof(int); ret = kupl_shm_win_alloc(bufsize, kupl_comm, &baseptr, &win); if (ret != KUPL_OK) { fprintf(stderr, "kupl baseptr alloc failed: %d\n", ret); return -1; } ((int *)baseptr)[0] = 0; // 进程0和进程1做peer fence for (int i = 0; i < ITERS; i++) { ((int *)baseptr)[0]++; if (world_rank == 0) { kupl_shm_peer_fence(win, 1); usleep(100); } else { kupl_shm_peer_fence(win, 0); usleep(10000); } } kupl_shm_win_free(win); kupl_shm_comm_destroy(kupl_comm); MPI_Finalize(); return 0; } |

上述示例演示了使用kupl peer fence的流程。kupl_shm_peer_fence函数用于win中一对进程间的同步。
父主题: 共享内存通信函数