/* Reticulum Simulator Parallelonium: mpi_wrapper.cc * * Copyright (C) 2005, Yann Golanski * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #include "main.h" /* Both mpi_send_object() and mpi_recv_object() are adapted from * Adrian Sheppard's code. */ template < class T > void mpi_send_object(const T & obj, int proc, int tag, const MPI::Intracomm & comm) { try { std::ostringstream oss; boost::archive::binary_oarchive oa(oss); oa << obj; std::string str(oss.str()); int size = str.size(); comm.Ssend(&size, 1, MPI_INT, proc, tag); comm.Ssend(&str[0], size, MPI_CHAR, proc, tag); } catch (MPI::Exception e) { std::cout << "mpi_send_object(): " << "RSP MPI ERROR [" << e.Get_error_code() << "] : \"" << e.Get_error_string() << "\"" << std::endl; throw e; } catch (...) { std::cerr << "mpi_send_object(): Unknown expection" << std::endl; throw; } } template < class T > void mpi_recv_object(T & obj, int proc, int tag, const MPI::Intracomm & comm) { try { int size; MPI::Status status; comm.Recv(&size, 1, MPI_INT, proc, tag, status); std::string buf(size, ' '); comm.Recv(&buf[0], size, MPI_CHAR, proc, tag, status); std::istringstream iss(buf); boost::archive::binary_iarchive ia(iss); ia >> obj; } catch (MPI::Exception e) { std::cout << "mpi_recv_object(): " << "RSP MPI ERROR [" << e.Get_error_code() << "] : \"" << e.Get_error_string() << "\"" << std::endl; throw e; } catch (...) { std::cerr << "mpi_recv_object(): Unknown expection" << std::endl; throw; } }