---
title: kpccl_shm_fence
description: "win上的进程间同步。"
url: https://www.hikunpeng.com/document/detail/zh/hpchistory/hpckit/develop2400/KunoengHPCKit_developer_061.html
sourcePath: /source/zh/hpchistory/hpckit/develop2400/KunoengHPCKit_developer_061.html
indexId: 697be404ccc21871a44b78b88c3b3a074fe7daff8d2d4ef64a6b90a5f9f5fa3373
---
# kpccl_shm_fence

win上的进程间同步。

#### 接口定义

int kpccl_shm_fence(kpccl_shm_win_h win)


#### 环境变量

使用环境变量 KPCCL_SHM_FENCE_ALGORITHM控制fence的底层实现。

可选值：

- rd（默认）
- linear


#### 参数


**表1 参数定义**

| 参数名 | 类型 | 描述 | 输入/输出 |
| --- | --- | --- | --- |
| win | kpccl\_shm\_win\_h | 需要同步的window对象 | 输入 |


#### 返回值

- 成功：返回KPCCL_OK
- 失败：返回KPCCL_ERROR


#### 示例

```
#include <stdio.h>
#include <mpi.h>
#include "kpccl.h"
int oob_allgather_callback(const void *sendbuf, void *recvbuf, int size, void *group，kpccl_shm_datatype_t datatype)
{
switch (datatype) {
case KPCCL_SHM_DATATYPE_CHAR:
return MPI_Allgather(sendbuf, size, MPI_CHAR, recvbuf, size, MPI_CHAR, (MPI_Comm)group);
case KPCCL_SHM_DATATYPE_INT:
return MPI_Allgather(sendbuf, size, MPI_INT, recvbuf, size, MPI_INT, (MPI_Comm)group);
case KPCCL_SHM_DATATYPE_LONG:
return MPI_Allgather(sendbuf, size, MPI_LONG, recvbuf, size, MPI_LONG, (MPI_Comm)group);
case KPCCL_SHM_DATATYPE_FLOAT:
return MPI_Allgather(sendbuf, size, MPI_FLOAT, recvbuf, size, MPI_FLOAT, (MPI_Comm)group);
case KPCCL_SHM_DATATYPE_DOUBLE:
return MPI_Allgather(sendbuf, size, MPI_DOUBLE, recvbuf, size, MPI_DOUBLE, (MPI_Comm)group);
default:
kpccl_error("not support datatype");
return KPCCL_ERROR;
}
}
int
oob_barrier_callback(
void
*
group)
{
return MPI_Barrier((MPI_Comm)group);
}
int main()
{
int myid, numprocs;
MPI_Comm comm = MPI_COMM_WORLD;
MPI_Comm_size(comm, &numprocs);
MPI_Comm_rank(comm, &myid);
kpccl_shm_oob_cb_t oob_cbs;
kpccl_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;
kpccl_shm_comm_h kpccl_comm;
kpccl_shm_comm_create(numprocs, myid, myid, oob_cbs_h, (
void
*)comm, &kpccl_comm);
kpccl_shm_win_h win;
void *baseptr, *otherptr;
kpccl_shm_win_alloc(65536, kpccl_comm, &baseptr, &win);
((int*)baseptr)[0] = 0;
MPI_Barrier(comm);
for (int i = 0; i < 5; i++) {
((int*)baseptr)[0]++;
kpccl_shm_fence
(win);
int cnt = 0;
if (myid == 0) {
while (cnt < 1000000) {
cnt++;
}
}
if (myid == 1) {
while (cnt < 10000) {
cnt++;
}
}
if (myid == 2) {
while (cnt < 100) {
cnt++;
}
}
}
MPI_Barrier(comm);
kpccl_shm_win_free(win);
kpccl_shm_comm_destroy(kpccl_comm);
}
```

上述示例演示了使用kpccl fence的流程。kpccl_shm_fence函数用于win中所有进程间的同步。
