MC.cpp
Go to the documentation of this file.
1 // Copyright (C) 2000, International Business Machines
2 // Corporation and others. All Rights Reserved.
3 
4 #include "BCP_buffer.hpp"
5 
6 #include "MC.hpp"
7 #include <cmath>
8 
11 {
12  int i, j;
14  for (i = 0; i < num_edges; ++i) {
15  buf.pack(edges[i].tail).pack(edges[i].head).pack(edges[i].cost);
16  }
17 
18  const int grid = static_cast<int>(sqrt(num_nodes + 1.0));
19  const int num_grid_nodes = grid * grid;
20 
21  buf.pack(ising_problem);
22  const bool has_four_cycles = ising_four_cycles != NULL;
23  buf.pack(has_four_cycles);
24  if (has_four_cycles)
25  buf.pack(ising_four_cycles, 4 * num_grid_nodes);
26 
27  const bool has_triangles = ising_triangles != NULL;
28  buf.pack(has_triangles);
29  if (has_triangles)
30  buf.pack(ising_triangles, 6 * num_grid_nodes);
31 
33  if (num_structure_type > 0) {
35  for (i = 0; i < num_structure_type; ++i) {
36  const MC_switch_structure* sw_structs = switch_structures[i];
37  for (j = 0; j < num_switch_structures[i]; ++j) {
38  const int num_nodes_j = sw_structs[j].num_nodes;
39  const int num_neighbors_j = sw_structs[j].num_neighbors;
40  buf.pack(num_nodes_j).pack(num_neighbors_j);
41  buf.pack(sw_structs[j].nodes, num_nodes_j + num_neighbors_j);
42  }
43  }
44  }
45 
46  const bool has_feas_sol = feas_sol != 0;
47  buf.pack(has_feas_sol);
48  if (has_feas_sol) {
51  .pack(feas_sol->cost);
52  }
53 
54  return buf;
55 }
56 
59 {
60  int i, j;
63  for (i = 0; i < num_edges; ++i) {
64  buf.unpack(edges[i].tail).unpack(edges[i].head).unpack(edges[i].cost);
65  }
66 
67  int itmp;
68 
69  buf.unpack(ising_problem);
70  bool has_four_cycles;
71  buf.unpack(has_four_cycles);
72  if (has_four_cycles)
73  buf.unpack(ising_four_cycles, itmp, true /* allocate */);
74 
75  bool has_triangles;
76  buf.unpack(has_triangles);
77  if (has_triangles)
78  buf.unpack(ising_triangles, itmp, true /* allocate */);
79 
81  if (num_structure_type > 0) {
82  buf.unpack(num_switch_structures, itmp, true /* allocate */);
84  for (i = 0; i < num_structure_type; ++i) {
85  if (num_switch_structures[i] > 0) {
88  int num_nodes_j;
89  int num_neighbors_j;
90  MC_switch_structure* sw_structs = switch_structures[i];
91  for (j = 0; j < num_switch_structures[i]; ++j) {
92  buf.unpack(num_nodes_j).unpack(num_neighbors_j);
93  sw_structs[j].num_nodes = num_nodes_j;
94  sw_structs[j].num_neighbors = num_neighbors_j;
95  buf.unpack(sw_structs[j].nodes, itmp, true /* allocate */);
96  sw_structs[j].neighbors = sw_structs[j].nodes + num_nodes_j;
97  }
98  }
99  }
100  }
101 
102 
103  bool has_feas_sol;
104  buf.unpack(has_feas_sol);
105  if (has_feas_sol) {
106  int n, m, *s;
107  double c, *v;
108  buf.unpack(s, n).unpack(v, m).unpack(c);
109  feas_sol = new MC_feas_sol(c, n, s, m, v);
110  }
111  return buf;
112 }
113 
114 void
116 {
117  int i;
118 
120  for (i = 0; i < num_nodes; ++i)
121  nodes[i].degree = 0;
122  for (i = 0; i < num_edges; ++i) {
123  ++nodes[edges[i].tail].degree;
124  ++nodes[edges[i].head].degree;
125  }
126 
128 
129  int total_degree = 0;
130  for (i = 0; i < num_nodes; ++i) {
131  nodes[i].adj_list = all_adj_list + total_degree;
132  total_degree += nodes[i].degree;
133  }
134 
135  for (i = 0; i < num_nodes; ++i)
136  nodes[i].degree = 0;
137 
138  for (i = 0; i < num_edges; ++i) {
139  const int t = edges[i].tail;
140  const int h = edges[i].head;
141  const double c = edges[i].cost;
142  const int degt = nodes[t].degree;
143  const int degh = nodes[h].degree;
144  nodes[t].adj_list[degt].neighbor = h;
145  nodes[h].adj_list[degh].neighbor = t;
146  nodes[t].adj_list[degt].cost = c;
147  nodes[h].adj_list[degh].cost = c;
148  nodes[t].adj_list[degt].index = i;
149  nodes[h].adj_list[degh].index = i;
150  ++nodes[t].degree;
151  ++nodes[h].degree;
152  }
153 }
int * ising_four_cycles
Definition: MC.hpp:105
double cost
Definition: MC.hpp:31
BCP_buffer & pack(const T &value)
Pack a single object of type T.
Definition: BCP_buffer.hpp:177
int num_edges
Definition: MC.hpp:92
BCP_buffer & unpack(T &value)
Unpack a single object of type T.
Definition: BCP_buffer.hpp:186
void create_adj_lists()
Definition: MC.cpp:115
int head
Definition: MC.hpp:30
int neighbor
Definition: MC.hpp:12
int num_nodes
Definition: MC.hpp:91
MC_graph_edge * edges
Definition: MC.hpp:95
MC_adjacency_entry * all_adj_list
Definition: MC.hpp:97
int * sign
Definition: MC.hpp:58
BCP_buffer & unpack(BCP_buffer &buf)
Definition: MC.cpp:58
int * ising_triangles
Definition: MC.hpp:107
int num_edges
Definition: MC.hpp:59
static char * j
Definition: OSdtoa.cpp:3622
int tail
Definition: MC.hpp:29
MC_switch_structure ** switch_structures
Definition: MC.hpp:111
bool ising_problem
Definition: MC.hpp:99
int degree
Definition: MC.hpp:21
double cost
Definition: MC.hpp:56
MC_graph_node * nodes
Definition: MC.hpp:96
double * value
Definition: MC.hpp:60
MC_feas_sol * feas_sol
Definition: MC.hpp:114
void fint fint fint fint fint fint fint fint fint fint real real real real real real real real * s
Definition: MC.hpp:10
int num_neighbors
Definition: MC.hpp:43
int index
Definition: MC.hpp:13
This class describes the message buffer used for all processes of BCP.
Definition: BCP_buffer.hpp:39
int num_structure_type
Definition: MC.hpp:109
MC_adjacency_entry * adj_list
Definition: MC.hpp:22
int * num_switch_structures
Definition: MC.hpp:110
void fint * m
static char * t
Definition: OSdtoa.cpp:3645
void fint * n
double cost
Definition: MC.hpp:14
real c
BCP_buffer & pack(BCP_buffer &buf)
Definition: MC.cpp:10
int * neighbors
Definition: MC.hpp:45
int num_nodes
Definition: MC.hpp:57