---
name: "MPI_Init_thread.openmpi"
section: "3"
platform: "linux"
package: "openmpi-doc"
version: "5.0.7-1"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/MPI_Init_thread.openmpi"
---

# MPI\_Init\_thread.openmpi(3)

*MPI\_Init\_thread* — Initializes the MPI execution environment

## SYNTAX

### C Syntax

```
#include <mpi.h>
int MPI_Init_thread(int *argc, char ***argv,
     int required, int *provided)
```

### Fortran Syntax

```
USE MPI
! or the older form: INCLUDE 'mpif.h'
MPI_INIT_THREAD(REQUIRED, PROVIDED, IERROR)
     INTEGER REQUIRED, PROVIDED, IERROR
```

### Fortran 2008 Syntax

```
USE mpi_f08
MPI_Init_thread(required, provided, ierror)
     INTEGER, INTENT(IN) :: required
     INTEGER, INTENT(OUT) :: provided
     INTEGER, OPTIONAL, INTENT(OUT) :: ierror
```

## INPUT PARAMETERS

- •

  **argc**: C only: Pointer to the number of arguments.

- •

  **argv**: C only: Argument vector.

- •

  **required**: Desired level of thread support (integer).

## OUTPUT PARAMETERS

- •

  **provided**: Available level of thread support (integer).

- •

  **ierror**: Fortran only: Error status (integer).

## DESCRIPTION

This routine, or *MPI\_Init*, must be called before most other MPI routines are called. There are a small number of exceptions, such as *MPI\_Initialized* and *MPI\_Finalized*. MPI can be initialized at most once; subsequent calls to *MPI\_Init* or *MPI\_Init\_thread* are erroneous.

*MPI\_Init\_thread*, as compared to *MPI\_Init*, has a provision to request a certain level of thread support in *required*:

- **MPI\_THREAD\_SINGLE**

  Only one thread will execute.

- **MPI\_THREAD\_FUNNELED**

  If the process is multithreaded, only the thread that called *MPI\_Init\_thread* will make MPI calls.

- **MPI\_THREAD\_SERIALIZED**

  If the process is multithreaded, only one thread will make MPI library calls at one time.

- **MPI\_THREAD\_MULTIPLE**

  If the process is multithreaded, multiple threads may call MPI at once with no restrictions.

The level of thread support available to the program is set in *provided*. In Open MPI, the value is dependent on how the library was configured and built. Note that there is no guarantee that *provided* will be greater than or equal to *required*.

Also note that calling *MPI\_Init\_thread* with a *required* value of MPI\_THREAD\_SINGLE is equivalent to calling *MPI\_Init*.

All MPI programs must contain a call to *MPI\_Init* or *MPI\_Init\_thread*. Open MPI accepts the C *argc* and *argv* arguments to main, but neither modifies, interprets, nor distributes them:

```
/* declare variables */
MPI_Init_thread(&argc, &argv, req, &prov);
/* parse arguments */
/* main program */
MPI_Finalize();
```

## NOTES

The Fortran version does not have provisions for **argc** and **argv** and takes only **IERROR**.

It is the caller’s responsibility to check the value of **provided**, as it may be less than what was requested in **required**.

The MPI Standard does not say what a program can do before an *MPI\_Init\_thread* or after an *MPI\_Finalize*. In the Open MPI implementation, it should do as little as possible. In particular, avoid anything that changes the external state of the program, such as opening files, reading standard input, or writing to standard output.

### MPI\_THREAD\_MULTIPLE Support

**MPI\_THREAD\_MULTIPLE** support is included if the environment in which Open MPI was built supports threading. You can check the output of *[ompi\_info(1)](/ompi_info/1.md)* to see if Open MPI has **MPI\_THREAD\_MULTIPLE** support:

```
shell$ ompi_info | grep "Thread support"
          Thread support: posix (MPI_THREAD_MULTIPLE: yes, OPAL support: yes, OMPI progress: no, Event lib: yes)
shell$
```

The **MPI\_THREAD\_MULTIPLE: yes** portion of the above output indicates that Open MPI was compiled with **MPI\_THREAD\_MULTIPLE** support.

Note that there is a small performance penalty for using **MPI\_THREAD\_MULTIPLE** support; latencies for short messages will be higher as compared to when using **MPI\_THREAD\_SINGLE**, for example.

## ERRORS

Almost all MPI routines return an error value; C routines as the return result of the function and Fortran routines in the last argument.

Before the error value is returned, the current MPI error handler associated with the communication object (e.g., communicator, window, file) is called. If no communication object is associated with the MPI call, then the call is considered attached to MPI\_COMM\_SELF and will call the associated MPI error handler. When MPI\_COMM\_SELF is not initialized (i.e., before *MPI\_Init*/*MPI\_Init\_thread*, after *MPI\_Finalize*, or when using the Sessions Model exclusively) the error raises the initial error handler. The initial error handler can be changed by calling *MPI\_Comm\_set\_errhandler* on MPI\_COMM\_SELF when using the World model, or the mpi\_initial\_errhandler CLI argument to mpiexec or info key to *MPI\_Comm\_spawn*/*MPI\_Comm\_spawn\_multiple*. If no other appropriate error handler has been set, then the MPI\_ERRORS\_RETURN error handler is called for MPI I/O functions and the MPI\_ERRORS\_ABORT error handler is called for all other MPI functions.

Open MPI includes three predefined error handlers that can be used:

- •

  **MPI\_ERRORS\_ARE\_FATAL** Causes the program to abort all connected MPI processes.

- •

  **MPI\_ERRORS\_ABORT** An error handler that can be invoked on a communicator, window, file, or session. When called on a communicator, it acts as if *MPI\_Abort* was called on that communicator. If called on a window or file, acts as if *MPI\_Abort* was called on a communicator containing the group of processes in the corresponding window or file. If called on a session, aborts only the local process.

- •

  **MPI\_ERRORS\_RETURN** Returns an error code to the application.

MPI applications can also implement their own error handlers by calling:

- •

  *MPI\_Comm\_create\_errhandler* then *MPI\_Comm\_set\_errhandler*

- •

  *MPI\_File\_create\_errhandler* then *MPI\_File\_set\_errhandler*

- •

  *MPI\_Session\_create\_errhandler* then *MPI\_Session\_set\_errhandler* or at *MPI\_Session\_init*

- •

  *MPI\_Win\_create\_errhandler* then *MPI\_Win\_set\_errhandler*

Note that MPI does not guarantee that an MPI program can continue past an error.

See the *MPI man page* for a full list of *MPI error codes*.

See the Error Handling section of the MPI-3.1 standard for more information.

**SEE ALSO:**

- •

  *MPI\_Init*

- •

  *MPI\_Initialized*

- •

  *MPI\_Finalize*

- •

  *MPI\_Finalized*

## COPYRIGHT

2003-2025, The Open MPI Community
