'Each process has a non-empty array of values. Find the minimum global value, the rank of the process that holds it and its index on this process
I have written this code from various sources. However, it still doesn't run and throws errors. I'm just a beginner, what am I doing wrong?
/* Minimum Global Value, Rank of the process */
#define LEN 1000
#include <stdio.h>
#include <omp.h>
#include <mpi.h>
#include <stdlib.h>
#include <assert.h>
int MPI_Reduce(const void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm);
int main(int argc, char *argv[]){
float val[LEN];
int count;
int myrank, minrank, minindex, i;
float minval;
struct {
float value;
int index;
} in, out;
/* Local MINLOC */
in.value = val[0];
in.index = 0;
for(i=1; i<count; i++) {
if(in.value > val[i]) {
in.value = val[i];
in.index = i;
}
}
/* Global MINLOC */
MPI_Init( &argc, &argv );
MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
in.index = myrank * LEN + in.index;
MPI_Reduce(in, out, 1, MPI_FLOAT_INT, MPI_MINLOC, root, comm);
if(myrank == root) {
minval = out.value;
minrank = out.index / LEN;
minindex = out.index % LEN;
}
MPI_Finalize();
return 0;
}
It throws various errors regarding the root, the structs and the MPI_Reduce arguments
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
