BaseCode.c 687 Bytes
Newer Older
iker_martin's avatar
iker_martin committed
1
2
#include <stdio.h>
#include <stdlib.h>
iker_martin's avatar
iker_martin committed
3
#include <unistd.h>
iker_martin's avatar
iker_martin committed
4
5
#include <mpi.h>

6
#define ROOT 0
iker_martin's avatar
iker_martin committed
7
#define FIRST 0
8
#define LAST 1
iker_martin's avatar
iker_martin committed
9
10
11
12
13
14
15
16

int main(int argc, char* argv[]) {
    MPI_Init(&argc, &argv);
    
    MPI_Comm spawn, new_comm;
    MPI_Comm_get_parent(&spawn);
    if(spawn == MPI_COMM_NULL) {
	int num_c = atoi(argv[1]);
17
        MPI_Comm_spawn(argv[0], MPI_ARGV_NULL, num_c, MPI_INFO_NULL, ROOT, MPI_COMM_WORLD, &spawn, MPI_ERRCODES_IGNORE);
iker_martin's avatar
iker_martin committed
18
19
        MPI_Intercomm_merge(spawn, FIRST,  &new_comm);
    } else {
20
        MPI_Intercomm_merge(spawn, LAST, &new_comm);
iker_martin's avatar
iker_martin committed
21
22
	MPI_Barrier(MPI_COMM_WORLD);
    }
iker_martin's avatar
iker_martin committed
23
24
    MPI_Comm_disconnect(&spawn);
    MPI_Comm_disconnect(&new_comm);
iker_martin's avatar
iker_martin committed
25
26
27

    MPI_Finalize(); 
}