Chris@16: // Copyright (C) 2005, 2006 Douglas Gregor . Chris@16: Chris@16: // Use, modification and distribution is subject to the Boost Software Chris@16: // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: /** @file communicator.hpp Chris@16: * Chris@16: * This header defines the @c communicator class, which is the basis Chris@16: * of all communication within Boost.MPI, and provides point-to-point Chris@16: * communication operations. Chris@16: */ Chris@16: #ifndef BOOST_MPI_COMMUNICATOR_HPP Chris@16: #define BOOST_MPI_COMMUNICATOR_HPP Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include // for std::range_error Chris@16: Chris@16: // For (de-)serializing sends and receives Chris@16: #include Chris@16: #include Chris@16: Chris@16: // For (de-)serializing skeletons and content Chris@16: #include Chris@16: Chris@16: // For (de-)serializing arrays Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #ifdef BOOST_MSVC Chris@16: # pragma warning(push) Chris@16: # pragma warning(disable : 4800) // forcing to bool 'true' or 'false' Chris@16: #endif Chris@16: Chris@16: namespace boost { namespace mpi { Chris@16: Chris@16: /** Chris@16: * @brief A constant representing "any process." Chris@16: * Chris@16: * This constant may be used for the @c source parameter of @c receive Chris@16: * operations to indicate that a message may be received from any Chris@16: * source. Chris@16: */ Chris@16: const int any_source = MPI_ANY_SOURCE; Chris@16: Chris@16: /** Chris@16: * @brief A constant representing "any tag." Chris@16: * Chris@16: * This constant may be used for the @c tag parameter of @c receive Chris@16: * operations to indicate that a @c send with any tag will be matched Chris@16: * by the receive. Chris@16: */ Chris@16: const int any_tag = MPI_ANY_TAG; Chris@16: Chris@16: /** Chris@16: * @brief Enumeration used to describe how to adopt a C @c MPI_Comm into Chris@16: * a Boost.MPI communicator. Chris@16: * Chris@16: * The values for this enumeration determine how a Boost.MPI Chris@16: * communicator will behave when constructed with an MPI Chris@16: * communicator. The options are: Chris@16: * Chris@16: * - @c comm_duplicate: Duplicate the MPI_Comm communicator to Chris@16: * create a new communicator (e.g., with MPI_Comm_dup). This new Chris@16: * MPI_Comm communicator will be automatically freed when the Chris@16: * Boost.MPI communicator (and all copies of it) is destroyed. Chris@16: * Chris@16: * - @c comm_take_ownership: Take ownership of the communicator. It Chris@16: * will be freed automatically when all of the Boost.MPI Chris@16: * communicators go out of scope. This option must not be used with Chris@16: * MPI_COMM_WORLD. Chris@16: * Chris@16: * - @c comm_attach: The Boost.MPI communicator will reference the Chris@16: * existing MPI communicator but will not free it when the Boost.MPI Chris@16: * communicator goes out of scope. This option should only be used Chris@16: * when the communicator is managed by the user or MPI library Chris@16: * (e.g., MPI_COMM_WORLD). Chris@16: */ Chris@16: enum comm_create_kind { comm_duplicate, comm_take_ownership, comm_attach }; Chris@16: Chris@16: /** Chris@16: * INTERNAL ONLY Chris@16: * Chris@16: * Forward declaration of @c group needed for the @c group Chris@16: * constructor and accessor. Chris@16: */ Chris@16: class group; Chris@16: Chris@16: /** Chris@16: * INTERNAL ONLY Chris@16: * Chris@16: * Forward declaration of @c intercommunicator needed for the "cast" Chris@16: * from a communicator to an intercommunicator. Chris@16: */ Chris@16: class intercommunicator; Chris@16: Chris@16: /** Chris@16: * INTERNAL ONLY Chris@16: * Chris@16: * Forward declaration of @c graph_communicator needed for the "cast" Chris@16: * from a communicator to a graph communicator. Chris@16: */ Chris@16: class graph_communicator; Chris@16: Chris@16: /** Chris@16: * @brief A communicator that permits communication and Chris@16: * synchronization among a set of processes. Chris@16: * Chris@16: * The @c communicator class abstracts a set of communicating Chris@16: * processes in MPI. All of the processes that belong to a certain Chris@16: * communicator can determine the size of the communicator, their rank Chris@16: * within the communicator, and communicate with any other processes Chris@16: * in the communicator. Chris@16: */ Chris@16: class BOOST_MPI_DECL communicator Chris@16: { Chris@16: public: Chris@16: /** Chris@16: * Build a new Boost.MPI communicator for @c MPI_COMM_WORLD. Chris@16: * Chris@16: * Constructs a Boost.MPI communicator that attaches to @c Chris@16: * MPI_COMM_WORLD. This is the equivalent of constructing with Chris@16: * @c (MPI_COMM_WORLD, comm_attach). Chris@16: */ Chris@16: communicator(); Chris@16: Chris@16: /** Chris@16: * Build a new Boost.MPI communicator based on the MPI communicator Chris@16: * @p comm. Chris@16: * Chris@16: * @p comm may be any valid MPI communicator. If @p comm is Chris@16: * MPI_COMM_NULL, an empty communicator (that cannot be used for Chris@16: * communication) is created and the @p kind parameter is Chris@16: * ignored. Otherwise, the @p kind parameters determines how the Chris@16: * Boost.MPI communicator will be related to @p comm: Chris@16: * Chris@16: * - If @p kind is @c comm_duplicate, duplicate @c comm to create Chris@16: * a new communicator. This new communicator will be freed when Chris@16: * the Boost.MPI communicator (and all copies of it) is destroyed. Chris@16: * This option is only permitted if @p comm is a valid MPI Chris@16: * intracommunicator or if the underlying MPI implementation Chris@16: * supports MPI 2.0 (which supports duplication of Chris@16: * intercommunicators). Chris@16: * Chris@16: * - If @p kind is @c comm_take_ownership, take ownership of @c Chris@16: * comm. It will be freed automatically when all of the Boost.MPI Chris@16: * communicators go out of scope. This option must not be used Chris@16: * when @c comm is MPI_COMM_WORLD. Chris@16: * Chris@16: * - If @p kind is @c comm_attach, this Boost.MPI communicator Chris@16: * will reference the existing MPI communicator @p comm but will Chris@16: * not free @p comm when the Boost.MPI communicator goes out of Chris@16: * scope. This option should only be used when the communicator is Chris@16: * managed by the user or MPI library (e.g., MPI_COMM_WORLD). Chris@16: */ Chris@16: communicator(const MPI_Comm& comm, comm_create_kind kind); Chris@16: Chris@16: /** Chris@16: * Build a new Boost.MPI communicator based on a subgroup of another Chris@16: * MPI communicator. Chris@16: * Chris@16: * This routine will construct a new communicator containing all of Chris@16: * the processes from communicator @c comm that are listed within Chris@16: * the group @c subgroup. Equivalent to @c MPI_Comm_create. Chris@16: * Chris@16: * @param comm An MPI communicator. Chris@16: * Chris@16: * @param subgroup A subgroup of the MPI communicator, @p comm, for Chris@16: * which we will construct a new communicator. Chris@16: */ Chris@16: communicator(const communicator& comm, const boost::mpi::group& subgroup); Chris@16: Chris@16: /** Chris@16: * @brief Determine the rank of the executing process in a Chris@16: * communicator. Chris@16: * Chris@16: * This routine is equivalent to @c MPI_Comm_rank. Chris@16: * Chris@16: * @returns The rank of the process in the communicator, which Chris@16: * will be a value in [0, size()) Chris@16: */ Chris@16: int rank() const; Chris@16: Chris@16: /** Chris@16: * @brief Determine the number of processes in a communicator. Chris@16: * Chris@16: * This routine is equivalent to @c MPI_Comm_size. Chris@16: * Chris@16: * @returns The number of processes in the communicator. Chris@16: */ Chris@16: int size() const; Chris@16: Chris@16: /** Chris@16: * This routine constructs a new group whose members are the Chris@16: * processes within this communicator. Equivalent to Chris@16: * calling @c MPI_Comm_group. Chris@16: */ Chris@16: boost::mpi::group group() const; Chris@16: Chris@16: // ---------------------------------------------------------------- Chris@16: // Point-to-point communication Chris@16: // ---------------------------------------------------------------- Chris@16: Chris@16: /** Chris@16: * @brief Send data to another process. Chris@16: * Chris@16: * This routine executes a potentially blocking send with tag @p tag Chris@16: * to the process with rank @p dest. It can be received by the Chris@16: * destination process with a matching @c recv call. Chris@16: * Chris@16: * The given @p value must be suitable for transmission over Chris@16: * MPI. There are several classes of types that meet these Chris@16: * requirements: Chris@16: * Chris@16: * - Types with mappings to MPI data types: If @c Chris@16: * is_mpi_datatype is convertible to @c mpl::true_, then @p Chris@16: * value will be transmitted using the MPI data type Chris@16: * @c get_mpi_datatype(). All primitive C++ data types that have Chris@16: * MPI equivalents, e.g., @c int, @c float, @c char, @c double, Chris@16: * etc., have built-in mappings to MPI data types. You may turn a Chris@16: * Serializable type with fixed structure into an MPI data type by Chris@16: * specializing @c is_mpi_datatype for your type. Chris@16: * Chris@16: * - Serializable types: Any type that provides the @c serialize() Chris@16: * functionality required by the Boost.Serialization library can be Chris@16: * transmitted and received. Chris@16: * Chris@16: * - Packed archives and skeletons: Data that has been packed into Chris@16: * an @c mpi::packed_oarchive or the skeletons of data that have Chris@16: * been backed into an @c mpi::packed_skeleton_oarchive can be Chris@16: * transmitted, but will be received as @c mpi::packed_iarchive and Chris@16: * @c mpi::packed_skeleton_iarchive, respectively, to allow the Chris@16: * values (or skeletons) to be extracted by the destination process. Chris@16: * Chris@16: * - Content: Content associated with a previously-transmitted Chris@16: * skeleton can be transmitted by @c send and received by @c Chris@16: * recv. The receiving process may only receive content into the Chris@16: * content of a value that has been constructed with the matching Chris@16: * skeleton. Chris@16: * Chris@16: * For types that have mappings to an MPI data type (including the Chris@16: * concent of a type), an invocation of this routine will result in Chris@16: * a single MPI_Send call. For variable-length data, e.g., Chris@16: * serialized types and packed archives, two messages will be sent Chris@16: * via MPI_Send: one containing the length of the data and the Chris@16: * second containing the data itself. Note that the transmission Chris@16: * mode for variable-length data is an implementation detail that Chris@16: * is subject to change. Chris@16: * Chris@16: * @param dest The rank of the remote process to which the data Chris@16: * will be sent. Chris@16: * Chris@16: * @param tag The tag that will be associated with this message. Tags Chris@16: * may be any integer between zero and an implementation-defined Chris@16: * upper limit. This limit is accessible via @c environment::max_tag(). Chris@16: * Chris@16: * @param value The value that will be transmitted to the Chris@16: * receiver. The type @c T of this value must meet the aforementioned Chris@16: * criteria for transmission. Chris@16: */ Chris@16: template Chris@16: void send(int dest, int tag, const T& value) const; Chris@16: Chris@16: /** Chris@16: * @brief Send the skeleton of an object. Chris@16: * Chris@16: * This routine executes a potentially blocking send with tag @p Chris@16: * tag to the process with rank @p dest. It can be received by the Chris@16: * destination process with a matching @c recv call. This variation Chris@16: * on @c send will be used when a send of a skeleton is explicitly Chris@16: * requested via code such as: Chris@16: * Chris@16: * @code Chris@16: * comm.send(dest, tag, skeleton(object)); Chris@16: * @endcode Chris@16: * Chris@16: * The semantics of this routine are equivalent to that of sending Chris@16: * a @c packed_skeleton_oarchive storing the skeleton of the @c Chris@16: * object. Chris@16: * Chris@16: * @param dest The rank of the remote process to which the skeleton Chris@16: * will be sent. Chris@16: * Chris@16: * @param tag The tag that will be associated with this message. Tags Chris@16: * may be any integer between zero and an implementation-defined Chris@16: * upper limit. This limit is accessible via @c environment::max_tag(). Chris@16: * Chris@16: * @param proxy The @c skeleton_proxy containing a reference to the Chris@16: * object whose skeleton will be transmitted. Chris@16: * Chris@16: */ Chris@16: template Chris@16: void send(int dest, int tag, const skeleton_proxy& proxy) const; Chris@16: Chris@16: /** Chris@16: * @brief Send an array of values to another process. Chris@16: * Chris@16: * This routine executes a potentially blocking send of an array of Chris@16: * data with tag @p tag to the process with rank @p dest. It can be Chris@16: * received by the destination process with a matching array @c Chris@16: * recv call. Chris@16: * Chris@16: * If @c T is an MPI datatype, an invocation of this routine will Chris@16: * be mapped to a single call to MPI_Send, using the datatype @c Chris@16: * get_mpi_datatype(). Chris@16: * Chris@16: * @param dest The process rank of the remote process to which Chris@16: * the data will be sent. Chris@16: * Chris@16: * @param tag The tag that will be associated with this message. Tags Chris@16: * may be any integer between zero and an implementation-defined Chris@16: * upper limit. This limit is accessible via @c environment::max_tag(). Chris@16: * Chris@16: * @param values The array of values that will be transmitted to the Chris@16: * receiver. The type @c T of these values must be mapped to an MPI Chris@16: * data type. Chris@16: * Chris@16: * @param n The number of values stored in the array. The destination Chris@16: * process must call receive with at least this many elements to Chris@16: * correctly receive the message. Chris@16: */ Chris@16: template Chris@16: void send(int dest, int tag, const T* values, int n) const; Chris@16: Chris@16: /** Chris@16: * @brief Send a message to another process without any data. Chris@16: * Chris@16: * This routine executes a potentially blocking send of a message Chris@16: * to another process. The message contains no extra data, and can Chris@16: * therefore only be received by a matching call to @c recv(). Chris@16: * Chris@16: * @param dest The process rank of the remote process to which Chris@16: * the message will be sent. Chris@16: * Chris@16: * @param tag The tag that will be associated with this message. Tags Chris@16: * may be any integer between zero and an implementation-defined Chris@16: * upper limit. This limit is accessible via @c environment::max_tag(). Chris@16: * Chris@16: */ Chris@16: void send(int dest, int tag) const; Chris@16: Chris@16: /** Chris@16: * @brief Receive data from a remote process. Chris@16: * Chris@16: * This routine blocks until it receives a message from the process @p Chris@16: * source with the given @p tag. The type @c T of the @p value must be Chris@16: * suitable for transmission over MPI, which includes serializable Chris@16: * types, types that can be mapped to MPI data types (including most Chris@16: * built-in C++ types), packed MPI archives, skeletons, and content Chris@16: * associated with skeletons; see the documentation of @c send for a Chris@16: * complete description. Chris@16: * Chris@16: * @param source The process that will be sending data. This will Chris@16: * either be a process rank within the communicator or the Chris@16: * constant @c any_source, indicating that we can receive the Chris@16: * message from any process. Chris@16: * Chris@16: * @param tag The tag that matches a particular kind of message sent Chris@16: * by the source process. This may be any tag value permitted by @c Chris@16: * send. Alternatively, the argument may be the constant @c any_tag, Chris@16: * indicating that this receive matches a message with any tag. Chris@16: * Chris@16: * @param value Will contain the value of the message after a Chris@16: * successful receive. The type of this value must match the value Chris@16: * transmitted by the sender, unless the sender transmitted a packed Chris@16: * archive or skeleton: in these cases, the sender transmits a @c Chris@16: * packed_oarchive or @c packed_skeleton_oarchive and the Chris@16: * destination receives a @c packed_iarchive or @c Chris@16: * packed_skeleton_iarchive, respectively. Chris@16: * Chris@16: * @returns Information about the received message. Chris@16: */ Chris@16: template Chris@16: status recv(int source, int tag, T& value) const; Chris@16: Chris@16: /** Chris@16: * @brief Receive a skeleton from a remote process. Chris@16: * Chris@16: * This routine blocks until it receives a message from the process @p Chris@16: * source with the given @p tag containing a skeleton. Chris@16: * Chris@16: * @param source The process that will be sending data. This will Chris@16: * either be a process rank within the communicator or the constant Chris@16: * @c any_source, indicating that we can receive the message from Chris@16: * any process. Chris@16: * Chris@16: * @param tag The tag that matches a particular kind of message Chris@16: * sent by the source process. This may be any tag value permitted Chris@16: * by @c send. Alternatively, the argument may be the constant @c Chris@16: * any_tag, indicating that this receive matches a message with any Chris@16: * tag. Chris@16: * Chris@16: * @param proxy The @c skeleton_proxy containing a reference to the Chris@16: * object that will be reshaped to match the received skeleton. Chris@16: * Chris@16: * @returns Information about the received message. Chris@16: */ Chris@16: template Chris@16: status recv(int source, int tag, const skeleton_proxy& proxy) const; Chris@16: Chris@16: /** Chris@16: * @brief Receive a skeleton from a remote process. Chris@16: * Chris@16: * This routine blocks until it receives a message from the process @p Chris@16: * source with the given @p tag containing a skeleton. Chris@16: * Chris@16: * @param source The process that will be sending data. This will Chris@16: * either be a process rank within the communicator or the constant Chris@16: * @c any_source, indicating that we can receive the message from Chris@16: * any process. Chris@16: * Chris@16: * @param tag The tag that matches a particular kind of message Chris@16: * sent by the source process. This may be any tag value permitted Chris@16: * by @c send. Alternatively, the argument may be the constant @c Chris@16: * any_tag, indicating that this receive matches a message with any Chris@16: * tag. Chris@16: * Chris@16: * @param proxy The @c skeleton_proxy containing a reference to the Chris@16: * object that will be reshaped to match the received skeleton. Chris@16: * Chris@16: * @returns Information about the received message. Chris@16: */ Chris@16: template Chris@16: status recv(int source, int tag, skeleton_proxy& proxy) const; Chris@16: Chris@16: /** Chris@16: * @brief Receive an array of values from a remote process. Chris@16: * Chris@16: * This routine blocks until it receives an array of values from the Chris@16: * process @p source with the given @p tag. If the type @c T is Chris@16: * Chris@16: * @param source The process that will be sending data. This will Chris@16: * either be a process rank within the communicator or the Chris@16: * constant @c any_source, indicating that we can receive the Chris@16: * message from any process. Chris@16: * Chris@16: * @param tag The tag that matches a particular kind of message sent Chris@16: * by the source process. This may be any tag value permitted by @c Chris@16: * send. Alternatively, the argument may be the constant @c any_tag, Chris@16: * indicating that this receive matches a message with any tag. Chris@16: * Chris@16: * @param values Will contain the values in the message after a Chris@16: * successful receive. The type of these elements must match the Chris@16: * type of the elements transmitted by the sender. Chris@16: * Chris@16: * @param n The number of values that can be stored into the @p Chris@16: * values array. This shall not be smaller than the number of Chris@16: * elements transmitted by the sender. Chris@16: * Chris@16: * @throws std::range_error if the message to be received contains Chris@16: * more than @p n values. Chris@16: * Chris@16: * @returns Information about the received message. Chris@16: */ Chris@16: template Chris@16: status recv(int source, int tag, T* values, int n) const; Chris@16: Chris@16: /** Chris@16: * @brief Receive a message from a remote process without any data. Chris@16: * Chris@16: * This routine blocks until it receives a message from the process Chris@16: * @p source with the given @p tag. Chris@16: * Chris@16: * @param source The process that will be sending the message. This Chris@16: * will either be a process rank within the communicator or the Chris@16: * constant @c any_source, indicating that we can receive the Chris@16: * message from any process. Chris@16: * Chris@16: * @param tag The tag that matches a particular kind of message Chris@16: * sent by the source process. This may be any tag value permitted Chris@16: * by @c send. Alternatively, the argument may be the constant @c Chris@16: * any_tag, indicating that this receive matches a message with any Chris@16: * tag. Chris@16: * Chris@16: * @returns Information about the received message. Chris@16: */ Chris@16: status recv(int source, int tag) const; Chris@16: Chris@16: /** Chris@16: * @brief Send a message to a remote process without blocking. Chris@16: * Chris@16: * The @c isend method is functionality identical to the @c send Chris@16: * method and transmits data in the same way, except that @c isend Chris@16: * will not block while waiting for the data to be Chris@16: * transmitted. Instead, a request object will be immediately Chris@16: * returned, allowing one to query the status of the communication Chris@16: * or wait until it has completed. Chris@16: * Chris@16: * @param dest The rank of the remote process to which the data Chris@16: * will be sent. Chris@16: * Chris@16: * @param tag The tag that will be associated with this message. Tags Chris@16: * may be any integer between zero and an implementation-defined Chris@16: * upper limit. This limit is accessible via @c environment::max_tag(). Chris@16: * Chris@16: * @param value The value that will be transmitted to the Chris@16: * receiver. The type @c T of this value must meet the aforementioned Chris@16: * criteria for transmission. Chris@16: * Chris@16: * @returns a @c request object that describes this communication. Chris@16: */ Chris@16: template Chris@16: request isend(int dest, int tag, const T& value) const; Chris@16: Chris@16: /** Chris@16: * @brief Send the skeleton of an object without blocking. Chris@16: * Chris@16: * This routine is functionally identical to the @c send method for Chris@16: * @c skeleton_proxy objects except that @c isend will not block Chris@16: * while waiting for the data to be transmitted. Instead, a request Chris@16: * object will be immediately returned, allowing one to query the Chris@16: * status of the communication or wait until it has completed. Chris@16: * Chris@16: * The semantics of this routine are equivalent to a non-blocking Chris@16: * send of a @c packed_skeleton_oarchive storing the skeleton of Chris@16: * the @c object. Chris@16: * Chris@16: * @param dest The rank of the remote process to which the skeleton Chris@16: * will be sent. Chris@16: * Chris@16: * @param tag The tag that will be associated with this message. Tags Chris@16: * may be any integer between zero and an implementation-defined Chris@16: * upper limit. This limit is accessible via @c environment::max_tag(). Chris@16: * Chris@16: * @param proxy The @c skeleton_proxy containing a reference to the Chris@16: * object whose skeleton will be transmitted. Chris@16: * Chris@16: * @returns a @c request object that describes this communication. Chris@16: */ Chris@16: template Chris@16: request isend(int dest, int tag, const skeleton_proxy& proxy) const; Chris@16: Chris@16: /** Chris@16: * @brief Send an array of values to another process without Chris@16: * blocking. Chris@16: * Chris@16: * This routine is functionally identical to the @c send method for Chris@16: * arrays except that @c isend will not block while waiting for the Chris@16: * data to be transmitted. Instead, a request object will be Chris@16: * immediately returned, allowing one to query the status of the Chris@16: * communication or wait until it has completed. Chris@16: * Chris@16: * @param dest The process rank of the remote process to which Chris@16: * the data will be sent. Chris@16: * Chris@16: * @param tag The tag that will be associated with this message. Tags Chris@16: * may be any integer between zero and an implementation-defined Chris@16: * upper limit. This limit is accessible via @c environment::max_tag(). Chris@16: * Chris@16: * @param values The array of values that will be transmitted to the Chris@16: * receiver. The type @c T of these values must be mapped to an MPI Chris@16: * data type. Chris@16: * Chris@16: * @param n The number of values stored in the array. The destination Chris@16: * process must call receive with at least this many elements to Chris@16: * correctly receive the message. Chris@16: * Chris@16: * @returns a @c request object that describes this communication. Chris@16: */ Chris@16: template Chris@16: request isend(int dest, int tag, const T* values, int n) const; Chris@16: Chris@16: /** Chris@16: * @brief Send a message to another process without any data Chris@16: * without blocking. Chris@16: * Chris@16: * This routine is functionally identical to the @c send method for Chris@16: * sends with no data, except that @c isend will not block while Chris@16: * waiting for the message to be transmitted. Instead, a request Chris@16: * object will be immediately returned, allowing one to query the Chris@16: * status of the communication or wait until it has completed. Chris@16: * Chris@16: * @param dest The process rank of the remote process to which Chris@16: * the message will be sent. Chris@16: * Chris@16: * @param tag The tag that will be associated with this message. Tags Chris@16: * may be any integer between zero and an implementation-defined Chris@16: * upper limit. This limit is accessible via @c environment::max_tag(). Chris@16: * Chris@16: * Chris@16: * @returns a @c request object that describes this communication. Chris@16: */ Chris@16: request isend(int dest, int tag) const; Chris@16: Chris@16: /** Chris@16: * @brief Prepare to receive a message from a remote process. Chris@16: * Chris@16: * The @c irecv method is functionally identical to the @c recv Chris@16: * method and receive data in the same way, except that @c irecv Chris@16: * will not block while waiting for data to be Chris@16: * transmitted. Instead, it immediately returns a request object Chris@16: * that allows one to query the status of the receive or wait until Chris@16: * it has completed. Chris@16: * Chris@16: * @param source The process that will be sending data. This will Chris@16: * either be a process rank within the communicator or the Chris@16: * constant @c any_source, indicating that we can receive the Chris@16: * message from any process. Chris@16: * Chris@16: * @param tag The tag that matches a particular kind of message sent Chris@16: * by the source process. This may be any tag value permitted by @c Chris@16: * send. Alternatively, the argument may be the constant @c any_tag, Chris@16: * indicating that this receive matches a message with any tag. Chris@16: * Chris@16: * @param value Will contain the value of the message after a Chris@16: * successful receive. The type of this value must match the value Chris@16: * transmitted by the sender, unless the sender transmitted a packed Chris@16: * archive or skeleton: in these cases, the sender transmits a @c Chris@16: * packed_oarchive or @c packed_skeleton_oarchive and the Chris@16: * destination receives a @c packed_iarchive or @c Chris@16: * packed_skeleton_iarchive, respectively. Chris@16: * Chris@16: * @returns a @c request object that describes this communication. Chris@16: */ Chris@16: template Chris@16: request irecv(int source, int tag, T& value) const; Chris@16: Chris@16: /** Chris@16: * @brief Initiate receipt of an array of values from a remote process. Chris@16: * Chris@16: * This routine initiates a receive operation for an array of values Chris@16: * transmitted by process @p source with the given @p tag. Chris@16: * Chris@16: * @param source The process that will be sending data. This will Chris@16: * either be a process rank within the communicator or the Chris@16: * constant @c any_source, indicating that we can receive the Chris@16: * message from any process. Chris@16: * Chris@16: * @param tag The tag that matches a particular kind of message sent Chris@16: * by the source process. This may be any tag value permitted by @c Chris@16: * send. Alternatively, the argument may be the constant @c any_tag, Chris@16: * indicating that this receive matches a message with any tag. Chris@16: * Chris@16: * @param values Will contain the values in the message after a Chris@16: * successful receive. The type of these elements must match the Chris@16: * type of the elements transmitted by the sender. Chris@16: * Chris@16: * @param n The number of values that can be stored into the @p Chris@16: * values array. This shall not be smaller than the number of Chris@16: * elements transmitted by the sender. Chris@16: * Chris@16: * @returns a @c request object that describes this communication. Chris@16: */ Chris@16: template Chris@16: request irecv(int source, int tag, T* values, int n) const; Chris@16: Chris@16: /** Chris@16: * @brief Initiate receipt of a message from a remote process that Chris@16: * carries no data. Chris@16: * Chris@16: * This routine initiates a receive operation for a message from Chris@16: * process @p source with the given @p tag that carries no data. Chris@16: * Chris@16: * @param source The process that will be sending the message. This Chris@16: * will either be a process rank within the communicator or the Chris@16: * constant @c any_source, indicating that we can receive the Chris@16: * message from any process. Chris@16: * Chris@16: * @param tag The tag that matches a particular kind of message Chris@16: * sent by the source process. This may be any tag value permitted Chris@16: * by @c send. Alternatively, the argument may be the constant @c Chris@16: * any_tag, indicating that this receive matches a message with any Chris@16: * tag. Chris@16: * Chris@16: * @returns a @c request object that describes this communication. Chris@16: */ Chris@16: request irecv(int source, int tag) const; Chris@16: Chris@16: /** Chris@16: * @brief Waits until a message is available to be received. Chris@16: * Chris@16: * This operation waits until a message matching (@p source, @p tag) Chris@16: * is available to be received. It then returns information about Chris@16: * that message. The functionality is equivalent to @c MPI_Probe. To Chris@16: * check if a message is available without blocking, use @c iprobe. Chris@16: * Chris@16: * @param source Determine if there is a message available from Chris@16: * this rank. If @c any_source, then the message returned may come Chris@16: * from any source. Chris@16: * Chris@16: * @param tag Determine if there is a message available with the Chris@16: * given tag. If @c any_tag, then the message returned may have any Chris@16: * tag. Chris@16: * Chris@16: * @returns Returns information about the first message that Chris@16: * matches the given criteria. Chris@16: */ Chris@16: status probe(int source = any_source, int tag = any_tag) const; Chris@16: Chris@16: /** Chris@16: * @brief Determine if a message is available to be received. Chris@16: * Chris@16: * This operation determines if a message matching (@p source, @p Chris@16: * tag) is available to be received. If so, it returns information Chris@16: * about that message; otherwise, it returns immediately with an Chris@16: * empty optional. The functionality is equivalent to @c Chris@16: * MPI_Iprobe. To wait until a message is available, use @c wait. Chris@16: * Chris@16: * @param source Determine if there is a message available from Chris@16: * this rank. If @c any_source, then the message returned may come Chris@16: * from any source. Chris@16: * Chris@16: * @param tag Determine if there is a message available with the Chris@16: * given tag. If @c any_tag, then the message returned may have any Chris@16: * tag. Chris@16: * Chris@16: * @returns If a matching message is available, returns Chris@16: * information about that message. Otherwise, returns an empty Chris@16: * @c boost::optional. Chris@16: */ Chris@16: optional Chris@16: iprobe(int source = any_source, int tag = any_tag) const; Chris@16: Chris@16: #ifdef barrier Chris@16: // Linux defines a function-like macro named "barrier". So, we need Chris@16: // to avoid expanding the macro when we define our barrier() Chris@16: // function. However, some C++ parsers (Doxygen, for instance) can't Chris@16: // handle this syntax, so we only use it when necessary. Chris@16: void (barrier)() const; Chris@16: #else Chris@16: /** Chris@16: * @brief Wait for all processes within a communicator to reach the Chris@16: * barrier. Chris@16: * Chris@16: * This routine is a collective operation that blocks each process Chris@16: * until all processes have entered it, then releases all of the Chris@16: * processes "simultaneously". It is equivalent to @c MPI_Barrier. Chris@16: */ Chris@16: void barrier() const; Chris@16: #endif Chris@16: Chris@16: /** @brief Determine if this communicator is valid for Chris@16: * communication. Chris@16: * Chris@16: * Evaluates @c true in a boolean context if this communicator is Chris@16: * valid for communication, i.e., does not represent Chris@16: * MPI_COMM_NULL. Otherwise, evaluates @c false. Chris@16: */ Chris@16: operator bool() const { return (bool)comm_ptr; } Chris@16: Chris@16: /** Chris@16: * @brief Access the MPI communicator associated with a Boost.MPI Chris@16: * communicator. Chris@16: * Chris@16: * This routine permits the implicit conversion from a Boost.MPI Chris@16: * communicator to an MPI communicator. Chris@16: * Chris@16: * @returns The associated MPI communicator. Chris@16: */ Chris@16: operator MPI_Comm() const; Chris@16: Chris@16: /** Chris@16: * Split the communicator into multiple, disjoint communicators Chris@16: * each of which is based on a particular color. This is a Chris@16: * collective operation that returns a new communicator that is a Chris@16: * subgroup of @p this. This routine is functionally equivalent to Chris@16: * @c MPI_Comm_split. Chris@16: * Chris@16: * @param color The color of this process. All processes with the Chris@16: * same @p color value will be placed into the same group. Chris@16: * Chris@16: * @returns A new communicator containing all of the processes in Chris@16: * @p this that have the same @p color. Chris@16: */ Chris@16: communicator split(int color) const; Chris@16: Chris@16: /** Chris@16: * Split the communicator into multiple, disjoint communicators Chris@16: * each of which is based on a particular color. This is a Chris@16: * collective operation that returns a new communicator that is a Chris@16: * subgroup of @p this. This routine is functionally equivalent to Chris@16: * @c MPI_Comm_split. Chris@16: * Chris@16: * @param color The color of this process. All processes with the Chris@16: * same @p color value will be placed into the same group. Chris@16: * Chris@16: * @param key A key value that will be used to determine the Chris@16: * ordering of processes with the same color in the resulting Chris@16: * communicator. If omitted, the rank of the processes in @p this Chris@16: * will determine the ordering of processes in the resulting Chris@16: * group. Chris@16: * Chris@16: * @returns A new communicator containing all of the processes in Chris@16: * @p this that have the same @p color. Chris@16: */ Chris@16: communicator split(int color, int key) const; Chris@16: Chris@16: /** Chris@16: * Determine if the communicator is in fact an intercommunicator Chris@16: * and, if so, return that intercommunicator. Chris@16: * Chris@16: * @returns an @c optional containing the intercommunicator, if this Chris@16: * communicator is in fact an intercommunicator. Otherwise, returns Chris@16: * an empty @c optional. Chris@16: */ Chris@16: optional as_intercommunicator() const; Chris@16: Chris@16: /** Chris@16: * Determine if the communicator has a graph topology and, if so, Chris@16: * return that @c graph_communicator. Even though the communicators Chris@16: * have different types, they refer to the same underlying Chris@16: * communication space and can be used interchangeably for Chris@16: * communication. Chris@16: * Chris@16: * @returns an @c optional containing the graph communicator, if this Chris@16: * communicator does in fact have a graph topology. Otherwise, returns Chris@16: * an empty @c optional. Chris@16: */ Chris@16: optional as_graph_communicator() const; Chris@16: Chris@16: /** Chris@16: * Determines whether this communicator has a Cartesian topology. Chris@16: */ Chris@16: bool has_cartesian_topology() const; Chris@16: Chris@16: #if 0 Chris@16: template Chris@16: communicator Chris@16: with_cartesian_topology(const Extents& extents, Chris@16: bool periodic = false, Chris@16: bool reorder = false) const; Chris@16: Chris@16: template Chris@16: communicator Chris@16: with_cartesian_topology(DimInputIterator first_dim, Chris@16: DimInputIterator last_dim, Chris@16: PeriodicInputIterator first_periodic, Chris@16: bool reorder = false); Chris@16: Chris@16: template Chris@16: communicator Chris@16: with_cartesian_topology(const multi_array& periods, Chris@16: bool reorder = false); Chris@16: #endif Chris@16: Chris@16: /** Abort all tasks in the group of this communicator. Chris@16: * Chris@16: * Makes a "best attempt" to abort all of the tasks in the group of Chris@16: * this communicator. Depending on the underlying MPI Chris@16: * implementation, this may either abort the entire program (and Chris@16: * possibly return @p errcode to the environment) or only abort Chris@16: * some processes, allowing the others to continue. Consult the Chris@16: * documentation for your MPI implementation. This is equivalent to Chris@16: * a call to @c MPI_Abort Chris@16: * Chris@16: * @param errcode The error code to return from aborted processes. Chris@16: * @returns Will not return. Chris@16: */ Chris@16: void abort(int errcode) const; Chris@16: Chris@16: protected: Chris@16: /** Chris@16: * INTERNAL ONLY Chris@16: * Chris@16: * Function object that frees an MPI communicator and deletes the Chris@16: * memory associated with it. Intended to be used as a deleter with Chris@16: * shared_ptr. Chris@16: */ Chris@16: struct comm_free Chris@16: { Chris@16: void operator()(MPI_Comm* comm) const Chris@16: { Chris@16: BOOST_ASSERT( comm != 0 ); Chris@16: BOOST_ASSERT(*comm != MPI_COMM_NULL); Chris@16: int finalized; Chris@16: BOOST_MPI_CHECK_RESULT(MPI_Finalized, (&finalized)); Chris@16: if (!finalized) Chris@16: BOOST_MPI_CHECK_RESULT(MPI_Comm_free, (comm)); Chris@16: delete comm; Chris@16: } Chris@16: }; Chris@16: Chris@16: Chris@16: /** Chris@16: * INTERNAL ONLY Chris@16: * Chris@16: * We're sending a type that has an associated MPI datatype, so we Chris@16: * map directly to that datatype. Chris@16: */ Chris@16: template Chris@16: void send_impl(int dest, int tag, const T& value, mpl::true_) const; Chris@16: Chris@16: /** Chris@16: * INTERNAL ONLY Chris@16: * Chris@16: * We're sending a type that does not have an associated MPI Chris@16: * datatype, so it must be serialized then sent as MPI_PACKED data, Chris@16: * to be deserialized on the receiver side. Chris@16: */ Chris@16: template Chris@16: void send_impl(int dest, int tag, const T& value, mpl::false_) const; Chris@16: Chris@16: /** Chris@16: * INTERNAL ONLY Chris@16: * Chris@16: * We're sending an array of a type that has an associated MPI Chris@16: * datatype, so we map directly to that datatype. Chris@16: */ Chris@16: template Chris@16: void Chris@16: array_send_impl(int dest, int tag, const T* values, int n, mpl::true_) const; Chris@16: Chris@16: /** Chris@16: * INTERNAL ONLY Chris@16: * Chris@16: * We're sending an array of a type that does not have an associated Chris@16: * MPI datatype, so it must be serialized then sent as MPI_PACKED Chris@16: * data, to be deserialized on the receiver side. Chris@16: */ Chris@16: template Chris@16: void Chris@16: array_send_impl(int dest, int tag, const T* values, int n, Chris@16: mpl::false_) const; Chris@16: Chris@16: /** Chris@16: * INTERNAL ONLY Chris@16: * Chris@16: * We're sending a type that has an associated MPI datatype, so we Chris@16: * map directly to that datatype. Chris@16: */ Chris@16: template Chris@16: request isend_impl(int dest, int tag, const T& value, mpl::true_) const; Chris@16: Chris@16: /** Chris@16: * INTERNAL ONLY Chris@16: * Chris@16: * We're sending a type that does not have an associated MPI Chris@16: * datatype, so it must be serialized then sent as MPI_PACKED data, Chris@16: * to be deserialized on the receiver side. Chris@16: */ Chris@16: template Chris@16: request isend_impl(int dest, int tag, const T& value, mpl::false_) const; Chris@16: Chris@16: /** Chris@16: * INTERNAL ONLY Chris@16: * Chris@16: * We're sending an array of a type that has an associated MPI Chris@16: * datatype, so we map directly to that datatype. Chris@16: */ Chris@16: template Chris@16: request Chris@16: array_isend_impl(int dest, int tag, const T* values, int n, Chris@16: mpl::true_) const; Chris@16: Chris@16: /** Chris@16: * INTERNAL ONLY Chris@16: * Chris@16: * We're sending an array of a type that does not have an associated Chris@16: * MPI datatype, so it must be serialized then sent as MPI_PACKED Chris@16: * data, to be deserialized on the receiver side. Chris@16: */ Chris@16: template Chris@16: request Chris@16: array_isend_impl(int dest, int tag, const T* values, int n, Chris@16: mpl::false_) const; Chris@16: Chris@16: /** Chris@16: * INTERNAL ONLY Chris@16: * Chris@16: * We're receiving a type that has an associated MPI datatype, so we Chris@16: * map directly to that datatype. Chris@16: */ Chris@16: template Chris@16: status recv_impl(int source, int tag, T& value, mpl::true_) const; Chris@16: Chris@16: /** Chris@16: * INTERNAL ONLY Chris@16: * Chris@16: * We're receiving a type that does not have an associated MPI Chris@16: * datatype, so it must have been serialized then sent as Chris@16: * MPI_PACKED. We'll receive it and then deserialize. Chris@16: */ Chris@16: template Chris@16: status recv_impl(int source, int tag, T& value, mpl::false_) const; Chris@16: Chris@16: /** Chris@16: * INTERNAL ONLY Chris@16: * Chris@16: * We're receiving an array of a type that has an associated MPI Chris@16: * datatype, so we map directly to that datatype. Chris@16: */ Chris@16: template Chris@16: status Chris@16: array_recv_impl(int source, int tag, T* values, int n, mpl::true_) const; Chris@16: Chris@16: /** Chris@16: * INTERNAL ONLY Chris@16: * Chris@16: * We're receiving a type that does not have an associated MPI Chris@16: * datatype, so it must have been serialized then sent as Chris@16: * MPI_PACKED. We'll receive it and then deserialize. Chris@16: */ Chris@16: template Chris@16: status Chris@16: array_recv_impl(int source, int tag, T* values, int n, mpl::false_) const; Chris@16: Chris@16: /** Chris@16: * INTERNAL ONLY Chris@16: * Chris@16: * We're receiving a type that has an associated MPI datatype, so we Chris@16: * map directly to that datatype. Chris@16: */ Chris@16: template Chris@16: request irecv_impl(int source, int tag, T& value, mpl::true_) const; Chris@16: Chris@16: /** Chris@16: * INTERNAL ONLY Chris@16: * Chris@16: * We're receiving a type that does not have an associated MPI Chris@16: * datatype, so it must have been serialized then sent as Chris@16: * MPI_PACKED. We'll receive it and then deserialize. Chris@16: */ Chris@16: template Chris@16: request irecv_impl(int source, int tag, T& value, mpl::false_) const; Chris@16: Chris@16: /** Chris@16: * INTERNAL ONLY Chris@16: * Chris@16: * We're receiving a type that has an associated MPI datatype, so we Chris@16: * map directly to that datatype. Chris@16: */ Chris@16: template Chris@16: request Chris@16: array_irecv_impl(int source, int tag, T* values, int n, mpl::true_) const; Chris@16: Chris@16: /** Chris@16: * INTERNAL ONLY Chris@16: * Chris@16: * We're receiving a type that does not have an associated MPI Chris@16: * datatype, so it must have been serialized then sent as Chris@16: * MPI_PACKED. We'll receive it and then deserialize. Chris@16: */ Chris@16: template Chris@16: request Chris@16: array_irecv_impl(int source, int tag, T* values, int n, mpl::false_) const; Chris@16: Chris@16: shared_ptr comm_ptr; Chris@16: }; Chris@16: Chris@16: /** Chris@16: * @brief Determines whether two communicators are identical. Chris@16: * Chris@16: * Equivalent to calling @c MPI_Comm_compare and checking whether the Chris@16: * result is @c MPI_IDENT. Chris@16: * Chris@16: * @returns True when the two communicators refer to the same Chris@16: * underlying MPI communicator. Chris@16: */ Chris@16: BOOST_MPI_DECL bool operator==(const communicator& comm1, const communicator& comm2); Chris@16: Chris@16: /** Chris@16: * @brief Determines whether two communicators are different. Chris@16: * Chris@16: * @returns @c !(comm1 == comm2) Chris@16: */ Chris@16: inline bool operator!=(const communicator& comm1, const communicator& comm2) Chris@16: { Chris@16: return !(comm1 == comm2); Chris@16: } Chris@16: Chris@16: Chris@16: /************************************************************************ Chris@16: * Implementation details * Chris@16: ************************************************************************/ Chris@16: // Count elements in a message Chris@16: template Chris@16: inline optional status::count() const Chris@16: { Chris@16: return count_impl(is_mpi_datatype()); Chris@16: } Chris@16: Chris@16: template Chris@16: optional status::count_impl(mpl::true_) const Chris@16: { Chris@16: if (m_count != -1) Chris@16: return m_count; Chris@16: Chris@16: int return_value; Chris@16: BOOST_MPI_CHECK_RESULT(MPI_Get_count, Chris@16: (&m_status, get_mpi_datatype(T()), &return_value)); Chris@16: if (return_value == MPI_UNDEFINED) Chris@16: return optional(); Chris@16: else Chris@16: /* Cache the result. */ Chris@16: return m_count = return_value; Chris@16: } Chris@16: Chris@16: template Chris@16: inline optional status::count_impl(mpl::false_) const Chris@16: { Chris@16: if (m_count == -1) Chris@16: return optional(); Chris@16: else Chris@16: return m_count; Chris@16: } Chris@16: Chris@16: // We're sending a type that has an associated MPI datatype, so we Chris@16: // map directly to that datatype. Chris@16: template Chris@16: void Chris@16: communicator::send_impl(int dest, int tag, const T& value, mpl::true_) const Chris@16: { Chris@16: BOOST_MPI_CHECK_RESULT(MPI_Send, Chris@16: (const_cast(&value), 1, get_mpi_datatype(value), Chris@16: dest, tag, MPI_Comm(*this))); Chris@16: } Chris@16: Chris@16: // We're sending a type that does not have an associated MPI Chris@16: // datatype, so it must be serialized then sent as MPI_PACKED data, Chris@16: // to be deserialized on the receiver side. Chris@16: template Chris@16: void Chris@16: communicator::send_impl(int dest, int tag, const T& value, mpl::false_) const Chris@16: { Chris@16: packed_oarchive oa(*this); Chris@16: oa << value; Chris@16: send(dest, tag, oa); Chris@16: } Chris@16: Chris@16: // Single-element receive may either send the element directly or Chris@16: // serialize it via a buffer. Chris@16: template Chris@16: void communicator::send(int dest, int tag, const T& value) const Chris@16: { Chris@16: this->send_impl(dest, tag, value, is_mpi_datatype()); Chris@16: } Chris@16: Chris@16: // We're sending an array of a type that has an associated MPI Chris@16: // datatype, so we map directly to that datatype. Chris@16: template Chris@16: void Chris@16: communicator::array_send_impl(int dest, int tag, const T* values, int n, Chris@16: mpl::true_) const Chris@16: { Chris@16: BOOST_MPI_CHECK_RESULT(MPI_Send, Chris@16: (const_cast(values), n, Chris@16: get_mpi_datatype(*values), Chris@16: dest, tag, MPI_Comm(*this))); Chris@16: } Chris@16: Chris@16: // We're sending an array of a type that does not have an associated Chris@16: // MPI datatype, so it must be serialized then sent as MPI_PACKED Chris@16: // data, to be deserialized on the receiver side. Chris@16: template Chris@16: void Chris@16: communicator::array_send_impl(int dest, int tag, const T* values, int n, Chris@16: mpl::false_) const Chris@16: { Chris@16: packed_oarchive oa(*this); Chris@16: oa << n << boost::serialization::make_array(values, n); Chris@16: send(dest, tag, oa); Chris@16: } Chris@16: Chris@16: // Array send must send the elements directly Chris@16: template Chris@16: void communicator::send(int dest, int tag, const T* values, int n) const Chris@16: { Chris@16: this->array_send_impl(dest, tag, values, n, is_mpi_datatype()); Chris@16: } Chris@16: Chris@16: // We're receiving a type that has an associated MPI datatype, so we Chris@16: // map directly to that datatype. Chris@16: template Chris@16: status communicator::recv_impl(int source, int tag, T& value, mpl::true_) const Chris@16: { Chris@16: status stat; Chris@16: Chris@16: BOOST_MPI_CHECK_RESULT(MPI_Recv, Chris@16: (const_cast(&value), 1, Chris@16: get_mpi_datatype(value), Chris@16: source, tag, MPI_Comm(*this), &stat.m_status)); Chris@16: return stat; Chris@16: } Chris@16: Chris@16: template Chris@16: status Chris@16: communicator::recv_impl(int source, int tag, T& value, mpl::false_) const Chris@16: { Chris@16: // Receive the message Chris@16: packed_iarchive ia(*this); Chris@16: status stat = recv(source, tag, ia); Chris@16: Chris@16: // Deserialize the data in the message Chris@16: ia >> value; Chris@16: Chris@16: return stat; Chris@16: } Chris@16: Chris@16: // Single-element receive may either receive the element directly or Chris@16: // deserialize it from a buffer. Chris@16: template Chris@16: status communicator::recv(int source, int tag, T& value) const Chris@16: { Chris@16: return this->recv_impl(source, tag, value, is_mpi_datatype()); Chris@16: } Chris@16: Chris@16: template Chris@16: status Chris@16: communicator::array_recv_impl(int source, int tag, T* values, int n, Chris@16: mpl::true_) const Chris@16: { Chris@16: status stat; Chris@16: BOOST_MPI_CHECK_RESULT(MPI_Recv, Chris@16: (const_cast(values), n, Chris@16: get_mpi_datatype(*values), Chris@16: source, tag, MPI_Comm(*this), &stat.m_status)); Chris@16: return stat; Chris@16: } Chris@16: Chris@16: template Chris@16: status Chris@16: communicator::array_recv_impl(int source, int tag, T* values, int n, Chris@16: mpl::false_) const Chris@16: { Chris@16: // Receive the message Chris@16: packed_iarchive ia(*this); Chris@16: status stat = recv(source, tag, ia); Chris@16: Chris@16: // Determine how much data we are going to receive Chris@16: int count; Chris@16: ia >> count; Chris@16: Chris@16: // Deserialize the data in the message Chris@16: boost::serialization::array arr(values, count > n? n : count); Chris@16: ia >> arr; Chris@16: Chris@16: if (count > n) { Chris@16: boost::throw_exception( Chris@16: std::range_error("communicator::recv: message receive overflow")); Chris@16: } Chris@16: Chris@16: stat.m_count = count; Chris@16: return stat; Chris@16: } Chris@16: Chris@16: // Array receive must receive the elements directly into a buffer. Chris@16: template Chris@16: status communicator::recv(int source, int tag, T* values, int n) const Chris@16: { Chris@16: return this->array_recv_impl(source, tag, values, n, is_mpi_datatype()); Chris@16: } Chris@16: Chris@16: // We're sending a type that has an associated MPI datatype, so we Chris@16: // map directly to that datatype. Chris@16: template Chris@16: request Chris@16: communicator::isend_impl(int dest, int tag, const T& value, mpl::true_) const Chris@16: { Chris@16: request req; Chris@16: BOOST_MPI_CHECK_RESULT(MPI_Isend, Chris@16: (const_cast(&value), 1, Chris@16: get_mpi_datatype(value), Chris@16: dest, tag, MPI_Comm(*this), &req.m_requests[0])); Chris@16: return req; Chris@16: } Chris@16: Chris@16: // We're sending a type that does not have an associated MPI Chris@16: // datatype, so it must be serialized then sent as MPI_PACKED data, Chris@16: // to be deserialized on the receiver side. Chris@16: template Chris@16: request Chris@16: communicator::isend_impl(int dest, int tag, const T& value, mpl::false_) const Chris@16: { Chris@16: shared_ptr archive(new packed_oarchive(*this)); Chris@16: *archive << value; Chris@16: request result = isend(dest, tag, *archive); Chris@16: result.m_data = archive; Chris@16: return result; Chris@16: } Chris@16: Chris@16: // Single-element receive may either send the element directly or Chris@16: // serialize it via a buffer. Chris@16: template Chris@16: request communicator::isend(int dest, int tag, const T& value) const Chris@16: { Chris@16: return this->isend_impl(dest, tag, value, is_mpi_datatype()); Chris@16: } Chris@16: Chris@16: template Chris@16: request Chris@16: communicator::array_isend_impl(int dest, int tag, const T* values, int n, Chris@16: mpl::true_) const Chris@16: { Chris@16: request req; Chris@16: BOOST_MPI_CHECK_RESULT(MPI_Isend, Chris@16: (const_cast(values), n, Chris@16: get_mpi_datatype(*values), Chris@16: dest, tag, MPI_Comm(*this), &req.m_requests[0])); Chris@16: return req; Chris@16: } Chris@16: Chris@16: template Chris@16: request Chris@16: communicator::array_isend_impl(int dest, int tag, const T* values, int n, Chris@16: mpl::false_) const Chris@16: { Chris@16: shared_ptr archive(new packed_oarchive(*this)); Chris@16: *archive << n << boost::serialization::make_array(values, n); Chris@16: request result = isend(dest, tag, *archive); Chris@16: result.m_data = archive; Chris@16: return result; Chris@16: } Chris@16: Chris@16: Chris@16: // Array isend must send the elements directly Chris@16: template Chris@16: request communicator::isend(int dest, int tag, const T* values, int n) const Chris@16: { Chris@16: return array_isend_impl(dest, tag, values, n, is_mpi_datatype()); Chris@16: } Chris@16: Chris@16: namespace detail { Chris@16: /** Chris@16: * Internal data structure that stores everything required to manage Chris@16: * the receipt of serialized data via a request object. Chris@16: */ Chris@16: template Chris@16: struct serialized_irecv_data Chris@16: { Chris@16: serialized_irecv_data(const communicator& comm, int source, int tag, Chris@16: T& value) Chris@16: : comm(comm), source(source), tag(tag), ia(comm), value(value) Chris@16: { Chris@16: } Chris@16: Chris@16: void deserialize(status& stat) Chris@16: { Chris@16: ia >> value; Chris@16: stat.m_count = 1; Chris@16: } Chris@16: Chris@16: communicator comm; Chris@16: int source; Chris@16: int tag; Chris@16: std::size_t count; Chris@16: packed_iarchive ia; Chris@16: T& value; Chris@16: }; Chris@16: Chris@16: template<> Chris@16: struct serialized_irecv_data Chris@16: { Chris@16: serialized_irecv_data(const communicator& comm, int source, int tag, Chris@16: packed_iarchive& ia) Chris@16: : comm(comm), source(source), tag(tag), ia(ia) { } Chris@16: Chris@16: void deserialize(status&) { /* Do nothing. */ } Chris@16: Chris@16: communicator comm; Chris@16: int source; Chris@16: int tag; Chris@16: std::size_t count; Chris@16: packed_iarchive& ia; Chris@16: }; Chris@16: Chris@16: /** Chris@16: * Internal data structure that stores everything required to manage Chris@16: * the receipt of an array of serialized data via a request object. Chris@16: */ Chris@16: template Chris@16: struct serialized_array_irecv_data Chris@16: { Chris@16: serialized_array_irecv_data(const communicator& comm, int source, int tag, Chris@16: T* values, int n) Chris@16: : comm(comm), source(source), tag(tag), ia(comm), values(values), n(n) Chris@16: { Chris@16: } Chris@16: Chris@16: void deserialize(status& stat); Chris@16: Chris@16: communicator comm; Chris@16: int source; Chris@16: int tag; Chris@16: std::size_t count; Chris@16: packed_iarchive ia; Chris@16: T* values; Chris@16: int n; Chris@16: }; Chris@16: Chris@16: template Chris@16: void serialized_array_irecv_data::deserialize(status& stat) Chris@16: { Chris@16: // Determine how much data we are going to receive Chris@16: int count; Chris@16: ia >> count; Chris@16: Chris@16: // Deserialize the data in the message Chris@16: boost::serialization::array arr(values, count > n? n : count); Chris@16: ia >> arr; Chris@16: Chris@16: if (count > n) { Chris@16: boost::throw_exception( Chris@16: std::range_error("communicator::recv: message receive overflow")); Chris@16: } Chris@16: Chris@16: stat.m_count = count; Chris@16: } Chris@16: } Chris@16: Chris@16: template Chris@16: optional Chris@16: request::handle_serialized_irecv(request* self, request_action action) Chris@16: { Chris@16: typedef detail::serialized_irecv_data data_t; Chris@16: shared_ptr data = static_pointer_cast(self->m_data); Chris@16: Chris@16: if (action == ra_wait) { Chris@16: status stat; Chris@16: if (self->m_requests[1] == MPI_REQUEST_NULL) { Chris@16: // Wait for the count message to complete Chris@16: BOOST_MPI_CHECK_RESULT(MPI_Wait, Chris@16: (self->m_requests, &stat.m_status)); Chris@16: // Resize our buffer and get ready to receive its data Chris@16: data->ia.resize(data->count); Chris@16: BOOST_MPI_CHECK_RESULT(MPI_Irecv, Chris@16: (data->ia.address(), data->ia.size(), MPI_PACKED, Chris@16: stat.source(), stat.tag(), Chris@16: MPI_Comm(data->comm), self->m_requests + 1)); Chris@16: } Chris@16: Chris@16: // Wait until we have received the entire message Chris@16: BOOST_MPI_CHECK_RESULT(MPI_Wait, Chris@16: (self->m_requests + 1, &stat.m_status)); Chris@16: Chris@16: data->deserialize(stat); Chris@16: return stat; Chris@16: } else if (action == ra_test) { Chris@16: status stat; Chris@16: int flag = 0; Chris@16: Chris@16: if (self->m_requests[1] == MPI_REQUEST_NULL) { Chris@16: // Check if the count message has completed Chris@16: BOOST_MPI_CHECK_RESULT(MPI_Test, Chris@16: (self->m_requests, &flag, &stat.m_status)); Chris@16: if (flag) { Chris@16: // Resize our buffer and get ready to receive its data Chris@16: data->ia.resize(data->count); Chris@16: BOOST_MPI_CHECK_RESULT(MPI_Irecv, Chris@16: (data->ia.address(), data->ia.size(),MPI_PACKED, Chris@16: stat.source(), stat.tag(), Chris@16: MPI_Comm(data->comm), self->m_requests + 1)); Chris@16: } else Chris@16: return optional(); // We have not finished yet Chris@16: } Chris@16: Chris@16: // Check if we have received the message data Chris@16: BOOST_MPI_CHECK_RESULT(MPI_Test, Chris@16: (self->m_requests + 1, &flag, &stat.m_status)); Chris@16: if (flag) { Chris@16: data->deserialize(stat); Chris@16: return stat; Chris@16: } else Chris@16: return optional(); Chris@16: } else { Chris@16: return optional(); Chris@16: } Chris@16: } Chris@16: Chris@16: template Chris@16: optional Chris@16: request::handle_serialized_array_irecv(request* self, request_action action) Chris@16: { Chris@16: typedef detail::serialized_array_irecv_data data_t; Chris@16: shared_ptr data = static_pointer_cast(self->m_data); Chris@16: Chris@16: if (action == ra_wait) { Chris@16: status stat; Chris@16: if (self->m_requests[1] == MPI_REQUEST_NULL) { Chris@16: // Wait for the count message to complete Chris@16: BOOST_MPI_CHECK_RESULT(MPI_Wait, Chris@16: (self->m_requests, &stat.m_status)); Chris@16: // Resize our buffer and get ready to receive its data Chris@16: data->ia.resize(data->count); Chris@16: BOOST_MPI_CHECK_RESULT(MPI_Irecv, Chris@16: (data->ia.address(), data->ia.size(), MPI_PACKED, Chris@16: stat.source(), stat.tag(), Chris@16: MPI_Comm(data->comm), self->m_requests + 1)); Chris@16: } Chris@16: Chris@16: // Wait until we have received the entire message Chris@16: BOOST_MPI_CHECK_RESULT(MPI_Wait, Chris@16: (self->m_requests + 1, &stat.m_status)); Chris@16: Chris@16: data->deserialize(stat); Chris@16: return stat; Chris@16: } else if (action == ra_test) { Chris@16: status stat; Chris@16: int flag = 0; Chris@16: Chris@16: if (self->m_requests[1] == MPI_REQUEST_NULL) { Chris@16: // Check if the count message has completed Chris@16: BOOST_MPI_CHECK_RESULT(MPI_Test, Chris@16: (self->m_requests, &flag, &stat.m_status)); Chris@16: if (flag) { Chris@16: // Resize our buffer and get ready to receive its data Chris@16: data->ia.resize(data->count); Chris@16: BOOST_MPI_CHECK_RESULT(MPI_Irecv, Chris@16: (data->ia.address(), data->ia.size(),MPI_PACKED, Chris@16: stat.source(), stat.tag(), Chris@16: MPI_Comm(data->comm), self->m_requests + 1)); Chris@16: } else Chris@16: return optional(); // We have not finished yet Chris@16: } Chris@16: Chris@16: // Check if we have received the message data Chris@16: BOOST_MPI_CHECK_RESULT(MPI_Test, Chris@16: (self->m_requests + 1, &flag, &stat.m_status)); Chris@16: if (flag) { Chris@16: data->deserialize(stat); Chris@16: return stat; Chris@16: } else Chris@16: return optional(); Chris@16: } else { Chris@16: return optional(); Chris@16: } Chris@16: } Chris@16: Chris@16: // We're receiving a type that has an associated MPI datatype, so we Chris@16: // map directly to that datatype. Chris@16: template Chris@16: request Chris@16: communicator::irecv_impl(int source, int tag, T& value, mpl::true_) const Chris@16: { Chris@16: request req; Chris@16: BOOST_MPI_CHECK_RESULT(MPI_Irecv, Chris@16: (const_cast(&value), 1, Chris@16: get_mpi_datatype(value), Chris@16: source, tag, MPI_Comm(*this), &req.m_requests[0])); Chris@16: return req; Chris@16: } Chris@16: Chris@16: template Chris@16: request Chris@16: communicator::irecv_impl(int source, int tag, T& value, mpl::false_) const Chris@16: { Chris@16: typedef detail::serialized_irecv_data data_t; Chris@16: shared_ptr data(new data_t(*this, source, tag, value)); Chris@16: request req; Chris@16: req.m_data = data; Chris@16: req.m_handler = request::handle_serialized_irecv; Chris@16: Chris@16: BOOST_MPI_CHECK_RESULT(MPI_Irecv, Chris@16: (&data->count, 1, Chris@16: get_mpi_datatype(data->count), Chris@16: source, tag, MPI_Comm(*this), &req.m_requests[0])); Chris@16: Chris@16: return req; Chris@16: } Chris@16: Chris@16: template Chris@16: request Chris@16: communicator::irecv(int source, int tag, T& value) const Chris@16: { Chris@16: return this->irecv_impl(source, tag, value, is_mpi_datatype()); Chris@16: } Chris@16: Chris@16: template Chris@16: request Chris@16: communicator::array_irecv_impl(int source, int tag, T* values, int n, Chris@16: mpl::true_) const Chris@16: { Chris@16: request req; Chris@16: BOOST_MPI_CHECK_RESULT(MPI_Irecv, Chris@16: (const_cast(values), n, Chris@16: get_mpi_datatype(*values), Chris@16: source, tag, MPI_Comm(*this), &req.m_requests[0])); Chris@16: return req; Chris@16: } Chris@16: Chris@16: template Chris@16: request Chris@16: communicator::array_irecv_impl(int source, int tag, T* values, int n, Chris@16: mpl::false_) const Chris@16: { Chris@16: typedef detail::serialized_array_irecv_data data_t; Chris@16: shared_ptr data(new data_t(*this, source, tag, values, n)); Chris@16: request req; Chris@16: req.m_data = data; Chris@16: req.m_handler = request::handle_serialized_array_irecv; Chris@16: Chris@16: BOOST_MPI_CHECK_RESULT(MPI_Irecv, Chris@16: (&data->count, 1, Chris@16: get_mpi_datatype(data->count), Chris@16: source, tag, MPI_Comm(*this), &req.m_requests[0])); Chris@16: Chris@16: return req; Chris@16: } Chris@16: Chris@16: Chris@16: // Array receive must receive the elements directly into a buffer. Chris@16: template Chris@16: request communicator::irecv(int source, int tag, T* values, int n) const Chris@16: { Chris@16: return this->array_irecv_impl(source, tag, values, n, is_mpi_datatype()); Chris@16: } Chris@16: Chris@16: /** Chris@16: * INTERNAL ONLY Chris@16: */ Chris@16: template<> Chris@16: BOOST_MPI_DECL void Chris@16: communicator::send(int dest, int tag, Chris@16: const packed_oarchive& ar) const; Chris@16: Chris@16: /** Chris@16: * INTERNAL ONLY Chris@16: */ Chris@16: template<> Chris@16: BOOST_MPI_DECL void Chris@16: communicator::send Chris@16: (int dest, int tag, const packed_skeleton_oarchive& ar) const; Chris@16: Chris@16: /** Chris@16: * INTERNAL ONLY Chris@16: */ Chris@16: template<> Chris@16: BOOST_MPI_DECL void Chris@16: communicator::send(int dest, int tag, const content& c) const; Chris@16: Chris@16: /** Chris@16: * INTERNAL ONLY Chris@16: */ Chris@16: template<> Chris@16: BOOST_MPI_DECL status Chris@16: communicator::recv(int source, int tag, Chris@16: packed_iarchive& ar) const; Chris@16: Chris@16: /** Chris@16: * INTERNAL ONLY Chris@16: */ Chris@16: template<> Chris@16: BOOST_MPI_DECL status Chris@16: communicator::recv Chris@16: (int source, int tag, packed_skeleton_iarchive& ar) const; Chris@16: Chris@16: /** Chris@16: * INTERNAL ONLY Chris@16: */ Chris@16: template<> Chris@16: BOOST_MPI_DECL status Chris@16: communicator::recv(int source, int tag, Chris@16: const content& c) const; Chris@16: Chris@16: /** Chris@16: * INTERNAL ONLY Chris@16: */ Chris@16: template<> Chris@16: inline status Chris@16: communicator::recv(int source, int tag, Chris@16: content& c) const Chris@16: { Chris@16: return recv(source,tag,c); Chris@16: } Chris@16: Chris@16: /** Chris@16: * INTERNAL ONLY Chris@16: */ Chris@16: template<> Chris@16: BOOST_MPI_DECL request Chris@16: communicator::isend(int dest, int tag, Chris@16: const packed_oarchive& ar) const; Chris@16: Chris@16: /** Chris@16: * INTERNAL ONLY Chris@16: */ Chris@16: template<> Chris@16: BOOST_MPI_DECL request Chris@16: communicator::isend Chris@16: (int dest, int tag, const packed_skeleton_oarchive& ar) const; Chris@16: Chris@16: /** Chris@16: * INTERNAL ONLY Chris@16: */ Chris@16: template<> Chris@16: BOOST_MPI_DECL request Chris@16: communicator::isend(int dest, int tag, const content& c) const; Chris@16: Chris@16: /** Chris@16: * INTERNAL ONLY Chris@16: */ Chris@16: template<> Chris@16: BOOST_MPI_DECL request Chris@16: communicator::irecv Chris@16: (int source, int tag, packed_skeleton_iarchive& ar) const; Chris@16: Chris@16: /** Chris@16: * INTERNAL ONLY Chris@16: */ Chris@16: template<> Chris@16: BOOST_MPI_DECL request Chris@16: communicator::irecv(int source, int tag, Chris@16: const content& c) const; Chris@16: Chris@16: /** Chris@16: * INTERNAL ONLY Chris@16: */ Chris@16: template<> Chris@16: inline request Chris@16: communicator::irecv(int source, int tag, Chris@16: content& c) const Chris@16: { Chris@16: return irecv(source, tag, c); Chris@16: } Chris@16: Chris@16: Chris@16: } } // end namespace boost::mpi Chris@16: Chris@16: // If the user has already included skeleton_and_content.hpp, include Chris@16: // the code to send/receive skeletons and content. Chris@16: #ifdef BOOST_MPI_SKELETON_AND_CONTENT_HPP Chris@16: # include Chris@16: #endif Chris@16: Chris@16: #ifdef BOOST_MSVC Chris@16: # pragma warning(pop) Chris@16: #endif Chris@16: Chris@16: #endif // BOOST_MPI_COMMUNICATOR_HPP