Untitled
unknown
c_cpp
a year ago
4.1 kB
12
Indexable
void *Malloc(unsigned int nbytes); int f(int i) ; int main(int argc, char **argv) { int myrank, nprocs; int *A; // the input array int *my_A; // part of A the current process handles int N; // size of the input array MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &nprocs); MPI_Comm_rank(MPI_COMM_WORLD, &myrank); if (myrank == 0) { N = 1000; A = (int *) Malloc(sizeof(int)*N); /* initialize A -- just an example. */ for(int i = 0; i<N;i++) A[i] = i; A[749] = A[750] = A[751] = 0; } if (myrank == 0) if ((N % nprocs) != 0) { fprintf(stderr, "size of array not divisable by number of processes\n"); MPI_Abort(MPI_COMM_WORLD, 2); } /* The root process first broadcasts N/nprocs, the size of the subarray each process will handle. This enables the processes to allocate memory for the subarray. And then the root scatters the array itself. */ int chunkSize; if (myrank == 0) chunkSize = N/nprocs; MPI_Bcast(&chunkSize, 1, MPI_INT, 0, MPI_COMM_WORLD); my_A = (int *) Malloc(sizeof(int)*chunkSize); MPI_Scatter(A, chunkSize, MPI_INT, my_A, chunkSize, MPI_INT, 0, MPI_COMM_WORLD); int *my_B = (int *) Malloc(sizeof(int)*chunkSize); #pragma omp parallel for default(none) shared(my_A, my_B) firstprivate(chunkSize) for(int i = 0; i < chunkSize; i++) my_B[i] = f(my_A[i]); int found3zeros = 0; #pragma omp parallel for default(none) shared(my_B)\ firstprivate(chunkSize) reduction(||:found3zeros) for(int i = 0; i < chunkSize-2; i++) if (my_B[i] == 0 && my_B[i+1] == 0 && my_B[i+2] == 0) found3zeros = 1; int any3zeros; // used by process 0 MPI_Reduce(&found3zeros, &any3zeros, 1, MPI_INT, MPI_LOR, 0, MPI_COMM_WORLD); if (myrank == 0) { if (any3zeros) printf("yes\n"); else { /* check if 3 zero sequence starts at one chunk and ends at the next chunk */ for (int c = 1; c < nprocs; c++) { int i = c*chunkSize; // index of beginning of chunk number c if (f(A[i-1]) == 0 && f(A[i]) == 0) { if (f(A[i-2]) == 0 || f(A[i+1]) == 0) { any3zeros = 1; break; } } } if (any3zeros) printf("yes\n"); else printf("no\n"); } } // myrank == 0 MPI_Finalize(); return 0; } void *Malloc(unsigned int nbytes ) { void *p = malloc(nbytes); if (p == NULL) { fprintf(stderr, "malloc failed\n"); MPI_Abort(MPI_COMM_WORLD, 1); } return p; } //////////////// int f(int i) ; int readA(**A); int main(int argc, char *argv[]) { int myrank, p,*A,*myA,chunckSize,*myB,N; MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &p); MPI_Comm_rank(MPI_COMM_WORLD, &rank); if (myrank == 0) { N = readA(&A); chunckSize = N/p; } MPI_Bcast(&chunkSize, 1, MPI_INT, 0, MPI_COMM_WORLD); myA = (int *)malloc(sizeof(int)*chunkSize); myB = (int *)malloc(sizeof(int)*chunkSize); MPI_Scatter(A, chunkSize, MPI_INT, myA, chunkSize, MPI_INT, 0, MPI_COMM_WORLD); int found3 = 0; #pragma omp parallel for for(int i = 0; i < chunkSize; i++) myB[i] = f(myA[i]); #pragma omp parallel for reduction(||:found3) for(int i = 0; i < chunkSize-2; i++) if (myB[i] == 0 && myB[i+1] == 0 && myB[i+2] == 0) found3 = 1; else{ found3=0; } int anyfound3 = 0 ; MPI_Reduce(&found3, &anyfound3, 1, MPI_INT, MPI_LOR, 0, MPI_COMM_WORLD); if (myrank == 0) { if (!anyfound3){ for (int chunckNum = 1; chunckNum < p; chunckNum++) { int seam = chunckNum*chunkSize; if (f(A[seam]) == 0 && f(A[seam-1]) == 0) { if (f(A[seam+1]) == 0 || f(A[seam-2]) == 0) { anyfound3 = 1; break; } } } } if (anyfound3) printf("yes\n"); else printf("no\n"); } // myrank == 0 MPI_Finalize(); return 0; } void *Malloc(unsigned int nbytes ) { void *p = malloc(nbytes); if (p == NULL) { fprintf(stderr, "malloc failed\n"); MPI_Abort(MPI_COMM_WORLD, 1); } return p; }
Editor is loading...
Leave a Comment