Neko  0.9.0
A portable framework for high-order spectral element flow simulations
device_mpi_reduce.c
Go to the documentation of this file.
1 /*
2  Copyright (c) 2022-2024, The Neko Authors
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions
7  are met:
8 
9  * Redistributions of source code must retain the above copyright
10  notice, this list of conditions and the following disclaimer.
11 
12  * Redistributions in binary form must reproduce the above
13  copyright notice, this list of conditions and the following
14  disclaimer in the documentation and/or other materials provided
15  with the distribution.
16 
17  * Neither the name of the authors nor the names of its
18  contributors may be used to endorse or promote products derived
19  from this software without specific prior written permission.
20 
21  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  POSSIBILITY OF SUCH DAMAGE.
33 */
34 
39 #include <stdlib.h>
40 #include <stdio.h>
41 #include <mpi.h>
42 #include <comm/comm.h>
43 
44 #include "device_mpi_op.h"
45 
46 void device_mpi_allreduce(void *buf_d, void *buf, int count, int nbytes, int op) {
47 
48  if (nbytes == sizeof(float)) {
49  if (op == DEVICE_MPI_SUM)
50  MPI_Allreduce(buf_d, buf, count, MPI_FLOAT, MPI_SUM, NEKO_COMM);
51  else if (op == DEVICE_MPI_MAX)
52  MPI_Allreduce(buf_d, buf, count, MPI_FLOAT, MPI_MAX, NEKO_COMM);
53  else {
54  fprintf(stderr, __FILE__ ": Invalid reduction op)\n");
55  exit(1);
56  }
57  }
58  else if (nbytes == sizeof(double)) {
59  if (op == DEVICE_MPI_SUM)
60  MPI_Allreduce(buf_d, buf, count, MPI_DOUBLE, MPI_SUM, NEKO_COMM);
61  else if (op == DEVICE_MPI_MAX)
62  MPI_Allreduce(buf_d, buf, count, MPI_DOUBLE, MPI_MAX, NEKO_COMM);
63  else {
64  fprintf(stderr, __FILE__ ": Invalid reduction op)\n");
65  exit(1);
66  }
67  }
68  else {
69  fprintf(stderr, __FILE__ ": Invalid data type)\n");
70  exit(1);
71  }
72 }
73 
74 void device_mpi_allreduce_inplace(void *buf_d, int count, int nbytes, int op) {
75 
76  if (nbytes == sizeof(float)) {
77  if (op == DEVICE_MPI_SUM)
78  MPI_Allreduce(MPI_IN_PLACE, buf_d, count,
79  MPI_FLOAT, MPI_SUM, NEKO_COMM);
80  else if (op == DEVICE_MPI_MAX)
81  MPI_Allreduce(MPI_IN_PLACE, buf_d, count,
82  MPI_FLOAT, MPI_MAX, NEKO_COMM);
83  else {
84  fprintf(stderr, __FILE__ ": Invalid reduction op)\n");
85  exit(1);
86  }
87  }
88  else if (nbytes == sizeof(double)) {
89  if (op == DEVICE_MPI_SUM)
90  MPI_Allreduce(MPI_IN_PLACE, buf_d, count,
91  MPI_DOUBLE, MPI_SUM, NEKO_COMM);
92  else if (op == DEVICE_MPI_MAX)
93  MPI_Allreduce(MPI_IN_PLACE, buf_d, count,
94  MPI_DOUBLE, MPI_MAX, NEKO_COMM);
95  }
96  else {
97  fprintf(stderr, __FILE__ ": Invalid data type)\n");
98  exit(1);
99  }
100 }
101 
MPI_Comm NEKO_COMM
Definition: comm_wrapper.c:10
#define DEVICE_MPI_MAX
Definition: device_mpi_op.h:10
#define DEVICE_MPI_SUM
Definition: device_mpi_op.h:9
void device_mpi_allreduce(void *buf_d, void *buf, int count, int nbytes, int op)
void device_mpi_allreduce_inplace(void *buf_d, int count, int nbytes, int op)
real * buf
Definition: pipecg_aux.cu:42