Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<mpi.h>
- #include<stdio.h>
- #include<stdlib.h>
- #include<math.h>
- #include<string.h>
- #define N 10
- const int data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
- int main(int argc, char * argv[]) {
- int rank;
- int nProcesses;
- int *v = (int*)malloc(sizeof(int)*N);
- int *aux = (int*)calloc(N, sizeof(int));
- v = (int*)memcpy(v, data, sizeof(data));
- MPI_Init(&argc, &argv);
- MPI_Comm_rank(MPI_COMM_WORLD, &rank);
- MPI_Comm_size(MPI_COMM_WORLD, &nProcesses);
- printf("Hello from %i/%i\n", rank, nProcesses);
- int i;
- int *local = (int*)malloc(sizeof(int) * N / nProcesses);
- MPI_Scatter(aux, N / nProcesses, MPI_INT, local, N / nProcesses, MPI_INT, 0, MPI_COMM_WORLD);
- int local_startIndex = rank * N / nProcesses;
- int local_stopIndex = (rank + 1) * N / nProcesses;
- printf("rank = %i startIndex = %i stopIndex = %i\n", rank, local_startIndex, local_stopIndex);
- int j, k;
- for( j = local_startIndex; j < local_stopIndex; j++ ) {
- for( k = 0; k < N; k++ ) {
- if (v[j] > v[k]) {
- local[j]++;
- // printf("local[%d] = %d\n", j, local[j]);
- }
- }
- }
- MPI_Gather(local, N / nProcesses, MPI_INT, aux, N / nProcesses, MPI_INT, 0, MPI_COMM_WORLD);
- if(rank == 0) {
- for(i = 0; i < N; i++) {
- printf("%d ", v[local[i]]);
- }
- }
- printf("\n");
- free(aux);
- free(v);
- MPI_Finalize();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement