COMBINATORIAL_BLAS  1.6
MatchingDefs.h
Go to the documentation of this file.
1 //
2 // MatchingDefs.h
3 //
4 //
5 // Created by Ariful Azad on 8/22/17.
6 //
7 //
8 
9 #ifndef MatchingDefs_h
10 #define MatchingDefs_h
11 
12 #include "../CombBLAS.h"
13 #include <iostream>
14 
15 namespace combblas {
16 
17 // Vertex data structure for maximal cardinality matching
18 template <typename T1, typename T2>
20 {
21 public:
22  VertexTypeML(T1 p=-1, T2 com=0){parent=p; comp = com; };
23 
24  friend bool operator<(const VertexTypeML & vtx1, const VertexTypeML & vtx2 )
25  {
26  if(vtx1.comp==vtx2.comp) return vtx1.parent<vtx2.parent;
27  else return vtx1.comp<vtx2.comp;
28  };
29  friend bool operator==(const VertexTypeML & vtx1, const VertexTypeML & vtx2 ){return (vtx1.parent==vtx2.parent) & (vtx1.comp==vtx2.comp);};
30  friend std::ostream& operator<<(std::ostream& os, const VertexTypeML & vertex ){os << "(" << vertex.parent << "," << vertex.comp << ")"; return os;};
31  T1 parent;
32  T2 comp; // can be index, probability, degree or an adjacent edge weight
33 };
34 
35 
36 
37 // Vertex data structure for maximum cardinality matching
38 template <typename IT>
40 {
41 public:
42  VertexTypeMM(IT p=-1, IT r=-1, double w=0){parent=p; root = r; comp = w;};
43  friend bool operator<(const VertexTypeMM & vtx1, const VertexTypeMM & vtx2 )
44  {
45  if(vtx1.comp==vtx2.comp)
46  {
47  if(vtx1.parent==vtx2.parent)
48  return vtx1.root<vtx2.root;
49  else return vtx1.parent<vtx2.parent;
50  }
51  else return vtx1.comp<vtx2.comp;
52 
53  };
54  friend bool operator==(const VertexTypeMM & vtx1, const VertexTypeMM & vtx2 ){return vtx1.parent==vtx2.parent;};
55  friend std::ostream& operator<<(std::ostream& os, const VertexTypeMM & vertex ){os << "(" << vertex.parent << "," << vertex.root << ","<< vertex.comp << ")"; return os;};
56  IT parent;
57  IT root;
58  double comp; // probability of selecting an edge or weight of an adjacent edge
59  //making it double so that we can always use edge weights or probability
60  // TODO: this is an overkill for Boolean matrices. Think a better way to cover the Boolean case
61 };
62 
63 // Semiring needed to compute degrees within a subgraph
64 template <typename T1, typename T2>
66 {
67  static T2 id(){ return 1; };
68  static bool returnedSAID() { return false; }
69  static MPI_Op mpi_op() { return MPI_SUM; };
70 
71  static T2 add(const T2 & arg1, const T2 & arg2)
72  {
73  return std::plus<T2>()(arg1, arg2);
74  }
75 
76  static T2 multiply(const T1 & arg1, const T2 & arg2)
77  {
78  return static_cast<T2> (1); // note: it is not called on a Boolean matrix
79  }
80 
81  static void axpy(const T1 a, const T2 & x, T2 & y)
82  {
83  y = add(y, multiply(a, x));
84  }
85 };
86 
87 
88 // Usual semiring used in maximal and maximum matching
89 template <typename T1, typename T2>
90 struct Select2ndMinSR
91 {
92  static T2 id(){ return T2(); };
93  static bool returnedSAID() { return false; }
94  static MPI_Op mpi_op() { return MPI_MIN; };
95 
96  static T2 add(const T2 & arg1, const T2 & arg2)
97  {
98  return std::min(arg1, arg2);
99  }
100 
101  static T2 multiply(const T1 & arg1, const T2 & arg2)
102  {
103  return arg2;
104  }
105 
106  static void axpy(const T1 a, const T2 & x, T2 & y)
107  {
108  y = add(y, multiply(a, x));
109  }
110 };
111 
112 // Designed to pseudo maximize weights on a maximal matching
113 template <typename T1, typename T2>
115 {
116  static T2 id(){ return T2(-1, std::numeric_limits<T1>::lowest()); };
117  static bool returnedSAID() { return false; }
118  static MPI_Op mpi_op() { return MPI_MAX; };
119 
120  static T2 add(const T2 & arg1, const T2 & arg2)
121  {
122  return std::max(arg1, arg2);
123  }
124 
125  static T2 multiply(const T1 & arg1, const T2 & arg2)
126  {
127  return T2(arg2.parent, arg1);
128  }
129 
130  static void axpy(const T1 a, const T2 & x, T2 & y)
131  {
132  y = add(y, multiply(a, x));
133  }
134 };
135 
136 
137 
138 // Designed to pseudo maximize weights on the augmenting paths
139 // for boolean matrix (T1 <=> bool), this semiring converts to Select2ndMax semiring
140 template <typename T1, typename T2>
142 {
143  static T2 id(){ return T2(-1, -1, std::numeric_limits<T1>::lowest()); };
144  static bool returnedSAID() { return false; }
145  static MPI_Op mpi_op() { return MPI_MAX; };
146 
147  static T2 add(const T2 & arg1, const T2 & arg2)
148  {
149  return std::max(arg1, arg2);
150  }
151 
152  static T2 multiply(const T1 & arg1, const T2 & arg2)
153  {
154  return T2(arg2.parent, arg2.root, arg1);
155  }
156 
157 
158  static void axpy(const T1 a, const T2 & x, T2 & y)
159  {
160  y = add(y, multiply(a, x));
161  }
162 };
163 
164 }
165 
166 #endif /* MatchingDefs_h */
static T2 multiply(const T1 &arg1, const T2 &arg2)
Definition: MatchingDefs.h:152
static bool returnedSAID()
Definition: MatchingDefs.h:144
friend bool operator<(const VertexTypeMM &vtx1, const VertexTypeMM &vtx2)
Definition: MatchingDefs.h:43
friend bool operator==(const VertexTypeMM &vtx1, const VertexTypeMM &vtx2)
Definition: MatchingDefs.h:54
VertexTypeML(T1 p=-1, T2 com=0)
Definition: MatchingDefs.h:22
static MPI_Op mpi_op()
Definition: MatchingDefs.h:94
static T2 add(const T2 &arg1, const T2 &arg2)
Definition: MatchingDefs.h:71
static MPI_Op mpi_op()
Definition: MatchingDefs.h:145
static void axpy(const T1 a, const T2 &x, T2 &y)
Definition: MatchingDefs.h:130
static bool returnedSAID()
Definition: MatchingDefs.h:93
static void axpy(const T1 a, const T2 &x, T2 &y)
Definition: MatchingDefs.h:81
static T2 multiply(const T1 &arg1, const T2 &arg2)
Definition: MatchingDefs.h:76
VertexTypeMM(IT p=-1, IT r=-1, double w=0)
Definition: MatchingDefs.h:42
static void axpy(const T1 a, const T2 &x, T2 &y)
Definition: MatchingDefs.h:158
static bool returnedSAID()
Definition: MatchingDefs.h:117
static void axpy(const T1 a, const T2 &x, T2 &y)
Definition: MatchingDefs.h:106
friend bool operator<(const VertexTypeML &vtx1, const VertexTypeML &vtx2)
Definition: MatchingDefs.h:24
friend bool operator==(const VertexTypeML &vtx1, const VertexTypeML &vtx2)
Definition: MatchingDefs.h:29
static T2 multiply(const T1 &arg1, const T2 &arg2)
Definition: MatchingDefs.h:125
static MPI_Op mpi_op()
Definition: MatchingDefs.h:118
static MPI_Op mpi_op()
Definition: MatchingDefs.h:69
static bool returnedSAID()
Definition: MatchingDefs.h:68
static T2 add(const T2 &arg1, const T2 &arg2)
Definition: MatchingDefs.h:147
static T2 add(const T2 &arg1, const T2 &arg2)
Definition: MatchingDefs.h:96
friend std::ostream & operator<<(std::ostream &os, const VertexTypeML &vertex)
Definition: MatchingDefs.h:30
static T2 multiply(const T1 &arg1, const T2 &arg2)
Definition: MatchingDefs.h:101
Definition: CCGrid.h:4
static T2 add(const T2 &arg1, const T2 &arg2)
Definition: MatchingDefs.h:120
SpDCCols< IT, NT > * multiply(SpDCCols< IT, NT > &splitA, SpDCCols< IT, NT > &splitB, CCGrid &CMG, bool isBT, bool threaded)
Definition: Multiplier.h:11