瀏覽代碼

mpi/examples/matrix_decomposition: code reorganisation

Nathalie Furmento 12 年之前
父節點
當前提交
b6427ad6ad

+ 3 - 0
mpi/examples/Makefile.am

@@ -57,6 +57,7 @@ EXTRA_DIST = 					\
 	matrix_decomposition/mpi_cholesky_kernels.h	\
 	matrix_decomposition/mpi_cholesky_models.h 	\
 	matrix_decomposition/mpi_cholesky_params.h	\
+	matrix_decomposition/mpi_cholesky_matrix.h	\
 	../tests/helper.h
 
 examplebindir = $(libdir)/starpu/mpi
@@ -142,6 +143,7 @@ matrix_decomposition_mpi_cholesky_SOURCES	=		\
 	matrix_decomposition/mpi_cholesky_kernels.c	\
 	matrix_decomposition/mpi_cholesky_codelets.c	\
 	matrix_decomposition/mpi_cholesky_params.c	\
+	matrix_decomposition/mpi_cholesky_matrix.c	\
 	$(top_srcdir)/examples/common/blas.c
 
 matrix_decomposition_mpi_cholesky_LDADD =			\
@@ -154,6 +156,7 @@ matrix_decomposition_mpi_cholesky_distributed_SOURCES =	\
 	matrix_decomposition/mpi_cholesky_kernels.c	\
 	matrix_decomposition/mpi_cholesky_codelets.c	\
 	matrix_decomposition/mpi_cholesky_params.c	\
+	matrix_decomposition/mpi_cholesky_matrix.c	\
 	$(top_srcdir)/examples/common/blas.c
 
 matrix_decomposition_mpi_cholesky_distributed_LDADD =	\

+ 7 - 66
mpi/examples/matrix_decomposition/mpi_cholesky.c

@@ -20,39 +20,7 @@
 #include "mpi_cholesky_params.h"
 #include "mpi_cholesky_models.h"
 #include "mpi_cholesky_codelets.h"
-
-void display_matrix(float ***bmat, int rank)
-{
-	unsigned i,j,x,y;
-
-	if (display)
-	{
-		printf("[%d] Input :\n", rank);
-
-		for(y=0 ; y<nblocks ; y++)
-		{
-			for(x=0 ; x<nblocks ; x++)
-			{
-				printf("Block %u,%u :\n", x, y);
-				for (j = 0; j < BLOCKSIZE; j++)
-				{
-					for (i = 0; i < BLOCKSIZE; i++)
-					{
-						if (i <= j)
-						{
-							printf("%2.2f\t", bmat[y][x][j +i*BLOCKSIZE]);
-						}
-						else
-						{
-							printf(".\t");
-						}
-					}
-					printf("\n");
-				}
-			}
-		}
-	}
-}
+#include "mpi_cholesky_matrix.h"
 
 int main(int argc, char **argv)
 {
@@ -63,7 +31,8 @@ int main(int argc, char **argv)
 
 	float ***bmat;
 	int rank, nodes, ret;
-	unsigned i,j,x,y;
+	double timing, flops;
+	int correctness;
 
 	ret = starpu_init(NULL);
 	STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
@@ -76,46 +45,18 @@ int main(int argc, char **argv)
 
 	parse_args(argc, argv, nodes);
 
-	bmat = malloc(nblocks * sizeof(float *));
-	for(x=0 ; x<nblocks ; x++)
-	{
-		bmat[x] = malloc(nblocks * sizeof(float *));
-		for(y=0 ; y<nblocks ; y++)
-		{
-			starpu_malloc((void **)&bmat[x][y], BLOCKSIZE*BLOCKSIZE*sizeof(float));
-			for (i = 0; i < BLOCKSIZE; i++)
-			{
-				for (j = 0; j < BLOCKSIZE; j++)
-				{
-					bmat[x][y][j +i*BLOCKSIZE] = (1.0f/(1.0f+(i+(x*BLOCKSIZE)+j+(y*BLOCKSIZE)))) + ((i+(x*BLOCKSIZE) == j+(y*BLOCKSIZE))?1.0f*size:0.0f);
-					//mat[j +i*size] = ((i == j)?1.0f*size:0.0f);
-				}
-			}
-		}
-	}
+	matrix_init(&bmat, rank, nodes, 1);
+	matrix_display(bmat, rank);
 
-	display_matrix(bmat, rank);
-
-	double timing, flops;
 	dw_cholesky(bmat, size, size/nblocks, nblocks, rank, nodes, &timing, &flops);
 
 	starpu_mpi_shutdown();
 
-	display_matrix(bmat, rank);
+	matrix_display(bmat, rank);
 
-	int correctness;
 	dw_cholesky_check_computation(bmat, size, rank, nodes, &correctness, &flops);
 
-	for(x=0 ; x<nblocks ; x++)
-	{
-		for(y=0 ; y<nblocks ; y++)
-		{
-			starpu_free((void *)bmat[x][y]);
-		}
-		free(bmat[x]);
-	}
-	free(bmat);
-
+	matrix_free(&bmat, rank, nodes, 1);
 	starpu_helper_cublas_shutdown();
 	starpu_shutdown();
 

+ 7 - 41
mpi/examples/matrix_decomposition/mpi_cholesky_distributed.c

@@ -20,6 +20,7 @@
 #include "mpi_cholesky_params.h"
 #include "mpi_cholesky_models.h"
 #include "mpi_cholesky_codelets.h"
+#include "mpi_cholesky_matrix.h"
 
 int main(int argc, char **argv)
 {
@@ -30,7 +31,7 @@ int main(int argc, char **argv)
 
 	float ***bmat;
 	int rank, nodes, ret;
-	unsigned i,j,x,y;
+	double timing, flops;
 
 	ret = starpu_init(NULL);
 	STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
@@ -43,56 +44,21 @@ int main(int argc, char **argv)
 
 	parse_args(argc, argv, nodes);
 
-	bmat = malloc(nblocks * sizeof(float *));
-	for(x=0 ; x<nblocks ; x++)
-	{
-		bmat[x] = malloc(nblocks * sizeof(float *));
-		for(y=0 ; y<nblocks ; y++)
-		{
-			int mpi_rank = my_distrib(x, y, nodes);
-			if (mpi_rank == rank)
-			{
-				starpu_malloc((void **)&bmat[x][y], BLOCKSIZE*BLOCKSIZE*sizeof(float));
-				for (i = 0; i < BLOCKSIZE; i++)
-				{
-					for (j = 0; j < BLOCKSIZE; j++)
-					{
-						bmat[x][y][j +i*BLOCKSIZE] = (1.0f/(1.0f+(i+(x*BLOCKSIZE)+j+(y*BLOCKSIZE)))) + ((i+(x*BLOCKSIZE) == j+(y*BLOCKSIZE))?1.0f*size:0.0f);
-						//mat[j +i*size] = ((i == j)?1.0f*size:0.0f);
-					}
-				}
-			}
-		}
-	}
+	matrix_init(&bmat, rank, nodes, 0);
 
-	double timing, flops;
 	dw_cholesky(bmat, size, size/nblocks, nblocks, rank, nodes, &timing, &flops);
 
 	starpu_mpi_shutdown();
 
+	matrix_free(&bmat, rank, nodes, 0);
+	starpu_helper_cublas_shutdown();
+	starpu_shutdown();
+
 	if (rank == 0)
 	{
 		fprintf(stdout, "Computation time (in ms): %2.2f\n", timing/1000);
 		fprintf(stdout, "Synthetic GFlops : %2.2f\n", (flops/timing/1000.0f));
 	}
 
-
-	for(x=0 ; x<nblocks ; x++)
-	{
-		for(y=0 ; y<nblocks ; y++)
-		{
-			int mpi_rank = my_distrib(x, y, nodes);
-			if (mpi_rank == rank)
-			{
-				starpu_free((void *)bmat[x][y]);
-			}
-		}
-		free(bmat[x]);
-	}
-	free(bmat);
-
-	starpu_helper_cublas_shutdown();
-	starpu_shutdown();
-
 	return 0;
 }

+ 102 - 0
mpi/examples/matrix_decomposition/mpi_cholesky_matrix.c

@@ -0,0 +1,102 @@
+/* StarPU --- Runtime system for heterogeneous multicore architectures.
+ *
+ * Copyright (C) 2009-2012  Université de Bordeaux 1
+ * Copyright (C) 2010  Mehdi Juhoor <mjuhoor@gmail.com>
+ * Copyright (C) 2010, 2011, 2012, 2013  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 <starpu.h>
+#include "mpi_cholesky_matrix.h"
+#include "mpi_cholesky_params.h"
+#include "mpi_cholesky_codelets.h"
+
+void matrix_display(float ***bmat, int rank)
+{
+	unsigned i,j,x,y;
+
+	if (display)
+	{
+		printf("[%d] Input :\n", rank);
+
+		for(y=0 ; y<nblocks ; y++)
+		{
+			for(x=0 ; x<nblocks ; x++)
+			{
+				printf("Block %u,%u :\n", x, y);
+				for (j = 0; j < BLOCKSIZE; j++)
+				{
+					for (i = 0; i < BLOCKSIZE; i++)
+					{
+						if (i <= j)
+						{
+							printf("%2.2f\t", bmat[y][x][j +i*BLOCKSIZE]);
+						}
+						else
+						{
+							printf(".\t");
+						}
+					}
+					printf("\n");
+				}
+			}
+		}
+	}
+}
+
+void matrix_init(float ****bmat, int rank, int nodes, int alloc_everywhere)
+{
+	unsigned i,j,x,y;
+
+	*bmat = malloc(nblocks * sizeof(float *));
+	for(x=0 ; x<nblocks ; x++)
+	{
+		(*bmat)[x] = malloc(nblocks * sizeof(float *));
+		for(y=0 ; y<nblocks ; y++)
+		{
+			int mpi_rank = my_distrib(x, y, nodes);
+			if (alloc_everywhere || (mpi_rank == rank))
+			{
+				starpu_malloc((void **)&(*bmat)[x][y], BLOCKSIZE*BLOCKSIZE*sizeof(float));
+				for (i = 0; i < BLOCKSIZE; i++)
+				{
+					for (j = 0; j < BLOCKSIZE; j++)
+					{
+						(*bmat)[x][y][j +i*BLOCKSIZE] = (1.0f/(1.0f+(i+(x*BLOCKSIZE)+j+(y*BLOCKSIZE)))) + ((i+(x*BLOCKSIZE) == j+(y*BLOCKSIZE))?1.0f*size:0.0f);
+						//mat[j +i*size] = ((i == j)?1.0f*size:0.0f);
+					}
+				}
+			}
+		}
+	}
+}
+
+void matrix_free(float ****bmat, int rank, int nodes, int alloc_everywhere)
+{
+	unsigned x, y;
+
+	for(x=0 ; x<nblocks ; x++)
+	{
+		for(y=0 ; y<nblocks ; y++)
+		{
+			int mpi_rank = my_distrib(x, y, nodes);
+			if (alloc_everywhere || (mpi_rank == rank))
+			{
+				starpu_free((void *)(*bmat)[x][y]);
+			}
+		}
+		free((*bmat)[x]);
+	}
+	free(*bmat);
+}
+

+ 27 - 0
mpi/examples/matrix_decomposition/mpi_cholesky_matrix.h

@@ -0,0 +1,27 @@
+/* StarPU --- Runtime system for heterogeneous multicore architectures.
+ *
+ * Copyright (C) 2009-2012  Université de Bordeaux 1
+ * Copyright (C) 2010  Mehdi Juhoor <mjuhoor@gmail.com>
+ * Copyright (C) 2010, 2011, 2012, 2013  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.
+ */
+
+#ifndef __MPI_CHOLESKY_MATRIX_H__
+#define __MPI_CHOLESKY_MATRIX_H__
+
+void matrix_display(float ***bmat, int rank);
+void matrix_init(float ****bmat, int rank, int nodes, int alloc_everywhere);
+void matrix_free(float ****bmat, int rank, int nodes, int alloc_everywhere);
+
+#endif /* __MPI_CHOLESKY_MATRIX_H__ */
+