MPI_Reduce
MPI_METHOD
MPI_Reduce(
_In_range_(!=, recvbuf) _In_opt_ const void* sendbuf,
_When_(root != MPI_PROC_NULL, _Out_opt_) void* recvbuf,
_In_range_(>=, 0) int count,
_In_ MPI_Datatype datatype,
_In_ MPI_Op op,
_mpi_coll_rank_(root) int root,
_In_ MPI_Comm comm
);
- sendbuf:包含要发送到根进程的数据的缓冲区的句柄。
- recvbuf:用于接收缩减操作结果的缓冲区的句柄。 此参数仅在根进程中非常重要。
- count;计数从此过程发送的元素数。
- datatype:缓冲区中每个元素的数据类型。 此参数必须与 操作 参数中指定的操作兼容。
op:要执行的全局缩减操作。句柄可以指示内置或应用程序定义的操作。
- MPI_MAX:最大值
- MPI_MIN:最小值
- MPI_SUM:求和
- MPI_PROD:求积
- MPI_LAND:逻辑与
- MPI_BAND:按位与
- MPI_LOR:逻辑或
- MPI_BOR:按位或
- MPI_LXOR:逻辑异或
- MPI_BXOR:按位异或
- MPI_MAXLOC:最大值且相应位置
- MPI_MINLOC:最小值且相应位置
- root:指定通信器内的接收进程的排名。
- comm:通讯,MPI_Comm通信器句柄。
#include "mpi.h"
#include "stdio.h"
#include "stdlib.h"
#include "time.h"
#define num 6
int main(int argc, char **argv){
MPI_Init(&argc, &argv);
int process_id;
MPI_Comm_rank(MPI_COMM_WORLD, &process_id);
int process_num;
MPI_Comm_size(MPI_COMM_WORLD, &process_num);
srand(process_id * time(NULL));
int data[num];
for (int i = 0; i < num; ++i) {
data[i] = rand() % 100;
}
printf("process %d got data : ", process_id);
for (int i = 0; i < num; ++i) {
printf("%d ", data[i]);
}
int recv_data[num];
MPI_Reduce(&data, recv_data, num, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
if (process_id == 0){
for (int i = 0; i < num; ++i) {
printf("%d ", recv_data[i]);
}
}
MPI_Finalize();
}
"C:\Program Files\Microsoft MPI\Bin\mpiexec.exe" -np 4 ./MPI_Reduce_data
process 3 got data : 22 32 55 38 83 17
process 1 got data : 33 90 65 26 53 26
process 2 got data : 27 61 60 48 84 87
process 0 got data : 38 19 38 37 55 97 120 202 218 149 275 227
Process finished with exit code 0
MPI_Allreduce
MPI_METHOD
MPI_Allreduce(
_In_range_(!=, recvbuf) _In_opt_ const void* sendbuf,
_Out_opt_ void* recvbuf,
_In_range_(>=, 0) int count,
_In_ MPI_Datatype datatype,
_In_ MPI_Op op,
_In_ MPI_Comm comm
);
- sendbuf:指向要发送到组中所有进程的数据的指针。 缓冲区中元素的数量和数据类型在计数和数据类型参数中指定。
- recvbuf:指向缓冲区的指针,用于接收缩减操作的结果。 此参数仅在根进程中非常重要。
- count:要从此过程发送的元素数。
- datatype:数据类型,缓冲区中每个元素的MPI_Datatype 。 此参数必须与操作参数中指定的操作兼容。
- op:指示要执行的全局缩减操作 的MPI_Op 句柄。 句柄可以指示内置或应用程序定义的操作。
- comm:MPI_Comm通信器句柄。
//
// Created by ronie on 2022/11/25.
//
#include "mpi.h"
#include "stdio.h"
#include "stdlib.h"
#include "time.h"
#define num 6
int main(int argc, char **argv){
MPI_Init(&argc, &argv);
int process_id;
MPI_Comm_rank(MPI_COMM_WORLD, &process_id);
int process_num;
MPI_Comm_size(MPI_COMM_WORLD, &process_num);
srand(process_id * time(NULL));
int data[num];
for (int i = 0; i < num; ++i) {
data[i] = rand() % 100;
}
printf("process %d got data : ", process_id);
for (int i = 0; i < num; ++i) {
printf("%d ", data[i]);
}
int recv_data[num];
MPI_Allreduce(&data, recv_data, num, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
printf("\nprocess %d recv data : ", process_id);
for (int i = 0; i < num; ++i) {
printf("%d ", recv_data[i]);
}
MPI_Finalize();
}
"C:\Program Files\Microsoft MPI\Bin\mpiexec.exe" -np 4 ./MPI_Allreduce_data
process 2 got data : 64 14 79 11 82 24
process 2 recv data : 130 128 275 70 200 200
process 0 got data : 38 19 38 37 55 97
process 0 recv data : 130 128 275 70 200 200
process 1 got data : 1 50 74 8 18 60
process 1 recv data : 130 128 275 70 200 200
process 3 got data : 27 45 84 14 45 19
process 3 recv data : 130 128 275 70 200 200
Process finished with exit code 0
Comments | NOTHING