Lab 7
Lab 7
if (numtasks == SIZE) {
// task 0 sends one element of rowtype to all tasks
if (rank == 0) {
for (i = 0; i < numtasks; i++) {
MPI_Send(&a[i][0], 1, rowtype, i, tag, MPI_COMM_WORLD);
}
}
MPI_Status stat;
MPI_Datatype columntype; // required variable
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
if (numtasks == SIZE) {
// task 0 sends one element of columntype to all tasks
if (rank == 0) {
for (i = 0; i < numtasks; i++)
MPI_Send(&a[0][i], 1, columntype, i, tag, MPI_COMM_WORLD);
}
Code: Data-type 3)
#include "mpi.h"
#include <stdio.h>
#define NELEMENTS 6
int main(int argc, char *argv[]) {
int numtasks, rank, source=0, dest, tag=1, i;
int blocklengths[2], displacements[2];
float a[16] =
{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0,
9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0};
float b[NELEMENTS];
MPI_Status stat;
MPI_Datatype indextype; // required variable
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
blocklengths[0] = 4;
blocklengths[1] = 2;
displacements[0] = 5;
displacements[1] = 12;
if (rank == 0) {
for (i = 0; i < numtasks; i++)
// task 0 sends one element of indextype to all tasks
MPI_Send(&a[0], 1, indextype, i, tag, MPI_COMM_WORLD);
}
Question 2) Can you think about howto use the above 4 derived data types to represent
themain diagonal elements(1.0, 6.0, 11.0, 16.0) of the above 4x4 matrix and distributethem
allprocesses.?
#include "mpi.h"
#include <stdio.h>
#define SIZE 4
MPI_Status stat;
MPI_Datatype rowtype, columntype, indextype, particletype;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
typedef struct {
float value;
} DiagonalElement;
MPI_Datatype diagnostictype;
MPI_Type_contiguous(1, MPI_FLOAT, &diagnostictype);
MPI_Type_commit(&diagnostictype);
if (rank == 0) {
diagonal[0] = a[0][0];
diagonal[1] = a[1][1];
diagonal[2] = a[2][2];
diagonal[3] = a[3][3];
MPI_Finalize();
return 0;
}
Explanation: The above code represents the diagonal elements from the 4x4
matrix and sends it to all the processors.