VCluster_base.hpp 26.5 KB
Newer Older
incardon's avatar
incardon committed
1 2
#ifndef VCLUSTER_BASE_HPP_
#define VCLUSTER_BASE_HPP_
incardon's avatar
incardon committed
3

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
#include "Vector/map_vector.hpp"
8 9 10
#include "MPI_wrapper/MPI_IallreduceW.hpp"
#include "MPI_wrapper/MPI_IrecvW.hpp"
#include "MPI_wrapper/MPI_IsendW.hpp"
incardon's avatar
incardon committed
11
#include "MPI_wrapper/MPI_IAllGather.hpp"
incardon's avatar
incardon committed
12
#include <exception>
13
#include "Vector/map_vector.hpp"
14 15
#ifdef DEBUG
#include "util/check_no_pointers.hpp"
16
#include "util/util_debug.hpp"
17
#endif
incardon's avatar
incardon committed
18
#include "util/Vcluster_log.hpp"
19
#include "memory/BHeapMemory.hpp"
Yaroslav's avatar
Yaroslav committed
20
#include "Packer_Unpacker/has_max_prop.hpp"
incardon's avatar
incardon committed
21
#include "data_type/aggregate.hpp"
incardon's avatar
incardon committed
22

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

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

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

incardon's avatar
incardon committed
37 38 39
#define RECEIVE_KNOWN 4
#define KNOWN_ELEMENT_OR_BYTE 8

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

incardon's avatar
incardon committed
49 50 51 52 53 54 55 56
///////////////////// Post functions /////////////

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


incardon's avatar
incardon committed
57
//! temporal buffer for reductions
incardon's avatar
incardon committed
58 59
union red
{
incardon's avatar
incardon committed
60
	//! char
incardon's avatar
incardon committed
61
	char c;
incardon's avatar
incardon committed
62
	//! unsigned char
incardon's avatar
incardon committed
63
	unsigned char uc;
incardon's avatar
incardon committed
64
	//! signed
incardon's avatar
incardon committed
65
	short s;
incardon's avatar
incardon committed
66
	//! unsigned short
incardon's avatar
incardon committed
67
	unsigned short us;
incardon's avatar
incardon committed
68
	//! integer
incardon's avatar
incardon committed
69
	int i;
incardon's avatar
incardon committed
70
	//! unsigned integer
incardon's avatar
incardon committed
71
	unsigned int ui;
incardon's avatar
incardon committed
72
	//! float
incardon's avatar
incardon committed
73
	float f;
incardon's avatar
incardon committed
74
	//! double
incardon's avatar
incardon committed
75 76 77
	double d;
};

78
/*! \brief This class virtualize the cluster of PC as a set of processes that communicate
incardon's avatar
incardon committed
79
 *
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
 * 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
99 100 101
 *
 */

incardon's avatar
incardon committed
102
class Vcluster_base
incardon's avatar
incardon committed
103
{
incardon's avatar
incardon committed
104
	//! log file
incardon's avatar
incardon committed
105 106
	Vcluster_log log;

107 108 109
	//! 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
110
	//! an acknowledge notification on the sending processor.
111 112 113 114 115 116
	//! 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
117 118 119 120
	//! 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
121
	size_t NBX_cnt;
122

incardon's avatar
incardon committed
123 124
	//! temporal vector used for meta-communication
	//! ( or meta-data before the real communication )
incardon's avatar
incardon committed
125 126
	openfpm::vector<size_t> proc_com;

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

incardon's avatar
incardon committed
130
	//! vector of MPI requests
incardon's avatar
incardon committed
131 132
	openfpm::vector<MPI_Request> req;

incardon's avatar
incardon committed
133
	//! vector of MPI status
incardon's avatar
incardon committed
134 135
	openfpm::vector<MPI_Status> stat;

incardon's avatar
incardon committed
136
	//! vector of functions to execute after all the request has been performed
incardon's avatar
incardon committed
137 138 139 140 141 142 143
	std::vector<int> post_exe;

	// Object array


	// Single objects

incardon's avatar
incardon committed
144
	//! number of processes
incardon's avatar
incardon committed
145
	int size;
incardon's avatar
incardon committed
146
	//! actual rank
incardon's avatar
incardon committed
147 148
	int rank;

incardon's avatar
incardon committed
149
	//! number of processing unit per process
incardon's avatar
incardon committed
150 151
	int numPE = 1;

incardon's avatar
incardon committed
152
	/*! This buffer is a temporal buffer for reductions
incardon's avatar
incardon committed
153 154 155 156 157 158 159 160
	 *
	 * 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
161
	//! vector of pointers of send buffers
162 163
	openfpm::vector<void *> ptr_send;

incardon's avatar
incardon committed
164
	//! vector of the size of send buffers
165 166
	openfpm::vector<size_t> sz_send;

incardon's avatar
incardon committed
167
	//! barrier request
168
	MPI_Request bar_req;
incardon's avatar
incardon committed
169 170

	//! barrier status
171 172
	MPI_Status bar_stat;

incardon's avatar
incardon committed
173
	//! disable operator=
incardon's avatar
incardon committed
174
	Vcluster_base & operator=(const Vcluster_base &)	{return *this;};
Pietro Incardona's avatar
Pietro Incardona committed
175

incardon's avatar
incardon committed
176
	//! disable copy constructor
incardon's avatar
incardon committed
177
	Vcluster_base(const Vcluster_base &)
incardon's avatar
incardon committed
178 179
	:NBX_cnt(0)
	{};
Pietro Incardona's avatar
Pietro Incardona committed
180

incardon's avatar
incardon committed
181 182 183 184 185
protected:

	//! Receive buffers
	openfpm::vector<BHeapMemory> recv_buf;

incardon's avatar
incardon committed
186 187 188
public:

	// Finalize the MPI program
incardon's avatar
incardon committed
189
	~Vcluster_base()
incardon's avatar
incardon committed
190
	{
Pietro Incardona's avatar
Pietro Incardona committed
191 192 193
#ifdef SE_CLASS2
		check_delete(this);
#endif
incardon's avatar
incardon committed
194 195 196 197
		n_vcluster--;

		// if there are no other vcluster instances finalize
		if (n_vcluster == 0)
Pietro Incardona's avatar
Pietro Incardona committed
198
		{
199 200 201 202
			int already_finalised;

			MPI_Finalized(&already_finalised);
			if (!already_finalised)
Pietro Incardona's avatar
Pietro Incardona committed
203
			{
204 205 206 207
				if (MPI_Finalize() != 0)
				{
					std::cerr << __FILE__ << ":" << __LINE__  << " MPI_Finalize FAILED \n";
				}
Pietro Incardona's avatar
Pietro Incardona committed
208
			}
Pietro Incardona's avatar
Pietro Incardona committed
209
		}
incardon's avatar
incardon committed
210 211
	}

212 213 214 215 216 217
	/*! \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
218
	Vcluster_base(int *argc, char ***argv)
incardon's avatar
incardon committed
219
	:NBX_cnt(0)
incardon's avatar
incardon committed
220
	{
Pietro Incardona's avatar
Pietro Incardona committed
221 222 223 224
#ifdef SE_CLASS2
		check_new(this,8,VCLUSTER_EVENT,PRJ_VCLUSTER);
#endif

incardon's avatar
incardon committed
225 226
		n_vcluster++;

227 228 229
		int already_initialised;
		MPI_Initialized(&already_initialised);

incardon's avatar
incardon committed
230
		// Check if MPI is already initialized
231
		if (!already_initialised)
incardon's avatar
incardon committed
232
		{
incardon's avatar
incardon committed
233

incardon's avatar
incardon committed
234 235
			MPI_Init(argc,argv);
		}
incardon's avatar
incardon committed
236

237 238
		// Get the total number of process
		// and the rank of this process
incardon's avatar
incardon committed
239 240 241 242

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

243
#ifdef SE_CLASS2
incardon's avatar
incardon committed
244 245 246
			process_v_cl = rank;
#endif

247
		// create and fill map scatter with one
incardon's avatar
incardon committed
248 249 250 251 252 253
		map_scatter.resize(size);

		for (size_t i = 0 ; i < map_scatter.size() ; i++)
		{
			map_scatter.get(i) = 1;
		}
incardon's avatar
incardon committed
254 255 256

		// open the log file
		log.openLog(rank);
257 258 259 260

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

263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
#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
299
				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" ;
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
				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
316 317 318 319 320 321 322 323 324 325
	/*! \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
326 327 328 329 330 331 332 333 334 335 336 337
	//! Get the total number of processing units
	size_t getProcessingUnits()
	{
		return size*numPE;
	}

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

338
	/*! \brief Sum the numbers across all processors and get the result
incardon's avatar
incardon committed
339 340 341 342 343
	 *
	 * \param num to reduce, input and output
	 *
	 */

344
	template<typename T> void sum(T & num)
incardon's avatar
incardon committed
345
	{
346 347 348 349
#ifdef DEBUG
		checkType<T>();
#endif

incardon's avatar
incardon committed
350 351 352 353 354 355 356 357 358
		// reduce over MPI

		// Create one request
		req.add();

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

359
	/*! \brief Get the maximum number across all processors (or reduction with infinity norm)
incardon's avatar
incardon committed
360 361 362 363 364 365
	 *
	 * \param num to reduce
	 *
	 */
	template<typename T> void max(T & num)
	{
366 367 368
#ifdef DEBUG
		checkType<T>();
#endif
incardon's avatar
incardon committed
369 370 371 372 373 374 375 376 377
		// reduce over MPI

		// Create one request
		req.add();

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

tonynsyde's avatar
tonynsyde committed
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
	/*! \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());
	}

398
	/*! \brief Send and receive multiple messages
399
	 *
400 401
	 * 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
incardon's avatar
incardon committed
402 403 404
	 * function. In this particular case the receiver know from which processor is going
	 * to receive.
	 *
405 406
	 *
	 * suppose the following situation the calling processor want to communicate
incardon's avatar
incardon committed
407 408 409 410
	 * * 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
411
	 *
incardon's avatar
incardon committed
412
	 * \param n_send number of send for this processor [4]
413
	 *
incardon's avatar
incardon committed
414 415
	 * \param prc list of processor with which it should communicate
	 *        [1,1,6,7,8]
416
	 *
incardon's avatar
incardon committed
417 418 419 420 421 422 423
	 * \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
	 *
	 * \param msg_alloc This is a call-back with the purpose of allocate space
	 *        for the incoming message and give back a valid pointer, supposing that this call-back has been triggered by
424 425
	 *        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
incardon's avatar
incardon committed
426 427
	 *        in the call-back are in order:
	 *        * message size required to receive the message [100]
428 429
	 *        * 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)
incardon's avatar
incardon committed
430
	 *        * processor id [5]
431
	 *        * ri request id (it is an id that goes from 0 to total_p, and is incremented
432
	 *           every time message_alloc is called)
433
	 *        * void pointer, parameter for additional data to pass to the call-back
434
	 *
incardon's avatar
incardon committed
435 436
	 * \param ptr_arg data passed to the call-back function specified
	 *
incardon's avatar
incardon committed
437
	 * \param opt options, NONE (ignored in this moment)
438 439
	 *
	 */
incardon's avatar
incardon committed
440
	template<typename T> void sendrecvMultipleMessagesNBX(openfpm::vector< size_t > & prc, openfpm::vector< T > & data, openfpm::vector< size_t > prc_recv, openfpm::vector< size_t > & recv_sz ,void * (* msg_alloc)(size_t,size_t,size_t,size_t,size_t,void *), void * ptr_arg, long int opt=NONE)
441
	{
incardon's avatar
incardon committed
442
		// Allocate the buffers
443 444

		for (size_t i = 0 ; i < prc.size() ; i++)
incardon's avatar
incardon committed
445 446 447
			send(prc.get(i),SEND_SPARSE + NBX_cnt,data.get(i).getPointer(),data.get(i).size());

		for (size_t i = 0 ; i < prc_recv.size() ; i++)
448
		{
incardon's avatar
incardon committed
449 450 451
			void * ptr_recv = msg_alloc(recv_sz.get(i),0,0,prc_recv.get(i),i,ptr_arg);

			recv(prc_recv.get(i),SEND_SPARSE + NBX_cnt,ptr_recv,recv_sz.get(i));
452 453
		}

incardon's avatar
incardon committed
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
		execute();

		// Circular counter
		NBX_cnt = (NBX_cnt + 1) % 1024;
	}

	/*! \brief Send and receive multiple messages
	 *
	 * 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. In this particular case the receiver know from which processor is going
	 * to receive.
	 *
	 *
	 * 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
	 *
	 * \param n_send number of send for this processor [4]
	 *
	 * \param prc list of processor with which it should communicate
	 *        [1,1,6,7,8]
	 *
	 * \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
	 *
	 * \param msg_alloc This is a call-back with the purpose of allocate space
	 *        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 (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
	 *           every time message_alloc is called)
	 *        * void pointer, parameter for additional data to pass to the call-back
	 *
	 * \param ptr_arg data passed to the call-back function specified
	 *
	 * \param opt options, NONE (ignored in this moment)
	 *
	 */
	void sendrecvMultipleMessagesNBX(size_t n_send , size_t sz[], size_t prc[] , void * ptr[], size_t n_recv, size_t prc_recv[] , size_t sz_recv[] ,void * (* msg_alloc)(size_t,size_t,size_t,size_t,size_t,void *), void * ptr_arg, long int opt=NONE)
	{
		// Allocate the buffers

		for (size_t i = 0 ; i < n_send ; i++)
			send(prc[i],SEND_SPARSE + NBX_cnt,ptr[i],sz[i]);

		for (size_t i = 0 ; i < n_recv ; i++)
		{
			void * ptr_recv = msg_alloc(sz_recv[i],0,0,prc_recv[i],i,ptr_arg);

			recv(prc_recv[i],SEND_SPARSE + NBX_cnt,ptr_recv,sz_recv[i]);
		}

		execute();

		// Circular counter
		NBX_cnt = (NBX_cnt + 1) % 1024;
520 521
	}

incardon's avatar
incardon committed
522 523
	/*! \brief Send and receive multiple messages
	 *
incardon's avatar
incardon committed
524
	 * It send multiple messages to a set of processors the and receive
525
	 * multiple messages from another set of processors, all the processor must call this
incardon's avatar
incardon committed
526
	 * function
527 528 529 530 531 532 533 534 535
	 *
	 * 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
536
	 * \param data vector containing the data to send [v=vector<vector<int>>, v.size()=4, T=vector<int>], T at the moment
537 538 539 540 541 542 543
	 *          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:
incardon's avatar
incardon committed
544
	 *        * message size required to receive the message (100)
545 546
	 *        * 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)
incardon's avatar
incardon committed
547
	 *        * processor id (5)
548
	 *        * ri request id (it is an id that goes from 0 to total_p, and is incremented
549
	 *           every time message_alloc is called)
550
	 *        * void pointer, parameter for additional data to pass to the call-back
incardon's avatar
incardon committed
551
	 *
incardon's avatar
incardon committed
552 553
	 * \param ptr_arg data passed to the call-back function specified
	 *
554
	 * \param opt options, only NONE supported
incardon's avatar
incardon committed
555 556
	 *
	 */
incardon's avatar
incardon committed
557
	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)
incardon's avatar
incardon committed
558
	{
559 560 561
#ifdef DEBUG
		checkType<typename T::value_type>();
#endif
incardon's avatar
incardon committed
562 563 564 565 566 567 568 569 570 571
		// 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);
		}

incardon's avatar
incardon committed
572
		sendrecvMultipleMessagesNBX(prc.size(),(size_t *)sz_send.getPointer(),(size_t *)prc.getPointer(),(void **)ptr_send.getPointer(),msg_alloc,ptr_arg,opt);
573 574
	}

incardon's avatar
incardon committed
575 576


577
	/*! \brief Send and receive multiple messages
578
	 *
579 580
	 * 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
incardon's avatar
incardon committed
581
	 * function
582
	 *
583 584 585 586 587
	 * 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
588
	 *
589
	 * \param n_send number of send for this processor [4]
590 591
	 *
	 * \param prc list of processor with which it should communicate
592
	 *        [1,1,6,7,8]
593
	 *
594 595 596 597
	 * \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
598 599
	 *
	 * \param msg_alloc This is a call-back with the purpose of allocate space
600 601 602
	 *        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
603
	 *        in the call-back are in order:
604 605 606 607 608
	 *        * 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
609
	 *           every time message_alloc is called)
610
	 *        * void pointer, parameter for additional data to pass to the call-back
611
	 *
incardon's avatar
incardon committed
612 613
	 * \param ptr_arg data passed to the call-back function specified
	 *
614 615 616
	 * \param opt options, NONE (ignored in this moment)
	 *
	 */
617
	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)
618
	{
619
		if (stat.size() != 0 || req.size() != 0)
incardon's avatar
incardon committed
620
			std::cerr << "Error: " << __FILE__ << ":" << __LINE__ << " this function must be called when no other requests are in progress. Please remember that id you use function like max(),sum(),send(),recv() check that you did not miss to call the function execute() \n";
621 622 623


		stat.clear();
624 625 626 627 628
		req.clear();
		// Do MPI_Issend

		for (size_t i = 0 ; i < n_send ; i++)
		{
incardon's avatar
incardon committed
629 630 631
			if (sz[i] != 0)
			{
				req.add();
632 633 634 635 636

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

incardon's avatar
incardon committed
637
				tot_sent += sz[i];
638
				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
639
				log.logSend(prc[i]);
incardon's avatar
incardon committed
640
			}
641 642 643 644 645 646 647
		}

		size_t rid = 0;
		int flag = false;

		bool reached_bar_req = false;

incardon's avatar
incardon committed
648 649
		log.start(10);

650 651 652
		// Wait that all the send are acknowledge
		do
		{
incardon's avatar
incardon committed
653

654 655 656 657 658
			// flag that notify that this processor reach the barrier
			// Barrier request

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

661
			// If I have an incoming message and is related to this NBX communication
662 663 664 665 666 667 668 669 670
			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
671 672 673
				// Log the receiving request
				log.logRecv(stat_t);

674 675
				rid++;

676 677 678 679
				// Check the pointer
#ifdef SE_CLASS2
				check_valid(ptr,msize);
#endif
incardon's avatar
incardon committed
680
				tot_recv += msize;
681
				MPI_SAFE_CALL(MPI_Recv(ptr,msize,MPI_BYTE,stat_t.MPI_SOURCE,SEND_SPARSE+NBX_cnt,MPI_COMM_WORLD,&stat_t));
682 683 684 685

#ifdef SE_CLASS2
				check_valid(ptr,msize);
#endif
686 687 688 689 690 691 692
			}

			// 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
693 694 695 696
				if (req.size() != 0)
				{MPI_SAFE_CALL(MPI_Testall(req.size(),&req.get(0),&flag,MPI_STATUSES_IGNORE));}
				else
					flag = true;
697 698 699 700 701 702

				// 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
703
			// Check if all processor reached the async barrier
704
			if (reached_bar_req)
incardon's avatar
incardon committed
705
			{MPI_SAFE_CALL(MPI_Test(&bar_req,&flag,&bar_stat))};
incardon's avatar
incardon committed
706 707

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

710 711 712 713 714 715
		} while (flag == false);

		// Remove the executed request

		req.clear();
		stat.clear();
incardon's avatar
incardon committed
716
		log.clear();
717 718 719

		// Circular counter
		NBX_cnt = (NBX_cnt + 1) % 1024;
incardon's avatar
incardon committed
720 721
	}

722 723 724 725 726 727
	/*! \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
728 729 730 731 732 733 734 735 736 737 738 739
	 * \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
	 *
	 */
740
	bool send(size_t proc, size_t tag, const void * mem, size_t sz)
incardon's avatar
incardon committed
741 742 743 744 745 746 747 748 749 750 751 752
	{
		// 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
753

incardon's avatar
incardon committed
754 755 756 757 758 759 760
	/*! \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
761 762 763 764 765 766 767
	 *
	 * \see sendRecvMultipleMessages
	 *
	 * \param proc processor id
	 * \param tag id
	 * \param v buffer to send
	 *
768 769
	 * \return true if succeed false otherwise
	 *
770
	 */
771
	template<typename T, typename Mem, typename gr> bool send(size_t proc, size_t tag, openfpm::vector<T,Mem,gr> & v)
772 773 774 775 776 777 778 779 780 781
	{
#ifdef DEBUG
		checkType<T>();
#endif

		// send over MPI

		// Create one request
		req.add();

782
		// send
783
		MPI_IsendW<T,Mem,gr>::send(proc,SEND_RECV_BASE + tag,v,req.last());
784 785

		return true;
786 787 788 789 790 791
	}

	/*! \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
incardon's avatar
incardon committed
792
	 *          consider to use or sendrecvMultipleMessagesNBX
793
	 *
incardon's avatar
incardon committed
794
	 * \warning operation is asynchronous execute must be called to ensure they are executed
795
	 *
incardon's avatar
incardon committed
796
	 * \see sendrecvMultipleMessagesNBX
797 798 799 800
	 *
	 * \param proc processor id
	 * \param tag id
	 * \param v buffer to send
incardon's avatar
incardon committed
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
	 * \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
819 820 821 822
    /*! \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
incardon's avatar
incardon committed
823
     *          consider to use sendrecvMultipleMessagesNBX
incardon's avatar
incardon committed
824 825 826
     *
     * \warning operation is asynchronous execute must be called to ensure they are executed
     *
incardon's avatar
incardon committed
827
     * \see sendrecvMultipleMessagesNBX
incardon's avatar
incardon committed
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
     *
     * \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
854 855
	 *
	 * send a primitive data T receive the same primitive T from all the other processors
incardon's avatar
incardon committed
856 857 858
	 *
	 * \warning operation is asynchronous execute must be called to ensure they are executed
	 *
859
	 * \param v vector to receive (automaticaly resized)
incardon's avatar
incardon committed
860
	 * \param send data to send
861
	 *
862 863
	 * \return true if succeed false otherwise
	 *
864
	 */
incardon's avatar
incardon committed
865
	template<typename T, typename Mem, typename gr> bool allGather(T & send, openfpm::vector<T,Mem,gr> & v)
866 867 868 869 870 871 872 873
	{
#ifdef DEBUG
		checkType<T>();
#endif

		// Create one request
		req.add();

incardon's avatar
incardon committed
874 875 876
		// Number of processors
		v.resize(getProcessingUnits());

877
		// gather
incardon's avatar
incardon committed
878
		MPI_IAllGatherW<T>::gather(&send,1,v.getPointer(),1,req.last());
879 880

		return true;
881 882 883
	}

	/*! \brief Execute all the requests
incardon's avatar
incardon committed
884 885 886 887
	 *
	 */
	void execute()
	{
888 889 890 891
		// if req == 0 return
		if (req.size() == 0)
			return;

incardon's avatar
incardon committed
892
		// Wait for all the requests
893
		stat.resize(req.size());
894
		MPI_SAFE_CALL(MPI_Waitall(req.size(),&req.get(0),&stat.get(0)));
incardon's avatar
incardon committed
895

896
		// Remove executed request and status
incardon's avatar
incardon committed
897 898 899 900 901
		req.clear();
		stat.clear();
	}
};

incardon's avatar
incardon committed
902

Pietro Incardona's avatar
Pietro Incardona committed
903

Pietro Incardona's avatar
Pietro Incardona committed
904

incardon's avatar
incardon committed
905 906
#endif