From 1654ef64fc33a0692e58516f6f212ec706a7b045 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Karol=20Sieroci=C5=84ski?= <ksiero@man.poznan.pl>
Date: Thu, 2 Aug 2018 12:43:15 +0200
Subject: [PATCH 1/3] Added copier and deleter to datatype class

---
 src/ref_any.cxx | 1 +
 1 file changed, 1 insertion(+)

diff --git a/src/ref_any.cxx b/src/ref_any.cxx
index 85a98b8f4..20d5bf72f 100644
--- a/src/ref_any.cxx
+++ b/src/ref_any.cxx
@@ -56,6 +56,7 @@ Ref Ref_base::do_copy(Ref_r ref)
 	return Ref {newbuffer, [](void* v){operator delete (v);}, std::move(densified_type), true, true};
 }
 
+
 const Datatype& Ref_base::type() const noexcept
 {
 	if (!m_content || !m_content->m_buffer) return UNDEF_TYPE;
-- 
GitLab


From 408d8d19b012236ab642082561d99387c16851e9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Karol=20Sieroci=C5=84ski?= <ksiero@man.poznan.pl>
Date: Thu, 2 Aug 2018 13:42:57 +0200
Subject: [PATCH 2/3] Passing ctx to hdf5 instead of logger, removed to_mpi()
 from Expression, split mpi plugin to 2 files

---
 plugins/decl_hdf5/decl_hdf5.cxx   |  2 +-
 plugins/decl_hdf5/decl_hdf5_cfg.h |  6 +--
 plugins/decl_hdf5/file_cfg.h      |  8 ++--
 plugins/mpi/mpi.cxx               | 33 ++++-----------
 plugins/mpi/mpi_comm_type.h       | 67 +++++++++++++++++++++++++++++++
 src/expression.cxx                | 25 ------------
 6 files changed, 82 insertions(+), 59 deletions(-)
 create mode 100644 plugins/mpi/mpi_comm_type.h

diff --git a/plugins/decl_hdf5/decl_hdf5.cxx b/plugins/decl_hdf5/decl_hdf5.cxx
index d17434abe..c8bcda0dc 100644
--- a/plugins/decl_hdf5/decl_hdf5.cxx
+++ b/plugins/decl_hdf5/decl_hdf5.cxx
@@ -386,7 +386,7 @@ struct decl_hdf5_plugin: Plugin {
 	
 	decl_hdf5_plugin(Context& ctx, PC_tree_t config, MPI_Comm*):
 		Plugin {ctx},
-		m_config{config, ctx.logger()}
+		m_config{ctx, config}
 	{
 		Hdf5_error_handler _;
 		if ( 0>H5open() ) handle_hdf5_err("Cannot initialize HDF5 library");
diff --git a/plugins/decl_hdf5/decl_hdf5_cfg.h b/plugins/decl_hdf5/decl_hdf5_cfg.h
index c33a1fa70..4650bdfc4 100644
--- a/plugins/decl_hdf5/decl_hdf5_cfg.h
+++ b/plugins/decl_hdf5/decl_hdf5_cfg.h
@@ -56,7 +56,7 @@ class Decl_hdf5_cfg
 	Decl_hdf5_cfg(const Decl_hdf5_cfg&)=delete;
 	
 public:
-	Decl_hdf5_cfg(PC_tree_t tree, PDI::Logger_sptr logger)
+	Decl_hdf5_cfg(PDI::Context& ctx, PC_tree_t tree)
 	{
 		using PDI::Error;
 		using PDI::len;
@@ -67,12 +67,12 @@ public:
 			int nb_files = len(tree);
 			for (int file_id=0; file_id<nb_files; ++file_id) {
 				vector<string> events;
-				m_files.emplace_back(PC_get(tree, "[%d]", file_id), events, logger);
+				m_files.emplace_back(ctx, PC_get(tree, "[%d]", file_id), events);
 				fill_rw(events);
 			}
 		} else if (!PC_status(PC_get(tree, "{0}"))) {
 			vector<string> events;
-			m_files.emplace_back(tree, events, logger);
+			m_files.emplace_back(ctx, tree, events);
 			fill_rw(events);
 		} else {
 			throw Error{PDI_ERR_CONFIG, "Unexpected Decl'HDF5 configuration: `%s'", to_string(tree).c_str()};
diff --git a/plugins/decl_hdf5/file_cfg.h b/plugins/decl_hdf5/file_cfg.h
index 5c5056617..14ab6b81d 100644
--- a/plugins/decl_hdf5/file_cfg.h
+++ b/plugins/decl_hdf5/file_cfg.h
@@ -67,7 +67,7 @@ class File_cfg
 	File_cfg(const File_cfg&)=delete;
 	
 public:
-	File_cfg(PC_tree_t tree, std::vector<std::string>& events, PDI::Logger_sptr logger):
+	File_cfg(PDI::Context& ctx, PC_tree_t tree, std::vector<std::string>& events):
 		m_file{PDI::to_string(PC_get(tree, ".file"))},
 		m_when{1}
 	{
@@ -104,7 +104,7 @@ public:
 			} else if ( key == "datasets" ) {
 				int nb_dataset = len(PC_get(tree, ".datasets"));
 				for (int dataset_id=0; dataset_id<nb_dataset; ++dataset_id) {
-					m_datasets.emplace(to_string(PC_get(tree, ".datasets{%d}", dataset_id)), Datatype_template::load(PC_get(tree, ".datasets<%d>", dataset_id), logger));
+					m_datasets.emplace(to_string(PC_get(tree, ".datasets{%d}", dataset_id)), Datatype_template::load(PC_get(tree, ".datasets<%d>", dataset_id), ctx.logger()));
 				}
 			} else if ( key == "write" ) {
 				PC_tree_t write_tree = PC_get(tree, ".write");
@@ -203,7 +203,7 @@ public:
 #ifdef H5_HAVE_PARALLEL
 	MPI_Comm communicator(PDI::Context& ctx) const
 	{
-		if (m_communicator) return m_communicator.to_mpi_comm(ctx);
+		if (m_communicator) return *(static_cast<const MPI_Comm*>(PDI::Ref_r{m_communicator.to_ref(ctx)}.get()));
 		return MPI_COMM_SELF;
 	}
 #endif
@@ -248,7 +248,7 @@ const PDI::Expression& Transfer_cfg::when() const
 MPI_Comm Transfer_cfg::communicator(PDI::Context& ctx) const
 {
 	if (!m_communicator) return parent().communicator(ctx);
-	return m_communicator.to_mpi_comm(ctx);
+	return *(static_cast<const MPI_Comm*>(PDI::Ref_r{m_communicator.to_ref(ctx)}.get()));
 }
 #endif
 
diff --git a/plugins/mpi/mpi.cxx b/plugins/mpi/mpi.cxx
index c2871d7c4..db81253fd 100644
--- a/plugins/mpi/mpi.cxx
+++ b/plugins/mpi/mpi.cxx
@@ -25,39 +25,18 @@
 #include <mpi.h>
 
 #include <pdi/context.h>
-#include <pdi/pdi_fwd.h>
 #include <pdi/plugin.h>
-#include <pdi/scalar_datatype.h>
+
+#include "mpi_comm_type.h"
+
+namespace {
 
 struct mpi_plugin: PDI::Plugin {
 
 	mpi_plugin(PDI::Context& ctx, PC_tree_t, MPI_Comm*):
 		Plugin{ctx}
 	{
-		// deleter for MPI_Comm
-		auto deleter = [](void* ptr) {
-			auto comm_ptr = static_cast<MPI_Comm*>(ptr);
-			MPI_Comm_free(comm_ptr);
-			delete comm_ptr;
-		};
-		
-		// load MPI_COMM_WORLD
-		MPI_Comm* comm = new MPI_Comm;
-		MPI_Comm_dup(MPI_COMM_WORLD, comm);
-		PDI::Ref comm_world_ref{comm, deleter, PDI::Datatype_uptr{new PDI::Scalar_datatype(PDI::Scalar_kind::MPI_COMM, sizeof(MPI_Comm))}, true, false};
-		context()["MPI_COMM_WORLD"].share(comm_world_ref, false, false);
-		
-		// load MPI_COMM_SELF
-		comm = new MPI_Comm;
-		MPI_Comm_dup(MPI_COMM_SELF, comm);
-		PDI::Ref comm_self_ref {comm, deleter, PDI::Datatype_uptr{new PDI::Scalar_datatype(PDI::Scalar_kind::MPI_COMM, sizeof(MPI_Comm))}, true, false};
-		context()["MPI_COMM_SELF"].share(comm_self_ref, false, false);
-		
-		// load MPI_COMM_NULL
-		comm = new MPI_Comm;
-		*comm = MPI_COMM_NULL;
-		PDI::Ref comm_null_ref {comm, &free, PDI::Datatype_uptr{new PDI::Scalar_datatype(PDI::Scalar_kind::MPI_COMM, sizeof(MPI_Comm))}, true, false};
-		context()["MPI_COMM_NULL"].share(comm_null_ref, false, false);
+		load_mpi_comm_predefineds(ctx);
 	}
 	
 	~mpi_plugin()
@@ -69,4 +48,6 @@ struct mpi_plugin: PDI::Plugin {
 	}
 };
 
+} // namespace <anonymous>
+
 PDI_PLUGIN(mpi)
\ No newline at end of file
diff --git a/plugins/mpi/mpi_comm_type.h b/plugins/mpi/mpi_comm_type.h
new file mode 100644
index 000000000..b1cac3479
--- /dev/null
+++ b/plugins/mpi/mpi_comm_type.h
@@ -0,0 +1,67 @@
+/*******************************************************************************
+ * Copyright (C) 2018 Institute of Bioorganic Chemistry Polish Academy of Science (PSNC)
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * * Neither the name of CEA nor the names of its contributors may be used to
+ *   endorse or promote products derived from this software without specific
+ *   prior written permission.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ ******************************************************************************/
+
+#ifndef MPI_COMM_TYPE_H_
+#define MPI_COMM_TYPE_H_
+
+#include <memory>
+#include <mpi.h>
+
+#include <pdi/context.h>
+#include <pdi/error.h>
+#include <pdi/pdi_fwd.h>
+#include <pdi/scalar_datatype.h>
+
+namespace  {
+
+void load_mpi_comm_predefineds(PDI::Context& ctx) {
+	// deleter for MPI_Comm
+	auto deleter = [](void* ptr) {
+		auto comm_ptr = static_cast<MPI_Comm*>(ptr);
+		MPI_Comm_free(comm_ptr);
+		delete comm_ptr;
+	};
+	
+	// load MPI_COMM_WORLD
+	MPI_Comm* comm = new MPI_Comm;
+	MPI_Comm_dup(MPI_COMM_WORLD, comm);
+	PDI::Ref comm_world_ref{comm, deleter, PDI::Datatype_uptr{new PDI::Scalar_datatype(PDI::Scalar_kind::UNKNOWN, sizeof(MPI_Comm))}, true, false};
+	ctx["MPI_COMM_WORLD"].share(comm_world_ref, false, false);
+	
+	// load MPI_COMM_SELF
+	comm = new MPI_Comm;
+	MPI_Comm_dup(MPI_COMM_SELF, comm);
+	PDI::Ref comm_self_ref {comm, deleter, PDI::Datatype_uptr{new PDI::Scalar_datatype(PDI::Scalar_kind::UNKNOWN, sizeof(MPI_Comm))}, true, false};
+	ctx["MPI_COMM_SELF"].share(comm_self_ref, false, false);
+	
+	// load MPI_COMM_NULL
+	comm = new MPI_Comm;
+	*comm = MPI_COMM_NULL;
+	PDI::Ref comm_null_ref {comm, &free, PDI::Datatype_uptr{new PDI::Scalar_datatype(PDI::Scalar_kind::UNKNOWN, sizeof(MPI_Comm))}, true, false};
+	ctx["MPI_COMM_NULL"].share(comm_null_ref, false, false);
+}
+
+}
+
+#endif // MPI_COMM_TYPE_H_
diff --git a/src/expression.cxx b/src/expression.cxx
index 31186706c..ff879fcd5 100644
--- a/src/expression.cxx
+++ b/src/expression.cxx
@@ -80,8 +80,6 @@ struct Expression::Impl {
 	
 	virtual Ref to_ref(Context&) const = 0;
 	
-	virtual MPI_Comm to_mpi_comm(Context&) const;
-	
 	static unique_ptr<Impl> parse_term(char const** val_str);
 	
 	static string parse_id(char const** val_str);
@@ -260,11 +258,6 @@ Ref Expression::to_ref(Context& ctx) const
 	return m_impl->to_ref(ctx);
 }
 
-MPI_Comm Expression::to_mpi_comm(Context& ctx) const
-{
-	return m_impl->to_mpi_comm(ctx);
-}
-
 Expression::Impl::~Impl() = default;
 
 string Expression::Impl::to_string(Context& ctx) const
@@ -275,24 +268,6 @@ string Expression::Impl::to_string(Context& ctx) const
 	return result.str();
 }
 
-
-MPI_Comm Expression::Impl::to_mpi_comm(Context& ctx) const
-{
-	try {
-		Ref_r comm_ref = to_ref(ctx);
-		
-		if (auto&& scalar_type = dynamic_cast<const Scalar_datatype*>(&comm_ref.type())) {
-			if (scalar_type->kind() == Scalar_kind::MPI_COMM) {
-				const MPI_Comm* comm = static_cast<const MPI_Comm*>(comm_ref.get());
-				return *comm;
-			}
-		}
-	} catch (Error e) {
-		throw Error{PDI_ERR_TYPE, "Defined communicator is not a reference to MPI_Comm"};
-	}
-	throw Error{PDI_ERR_TYPE, "Trying to cast to MPI_Comm something that is not a MPI_Comm"};
-}
-
 unique_ptr<Expression::Impl> Expression::Impl::parse_term(char const** val_str)
 {
 	if (**val_str == '(') {
-- 
GitLab


From bd3429677ff85f1177ae1d56083b10047e1f62e4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Karol=20Sieroci=C5=84ski?= <ksiero@man.poznan.pl>
Date: Thu, 2 Aug 2018 13:50:38 +0200
Subject: [PATCH 3/3] Removed to_mpi_comm declaration

---
 include/pdi/expression.h | 6 ------
 1 file changed, 6 deletions(-)

diff --git a/include/pdi/expression.h b/include/pdi/expression.h
index 1ad410a06..e81fcdbda 100644
--- a/include/pdi/expression.h
+++ b/include/pdi/expression.h
@@ -155,12 +155,6 @@ public:
 	 */
 	Ref to_ref(Context& ctx) const;
 	
-	/** Evaluates an expression as a MPI_Comm
-	 *
-	 * \return the MPI communicator
-	 */
-	MPI_Comm to_mpi_comm(Context& ctx) const;
-	
 };
 
 } // namespace PDI
-- 
GitLab