MeDiPack  1.3.0
A Message Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
typeDefault.hpp
Go to the documentation of this file.
1/*
2 * MeDiPack, a Message Differentiation Package
3 *
4 * Copyright (C) 2015-2024 Chair for Scientific Computing (SciComp), University of Kaiserslautern-Landau
5 * Homepage: http://scicomp.rptu.de
6 * Contact: Prof. Nicolas R. Gauger (codi@scicomp.uni-kl.de)
7 *
8 * Lead developers: Max Sagebaum (SciComp, University of Kaiserslautern-Landau)
9 *
10 * This file is part of MeDiPack (http://scicomp.rptu.de/software/codi).
11 *
12 * MeDiPack is free software: you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation, either
15 * version 3 of the License, or (at your option) any later version.
16 *
17 * MeDiPack is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20 *
21 * See the GNU Lesser General Public License for more details.
22 * You should have received a copy of the GNU
23 * Lesser General Public License along with MeDiPack.
24 * If not, see <http://www.gnu.org/licenses/>.
25 *
26 * Authors: Max Sagebaum, Tim Albring (SciComp, University of Kaiserslautern-Landau)
27 */
28
29#pragma once
30
31#include <new>
32
33#include "../macros.h"
34#include "typeInterface.hpp"
35#include "op.hpp"
36
40namespace medi {
41
47 template<typename ADTool>
48 class MpiTypeDefault final
49 : public MpiTypeBase<
50 MpiTypeDefault<ADTool>,
51 typename ADTool::Type,
52 typename ADTool::ModifiedType,
53 ADTool>
54 {
56
57 public:
58
59 typedef typename ADTool::Type Type;
60 typedef typename ADTool::ModifiedType ModifiedType;
61 typedef typename ADTool::PrimalType PrimalType;
62 typedef typename ADTool::AdjointType AdjointType;
63 typedef typename ADTool::IndexType IndexType;
64
65 typedef ADTool Tool;
66
67 bool isClone;
68
70
71 MpiTypeDefault(Tool* adTool, MPI_Datatype type, MPI_Datatype modType) :
72 MpiTypeBase<MpiTypeDefault<ADTool>, Type, ModifiedType, Tool>(type, modType),
73 isClone(false),
74 adTool(adTool) {}
75
76 private:
77 MpiTypeDefault(Tool* adTool, MPI_Datatype type, MPI_Datatype modType, bool isClone) :
78 MpiTypeBase<MpiTypeDefault<ADTool>, Type, ModifiedType, Tool>(type, modType),
80 adTool(adTool) {}
81
82 public:
83
85 if(isClone) {
86 MPI_Datatype temp;
87 if(this->getModifiedMpiType() != this->getMpiType()) {
88 temp = this->getModifiedMpiType();
89 MPI_Type_free(&temp);
90 }
91 temp = this->getMpiType();
92 MPI_Type_free(&temp);
93 }
94 }
95
96 const Tool& getADTool() const {
97 return *adTool;
98 }
99
100 int computeActiveElements(const int count) const {
101 return count;
102 }
103
105 return adTool->isModifiedBufferRequired();
106 }
107
108 inline void copyIntoModifiedBuffer(const Type* buf, size_t bufOffset, ModifiedType* bufMod, size_t bufModOffset, int elements) const {
109 if(adTool->isModifiedBufferRequired()) {
110 for(int i = 0; i < elements; ++i) {
111 ADTool::setIntoModifyBuffer(bufMod[bufModOffset + i], buf[bufOffset + i]);
112 }
113 }
114 }
115
116 inline void copyFromModifiedBuffer(Type* buf, size_t bufOffset, const ModifiedType* bufMod, size_t bufModOffset, int elements) const {
117 if(adTool->isModifiedBufferRequired()) {
118 for(int i = 0; i < elements; ++i) {
119 ADTool::getFromModifyBuffer(bufMod[bufModOffset + i], buf[bufOffset + i]);
120 }
121 }
122 }
123
124 inline void getIndices(const Type* buf, size_t bufOffset, IndexType* indices, size_t bufModOffset, int elements) const {
125 int indexOffset = computeActiveElements((int)bufModOffset);
126
127 for(int i = 0; i < elements; ++i) {
128 indices[indexOffset + i] = ADTool::getIndex(buf[bufOffset + i]);
129 }
130 }
131
132 inline void registerValue(Type* buf, size_t bufOffset, IndexType* indices, PrimalType* oldPrimals, size_t bufModOffset, int elements) const {
133 int indexOffset = computeActiveElements((int)bufModOffset);
134
135 for(int i = 0; i < elements; ++i) {
136 ADTool::registerValue(buf[bufOffset + i], oldPrimals[indexOffset + i], indices[indexOffset + i]);
137 }
138 }
139
140 inline void clearIndices(Type* buf, size_t bufOffset, int elements) const {
141 for(int i = 0; i < elements; ++i) {
142 ADTool::clearIndex(buf[bufOffset + i]);
143 }
144 }
145
146 inline void createIndices(Type* buf, size_t bufOffset, IndexType* indices, size_t bufModOffset, int elements) const {
147 int indexOffset = computeActiveElements((int)bufModOffset);
148
149 for(int i = 0; i < elements; ++i) {
150 ADTool::createIndex(buf[bufOffset + i], indices[indexOffset + i]);
151 }
152 }
153
154 inline void getValues(const Type* buf, size_t bufOffset, PrimalType* primals, size_t bufModOffset, int elements) const {
155 int primalOffset = computeActiveElements((int)bufModOffset);
156
157 for(int pos = 0; pos < elements; ++pos) {
158 primals[primalOffset + pos] = ADTool::getValue(buf[bufOffset + pos]);
159 }
160 }
161
162 inline void performReduce(Type* buf, Type* target, int count, AMPI_Op op, int ranks) const {
163 for(int j = 1; j < ranks; ++j) {
164 MPI_Reduce_local(&buf[j * count], buf, count, this->getMpiType(), op.primalFunction);
165 }
166
167 if(0 != ranks) {
168 copy(buf, 0, target, 0, count);
169 }
170 }
171
172 void copy(Type* from, size_t fromOffset, Type* to, size_t toOffset, int count) const {
173 for(int i = 0; i < count; ++i) {
174 to[toOffset + i] = from[fromOffset + i];
175 }
176
177 }
178
179 void initializeType(Type* buf, size_t bufOffset, int elements) const {
180 for(int i = 0; i < elements; ++i) {
181 new(&buf[bufOffset + i]) Type;
182 }
183 }
184
185 void freeType(Type* buf, size_t bufOffset, int elements) const {
186 for(int i = 0; i < elements; ++i) {
187 buf[bufOffset + i].~Type();
188 }
189 }
190
191 inline void createTypeBuffer(Type* &buf, size_t size) const {
192 buf = new Type[size];
193 }
194
195 inline void createModifiedTypeBuffer(ModifiedType* &buf, size_t size) const {
196 buf = new ModifiedType[size];
197 }
198
199 inline void deleteTypeBuffer(Type* &buf, size_t size) const {
200 MEDI_UNUSED(size);
201
202 if(NULL != buf) {
203 delete [] buf;
204 buf = NULL;
205 }
206 }
207
208 inline void deleteModifiedTypeBuffer(ModifiedType* &buf) const {
209 if(NULL != buf) {
210 delete [] buf;
211 buf = NULL;
212 }
213 }
214
215 inline MpiTypeDefault* clone() const {
216 MPI_Datatype type;
217 MPI_Datatype modType;
218
219 MPI_Type_dup(this->getMpiType(), &type);
220 if(this->getMpiType() != this->getModifiedMpiType()) {
221 MPI_Type_dup(this->getModifiedMpiType(), &modType);
222 } else {
223 modType = type;
224 }
225
226 return new MpiTypeDefault(adTool, this->getMpiType(), this->getModifiedMpiType(), true);
227 }
228 };
229}
Definition typeInterface.hpp:324
The default implementation of a MPI type that is represented by an AD type.
Definition typeDefault.hpp:54
~MpiTypeDefault()
Definition typeDefault.hpp:84
ADTool::Type Type
Definition typeDefault.hpp:59
void clearIndices(Type *buf, size_t bufOffset, int elements) const
Definition typeDefault.hpp:140
const Tool & getADTool() const
Get the AD tool that handled the AD specifics.
Definition typeDefault.hpp:96
void initializeType(Type *buf, size_t bufOffset, int elements) const
Definition typeDefault.hpp:179
void createTypeBuffer(Type *&buf, size_t size) const
Definition typeDefault.hpp:191
MpiTypeDefault(Tool *adTool, MPI_Datatype type, MPI_Datatype modType)
Definition typeDefault.hpp:71
int computeActiveElements(const int count) const
Get the number of active elements that are contained in count versions of the type.
Definition typeDefault.hpp:100
void deleteModifiedTypeBuffer(ModifiedType *&buf) const
Definition typeDefault.hpp:208
MpiTypeDefault * clone() const
Creates a clone of the mpi type also calling MPI_Type_dub.
Definition typeDefault.hpp:215
void copyIntoModifiedBuffer(const Type *buf, size_t bufOffset, ModifiedType *bufMod, size_t bufModOffset, int elements) const
Definition typeDefault.hpp:108
void createIndices(Type *buf, size_t bufOffset, IndexType *indices, size_t bufModOffset, int elements) const
Definition typeDefault.hpp:146
ADTool::PrimalType PrimalType
Definition typeDefault.hpp:61
void createModifiedTypeBuffer(ModifiedType *&buf, size_t size) const
Definition typeDefault.hpp:195
ADTool::IndexType IndexType
Definition typeDefault.hpp:63
void copyFromModifiedBuffer(Type *buf, size_t bufOffset, const ModifiedType *bufMod, size_t bufModOffset, int elements) const
Definition typeDefault.hpp:116
void performReduce(Type *buf, Type *target, int count, AMPI_Op op, int ranks) const
Definition typeDefault.hpp:162
void copy(Type *from, size_t fromOffset, Type *to, size_t toOffset, int count) const
Definition typeDefault.hpp:172
ADTool Tool
Definition typeDefault.hpp:65
void deleteTypeBuffer(Type *&buf, size_t size) const
Definition typeDefault.hpp:199
bool isModifiedBufferRequired() const
Tell the functions if the underlying AD tool requires new send/recv buffers or if the original buffer...
Definition typeDefault.hpp:104
void freeType(Type *buf, size_t bufOffset, int elements) const
Definition typeDefault.hpp:185
ADTool::ModifiedType ModifiedType
Definition typeDefault.hpp:60
Tool * adTool
Definition typeDefault.hpp:69
bool isClone
Definition typeDefault.hpp:67
void getValues(const Type *buf, size_t bufOffset, PrimalType *primals, size_t bufModOffset, int elements) const
Definition typeDefault.hpp:154
void registerValue(Type *buf, size_t bufOffset, IndexType *indices, PrimalType *oldPrimals, size_t bufModOffset, int elements) const
Definition typeDefault.hpp:132
ADTool::AdjointType AdjointType
Definition typeDefault.hpp:62
void getIndices(const Type *buf, size_t bufOffset, IndexType *indices, size_t bufModOffset, int elements) const
Definition typeDefault.hpp:124
MPI_Datatype getMpiType() const
Return the MPI type for the data that this interface represents.
Definition typeInterface.hpp:127
MPI_Datatype getModifiedMpiType() const
Return the MPI type for the modified data.
Definition typeInterface.hpp:135
#define INTERFACE_DEF(interface, name,...)
Definition macros.h:116
#define MEDI_UNUSED(name)
Definition macros.h:108
Global namespace for MeDiPack - Message Differentiation Package.
Definition adjointInterface.hpp:37
Structure for the special handling of the MPI_Op structure.
Definition op.hpp:50
MPI_Op primalFunction
The mpi operator for the unmodified AD types. The AD tool needs to record all operations that are eva...
Definition op.hpp:69
The static methods for the AD tool interface.
Definition adToolInterface.h:201