MPI_IBcastW.hpp 5.82 KB
Newer Older
incardon's avatar
incardon committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * MPI_IBcastW.hpp
 *
 *  Created on: Apr 8, 2017
 *      Author: i-bird
 */

#ifndef OPENFPM_VCLUSTER_SRC_MPI_WRAPPER_MPI_IBCASTW_HPP_
#define OPENFPM_VCLUSTER_SRC_MPI_WRAPPER_MPI_IBCASTW_HPP_



#include <mpi.h>

15 16


incardon's avatar
incardon committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
/*! \brief Set of wrapping classing for MPI_Irecv
 *
 * The purpose of these classes is to correctly choose the right call based on the type we want to receive
 *
 */

/*! \brief General recv for general buffer
 *
 * \param proc processor from which to receive
 * \param tag
 * \param buf buffer where to store the data
 * \param sz size to receive
 * \param req MPI request
 *
 */

class MPI_IBcastWB
{
public:
	static inline void bcast(size_t proc ,void * buf, size_t sz, MPI_Request & req)
	{
		MPI_SAFE_CALL(MPI_Ibcast(buf,sz,MPI_BYTE, proc , MPI_COMM_WORLD,&req));
	}
};

/*! \brief General recv for vector of
 *
 * \tparam any type
 *
 */

template<typename T> class MPI_IBcastW
{
public:
51
	template<typename Memory> static inline void bcast(size_t proc ,openfpm::vector<T,Memory> & v, MPI_Request & req)
incardon's avatar
incardon committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 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
	{
		MPI_SAFE_CALL(MPI_Ibcast(v.getPointer(), v.size() * sizeof(T),MPI_BYTE, proc , MPI_COMM_WORLD,&req));
	}
};


/*! \brief specialization for vector of integer
 *
 */
template<> class MPI_IBcastW<int>
{
public:
	static inline void bcast(size_t proc ,openfpm::vector<int> & v, MPI_Request & req)
	{
		MPI_SAFE_CALL(MPI_Ibcast(v.getPointer(), v.size(),MPI_INT, proc , MPI_COMM_WORLD,&req));
	}
};

/*! \brief specialization for unsigned integer
 *
 */
template<> class MPI_IBcastW<unsigned int>
{
public:
	static inline void bcast(size_t proc ,openfpm::vector<unsigned int> & v, MPI_Request & req)
	{
		MPI_SAFE_CALL(MPI_Ibcast(v.getPointer(), v.size(),MPI_UNSIGNED, proc , MPI_COMM_WORLD,&req));
	}
};

/*! \brief specialization for short
 *
 */
template<> class MPI_IBcastW<short>
{
public:
	static inline void bcast(size_t proc ,openfpm::vector<short> & v, MPI_Request & req)
	{
		MPI_SAFE_CALL(MPI_Ibcast(v.getPointer(), v.size(),MPI_SHORT, proc , MPI_COMM_WORLD,&req));
	}
};

/*! \brief specialization for short
 *
 */
template<> class MPI_IBcastW<unsigned short>
{
public:
	static inline void bcast(size_t proc ,openfpm::vector<unsigned short> & v, MPI_Request & req)
	{
		MPI_SAFE_CALL(MPI_Ibcast(v.getPointer(), v.size(),MPI_UNSIGNED_SHORT, proc , MPI_COMM_WORLD,&req));
	}
};

/*! \brief specialization for char
 *
 */
template<> class MPI_IBcastW<char>
{
public:
	static inline void bcast(size_t proc ,openfpm::vector<char> & v, MPI_Request & req)
	{
		MPI_SAFE_CALL(MPI_Ibcast(v.getPointer(), v.size(),MPI_CHAR, proc , MPI_COMM_WORLD,&req));
	}
};

/*! \brief specialization for char
 *
 */
template<> class MPI_IBcastW<unsigned char>
{
public:
	static inline void bcast(size_t proc ,openfpm::vector<unsigned char> & v, MPI_Request & req)
	{
		MPI_SAFE_CALL(MPI_Ibcast(v.getPointer(), v.size(),MPI_UNSIGNED_CHAR, proc , MPI_COMM_WORLD,&req));
	}
};

/*! \brief specialization for size_t
 *
 */
template<> class MPI_IBcastW<size_t>
{
public:
	static inline void bcast(size_t proc ,openfpm::vector<size_t> & v, MPI_Request & req)
	{
		MPI_SAFE_CALL(MPI_Ibcast(v.getPointer(), v.size(),MPI_UNSIGNED_LONG, proc , MPI_COMM_WORLD,&req));
	}
};

/*! \brief specialization for size_t
 *
 */
template<> class MPI_IBcastW<long int>
{
public:
	static inline void bcast(size_t proc ,openfpm::vector<long int> & v, MPI_Request & req)
	{
		MPI_SAFE_CALL(MPI_Ibcast(v.getPointer(), v.size(),MPI_LONG, proc , MPI_COMM_WORLD,&req));
	}
};

/*! \brief specialization for float
 *
 */
template<> class MPI_IBcastW<float>
{
public:
	static inline void bcast(size_t proc ,openfpm::vector<float> & v, MPI_Request & req)
	{
		MPI_SAFE_CALL(MPI_Ibcast(v.getPointer(), v.size(),MPI_FLOAT, proc , MPI_COMM_WORLD,&req));
	}
};

/*! \brief specialization for double
 *
 */
template<> class MPI_IBcastW<double>
{
public:
	static inline void bcast(size_t proc ,openfpm::vector<double> & v, MPI_Request & req)
	{
		MPI_SAFE_CALL(MPI_Ibcast(v.getPointer(), v.size(),MPI_DOUBLE, proc , MPI_COMM_WORLD,&req));
	}
};


179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
/*! \brief this class is a functor for "for_each" algorithm
 *
 * This class is a functor for "for_each" algorithm. For each
 * element of the boost::vector the operator() is called.
 * Is mainly used to process broadcast request for each buffer
 *
 */
template<typename vect>
struct bcast_inte_impl
{
	//! vector to broadcast
	vect & send;

	//! vector of requests
	openfpm::vector<MPI_Request> & req;

	//! root processor
	size_t root;

	/*! \brief constructor
	 *
	 * \param v set of pointer buffers to set
	 *
	 */
	inline bcast_inte_impl(vect & send,
						   openfpm::vector<MPI_Request> & req,
			               size_t root)
	:send(send),req(req),root(root)
	{};

	//! It call the copy function for each property
	template<typename T>
	inline void operator()(T& t)
	{
		typedef typename boost::mpl::at<typename vect::value_type::type,T>::type send_type;

		// Create one request
		req.add();

		// gather
		MPI_IBcastWB::bcast(root,&send.template get<T::value>(0),send.size()*sizeof(send_type),req.last());
	}
};

template<bool is_lin_or_inte>
struct b_cast_helper
{
	 template<typename T, typename Mem, typename lt_type, template<typename> class layout_base >
	 static void bcast_(openfpm::vector<MPI_Request> & req,
			            openfpm::vector<T,Mem,lt_type,layout_base> & v,
			            size_t root)
	{
		// Create one request
		req.add();

		// gather
		MPI_IBcastW<T>::bcast(root,v,req.last());
	}
};

template<>
struct b_cast_helper<false>
{
	 template<typename T, typename Mem, typename lt_type, template<typename> class layout_base >
	 static void bcast_(openfpm::vector<MPI_Request> & req,
			            openfpm::vector<T,Mem,lt_type,layout_base> & v,
			            size_t root)
	{
		 bcast_inte_impl<openfpm::vector<T,Mem,lt_type,layout_base>> bc(v,req,root);

		 boost::mpl::for_each_ref<boost::mpl::range_c<int,0,T::max_prop>>(bc);
	}
};

incardon's avatar
incardon committed
253
#endif /* OPENFPM_VCLUSTER_SRC_MPI_WRAPPER_MPI_IBCASTW_HPP_ */