Untitled
unknown
c_cpp
2 years ago
3.4 kB
9
Indexable
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 main(int argc, char **argv) {
int my_rank, nprocs;
int *A, *B; // the input arrays
int N; // size of A. size of B is N+1
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
if (my_rank == 0) {
N = 999;
A = (int *) Malloc(sizeof(int)*N);
B = (int *) Malloc(sizeof(int)*(N+1));
/* initialize A and B -- just an example. */
for(int i = 0; i<N;i++) {
A[i] = B[i] = 100;
}
B[23] = 23;
B[513] = 513;
B[N] = 100;
}
/* The root process first broadcasts N
This enables the processes to allocate memory for local copies of A and B.
And then the root broadcasts both arrays.
*/
MPI_Bcast(&N, 1, MPI_INT, 0, MPI_COMM_WORLD);
if (my_rank != 0) {
A = (int *) Malloc(sizeof(int)*N);
B = (int *) Malloc(sizeof(int)*(N+1));
}
MPI_Bcast(A, N, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Bcast(B, N+1, MPI_INT, 0, MPI_COMM_WORLD);
int chunk = (N+1)/nprocs;
int my_start = my_rank * chunk;
int my_end = my_start + chunk;
int distance = N+1; // distance between A and B must be smaller than N+1
#pragma omp parallel for default(none) shared(A, B, my_start, my_end, N) reduction(min:distance)
for(int skip = my_start; skip < my_end; skip++) {
/* compare A with B but skip B[skip] */
int count = 0; // count number of places in which A and B differ
for (int i = 0, j = 0; i < N; )
if (j == skip)
j++;
else {
if (A[i] != B[j])
count++;
i++; j++;
}
if (count < distance)
distance = count;
}
int final_distance; // used by process 0
MPI_Reduce(&distance, &final_distance, 1, MPI_INT, MPI_MIN, 0, MPI_COMM_WORLD);
if (my_rank == 0)
printf("distance between A and B is %d\n", final_distance);
MPI_Finalize();
return 0;
}
//////////
int calcDistance(int *A,int *B,int n,int skip);
int main(int argc, char *argv[]) {
int rank, p,*A,*B,n,chunckSize,distance;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &p);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (my_rank == 0) {
int N = readA(&A);
readA(&B);
n=N;
}
MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
if (my_rank != 0) {
A = (int *) Malloc(sizeof(int)*n);
B = (int *) Malloc(sizeof(int)*(n+1));
}
MPI_Bcast(A, n, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Bcast(B, n+1, MPI_INT, 0, MPI_COMM_WORLD);
int chunkSize = (N+1)/p;
int my_start = my_rank * chunkSize;
int my_end = my_start + chunkSize;
int distance = n+1; // distance between A and B must be smaller than N+1
#pragma omp parallel for reduction(min:distance)
for(int skip = my_start; skip < my_end; skip++) {
distance=calcDistance(A,B,n,skip);
}
int final_min_distance = distance;
MPI_Reduce(&distance, &final_min_distance, 1, MPI_INT, MPI_MIN, 0, MPI_COMM_WORLD);
if (my_rank == 0)
printf("distance between A and B is %d\n", final_min_distance);
MPI_Finalize();
return 0;
}
int calcDistance(int *A,int *B,int n,int skip){
int distance = 0;
for(int i = 0 , int j = 0 ; i<n+1;i++)
if(i == skip){
continue;
if (B[i] != A[j])
distance += 1;
j++;
}
return distance;
}Editor is loading...
Leave a Comment