0
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <mpi.h>

int  main(int argc, char **argv)
{
    int size;
    int rank;
    int tag;
    int chunksize;
    int letters[26];
    int start;
    int lineCount;
    int result[26];
    int Addletters[26];
    unsigned char c;
    char *data;



    MPI_Init(NULL, NULL);
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);


  
    if (rank==0){
            FILE *file;

            file = fopen("WarAndPeace.txt", "r");
                    while ((c = getc(file)) != EOF){
                        if ( isalpha (c)) {
                            lineCount++;
                    } 
                }     
            fclose(file); 
            data = (char*)malloc(sizeof(char) * lineCount);  
            chunksize = lineCount/size;
            int remainder = lineCount%size;
            
            file = fopen("WarAndPeace.txt", "r");
                int n=0;
                while ((c = getc(file)) != EOF){
                    if ( isalpha (c)) {
                        data[n]  = tolower(c);
                        n++;
            } 
         }
         fclose(file); 


        for(int i= 1; i<size; i++){
            start = i*chunksize;
            MPI_Send(&(data[start]), chunksize, MPI_BYTE, i, tag, MPI_COMM_WORLD);
            if(i == (size-1)){
                chunksize + remainder;
            }
        }
        for(int i= 1; i<size; i++){
          MPI_Recv(&result, 1, MPI_BYTE, MPI_ANY_SOURCE, tag, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
           for(int i=0; i<26; i++){
                Addletters[i]+=result[i];
            }
        }
          for(int c=0; c<26; c++){
            printf("%c \t  %d\n", c + 'a', Addletters[c]);
        }
        
    }else{
         MPI_Recv(&data, chunksize, MPI_BYTE, 0, tag, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
             for(int i=0; i<chunksize; i++){
                if(data[i]>='a' && data[i]<='z'){
                    letters[data[i]-'a']++;
                }
            }
         MPI_Send(&letters, 1, MPI_BYTE,0,tag,MPI_COMM_WORLD);
    }
    MPI_Finalize(); 
    return 0;
}


[tl-03:3093864] *** An error occurred in MPI_Recv
[tl-03:3093864] *** reported by process [1716191233,3]
[tl-03:3093864] *** on communicator MPI_COMM_WORLD
[tl-03:3093864] *** MPI_ERR_COUNT: invalid count argument
[tl-03:3093864] *** MPI_ERRORS_ARE_FATAL (processes in this communicator will now abort,
[tl-03:3093864] ***    and potentially your MPI job)
[tl-03:3093851] PMIX ERROR: UNREACHABLE in file ../../../src/server/pmix_server.c at line 2193
[tl-03:3093851] 1 more process has sent help message help-mpi-errors.txt / mpi_errors_are_fatal
[tl-03:3093851] Set MCA parameter "orte_base_help_aggregate" to 0 to see all help / error messages

i am trying to count the occurence of letters in a text file. In this programme, i have to read the text file in rank 0 which is what i have done and also i have to send chunks of the data to the other processors . The other processes should be able to count the letters and send them back . please tell me where i am going wrong Thanks in advance

  • Is there a reason you are using MPI here? A simple frequency array will do. If you will have more than 4 billion of each character, then using `unsigned long long` for the array type. I don't see how MPI helps here. – David C. Rankin Feb 23 '21 at 19:27
  • Generally, when wanting the frequency of any object within a specified range, you want your frequency array to cover the entire range of possible values for that object. For a full explanation of what is frequency array is, and how to use it, a fuller explanation is provided in answer to [How to remove duplicate char in string in C](https://stackoverflow.com/a/63255183/3422102) An example related to characters is [Find most frequent letter in C](https://stackoverflow.com/a/65462132/3422102) – David C. Rankin Feb 23 '21 at 19:32
  • 1
    You compute the value of `chunksize` in rank 0 only but use it in all other ranks where it has a random initial value. Use `MPI_Bcast` to broadcast the value of `chunksize` to all other ranks first. – Hristo Iliev Feb 23 '21 at 19:40
  • You also need to allocate `data` before `MPI_Recv(data, ...)` (no `&`) here. That being said, this is a textbook use case for `MPI_Scatterv()` and `MPI_Reduce()`. – Gilles Gouaillardet Feb 23 '21 at 23:59
  • Not to mention `MPI-IO` instead of scattering the file content. – Gilles Gouaillardet Feb 24 '21 at 00:01

0 Answers0