From 7dd825c1dbfa64fe47cf274f8ec2142e34e3771a Mon Sep 17 00:00:00 2001
From: Pietro Incardona <i-bird@localhost.localdomain>
Date: Tue, 24 May 2016 14:56:38 +0200
Subject: [PATCH] Fixing osx PETSC installation

---
 example/Vector/0_simple/main.cpp | 148 +++++++++++++++----------------
 script/install_PETSC.sh          |  30 ++++++-
 script/install_SUITESPARSE.sh    |   2 +
 script/pre_req                   |  25 ++++++
 script/solve_gsed                |  19 ++++
 src/Grid/grid_dist_id.hpp        |   1 +
 src/Vector/vector_dist.hpp       |   4 +-
 7 files changed, 149 insertions(+), 80 deletions(-)
 create mode 100755 script/solve_gsed

diff --git a/example/Vector/0_simple/main.cpp b/example/Vector/0_simple/main.cpp
index f64c635f9..83324637c 100644
--- a/example/Vector/0_simple/main.cpp
+++ b/example/Vector/0_simple/main.cpp
@@ -1,41 +1,17 @@
 #include "Vector/vector_dist.hpp"
-#include "Decomposition/CartDecomposition.hpp"
 
 /*
  * ### WIKI 1 ###
  *
  * ## Simple example
  * 
- * This example show several basic functionalities of the distributed vector
+ * This example show several basic functionalities of the distributed vector, A distributed vector is nothing else than
+ * a set of particles in an N dimensional space
  * 
  * ### WIKI END ###
  * 
  */
 
-/*
- * ### WIKI 2 ###
- *
- * We define a particle structure it contain 4 scalars one vector with 3 components
- * and a tensor of rank 2 3x3
- *
- * ### WIKI END ###
- *
- */
-
-template<typename T> class Particle
-{
-public:
-
-	typedef boost::fusion::vector<T,T[3],T[3][3]> type;
-
-	type data;
-
-	static const unsigned int s = 0;
-	static const unsigned int v = 1;
-	static const unsigned int t = 2;
-	static const unsigned int max_prop = 3;
-};
-
 int main(int argc, char* argv[])
 {
 	//
@@ -44,65 +20,80 @@ int main(int argc, char* argv[])
 	// Here we Initialize the library, than we create a uniform random generator between 0 and 1 to to generate particles
 	// randomly in the domain, we create a Box that define our domain, boundary conditions, and ghost
 	//
-	openfpm_init(&argc,&argv);
-	Vcluster & v_cl = create_vcluster();
 	
-	typedef Point<2,float> s;
-
-	// set the seed
-	// create the random generator engine
-	std::srand(v_cl.getProcessUnitID());
-	std::default_random_engine eg;
-	std::uniform_real_distribution<float> ud(0.0f, 1.0f);
+        // initialize the library
+	openfpm_init(&argc,&argv);
 
+	// Here we define our domain a 2D box with internals from 0 to 1.0 for x and y
 	Box<2,float> domain({0.0,0.0},{1.0,1.0});
+
+	// Here we define the boundary conditions of our problem
     size_t bc[2]={PERIODIC,PERIODIC};
+
+	// extended boundary around the domain, and the processor domain
 	Ghost<2,float> g(0.01);
 	
 	//
 	// ### WIKI 3 ###
 	//
 	// Here we are creating a distributed vector defined by the following parameters
+	//
+	// * 2 is the Dimensionality of the space where the objects live
+	// * float is the type used for the spatial coordinate of the particles
+	// * float,float[3],float[3][3] is the information stored by each particle a scalar float, a vector float[3] and a tensor of rank 2 float[3][3]
+	//   the list of properties must be put into an aggregate data astructure aggregate<prop1,prop2,prop3, ... >
 	// 
-	// * Dimensionality of the space where the objects live 2D (1° template parameters)
-	// * Type of the space, float (2° template parameters)
-	// * Information stored by each object (3* template parameters), in this case a Point_test store 4 scalars
-	//   1 vector and an asymmetric tensor of rank 2
-	// 
-	// Constructor instead require:
+	// vd is the instantiation of the object
+	//
+	// The Constructor instead require:
 	//
 	// * Number of particles 4096 in this case
 	// * Domain where is defined this structure
+	// * bc boundary conditions
+	// * g Ghost
 	//
 	// The following construct a vector where each processor has 4096 / N_proc (N_proc = number of processor)
 	// objects with an undefined position in space. This non-space decomposition is also called data-driven
 	// decomposition
 	//
-	vector_dist<2,float, Particle<float> > vd(4096,domain,bc,g);
+	vector_dist<2,float, aggregate<float,float[3],float[3][3]> > vd(4096,domain,bc,g);
+
+	// the scalar is the element at position 0 in the aggregate
+	const int scalar = 0;
+
+	// the vector is the element at position 1 in the aggregate
+	const int vector = 1;
+
+	// the tensor is the element at position 2 in the aggregate
+	const int tensor = 2;
 
 	//
 	// ### WIKI 5 ###
 	//
-	// Get an iterator that go through the particles, in an undefined position state and define its position
+	// Get an iterator that go through the 4096 particles, in an undefined position state and define its position
 	//
-	auto it = vd.getIterator();
+	auto it = vd.getDomainIterator();
 
 	while (it.isNext())
 	{
 		auto key = it.get();
 
-		vd.getPos(key)[0] = ud(eg);
-		vd.getPos(key)[1] = ud(eg);
+		// we define x, assign a random position between 0.0 and 1.0
+		vd.getPos(key)[0] = rand() / RAND_MAX;
+
+		// we define y, assign a random position between 0.0 and 1.0
+		vd.getPos(key)[1] = rand() / RAND_MAX;
 
+		// next particle
 		++it;
 	}
 
 	//
 	// ### WIKI 6 ###
 	//
-	// Once we define the position, we distribute them according to the default decomposition
-	// The default decomposition is created even before assigning the position to the object
-	// (This will probably change in future)
+	// Once we define the position, we distribute them according to the default space decomposition
+	// The default decomposition is created even before assigning the position to the object. It determine
+	// which part of space each processor manage
 	//
 	vd.map();
 
@@ -112,39 +103,44 @@ int main(int argc, char* argv[])
 	// We get the object that store the decomposition, than we iterate again across all the objects, we count them
 	// and we confirm that all the particles are local
 	//
+	//Counter we use it later
 	size_t cnt = 0;
+
+	// Get the space decomposition
 	auto & ct = vd.getDecomposition();
-	it = vd.getIterator();
 
+	// Get a particle iterator
+	it = vd.getDomainIterator();
+
+	// For each particle ...
 	while (it.isNext())
 	{
-		auto key = it.get();
-
-		// The template parameter is unuseful and will probably disappear
-		if (ct.isLocal(vd.getPos(key)) == false)
-			std::cerr << "Error particle is not local" << "\n";
-
-		// set the all the properties to some numbers
-
-		// scalar
-		vd.template getProp<0>(key) = 1.0;
-
-		vd.template getProp<1>(key)[0] = 1.0;
-		vd.template getProp<1>(key)[1] = 1.0;
-		vd.template getProp<1>(key)[2] = 1.0;
-
-		vd.template getProp<2>(key)[0][0] = 1.0;
-		vd.template getProp<2>(key)[0][1] = 1.0;
-		vd.template getProp<2>(key)[0][2] = 1.0;
-		vd.template getProp<2>(key)[1][0] = 1.0;
-		vd.template getProp<2>(key)[1][1] = 1.0;
-		vd.template getProp<2>(key)[1][2] = 1.0;
-		vd.template getProp<2>(key)[2][0] = 1.0;
-		vd.template getProp<2>(key)[2][1] = 1.0;
-		vd.template getProp<2>(key)[2][2] = 1.0;
-
+		// ... p
+		auto p = it.get();
+
+		// we set the properties of the particle p
+		
+                // the scalar property
+		vd.template getProp<scalar>(p) = 1.0;
+
+		vd.template getProp<vector>(p)[0] = 1.0;
+		vd.template getProp<vector>(p)[1] = 1.0;
+		vd.template getProp<vector>(p)[2] = 1.0;
+
+		vd.template getProp<tensor>(p)[0][0] = 1.0;
+		vd.template getProp<tensor>(p)[0][1] = 1.0;
+		vd.template getProp<tensor>(p)[0][2] = 1.0;
+		vd.template getProp<tensor>(p)[1][0] = 1.0;
+		vd.template getProp<tensor>(p)[1][1] = 1.0;
+		vd.template getProp<tensor>(p)[1][2] = 1.0;
+		vd.template getProp<tensor>(p)[2][0] = 1.0;
+		vd.template getProp<tensor>(p)[2][1] = 1.0;
+		vd.template getProp<tensor>(p)[2][2] = 1.0;
+
+		// increment the counter
 		cnt++;
 
+		// next particle
 		++it;
 	}
 
@@ -156,6 +152,8 @@ int main(int argc, char* argv[])
 	// the variable count and finaly execute. All the operations are asynchronous, execute work like a barrier and ensure that all the 
 	// queued operations are executed
 	//
+	
+	auto & v_cl = create_vcluster();
 	v_cl.sum(cnt);
 	v_cl.execute();
 	
diff --git a/script/install_PETSC.sh b/script/install_PETSC.sh
index e545127ce..d7bb78736 100755
--- a/script/install_PETSC.sh
+++ b/script/install_PETSC.sh
@@ -7,6 +7,12 @@ if [ -d "$1/PETSC" ]; then
   exit 0
 fi
 
+# Detect gcc pr clang
+
+source script/detect_gcc
+detect_gcc_or_clang g++
+
+
 ## If some dependencies has been installed feed them to PETSC
 
 MUMPS_extra_libs=""
@@ -14,7 +20,7 @@ MUMPS_extra_libs=""
 configure_options=""
 configure_options_scalapack=""
 configure_options_superlu=""
-configure_trilinos_options=" -D TPL_ENABLE_MPI=ON -D Trilinos_ENABLE_OpenMP=ON"
+configure_trilinos_options=" -D TPL_ENABLE_MPI=ON "
 configure_options_hypre=""
 
 ### Here we install OpenBLAS and SUITESPARSE
@@ -84,7 +90,15 @@ if [ ! -d "$1/TRILINOS" ]; then
   cd trilinos-12.6.1-Source
   mkdir build
   cd build
-  cmake -D CMAKE_INSTALL_PREFIX:PATH=$1/TRILINOS -D CMAKE_BUILD_TYPE=RELEASE -D Trilinos_ENABLE_TESTS=OFF  -D Trilinos_ENABLE_ALL_PACKAGES=ON $configure_trilinos_options  ../.
+
+  ### On clang we have no openMP
+  if [ x"$dgc_compiler" == x"clang++" ]; then
+    conf_trl_openmp="-D Trilinos_ENABLE_OpenMP=OFF"
+  else
+    conf_trl_openmp="-D Trilinos_ENABLE_OpenMP=ON"
+  fi
+
+  cmake -D CMAKE_INSTALL_PREFIX:PATH=$1/TRILINOS -D CMAKE_BUILD_TYPE=RELEASE $conf_trl_openmp -D Trilinos_ENABLE_TESTS=OFF  -D Trilinos_ENABLE_ALL_PACKAGES=ON $configure_trilinos_options  ../.
 
   make -j $2
   if [ $? -eq 0 ]; then
@@ -139,8 +153,16 @@ if [ ! -d "$1/MUMPS" ]; then
   if [ x"$platform" = x"osx"  ]; then
     # installation for OSX
 
-    echo "OSX TO DO BYE"
-    exit 1
+    sed -i "" -e "s|CC      = cc|CC = mpicc|" Makefile.inc
+    sed -i "" -e "s|FC      = f90| FC = mpif90|" Makefile.inc
+    sed -i "" -e "s|FL      = f90| FL = mpif90|" Makefile.inc 
+
+    sed -i "" -e "s|SCALAP  = -lscalapack -lblacs|SCALAP = -L$1/SCALAPACK/lib -L$1/OPENBLAS/lib -lscalapack|" Makefile.inc
+    sed -i "" -e "s|LIBBLAS = -lblas|LIBBLAS = -lopenblas|" Makefile.inc
+
+    sed -i "" -e "s|OPTF    = -O|OPTF = -fpic -O3" Makefile.inc
+    sed -i "" -e "s|OPTC    = -O -I.|OPTC = -fpic -O3 -I." Makefile.inc
+    sed -i "" -e "s|OPTL    = -O|OPTL = -fpic -O3" Makefile.inc
 
   else
     # Installation for linux
diff --git a/script/install_SUITESPARSE.sh b/script/install_SUITESPARSE.sh
index b96df00ad..8ad698013 100755
--- a/script/install_SUITESPARSE.sh
+++ b/script/install_SUITESPARSE.sh
@@ -1,7 +1,9 @@
 #! /bin/bash
 
+source script/detect_gcc
 source script/discover_os
 
+detect_gcc_or_clang g++
 discover_os
 
 # check if the directory $1/SUITESPARSE exist
diff --git a/script/pre_req b/script/pre_req
index e097e4eb8..a2efc79b4 100755
--- a/script/pre_req
+++ b/script/pre_req
@@ -68,6 +68,31 @@ if [ x"$platform" = x"linux" -a x"$pcman" = x"yum"  ]; then
   fi
 fi
 
+#### if we are on OSX we install gnu-sed
+
+if [ x"platform" == x"osx" ]
+
+  command -v gsed >/dev/null 2>&1
+  if [ $? -ne 0 ]; then
+    echo >&2
+    echo -e "gsed\033[91;5;1m FAILED \033[0m"
+    echo "OpenFPM require gnu-sed but it's not installed, searching a solution... "
+    solve_gsed $platform
+    command -v gsed >/dev/null 2>&1
+    if [ $? -ne 0 ]; then
+      echo -e "gsed\033[91;5;1m FAILED \033[0m"
+      exit 1
+    else
+      echo -e "gsed\033[92;1m SUCCESS \033[0m"
+    fi
+  else
+    echo -e "gsed\033[92;1m SUCCESS \033[0m"
+  fi
+
+fi
+
+####
+
 command -v cmake >/dev/null 2>&1
 if [ $? -ne 0 ]; then
   echo >&2
diff --git a/script/solve_gsed b/script/solve_gsed
new file mode 100755
index 000000000..7a3f74a9e
--- /dev/null
+++ b/script/solve_gsed
@@ -0,0 +1,19 @@
+
+#! /bin/bash 
+
+function solve_gsed() {        
+source script/show_solutions
+source script/discover_package_manager
+discover_package_manager $1
+pcman=$discover_package_manager_ret
+
+if [ x"$pcman" = x"" ]; then
+        exit 1
+fi
+
+if [ x"$1" = x"osx" ]; then 
+        commands[0]="$pcman install gnu-sed"
+        possible_solutions "${commands[@]}"
+fi 
+}
+
diff --git a/src/Grid/grid_dist_id.hpp b/src/Grid/grid_dist_id.hpp
index 7be1ed34b..7a0f96cc0 100644
--- a/src/Grid/grid_dist_id.hpp
+++ b/src/Grid/grid_dist_id.hpp
@@ -18,6 +18,7 @@
 #include "Packer_Unpacker/Packer.hpp"
 #include "Packer_Unpacker/Unpacker.hpp"
 #include "Decomposition/CartDecomposition.hpp"
+#include "data_type/aggregate.hpp"
 
 // External ghost box to send for internal ghost box fixation
 template<unsigned int dim>
diff --git a/src/Vector/vector_dist.hpp b/src/Vector/vector_dist.hpp
index 1dde07d81..0e28d613f 100644
--- a/src/Vector/vector_dist.hpp
+++ b/src/Vector/vector_dist.hpp
@@ -26,6 +26,7 @@
 #include "Grid/grid_dist_id_iterator_dec.hpp"
 #include "Vector/vector_dist_ofb.hpp"
 #include "Decomposition/CartDecomposition.hpp"
+#include "data_type/aggregate.hpp"
 
 #define V_SUB_UNIT_FACTOR 64
 
@@ -1015,7 +1016,7 @@ public:
 	{
 		// Get ghost and anlarge by 1%
 		Ghost<dim,St> g = dec.getGhost();
-		g.magnify(1.01);
+		g.magnify(1.013);
 
 		return getCellList(r_cut, g);
 	}
@@ -1077,6 +1078,7 @@ public:
 		{
 			div[i] = static_cast<size_t>((pbox.getP2().get(i) - pbox.getP1().get(i)) / r_cut);
 			div[i]++;
+			pbox.setHigh(i,pbox.getLow(i) + div[i]*r_cut);
 		}
 
 		cell_list.Initialize(pbox, div);
-- 
GitLab