यिर्मयाह सही है - MPI_Type_create_struct यहां जाने का तरीका है।
यह याद रखना महत्वपूर्ण है कि एमपीआई एक पुस्तकालय है, जो भाषा में नहीं बनाया गया है; इसलिए यह "देख" नहीं सकता है कि एक संरचना खुद को क्रमबद्ध करने की तरह दिखती है। तो जटिल डेटा प्रकार भेजने के लिए, आपको स्पष्ट रूप से इसके लेआउट को परिभाषित करना होगा। एक ऐसी भाषा में जिसमें धारावाहिकता के लिए मूल समर्थन है, एमपीआई रैपर का एक सेट निश्चित रूप से इसका उपयोग कर सकता है; उदाहरण के लिए mpi4py जटिल डेटा प्रकारों को पारदर्शी रूप से भेजने के लिए पायथन के pickle का उपयोग करता है; लेकिन सी में, आपको अपनी आस्तीन को रोल करना होगा और इसे स्वयं करना होगा।
अपने संरचना के लिए, यह इस तरह दिखता है:
#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;
}
स्रोत
2012-03-25 23:38:53
आपकी बहुत व्यापक और त्वरित प्रतिक्रिया के लिए धन्यवाद। मैं वास्तव में इसकी प्रशंसा करता हूँ। तुमने मुझे पूरी तरह से कवर किया है। (हालांकि मुझे लगता है कि आप शीर्ष कारण पर शामिल करना भूल गए हैं अन्यथा संकलक त्रुटियां देता है ..) –
kstratis
आप ऑफसेट() के लिए सही हैं। मैंने कोड को उचित रूप से अपडेट किया है। –