Browse Source

mpi/examples: add example with intra-communicator communications

Nathalie Furmento 10 years ago
parent
commit
f01c499741
3 changed files with 182 additions and 1 deletions
  1. 21 1
      mpi/examples/Makefile.am
  2. 135 0
      mpi/examples/comm/comm.c
  3. 26 0
      mpi/examples/helper.h

+ 21 - 1
mpi/examples/Makefile.am

@@ -96,11 +96,13 @@ stencil_stencil5_LDADD =		\
 
 starpu_mpi_EXAMPLES	+=	\
 	stencil/stencil5
+endif
 
 ##################
 # MPI LU example #
 ##################
 
+if BUILD_EXAMPLES
 if !NO_BLAS_LIB
 
 examplebin_PROGRAMS += 			\
@@ -183,12 +185,13 @@ mpi_lu_plu_outofcore_example_double_SOURCES =	\
 	mpi_lu/pdlu_implicit.c			\
 	../../examples/common/blas.c
 endif
-
+endif
 
 ########################
 # MPI Cholesky example #
 ########################
 
+if BUILD_EXAMPLES
 if !NO_BLAS_LIB
 examplebin_PROGRAMS +=		\
 	matrix_decomposition/mpi_cholesky			\
@@ -224,11 +227,13 @@ starpu_mpi_EXAMPLES +=				\
 	matrix_decomposition/mpi_cholesky			\
 	matrix_decomposition/mpi_cholesky_distributed
 endif
+endif
 
 ###################
 # complex example #
 ###################
 
+if BUILD_EXAMPLES
 examplebin_PROGRAMS +=			\
 	complex/mpi_complex
 
@@ -243,6 +248,21 @@ starpu_mpi_EXAMPLES	+=			\
 	complex/mpi_complex
 endif
 
+###################
+# comm example #
+###################
+
+if BUILD_EXAMPLES
+examplebin_PROGRAMS +=			\
+	comm/comm
+
+comm_comm_LDADD =		\
+	../src/libstarpumpi-@STARPU_EFFECTIVE_VERSION@.la
+
+starpu_mpi_EXAMPLES	+=			\
+	comm/comm
+endif
+
 
 showcheck:
 	-cat $(TEST_LOGS) /dev/null

+ 135 - 0
mpi/examples/comm/comm.c

@@ -0,0 +1,135 @@
+/* StarPU --- Runtime system for heterogeneous multicore architectures.
+ *
+ * Copyright (C) 2015  Centre National de la Recherche Scientifique
+ *
+ * StarPU is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or (at
+ * your option) any later version.
+ *
+ * StarPU is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * See the GNU Lesser General Public License in COPYING.LGPL for more details.
+ */
+
+/*
+ * This example splits the whole set of communicators in subgroups,
+ * all communications take place within each subgroups
+ */
+
+#include <starpu_mpi.h>
+#include "../helper.h"
+
+void func_cpu(void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args)
+{
+	int *value = (int *)STARPU_VARIABLE_GET_PTR(descr[0]);
+	int rank;
+
+	starpu_codelet_unpack_args(_args, &rank);
+	FPRINTF_MPI(stderr, "Executing codelet with value %d and rank %d\n", *value, rank);
+	STARPU_ASSERT_MSG(*value == rank, "Received value %d is not the expected value %d\n", *value, rank);
+}
+
+struct starpu_codelet mycodelet =
+{
+	.cpu_funcs = {func_cpu},
+	.nbuffers = 1,
+	.modes = {STARPU_RW}
+};
+
+int main(int argc, char **argv)
+{
+	int size, n, x=789;
+	int color;
+	MPI_Comm newcomm;
+	int rank, newrank;
+	int ret;
+	starpu_data_handle_t data[2];
+
+        MPI_Init(&argc, &argv);
+        MPI_Comm_rank(MPI_COMM_WORLD, &rank);
+        MPI_Comm_size(MPI_COMM_WORLD, &size);
+
+        if (size < 4)
+        {
+		FPRINTF(stderr, "We need at least 4 processes.\n");
+                MPI_Finalize();
+                return STARPU_TEST_SKIPPED;
+        }
+
+	color = rank%2;
+	MPI_Comm_split(MPI_COMM_WORLD, color, rank, &newcomm);
+	MPI_Comm_rank(newcomm, &newrank);
+	FPRINTF(stderr, "[%d][%d] color %d\n", rank, newrank, color);
+
+	if (newrank == 0)
+	{
+		FPRINTF(stderr, "[%d][%d] sending %d\n", rank, newrank, rank);
+		MPI_Send(&rank, 1, MPI_INT, 1, 10, newcomm);
+	}
+	else if (newrank == 1)
+	{
+		MPI_Recv(&x, 1, MPI_INT, 0, 10, newcomm, NULL);
+		FPRINTF(stderr, "[%d][%d] received %d\n", rank, newrank, x);
+	}
+
+        ret = starpu_init(NULL);
+        STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
+        ret = starpu_mpi_init_comm(NULL, NULL, 0, newcomm);
+        STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
+
+	if (newrank == 0)
+	{
+		starpu_variable_data_register(&data[0], STARPU_MAIN_RAM, (uintptr_t)&rank, sizeof(unsigned));
+		starpu_variable_data_register(&data[1], STARPU_MAIN_RAM, (uintptr_t)&rank, sizeof(unsigned));
+		starpu_mpi_data_register_comm(data[1], 22, 0, newcomm);
+	}
+	else
+		starpu_variable_data_register(&data[0], -1, (uintptr_t)NULL, sizeof(unsigned));
+	starpu_mpi_data_register_comm(data[0], 12, 0, newcomm);
+
+	if (newrank == 0)
+	{
+		starpu_mpi_req req[2];
+		starpu_mpi_issend(data[1], &req[0], 1, 22, newcomm);
+		starpu_mpi_isend(data[0], &req[1], 1, 12, newcomm);
+		starpu_mpi_wait(&req[0], NULL);
+		starpu_mpi_wait(&req[1], NULL);
+	}
+	else if (newrank == 1)
+	{
+		int *xx;
+
+		starpu_mpi_recv(data[0], 0, 12, newcomm, NULL);
+		xx = (int *)starpu_variable_get_local_ptr(data[0]);
+		FPRINTF(stderr, "[%d][%d] received %d\n", rank, newrank, *xx);
+		STARPU_ASSERT_MSG(x==*xx, "Received value %d is incorrect (should be %d)\n", *xx, x);
+
+		starpu_variable_data_register(&data[1], -1, (uintptr_t)NULL, sizeof(unsigned));
+		starpu_mpi_data_register_comm(data[1], 22, 0, newcomm);
+		starpu_mpi_recv(data[0], 0, 22, newcomm, NULL);
+		xx = (int *)starpu_variable_get_local_ptr(data[0]);
+		FPRINTF(stderr, "[%d][%d] received %d\n", rank, newrank, *xx);
+		STARPU_ASSERT_MSG(x==*xx, "Received value %d is incorrect (should be %d)\n", *xx, x);
+	}
+
+	if (newrank == 0 || newrank == 1)
+	{
+		starpu_mpi_insert_task(newcomm, &mycodelet,
+				       STARPU_RW, data[0],
+				       STARPU_VALUE, &x, sizeof(x),
+				       STARPU_EXECUTE_ON_NODE, 1,
+				       0);
+
+		starpu_task_wait_for_all();
+		starpu_data_unregister(data[0]);
+		starpu_data_unregister(data[1]);
+	}
+
+	starpu_mpi_shutdown();
+	starpu_shutdown();
+        MPI_Finalize();
+	return 0;
+}

+ 26 - 0
mpi/examples/helper.h

@@ -0,0 +1,26 @@
+/* StarPU --- Runtime system for heterogeneous multicore architectures.
+ *
+ * Copyright (C) 2011, 2012, 2013, 2015  Centre National de la Recherche Scientifique
+ *
+ * StarPU is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or (at
+ * your option) any later version.
+ *
+ * StarPU is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * See the GNU Lesser General Public License in COPYING.LGPL for more details.
+ */
+
+#include <errno.h>
+
+#define STARPU_TEST_SKIPPED 77
+
+#define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
+#define FPRINTF_MPI(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) { \
+    						int _disp_rank; MPI_Comm_rank(MPI_COMM_WORLD, &_disp_rank);       \
+                                                fprintf(ofile, "[%d][starpu_mpi][%s] " fmt , _disp_rank, __starpu_func__ ,## __VA_ARGS__); \
+                                                fflush(ofile); }} while(0);
+