MeDiPack  1.3.0
A Message Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
typeInterface.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 <mpi.h>
32
33#include "../adToolInterface.h"
34#include "op.hpp"
35
39namespace medi {
40
64 private:
65
66 MPI_Datatype mpiType;
67 MPI_Datatype modifiedMpiType;
68 public:
69
73 typedef void Type;
74
80 typedef void ModifiedType;
81
85 typedef void PrimalType;
86
90 typedef void IndexType;
91
92 //TODO: remove
93 typedef void AdjointType;
94
100 MpiTypeInterface(MPI_Datatype mpiType, MPI_Datatype modifiedMpiType) :
101 mpiType(mpiType),
102 modifiedMpiType(modifiedMpiType) {}
103
107 virtual ~MpiTypeInterface() {}
108
109 protected:
110
116 void setMpiTypes(MPI_Datatype mpiType, MPI_Datatype modifiedMpiType) {
117 this->mpiType = mpiType;
118 this->modifiedMpiType = modifiedMpiType;
119 }
120
121 public:
122
127 MPI_Datatype getMpiType() const {
128 return mpiType;
129 }
130
135 MPI_Datatype getModifiedMpiType() const {
136 return modifiedMpiType;
137 }
138
139
145 virtual bool isModifiedBufferRequired() const = 0;
146
153 virtual int computeActiveElements(const int count) const = 0;
154
159 virtual const ADToolInterface& getADTool() const = 0;
160
171 virtual void copyIntoModifiedBuffer(const void* buf, size_t bufOffset, void* bufMod, size_t bufModOffset, int elements) const = 0;
172
183 virtual void copyFromModifiedBuffer(void* buf, size_t bufOffset, const void* bufMod, size_t bufModOffset, int elements) const = 0;
184
194 virtual void getIndices(const void* buf, size_t bufOffset, void* indices, size_t bufModOffset, int elements) const = 0;
195
206 virtual void registerValue(void* buf, size_t bufOffset, void* indices, void* oldPrimals, size_t bufModOffset, int elements) const = 0;
207
215 virtual void clearIndices(void* buf, size_t bufOffset, int elements) const = 0;
216
226 virtual void createIndices(void* buf, size_t bufOffset, void* indices, size_t bufModOffset, int elements) const = 0;
227
237 virtual void getValues(const void* buf, size_t bufOffset, void* primals, size_t bufModOffset, int elements) const = 0;
238
248 virtual void performReduce(void* buf, void* target, int count, AMPI_Op op, int ranks) const = 0;
249
259 virtual void copy(void* from, size_t fromOffset, void* to, size_t toOffset, int count) const = 0;
260
268 virtual void initializeType(Type* buf, size_t bufOffset, int elements) const = 0;
269
277 virtual void freeType(Type* buf, size_t bufOffset, int elements) const = 0;
278
285 virtual void createTypeBuffer(void* &buf, size_t size) const = 0;
286
293 virtual void createModifiedTypeBuffer(void* &buf, size_t size) const = 0;
294
301 virtual void deleteTypeBuffer(void* &buf, size_t size) const = 0;
302
308 virtual void deleteModifiedTypeBuffer(void* &buf) const = 0;
309
315 virtual MpiTypeInterface* clone() const = 0;
316 };
317
323 template <typename Impl, typename TypeB, typename ModifiedTypeB, typename ADToolB>
325 public:
326
327 typedef typename ADToolB::PrimalType PrimalTypeB;
328 typedef typename ADToolB::IndexType IndexTypeB;
329
330 MpiTypeBase(MPI_Datatype mpiType, MPI_Datatype modifiedMpiType) :
331 MpiTypeInterface(mpiType, modifiedMpiType) {}
332
333 int computeActiveElements(const int count) const {
334 return cast().computeActiveElements(count);
335 }
336
337 void copyIntoModifiedBuffer(const void* buf, size_t bufOffset, void* bufMod, size_t bufModOffset, int elements) const {
338 cast().copyIntoModifiedBuffer(castBuffer<TypeB>(buf), bufOffset, castBuffer<ModifiedTypeB>(bufMod), bufModOffset, elements);
339 }
340
341 void copyFromModifiedBuffer(void* buf, size_t bufOffset, const void* bufMod, size_t bufModOffset, int elements) const {
342 cast().copyFromModifiedBuffer(castBuffer<TypeB>(buf), bufOffset, castBuffer<ModifiedTypeB>(bufMod), bufModOffset, elements);
343 }
344
345 void getIndices(const void* buf, size_t bufOffset, void* indices, size_t bufModOffset, int elements) const {
346 cast().getIndices(castBuffer<TypeB>(buf), bufOffset, castBuffer<IndexTypeB>(indices), bufModOffset, elements);
347 }
348
349 void registerValue(void* buf, size_t bufOffset, void* indices, void* oldPrimals, size_t bufModOffset, int elements) const {
350 cast().registerValue(castBuffer<TypeB>(buf), bufOffset, castBuffer<IndexTypeB>(indices), castBuffer<PrimalTypeB>(oldPrimals), bufModOffset, elements);
351 }
352
353 void clearIndices(void* buf, size_t bufOffset, int elements) const {
354 cast().clearIndices(castBuffer<TypeB>(buf), bufOffset, elements);
355 }
356
357 void createIndices(void* buf, size_t bufOffset, void* indices, size_t bufModOffset, int elements) const {
358 cast().createIndices(castBuffer<TypeB>(buf), bufOffset, castBuffer<IndexTypeB>(indices), bufModOffset, elements);
359 }
360
361 void getValues(const void* buf, size_t bufOffset, void* primals, size_t bufModOffset, int elements) const {
362 cast().getValues(castBuffer<TypeB>(buf), bufOffset, castBuffer<PrimalTypeB>(primals), bufModOffset, elements);
363 }
364
365 void performReduce(void* buf, void* target, int count, AMPI_Op op, int ranks) const {
366 cast().performReduce(castBuffer<TypeB>(buf), castBuffer<TypeB>(target), count, op, ranks);
367 }
368
369 void copy(void* from, size_t fromOffset, void* to, size_t toOffset, int count) const {
370 cast().copy(castBuffer<TypeB>(from), fromOffset, castBuffer<TypeB>(to), toOffset, count);
371 }
372
373 void initializeType(void* buf, size_t bufOffset, int elements) const {
374 cast().initializeType(castBuffer<TypeB>(buf), bufOffset, elements);
375 }
376
377 void freeType(void* buf, size_t bufOffset, int elements) const {
378 cast().freeType(castBuffer<TypeB>(buf), bufOffset, elements);
379 }
380
381 void createTypeBuffer(void* &buf, size_t size) const {
382 cast().createTypeBuffer(castBuffer<TypeB>(buf), size);
383 }
384
385 void createModifiedTypeBuffer(void* &buf, size_t size) const {
386 cast().createModifiedTypeBuffer(castBuffer<ModifiedTypeB>(buf), size);
387 }
388
389 void deleteTypeBuffer(void* &buf, size_t size) const {
390 cast().deleteTypeBuffer(castBuffer<TypeB>(buf), size);
391 }
392
393 void deleteModifiedTypeBuffer(void* &buf) const {
394 cast().deleteModifiedTypeBuffer(castBuffer<ModifiedTypeB>(buf));
395 }
396
397 private:
398
399 inline Impl& cast() {
400 return *reinterpret_cast<Impl*>(this);
401 }
402
403 inline const Impl& cast() const {
404 return *reinterpret_cast<const Impl*>(this);
405 }
406
407 template <typename T>
408 inline T*& castBuffer(void*& buf) const {
409 return reinterpret_cast<T*&>(buf);
410 }
411
412 template <typename T>
413 inline const T*& castBuffer(const void* &buf) const {
414 return reinterpret_cast<const T*&>(buf);
415 }
416 };
417}
The interface for the AD tool that is accessed by MeDiPack.
Definition adToolInterface.h:42
Definition typeInterface.hpp:324
void copyIntoModifiedBuffer(const void *buf, size_t bufOffset, void *bufMod, size_t bufModOffset, int elements) const
Copy all data into the modified buffer and perform the special handling for the AD type.
Definition typeInterface.hpp:337
void copy(void *from, size_t fromOffset, void *to, size_t toOffset, int count) const
Copy the elements of one buffer into the other.
Definition typeInterface.hpp:369
void getIndices(const void *buf, size_t bufOffset, void *indices, size_t bufModOffset, int elements) const
Get all the AD identifiers from all AD types in the buffer.
Definition typeInterface.hpp:345
ADToolB::IndexType IndexTypeB
Definition typeInterface.hpp:328
int computeActiveElements(const int count) const
Get the number of active elements that are contained in count versions of the type.
Definition typeInterface.hpp:333
void deleteTypeBuffer(void *&buf, size_t size) const
Delete the temporary buffer.
Definition typeInterface.hpp:389
void createModifiedTypeBuffer(void *&buf, size_t size) const
Create a temporary buffer of the modified type that this interface represents.
Definition typeInterface.hpp:385
void clearIndices(void *buf, size_t bufOffset, int elements) const
Clear the AD types in the buffer such that they can be overwritten.
Definition typeInterface.hpp:353
void createTypeBuffer(void *&buf, size_t size) const
Create a temporary buffer of the type that this interface represents.
Definition typeInterface.hpp:381
void initializeType(void *buf, size_t bufOffset, int elements) const
Initialize the types in the buffer.
Definition typeInterface.hpp:373
void deleteModifiedTypeBuffer(void *&buf) const
Delete the temporary buffer for the modified types.
Definition typeInterface.hpp:393
void createIndices(void *buf, size_t bufOffset, void *indices, size_t bufModOffset, int elements) const
Create indices for a reciving buffer if necessary.
Definition typeInterface.hpp:357
void getValues(const void *buf, size_t bufOffset, void *primals, size_t bufModOffset, int elements) const
Get the primal values from the AD types.
Definition typeInterface.hpp:361
void performReduce(void *buf, void *target, int count, AMPI_Op op, int ranks) const
Perform a local reduce operation.
Definition typeInterface.hpp:365
MpiTypeBase(MPI_Datatype mpiType, MPI_Datatype modifiedMpiType)
Definition typeInterface.hpp:330
void registerValue(void *buf, size_t bufOffset, void *indices, void *oldPrimals, size_t bufModOffset, int elements) const
Register all the AD values on the new machine.
Definition typeInterface.hpp:349
void copyFromModifiedBuffer(void *buf, size_t bufOffset, const void *bufMod, size_t bufModOffset, int elements) const
Copy all data from the modified buffer and perform the special handling for the AD type.
Definition typeInterface.hpp:341
ADToolB::PrimalType PrimalTypeB
Definition typeInterface.hpp:327
void freeType(void *buf, size_t bufOffset, int elements) const
Destroy the types in the buffer.
Definition typeInterface.hpp:377
Wrapper interface for MPI types in communications.
Definition typeInterface.hpp:63
void AdjointType
Definition typeInterface.hpp:93
virtual void getIndices(const void *buf, size_t bufOffset, void *indices, size_t bufModOffset, int elements) const =0
Get all the AD identifiers from all AD types in the buffer.
virtual void copyFromModifiedBuffer(void *buf, size_t bufOffset, const void *bufMod, size_t bufModOffset, int elements) const =0
Copy all data from the modified buffer and perform the special handling for the AD type.
virtual void freeType(Type *buf, size_t bufOffset, int elements) const =0
Destroy the types in the buffer.
void ModifiedType
The type of the modified buffer data which is send over the network.
Definition typeInterface.hpp:80
void Type
The type of the user data which is represented by this interface.
Definition typeInterface.hpp:73
void PrimalType
The type of the floating point values which are handled by the AD tool.
Definition typeInterface.hpp:85
virtual void copy(void *from, size_t fromOffset, void *to, size_t toOffset, int count) const =0
Copy the elements of one buffer into the other.
virtual void getValues(const void *buf, size_t bufOffset, void *primals, size_t bufModOffset, int elements) const =0
Get the primal values from the AD types.
virtual bool isModifiedBufferRequired() const =0
Tell the functions if the underlying AD tool requires new send/recv buffers or if the original buffer...
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
virtual void performReduce(void *buf, void *target, int count, AMPI_Op op, int ranks) const =0
Perform a local reduce operation.
virtual const ADToolInterface & getADTool() const =0
Get the AD tool that handled the AD specifics.
virtual MpiTypeInterface * clone() const =0
Creates a clone of the mpi type also calling MPI_Type_dub.
virtual ~MpiTypeInterface()
Virtual destructor.
Definition typeInterface.hpp:107
virtual void deleteTypeBuffer(void *&buf, size_t size) const =0
Delete the temporary buffer.
virtual void createTypeBuffer(void *&buf, size_t size) const =0
Create a temporary buffer of the type that this interface represents.
virtual void registerValue(void *buf, size_t bufOffset, void *indices, void *oldPrimals, size_t bufModOffset, int elements) const =0
Register all the AD values on the new machine.
void setMpiTypes(MPI_Datatype mpiType, MPI_Datatype modifiedMpiType)
Helper method for extending classes to set the types after the initial construction.
Definition typeInterface.hpp:116
void IndexType
The type of the identifiers for the AD tool.
Definition typeInterface.hpp:90
virtual void initializeType(Type *buf, size_t bufOffset, int elements) const =0
Initialize the types in the buffer.
virtual void createModifiedTypeBuffer(void *&buf, size_t size) const =0
Create a temporary buffer of the modified type that this interface represents.
virtual void createIndices(void *buf, size_t bufOffset, void *indices, size_t bufModOffset, int elements) const =0
Create indices for a reciving buffer if necessary.
virtual int computeActiveElements(const int count) const =0
Get the number of active elements that are contained in count versions of the type.
MpiTypeInterface(MPI_Datatype mpiType, MPI_Datatype modifiedMpiType)
Wrapper interface for MPI types in communications. See the class description for details.
Definition typeInterface.hpp:100
virtual void deleteModifiedTypeBuffer(void *&buf) const =0
Delete the temporary buffer for the modified types.
virtual void copyIntoModifiedBuffer(const void *buf, size_t bufOffset, void *bufMod, size_t bufModOffset, int elements) const =0
Copy all data into the modified buffer and perform the special handling for the AD type.
virtual void clearIndices(void *buf, size_t bufOffset, int elements) const =0
Clear the AD types in the buffer such that they can be overwritten.
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