From 5a02d5cc62c70c0e2887e8514e5499700f40e75c Mon Sep 17 00:00:00 2001
From: Thomas Fischer <thomas.fischer@ufz.de>
Date: Mon, 2 Jul 2012 13:28:13 +0200
Subject: [PATCH] [file SimpleTests/MatrixTests/MatVecMultNDPerm.cpp] - fixed a
 typo - print host info under Unix - fixed some memory leaks

[file SimpleTests/MatrixTests/MatVecMultNDPermOpenMP.cpp]
- added TCLAP and logog to file
- added Copyright and License notice
---
 SimpleTests/MatrixTests/CMakeLists.txt        |   1 +
 SimpleTests/MatrixTests/MatVecMultNDPerm.cpp  |  13 +-
 .../MatrixTests/MatVecMultNDPermOpenMP.cpp    | 150 ++++++++++++------
 3 files changed, 112 insertions(+), 52 deletions(-)

diff --git a/SimpleTests/MatrixTests/CMakeLists.txt b/SimpleTests/MatrixTests/CMakeLists.txt
index 3f6ca5d90ca..c06884683a9 100644
--- a/SimpleTests/MatrixTests/CMakeLists.txt
+++ b/SimpleTests/MatrixTests/CMakeLists.txt
@@ -60,6 +60,7 @@ IF (METIS_FOUND)
 	TARGET_LINK_LIBRARIES ( MatVecMultNDPermOpenMP
 		BaseLib
 		MathLib
+		logog
 		${METIS_LIBRARIES}
 		${ADDITIONAL_LIBS}
 	)
diff --git a/SimpleTests/MatrixTests/MatVecMultNDPerm.cpp b/SimpleTests/MatrixTests/MatVecMultNDPerm.cpp
index 50cc513f624..7390673c714 100644
--- a/SimpleTests/MatrixTests/MatVecMultNDPerm.cpp
+++ b/SimpleTests/MatrixTests/MatVecMultNDPerm.cpp
@@ -13,6 +13,9 @@
 
 #ifdef OGS_BUILD_INFO
 #include "BuildInfo.h"
+#endif
+
+#ifdef UNIX
 #include <sys/unistd.h>
 #endif
 
@@ -56,7 +59,7 @@ int main(int argc, char *argv[])
 	// such as "-m matrix".
 	TCLAP::ValueArg<std::string> matrix_arg("m","matrix","input matrix file in CRS format",true,"","file name of the matrix in CRS format");
 
-	// Add the argument mesh_arg to the CmdLine object. The CmdLine object
+	// Add the argument matrix_arg to the CmdLine object. The CmdLine object
 	// uses this Arg to parse the command line.
 	cmd.add( matrix_arg );
 
@@ -90,6 +93,9 @@ int main(int argc, char *argv[])
 	} else {
 		INFO("CXX_FLAGS: %s %s", CMAKE_CXX_FLAGS, CMAKE_CXX_FLAGS_DEBUG);
 	}
+#endif
+
+#ifdef UNIX
 	const size_t length(256);
 	char *hostname(new char[length]);
 	gethostname (hostname, length);
@@ -192,5 +198,10 @@ int main(int argc, char *argv[])
 	delete [] x;
 	delete [] y;
 
+	delete custom_format;
+	delete logogCout;
+	LOGOG_SHUTDOWN();
+
+
 	return 0;
 }
diff --git a/SimpleTests/MatrixTests/MatVecMultNDPermOpenMP.cpp b/SimpleTests/MatrixTests/MatVecMultNDPermOpenMP.cpp
index 6998eae46b6..83fcc549e6a 100644
--- a/SimpleTests/MatrixTests/MatVecMultNDPermOpenMP.cpp
+++ b/SimpleTests/MatrixTests/MatVecMultNDPermOpenMP.cpp
@@ -1,5 +1,10 @@
 /**
- * MatVecMultNDPermOpenMP.cpp
+ * Copyright (c) 2012, OpenGeoSys Community (http://www.opengeosys.net)
+ *            Distributed under a Modified BSD License.
+ *              See accompanying file LICENSE.txt or
+ *              http://www.opengeosys.net/LICENSE.txt
+ *
+ * \qfile MatVecMultNDPermOpenMP.cpp
  *
  * Created on 2012-01-20 by Thomas Fischer
  */
@@ -9,6 +14,11 @@
 // BaseLib
 #include "RunTime.h"
 #include "CPUTime.h"
+// BaseLib/tclap
+#include "tclap/CmdLine.h"
+// BaseLib/logog
+#include "logog.hpp"
+#include "formatter.hpp"
 
 // MathLib
 #include "sparse.h"
@@ -17,20 +27,83 @@
 #include "LinAlg/Sparse/NestedDissectionPermutation/CRSMatrixReorderedOpenMP.h"
 #include "LinAlg/Sparse/NestedDissectionPermutation/Cluster.h"
 
+#ifdef UNIX
+#include <sys/unistd.h>
+#endif
+
+#ifdef OGS_BUILD_INFO
+#include "BuildInfo.h"
+#endif
+
+/**
+ * new formatter for logog
+ */
+class FormatterCustom : public logog::FormatterGCC
+{
+    virtual TOPIC_FLAGS GetTopicFlags( const logog::Topic &topic )
+    {
+        return ( Formatter::GetTopicFlags( topic ) &
+                 ~( TOPIC_FILE_NAME_FLAG | TOPIC_LINE_NUMBER_FLAG ));
+    }
+};
+
 int main(int argc, char *argv[])
 {
-	if (argc < 4) {
-		std::cout << "Usage: " << argv[0] << " matrix number_of_multiplications resultfile" << std::endl;
-		return 1;
-	}
+	LOGOG_INITIALIZE();
+
+	TCLAP::CmdLine cmd("The purpose of this program is the speed test of sparse matrix vector multiplication (MVM) employing OpenMP technique, where the matrix is stored in CRS format. Before executing the MVM a nested dissection reordering is performed.", ' ', "0.1");
+
+	// Define a value argument and add it to the command line.
+	// A value arg defines a flag and a type of value that it expects,
+	// such as "-m matrix".
+	TCLAP::ValueArg<std::string> matrix_arg("m","matrix","input matrix file in CRS format",true,"","file name of the matrix in CRS format");
+
+	// Add the argument matrix_arg to the CmdLine object. The CmdLine object
+	// uses this Arg to parse the command line.
+	cmd.add( matrix_arg );
+
+	TCLAP::ValueArg<unsigned> n_cores_arg("n", "number-cores", "number of cores to use", true, 1, "number of cores");
+	cmd.add( n_cores_arg );
+
+	TCLAP::ValueArg<unsigned> n_mults_arg("n", "number-of-multiplications", "number of multiplications to perform", true, 10, "number of multiplications");
+	cmd.add( n_mults_arg );
+
+	TCLAP::ValueArg<std::string> output_arg("o", "output", "output file", false, "", "string");
+	cmd.add( output_arg );
+
+	TCLAP::ValueArg<unsigned> verbosity_arg("v", "verbose", "level of verbosity [0 very low information, 1 much information]", false, 0, "string");
+	cmd.add( verbosity_arg );
+
+	cmd.parse( argc, argv );
 
 	// read the number of multiplication to execute
-	unsigned n_mults (0);
-	n_mults = atoi (argv[2]);
+	unsigned n_mults (n_mults_arg.getValue());
+	std::string fname_mat (matrix_arg.getValue());
+	bool verbose (verbosity_arg.getValue());
+
+	FormatterCustom *custom_format (new FormatterCustom);
+	logog::Cout *logogCout(new logog::Cout);
+	logogCout->SetFormatter(*custom_format);
 
-	std::string fname_mat (argv[1]);
+	// read number of threads
+	unsigned n_threads (n_cores_arg.getValue());
 
-	bool verbose (true);
+#ifdef OGS_BUILD_INFO
+	INFO("%s was build with compiler %s", argv[0], CMAKE_CXX_COMPILER);
+	if (std::string(CMAKE_BUILD_TYPE).compare("Release") == 0) {
+		INFO("CXX_FLAGS: %s %s", CMAKE_CXX_FLAGS, CMAKE_CXX_FLAGS_RELEASE);
+	} else {
+		INFO("CXX_FLAGS: %s %s", CMAKE_CXX_FLAGS, CMAKE_CXX_FLAGS_DEBUG);
+	}
+#endif
+
+#ifdef UNIX
+	const size_t length(256);
+	char *hostname(new char[length]);
+	gethostname (hostname, length);
+	INFO("hostname: %s", hostname);
+	delete [] hostname;
+#endif
 
 	// *** reading matrix in crs format from file
 	std::ifstream in(fname_mat.c_str(), std::ios::in | std::ios::binary);
@@ -38,21 +111,22 @@ int main(int argc, char *argv[])
 	unsigned *iA(NULL), *jA(NULL), n;
 	if (in) {
 		if (verbose) {
-			std::cout << "reading matrix from " << fname_mat << " ... " << std::flush;
+			INFO("reading matrix from %s ...", fname_mat.c_str());
 		}
 		BaseLib::RunTime timer;
 		timer.start();
 		CS_read(in, n, iA, jA, A);
 		timer.stop();
 		if (verbose) {
-			std::cout << "ok, [wclock: " << timer.elapsed() << " s]" << std::endl;
+			INFO("\t- took %e s", timer.elapsed());
 		}
 	} else {
-		std::cout << "error reading matrix from " << fname_mat << std::endl;
+		ERR("error reading matrix from %s", fname_mat.c_str());
+		return -1;
 	}
 	unsigned nnz(iA[n]);
 	if (verbose) {
-		std::cout << "Parameters read: n=" << n << ", nnz=" << nnz << std::endl;
+		INFO("\tParameters read: n=%d, nnz=%d", n, nnz);
 	}
 
 	MathLib::CRSMatrixReorderedOpenMP mat(n, iA, jA, A);
@@ -69,7 +143,7 @@ int main(int argc, char *argv[])
 
 	// calculate the nested dissection reordering
 	if (verbose) {
-		std::cout << "calculating nested dissection permutation of matrix ... " << std::flush;
+		INFO("*** calculating nested dissection (ND) permutation of matrix ...");
 	}
 	run_timer.start();
 	cpu_timer.start();
@@ -82,45 +156,26 @@ int main(int argc, char *argv[])
 	cpu_timer.stop();
 	run_timer.stop();
 	if (verbose) {
-		std::cout << cpu_timer.elapsed() << "\t" << run_timer.elapsed() << std::endl;
-	} else {
-		if (argc == 4) {
-			std::ofstream result_os(argv[3], std::ios::app);
-			if (result_os) {
-				result_os << cpu_timer.elapsed() << "\t" << run_timer.elapsed() << " calc nested dissection perm" << std::endl;
-			}
-			result_os.close();
-		} else {
-			std::cout << cpu_timer.elapsed() << "\t" << run_timer.elapsed() << " calc nested dissection perm" << std::endl;
-		}
+		INFO("\t[ND] - took %e sec \t%e sec", cpu_timer.elapsed(), run_timer.elapsed());
 	}
 
-
 	// applying the nested dissection reordering
 	if (verbose) {
-		std::cout << "applying nested dissection permutation to FEM matrix ... " << std::flush;
+		INFO("\t[ND] applying nested dissection permutation to FEM matrix ... ");
 	}
 	run_timer.start();
 	cpu_timer.start();
 	mat.reorderMatrix(op_perm, po_perm);
 	cpu_timer.stop();
 	run_timer.stop();
-	if (verbose) std::cout << cpu_timer.elapsed() << "\t" << run_timer.elapsed() << std::endl;
-	else {
-		if (argc == 4) {
-			std::ofstream result_os(argv[3], std::ios::app);
-			if (result_os) {
-				result_os << cpu_timer.elapsed() << "\t" << run_timer.elapsed() << " applying nested dissection perm" << std::endl;
-			}
-			result_os.close();
-		} else {
-			std::cout << cpu_timer.elapsed() << "\t" << run_timer.elapsed() << std::endl;
-		}
+	if (verbose) {
+		INFO("\t[ND]: - took %e sec\t%e sec", cpu_timer.elapsed(), run_timer.elapsed());
 	}
 
 	if (verbose) {
-		std::cout << "matrix vector multiplication with Toms amuxCRS ... " << std::flush;
+		INFO("*** %d matrix vector multiplications (MVM) with Toms amuxCRS (%d threads)... ", n_mults, n_threads);
 	}
+
 	run_timer.start();
 	cpu_timer.start();
 	for (size_t k(0); k<n_mults; k++) {
@@ -130,22 +185,15 @@ int main(int argc, char *argv[])
 	run_timer.stop();
 
 	if (verbose) {
-		std::cout << "done [" << cpu_timer.elapsed() << " sec cpu time], [wclock: "
-				<< run_timer.elapsed() << " sec]" << std::endl;
-	} else {
-		if (argc == 4) {
-			std::ofstream result_os (argv[3], std::ios::app);
-			if (result_os) {
-				result_os << cpu_timer.elapsed() << "\t" << run_timer.elapsed() << " " << n_mults << " MatVecMults, matrix " << fname_mat << std::endl;
-			}
-			result_os.close();
-		} else {
-			std::cout << cpu_timer.elapsed() << "\t" << run_timer.elapsed() << std::endl;
-		}
+		INFO("\t[MVM] - took %e sec\t %e sec", cpu_timer.elapsed(), run_timer.elapsed());
 	}
 
 	delete [] x;
 	delete [] y;
 
+	delete custom_format;
+	delete logogCout;
+	LOGOG_SHUTDOWN();
+
 	return 0;
 }
-- 
GitLab