如何编写用于分析的 MPI 库

How to write MPI library for profiling

我想,比如说,在用 C 编写的程序中调用的所有 MPI_Sends 之前写一个字符串。为此,我想编写一个分析库。我在 mylib.c

中写了这段代码
#include <stdio.h>
#include <mpi.h>

int MPI_Send(void *buf, int count, MPI_Datatype type, int to, int tag, MPI_Comm comm)
{
    printf("Some string");
    return PMPI_Send(buf, count, type, to, tag, comm);
}

如果我 运行 mpicc -fPIC mylib.c -o mylib.o 我得到错误 - error: conflicting types for ‘MPI_Send’ 如何正确编写此代码以及如何将其编译并 link 到我的 mpi 程序中?

您需要使用此签名声明您的函数:

int MPI_Send(const void* buf, int count, MPI_Datatype type, 
             int to, int tag, MPI_Comm comm) { ... }

第一个参数是constvoid* != const void*。那就是你所缺少的。例如:https://www.mpich.org/static/docs/latest/www3/MPI_Send.html