Neko  0.8.99
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, 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 
40 #include <stdlib.h>
41 #include <stdio.h>
42 #include <mpi.h>
43 #include <comm/comm.h>
44 
45 #include "device_mpi_op.h"
46 
47 void device_mpi_allreduce(void *buf_d, void *buf, int count, int nbytes, int op) {
48 
49  if (nbytes == sizeof(float)) {
50  if (op == DEVICE_MPI_SUM)
51  MPI_Allreduce(buf_d, buf, count, MPI_FLOAT, MPI_SUM, NEKO_COMM);
52  else if (op == DEVICE_MPI_MAX)
53  MPI_Allreduce(buf_d, buf, count, MPI_FLOAT, MPI_MAX, NEKO_COMM);
54  else {
55  fprintf(stderr, __FILE__ ": Invalid reduction op)\n");
56  exit(1);
57  }
58  }
59  else if (nbytes == sizeof(double)) {
60  if (op == DEVICE_MPI_SUM)
61  MPI_Allreduce(buf_d, buf, count, MPI_DOUBLE, MPI_SUM, NEKO_COMM);
62  else if (op == DEVICE_MPI_MAX)
63  MPI_Allreduce(buf_d, buf, count, MPI_DOUBLE, MPI_MAX, NEKO_COMM);
64  else {
65  fprintf(stderr, __FILE__ ": Invalid reduction op)\n");
66  exit(1);
67  }
68  }
69  else {
70  fprintf(stderr, __FILE__ ": Invalid data type)\n");
71  exit(1);
72  }
73 }
74 
75 void device_mpi_allreduce_inplace(void *buf_d, int count, int nbytes, int op) {
76 
77  if (nbytes == sizeof(float)) {
78  if (op == DEVICE_MPI_SUM)
79  MPI_Allreduce(MPI_IN_PLACE, buf_d, count,
80  MPI_FLOAT, MPI_SUM, NEKO_COMM);
81  else if (op == DEVICE_MPI_MAX)
82  MPI_Allreduce(MPI_IN_PLACE, buf_d, count,
83  MPI_FLOAT, MPI_MAX, NEKO_COMM);
84  else {
85  fprintf(stderr, __FILE__ ": Invalid reduction op)\n");
86  exit(1);
87  }
88  }
89  else if (nbytes == sizeof(double)) {
90  if (op == DEVICE_MPI_SUM)
91  MPI_Allreduce(MPI_IN_PLACE, buf_d, count,
92  MPI_DOUBLE, MPI_SUM, NEKO_COMM);
93  else if (op == DEVICE_MPI_MAX)
94  MPI_Allreduce(MPI_IN_PLACE, buf_d, count,
95  MPI_DOUBLE, MPI_MAX, NEKO_COMM);
96  }
97  else {
98  fprintf(stderr, __FILE__ ": Invalid data type)\n");
99  exit(1);
100  }
101 }
102 
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