0

Problem is in my misunderstanding on how boost MPI wrappers work. I am using Microsoft MPI implementation. In this code I am trying to scatter std::vector to processes but get a debug assertion vector subscript out of range error.

This code is my bootstrap to implement hypersort algorithm using boost wrappers and Microsoft MPI.

I tried examples from boost libraries but got the same assertion. Also I tried to include both <vector> and <boost/serialization/vector> but it didn't help. I run my program on Windows 10 using boost 1.70 and latest version of Microsoft MPI.

#pragma once

#include <boost/mpi.hpp>
#include <boost/mpi/collectives.hpp>
#include <boost/serialization/vector.hpp>

#include "qsort.hpp"
#include "utils.hpp"

namespace algo {
namespace mpi_extension {

namespace mpi = boost::mpi;

void hyperqsort_v1(int argc, char* argv[]) {
    mpi::environment env(argc, argv);
    mpi::communicator world;
    int value = 0;
    int recv_value = 0;

    std::vector<int> unsorted_list{5, 3, 6, 2, 9, 1, 10, 7};
    auto distribuion_number = unsorted_list.size() / world.size();
    std::vector<std::vector<int>> unsorted_dist_list;
    if(0 == world.rank()) {
        for(size_t j = 0; j < world.size(); ++j) {
            for(size_t k = 0 + j; k < distribuion_number + j; ++k) {
                unsorted_dist_list[j].push_back(unsorted_list[j + k]);
            }
        }

    }
    std::vector<int> recv_vector;
    recv_vector.resize(distribuion_number);
    mpi::scatter(
        world, unsorted_dist_list, recv_vector, 0);
}

} // namespace mpi_extension
} // namespace algo

qsort.hpp - sequential implementation of qsort algorithm

I expect all processes in communicator have their piece of unsorted list.

I can produce this error only in Debug build

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
powercat
  • 99
  • 4
  • 2
    ***I can produce this error only in Debug build*** I think that is because in release mode the check for out of bounds is not present. You still have some bug. – drescherjm Apr 21 '19 at 14:03
  • 1
    The first step is to have your debugger tell you what line contains the out of bounds access. – drescherjm Apr 21 '19 at 14:04

2 Answers2

1

You only get that error in debug builds because the Microsoft compiler only includes that check in debug builds. The problem still exists in a Release build.

If world.rank() is zero, unsorted_dist_list will be an empty vector when you use it in unsorted_dist_list[j]. At a minimum, you should add

unsorted_dist_list.resize(world.size());

before the for (size_t j = 0; loop.

If world.rank() is not zero, unsorted_dist_list will be empty when passed to mpi::scatter.

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
-2

Correct way is to use std::vector::data() to get the address of first element. May be it is specific only for Microsoft MPI implementation.

Example without rising debug assertion: mpiexec -n 4

mpi::environment env;
mpi::communicator world;

std::vector<int> unsorted_list{11, 36, 44, 50, 53, 67, 86, 95};
std::vector<int> list;
list.resize(2);

mpi::scatter(world, unsorted_list.data(), list.data(), 2, 0);
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
powercat
  • 99
  • 4