Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <time.h>
- #include "mpi.h"
- #include "omp.h"
- #define SIZE 10
- #define NUMBER_TO_FIND 1
- #define RANGE 3
- void create_array(int **array, int size)
- {
- *array = (int*) malloc(size * sizeof(int));
- for (int i = 0; i < size; ++i)
- (*array)[i] = -1;
- }
- void fill_array(int *array, int size) {
- for (int i = 0; i < SIZE; ++i)
- array[i] = rand() % RANGE;
- }
- void print_array(int* array, int size)
- {
- for (int i = 0; i < size; ++i)
- printf("%d ", array[i]);
- printf("\n");
- }
- void print_result(int* array, int size)
- {
- for (int i = 0; i < size; ++i)
- if (array[i] != -1)
- printf("%d ", array[i]);
- printf("\n");
- }
- void find(int* array, int* result, int num, int starting_point, int size)
- {
- int pos = -1;
- #pragma omp paralel for shared(array, result) reduction(+:pos)
- for (int i = 0; i < size; ++i)
- if (array[i] == num) {
- pos += 1;
- result[pos] = starting_point + i;
- }
- }
- int main(int argc, char *argv[])
- {
- srand(time(NULL));
- int rank, comm_size;
- MPI_Init(&argc, &argv);
- MPI_Comm_size(MPI_COMM_WORLD, &comm_size);
- MPI_Comm_rank(MPI_COMM_WORLD, &rank);
- int *global_array, *global_result;
- int *local_array, *local_result;
- int local_size = SIZE / comm_size;
- if (rank == comm_size-1)
- local_size += SIZE % comm_size;
- global_array = NULL;
- global_result = NULL;
- if (rank == 0)
- {
- printf("Array\n");
- create_array(&global_array, SIZE);
- fill_array(global_array, SIZE);
- print_array(global_array, SIZE);
- create_array(&global_result, SIZE);
- }
- create_array(&local_array, local_size);
- create_array(&local_result, local_size);
- int counts[comm_size];
- int displacements[comm_size];
- if (rank == 0)
- {
- for (int i = 0; i < comm_size; ++i)
- {
- if (i == comm_size-1)
- counts[i] = local_size + (SIZE % comm_size);
- else
- counts[i] = local_size;
- if (i == 0)
- displacements[i] = 0;
- else
- displacements[i] = displacements[i-1] + counts[i-1];
- }
- }
- MPI_Scatterv( global_array, counts, displacements, MPI_INT,
- &(local_array[0]), local_size, MPI_INT,
- 0, MPI_COMM_WORLD );
- // for (int i = 0; i < comm_size; ++i)
- // {
- // if (rank == i){
- // printf("RANK: %d\n", rank);
- // print_array(local_array, local_size);
- // }
- // MPI_Barrier(MPI_COMM_WORLD);
- // }
- // MPI_Barrier(MPI_COMM_WORLD);
- int starting_point;
- MPI_Scatter(displacements, 1, MPI_INT, &starting_point, 1, MPI_INT, 0, MPI_COMM_WORLD);
- // printf("RANK: %d, ST: %d\n", rank, starting_point);
- // MPI_Barrier(MPI_COMM_WORLD);
- find(local_array, local_result, NUMBER_TO_FIND, starting_point, local_size);
- MPI_Barrier(MPI_COMM_WORLD);
- MPI_Gatherv( &(local_result[0]), local_size, MPI_INT,
- global_result, counts, displacements, MPI_INT,
- 0, MPI_COMM_WORLD );
- if (rank == 0){
- printf("%d found at positions:\n", NUMBER_TO_FIND);
- print_result(global_result, SIZE);
- }
- MPI_Finalize();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement