Search code examples
fortranmpiderived-types

Duplicating an MPI derived type vs. setting one type equal to another


Let's say I have an MPI derived type, type_a in a Fortran code. I would like a second copy of it, type_b. Is there a practical difference between

call mpi_type_dup(type_a, type_b, err)

and

type_b = type_a ?

I don't want to change any of the "associated key values" that the MPI standard mentions.

Is one of these to be preferred to the other? Also, if type_a is already committed, do I still have to commit type_b?


Solution

  • In Fortran, a derived datatype is an integer (that is abstracted if you use mpi_f08 bindings.

    If you type_b=type_a, then you do not have to commit type_b before using it for communications, but if you later call mpi_type_free(type_a), thentype_b cannot be used any more.

    If you duplicate type_a, then you have to commit type_b before using it for communications, both types are independent and both must be free'd when they are no more needed.

    Regarding the "associated key values"

    MPI_Type_dup is a type constructor that duplicates the existing type with associated key values. For each key value, the respective copy callback function determines the attribute value associated with this key in the new communicator. One particular action that a copy callback may take is to delete the attribute from the new data type.

    So as long as all the copy callback functions do preserve the attributes, this subroutine can be used.

    If you know that type_a won't be free'd when type_b is used, then type_b=type_a is much simpler and more efficient.