Jeremiah tiene razón - MPI_Type_create_struct es el camino a seguir aquí.
Es importante recordar que MPI es una biblioteca, no está integrada en el idioma; por lo tanto, no puede "ver" cómo se ve una estructura para serializarla por sí misma. Entonces, para enviar tipos de datos complejos, debe definir explícitamente su diseño. En un lenguaje que tiene soporte nativo para la serialización, un conjunto de envoltorios MPI puede hacer uso de eso; mpi4py utiliza, por ejemplo, el pickle de python para enviar de forma transparente tipos de datos complejos; pero en C, tienes que arremangarte y hacerlo tú mismo.
Por su estructura, que se parece a esto:
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#include <stddef.h>
typedef struct car_s {
int shifts;
int topSpeed;
} car;
int main(int argc, char **argv) {
const int tag = 13;
int size, rank;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
if (size < 2) {
fprintf(stderr,"Requires at least two processes.\n");
exit(-1);
}
/* create a type for struct car */
const int nitems=2;
int blocklengths[2] = {1,1};
MPI_Datatype types[2] = {MPI_INT, MPI_INT};
MPI_Datatype mpi_car_type;
MPI_Aint offsets[2];
offsets[0] = offsetof(car, shifts);
offsets[1] = offsetof(car, topSpeed);
MPI_Type_create_struct(nitems, blocklengths, offsets, types, &mpi_car_type);
MPI_Type_commit(&mpi_car_type);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (rank == 0) {
car send;
send.shifts = 4;
send.topSpeed = 100;
const int dest = 1;
MPI_Send(&send, 1, mpi_car_type, dest, tag, MPI_COMM_WORLD);
printf("Rank %d: sent structure car\n", rank);
}
if (rank == 1) {
MPI_Status status;
const int src=0;
car recv;
MPI_Recv(&recv, 1, mpi_car_type, src, tag, MPI_COMM_WORLD, &status);
printf("Rank %d: Received: shifts = %d topSpeed = %d\n", rank,
recv.shifts, recv.topSpeed);
}
MPI_Type_free(&mpi_car_type);
MPI_Finalize();
return 0;
}
Gracias por su respuesta muy completa y rápida. Realmente lo aprecio. Me tienes completamente cubierto. (Sin embargo, creo que se olvidó de incluir en la causa principal; de lo contrario, el compilador genera errores ...) –
kstratis
Tiene razón: es necesario para el offsetof(). He actualizado el código apropiadamente. –