向 MPI 进程发送函数
Send a function to MPI processes
我目前正在为分布式系统编写一个运行时系统软件,然后我打算评估一些并行管理的东西。我的运行时系统依赖于 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 进程接收器的地址 space 中不再有效。
由于我不知道我将要执行的不同类型任务的数量,这增加了另一个困难,因为我无法创建相应的地图,只能将唯一的 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函数,returns报错信息:
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.
我目前正在为分布式系统编写一个运行时系统软件,然后我打算评估一些并行管理的东西。我的运行时系统依赖于 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 进程接收器的地址 space 中不再有效。 由于我不知道我将要执行的不同类型任务的数量,这增加了另一个困难,因为我无法创建相应的地图,只能将唯一的 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函数,returns报错信息:
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.