0

I'm trying to write an MPI worker in C that will communicate with the MPI master, written in python. The master will send out scatters and gathers, and the C worker should receive those and return variables via gather. The problem is, I'm having trouble successfully writing the worker file.

(The reason the worker is in C is because this code is a skeleton to wrap around preexisting python and C programs.)

Here is the code I've written:

#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();

And the error output when I attempt to compile is:

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

The issue seems to be the formatting of my MPI calls, but I'm not sure what to fix there.

Any help would be greatly appreciated. Thank you!

  • Lots of problems in your code. In function `randomarray()` for instance, `i` is not defined, you need an additional bracket `}` at the end of the function, after `return`. And `float array[nelements2];` create a local array of `nelements2`...which will be deleted at `return`. Use something like `float *array =malloc(sizeof(float)*nbelement2);` if you wish to access array's elements outside of function `randomarray()`. And don't forget to `free()` the memory : if you call `malloc` three times, you should call `free` three times... What does `rand() % 0` mean ? Read errors and warns!Bye, Francis – francis Mar 06 '14 at 22:19
  • You seem to be writing your C code as if you are writing in Python. Executable statements in C have to be gathered in functions. You cannot simply place a function call like your call to `MPI_Comm_get_parent()` or an assignment with a function call like calling `malloc()` at the global level. You cannot also declare a function inside another function. – Hristo Iliev Mar 07 '14 at 07:11
  • @Hristo This was an embarrassing mistake to make, I was writing it very much like Python (I've been using Python a lot of late, much more than C) and I got it working. – astromaddie Mar 17 '14 at 18:35

0 Answers0