我正在尝试使用SWIG包装器编译MPI C代码以进行Python接口。从StackOverflow和谷歌搜索,我知道这是SWIG包装期间链接的问题,但我无法找到解决问题的方法。
我用以下几行编译:
>swig -python worker_c.i
>mpicc -fPIC $(python-config --includes) -c worker_c.c worker_c_wrap.c
>ld -zmuldefs -shared worker_c.o worker_c_wrap.o -o _worker_c.so
并导入生成的worker_c.py会产生:
Python 2.7.2+ (default, Jul 20 2012, 22:15:08)
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import worker_c
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "worker_c.py", line 25, in <module>
_worker_c = swig_import_helper()
File "worker_c.py", line 21, in swig_import_helper
_mod = imp.load_module('_worker_c', fp, pathname, description)
ImportError: ./_worker_c.so: undefined symbol: ompi_mpi_int
这是我的源代码:
worker_c.c
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
const int nelements1 = 1E3;
const int nelements2 = 1E6;
int outarray(){
float* array = malloc(sizeof(float) * nelements2);
int i = 0;
for( i = 0; i < nelements2; i++ ){
array[i] = 1.110001;
}
return *array;
}
int main(int argc, char *argv[]){
MPI_Comm comm;
MPI_Comm_get_parent(&comm);
int myid, world_size;//, size;
int root = 0;
int* endloop = malloc(sizeof(int));
void* input = malloc(sizeof(float) * nelements1);
void* output;
void* sendbuff;
int recv = 1;
MPI_Comm_rank(MPI_COMM_WORLD, &myid);
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
output = (void *)outarray();
MPI_Scatter(sendbuff, recv, MPI_INT, endloop, recv, MPI_INT, root, comm);
while ( endloop[0] < 1) {
MPI_Barrier(comm);
MPI_Scatter(sendbuff, recv, MPI_FLOAT, input, nelements1, MPI_FLOAT, root, comm);
MPI_Barrier(comm);
MPI_Gather(sendbuff, recv, MPI_FLOAT, output, nelements2, MPI_FLOAT, root, comm);
MPI_Scatter(sendbuff, recv, MPI_INT, endloop, recv, MPI_INT, root, comm);
}
free(endloop);
free(input);
MPI_Finalize();
}
worker_c.i
// file: worker_c.i
%module worker_c
%{
#include <mpi.h>
#include <stdlib.h>
#include "worker_c.c"
%}
%include mpi4py/mpi4py.i
%mpi4py_typemap(Comm, MPI_Comm);
void outarray();
void main(int argc, char *argv[]);
我对SWIG和MPI没什么经验,所以我很感激任何可用的帮助。 谢谢!