首页 > 解决方案 > 向 MPI 进程发送函数

问题描述

我目前正在为分布式系统编写运行时系统软件,然后我打算评估一些并行管理的东西。我的运行时系统依赖于 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 进程接收器的地址空间中不再有效。由于我不知道我将拥有多少不同类型的任务,这增加了另一个困难,因为我无法创建相应的地图并且只能向 MPI 进程发送一个唯一的 ID。你有什么想法来解决我的问题吗?

谢谢 !

标签: c++pthreadsmpifunction-pointerssend

解决方案


正如 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,其中指定:

函数 dlopen() 加载以 null 结尾的字符串文件名命名的动态库文件,并为动态库返回一个不透明的“句柄”。如果 filename 为 NULL,则返回的句柄用于主程序。


推荐阅读