Untitled
unknown
c_cpp
7 months ago
1.4 kB
3
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); for (j = N/2; j < N; j++) MPI_Send(A[j], strlen(A[j]) + 1, MPI_CHAR, 1, 0, MPI_COMM_WORLD); } else { for (j = 0; j < N/2; j++) { MPI_Probe(0, 0, MPI_COMM_WORLD, &status); MPI_Get_count(&status, MPI_CHAR, &count); A[j] = malloc(count); MPI_Recv(A[j], count, MPI_CHAR, 0, 0, MPI_COMM_WORLD, &status); 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