| 1 | /*
|
|---|
| 2 | * vmg - a versatile multigrid solver
|
|---|
| 3 | * Copyright (C) 2012 Institute for Numerical Simulation, University of Bonn
|
|---|
| 4 | *
|
|---|
| 5 | * vmg is free software: you can redistribute it and/or modify
|
|---|
| 6 | * it under the terms of the GNU General Public License as published by
|
|---|
| 7 | * the Free Software Foundation, either version 3 of the License, or
|
|---|
| 8 | * (at your option) any later version.
|
|---|
| 9 | *
|
|---|
| 10 | * vmg is distributed in the hope that it will be useful,
|
|---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 13 | * GNU General Public License for more details.
|
|---|
| 14 | *
|
|---|
| 15 | * You should have received a copy of the GNU General Public License
|
|---|
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|---|
| 17 | */
|
|---|
| 18 |
|
|---|
| 19 | #ifdef HAVE_CONFIG_H
|
|---|
| 20 | #include <config.h>
|
|---|
| 21 | #endif
|
|---|
| 22 |
|
|---|
| 23 | #ifndef HAVE_MPI
|
|---|
| 24 | #error MPI is needed to compile VMG::MPI::Datatype
|
|---|
| 25 | #endif
|
|---|
| 26 |
|
|---|
| 27 | #include <mpi.h>
|
|---|
| 28 | #ifdef HAVE_MARMOT
|
|---|
| 29 | #include <enhancempicalls.h>
|
|---|
| 30 | #include <sourceinfompicalls.h>
|
|---|
| 31 | #endif
|
|---|
| 32 |
|
|---|
| 33 | #include <cstring>
|
|---|
| 34 |
|
|---|
| 35 | #include "comm/mpi/datatype.hpp"
|
|---|
| 36 |
|
|---|
| 37 | using namespace VMG;
|
|---|
| 38 |
|
|---|
| 39 | void VMG::MPI::Datatype::Send(Grid& grid, const int& tag, const MPI_Comm& comm) const
|
|---|
| 40 | {
|
|---|
| 41 | if (Feasible())
|
|---|
| 42 | MPI_Send(&grid(0), 1, _type, _rank, _tag_send+tag, comm);
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | void VMG::MPI::Datatype::Isend(Grid& grid, const int& tag, const MPI_Comm& comm, MPI_Request& request) const
|
|---|
| 46 | {
|
|---|
| 47 | if (Feasible())
|
|---|
| 48 | MPI_Isend(&grid(0), 1, _type, _rank, _tag_send+tag, comm, &request);
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | void VMG::MPI::Datatype::Recv(Grid& grid, const int& tag, const MPI_Comm& comm) const
|
|---|
| 52 | {
|
|---|
| 53 | if (Feasible())
|
|---|
| 54 | MPI_Recv(&grid(0), 1 ,_type, _rank, _tag_recv+tag, comm, MPI_STATUS_IGNORE);
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | void VMG::MPI::Datatype::Irecv(Grid& grid, const int& tag, const MPI_Comm& comm, MPI_Request& request) const
|
|---|
| 58 | {
|
|---|
| 59 | if (Feasible())
|
|---|
| 60 | MPI_Irecv(&grid(0), 1, _type, _rank, _tag_recv+tag, comm, &request);
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | void VMG::MPI::Datatype::SendBuffered(const Grid& grid, const int& tag, const MPI_Comm& comm)
|
|---|
| 64 | {
|
|---|
| 65 | if (Feasible()) {
|
|---|
| 66 |
|
|---|
| 67 | Index i;
|
|---|
| 68 | int c = 0;
|
|---|
| 69 | const Index end = _starts + _subsizes;
|
|---|
| 70 | const size_t memcpy_size = _subsizes.Z() * sizeof(vmg_float);
|
|---|
| 71 |
|
|---|
| 72 | for (i.X()=_starts.X(); i.X()<end.X(); ++i.X())
|
|---|
| 73 | for (i.Y()=_starts.Y(); i.Y()<end.Y(); ++i.Y()) {
|
|---|
| 74 | std::memcpy(&_buffer[c], &grid.GetVal(i.X(), i.Y(), _starts.Z()), memcpy_size);
|
|---|
| 75 | c += _subsizes.Z();
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | MPI_Send(&_buffer.front(), _buffer.size(), MPI_DOUBLE, _rank, _tag_send+tag, comm);
|
|---|
| 79 | }
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | void VMG::MPI::Datatype::IsendBuffered(const Grid& grid, const int& tag, const MPI_Comm& comm, MPI_Request& request)
|
|---|
| 83 | {
|
|---|
| 84 | if (Feasible()) {
|
|---|
| 85 |
|
|---|
| 86 | Index i;
|
|---|
| 87 | unsigned int c = 0;
|
|---|
| 88 | const Index end = _starts + _subsizes;
|
|---|
| 89 | const size_t memcpy_size = _subsizes.Z() * sizeof(vmg_float);
|
|---|
| 90 |
|
|---|
| 91 | for (i.X()=_starts.X(); i.X()<end.X(); ++i.X())
|
|---|
| 92 | for (i.Y()=_starts.Y(); i.Y()<end.Y(); ++i.Y()) {
|
|---|
| 93 | std::memcpy(&_buffer[c], &grid.GetVal(i.X(), i.Y(), _starts.Z()), memcpy_size);
|
|---|
| 94 | c += _subsizes.Z();
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | assert(c == _buffer.size());
|
|---|
| 98 |
|
|---|
| 99 | MPI_Isend(&_buffer.front(), _buffer.size(), MPI_DOUBLE, _rank, _tag_send+tag, comm, &request);
|
|---|
| 100 | }
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | void VMG::MPI::Datatype::RecvBuffered(const int& tag, const MPI_Comm& comm)
|
|---|
| 104 | {
|
|---|
| 105 | if (Feasible())
|
|---|
| 106 | MPI_Recv(&_buffer.front(), _buffer.size(), MPI_DOUBLE, _rank, _tag_recv+tag, comm, MPI_STATUS_IGNORE);
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | void VMG::MPI::Datatype::IrecvBuffered(const int& tag, const MPI_Comm& comm, MPI_Request& request)
|
|---|
| 110 | {
|
|---|
| 111 | if (Feasible())
|
|---|
| 112 | MPI_Irecv(&_buffer.front(), _buffer.size(), MPI_DOUBLE, _rank, _tag_recv+tag, comm, &request);
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | void VMG::MPI::Datatype::GridReplace(Grid& grid) const
|
|---|
| 116 | {
|
|---|
| 117 | if (Feasible()) {
|
|---|
| 118 |
|
|---|
| 119 | Index i;
|
|---|
| 120 | unsigned int c = 0;
|
|---|
| 121 | const Index end = _starts + _subsizes;
|
|---|
| 122 | const size_t memcpy_size = _subsizes.Z() * sizeof(vmg_float);
|
|---|
| 123 |
|
|---|
| 124 | for (i.X()=_starts.X(); i.X()<end.X(); ++i.X())
|
|---|
| 125 | for (i.Y()=_starts.Y(); i.Y()<end.Y(); ++i.Y()) {
|
|---|
| 126 | std::memcpy(&grid(i.X(), i.Y(), _starts.Z()), &_buffer[c], memcpy_size);
|
|---|
| 127 | c += _subsizes.Z();
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | assert(c == _buffer.size());
|
|---|
| 131 | }
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | void VMG::MPI::Datatype::GridSum(Grid& grid) const
|
|---|
| 135 | {
|
|---|
| 136 | if (Feasible()) {
|
|---|
| 137 |
|
|---|
| 138 | Index i;
|
|---|
| 139 | const Index end = _starts + _subsizes;
|
|---|
| 140 | std::vector<vmg_float>::const_iterator iter = _buffer.begin();
|
|---|
| 141 |
|
|---|
| 142 | for (i.X()=_starts.X(); i.X()<end.X(); ++i.X())
|
|---|
| 143 | for (i.Y()=_starts.Y(); i.Y()<end.Y(); ++i.Y())
|
|---|
| 144 | for (i.Z()=_starts.Z(); i.Z()<end.Z(); ++i.Z())
|
|---|
| 145 | grid(i) += *iter++;
|
|---|
| 146 |
|
|---|
| 147 | assert(iter == _buffer.end());
|
|---|
| 148 | }
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | void VMG::MPI::Datatype::Set(const GridIteratorSet& bounds, const Grid& grid, const int& rank,
|
|---|
| 152 | const int& tag_send, const int& tag_receive)
|
|---|
| 153 | {
|
|---|
| 154 | _sizes = grid.Local().SizeTotal();
|
|---|
| 155 | _subsizes = bounds.Begin().GetEnd() - bounds.Begin().GetBegin();
|
|---|
| 156 | _starts = bounds.Begin().GetBegin();
|
|---|
| 157 | _rank = rank;
|
|---|
| 158 | _tag_send = tag_send;
|
|---|
| 159 | _tag_recv = tag_receive;
|
|---|
| 160 |
|
|---|
| 161 | if (_type != MPI_DATATYPE_NULL)
|
|---|
| 162 | MPI_Type_free(&_type);
|
|---|
| 163 |
|
|---|
| 164 | InitDatatype();
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | void VMG::MPI::Datatype::Set(const Index& sizes, const Index& subsizes, const Index& starts, const int& rank,
|
|---|
| 168 | const int& tag_send, const int& tag_receive)
|
|---|
| 169 | {
|
|---|
| 170 | _sizes = sizes;
|
|---|
| 171 | _subsizes = subsizes;
|
|---|
| 172 | _starts = starts;
|
|---|
| 173 | _rank = rank;
|
|---|
| 174 | _tag_send = tag_send;
|
|---|
| 175 | _tag_recv = tag_receive;
|
|---|
| 176 |
|
|---|
| 177 | if (_type != MPI_DATATYPE_NULL)
|
|---|
| 178 | MPI_Type_free(&_type);
|
|---|
| 179 |
|
|---|
| 180 | InitDatatype();
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | void VMG::MPI::Datatype::InitDatatype()
|
|---|
| 184 | {
|
|---|
| 185 | if (Feasible()) {
|
|---|
| 186 | MPI_Type_create_subarray(3, _sizes.vec(), _subsizes.vec(), _starts.vec(), MPI_ORDER_C, MPI_DOUBLE, &_type);
|
|---|
| 187 | MPI_Type_commit(&_type);
|
|---|
| 188 | if (_alloc_buffer)
|
|---|
| 189 | _buffer.resize(_subsizes.Product());
|
|---|
| 190 | }else {
|
|---|
| 191 | _type = MPI_DATATYPE_NULL;
|
|---|
| 192 | }
|
|---|
| 193 | }
|
|---|