Untitled

mail@pastecode.io avatar
unknown
c_cpp
a month ago
1.3 kB
2
Indexable
Never
void readA(char ***A);
int calcCount(char **A);
int main(int argc, char **argv) {
    MPI_Init(&argc, &argv);
    int rank, p, **A;
    int n = N ;
    MPI_Comm_size(MPI_COMM_WORLD, &p);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);

    if (rank == 0) {
        readA(&A);
        MPI_Send(&A+n/2,n,MPI_CHAR,1,0,WORLD);
    }
    if(rank ==1){
        A = malloc(n/2 * sizeof(char));// איך לקרוא את הגודל של הכל ולהגדיר אותו??..
       // !! ? *A = (char **)malloc(n/2 * sizeof(char *));
        MPI_Recv(&A,n/2,MPI_CHAR,0,0,WORLD);
    }
    char count[] = (**char)calloc(sizeof(char),n/2);

    #pragma omp parallel for 
    for (int i = 0; i < n/2; i++)
        count[i] +=calcCount(A[i]);


    if(rank==1)
    MPI_Send(&count,n/2,MPI_CHAR,1,0,WORLD);
    if(rank==0)
    MPI_Recv(&A+n/2,n/2,MPI_CHAR,0,0,WORLD);

    int max_index = 0 ;

    #pragma omp parallel for 
    for (int i = 1; i < n-1; i++)
        if(count[i] > count[max_index])
          max_index = i ;

     printf("A[%d] has count of %d",max_index,count[max_index]);

    MPI_Finalize();
    return EXIT_SUCCESS;
}

int calcCount(char **A){
    int count = 0;
     for (int i = 0; i < strlen(A); i++)
        if(A[i] == 'a')
          count ++ ;
    return count;
}

Leave a Comment