SlideShare a Scribd company logo
Parallel Programming using
                            MPI
                Collective Communications
                                      Claudio Gheller
                                         CINECA
                                    c.gheller@cineca.it




Collective Communications




   Collective Communications

     !"#$$%&'()*'#&+,'&-#.-'&/,),/0#%1,#2,10#(3++
     !")..34,56,).. 10#(3++3+,'&,),(#$$%&'()*#0
   Barrier Synchronization
   Broadcast
   Gather/Scatter
   Reduction (sum, max, prod, … )




                                                          2




                                                              1
Collective Communications




     Characteristics



     All processes must call the collective routine
     No non-blocking collective communication
     No tags



                  7)23+*,)&4,$#+*,322'('3&*,
                    (#$$%&'()*'#&,$#43



                                                                         3




Collective Communications



    MPI_Barrier
    Stop processes until all processes within a communicator reach the
       barrier

    Fortran:
    CALL MPI_BARRIER(comm, ierr)


    C:
    int MPI_Barrier(MPI_Comm comm)




                                                                         4




                                                                             2
Collective Communications



  Barrier
                        *:                                  *;                                  *<
                         8;
              89
                                   8=
                   8:         8<                  89   8:    8;   8<   8=




                                        barrier                             barrier
                                                                                      89   8:   8;   8<   8=




         *



                                                                                                               5




Collective Communications

               Broadcast (MPI_BCAST)
    One-to-all communication: same data sent from root process to all others in the
       communicator

    Fortran:
             INTEGER count, type, root, comm, ierr
             CALL MPI_BCAST(buf, count, type, root, comm, ierr)
             Buf array of type type


    C:
             int MPI_Bcast(void *buf, int count, MPI_Datatype, datatypem int root,
                MPI_Comm comm)


    All processes must specify same root, rank and comm




                                                                                                               6




                                                                                                                   3
Collective Communications

              Broadcast
 PROGRAM broad_cast
  INCLUDE 'mpif.h'
  INTEGER ierr, myid, nproc, root
  INTEGER status(MPI_STATUS_SIZE)
  REAL A(2)                                                                                        89
  CALL MPI_INIT(ierr)
  CALL MPI_COMM_SIZE(MPI_COMM_WORLD, nproc, ierr)
  CALL MPI_COMM_RANK(MPI_COMM_WORLD, myid, ierr)                                            ):
  root = 0                                                                                         8:
  IF( myid .EQ. 0 ) THEN                         89                                         ):
    a(1) = 2.0
    a(2) = 4.0                                                                              ):
  END IF                                                                                           8;
  CALL MPI_BCAST(a, 2, MPI_REAL, 0,
                                                                                            ):
                 MPI_COMM_WORLD, ierr)
  WRITE(6,*) myid, ': a(1)=', a(1), 'a(2)=', a(2)
  CALL MPI_FINALIZE(ierr)                                                                           8<
  END




                                                                                                              7




Collective Communications

                          Scatter / Gather



     7()**30                                           >)*?30

    sndbuf                                             sndbuf       sndbuf       sndbuf           sndbuf

   89
    89        ):    );   )<   )=                       89
                                                        89   ):     8:
                                                                     8:    );        8;
                                                                                      8;    )<    8<
                                                                                                   8<    )=




   89    ):        8:    );        8;   )<   8<   )=
                                                                  89
                                                                   89      ):   );     )<    )=
  rcvbuf           rcvbuf      rcvbuf        rcvbuf
                                                                  rcvbuf




                                                                                                              8




                                                                                                                  4
Collective Communications



   MPI_Scatter
   One-to-all communication: different data sent from root process to all others in the
      communicator



   Fortran:
        CALL MPI_SCATTER(sndbuf, sndcount, sndtype, rcvbuf, rcvcount, rcvtype,
           root, comm, ierr)


   Arguments definition are like other MPI subroutine
   sndcount is the number of elements sent to each process, not the size of sndbuf, that
      should be sndcount times the number of process in the communicator
   The sender arguments are meaningful only for root




                                                                                           9




Collective Communications




                                                                                           10




                                                                                                5
Collective Communications


   MPI_SCATTERV
   Usage
       –   int MPI_Scatterv( void* sendbuf,               /* in */
                             int* sendcounts,             /* in */
                             int* displs,                 /* in */
                             MPI_Datatype sendtype,       /* in */
                             void* recvbuf,               /* in */
                             int recvcount,               /* in */
                             MPI_Datatype recvtype,       /* in */
                             int root,                               /* in */
                             MPI_Comm comm);              /* in */
   Description
       –   Distributes individual messages from root to each process in communicator
       –   Messages can have different sizes and displacements




                                                                                       11




Collective Communications




                                                                                       12




                                                                                            6
Collective Communications



   MPI_Gather
   One-to-all communication: different data collected by the root process, from all others
      processes in the communicator. Is the opposite of Scatter
   Fortran:
   CALL MPI_GATHER(sndbuf, sndcount, sndtype, rcvbuf, rcvcount,
      rcvtype, root, comm, ierr)




   Arguments definition are like other MPI subroutine
   rcvcount is the number of elements collected from each process, not the size of rcvbuf,
       that should be rcvcount times the number of process in the communicator
   The receiver arguments are meaningful only for root




                                                                                             13




Collective Communications




                                                                                             14




                                                                                                  7
Collective Communications


   MPI_GATHERV
   Usage
       –   int MPI_Gatherv( void* sendbuf,                /* in */
                          int sendcount,                  /* in */
                          MPI_Datatype sendtype,          /* in */
                          void* recvbuf,                  /* out */
                          int* recvcount,                 /* in */
                          int* displs,                                /* in */
                          MPI_Datatype recvtype,          /* in */
                          int root,                       /* in */
                          MPI_Comm comm );                /* in */
   Description
       –   Collects individual messages from each process in communicator to the root process and
           store them in rank order
       –   Messages can have different sizes and displacements




                                                                                                    15




Collective Communications




                                                                                                    16




                                                                                                         8
Collective Communications

  Scatter example
    PROGRAM scatter
    INCLUDE 'mpif.h'
    INTEGER ierr, myid, nproc, nsnd, I, root
    INTEGER status(MPI_STATUS_SIZE)
    REAL A(16), B(2)
    CALL MPI_INIT(ierr)
    CALL MPI_COMM_SIZE(MPI_COMM_WORLD, nproc, ierr)
    CALL MPI_COMM_RANK(MPI_COMM_WORLD, myid, ierr)
    root = 0
    IF( myid .eq. root ) THEN
      DO i = 1, 16
        a(i) = REAL(i)
      END DO
    END IF
    nsnd = 2
    CALL MPI_SCATTER(a, nsnd, MPI_REAL, b, nsnd,
   & MPI_REAL, root, MPI_COMM_WORLD, ierr)
    WRITE(6,*) myid, ': b(1)=', b(1), 'b(2)=', b(2)
    CALL MPI_FINALIZE(ierr)


                                                        17




Collective Communications
    Gather example
      PROGRAM gather
      INCLUDE 'mpif.h'
      INTEGER ierr, myid, nproc, nsnd, I, root
      INTEGER status(MPI_STATUS_SIZE)
      REAL A(16), B(2)
      CALL MPI_INIT(ierr)
      CALL MPI_COMM_SIZE(MPI_COMM_WORLD, nproc, ierr)
      CALL MPI_COMM_RANK(MPI_COMM_WORLD, myid, ierr)
      root = 0
      b(1) = REAL( myid )
      b(2) = REAL( myid )
      nsnd = 2
      CALL MPI_GATHER(b, nsnd, MPI_REAL, a, nsnd,
     & MPI_REAL, root MPI_COMM_WORLD, ierr)
      IF( myid .eq. root ) THEN
        DO i = 1, (nsnd*nproc)
          WRITE(6,*) myid, ': a(i)=', a(i)
        END DO
      END IF
      CALL MPI_FINALIZE(ierr)

                                                        18




                                                             9
Collective Communications

                    MPI_Alltoall
            @#0*0)&A
            CALL MPI_ALLTOALL(sndbuf, sndcount, sndtype, rcvbuf, rcvcount,
            rcvtype, comm, ierr)


               89      ):   );   )<   )=              89   ):   5:   (:   4:




               89                                     89




                                                                               rcvbuf
   sndbuf




                       5:   5;   5<   5=                   );   5;   (;   4;




               89      (:   (;   (<   (=              89   )<   5<   (<   4<




               89      4:   4;   4<   4=              89   )=   5=   (=   4=




                B306,%+32%.,*#,'$1.3$3&*,4)*),*0)&+1#+'*'#&

                                                                                 19




Collective Communications



     Reduction
     The reduction operation allow to:
     •       Collect data from each process
     •       Reduce the data to a single value
     •       Store the result on the root processes
     •       Store the result on all processes
     •       Overlap of communication and computing




                                                                                 20




                                                                                        10
Collective Communications

               Reduce, Parallel Sum

          89    ):   5:

                                                                           89    7) 75
          8:    );   5;
                                                                            8:    7) 75
                                         7)C):D);D)<D)=
          8;    )<   5<                  75C5:D5;D5<D5=
                                                                            8;    7) 75

          8<    )=    5=                                                   8<    7) 75


                     E34%(*'#&,2%&(*'#&,F#0G+,F'*?,)00)6+
                     #*?30,#130)*'#&A,10#4%(*H,$'&H,$)IH,)&4H,JK,




                                                                                          21




Collective Communications



    MPI_REDUCE and MPI_ALLREDUCE
    Fortran:
    MPI_REDUCE( snd_buf, rcv_buf, count, type, op, root, comm, ierr)

    snd_buf      input array of type type containing local values.
    rcv_buf      output array of type type containing global results
    Count       (INTEGER) number of element of snd_buf and rcv_buf
    type        (INTEGER) MPI type of snd_buf and rcv_buf
    op          (INTEGER) parallel operation to be performed
    root        (INTEGER) MPI id of the process storing the result
    comm        (INTEGER) communicator of processes involved in the operation
    ierr        (INTEGER) output, error code (if ierr=0 no error occours)



    MPI_ALLREDUCE( snd_buf, rcv_buf, count, type, op, comm, ierr)

    The argument root is missing, the result is stored to all processes.




                                                                                          22




                                                                                               11
Collective Communications


         Predefined Reduction Operations
                   MPI op           Function

                   MPI_MAX          Maximum

                   MPI_MIN          Minimum

                   MPI_SUM          Sum

                   MPI_PROD         Product

                   MPI_LAND         Logical AND

                   MPI_BAND         Bitwise AND

                   MPI_LOR          Logical OR

                   MPI_BOR          Bitwise OR

                   MPI_LXOR         Logical exclusive OR

                   MPI_BXOR         Bitwise exclusive OR

                   MPI_MAXLOC       Maximum and location

                   MPI_MINLOC       Minimum and location




                                                                     23




Collective Communications

    Reduce / 1

    C:
         int MPI_Reduce(void * snd_buf, void * rcv_buf, int count,
         MPI_Datatype type, MPI_Op op, int root, MPI_Comm comm)

         int MPI_Allreduce(void * snd_buf, void * rcv_buf, int
         count, MPI_Datatype type, MPI_Op op, MPI_Comm comm)




                                                                     24




                                                                          12
Collective Communications

       Reduce, example
       PROGRAM reduce
         INCLUDE 'mpif.h'
         INTEGER ierr, myid, nproc, root
         INTEGER status(MPI_STATUS_SIZE)
         REAL A(2), res(2)
         CALL MPI_INIT(ierr)
         CALL MPI_COMM_SIZE(MPI_COMM_WORLD, nproc, ierr)
         CALL MPI_COMM_RANK(MPI_COMM_WORLD, myid, ierr)
         root = 0
         a(1) = 2.0*myid
         a(2) = 4.0+myid
         CALL MPI_REDUCE(a, res, 2, MPI_REAL, MPI_SUM, root,
        & MPI_COMM_WORLD, ierr)
         IF( myid .EQ. 0 ) THEN
           WRITE(6,*) myid, ': res(1)=', res(1), 'res(2)=', res(2)
         END IF
         CALL MPI_FINALIZE(ierr)
         END


                                                                     25




                                                                          13

More Related Content

PPTX
Bigdata Presentation
PDF
How to Leverage Go for Your Networking Needs
PDF
Golang concurrency design
PDF
gRPC in Go
PDF
Fast and cost effective geospatial analysis pipeline with AWS lambda
PDF
Concurrency with Go
PDF
Good news, everybody! Guile 2.2 performance notes (FOSDEM 2016)
PDF
Boost.Python - domesticating the snake
Bigdata Presentation
How to Leverage Go for Your Networking Needs
Golang concurrency design
gRPC in Go
Fast and cost effective geospatial analysis pipeline with AWS lambda
Concurrency with Go
Good news, everybody! Guile 2.2 performance notes (FOSDEM 2016)
Boost.Python - domesticating the snake

What's hot (19)

PDF
Networking and Go: An Epic Journey
PDF
MessagePack(msgpack): Compact and Fast Serialization Library
PDF
Go concurrency
PDF
Concurrency in Golang
PDF
FOSDEM 2020: Querying over millions and billions of metrics with M3DB's index
PDF
Machine Learning on Code - SF meetup
PDF
Tuga IT 2017 - Redis
PDF
Dafunctor
PDF
FOSDEM 2019: M3, Prometheus and Graphite with metrics and monitoring in an in...
PDF
Golang design4concurrency
PDF
最近作ったN個のCPANモジュール Yokohama.pm #10
PDF
ODP
LCDS - State Presentation
ODP
libpcap
PDF
ゼロから作るパケット転送用OS (Internet Week 2014)
PPTX
Lua: the world's most infuriating language
PDF
Go on!
PDF
DConf 2016: Keynote by Walter Bright
PDF
Specializing the Data Path - Hooking into the Linux Network Stack
Networking and Go: An Epic Journey
MessagePack(msgpack): Compact and Fast Serialization Library
Go concurrency
Concurrency in Golang
FOSDEM 2020: Querying over millions and billions of metrics with M3DB's index
Machine Learning on Code - SF meetup
Tuga IT 2017 - Redis
Dafunctor
FOSDEM 2019: M3, Prometheus and Graphite with metrics and monitoring in an in...
Golang design4concurrency
最近作ったN個のCPANモジュール Yokohama.pm #10
LCDS - State Presentation
libpcap
ゼロから作るパケット転送用OS (Internet Week 2014)
Lua: the world's most infuriating language
Go on!
DConf 2016: Keynote by Walter Bright
Specializing the Data Path - Hooking into the Linux Network Stack
Ad

Viewers also liked (20)

KEY
Using MPI
PPT
MPI Introduction
PPT
PPTX
MPI message passing interface
PPT
Introduction to MPI
PDF
Converged solutions for HPC and Big Data Analytics using Clusters and Clouds
PPT
Amax Gpu Hpc
PDF
PPT
Message passing interface
PDF
Introduction to Linux #1
PPT
Open MPI 2
PDF
High Performance Computing using MPI
PPTX
The Business of Social Media
PDF
The hottest analysis tools for startups
PPTX
10 Steps of Project Management in Digital Agencies
PDF
Lost in Cultural Translation
PDF
PPTX
What is Big Data?
PPTX
Big data ppt
PPTX
All About Beer
Using MPI
MPI Introduction
MPI message passing interface
Introduction to MPI
Converged solutions for HPC and Big Data Analytics using Clusters and Clouds
Amax Gpu Hpc
Message passing interface
Introduction to Linux #1
Open MPI 2
High Performance Computing using MPI
The Business of Social Media
The hottest analysis tools for startups
10 Steps of Project Management in Digital Agencies
Lost in Cultural Translation
What is Big Data?
Big data ppt
All About Beer
Ad

Similar to Parallel programming using MPI (20)

PPTX
Programming using MPI and OpenMP
PPT
Collective Communications in MPI
PDF
Operating Systems - Distributed Parallel Computing
PDF
More mpi4py
PDF
XMOS XS1 and XC
PDF
Nov 03 MS
PDF
MPI Presentation
PPT
Open MPI
PPT
Parallel computing(2)
PDF
mpi4py.pdf
PDF
Move Message Passing Interface Applications to the Next Level
PPTX
Introduction to MPI Basics easy way.pptx
PDF
Nov 05 MS1
PDF
June 03 MS1
PDF
Parallel and Distributed Computing Chapter 10
PDF
June 09 MS1
PPT
Lecture9
PDF
Lecture-4_Process Management.pdf
PDF
Non-blocking Communications In High Performance Computing.pdf
Programming using MPI and OpenMP
Collective Communications in MPI
Operating Systems - Distributed Parallel Computing
More mpi4py
XMOS XS1 and XC
Nov 03 MS
MPI Presentation
Open MPI
Parallel computing(2)
mpi4py.pdf
Move Message Passing Interface Applications to the Next Level
Introduction to MPI Basics easy way.pptx
Nov 05 MS1
June 03 MS1
Parallel and Distributed Computing Chapter 10
June 09 MS1
Lecture9
Lecture-4_Process Management.pdf
Non-blocking Communications In High Performance Computing.pdf

More from Majong DevJfu (20)

PDF
9 - Architetture Software - SOA Cloud
PDF
8 - Architetture Software - Architecture centric processes
PDF
7 - Architetture Software - Software product line
PDF
6 - Architetture Software - Model transformation
PDF
5 - Architetture Software - Metamodelling and the Model Driven Architecture
PDF
4 - Architetture Software - Architecture Portfolio
PDF
3 - Architetture Software - Architectural styles
PDF
2 - Architetture Software - Software architecture
PDF
1 - Architetture Software - Software as a product
PDF
10 - Architetture Software - More architectural styles
PDF
PDF
PDF
4 (uml basic)
POT
Tmd template-sand
PPT
26 standards
9 - Architetture Software - SOA Cloud
8 - Architetture Software - Architecture centric processes
7 - Architetture Software - Software product line
6 - Architetture Software - Model transformation
5 - Architetture Software - Metamodelling and the Model Driven Architecture
4 - Architetture Software - Architecture Portfolio
3 - Architetture Software - Architectural styles
2 - Architetture Software - Software architecture
1 - Architetture Software - Software as a product
10 - Architetture Software - More architectural styles
4 (uml basic)
Tmd template-sand
26 standards

Parallel programming using MPI

  • 1. Parallel Programming using MPI Collective Communications Claudio Gheller CINECA c.gheller@cineca.it Collective Communications Collective Communications !"#$$%&'()*'#&+,'&-#.-'&/,),/0#%1,#2,10#(3++ !")..34,56,).. 10#(3++3+,'&,),(#$$%&'()*#0 Barrier Synchronization Broadcast Gather/Scatter Reduction (sum, max, prod, … ) 2 1
  • 2. Collective Communications Characteristics All processes must call the collective routine No non-blocking collective communication No tags 7)23+*,)&4,$#+*,322'('3&*, (#$$%&'()*'#&,$#43 3 Collective Communications MPI_Barrier Stop processes until all processes within a communicator reach the barrier Fortran: CALL MPI_BARRIER(comm, ierr) C: int MPI_Barrier(MPI_Comm comm) 4 2
  • 3. Collective Communications Barrier *: *; *< 8; 89 8= 8: 8< 89 8: 8; 8< 8= barrier barrier 89 8: 8; 8< 8= * 5 Collective Communications Broadcast (MPI_BCAST) One-to-all communication: same data sent from root process to all others in the communicator Fortran: INTEGER count, type, root, comm, ierr CALL MPI_BCAST(buf, count, type, root, comm, ierr) Buf array of type type C: int MPI_Bcast(void *buf, int count, MPI_Datatype, datatypem int root, MPI_Comm comm) All processes must specify same root, rank and comm 6 3
  • 4. Collective Communications Broadcast PROGRAM broad_cast INCLUDE 'mpif.h' INTEGER ierr, myid, nproc, root INTEGER status(MPI_STATUS_SIZE) REAL A(2) 89 CALL MPI_INIT(ierr) CALL MPI_COMM_SIZE(MPI_COMM_WORLD, nproc, ierr) CALL MPI_COMM_RANK(MPI_COMM_WORLD, myid, ierr) ): root = 0 8: IF( myid .EQ. 0 ) THEN 89 ): a(1) = 2.0 a(2) = 4.0 ): END IF 8; CALL MPI_BCAST(a, 2, MPI_REAL, 0, ): MPI_COMM_WORLD, ierr) WRITE(6,*) myid, ': a(1)=', a(1), 'a(2)=', a(2) CALL MPI_FINALIZE(ierr) 8< END 7 Collective Communications Scatter / Gather 7()**30 >)*?30 sndbuf sndbuf sndbuf sndbuf sndbuf 89 89 ): ); )< )= 89 89 ): 8: 8: ); 8; 8; )< 8< 8< )= 89 ): 8: ); 8; )< 8< )= 89 89 ): ); )< )= rcvbuf rcvbuf rcvbuf rcvbuf rcvbuf 8 4
  • 5. Collective Communications MPI_Scatter One-to-all communication: different data sent from root process to all others in the communicator Fortran: CALL MPI_SCATTER(sndbuf, sndcount, sndtype, rcvbuf, rcvcount, rcvtype, root, comm, ierr) Arguments definition are like other MPI subroutine sndcount is the number of elements sent to each process, not the size of sndbuf, that should be sndcount times the number of process in the communicator The sender arguments are meaningful only for root 9 Collective Communications 10 5
  • 6. Collective Communications MPI_SCATTERV Usage – int MPI_Scatterv( void* sendbuf, /* in */ int* sendcounts, /* in */ int* displs, /* in */ MPI_Datatype sendtype, /* in */ void* recvbuf, /* in */ int recvcount, /* in */ MPI_Datatype recvtype, /* in */ int root, /* in */ MPI_Comm comm); /* in */ Description – Distributes individual messages from root to each process in communicator – Messages can have different sizes and displacements 11 Collective Communications 12 6
  • 7. Collective Communications MPI_Gather One-to-all communication: different data collected by the root process, from all others processes in the communicator. Is the opposite of Scatter Fortran: CALL MPI_GATHER(sndbuf, sndcount, sndtype, rcvbuf, rcvcount, rcvtype, root, comm, ierr) Arguments definition are like other MPI subroutine rcvcount is the number of elements collected from each process, not the size of rcvbuf, that should be rcvcount times the number of process in the communicator The receiver arguments are meaningful only for root 13 Collective Communications 14 7
  • 8. Collective Communications MPI_GATHERV Usage – int MPI_Gatherv( void* sendbuf, /* in */ int sendcount, /* in */ MPI_Datatype sendtype, /* in */ void* recvbuf, /* out */ int* recvcount, /* in */ int* displs, /* in */ MPI_Datatype recvtype, /* in */ int root, /* in */ MPI_Comm comm ); /* in */ Description – Collects individual messages from each process in communicator to the root process and store them in rank order – Messages can have different sizes and displacements 15 Collective Communications 16 8
  • 9. Collective Communications Scatter example PROGRAM scatter INCLUDE 'mpif.h' INTEGER ierr, myid, nproc, nsnd, I, root INTEGER status(MPI_STATUS_SIZE) REAL A(16), B(2) CALL MPI_INIT(ierr) CALL MPI_COMM_SIZE(MPI_COMM_WORLD, nproc, ierr) CALL MPI_COMM_RANK(MPI_COMM_WORLD, myid, ierr) root = 0 IF( myid .eq. root ) THEN DO i = 1, 16 a(i) = REAL(i) END DO END IF nsnd = 2 CALL MPI_SCATTER(a, nsnd, MPI_REAL, b, nsnd, & MPI_REAL, root, MPI_COMM_WORLD, ierr) WRITE(6,*) myid, ': b(1)=', b(1), 'b(2)=', b(2) CALL MPI_FINALIZE(ierr) 17 Collective Communications Gather example PROGRAM gather INCLUDE 'mpif.h' INTEGER ierr, myid, nproc, nsnd, I, root INTEGER status(MPI_STATUS_SIZE) REAL A(16), B(2) CALL MPI_INIT(ierr) CALL MPI_COMM_SIZE(MPI_COMM_WORLD, nproc, ierr) CALL MPI_COMM_RANK(MPI_COMM_WORLD, myid, ierr) root = 0 b(1) = REAL( myid ) b(2) = REAL( myid ) nsnd = 2 CALL MPI_GATHER(b, nsnd, MPI_REAL, a, nsnd, & MPI_REAL, root MPI_COMM_WORLD, ierr) IF( myid .eq. root ) THEN DO i = 1, (nsnd*nproc) WRITE(6,*) myid, ': a(i)=', a(i) END DO END IF CALL MPI_FINALIZE(ierr) 18 9
  • 10. Collective Communications MPI_Alltoall @#0*0)&A CALL MPI_ALLTOALL(sndbuf, sndcount, sndtype, rcvbuf, rcvcount, rcvtype, comm, ierr) 89 ): ); )< )= 89 ): 5: (: 4: 89 89 rcvbuf sndbuf 5: 5; 5< 5= ); 5; (; 4; 89 (: (; (< (= 89 )< 5< (< 4< 89 4: 4; 4< 4= 89 )= 5= (= 4= B306,%+32%.,*#,'$1.3$3&*,4)*),*0)&+1#+'*'#& 19 Collective Communications Reduction The reduction operation allow to: • Collect data from each process • Reduce the data to a single value • Store the result on the root processes • Store the result on all processes • Overlap of communication and computing 20 10
  • 11. Collective Communications Reduce, Parallel Sum 89 ): 5: 89 7) 75 8: ); 5; 8: 7) 75 7)C):D);D)<D)= 8; )< 5< 75C5:D5;D5<D5= 8; 7) 75 8< )= 5= 8< 7) 75 E34%(*'#&,2%&(*'#&,F#0G+,F'*?,)00)6+ #*?30,#130)*'#&A,10#4%(*H,$'&H,$)IH,)&4H,JK, 21 Collective Communications MPI_REDUCE and MPI_ALLREDUCE Fortran: MPI_REDUCE( snd_buf, rcv_buf, count, type, op, root, comm, ierr) snd_buf input array of type type containing local values. rcv_buf output array of type type containing global results Count (INTEGER) number of element of snd_buf and rcv_buf type (INTEGER) MPI type of snd_buf and rcv_buf op (INTEGER) parallel operation to be performed root (INTEGER) MPI id of the process storing the result comm (INTEGER) communicator of processes involved in the operation ierr (INTEGER) output, error code (if ierr=0 no error occours) MPI_ALLREDUCE( snd_buf, rcv_buf, count, type, op, comm, ierr) The argument root is missing, the result is stored to all processes. 22 11
  • 12. Collective Communications Predefined Reduction Operations MPI op Function MPI_MAX Maximum MPI_MIN Minimum MPI_SUM Sum MPI_PROD Product MPI_LAND Logical AND MPI_BAND Bitwise AND MPI_LOR Logical OR MPI_BOR Bitwise OR MPI_LXOR Logical exclusive OR MPI_BXOR Bitwise exclusive OR MPI_MAXLOC Maximum and location MPI_MINLOC Minimum and location 23 Collective Communications Reduce / 1 C: int MPI_Reduce(void * snd_buf, void * rcv_buf, int count, MPI_Datatype type, MPI_Op op, int root, MPI_Comm comm) int MPI_Allreduce(void * snd_buf, void * rcv_buf, int count, MPI_Datatype type, MPI_Op op, MPI_Comm comm) 24 12
  • 13. Collective Communications Reduce, example PROGRAM reduce INCLUDE 'mpif.h' INTEGER ierr, myid, nproc, root INTEGER status(MPI_STATUS_SIZE) REAL A(2), res(2) CALL MPI_INIT(ierr) CALL MPI_COMM_SIZE(MPI_COMM_WORLD, nproc, ierr) CALL MPI_COMM_RANK(MPI_COMM_WORLD, myid, ierr) root = 0 a(1) = 2.0*myid a(2) = 4.0+myid CALL MPI_REDUCE(a, res, 2, MPI_REAL, MPI_SUM, root, & MPI_COMM_WORLD, ierr) IF( myid .EQ. 0 ) THEN WRITE(6,*) myid, ': res(1)=', res(1), 'res(2)=', res(2) END IF CALL MPI_FINALIZE(ierr) END 25 13