File: distributed_queue_test.cpp

package info (click to toggle)
boost1.88 1.88.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 576,932 kB
  • sloc: cpp: 4,149,234; xml: 136,789; ansic: 35,092; python: 33,910; asm: 5,698; sh: 4,604; ada: 1,681; makefile: 1,633; pascal: 1,139; perl: 1,124; sql: 640; yacc: 478; ruby: 271; java: 77; lisp: 24; csh: 6
file content (117 lines) | stat: -rw-r--r-- 3,254 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright (C) 2004-2006 The Trustees of Indiana University.

// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

//  Authors: Douglas Gregor
//           Andrew Lumsdaine
#include <boost/graph/use_mpi.hpp>
#include <boost/config.hpp>
#include <boost/throw_exception.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/graph/distributed/queue.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/graph/distributed/mpi_process_group.hpp>
#include <boost/pending/queue.hpp>
#include <boost/property_map/property_map.hpp>
#include <boost/property_map/parallel/parallel_property_maps.hpp>
#include <utility>
#include <iostream>

#ifdef BOOST_NO_EXCEPTIONS
void
boost::throw_exception(std::exception const& ex)
{
    std::cout << ex.what() << std::endl;
    abort();
}
#endif

using boost::graph::distributed::mpi_process_group;

struct global_value 
{
  global_value(int p = -1, std::size_t l = 0) : processor(p), value(l) {}

  int processor;
  std::size_t value;

  template<typename Archiver>
  void serialize(Archiver& ar, const unsigned int /*version*/)
  {
    ar & processor & value;
  }
};

namespace boost { namespace mpi {
    template<> struct is_mpi_datatype<global_value> : mpl::true_ { };
} } // end namespace boost::mpi

BOOST_IS_BITWISE_SERIALIZABLE(global_value)
BOOST_CLASS_IMPLEMENTATION(global_value,object_serializable)
BOOST_CLASS_TRACKING(global_value,track_never)

inline bool operator==(const global_value& x, const global_value& y)
{ return x.processor == y.processor && x.value == y.value; }

struct global_value_owner_map
{
  typedef int value_type;
  typedef value_type reference;
  typedef global_value key_type;
  typedef boost::readable_property_map_tag category;
};

int get(global_value_owner_map, global_value k)
{
  return k.processor;
}

void test_distributed_queue()
{
  mpi_process_group process_group;

  typedef boost::queue<global_value> local_queue_type;

  typedef boost::graph::distributed::distributed_queue<mpi_process_group, 
                                             global_value_owner_map,
                                             local_queue_type> dist_queue_type;

  dist_queue_type Q(process_group, global_value_owner_map());

  mpi_process_group::process_id_type id = process_id(process_group),
    n = num_processes(process_group);
  global_value v(0, 0);

  if (id == 0) {
    std::cerr << "Should print level of each processor in a binary tree:\n";
  }
  synchronize(process_group);

  if (id == n-1) Q.push(v);
  while (!Q.empty()) {
    v = Q.top(); Q.pop();
    
    std::cerr << "#" << id << ": level = " << v.value << std::endl;

    int level_begin = 1;
    for (std::size_t i = 0; i < v.value; ++i) level_begin *= 2;
    int level_end = level_begin * 2;
    BOOST_TEST(level_begin <= (id + 1));
    BOOST_TEST((id + 1) <= level_end);

    ++v.value;
    v.processor = v.processor * 2 + 1;
    if (v.processor < n) Q.push(v);
    ++v.processor;
    if (v.processor < n) Q.push(v);
  }
}

int main(int argc, char** argv)
{
  boost::mpi::environment env(argc, argv);
  test_distributed_queue();
  return boost::report_errors();
}