Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdlib>
- #include <iostream>
- #include <sstream>
- #include <vector>
- #include "mpi.h"
- using namespace std;
- void set_time(int rank) {
- srand(rank);
- }
- int main(int argc, char** argv)
- {
- if (argc != 2) {
- cerr << "Invalid count of arguments";
- } else {
- size_t n;
- stringstream convert1(argv[1]);
- if (!(convert >> n))
- n = DEFAULT_N;
- size_t a;
- stringstream convert1(argv[2]);
- if (!(convert >> a))
- a = DEFAULT_A;
- size_t b;
- stringstream convert1(argv[3]);
- if (!(convert >> b))
- b = DEFAULT_B;
- //process init
- int size, rank;
- MPI_Status status;
- MPI_Init(&argc, &argv);
- MPI_Comm_size(MPI_COMM_WORLD, &size);
- MPI_Comm_rank(MPI_COMM_WORLD, &rank);
- set_time(rank);
- int block_size = n / size + (rank < n % size); // difference between procs <= 1 element
- bool is_wa = true;
- int cnt_in_segment = 0;
- bool curr_is_wa = true;
- int curr_cnt_in_segment = 0;
- int *arr = new int[block_size];
- for (int i = 0; i < block_size; ++i) {
- arr[i] = rand();
- }
- for (int i = 0; i < block_size; ++i) {
- if ((i > 0) && (arr[i - 1] > arr[i])) {
- curr_is_wa = false;
- }
- if (a <= arr[i] && arr[i] <= b) {
- ++curr_cnt_in_segment;
- }
- }
- int lb;
- MPI_Request requests[(rank > 0) * 2];
- if (rank > 0) {
- MPI_Isend(arr[0], 1, MPI_INT, std::max(rank - 1, 0), 0, MPI_COMM_WORLD, &requests[0]);
- MPI_Irecv(lb, 1, MPI_INT, std::max(rank - 1, 0), 0, MPI_COMM_WORLD, &requests[1]);
- if (lb > arr[0]) {
- is_wa = false;
- }
- }
- delete [] arr;
- MPI_Reduce(&curr_cnt_in_segment, &cnt_in_segment, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
- MPI_Reduce(&curr_is_wa, &is_wa, 1, MPI_BOOL, MPI_LAND, 0, MPI_COMM_WORLD);
- MPI_Finalize();
- if (rank == 0) {
- if (is_wa) {
- cout << "Is weakly ascending. Numbers in [a:b]: " << cnt_in_segment;
- } else {
- return -1;
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement