COMBINATORIAL_BLAS  1.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SpTuples.h
Go to the documentation of this file.
1 /****************************************************************/
2 /* Parallel Combinatorial BLAS Library (for Graph Computations) */
3 /* version 1.1 -------------------------------------------------*/
4 /* date: 12/25/2010 --------------------------------------------*/
5 /* authors: Aydin Buluc (abuluc@lbl.gov), Adam Lugowski --------*/
6 /****************************************************************/
7 
8 #ifndef _SP_TUPLES_H
9 #define _SP_TUPLES_H
10 
11 #include <iostream>
12 #include <fstream>
13 #include <cmath>
14 #include <cassert>
15 #include "CombBLAS.h"
16 #include "SpMat.h"
17 #include "SpDefs.h"
18 #include "StackEntry.h"
19 #include "Compare.h"
20 using namespace std;
21 
22 template <class IU, class NU>
23 class SpDCCols;
24 
25 template <class IU, class NU>
26 class Dcsc;
27 
28 
36 template <class IT, class NT>
37 class SpTuples: public SpMat<IT, NT, SpTuples<IT,NT> >
38 {
39 public:
40  // Constructors
41  SpTuples (int64_t size, IT nRow, IT nCol);
42  SpTuples (int64_t size, IT nRow, IT nCol, tuple<IT, IT, NT> * mytuples);
43  SpTuples (int64_t maxnnz, IT nRow, IT nCol, vector<IT> & edges, bool removeloops = true); // Graph500 contructor
44  SpTuples (int64_t size, IT nRow, IT nCol, StackEntry<NT, pair<IT,IT> > * & multstack);
45  SpTuples (const SpTuples<IT,NT> & rhs); // Actual Copy constructor
46  SpTuples (const SpDCCols<IT,NT> & rhs); // Copy constructor for conversion from SpDCCols
47  ~SpTuples();
48 
49  SpTuples<IT,NT> & operator=(const SpTuples<IT,NT> & rhs);
50 
51  IT & rowindex (IT i) { return joker::get<0>(tuples[i]); }
52  IT & colindex (IT i) { return joker::get<1>(tuples[i]); }
53  NT & numvalue (IT i) { return joker::get<2>(tuples[i]); }
54 
55  IT rowindex (IT i) const { return joker::get<0>(tuples[i]); }
56  IT colindex (IT i) const { return joker::get<1>(tuples[i]); }
57  NT numvalue (IT i) const { return joker::get<2>(tuples[i]); }
58 
59  void SortRowBased()
60  {
61  RowLexiCompare<IT,NT> rowlexicogcmp;
62  if(!SpHelper::is_sorted(tuples, tuples+nnz, rowlexicogcmp))
63  sort(tuples , tuples+nnz, rowlexicogcmp);
64 
65  // Default "operator<" for tuples uses lexicographical ordering
66  // However, cray compiler complains about it, so we use rowlexicogcmp
67  }
68 
69  void SortColBased()
70  {
71  ColLexiCompare<IT,NT> collexicogcmp;
72  if(!SpHelper::is_sorted(tuples, tuples+nnz, collexicogcmp))
73  sort(tuples , tuples+nnz, collexicogcmp );
74  }
75 
79  IT RemoveLoops()
80  {
81  IT loop = 0;
82  for(IT i=0; i< nnz; ++i)
83  {
84  if(joker::get<0>(tuples[i]) == joker::get<1>(tuples[i])) ++loop;
85  }
86  tuple<IT, IT, NT> * ntuples = new tuple<IT,IT,NT>[nnz-loop];
87 
88  IT ni = 0;
89  for(IT i=0; i< nnz; ++i)
90  {
91  if(joker::get<0>(tuples[i]) != joker::get<1>(tuples[i]))
92  {
93  ntuples[ni++] = tuples[i];
94  }
95  }
96  delete [] tuples;
97  tuples = ntuples;
98  nnz = nnz-loop;
99  return loop;
100  }
101 
102  pair<IT,IT> RowLimits()
103  {
104  if(nnz > 0)
105  {
106  RowCompare<IT,NT> rowcmp;
107  tuple<IT,IT,NT> * maxit = max_element(tuples, tuples+nnz, rowcmp);
108  tuple<IT,IT,NT> * minit = min_element(tuples, tuples+nnz, rowcmp);
109  return make_pair(joker::get<0>(*minit), joker::get<0>(*maxit));
110  }
111  else
112  return make_pair(0,0);
113  }
114  pair<IT,IT> ColLimits()
115  {
116  if(nnz > 0)
117  {
118  ColCompare<IT,NT> colcmp;
119  tuple<IT,IT,NT> * maxit = max_element(tuples, tuples+nnz, colcmp);
120  tuple<IT,IT,NT> * minit = min_element(tuples, tuples+nnz, colcmp);
121  return make_pair(joker::get<1>(*minit), joker::get<1>(*maxit));
122  }
123  else
124  return make_pair(0,0);
125  }
126  tuple<IT, IT, NT> front() { return tuples[0]; };
127  tuple<IT, IT, NT> back() { return tuples[nnz-1]; };
128 
129  // Performs a balanced merge of the array of SpTuples
130  template<typename SR, typename IU, typename NU>
131  friend SpTuples<IU,NU> MergeAll(const vector<SpTuples<IU,NU> *> & ArrSpTups, IU mstar, IU nstar, bool delarrs);
132 
133  template<typename SR, typename IU, typename NU>
134  friend SpTuples<IU,NU> * MergeAllRec(const vector<SpTuples<IU,NU> *> & ArrSpTups, IU mstar, IU nstar);
135 
136  ofstream& putstream (ofstream& outfile) const;
137  ifstream& getstream (ifstream& infile);
138 
139  bool isZero() const { return (nnz == 0); }
140  IT getnrow() const { return m; }
141  IT getncol() const { return n; }
142  int64_t getnnz() const { return nnz; }
143 
144  void PrintInfo();
145 
146 private:
147  tuple<IT, IT, NT> * tuples; // boost:tuple
155  IT m;
156  IT n;
157  int64_t nnz;
158 
159  SpTuples (){}; // Default constructor does nothing, hide it
160 
161  void FillTuples (Dcsc<IT,NT> * mydcsc);
162 
163  template <class IU, class NU>
164  friend class SpDCCols;
165 };
166 
167 
168 // At this point, complete type of of SpTuples is known, safe to declare these specialization (but macros won't work as they are preprocessed)
169 template <> struct promote_trait< SpTuples<int,int> , SpTuples<int,int> >
170  {
172  };
173 template <> struct promote_trait< SpTuples<int,float> , SpTuples<int,float> >
174  {
176  };
177 template <> struct promote_trait< SpTuples<int,double> , SpTuples<int,double> >
178  {
180  };
181 template <> struct promote_trait< SpTuples<int,bool> , SpTuples<int,int> >
182  {
184  };
185 template <> struct promote_trait< SpTuples<int,int> , SpTuples<int,bool> >
186  {
188  };
189 template <> struct promote_trait< SpTuples<int,int> , SpTuples<int,float> >
190  {
192  };
193 template <> struct promote_trait< SpTuples<int,float> , SpTuples<int,int> >
194  {
196  };
197 template <> struct promote_trait< SpTuples<int,int> , SpTuples<int,double> >
198  {
200  };
201 template <> struct promote_trait< SpTuples<int,double> , SpTuples<int,int> >
202  {
204  };
205 template <> struct promote_trait< SpTuples<int,unsigned> , SpTuples<int,bool> >
206  {
208  };
209 template <> struct promote_trait< SpTuples<int,bool> , SpTuples<int,unsigned> >
210  {
212  };
213 template <> struct promote_trait< SpTuples<int,bool> , SpTuples<int,double> >
214  {
216  };
217 template <> struct promote_trait< SpTuples<int,bool> , SpTuples<int,float> >
218  {
220  };
221 template <> struct promote_trait< SpTuples<int,double> , SpTuples<int,bool> >
222  {
224  };
225 template <> struct promote_trait< SpTuples<int,float> , SpTuples<int,bool> >
226  {
228  };
229 template <> struct promote_trait< SpTuples<int64_t,int> , SpTuples<int64_t,int> >
230  {
232  };
233 template <> struct promote_trait< SpTuples<int64_t,float> , SpTuples<int64_t,float> >
234  {
236  };
237 template <> struct promote_trait< SpTuples<int64_t,double> , SpTuples<int64_t,double> >
238  {
240  };
242  {
244  };
245 template <> struct promote_trait< SpTuples<int64_t,bool> , SpTuples<int64_t,int> >
246  {
248  };
249 template <> struct promote_trait< SpTuples<int64_t,int> , SpTuples<int64_t,bool> >
250  {
252  };
253 template <> struct promote_trait< SpTuples<int64_t,int> , SpTuples<int64_t,float> >
254  {
256  };
257 template <> struct promote_trait< SpTuples<int64_t,float> , SpTuples<int64_t,int> >
258  {
260  };
261 template <> struct promote_trait< SpTuples<int64_t,int> , SpTuples<int64_t,double> >
262  {
264  };
265 template <> struct promote_trait< SpTuples<int64_t,double> , SpTuples<int64_t,int> >
266  {
268  };
269 template <> struct promote_trait< SpTuples<int64_t,unsigned> , SpTuples<int64_t,bool> >
270  {
272  };
273 template <> struct promote_trait< SpTuples<int64_t,bool> , SpTuples<int64_t,unsigned> >
274  {
276  };
277 template <> struct promote_trait< SpTuples<int64_t,bool> , SpTuples<int64_t,double> >
278  {
280  };
281 template <> struct promote_trait< SpTuples<int64_t,bool> , SpTuples<int64_t,float> >
282  {
284  };
285 template <> struct promote_trait< SpTuples<int64_t,double> , SpTuples<int64_t,bool> >
286  {
288  };
289 template <> struct promote_trait< SpTuples<int64_t,float> , SpTuples<int64_t,bool> >
290  {
292  };
293 #include "SpTuples.cpp"
294 #endif