Using boost::mpi to take the Euclidean norm of a vector I am learning MPI (via boost::mpi) and wrote this program that takes the euclidean norm of a 1-D vector (represented as an array here.) I want to make sure that I am using boost::mpi properly, in terms of both functions used and the way the code is parallelized.

The sample below is the program with just the norm functionality; it takes no input from the user and always uses the same seed for consistency reasons. Another limitation is that the vector size must be evenly divisible into the MPI world size.

I appreciate any comments and am thankful for your time.

#include &lt;boost/mpi.hpp&gt;
#include &lt;chrono&gt;
#include &lt;cmath&gt;
#include &lt;iostream&gt;
#include &lt;numeric&gt;
#include &lt;random&gt;
#include &lt;utility&gt;
#include &lt;vector&gt;

int main() {
  boost::mpi::environment env;
  boost::mpi::communicator world;
  //unsigned long TWIST_SEED = std::chrono::system_clock::now().time_since_epoch().count();
  unsigned long TWIST_SEED = 2;
  unsigned long VECT_SIZE = 10;
  double *main_vector = new double[VECT_SIZE];
  if (world.rank() == 0) {
    std::mt19937 generator(TWIST_SEED);
    std::uniform_real_distribution&lt;double&gt; dis(-222222.22, 222222.22);
    for (int i = 0; i &lt; VECT_SIZE; i++) {
      main_vector[i] = dis(generator);
    }
  }
  int n_bar = VECT_SIZE / world.size();
  if (world.rank() == 0) {
    if (VECT_SIZE &lt;= 10) {
      std::cout &lt;&lt; "Your vector is small enough to output. It is: \n{";
      for (int i = 0; i &lt; VECT_SIZE; i++) {
        if (i == VECT_SIZE - 1) {
          std::cout &lt;&lt; main_vector[i];
        } else {
          std::cout &lt;&lt; main_vector[i] &lt;&lt; ", ";
        }
      }
      std::cout &lt;&lt; "}" &lt;&lt; std::endl;
    }
  }
  double *local_vector = new double[n_bar];
  boost::mpi::scatter(world, main_vector, local_vector, n_bar, 0);
  std::vector&lt;double&gt; dot;
  double local_dot = 0.0;
  for (int i = 0; i &lt; n_bar; i++) {
    local_dot += local_vector[i] * local_vector[i];
  }
  boost::mpi::gather(world, local_dot, dot, 0);
  if (world.rank() == 0) {
    std::cout &lt;&lt; "The norm is " &lt;&lt; sqrt(std::accumulate(dot.begin(), dot.end(), 0.0)) &lt;&lt; std::endl;
  }
  delete[] main_vector;
  return 0;
}
You've misspelt std::sqrt.

Calculation of 2D and 3D Euclidean norm is more accurate and efficient via std::hypot() - would it be reasonable to use that as a basis for the N-dimensional form?

As always, prefer standard containers such as std::vector over raw pointers created with new[].