Skip to content

Instantly share code, notes, and snippets.

@smutch
Last active August 29, 2015 14:06
Show Gist options
  • Save smutch/ebd499d9495225de72b3 to your computer and use it in GitHub Desktop.
Save smutch/ebd499d9495225de72b3 to your computer and use it in GitHub Desktop.
c: test split MPI_Comm with gbpSID
import os
env = Environment(env=os.environ)
env.Replace(CC = "mpicc")
env.AppendUnique(CFLAGS = ["-std=gnu99"])
libs = {}
libs["gbpCode"] = {"I" : "~/3rd_party/gbpCode/myInclude",
"L" : "~/3rd_party/gbpCode/myLib/mpi",
"l" : "gbpLib",
}
for lib in libs.itervalues():
env.AppendUnique(CPPPATH = os.path.expanduser(lib["I"]))
env.AppendUnique(LIBPATH = os.path.expanduser(lib["L"]))
env.AppendUnique(LIBS = os.path.expanduser(lib["l"]))
sources = ["split.c"]
env.Program("split", sources)
#define _MAIN
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#include <gbpLib.h>
int main(int argc, char *argv[])
{
// Init MPI
MPI_Init(&argc, &argv);
// Copy the world communicator
MPI_Comm world;
MPI_Comm_dup(MPI_COMM_WORLD, &world);
// Get this rank's info on world
int world_rank, world_size;
MPI_Comm_rank(world, &world_rank);
MPI_Comm_size(world, &world_size);
fprintf(stderr, "I am world rank %d(/%d)\n", world_rank, world_size);
// Create a new split communicator
// All odd odd have color 0 and all evens have color 1
int my_color = (world_rank % 2) ? 0 : 1;
MPI_Comm split_comm;
MPI_Comm_split(world, my_color, world_rank, &split_comm);
int split_rank, split_size;
MPI_Comm_rank(split_comm, &split_rank);
MPI_Comm_size(split_comm, &split_size);
fprintf(stderr, "I am world rank %d(/%d), have color %d and split_comm rank %d(/%d)\n",
world_rank, world_size, my_color, split_rank, split_size);
// Call SID_init
SID_init(&argc, &argv, NULL, &split_comm);
SID_log_set_fp(stderr);
SID_log("I am world rank %d(/%d), have color %d and SID rank %d(/%d)\n",
SID_LOG_COMMENT|SID_LOG_ALLRANKS, world_rank, world_size, my_color,
SID.My_rank, SID.n_proc);
// Free the comms we made
// N.B. We don't need to free split_comm as this was done by SID_init
MPI_Comm_free(&world);
// Call SID_Finalize (which contains an MPI_Finalize call)
SID_exit(EXIT_SUCCESS);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment