MeDiPack  1.3.3
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-2025 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/medi).
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
68
69 bool isClone;
70
72
73 MpiTypeDefault(Tool* adTool, MPI_Datatype type, MPI_Datatype modType) :
74 Base(type, modType),
75 isClone(false),
76 adTool(adTool) {}
77
78 private:
79 MpiTypeDefault(Tool* adTool, MPI_Datatype type, MPI_Datatype modType, bool isClone) :
80 Base(type, modType),
82 adTool(adTool) {}
83
84 public:
85
87 if(isClone) {
88 MPI_Datatype temp;
89 if(this->getModifiedMpiType() != this->getMpiType()) {
90 temp = this->getModifiedMpiType();
91 MPI_Type_free(&temp);
92 }
93 temp = this->getMpiType();
94 MPI_Type_free(&temp);
95 }
96 }
97
98 const Tool& getADTool() const {
99 return *adTool;
100 }
101
102 int computeActiveElements(const int count) const {
103 return count;
104 }
105
107 return adTool->isModifiedBufferRequired();
108 }
109
110 inline void copyIntoModifiedBuffer(const Type* buf, size_t bufOffset, ModifiedType* bufMod, size_t bufModOffset, int elements) const {
111 if(adTool->isModifiedBufferRequired()) {
112 for(int i = 0; i < elements; ++i) {
113 ADTool::setIntoModifyBuffer(bufMod[bufModOffset + i], buf[bufOffset + i]);
114 }
115 }
116 }
117
118 inline void copyFromModifiedBuffer(Type* buf, size_t bufOffset, const ModifiedType* bufMod, size_t bufModOffset, int elements) const {
119 if(adTool->isModifiedBufferRequired()) {
120 for(int i = 0; i < elements; ++i) {
121 ADTool::getFromModifyBuffer(bufMod[bufModOffset + i], buf[bufOffset + i]);
122 }
123 }
124 }
125
126 inline void getIndices(const Type* buf, size_t bufOffset, IndexType* indices, size_t bufModOffset, int elements) const {
127 int indexOffset = computeActiveElements((int)bufModOffset);
128
129 for(int i = 0; i < elements; ++i) {
130 indices[indexOffset + i] = ADTool::getIndex(buf[bufOffset + i]);
131 }
132 }
133
134 inline void registerValue(Type* buf, size_t bufOffset, IndexType* indices, PrimalType* oldPrimals, size_t bufModOffset, int elements) const {
135 int indexOffset = computeActiveElements((int)bufModOffset);
136
137 for(int i = 0; i < elements; ++i) {
138 ADTool::registerValue(buf[bufOffset + i], oldPrimals[indexOffset + i], indices[indexOffset + i]);
139 }
140 }
141
142 inline void clearIndices(Type* buf, size_t bufOffset, int elements) const {
143 for(int i = 0; i < elements; ++i) {
144 ADTool::clearIndex(buf[bufOffset + i]);
145 }
146 }
147
148 inline void createIndices(Type* buf, size_t bufOffset, IndexType* indices, size_t bufModOffset, int elements) const {
149 int indexOffset = computeActiveElements((int)bufModOffset);
150
151 for(int i = 0; i < elements; ++i) {
152 ADTool::createIndex(buf[bufOffset + i], indices[indexOffset + i]);
153 }
154 }
155
156 inline void getValues(const Type* buf, size_t bufOffset, PrimalType* primals, size_t bufModOffset, int elements) const {
157 int primalOffset = computeActiveElements((int)bufModOffset);
158
159 for(int pos = 0; pos < elements; ++pos) {
160 primals[primalOffset + pos] = ADTool::getValue(buf[bufOffset + pos]);
161 }
162 }
163
164 inline void performReduce(Type* buf, Type* target, int count, AMPI_Op op, int ranks) const {
165 for(int j = 1; j < ranks; ++j) {
166 MPI_Reduce_local(&buf[j * count], buf, count, this->getMpiType(), op.primalFunction);
167 }
168
169 if(0 != ranks) {
170 copy(buf, 0, target, 0, count);
171 }
172 }
173
174 using Base::copy;
175 void copy(Type* from, size_t fromOffset, Type* to, size_t toOffset, int count) const {
176 for(int i = 0; i < count; ++i) {
177 to[toOffset + i] = from[fromOffset + i];
178 }
179
180 }
181
182 void initializeType(Type* buf, size_t bufOffset, int elements) const {
183 for(int i = 0; i < elements; ++i) {
184 new(&buf[bufOffset + i]) Type;
185 }
186 }
187
188 void freeType(Type* buf, size_t bufOffset, int elements) const {
189 for(int i = 0; i < elements; ++i) {
190 buf[bufOffset + i].~Type();
191 }
192 }
193
194 inline void createTypeBuffer(Type* &buf, size_t size) const {
195 buf = new Type[size];
196 }
197
198 inline void createModifiedTypeBuffer(ModifiedType* &buf, size_t size) const {
199 buf = new ModifiedType[size];
200 }
201
202 inline void deleteTypeBuffer(Type* &buf, size_t size) const {
203 MEDI_UNUSED(size);
204
205 if(NULL != buf) {
206 delete [] buf;
207 buf = NULL;
208 }
209 }
210
211 inline void deleteModifiedTypeBuffer(ModifiedType* &buf) const {
212 if(NULL != buf) {
213 delete [] buf;
214 buf = NULL;
215 }
216 }
217
218 inline MpiTypeDefault* clone() const {
219 MPI_Datatype type;
220 MPI_Datatype modType;
221
222 MPI_Type_dup(this->getMpiType(), &type);
223 if(this->getMpiType() != this->getModifiedMpiType()) {
224 MPI_Type_dup(this->getModifiedMpiType(), &modType);
225 } else {
226 modType = type;
227 }
228
229 return new MpiTypeDefault(adTool, this->getMpiType(), this->getModifiedMpiType(), true);
230 }
231 };
232}
void copy(void *from, size_t fromOffset, void *to, size_t toOffset, int count) const
Definition typeInterface.hpp:369
MpiTypeBase(MPI_Datatype mpiType, MPI_Datatype modifiedMpiType)
Definition typeInterface.hpp:330
~MpiTypeDefault()
Definition typeDefault.hpp:86
ADTool::Type Type
Definition typeDefault.hpp:59
void clearIndices(Type *buf, size_t bufOffset, int elements) const
Definition typeDefault.hpp:142
const Tool & getADTool() const
Get the AD tool that handled the AD specifics.
Definition typeDefault.hpp:98
void initializeType(Type *buf, size_t bufOffset, int elements) const
Definition typeDefault.hpp:182
void createTypeBuffer(Type *&buf, size_t size) const
Definition typeDefault.hpp:194
MpiTypeDefault(Tool *adTool, MPI_Datatype type, MPI_Datatype modType)
Definition typeDefault.hpp:73
int computeActiveElements(const int count) const
Get the number of active elements that are contained in count versions of the type.
Definition typeDefault.hpp:102
void deleteModifiedTypeBuffer(ModifiedType *&buf) const
Definition typeDefault.hpp:211
MpiTypeDefault * clone() const
Creates a clone of the mpi type also calling MPI_Type_dub.
Definition typeDefault.hpp:218
void copyIntoModifiedBuffer(const Type *buf, size_t bufOffset, ModifiedType *bufMod, size_t bufModOffset, int elements) const
Definition typeDefault.hpp:110
MpiTypeBase< MpiTypeDefault< ADTool >, Type, ModifiedType, Tool > Base
Definition typeDefault.hpp:67
void createIndices(Type *buf, size_t bufOffset, IndexType *indices, size_t bufModOffset, int elements) const
Definition typeDefault.hpp:148
ADTool::PrimalType PrimalType
Definition typeDefault.hpp:61
void createModifiedTypeBuffer(ModifiedType *&buf, size_t size) const
Definition typeDefault.hpp:198
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:118
void performReduce(Type *buf, Type *target, int count, AMPI_Op op, int ranks) const
Definition typeDefault.hpp:164
void copy(Type *from, size_t fromOffset, Type *to, size_t toOffset, int count) const
Definition typeDefault.hpp:175
ADTool Tool
Definition typeDefault.hpp:65
void deleteTypeBuffer(Type *&buf, size_t size) const
Definition typeDefault.hpp:202
bool isModifiedBufferRequired() const
Tell the functions if the underlying AD tool requires new send/recv buffers or if the original buffer...
Definition typeDefault.hpp:106
void freeType(Type *buf, size_t bufOffset, int elements) const
Definition typeDefault.hpp:188
ADTool::ModifiedType ModifiedType
Definition typeDefault.hpp:60
Tool * adTool
Definition typeDefault.hpp:71
bool isClone
Definition typeDefault.hpp:69
void getValues(const Type *buf, size_t bufOffset, PrimalType *primals, size_t bufModOffset, int elements) const
Definition typeDefault.hpp:156
void registerValue(Type *buf, size_t bufOffset, IndexType *indices, PrimalType *oldPrimals, size_t bufModOffset, int elements) const
Definition typeDefault.hpp:134
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:126
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