Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// 24. 9. 2016 (4)
#include <stdio.h>
#include <mpi.h>
#define W MPI_COMM_WORLD
int q(int i, int j, int m) {
return i * m + j;
}
int main(int argc, char** argv) {
MPI_Init(&argc, &argv);
int rank, size;
MPI_Comm_rank(W, &rank);
MPI_Comm_size(W, &size);
int n = 6, m = 6;
int A[n * m], C[n * m];
MPI_Datatype MPI_FOO;
if (rank == 0) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
A[i * m + j] = i * 10 + j;
printf("%2d ", A[i * m + j]);
}
printf("\n");
}
printf("\n");
printf("\n");
}
MPI_Type_vector(n * m / 2, 1, 2, MPI_INT, &MPI_FOO);
MPI_Type_commit(&MPI_FOO);
if (rank == 0) {
for (int r = 1; r < size; r++) {
MPI_Send(A, 1, MPI_FOO, r, 0, W);
}
}
// printf("[%d]", rank);
if (rank != 0) {
MPI_Recv(A, m * n / 2, MPI_INT, 0, 0, W, MPI_STATUS_IGNORE);
}
if (rank != 0) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
C[i * m + j] = 0;
}
}
int i = 0, j = m - 1, z = 0;
for (int k = 0; k < n * m / 2; k++) {
C[i * m + j] = A[k];
i++;
if (i > n - 1 - z) {
i = 0;
j--;
z++;
}
}
}
if (rank != 0) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
printf("%4d", C[i * m + j]);
}
printf("\n");
}
}
MPI_Finalize();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment