! arrayMPIF.c ! Run this with 2 processors. Processor 1 creates an array of 'count' random ! numbers. In this example 'count' is 100. Processor 0 (the root process) ! receives this array from processor 1. ! Make sure you are understanding what each parameter in the MPI calls represent. program main include "mpif.h" integer MAXVALS, NUMSPERLINE parameter (MAXVALS=100,NUMSPERLINE=10) integer rank, size, count, data(MAXVALS) real randomarray(5) integer ierr, status(MPI_STATUS_SIZE) call MPI_INIT(ierr) call MPI_COMM_RANK(MPI_COMM_WORLD, rank, ierr) call MPI_COMM_SIZE(MPI_COMM_WORLD, size, ierr) call random_seed() call random_seed() count = 100 if (rank .eq. 0) then call MPI_RECV(data, 100, MPI_DOUBLE, 1, 10, & MPI_COMM_WORLD, status, ierr); print *, 'Data received from process 1:' ! THIS DO LOOP PRINTS ONE ARRAY ELEMENT PER LINE ! do i = 1, count ! print *, data(i) ! end do ! THIS VERSION USES AN 'IMPLIED DO LOOP' IN THE WRITE STATEMENT TO PRINT ! 10 NUMBERS PER LINE. LET ME KNOW IF YOU FIND AN EASIER WAY TO DO THIS. do i = 1, count/NUMSPERLINE ! Make count divisible by NUMSPERLINE write(*,"(10I7)") & (data(j), j=(i-1)*NUMSPERLINE+1, (i-1)*NUMSPERLINE+NUMSPERLINE) end do else if (rank .eq. 1) then do i = 1, count call random_number(randomarray) data(i) = int(randomarray(1)*1000) end do call MPI_SEND(data, count, MPI_DOUBLE, 0, 10, & MPI_COMM_WORLD, ierr); end if end if call MPI_FINALIZE(); end program ! CRAY SV1: ! f90 arrayMPIF.f90 ! mpirun -np 2 a.out ! Data received from process 1: ! 950 275 831 83 212 18 584 871 464 763 ! 210 926 93 183 722 422 576 589 804 702 ! 485 854 184 812 362 764 916 957 911 542 ! 47 529 185 955 589 190 850 180 3 104 ! 66 404 557 937 447 954 34 205 60 316 ! 827 778 415 653 906 392 331 939 258 522 ! 635 366 154 324 871 765 997 134 952 39 ! 951 489 473 383 516 166 972 919 269 620 ! 568 508 191 507 378 341 192 997 948 523 ! 18 234 372 587 32 688 153 625 23 136