c++ - 向 MPI 进程发送函数

标签 c++ pthreads mpi function-pointers send

我目前正在为分布式系统编写一个运行时系统软件,然后我打算评估一些并行管理的东西。我的运行时系统依赖于 OpenMP3.0 标准中的任务编程模型,但适用于另一类具有 MPI 的机器。

为此,我创建了一些 MPI 进程(每台机器一个)并在其上启动多个线程。 有一个主进程负责为其他进程创建新任务,它需要发送一些工作去做。 每个任务都包含一个函数指针(要做的工作),以及一组传递给这个函数的参数。 像这样:

    class Task
    {
      public:
        typdef struct
        {
          // ... Storing and packing arguments
        } args_t;
        Task();
        ~Task();
        void exec()
        {
          // Executing the function pointed by "func_ptr"
          // with the specified arguments in "args"
          func_ptr( args );
        }
      private:
        void (*func_ptr)(args_t);
        args_t args;
    };

为了传递参数,我打算使用 MPI_Type_create_struct 函数。 但是,我现在的问题是:如何将函数发送到另一个 MPI 进程? 如果我发送指针函数,它将在 MPI 进程接收器的地址空间中不再有效。 由于我不知道我将要执行的不同类型任务的数量,这增加了另一个困难,因为我无法创建相应的 map ,只能将唯一的 ID 发送到 MPI 进程。 您有解决我的问题的想法吗?

谢谢!

最佳答案

按照 Gilles Gouillardet 的建议,我尝试使用 dlopen() 和 dlsym() 函数来解决这个问题。 我尝试了一个小程序来找到指向 helloWorld 函数的指针:

    #include <dlfcn.h>
    #include <iostream>

    void helloWorld(void)
    {
      std::cout << "Hello World !" << std::endl;
    }

    int main(int argc, char** argv)
    {
        void *handle;
        void (*task)(void);
        char* error;
        handle = dlopen(NULL, RTLD_LAZY);
        if(!handle)
        {
          fprintf(stderr, "dlopen error: %s\n", dlerror());
          exit(EXIT_FAILURE);
        }
        dlerror();

        *(void **) (&task) = dlsym(handle, "helloWorld");
        if( (error = dlerror()) != NULL)
        {
          fprintf(stderr, "dlsym error: %s\n", dlerror());
          exit(EXIT_FAILURE);
        }
        dlclose(handle);

      return EXIT_SUCCESS;
    }

然而,dlsym 函数无法找到 helloWorld 函数,并返回错误消息:

    dlsym error: (null)

我现在不尝试找到这个问题的解决方案,但我正在寻找它。 如果有人对 dlsymp 功能有任何经验,请与我分享您的经验。

编辑:由于 dlopen 联机帮助页 (https://linux.die.net/man/3/dlsym),我将“NULL”传递给 dlopen,其中指定:

The function dlopen() loads the dynamic library file named by the null-terminated string filename and returns an opaque "handle" for the dynamic library. If filename is NULL, then the returned handle is for the main program.

关于c++ - 向 MPI 进程发送函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51364515/

相关文章:

mpi - 如何在 MPI 中使用单个节点进行写入

c++ - 在 C++ 上搜索结构内部的对象

c++ - 临时文件流

c++ - 为什么 __thread 在使用 printf 语句时给出链接器错误?

c++ - 在指定 C++ 异常和 pthread 取消的交互方面有什么进展吗?

c - 无法使用 C 中的线程创建文件

c - 具有包含动态分配数组的自定义数据类型的 MPI_reduce() : segmentation fault

c++ - 如何在 C++ 中实现 2 路依赖注入(inject)?

c++ - Delphi:使用调试器调用 C dll 函数需要 15 秒,没有调试器需要 0.16 秒。为什么?

docker - 将 MPI 与 docker 容器一起使用