#include #include #include #include int main(int argc, char* argv[]) { int pid, nprocs; int N,root; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &pid); MPI_Comm_size(MPI_COMM_WORLD, &nprocs); root= atoi(argv[2]); N=atoi(argv[1]); int sendcounts[nprocs]; int displs[nprocs]; int T[N]; if (pid == root) { for(int i = 0; i < N; i++) T[i] = rand() % 50; for(int i = 0; i < N; i++) printf("%d ", T[i]); printf("\n-----------------------------\n"); int d = 0; for (int i = 0; i < nprocs; i++) { sendcounts[i] = N / nprocs; if (i < N % nprocs) {sendcounts[i]++;} displs[i] = d; d += sendcounts[i]; } for(int i = 0; i < nprocs; i++) printf("displs %d ", displs[i]); printf("\n-----------------------------\n"); for(int i = 0; i < nprocs; i++) printf(" sendcount %d ", sendcounts[i]); printf("\n-----------------------------\n"); } int local_n = sendcounts[pid]; int local_T[local_n]; printf("%d ", sendcounts[1]); printf("\n-----------------------------\n"); // MPI_Scatter( T, local_n, MPI_INT, local_T, local_n, MPI_INT, 0, MPI_COMM_WORLD); MPI_Scatterv(T, sendcounts, displs, MPI_INT, local_T, local_n, MPI_INT, root, MPI_COMM_WORLD); printf("processus % d va traiter %d ", pid,local_n); // for(int i = 0; i < sendcounts[pid]; i++) { // printf("\n-----------------------------\n"); // printf("pid % d: %d ", pid,local_T[i]); //} int local_max=local_T[0]; for(int i = 1; i < local_n; i++) { if(local_max <= local_T[i]){ local_max=local_T[i]; } } printf("pid %d : %d\n", pid, local_max); int max ; MPI_Reduce(&local_max, &max, 1, MPI_INT, MPI_MAX, root, MPI_COMM_WORLD); if (pid == root) { printf("Le max: %d\n", max); } MPI_Finalize(); return 0; }