Untitled
unknown
c_cpp
2 years ago
1.2 kB
3
Indexable
#include "mpi.h" #include <stdio.h> #include <stdlib.h> #define N 8 int f(int number){ return number*2; } int main(int argc, char* argv[]) { int rank, numprocs; int A[N] = {1,4,5,7,3,2,9,8}; int B[8],C[8]; MPI_Status status; MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &numprocs); MPI_Comm_rank(MPI_COMM_WORLD, &rank); if(rank == 0 && N % numprocs != 0) MPI_Abort(MPI_COMM_WORLD, 0); int size = N / numprocs; MPI_Scatter(A, size, MPI_INT, B, size, MPI_INT, 0, MPI_COMM_WORLD); int start = size*rank; for(int i = start; i < start + size ; i++) { B[i] = f(A[i]); printf("B[i] = %d\n", B[i]); } MPI_Gather(B, size, MPI_INT, B+start , size, MPI_INT, 0, MPI_COMM_WORLD); if(rank==0) { /* for(int i=0; i < numprocs; i++) { printf("here \n"); MPI_Recv(B + i*size, size, MPI_INT, MPI_ANY_SOURCE, 1, MPI_COMM_WORLD, &status); printf("after \n"); } */ printf("BEFORE LOOP \n"); for(int i=0; i<N; i++){ if(B[i] > B[i-1]) printf("B[%d] = %d\n" , i, B[i]); } } // else // { // //MPI_Send(B, size, MPI_INT, 0, 1, MPI_COMM_WORLD); // // MPI_Gather(B, size, MPI_INT, NULL, 0, MPI_INT, 0, MPI_COMM_WORLD); // } MPI_Finalize(); return 0; }
Editor is loading...