0

I need to send an array of char and a counter to the master process. My idea was to define an mpi type and send it later to the master. The array is variable size and needs to be created from a char pointer. Basically the processes have this data structure:

struct WordCounter
{
  char *word;
  int word_count;
  struct WordCounter *pNext;                        /* Pointer to the next word counter in the list */
};

What I need to do is to try to send char * word and int word count to the master, so char*word must be copied into an array of char without pointer. How should I define the new mpi data type?

My idea is to use this struct and commit it as Mpi data type, but how i should manage the construction? I can take the dimension of char *word and using inside the struct to commit.

struct WordAndCount
{
  char word[n];  // n varies according to the length of the word
  int count;
};

What im trying to do is:

MPI_Init(&argc,&argv);
int world_size,rank;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
MPI_Comm_rank (MPI_COMM_WORLD,&rank);
MPI_Datatype wordinfo,oldtype[1];
MPI_Aint offset[1],lw,extent;
int blockcounts[1];

offset[0] = 0;
oldtype[0] = MPI_CHAR;
blockcounts[0] = 1;

After this i can't go on, im a newbie with mpi, can you help me? Another thing is how i can copy the char pointer inside at the static array. I can do it with a simple for that loop the pointer and copy the char inside to array?

An example is:

Master builded his WordCounter that contains: Hello (word) 1(counter), Hi 2, etc... Process 1 builded his WordCounter that contains: Hello 3, hi 4. Process 2 builded his WordCounter that contains: hi 8, hello 3, sun 5.

Master needs to recive through a gather at first round hello 3 (from process1), hi 8 (from process 2). At second round hi 4 (from process 1) hello 3 (from process 2) and so on.

  • Would it not be easier to send the int and the variable length char[] array separately? Or is the length of char[] always n and that one is known beforehand? Maybe also check [here](https://stackoverflow.com/questions/32928523/create-mpi-type-for-struct-containing-dynamic-array). – Flusslauf Jul 22 '21 at 08:52
  • @Flusslauf I need to use one message, beacuse after i get this information i need to call a gather function for passing it to master form each process. – Gabriele Pisapia Jul 22 '21 at 08:57
  • So you mean the struct you are creating will be created on say N (in some group) processes and then be gathered to say process 0 via Gather? Will the structs then have different sizes on all processes (so can the ```n``` differ)? – Flusslauf Jul 22 '21 at 09:05
  • more simply n, it depends on the word considered to the pointer of the WordCounter structure, so the length of char * word. for each word in WordCounter I need to pass the word and the counter of the word considered. An example: Master has got: hello (word) 1 (count), hi 2, etc... Process 1 has got: hello 3, hi 4. Process 2 has got: hi 8, hello 3, sun 5 master receive through gather the words hello (from process 1 and its counter then 3), from process 2 must receive hi with counter 8 and so on. @Flusslauf – Gabriele Pisapia Jul 22 '21 at 09:15
  • The amount of words and their length and their order in the list vary over the processes (coming from your example)? Creating an MPI_Datatype needs to know its size so two structs with different n will produce different MPI_Datatypes. And when calling gather you need to have the same type, too. Also - how would master know when to stop gathering? Since your linked list has different length on all processes. Check out [this](https://stackoverflow.com/questions/2258759/passing-variable-length-structures-between-mpi-processes) on why that does not work and a suggestion on what to do instead. – Flusslauf Jul 22 '21 at 09:34
  • I would probably suggest: gather all words first (maybe by separating the via some special char or by gathering their lengths first) and then send the respective word counts. You could also send all data as a pure Byte stream and do something like in the link in my earlier comment. – Flusslauf Jul 22 '21 at 09:39

0 Answers0