diff --git a/.gitignore b/.gitignore index 8deef0c..7fe1d18 100644 --- a/.gitignore +++ b/.gitignore @@ -34,3 +34,7 @@ collective-big-count/test_scatterv collective-big-count/*_uniform_count comm_split_type/cmsplit_type + +singleton/hello_c +singleton/simple_spawn +singleton/simple_spawn_multiple diff --git a/singleton/Makefile b/singleton/Makefile new file mode 100644 index 0000000..4b238c3 --- /dev/null +++ b/singleton/Makefile @@ -0,0 +1,18 @@ +CC=mpicc +CFLAGS=-Wall -g -O0 + +PROGS=hello_c simple_spawn simple_spawn_multiple + +all: $(PROGS) + +hello_c: hello_c.c + $(CC) hello_c.c $(CFLAGS) -o hello_c + +simple_spawn: simple_spawn.c + $(CC) simple_spawn.c $(CFLAGS) -o simple_spawn + +simple_spawn_multiple: simple_spawn_multiple.c + $(CC) simple_spawn_multiple.c $(CFLAGS) -o simple_spawn_multiple + +clean: + $(RM) $(PROGS) *.o diff --git a/singleton/hello_c.c b/singleton/hello_c.c new file mode 100644 index 0000000..429d4a1 --- /dev/null +++ b/singleton/hello_c.c @@ -0,0 +1,27 @@ +#include +#include +#include +#include +#include "mpi.h" + +int main(int argc, char* argv[]) +{ + int mcw_rank, mcw_size, len; + char name[MPI_MAX_PROCESSOR_NAME]; + + MPI_Init(&argc, &argv); + + MPI_Comm_rank(MPI_COMM_WORLD, &mcw_rank); + MPI_Comm_size(MPI_COMM_WORLD, &mcw_size); + MPI_Get_processor_name(name, &len); + + MPI_Barrier( MPI_COMM_WORLD ); + + printf("%3d/%3d) [%s] %d Hello, world!\n", + mcw_rank, mcw_size, name, (int)getpid()); + fflush(NULL); + + MPI_Finalize(); + + return 0; +} diff --git a/singleton/run.sh b/singleton/run.sh new file mode 100755 index 0000000..4cbe64d --- /dev/null +++ b/singleton/run.sh @@ -0,0 +1,38 @@ +#!/bin/bash -e + +echo "=====================" +echo "Testing: Hello with mpirun" +echo "=====================" +mpirun --np 1 ./hello_c + +echo "=====================" +echo "Testing: Hello as a singleton" +echo "=====================" +./hello_c + + +echo "=====================" +echo "Testing: MPI_Comm_spawn with mpirun" +echo "=====================" +mpirun --np 1 ./simple_spawn ./simple_spawn + +echo "=====================" +echo "Testing: MPI_Comm_spawn as a singleton" +echo "=====================" +./simple_spawn ./simple_spawn + + +echo "=====================" +echo "Testing: MPI_Comm_spawn_multiple with mpirun" +echo "=====================" +mpirun --np 1 ./simple_spawn_multiple ./simple_spawn_multiple + +echo "=====================" +echo "Testing: MPI_Comm_spawn_multiple as a singleton" +echo "=====================" +./simple_spawn_multiple ./simple_spawn_multiple + + +echo "=====================" +echo "Success" +echo "=====================" diff --git a/singleton/simple_spawn.c b/singleton/simple_spawn.c new file mode 100644 index 0000000..d213f72 --- /dev/null +++ b/singleton/simple_spawn.c @@ -0,0 +1,24 @@ +#include +#include + +int main(int argc, char *argv[]) +{ + MPI_Comm parent, intercomm; + + MPI_Init(NULL, NULL); + + MPI_Comm_get_parent(&parent); + if (MPI_COMM_NULL != parent) + MPI_Comm_disconnect(&parent); + + if (argc > 1) { + printf("Spawning '%s' ... ", argv[1]); + MPI_Comm_spawn(argv[1], MPI_ARGV_NULL, + 1, MPI_INFO_NULL, 0, MPI_COMM_SELF, + &intercomm, MPI_ERRCODES_IGNORE); + MPI_Comm_disconnect(&intercomm); + printf("OK\n"); + } + + MPI_Finalize(); +} diff --git a/singleton/simple_spawn_multiple.c b/singleton/simple_spawn_multiple.c new file mode 100644 index 0000000..ecb3b05 --- /dev/null +++ b/singleton/simple_spawn_multiple.c @@ -0,0 +1,48 @@ +#include +#include +#include + +int main(int argc, char *argv[]) +{ + MPI_Comm parent, intercomm; + int maxprocs[2]; + char *command[2]; + char **spawn_argv[2]; + char *argv0[2]; + char *argv1[2]; + MPI_Info info[2]; + + MPI_Init(NULL, NULL); + + MPI_Comm_get_parent(&parent); + if (MPI_COMM_NULL != parent) { + printf("Hello from a Child (%s)\n", argv[1]); + fflush(NULL); + MPI_Barrier(parent); + MPI_Comm_disconnect(&parent); + } + else if(argc > 1) { + maxprocs[0] = 1; + maxprocs[1] = 2; + command[0] = strdup(argv[1]); + command[1] = strdup(argv[1]); + spawn_argv[0] = argv0; + spawn_argv[1] = argv1; + argv0[0] = strdup("A"); + argv0[1] = NULL; + argv1[0] = strdup("B"); + argv1[1] = NULL; + info[0] = MPI_INFO_NULL; + info[1] = MPI_INFO_NULL; + + printf("Spawning Multiple '%s' ... ", argv[1]); + MPI_Comm_spawn_multiple(2, command, spawn_argv, maxprocs, info, + 0, MPI_COMM_SELF, + &intercomm, MPI_ERRCODES_IGNORE); + MPI_Barrier(intercomm); + MPI_Comm_disconnect(&intercomm); + printf("OK\n"); + } + + MPI_Finalize(); +}