Untitled

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

    if (rank == 0) {
        N = readA(&A);
        chunckSize = N / p;
        B = malloc(chunckSize * sizeof(int));
    }

    MPI_Bcast(&chunckSize, 1, MPI_INT, 0, MPI_COMM_WORLD);

    if(rank !=0){
        A = malloc(chunckSize * sizeof(int));
        B = malloc(chunckSize * sizeof(int));
    }

    MPI_Scatter(A, chunckSize, MPI_INT, A, chunckSize, MPI_INT, 0, MPI_COMM_WORLD);

    #pragma omp parallel for 
    for (int i = 0; i < chunckSize; i++)
        B[i] = f(A[i]); // Assuming f is defined elsewhere

    MPI_Gather(B, chunckSize, MPI_INT, B, chunckSize, MPI_INT, 0, MPI_COMM_WORLD);

    if (rank == 0) {
        int isMono = 1;
        #pragma omp parallel for reduction(&&:isMono)
        for (int i = 1; i < N; i++) {
            if (B[i] <= B[i-1]) {
                isMono = 0;
            }
        }
        printf("isMono ? %s\n", isMono ? "Yes" : "No");
    }

    MPI_Finalize();
    return EXIT_SUCCESS;
}
Leave a Comment