---
title: kupl_shm_comm_create
description: "创建kupl comm。"
url: https://www.hikunpeng.com/document/detail/zh/kunpenghpcs/hpckit/devg/KunpengHPCKit_developer_086.html
sourcePath: /source/zh/kunpenghpcs/hpckit/devg/KunpengHPCKit_developer_086.html
indexId: 8f052031380ab390b59c7a569465525ad0318f7a60392d945075efe1b8359b8c67
---
# kupl_shm_comm_create

创建kupl comm。

#### 接口定义

int kupl_shm_comm_create(int size, int rank, int pid, kupl_shm_oob_cb_h oob_cbs, void *group, kupl_shm_comm_h *comm);


#### 参数


**表1 参数定义**

| 参数名 | 类型 | 描述 | 输入/输出 |
| --- | --- | --- | --- |
| size | int | 需要创建的kupl通信域大小 | 输入 |
| rank | int | 当前进程的进程编号（从0开始的相对编码） | 输入 |
| pid | int | 当前进程的pid | 输入 |
| oob\_cbs | kupl\_shm\_oob\_cb\_h | 带外函数指针，详见表2 shm带外函数的结构体定义 | 输入 |
| group | void \* | 带外函数指针依赖的参数 | 输入 |
| comm | kupl\_shm\_comm\_h | 需要创建的kupl通信域 | 输出 |


**表2 shm带外函数的结构体定义**

| 参数名 | 类型 | 描述 |
| --- | --- | --- |
| oob\_allgather | int (\*kupl\_shm\_oob\_allgather\_cb\_t)(const void\*, void\*, int, void\*, kupl\_shm\_datatype\_t) | 带外allgather函数指针，用于kupl\_comm创建时进程相关信息的收集 |
| oob\_barrier | int (\*kupl\_shm\_oob\_barrier\_cb\_t)(void \*) | 带外barrier函数指针，确保kupl\_comm创建的正确性 |


#### 返回值

- 成功：返回KUPL_OK
- 失败：返回KUPL_ERROR


#### 示例

```
#include <stdio.h>
#include <mpi.h>
#include <unistd.h>
#include "kupl.h"

// 创建 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);
    // 获取进程 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_comm_destroy(kupl_comm);
    MPI_Finalize();
    return 0;
}
```

上述示例演示了创建、清理一个kupl comm的流程。kupl_shm_comm_create函数创建了一个kupl comm。
