---
# System prepended metadata

title: MPI

---

# MPI
> Message Passing Interface
> [Tutorial](https://computing.llnl.gov/tutorials/mpi/)
###### tags: CO3067

## T

![](https://i.imgur.com/p7ibQkN.png)

- In distributed memory, all machines must support MPI

**Blocking**: return from the procedure indicates the user is allowed to reuse resources specified in the call
- Performance is not good
- Simple


**Non blocking**: makes programs run faster
- Faster
- Requires more steps

**Collective**: one process sends message for all other processes in the group (broadcast)
* ![](https://i.imgur.com/wSOSA9q.png)



**Message Envelope**: distinguish messages and selectively receive them
- `<source, destination, tag (kind of message -> 0/1 when u dont know), communicator>`

**Communicator**: 
- Communication context for a communication operation
- Messages are always received within the context they were sent
- `MPI_COMM_WORLD`


## MPI

### MPI_INIT

![](https://i.imgur.com/eTJR4Bq.png)


**Usage**
```cpp=
int MPI_Init( int* argc_ptr,                /* in */
              char** argv_ptr[] );         /* in */
```

**Description**
- Initialize MPI
- All MPI programs must call this routines once and only once before any other MPI routines


### MPI_Finalize

**Usage:**
```cpp=
int MPI_Finalize (void);
```

**Description:**
- Terminates all MPI processing
- Make sure this routine is the last MPI call.
- All pending communications involving a process have completed before the process calls MPI_FINALIZE 


### MPI_Comm_Size

**Usage:**
```cpp=
int MPI_Comm_size( MPI_Comm comm,     /* in */
                   int* size );      /* out */
```
 
**Description:**
- Return the number of processes in the group associated with a communicator

![](https://i.imgur.com/H274Grf.png)

### MPI_Abort
**Usage:**
```cpp=
int MPI_Abort( MPI_Comm comm,    /* in */
               int errorcode ); /* in */
```
 
**Description:**
- Forces all processes of an MPI job to terminate

**Simple program**
```cpp=
#include “mpi.h”
int main( int argc, char* argv[] )
{
     int rank;
     int nproc;
     MPI_Init( &argc, &argv );
     MPI_Comm_size( MPI_COMM_WORLD, &nproc );
     MPI_Comm_rank( MPI_COMM_WORLD, &rank );
     /* write codes for you */
     MPI_Finalize();
} 
```

## Point-to-Point Communication

### MPI_SEND