MPI前向声明的标头

时间:2019-10-21 22:51:23

标签: c++ header dependencies

我正在使用一个简短的MPI包装器,如下所示:

grid = []
for row in range(0, 4):
    for col in range(0, 4):
        grid.append('-')
    grid.append("")
' '.join(grid)

我想在这里摆脱对grid = [] for row in range(0, 4): grid.append(['-']*4) 的依赖,因此我试图添加一个#ifndef INC_MyLib_MPIWRAPPER_H #define INC_MyLib_MPIWRAPPER_H #include <mpi.h> /* libMyLib MPI struct & macros */ namespace libMyLib{ extern int mpi_rank; extern int mpi_nranks; extern int mpi_msg; extern int mpi_msg_; extern MPI_Comm mpi_active_comm; } #define MPI_INIT MPI_Init(&argc, &argv); MPI_Comm_size( libMyLib::mpi_active_comm, &libMyLib::mpi_nranks ); MPI_Comm_rank( libMyLib::mpi_active_comm, &libMyLib::mpi_rank ); #define MPI_INIT_NULL MPI_Init(nullptr, nullptr); MPI_Comm_size( libMyLib::mpi_active_comm, &libMyLib::mpi_nranks ); MPI_Comm_rank( libMyLib::mpi_active_comm, &libMyLib::mpi_rank ); #define MPI_FINISH MPI_Finalize( ); #define MPI_INTERRUPT libMyLib::mpi_msg = 1; MPI_Allreduce(&libMyLib::mpi_msg, &libMyLib::mpi_msg_, 1, MPI_INT, MPI_SUM, libMyLib::mpi_active_comm); #define MPI_ERROR_CHECK libMyLib::mpi_msg = 0; MPI_Allreduce(&libMyLib::mpi_msg, &libMyLib::mpi_msg_, 1, MPI_INT, MPI_SUM, libMyLib::mpi_active_comm);; if( libMyLib::mpi_msg_ > 0 ){throw std::runtime_error(" eror " );} #endif //INC_MyLib_MPIWRAPPER_H 类型的前向声明:

mpi.h

问题是,在一个代码中,我需要将此包装器与真正的MPI标头结合起来,并且出现如下错误:

#ifndef INC_MyLib_MPIWRAPPER_H
#define INC_MyLib_MPIWRAPPER_H

#include <mpi.h>

/* MPI forward declarations to remove MPI header dependency from API */
typedef int MPI_Comm;
typedef int MPI_Datatype;
#define MPI_INT ((MPI_Datatype)0x4c000405)

typedef int MPI_Op;
#define MPI_SUM     (MPI_Op)(0x58000003)
int MPI_Allreduce(const void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm);

#if defined(HAVE_VISIBILITY)
#define MPICH_API_PUBLIC __attribute__((visibility ("default")))
#else
#define MPICH_API_PUBLIC
#endif
int MPI_Finalize(void) MPICH_API_PUBLIC;
int MPI_Comm_size(MPI_Comm comm, int *size) MPICH_API_PUBLIC;
int MPI_Comm_rank(MPI_Comm comm, int *rank) MPICH_API_PUBLIC;
int MPI_Init(int *argc, char ***argv) MPICH_API_PUBLIC;

/* libMyLib MPI struct & macros */
namespace libMyLib{
    extern int mpi_rank;
    extern int mpi_nranks;
    extern int mpi_msg;
    extern int mpi_msg_;
    extern MPI_Comm mpi_active_comm;
}

#define MPI_INIT MPI_Init(&argc, &argv); MPI_Comm_size( libMyLib::mpi_active_comm, &libMyLib::mpi_nranks ); MPI_Comm_rank( libMyLib::mpi_active_comm, &libMyLib::mpi_rank );
#define MPI_INIT_NULL MPI_Init(nullptr, nullptr); MPI_Comm_size( libMyLib::mpi_active_comm, &libMyLib::mpi_nranks ); MPI_Comm_rank( libMyLib::mpi_active_comm, &libMyLib::mpi_rank );
#define MPI_FINISH MPI_Finalize( );

#define MPI_INTERRUPT libMyLib::mpi_msg = 1; MPI_Allreduce(&libMyLib::mpi_msg, &libMyLib::mpi_msg_, 1, MPI_INT, MPI_SUM, libMyLib::mpi_active_comm);
#define MPI_ERROR_CHECK libMyLib::mpi_msg = 0; MPI_Allreduce(&libMyLib::mpi_msg, &libMyLib::mpi_msg_, 1, MPI_INT, MPI_SUM, libMyLib::mpi_active_comm);; if( libMyLib::mpi_msg_ > 0 ){throw std::runtime_error(" eror " );}

#endif //INC_MyLib_MPIWRAPPER_H

虽然我能理解,但是我想知道如果有什么办法,如何使向前声明能够使用包装器,并在需要时将其与mpi.h头结合起来?< / strong>

1 个答案:

答案 0 :(得分:0)

我不知道您为什么要尝试执行此操作,或者它是否将完全正常运行,但是您现在正在观察的特定错误是因为MPI是C库。

C ++将C实体与C ++实体分开对待。因此,如果您在C ++代码中声明函数MPI_Allreduce,则不会引用与在C代码中使用相同的声明所得到的同一函数MPI_Allreduce

为了使名称引用C实体,您必须使用C语言链接进行声明,这是通过

完成的
extern "C" {
    // declarations go here
}

您可以查看mpi.h,看看它是否具有相同的作用。