Logo Cineca Logo SCAI

You are here

Exercise 12

Each task initializes a square nxn matrix (n is the total number of the tasks) with 0s, except for the diagonal elements of the matrix that are initialized with the task's rank number.


Each task sends to rank 0 an array containing all the elements of its diagonal. Task 0 overwrites the array sent by process i on
 the i-th row (column if Fortran) of its local matrix. At the end, task 0 prints its final matrix, on which each element should be the number of its row (or column).













In order to send a diagonal, a proper vector datatype should be created and set for reading the diagonal elements of a matrix with the right displacement and stride. When you are communicating to rank 0, keep in mind that you are sending a single vector datatype, but you want to receive an array of n elements, that have to be stored contiguously in its matrix row (or column).


HINTS:


 

C

MPI_TYPE_VECTOR

int MPI_Type_vector(int count, int blocklength, int stride, MPI_Datatype oldtype, MPI_Datatype *newtype)

MPI_TYPE_COMMIT

int MPI_Type_commit(MPI_Datatype *datatype)

MPI_TYPE_FREE

int MPI_Type_free(MPI_Datatype *datatype)

MPI_GATHER

int MPI_Gather(void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm)

MPI_INIT

int MPI_Init(int *argc, char ***argv)

MPI_COMM_SIZE

int MPI_Comm_size(MPI_Comm comm, int *size)

MPI_COMM_RANK

int MPI_Comm_rank(MPI_Comm comm, int *rank)

MPI_FINALIZE

int MPI_Finalize(void)

 


FORTRAN

MPI_TYPE_VECTOR

MPI_TYPE_VECTOR(COUNT, BLOCKLENGTH, STRIDE, OLDTYPE, NEWTYPE, IERROR)
INTEGER COUNT, BLOCKLENGTH, STRIDE, OLDTYPE, NEWTYPE, IERROR

MPI_TYPE_COMMIT

MPI_TYPE_COMMIT(DATATYPE, IERROR)
INTEGER DATATYPE, IERROR

MPI_TYPE_FREE

MPI_TYPE_FREE(DATATYPE, IERROR)
INTEGER DATATYPE, IERROR

MPI_GATHER

MPI_GATHER(SENDBUF, SENDCOUNT, SENDTYPE, RECVBUF, RECVCOUNT, RECVTYPE, ROOT, COMM, IERROR)
<type> SENDBUF(*), RECVBUF(*)
INTEGER SENDCOUNT, SENDTYPE, RECVCOUNT, RECVTYPE, ROOT, COMM, IERROR

MPI_INIT

MPI_INIT(IERROR)
INTEGER IERROR

MPI_COMM_SIZE

MPI_COMM_SIZE(COMM, SIZE, IERROR)
INTEGER COMM, SIZE, IERROR

MPI_COMM_RANK

MPI_COMM_RANK(COMM, RANK, IERROR)
INTEGER COMM, RANK, IERROR

MPI_FINALIZE

MPI_FINALIZE(IERROR)
INTEGER IERROR