Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include <stdio.h>
#include <mpi.h>
#define W MPI_COMM_WORLD
#define MPI_SI MPI_STATUS_IGNORE
#define n 6
void printArray(int size, int array[size], int rank) {
for (int i = 0; i < size; i++) {
if (rank == -1) printf("%4d ", array[i]);
else printf("{%d}%4d ", rank, array[i]);
}
printf("\n");
}
void printMatrix(int rows, int cols, int matrix[rows][cols], int rank) {
for (int i = 0; i < rows; i++) {
printArray(cols, matrix[i], rank);
}
}
int main(int argc, char** argv) {
MPI_Init(&argc, &argv);
int rank, size;
MPI_Comm_rank(W, &rank);
MPI_Comm_size(W, &size);
// Inicijalizacija
int A[n][n], C[n][n] = {0};
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
A[i][j] = 10 * i + j;
}
}
// Definisanje tipa za slanje
MPI_Datatype MPI_FOO;
MPI_Type_vector(n / 2, 2, 2 * n, MPI_INT, &MPI_FOO);
MPI_Type_create_resized(MPI_FOO, 0, 2 * sizeof(int), &MPI_FOO);
MPI_Type_commit(&MPI_FOO);
// Definsnje tipa za primanje
MPI_Datatype MPI_COL;
MPI_Type_vector(n, 1, n, MPI_INT, &MPI_COL);
MPI_Type_create_resized(MPI_COL, 0, sizeof(int), &MPI_COL);
MPI_Type_commit(&MPI_COL);
// Slanje i prikaz
if (rank == 0) MPI_Send(A, n / 2, MPI_FOO, 1, 0, W);
else MPI_Recv(C, n / 2, MPI_COL, 0, 0, W, MPI_SI);
if (rank != 0) printMatrix(n, n, C, -1);
MPI_Finalize();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment