Python MPI主程序和C MPI工作程序之间的相互通信?

Python MPI主程序和C MPI工作程序之间的相互通信?,c,mpi,openmpi,C,Mpi,Openmpi,我正在尝试用C编写一个MPI工作程序,它将与用python编写的MPI主程序进行通信。主节点将发送散点和聚集,C工作节点应接收这些散点和聚集,并通过聚集返回变量。问题是,我无法成功地编写工作文件 (worker使用C的原因是因为此代码是一个围绕现有python和C程序的框架。) 以下是我编写的代码: #include <stdio.h> #include <mpi.h> MPI_Comm comm; MPI_Comm_get_parent(&comm); in

我正在尝试用C编写一个MPI工作程序,它将与用python编写的MPI主程序进行通信。主节点将发送散点和聚集,C工作节点应接收这些散点和聚集,并通过聚集返回变量。问题是,我无法成功地编写工作文件

(worker使用C的原因是因为此代码是一个围绕现有python和C程序的框架。)

以下是我编写的代码:

#include <stdio.h>
#include <mpi.h>

MPI_Comm comm;
MPI_Comm_get_parent(&comm);

int myid, world_size, size;
int root = 0;
int* endloop = malloc(sizeof(int));
int nelements1 = 1E3;
int nelements2 = 1E6;
float* input = malloc(sizeof(float) * nelements1);
float output[nelements2];

int randomarray(){
        float array[nelements2];
        srand(time(NULL));
        for( i = 0; i < nelements2; i++ ){
                array[i] = rand() % 0 + 1E6;
        }

        return array;

MPI_Comm_rank(MPI_COMM_WORLD, &myid);
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
MPI_Comm_remote_size(comm, &size);

output = randomarray();

MPI_Scatter(endloop, 1, MPI_INT, root, comm);

while ( endloop[0] < 1) {
        MPI_Barrier(comm);
        MPI_Scatter(input, nelements1, MPI_FLOAT, root, comm);
        MPI_Barrier(comm);
        MPI_Gather(output, nelements2, MPI_FLOAT, root, comm);
        MPI_Scatter(endloop, 1, MPI_INT, root, comm);
}

MPI_Finalize();
问题似乎是我的MPI调用的格式,但我不确定该修复什么


任何帮助都将不胜感激。谢谢大家!

代码中有很多问题。例如,在函数
randomarray()
中,
i
未定义,您需要在函数末尾
}
return
之后添加一个括号。和
浮点数组[nelements2]
创建
nelements2
的本地数组,该数组将在
返回时删除。使用类似于
float*array=malloc(sizeof(float)*nbelement2)的东西如果您希望访问函数
randomarray()
之外的数组元素。不要忘记
free()
内存:如果你调用
malloc
三次,你应该调用
free
三次。。。
rand()%0
是什么意思?阅读错误和警告!再见,Francis你似乎在用Python编写C代码。C中的可执行语句必须收集在函数中。您不能简单地在全局级别上调用函数调用,如调用
MPI\u Comm\u get\u parent()
,或调用函数调用赋值,如调用
malloc()
。您也不能在另一个函数中声明函数。@Hristo这是一个令人尴尬的错误,我编写它时非常像Python(我最近一直在使用Python,远远超过C),我让它工作起来了。
maddie@exo:~/code/bart_commloop$ mpicc worker.c -o worker
worker.c:5:21: error: expected declaration specifiers or ‘...’ before ‘&’ token
worker.c:9:16: warning: incompatible implicit declaration of built-in function ‘malloc’ [enabled by default]
worker.c:9:1: error: initializer element is not constant
worker.c:12:1: error: initializer element is not constant
worker.c:13:7: error: variably modified ‘output’ at file scope
worker.c: In function ‘randomarray’:
worker.c:18:7: error: ‘i’ undeclared (first use in this function)
worker.c:18:7: note: each undeclared identifier is reported only once for each function it appears in
worker.c:19:21: warning: division by zero [-Wdiv-by-zero]
worker.c:22:2: warning: return makes integer from pointer without a cast [enabled by default]
worker.c:22:2: warning: function returns address of local variable [enabled by default]
worker.c:28:8: error: incompatible types when assigning to type ‘float[1]’ from type ‘int’
worker.c:30:1: warning: passing argument 4 of ‘MPI_Scatter’ makes pointer from integer without a cast [enabled by default]
/usr/lib/openmpi/include/mpi.h:1197:20: note: expected ‘void *’ but argument is of type ‘int’
worker.c:30:1: warning: passing argument 5 of ‘MPI_Scatter’ makes integer from pointer without a cast [enabled by default]
/usr/lib/openmpi/include/mpi.h:1197:20: note: expected ‘int’ but argument is of type ‘MPI_Comm’
worker.c:30:1: error: too few arguments to function ‘MPI_Scatter’
/usr/lib/openmpi/include/mpi.h:1197:20: note: declared here
worker.c:34:2: warning: passing argument 4 of ‘MPI_Scatter’ makes pointer from integer without a cast [enabled by default]
/usr/lib/openmpi/include/mpi.h:1197:20: note: expected ‘void *’ but argument is of type ‘int’
worker.c:34:2: warning: passing argument 5 of ‘MPI_Scatter’ makes integer from pointer without a cast [enabled by default]
/usr/lib/openmpi/include/mpi.h:1197:20: note: expected ‘int’ but argument is of type ‘MPI_Comm’
worker.c:34:2: error: too few arguments to function ‘MPI_Scatter’
/usr/lib/openmpi/include/mpi.h:1197:20: note: declared here
worker.c:36:2: warning: passing argument 4 of ‘MPI_Gather’ makes pointer from integer without a cast [enabled by default]
/usr/lib/openmpi/include/mpi.h:1058:20: note: expected ‘void *’ but argument is of type ‘int’
worker.c:36:2: warning: passing argument 5 of ‘MPI_Gather’ makes integer from pointer without a cast [enabled by default]
/usr/lib/openmpi/include/mpi.h:1058:20: note: expected ‘int’ but argument is of type ‘MPI_Comm’
worker.c:36:2: error: too few arguments to function ‘MPI_Gather’
/usr/lib/openmpi/include/mpi.h:1058:20: note: declared here
worker.c:37:2: warning: passing argument 4 of ‘MPI_Scatter’ makes pointer from integer without a cast [enabled by default]
/usr/lib/openmpi/include/mpi.h:1197:20: note: expected ‘void *’ but argument is of type ‘int’
worker.c:37:2: warning: passing argument 5 of ‘MPI_Scatter’ makes integer from pointer without a cast [enabled by default]
/usr/lib/openmpi/include/mpi.h:1197:20: note: expected ‘int’ but argument is of type ‘MPI_Comm’
worker.c:37:2: error: too few arguments to function ‘MPI_Scatter’
/usr/lib/openmpi/include/mpi.h:1197:20: note: declared here
worker.c:40:1: error: expected declaration or statement at end of input