VCluster.hpp 34.4 KB
Newer Older
incardon's avatar
incardon committed
1 2 3
#ifndef VCLUSTER
#define VCLUSTER

4
#include "config.h"
incardon's avatar
incardon committed
5
#include <mpi.h>
6
#include "MPI_wrapper/MPI_util.hpp"
incardon's avatar
incardon committed
7 8
#include "VCluster_object.hpp"
#include "Vector/map_vector.hpp"
9 10 11
#include "MPI_wrapper/MPI_IallreduceW.hpp"
#include "MPI_wrapper/MPI_IrecvW.hpp"
#include "MPI_wrapper/MPI_IsendW.hpp"
incardon's avatar
incardon committed
12
#include "MPI_wrapper/MPI_IAllGather.hpp"
incardon's avatar
incardon committed
13
#include <exception>
14
#include "Vector/map_vector.hpp"
15 16
#ifdef DEBUG
#include "util/check_no_pointers.hpp"
17
#include "util/util_debug.hpp"
18
#endif
incardon's avatar
incardon committed
19
#include "util/Vcluster_log.hpp"
20
#include "memory/BHeapMemory.hpp"
Yaroslav's avatar
Yaroslav committed
21
#include "Packer_Unpacker/has_max_prop.hpp"
incardon's avatar
incardon committed
22
#include "data_type/aggregate.hpp"
incardon's avatar
incardon committed
23

Pietro Incardona's avatar
Pietro Incardona committed
24 25 26 27
#ifdef HAVE_PETSC
#include <petscvec.h>
#endif

incardon's avatar
incardon committed
28 29
#define MSG_LENGTH 1024
#define MSG_SEND_RECV 1025
30
#define SEND_SPARSE 4096
incardon's avatar
incardon committed
31 32 33
#define NONE 1
#define NEED_ALL_SIZE 2

34 35
#define SERIVCE_MESSAGE_TAG 16384
#define SEND_RECV_BASE 8192
incardon's avatar
incardon committed
36
#define GATHER_BASE 24576
incardon's avatar
incardon committed
37

Pietro Incardona's avatar
Pietro Incardona committed
38
// number of vcluster instances
incardon's avatar
incardon committed
39
extern size_t n_vcluster;
Pietro Incardona's avatar
Pietro Incardona committed
40
// Global MPI initialization
incardon's avatar
incardon committed
41
extern bool global_mpi_init;
Pietro Incardona's avatar
Pietro Incardona committed
42 43
// initialization flag
extern bool ofp_initialized;
incardon's avatar
incardon committed
44 45
extern size_t tot_sent;
extern size_t tot_recv;
incardon's avatar
incardon committed
46

incardon's avatar
incardon committed
47 48 49 50 51 52 53
///////////////////// Post functions /////////////

template<typename T> void assign(T * ptr1, T * ptr2)
{
	*ptr1 = *ptr2;
};

incardon's avatar
incardon committed
54
//! Helper class to add data without serialization
incardon's avatar
incardon committed
55 56 57
template<bool sr>
struct op_ssend_recv_add_sr
{
incardon's avatar
incardon committed
58
	//! Add data
incardon's avatar
incardon committed
59 60 61 62 63 64 65
	template<typename T, typename D, typename S, int ... prp> static void execute(D & recv,S & v2, size_t i)
	{
		// Merge the information
		recv.template add_prp<typename T::value_type,PtrMemory,openfpm::grow_policy_identity,openfpm::vect_isel<typename T::value_type>::value,prp...>(v2);
	}
};

incardon's avatar
incardon committed
66
//! Helper class to add data with serialization
incardon's avatar
incardon committed
67 68 69
template<>
struct op_ssend_recv_add_sr<true>
{
incardon's avatar
incardon committed
70
	//! Add data
incardon's avatar
incardon committed
71 72 73 74 75 76 77
	template<typename T, typename D, typename S, int ... prp> static void execute(D & recv,S & v2, size_t i)
	{
		// Merge the information
		recv.template add_prp<typename T::value_type,HeapMemory,openfpm::grow_policy_double,openfpm::vect_isel<typename T::value_type>::value, prp...>(v2);
	}
};

incardon's avatar
incardon committed
78
//! Helper class to add data
incardon's avatar
incardon committed
79 80 81
template<typename op>
struct op_ssend_recv_add
{
incardon's avatar
incardon committed
82
	//! Add data
incardon's avatar
incardon committed
83 84 85 86 87 88 89
	template<bool sr, typename T, typename D, typename S, int ... prp> static void execute(D & recv,S & v2, size_t i)
	{
		// Merge the information
		op_ssend_recv_add_sr<sr>::template execute<T,D,S,prp...>(recv,v2,i);
	}
};

incardon's avatar
incardon committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
//! Helper class to merge data without serialization
template<bool sr,template<typename,typename> class op>
struct op_ssend_recv_merge_impl
{
	//! Merge the
	template<typename T, typename D, typename S, int ... prp> inline static void execute(D & recv,S & v2,size_t i,openfpm::vector<openfpm::vector<aggregate<size_t,size_t>>> & opart)
	{
		// Merge the information
		recv.template merge_prp_v<op,typename T::value_type, PtrMemory, openfpm::grow_policy_identity, prp...>(v2,opart.get(i));
	}
};

//! Helper class to merge data with serialization
template<template<typename,typename> class op>
struct op_ssend_recv_merge_impl<true,op>
{
	//! merge the data
	template<typename T, typename D, typename S, int ... prp> inline static void execute(D & recv,S & v2,size_t i,openfpm::vector<openfpm::vector<aggregate<size_t,size_t>>> & opart)
	{
		// Merge the information
		recv.template merge_prp_v<op,typename T::value_type, HeapMemory, openfpm::grow_policy_double, prp...>(v2,opart.get(i));
	}
};

//! Helper class to merge data
incardon's avatar
incardon committed
115 116 117
template<template<typename,typename> class op>
struct op_ssend_recv_merge
{
incardon's avatar
incardon committed
118
	//! For each processor contain the list of the particles with which I must merge the information
incardon's avatar
incardon committed
119 120
	openfpm::vector<openfpm::vector<aggregate<size_t,size_t>>> & opart;

incardon's avatar
incardon committed
121
	//! constructor
incardon's avatar
incardon committed
122 123 124 125
	op_ssend_recv_merge(openfpm::vector<openfpm::vector<aggregate<size_t,size_t>>> & opart)
	:opart(opart)
	{}

incardon's avatar
incardon committed
126
	//! execute the merge
incardon's avatar
incardon committed
127 128
	template<bool sr, typename T, typename D, typename S, int ... prp> void execute(D & recv,S & v2,size_t i)
	{
incardon's avatar
incardon committed
129
		op_ssend_recv_merge_impl<sr,op>::template execute<T,D,S,prp...>(recv,v2,i,opart);
incardon's avatar
incardon committed
130 131 132
	}
};

incardon's avatar
incardon committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
//! Helper class to merge data without serialization
template<bool sr>
struct op_ssend_gg_recv_merge_impl
{
	//! Merge the
	template<typename T, typename D, typename S, int ... prp> inline static void execute(D & recv,S & v2,size_t i,size_t & start)
	{
		// Merge the information
		recv.template merge_prp_v<replace_,typename T::value_type, PtrMemory, openfpm::grow_policy_identity, prp...>(v2,start);

		start += v2.size();
	}
};

//! Helper class to merge data with serialization
template<>
struct op_ssend_gg_recv_merge_impl<true>
{
	//! merge the data
	template<typename T, typename D, typename S, int ... prp> inline static void execute(D & recv,S & v2,size_t i,size_t & start)
	{
		// Merge the information
		recv.template merge_prp_v<replace_,typename T::value_type, HeapMemory, openfpm::grow_policy_double, prp...>(v2,start);

		// from
		start += v2.size();
	}
};

//! Helper class to merge data
struct op_ssend_gg_recv_merge
{
	//! starting marker
	size_t start;

	//! constructor
	op_ssend_gg_recv_merge(size_t start)
	:start(start)
	{}

	//! execute the merge
	template<bool sr, typename T, typename D, typename S, int ... prp> void execute(D & recv,S & v2,size_t i)
	{
		op_ssend_gg_recv_merge_impl<sr>::template execute<T,D,S,prp...>(recv,v2,i,start);
	}
};

incardon's avatar
incardon committed
180 181
//////////////////////////////////////////////////

incardon's avatar
incardon committed
182
//! temporal buffer for reductions
incardon's avatar
incardon committed
183 184
union red
{
incardon's avatar
incardon committed
185
	//! char
incardon's avatar
incardon committed
186
	char c;
incardon's avatar
incardon committed
187
	//! unsigned char
incardon's avatar
incardon committed
188
	unsigned char uc;
incardon's avatar
incardon committed
189
	//! signed
incardon's avatar
incardon committed
190
	short s;
incardon's avatar
incardon committed
191
	//! unsigned short
incardon's avatar
incardon committed
192
	unsigned short us;
incardon's avatar
incardon committed
193
	//! integer
incardon's avatar
incardon committed
194
	int i;
incardon's avatar
incardon committed
195
	//! unsigned integer
incardon's avatar
incardon committed
196
	unsigned int ui;
incardon's avatar
incardon committed
197
	//! float
incardon's avatar
incardon committed
198
	float f;
incardon's avatar
incardon committed
199
	//! double
incardon's avatar
incardon committed
200 201 202 203
	double d;
};


204
/*! \brief This class virtualize the cluster of PC as a set of processes that communicate
incardon's avatar
incardon committed
205
 *
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
 * At the moment it is an MPI-like interface, with a more type aware, and simple, interface.
 *  It also give some more complex communication functionalities like **Dynamic Sparse Data Exchange**
 *
 * Actually VCluster expose a Computation driven parallelism (MPI-like), with a plan of extending to
 * communication driven parallelism
 *
 * * In computation driven parallelism, the program compute than communicate to the other processors
 *
 * * In a communication driven parallelism, (Charm++ or HPX), the program receive messages, this receiving
 *   messages trigger computation
 *
 * ### An example of sending and receive plain buffers
 * \snippet VCluster_unit_test_util.hpp Send and receive plain buffer data
 * ### An example of sending vectors of primitives with (T=float,double,lont int,...)
 * \snippet VCluster_unit_test_util.hpp Sending and receiving primitives
 * ### An example of sending vectors of complexes object
 * \snippet VCluster_unit_test_util.hpp Send and receive vectors of complex
 * ### An example of gathering numbers from all processors
 * \snippet VCluster_unit_test_util.hpp allGather numbers
incardon's avatar
incardon committed
225 226 227 228 229
 *
 */

class Vcluster
{
incardon's avatar
incardon committed
230
	//! log file
incardon's avatar
incardon committed
231 232
	Vcluster_log log;

233 234 235
	//! NBX has a potential pitfall that must be addressed,
	//! NBX Send all the messages and probe for incoming messages,
	//! if there is an incoming message it receive it producing
236
	//! an acknowledge notification on the sending processor.
237 238 239 240 241 242
	//! When all the sends has been acknowledged, the processor call the MPI_Ibarrier
	//! when all the processors call MPI_Ibarrier all send has been received.
	//! While the processors are waiting for the MPI_Ibarrier to complete, all processors
	//! are still probing for incoming message, Unfortunately some processor
	//! can quit the MPI_Ibarrier before others and this mean that some
	//! processor can exit the probing status before others, these processors can in theory
243 244 245 246
	//! start new communications while the other processor are still in probing status producing
	//! a wrong send/recv association to
	//! resolve this problem an incremental NBX_cnt is used as message TAG to distinguish that the
	//! messages come from other send or subsequent NBX procedures
incardon's avatar
incardon committed
247
	size_t NBX_cnt;
248

incardon's avatar
incardon committed
249 250
	//! temporal vector used for meta-communication
	//! ( or meta-data before the real communication )
incardon's avatar
incardon committed
251 252
	openfpm::vector<size_t> proc_com;

incardon's avatar
incardon committed
253
	//! vector that contain the scatter map (it is basically an array of one)
incardon's avatar
incardon committed
254 255
	openfpm::vector<int> map_scatter;

incardon's avatar
incardon committed
256
	//! vector of MPI requests
incardon's avatar
incardon committed
257 258
	openfpm::vector<MPI_Request> req;

incardon's avatar
incardon committed
259
	//! vector of MPI status
incardon's avatar
incardon committed
260 261
	openfpm::vector<MPI_Status> stat;

incardon's avatar
incardon committed
262
	//! vector of functions to execute after all the request has been performed
incardon's avatar
incardon committed
263 264 265 266 267 268 269
	std::vector<int> post_exe;

	// Object array


	// Single objects

incardon's avatar
incardon committed
270
	//! number of processes
incardon's avatar
incardon committed
271
	int size;
incardon's avatar
incardon committed
272
	//! actual rank
incardon's avatar
incardon committed
273 274
	int rank;

incardon's avatar
incardon committed
275
	//! number of processing unit per process
incardon's avatar
incardon committed
276 277
	int numPE = 1;

incardon's avatar
incardon committed
278
	/*! This buffer is a temporal buffer for reductions
incardon's avatar
incardon committed
279 280 281 282 283 284 285 286
	 *
	 * MPI_Iallreduce does not accept recv and send buffer to be the same
	 * r is used to overcome this problem (is given as second parameter)
	 * after the execution the data is copied back
	 *
	 */
	std::vector<red> r;

incardon's avatar
incardon committed
287
	//! vector of pointers of send buffers
288 289
	openfpm::vector<void *> ptr_send;

incardon's avatar
incardon committed
290
	//! vector of the size of send buffers
291 292
	openfpm::vector<size_t> sz_send;

incardon's avatar
incardon committed
293
	//! sending map
294 295
	openfpm::vector<size_t> map;

incardon's avatar
incardon committed
296
	//! Receive buffers
297
	openfpm::vector<BHeapMemory> recv_buf;
298

incardon's avatar
incardon committed
299
	//! barrier request
300
	MPI_Request bar_req;
incardon's avatar
incardon committed
301 302

	//! barrier status
303 304
	MPI_Status bar_stat;

incardon's avatar
incardon committed
305
	//! disable operator=
Pietro Incardona's avatar
Pietro Incardona committed
306 307
	Vcluster & operator=(const Vcluster &)	{return *this;};

incardon's avatar
incardon committed
308 309 310 311
	//! disable copy constructor
	Vcluster(const Vcluster &)
	:NBX_cnt(0)
	{};
Pietro Incardona's avatar
Pietro Incardona committed
312

incardon's avatar
incardon committed
313 314 315 316 317
public:

	// Finalize the MPI program
	~Vcluster()
	{
Pietro Incardona's avatar
Pietro Incardona committed
318 319 320
#ifdef SE_CLASS2
		check_delete(this);
#endif
incardon's avatar
incardon committed
321 322 323 324
		n_vcluster--;

		// if there are no other vcluster instances finalize
		if (n_vcluster == 0)
Pietro Incardona's avatar
Pietro Incardona committed
325
		{
326 327 328 329
			int already_finalised;

			MPI_Finalized(&already_finalised);
			if (!already_finalised)
Pietro Incardona's avatar
Pietro Incardona committed
330
			{
331 332 333 334
				if (MPI_Finalize() != 0)
				{
					std::cerr << __FILE__ << ":" << __LINE__  << " MPI_Finalize FAILED \n";
				}
Pietro Incardona's avatar
Pietro Incardona committed
335
			}
Pietro Incardona's avatar
Pietro Incardona committed
336
		}
incardon's avatar
incardon committed
337 338
	}

339 340 341 342 343 344
	/*! \brief Virtual cluster constructor
	 *
	 * \param argc pointer to arguments counts passed to the program
	 * \param argv pointer to arguments vector passed to the program
	 *
	 */
incardon's avatar
incardon committed
345
	Vcluster(int *argc, char ***argv)
incardon's avatar
incardon committed
346
	:NBX_cnt(0)
incardon's avatar
incardon committed
347
	{
Pietro Incardona's avatar
Pietro Incardona committed
348 349 350 351
#ifdef SE_CLASS2
		check_new(this,8,VCLUSTER_EVENT,PRJ_VCLUSTER);
#endif

incardon's avatar
incardon committed
352 353
		n_vcluster++;

354 355 356
		int already_initialised;
		MPI_Initialized(&already_initialised);

incardon's avatar
incardon committed
357
		// Check if MPI is already initialized
358
		if (!already_initialised)
incardon's avatar
incardon committed
359
		{
incardon's avatar
incardon committed
360

incardon's avatar
incardon committed
361 362
			MPI_Init(argc,argv);
		}
incardon's avatar
incardon committed
363

364 365
		// Get the total number of process
		// and the rank of this process
incardon's avatar
incardon committed
366 367 368 369

		MPI_Comm_size(MPI_COMM_WORLD, &size);
		MPI_Comm_rank(MPI_COMM_WORLD, &rank);

370
#ifdef SE_CLASS2
incardon's avatar
incardon committed
371 372 373
			process_v_cl = rank;
#endif

374
		// create and fill map scatter with one
incardon's avatar
incardon committed
375 376 377 378 379 380
		map_scatter.resize(size);

		for (size_t i = 0 ; i < map_scatter.size() ; i++)
		{
			map_scatter.get(i) = 1;
		}
incardon's avatar
incardon committed
381 382 383

		// open the log file
		log.openLog(rank);
384 385 386 387

		// Initialize bar_req
		bar_req = MPI_Request();
		bar_stat = MPI_Status();
incardon's avatar
incardon committed
388 389
	}

390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
#ifdef DEBUG

	/*! \brief Check for wrong types
	 *
	 * In general we do not know if a type T make sense to be sent or not, but if it has pointer
	 * inside it does not. This function check if the basic type T has a method called noPointers,
	 * This function in general notify if T has internally pointers. If T has pointer an error
	 * is printed, is T does not have the method a WARNING is printed
	 *
	 * \tparam T type to check
	 *
	 */
	template<typename T> void checkType()
	{
		// if T is a primitive like int, long int, float, double, ... make sense
		// (pointers, l-references and r-references are not fundamentals)
		if (std::is_fundamental<T>::value == true)
			return;

		// if it is a pointer make no sense
		if (std::is_pointer<T>::value == true)
			std::cerr << "Error: " << __FILE__ << ":" << __LINE__ << " the type " << demangle(typeid(T).name()) << " is a pointer, sending pointers values has no sense\n";

		// if it is an l-value reference make no send
		if (std::is_lvalue_reference<T>::value == true)
			std::cerr << "Error: " << __FILE__ << ":" << __LINE__ << " the type " << demangle(typeid(T).name()) << " is a pointer, sending pointers values has no sense\n";

		// if it is an r-value reference make no send
		if (std::is_rvalue_reference<T>::value == true)
			std::cerr << "Error: " << __FILE__ << ":" << __LINE__ << " the type " << demangle(typeid(T).name()) << " is a pointer, sending pointers values has no sense\n";

		// ... if not, check that T has a method called noPointers
		switch (check_no_pointers<T>::value())
		{
			case PNP::UNKNOWN:
			{
incardon's avatar
incardon committed
426
				std::cerr << "Warning: " << __FILE__ << ":" << __LINE__ << " impossible to check the type " << demangle(typeid(T).name()) << " please consider to add a static method \"static bool noPointers()\" \n" ;
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
				break;
			}
			case PNP::POINTERS:
			{
				std::cerr << "Error: " << __FILE__ << ":" << __LINE__ << " the type " << demangle(typeid(T).name()) << " has pointers inside, sending pointers values has no sense\n";
				break;
			}
			default:
			{

			}
		}
	}

#endif

Pietro Incardona's avatar
Pietro Incardona committed
443 444 445 446 447 448 449 450 451 452
	/*! \brief Get the MPI_Communicator (or processor group) this VCluster is using
	 *
	 * \return MPI comunicator
	 *
	 */
	MPI_Comm getMPIComm()
	{
		return MPI_COMM_WORLD;
	}

incardon's avatar
incardon committed
453 454 455 456 457 458 459 460 461 462 463 464
	//! Get the total number of processing units
	size_t getProcessingUnits()
	{
		return size*numPE;
	}

	//! Get the process unit id
	size_t getProcessUnitID()
	{
		return rank;
	}

465
	/*! \brief Sum the numbers across all processors and get the result
incardon's avatar
incardon committed
466 467 468 469 470
	 *
	 * \param num to reduce, input and output
	 *
	 */

471
	template<typename T> void sum(T & num)
incardon's avatar
incardon committed
472
	{
473 474 475 476
#ifdef DEBUG
		checkType<T>();
#endif

incardon's avatar
incardon committed
477 478 479 480 481 482 483 484 485
		// reduce over MPI

		// Create one request
		req.add();

		// reduce
		MPI_IallreduceW<T>::reduce(num,MPI_SUM,req.last());
	}

486
	/*! \brief Get the maximum number across all processors (or reduction with infinity norm)
incardon's avatar
incardon committed
487 488 489 490 491 492
	 *
	 * \param num to reduce
	 *
	 */
	template<typename T> void max(T & num)
	{
493 494 495
#ifdef DEBUG
		checkType<T>();
#endif
incardon's avatar
incardon committed
496 497 498 499 500 501 502 503 504
		// reduce over MPI

		// Create one request
		req.add();

		// reduce
		MPI_IallreduceW<T>::reduce(num,MPI_MAX,req.last());
	}

tonynsyde's avatar
tonynsyde committed
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
	/*! \brief Get the minimum number across all processors (or reduction with insinity norm)
	 *
	 * \param num to reduce
	 *
	 */

	template<typename T> void min(T & num)
	{
#ifdef DEBUG
		checkType<T>();
#endif
		// reduce over MPI

		// Create one request
		req.add();

		// reduce
		MPI_IallreduceW<T>::reduce(num,MPI_MIN,req.last());
	}

525

526
	/*! \brief Send and receive multiple messages
527
	 *
528 529 530 531 532 533 534 535 536 537 538 539
	 * It send multiple messages to a set of processors the and receive
	 * multiple messages from another set of processors, all the processor must call this
	 * function, NBX is more performant than PCX with more processors (1000+)
	 *
	 * suppose the following situation the calling processor want to communicate
	 * * 2 vector of 100 integers to processor 1
	 * * 1 vector of 50 integers to processor 6
	 * * 1 vector of 48 integers to processor 7
	 * * 1 vector of 70 integers to processor 8
	 *
	 * \param prc list of processors you should communicate with [1,1,6,7,8]
	 *
incardon's avatar
incardon committed
540
	 * \param data vector containing the data to send [v=vector<vector<int>>, v.size()=4, T=vector<int>], T at the moment
541 542 543 544 545 546 547 548 549 550 551 552
	 *          is only tested for vectors of 0 or more generic elements (without pointers)
	 *
	 * \param msg_alloc This is a call-back with the purpose to allocate space
	 *        for the incoming messages and give back a valid pointer, supposing that this call-back has been triggered by
	 *        the processor of id 5 that want to communicate with me a message of size 100 byte the call-back will have
	 *        the following 6 parameters
	 *        in the call-back in order:
	 *        * message size required to receive the message (100)
	 *        * total message size to receive from all the processors (NBX does not provide this information)
	 *        * the total number of processor want to communicate with you (NBX does not provide this information)
	 *        * processor id (5)
	 *        * ri request id (it is an id that goes from 0 to total_p, and is incremented
553
	 *           every time message_alloc is called)
554
	 *        * void pointer, parameter for additional data to pass to the call-back
555
	 *
incardon's avatar
incardon committed
556 557
	 * \param ptr_arg data passed to the call-back function specified
	 *
558
	 * \param opt options, only NONE supported
559 560 561 562
	 *
	 */
	template<typename T> void sendrecvMultipleMessagesNBX(openfpm::vector< size_t > & prc, openfpm::vector< T > & data, void * (* msg_alloc)(size_t,size_t,size_t,size_t,size_t,void *), void * ptr_arg, long int opt=NONE)
	{
563 564 565
#ifdef DEBUG
		checkType<typename T::value_type>();
#endif
566 567 568 569 570 571 572 573 574 575 576 577 578
		// resize the pointer list
		ptr_send.resize(prc.size());
		sz_send.resize(prc.size());

		for (size_t i = 0 ; i < prc.size() ; i++)
		{
			ptr_send.get(i) = data.get(i).getPointer();
			sz_send.get(i) = data.get(i).size() * sizeof(typename T::value_type);
		}

		sendrecvMultipleMessagesNBX(prc.size(),(size_t *)sz_send.getPointer(),(size_t *)prc.getPointer(),(void **)ptr_send.getPointer(),msg_alloc,ptr_arg,opt);
	}

incardon's avatar
incardon committed
579 580
	/*! \brief Send and receive multiple messages
	 *
581 582 583 584 585 586 587 588 589 590 591 592
	 * It sends multiple messages to a set of processors and receives
	 * multiple messages from another set of processors, all the processor must call this
	 * function, NBX is more performant than PCX with more processors (1000+)
	 *
	 * suppose the following situation the calling processor want to communicate
	 * * 2 vector of 100 integers to processor 1
	 * * 1 vector of 50 integers to processor 6
	 * * 1 vector of 48 integers to processor 7
	 * * 1 vector of 70 integers to processor 8
	 *
	 * \param prc list of processors you should communicate with [1,1,6,7,8]
	 *
incardon's avatar
incardon committed
593
	 * \param data vector containing the data to send [v=vector<vector<int>>, v.size()=4, T=vector<int>], T at the moment
594 595 596 597 598 599 600 601 602 603 604 605
	 *          is only tested for vectors of 0 or more generic elements (without pointers)
	 *
	 * \param msg_alloc This is a call-back with the purpose to allocate space
	 *        for the incoming messages and give back a valid pointer, supposing that this call-back has been triggered by
	 *        the processor of id 5 that want to communicate with me a message of size 100 byte the call-back will have
	 *        the following 6 parameters
	 *        in the call-back in order:
	 *        * message size required to receive the message [100]
	 *        * total message size to receive from all the processors (NBX does not provide this information)
	 *        * the total number of processor want to communicate with you (NBX does not provide this information)
	 *        * processor id [5]
	 *        * ri request id (it is an id that goes from 0 to total_p, and is incremented
606
	 *           every time message_alloc is called)
607
	 *        * void pointer, parameter for additional data to pass to the call-back
incardon's avatar
incardon committed
608
	 *
incardon's avatar
incardon committed
609 610
	 * \param ptr_arg data passed to the call-back function specified
	 *
611
	 * \param opt options, only NONE supported
incardon's avatar
incardon committed
612 613
	 *
	 */
614
	template<typename T> void sendrecvMultipleMessagesPCX(openfpm::vector< size_t > & prc, openfpm::vector< T > & data, void * (* msg_alloc)(size_t,size_t,size_t,size_t,size_t,void *), void * ptr_arg, long int opt=NONE)
incardon's avatar
incardon committed
615
	{
616 617 618
#ifdef DEBUG
		checkType<typename T::value_type>();
#endif
incardon's avatar
incardon committed
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
		// resize map with the number of processors
		map.resize(size);

		// reset the sending buffer
		map.fill(0);

		// create sending map
		for (size_t i = 0 ; i < prc.size() ; i++)
		{map.get(prc.get(i)) = 1;}

		// resize the pointer list
		ptr_send.resize(prc.size());
		sz_send.resize(prc.size());

		for (size_t i = 0 ; i < prc.size() ; i++)
		{
			ptr_send.get(i) = data.get(i).getPointer();
			sz_send.get(i) = data.get(i).size() * sizeof(typename T::value_type);
		}

639 640 641
		sendrecvMultipleMessagesPCX(prc.size(),(size_t *)map.getPointer(),(size_t *)sz_send.getPointer(),(size_t *)prc.getPointer(),(void **)ptr_send.getPointer(),msg_alloc,ptr_arg,opt);
	}

642
	/*! \brief Send and receive multiple messages
643
	 *
644 645 646
	 * It send multiple messages to a set of processors the and receive
	 * multiple messages from another set of processors, all the processor must call this
	 * function, NBX is more performant than PCX with more processors (1000+)
647
	 *
648 649 650 651 652
	 * suppose the following situation the calling processor want to communicate
	 * * 2 messages of size 100 byte to processor 1
	 * * 1 message of size 50 byte to processor 6
	 * * 1 message of size 48 byte to processor 7
	 * * 1 message of size 70 byte to processor 8
653
	 *
654
	 * \param n_send number of send for this processor [4]
655 656
	 *
	 * \param prc list of processor with which it should communicate
657
	 *        [1,1,6,7,8]
658
	 *
659 660 661 662
	 * \param sz the array contain the size of the message for each processor
	 *        (zeros must not be presents) [100,100,50,48,70]
	 *
	 * \param ptr array that contain the pointers to the message to send
663 664
	 *
	 * \param msg_alloc This is a call-back with the purpose of allocate space
665 666 667
	 *        for the incoming message and give back a valid pointer, supposing that this call-back has been triggered by
	 *        the processor of id 5 that want to communicate with me a message of size 100 byte the call-back will have
	 *        the following 6 parameters
668
	 *        in the call-back are in order:
669 670 671 672 673
	 *        * message size required to receive the message [100]
	 *        * total message size to receive from all the processors (NBX does not provide this information)
	 *        * the total number of processor want to communicate with you (NBX does not provide this information)
	 *        * processor id [5]
	 *        * ri request id (it is an id that goes from 0 to total_p, and is incremented
674
	 *           every time message_alloc is called)
675
	 *        * void pointer, parameter for additional data to pass to the call-back
676
	 *
incardon's avatar
incardon committed
677 678
	 * \param ptr_arg data passed to the call-back function specified
	 *
679 680 681
	 * \param opt options, NONE (ignored in this moment)
	 *
	 */
682
	void sendrecvMultipleMessagesNBX(size_t n_send , size_t sz[], size_t prc[] , void * ptr[], void * (* msg_alloc)(size_t,size_t,size_t,size_t,size_t,void *), void * ptr_arg, long int opt = NONE)
683
	{
684 685 686 687 688
		if (stat.size() != 0 || req.size() != 0)
			std::cerr << "Error: " << __FILE__ << ":" << __LINE__ << " this function must be called when no other requests are in progress \n";


		stat.clear();
689 690 691 692 693
		req.clear();
		// Do MPI_Issend

		for (size_t i = 0 ; i < n_send ; i++)
		{
incardon's avatar
incardon committed
694 695 696
			if (sz[i] != 0)
			{
				req.add();
697 698 699 700 701

#ifdef SE_CLASS2
				check_valid(ptr[i],sz[i]);
#endif

incardon's avatar
incardon committed
702
				tot_sent += sz[i];
703
				MPI_SAFE_CALL(MPI_Issend(ptr[i], sz[i], MPI_BYTE, prc[i], SEND_SPARSE + NBX_cnt, MPI_COMM_WORLD,&req.last()));
incardon's avatar
incardon committed
704
				log.logSend(prc[i]);
incardon's avatar
incardon committed
705
			}
706 707 708 709 710 711 712
		}

		size_t rid = 0;
		int flag = false;

		bool reached_bar_req = false;

incardon's avatar
incardon committed
713 714
		log.start(10);

715 716 717
		// Wait that all the send are acknowledge
		do
		{
incardon's avatar
incardon committed
718

719 720 721 722 723
			// flag that notify that this processor reach the barrier
			// Barrier request

			MPI_Status stat_t;
			int stat = false;
724
			MPI_SAFE_CALL(MPI_Iprobe(MPI_ANY_SOURCE,SEND_SPARSE + NBX_cnt,MPI_COMM_WORLD,&stat,&stat_t));
725

726
			// If I have an incoming message and is related to this NBX communication
727 728 729 730 731 732 733 734 735
			if (stat == true)
			{
				// Get the message size
				int msize;
				MPI_SAFE_CALL(MPI_Get_count(&stat_t,MPI_BYTE,&msize));

				// Get the pointer to receive the message
				void * ptr = msg_alloc(msize,0,0,stat_t.MPI_SOURCE,rid,ptr_arg);

incardon's avatar
incardon committed
736 737 738
				// Log the receiving request
				log.logRecv(stat_t);

739 740
				rid++;

741 742 743 744
				// Check the pointer
#ifdef SE_CLASS2
				check_valid(ptr,msize);
#endif
incardon's avatar
incardon committed
745
				tot_recv += msize;
746
				MPI_SAFE_CALL(MPI_Recv(ptr,msize,MPI_BYTE,stat_t.MPI_SOURCE,SEND_SPARSE+NBX_cnt,MPI_COMM_WORLD,&stat_t));
747 748 749 750

#ifdef SE_CLASS2
				check_valid(ptr,msize);
#endif
751 752 753 754 755 756 757
			}

			// Check the status of all the MPI_issend and call the barrier if finished

			if (reached_bar_req == false)
			{
				int flag = false;
incardon's avatar
incardon committed
758 759 760 761
				if (req.size() != 0)
				{MPI_SAFE_CALL(MPI_Testall(req.size(),&req.get(0),&flag,MPI_STATUSES_IGNORE));}
				else
					flag = true;
762 763 764 765 766 767

				// If all send has been completed
				if (flag == true)
				{MPI_SAFE_CALL(MPI_Ibarrier(MPI_COMM_WORLD,&bar_req));reached_bar_req = true;}
			}

incardon's avatar
incardon committed
768
			// Check if all processor reached the async barrier
769
			if (reached_bar_req)
incardon's avatar
incardon committed
770
			{MPI_SAFE_CALL(MPI_Test(&bar_req,&flag,&bar_stat))};
incardon's avatar
incardon committed
771 772

			// produce a report if communication get stuck
incardon's avatar
incardon committed
773
			log.NBXreport(NBX_cnt,req,reached_bar_req,bar_stat);
incardon's avatar
incardon committed
774

775 776 777 778 779 780
		} while (flag == false);

		// Remove the executed request

		req.clear();
		stat.clear();
incardon's avatar
incardon committed
781
		log.clear();
782 783 784

		// Circular counter
		NBX_cnt = (NBX_cnt + 1) % 1024;
incardon's avatar
incardon committed
785 786 787 788
	}

	/*! \brief Send and receive multiple messages
	 *
789 790 791
	 * It send multiple messages to a set of processors the and receive
	 * multiple messages from another set of processors, all the processor must call this
	 * function, NBX is more performant than PCX with more processors (1000+)
incardon's avatar
incardon committed
792
	 *
793 794 795 796 797
	 * suppose the following situation the calling processor want to communicate
	 * * 2 messages of size 100 byte to processor 1
	 * * 1 message of size 50 byte to processor 6
	 * * 1 message of size 48 byte to processor 7
	 * * 1 message of size 70 byte to processor 8
incardon's avatar
incardon committed
798
	 *
799
	 * \param n_send number of send for this processor [4]
incardon's avatar
incardon committed
800
	 *
801 802
	 * \param prc list of processor with which it should communicate
	 *        [1,1,6,7,8]
incardon's avatar
incardon committed
803
	 *
804 805 806
	 * \param map array containing an array of the number of messages for
	 *        each processor the colling processor want to communicate
	 *        [0 2 0 0 0 0 1 1 1]
incardon's avatar
incardon committed
807
	 *
808 809
	 * \param sz the array contain the size of the message for each processor
	 *        (zeros must not be presents) [100,100,50,48,70]
incardon's avatar
incardon committed
810
	 *
811
	 * \param ptr array that contain the pointers to the message to send
incardon's avatar
incardon committed
812 813
	 *
	 * \param msg_alloc This is a call-back with the purpose of allocate space
814 815 816 817 818 819 820 821 822 823 824
	 *        for the incoming message and give back a valid pointer, supposing that this call-back has been triggered by
	 *        the processor of id 5 that want to communicate with me a message of size 100 byte the call-back will have
	 *        the following 6 parameters
	 *        in the call-back are in order:
	 *        * message size required to receive the message [100]
	 *        * total message size to receive from all the processors
	 *        * the total number of processor want to communicate with the calling processor
	 *        * processor id [5]
	 *        * ri request id (it is an id that goes from 0 to total_p, and is incremented
	 *           every time message_alloc is called)
	 *        * void pointer, parameter for additional data to pass to the call-back
incardon's avatar
incardon committed
825
	 *
incardon's avatar
incardon committed
826 827
	 * \param ptr_arg pointer passed to the call-back function
	 *
828
	 * \param opt options, NONE (ignored in this moment)
incardon's avatar
incardon committed
829 830
	 *
	 */
831
	void sendrecvMultipleMessagesPCX(size_t n_send, size_t * map, size_t sz[], size_t prc[] , void * ptr[], void * (* msg_alloc)(size_t,size_t,size_t,size_t,size_t,void *), void * ptr_arg, long int opt)
incardon's avatar
incardon committed
832
	{
833 834 835 836
		if (stat.size() != 0 || req.size() != 0)
			std::cerr << "Error: " << __FILE__ << ":" << __LINE__ << " this function must be called when no other requests are in progress \n";

		stat.clear();
incardon's avatar
incardon committed
837
		req.clear();
838

incardon's avatar
incardon committed
839
		req.add();
incardon's avatar
incardon committed
840
		stat.add();
incardon's avatar
incardon committed
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873

		proc_com.resize(1);

		MPI_SAFE_CALL(MPI_Ireduce_scatter(map,&proc_com.get(0),&map_scatter.get(0),MPI_UNSIGNED_LONG,MPI_SUM,MPI_COMM_WORLD,&req.last()));
		MPI_SAFE_CALL(MPI_Waitall(req.size(),&req.get(0),&stat.get(0)));

		// Remove the executed request

		req.clear();
		stat.clear();

		// Allocate the temporal buffer to store the message size for each processor

		size_t n_proc_com = proc_com.get(0);
		proc_com.resize(n_proc_com * 2);

		// queue n_proc_com MPI_Irecv with source ANY_SOURCE to get
		// the message length from each processor and
		//  send the message length to each processor

		for (size_t i = 0 ; i < n_proc_com ; i++)
		{
			req.add();
			MPI_SAFE_CALL(MPI_Irecv(&proc_com.get(i),1,MPI_UNSIGNED_LONG,MPI_ANY_SOURCE,MSG_LENGTH,MPI_COMM_WORLD,&req.last()));
		}

		for (size_t i = 0 ; i < n_send ; i++)
		{
			req.add();
			MPI_SAFE_CALL(MPI_Isend(&sz[i],1,MPI_UNSIGNED_LONG,prc[i],MSG_LENGTH,MPI_COMM_WORLD,&req.last()));
		}

		stat.resize(req.size());
874
		if (req.size() != 0) {MPI_SAFE_CALL(MPI_Waitall(req.size(),&req.get(0),&stat.get(0)));}
incardon's avatar
incardon committed
875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900

		// Use proc_com to get the processor id that try to communicate

		for (size_t i = 0 ; i < n_proc_com ; i++)
		{
			proc_com.get(n_proc_com+i) = stat.get(i).MPI_SOURCE;
		}

		// Remove the executed request

		req.clear();
		stat.clear();

		// Calculate the total size of the message

		size_t total_msg = 0;

		for (size_t i = 0 ; i < n_proc_com ; i++)
		{
			total_msg += proc_com.get(i);
		}

		// Receive the message

		for (size_t i = 0 ; i < n_proc_com ; i++)
		{
incardon's avatar
incardon committed
901
			void * ptr = msg_alloc(proc_com.get(i),total_msg,n_proc_com,proc_com.get(n_proc_com+i),i,ptr_arg);
incardon's avatar
incardon committed
902 903

			req.add();
904 905 906 907 908

#ifdef SE_CLASS2
				check_valid(ptr,proc_com.get(i));
#endif

incardon's avatar
incardon committed
909 910 911 912 913 914 915 916
			MPI_SAFE_CALL(MPI_Irecv(ptr,proc_com.get(i),MPI_BYTE,proc_com.get(i+n_proc_com),MSG_SEND_RECV,MPI_COMM_WORLD,&req.last()));
		}

		// Send all the messages this processor must send

		for (size_t i = 0 ; i < n_send ; i++)
		{
			req.add();
917 918 919 920 921

#ifdef SE_CLASS2
			check_valid(ptr[i],sz[i]);
#endif

incardon's avatar
incardon committed
922 923 924 925
			MPI_SAFE_CALL(MPI_Isend(ptr[i],sz[i],MPI_BYTE,prc[i],MSG_SEND_RECV,MPI_COMM_WORLD,&req.last()));
		}

		stat.resize(req.size());
926
		if (req.size() != 0) {MPI_SAFE_CALL(MPI_Waitall(req.size(),&req.get(0),&stat.get(0)));}
incardon's avatar
incardon committed
927 928 929 930 931 932

		// Remove the executed request
		req.clear();
		stat.clear();
	}

933 934 935 936 937 938
	/*! \brief Send data to a processor
	 *
	 * \warning In order to avoid deadlock every send must be coupled with a recv
	 *          in case you want to send data without knowledge from the other side
	 *          consider to use sendRecvMultipleMessages
	 *
incardon's avatar
incardon committed
939 940 941 942 943 944 945 946 947 948 949 950
	 * \warning operation is asynchronous execute must be called to ensure they are executed
	 *
	 * \see sendRecvMultipleMessages
	 *
	 * \param proc processor id
	 * \param tag id
	 * \param mem buffer with the data to send
	 * \param sz size
	 *
	 * \return true if succeed false otherwise
	 *
	 */
951
	bool send(size_t proc, size_t tag, const void * mem, size_t sz)
incardon's avatar
incardon committed
952 953 954 955 956 957 958 959 960 961 962 963
	{
		// send over MPI

		// Create one request
		req.add();

		// send
		MPI_IsendWB::send(proc,SEND_RECV_BASE + tag,mem,sz,req.last());

		return true;
	}

incardon's avatar
incardon committed
964

incardon's avatar
incardon committed
965 966 967 968 969 970 971
	/*! \brief Send data to a processor
	 *
	 * \warning In order to avoid deadlock every send must be coupled with a recv
	 *          in case you want to send data without knowledge from the other side
	 *          consider to use sendRecvMultipleMessages
	 *
	 * \warning operation is asynchronous execute must be called to ensure they are executed
972 973 974 975 976 977 978
	 *
	 * \see sendRecvMultipleMessages
	 *
	 * \param proc processor id
	 * \param tag id
	 * \param v buffer to send
	 *
979 980
	 * \return true if succeed false otherwise
	 *
981
	 */
982
	template<typename T, typename Mem, typename gr> bool send(size_t proc, size_t tag, openfpm::vector<T,Mem,gr> & v)
983 984 985 986 987 988 989 990 991 992
	{
#ifdef DEBUG
		checkType<T>();
#endif

		// send over MPI

		// Create one request
		req.add();

993
		// send
994
		MPI_IsendW<T,Mem,gr>::send(proc,SEND_RECV_BASE + tag,v,req.last());
995 996

		return true;
997 998 999 1000 1001 1002
	}

	/*! \brief Recv data from a processor
	 *
	 * \warning In order to avoid deadlock every recv must be coupled with a send
	 *          in case you want to send data without knowledge from the other side
1003
	 *          consider to use sendrecvMultipleMessagesPCX or sendrecvMultipleMessagesNBX
1004
	 *
incardon's avatar
incardon committed
1005
	 * \warning operation is asynchronous execute must be called to ensure they are executed
1006
	 *
1007
	 * \see sendrecvMultipleMessagesPCX sendrecvMultipleMessagesNBX
1008 1009 1010 1011
	 *
	 * \param proc processor id
	 * \param tag id
	 * \param v buffer to send
incardon's avatar
incardon committed
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
	 * \param sz size of the buffer
	 *
	 * \return true if succeed false otherwise
	 *
	 */
	bool recv(size_t proc, size_t tag, void * v, size_t sz)
	{
		// recv over MPI

		// Create one request
		req.add();

		// receive
		MPI_IrecvWB::recv(proc,SEND_RECV_BASE + tag,v,sz,req.last());

		return true;
	}

incardon's avatar
incardon committed
1030 1031 1032 1033
    /*! \brief Recv data from a processor
     *
     * \warning In order to avoid deadlock every recv must be coupled with a send
     *          in case you want to send data without knowledge from the other side
1034
     *          consider to use sendrecvMultipleMessagesPCX sendrecvMultipleMessagesNBX
incardon's avatar
incardon committed
1035 1036 1037
     *
     * \warning operation is asynchronous execute must be called to ensure they are executed
     *
1038
     * \see sendrecvMultipleMessagesPCX sendrecvMultipleMessagesNBX
incardon's avatar
incardon committed
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
     *
     * \param proc processor id
     * \param tag id
     * \param v vector to send
     *
     * \return true if succeed false otherwise
     *
     */
    template<typename T, typename Mem, typename gr> bool recv(size_t proc, size_t tag, openfpm::vector<T,Mem,gr> & v)
    {
#ifdef DEBUG
            checkType<T>();
#endif

            // recv over MPI

            // Create one request
            req.add();

            // receive
            MPI_IrecvW<T>::recv(proc,SEND_RECV_BASE + tag,v,req.last());

            return true;
    }

	/*! \brief Gather the data from all processors
1065 1066
	 *
	 * send a primitive data T receive the same primitive T from all the other processors
incardon's avatar
incardon committed
1067 1068 1069
	 *
	 * \warning operation is asynchronous execute must be called to ensure they are executed
	 *
1070
	 * \param v vector to receive (automaticaly resized)
incardon's avatar
incardon committed
1071
	 * \param send data to send
1072
	 *
1073 1074
	 * \return true if succeed false otherwise
	 *
1075
	 */
incardon's avatar
incardon committed
1076
	template<typename T, typename Mem, typename gr> bool allGather(T & send, openfpm::vector<T,Mem,gr> & v)
1077 1078 1079 1080 1081 1082 1083 1084
	{
#ifdef DEBUG
		checkType<T>();
#endif

		// Create one request
		req.add();

incardon's avatar
incardon committed
1085 1086 1087
		// Number of processors
		v.resize(getProcessingUnits());

1088
		// gather
incardon's avatar
incardon committed
1089
		MPI_IAllGatherW<T>::gather(&send,1,v.getPointer(),1,req.last());
1090 1091

		return true;
1092 1093 1094
	}

	/*! \brief Execute all the requests
incardon's avatar
incardon committed
1095 1096 1097 1098
	 *
	 */
	void execute()
	{
1099 1100 1101 1102
		// if req == 0 return
		if (req.size() == 0)
			return;

incardon's avatar
incardon committed
1103
		// Wait for all the requests
1104
		stat.resize(req.size());
1105
		MPI_SAFE_CALL(MPI_Waitall(req.size(),&req.get(0),&stat.get(0)));
incardon's avatar
incardon committed
1106

1107
		// Remove executed request and status
incardon's avatar
incardon committed
1108 1109 1110
		req.clear();
		stat.clear();
	}
1111 1112


1113 1114
	/////////////////////// Semantic communication ///////////////////////
	#include "VCluster_semantic.ipp"
incardon's avatar
incardon committed
1115 1116
};

Pietro Incardona's avatar
Pietro Incardona committed
1117
// Function to initialize the global VCluster //
incardon's avatar
incardon committed
1118

1119
extern Vcluster * global_v_cluster_private;
Pietro Incardona's avatar
Pietro Incardona committed
1120 1121 1122 1123 1124 1125 1126

/*! \brief Initialize a global instance of Runtime Virtual Cluster Machine
 *
 * Initialize a global instance of Runtime Virtual Cluster Machine
 *
 */

1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
static inline void init_global_v_cluster_private(int *argc, char ***argv)
{
	if (global_v_cluster_private == NULL)
		global_v_cluster_private = new Vcluster(argc,argv);
}

static inline void delete_global_v_cluster_private()
{
	delete global_v_cluster_private;
}

static inline Vcluster & create_vcluster()
{
#ifdef SE_CLASS1

	if (global_v_cluster_private == NULL)
		std::cerr << __FILE__ << ":" << __LINE__ << " Error you must call openfpm_init before using any distributed data structures";

#endif

	return *global_v_cluster_private;
}

Pietro Incardona's avatar
Pietro Incardona committed
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
/*! \brief Check if the library has been initialized
 *
 * \return true if the library has been initialized
 *
 */
static inline bool is_openfpm_init()
{
	return ofp_initialized;
}

1160 1161 1162 1163 1164 1165
/*! \brief Initialize the library
 *
 * This function MUST be called before any other function
 *
 */
static inline void openfpm_init(int *argc, char ***argv)
Pietro Incardona's avatar
Pietro Incardona committed
1166
{
Pietro Incardona's avatar
Pietro Incardona committed
1167 1168 1169 1170 1171 1172
#ifdef HAVE_PETSC

	PetscInitialize(argc,argv,NULL,NULL);

#endif

1173
	init_global_v_cluster_private(argc,argv);
Pietro Incardona's avatar
Pietro Incardona committed
1174
	ofp_initialized = true;
Pietro Incardona's avatar
Pietro Incardona committed
1175 1176
}

1177 1178 1179 1180 1181 1182
/*! \brief Finalize the library
 *
 * This function MUST be called at the end of the program
 *
 */
static inline void openfpm_finalize()
Pietro Incardona's avatar
Pietro Incardona committed
1183
{
Pietro Incardona's avatar
Pietro Incardona committed
1184 1185 1186 1187 1188 1189
#ifdef HAVE_PETSC

	PetscFinalize();

#endif

1190
	delete_global_v_cluster_private();
Pietro Incardona's avatar
Pietro Incardona committed
1191
	ofp_initialized = false;
Pietro Incardona's avatar
Pietro Incardona committed
1192 1193
}

incardon's avatar
incardon committed
1194 1195
#endif