MeDiPack  1.3.0
A Message Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
op.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 "../macros.h"
34#include "../typeDefinitions.h"
35
39namespace medi {
40
41 static void noPreAdjointOperation(void* adjoints, void* primals, int count, int dim) { MEDI_UNUSED(adjoints); MEDI_UNUSED(primals); MEDI_UNUSED(count); MEDI_UNUSED(dim); }
42 static void noPostAdjointOperation(void* adjoints, void* primals, void* rootPrimals, int count, int dim) { MEDI_UNUSED(adjoints); MEDI_UNUSED(primals); MEDI_UNUSED(rootPrimals); MEDI_UNUSED(count); MEDI_UNUSED(dim); }
43
50 struct AMPI_Op {
51
56
61
70
78
83
88
93
100 requiresPrimal(false),
101 requiresPrimalSend(false),
102 primalFunction(MPI_OP_NULL),
103 modifiedPrimalFunction(MPI_OP_NULL),
104 preAdjointOperation(noPreAdjointOperation),
105 postAdjointOperation(noPostAdjointOperation),
106 hasAdjoint(false) {}
107
129 int init(const bool requiresPrimal, const bool requiresPrimalSend, MPI_User_function* primalFunction, int primalFunctionCommute, MPI_User_function* modifiedPrimalFunction, int modifiedPrimalFunctionCommute, const PreAdjointOperation preAdjointOperation, const PostAdjointOperation postAdjointOperation) {
130 int result1 = MPI_Op_create(primalFunction, primalFunctionCommute, &this->primalFunction);
131 MPI_Op_create(modifiedPrimalFunction, modifiedPrimalFunctionCommute, &this->modifiedPrimalFunction);
132
133 this->requiresPrimal = requiresPrimal;
134 this->requiresPrimalSend = requiresPrimalSend;
135 this->preAdjointOperation = preAdjointOperation;
136 this->postAdjointOperation = postAdjointOperation;
137 this->hasAdjoint = true;
138
139 return result1;
140 }
141
155 int init(MPI_User_function* user_fn, int commute) {
156 MPI_Op op;
157
158 int result = MPI_Op_create(user_fn, commute, &op);
159 init(op);
160
161 return result;
162 }
163
176 void init(MPI_Op op) {
177 this->primalFunction = op;
178 this->requiresPrimal = false;
179 this->requiresPrimalSend = false;
180 this->modifiedPrimalFunction = MPI_SUM;
181 this->preAdjointOperation = noPreAdjointOperation;
182 this->postAdjointOperation = noPostAdjointOperation;
183 this->hasAdjoint = false;
184 }
185
186 int free() {
187 if(this->hasAdjoint) {
188 MPI_Op_free(&this->modifiedPrimalFunction);
189 }
190
191 return MPI_Op_free(&this->primalFunction);
192 }
193 };
194
195 inline bool operator ==(AMPI_Op const& a, AMPI_Op const& b) {
196 // Just check the equality for the primal function.
197 return a.primalFunction == b.primalFunction;
198 }
199
200 inline bool operator !=(AMPI_Op const& a, AMPI_Op const& b) {
201 return !(a == b);
202 }
203
204 extern const AMPI_Op AMPI_OP_NULL;
205}
#define MEDI_UNUSED(name)
Definition macros.h:108
Global namespace for MeDiPack - Message Differentiation Package.
Definition adjointInterface.hpp:37
bool operator!=(const AMPI_Request &a, const AMPI_Request &b)
Definition async.hpp:86
const AMPI_Op AMPI_OP_NULL
Definition op.cpp:35
void(* PreAdjointOperation)(void *adjoints, void *primals, int count, int dim)
Definition typeDefinitions.h:49
bool operator==(const AMPI_Request &a, const AMPI_Request &b)
Definition async.hpp:82
void(* PostAdjointOperation)(void *adjoints, void *primals, void *rootPrimals, int count, int dim)
Definition typeDefinitions.h:50
Structure for the special handling of the MPI_Op structure.
Definition op.hpp:50
PreAdjointOperation preAdjointOperation
The operation that is evaluated on each adjoint value before they are send in a message.
Definition op.hpp:82
int free()
Definition op.hpp:186
PostAdjointOperation postAdjointOperation
The operation that is evaluated on each adjoint after the values have been received in a message.
Definition op.hpp:87
int init(const bool requiresPrimal, const bool requiresPrimalSend, MPI_User_function *primalFunction, int primalFunctionCommute, MPI_User_function *modifiedPrimalFunction, int modifiedPrimalFunctionCommute, const PreAdjointOperation preAdjointOperation, const PostAdjointOperation postAdjointOperation)
Creates an operator with a specialized adjoint handling.
Definition op.hpp:129
AMPI_Op()
Default constructor for static initialization.
Definition op.hpp:99
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
bool requiresPrimalSend
Indicates if the primal on the receiving side needs to be send to the sending side.
Definition op.hpp:60
bool hasAdjoint
Indicates if the user has provided a specialized adjoint handling for the operator.
Definition op.hpp:92
int init(MPI_User_function *user_fn, int commute)
Creates an operator that has no special adjoint handling.
Definition op.hpp:155
void init(MPI_Op op)
Creates an operator that has no special adjoint handling.
Definition op.hpp:176
bool requiresPrimal
Indicates if the primal on the sending and receiving side are required by this operator.
Definition op.hpp:55
MPI_Op modifiedPrimalFunction
The mpi operator for the modified AD types. This are the operations that are evaluated during the mpi...
Definition op.hpp:77