Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include "stdafx.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <mpi.h>
- #include <math.h>
- #define n 5
- #define k 10
- #define m 7
- struct Data {
- int masn[n];
- double masd[n];
- double masdd[m];
- };
- int main(int arc, char **argv) {
- int size, rank, msgtag = 12;
- MPI_Status status;
- int position;
- //шапочка
- if (MPI_Init(&arc, &argv) != MPI_SUCCESS) return 1;
- if (MPI_Comm_size(MPI_COMM_WORLD, &size) != MPI_SUCCESS) {
- MPI_Finalize();
- return 2;
- }
- if (MPI_Comm_rank(MPI_COMM_WORLD, &rank) != MPI_SUCCESS) {
- MPI_Finalize();
- return 3;
- }
- Data data[k];
- int len = sizeof(data);
- if (!rank) {
- void *buf = malloc(len);
- for (int i = 0; i < k; i++) {
- for (int j = 0; j < n; j++) {
- data[i].masn[j] = j*j;
- data[i].masd[j] = (double)(j*j + 0.5);
- }
- for (int t = 0; t < m; t++) {
- data[i].masdd[t] = (double)(t*t + 0.1);
- }
- }
- position = 0;
- for (int i = 0; i < k; i++) {
- MPI_Pack(data[i].masn, n, MPI_INT, buf, len, &position, MPI_COMM_WORLD);
- MPI_Pack(data[i].masd, n, MPI_DOUBLE, buf, len, &position, MPI_COMM_WORLD);
- MPI_Pack(data[i].masdd, m, MPI_DOUBLE, buf, len, &position, MPI_COMM_WORLD);
- }
- MPI_Send(&position, 1, MPI_INT, 1, msgtag, MPI_COMM_WORLD);
- MPI_Send(buf, position, MPI_PACKED, 1, msgtag, MPI_COMM_WORLD);
- free(buf);
- }
- if (rank == 1) {
- int pos = 0;
- void *mas = malloc(len);
- MPI_Recv(&position, 1, MPI_INT, 0, msgtag, MPI_COMM_WORLD, &status);
- MPI_Recv(mas, position, MPI_PACKED, 0, msgtag, MPI_COMM_WORLD, &status);
- for (int i = 0; i < k; i++) {
- MPI_Unpack(mas, position, &pos, data[i].masn, n, MPI_INT, MPI_COMM_WORLD);
- MPI_Unpack(mas, position, &pos, data[i].masd, n, MPI_DOUBLE, MPI_COMM_WORLD);
- MPI_Unpack(mas, position, &pos, data[i].masdd, m, MPI_DOUBLE, MPI_COMM_WORLD);
- }
- free(mas);
- for (int i = 0; i < k; i++) {
- for (int j = 0; j < n; j++) {
- fprintf(stderr, "%d ", data[i].masn[j]);
- }
- fprintf(stderr, "\n");
- for (int t = 0; t < n; t++) {
- fprintf(stderr, "%f ", data[i].masd[t]);
- }
- fprintf(stderr, "\n");
- for (int l = 0; l < n; l++) {
- fprintf(stderr, "%f ", data[i].masdd[l]);
- }
- fprintf(stderr, "\n");
- fprintf(stderr, "\n");
- fprintf(stderr, "\n");
- }
- fprintf(stderr, "%d\n", data[1].masn[1]);
- }
- //тапочки
- MPI_Finalize();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement