COMBINATORIAL_BLAS  1.6
VectorIO.cpp
Go to the documentation of this file.
1 #include <mpi.h>
2 #include <sys/time.h>
3 #include <iostream>
4 #include <functional>
5 #include <algorithm>
6 #include <vector>
7 #include <sstream>
8 #include "CombBLAS/CombBLAS.h"
9 
10 using namespace std;
11 using namespace combblas;
12 
13 
14 
15 template <class IT, class NT>
17 {
18  public:
20  pair<NT,NT> getNoNum(IT index) { return make_pair((NT) 0, (NT) 0); }
21 
22 
23  template <typename c, typename t>
24  pair<NT,NT> read(std::basic_istream<c,t>& is, IT index)
25  {
26  pair<NT,NT> pp;
27  is >> pp.first >> pp.second;
28  return pp;
29  }
30 
31 
32  template <typename c, typename t>
33  void save(std::basic_ostream<c,t>& os, const pair<NT,NT> & pp, IT index)
34  {
35  os << pp.first << "\t" << pp.second;
36  }
37 };
38 
39 
40 int main(int argc, char* argv[])
41 {
42  int nprocs, myrank;
43  MPI_Init(&argc, &argv);
44  MPI_Comm_size(MPI_COMM_WORLD,&nprocs);
45  MPI_Comm_rank(MPI_COMM_WORLD,&myrank);
46 
47  if(argc < 5)
48  {
49  if(myrank == 0)
50  {
51  cout << "Usage: ./VectorIO <DictionaryVector> <UnPermutedVector> <OutputName> base(0 or 1)" << endl;
52  cout << "Example: ./VectorIO vertex_dict.mtx clusters.mtx original_clusters.mtx 0" << endl;
53  }
54  MPI_Finalize();
55  return -1;
56  }
57  {
58  string vec1name(argv[1]);
59  string vec2name(argv[2]);
60  string voutname(argv[3]);
61  int base = atoi(argv[4]);
62 
63 
64  FullyDistVec<int64_t,string> vecdict, clusters; // keep cluster ids as "string" for generality
65 
66  // the "index sets" of vecdict and clusters are the same
67  // goal here is to just convert the indices in "clusters" to values in vecdict
68 
69  vecdict.ParallelRead(vec1name, base, // keeps lexicographically larger one in case of duplicates (after warning)
70  [](string s1, string s2) { cout << "Unexpected duplicate in dictionary" << endl; return std::max<string>(s1, s2); });
71 
72  clusters.ParallelRead(vec2name, base, // keeps lexicographically larger one in case of duplicates (after warning)
73  [](string s1, string s2) { cout << "Unexpected duplicate in unpermuted vector" << endl; return std::max<string>(s1, s2); });
74 
75  vecdict.PrintInfo("dictionary");
76  clusters.PrintInfo("unpermuted cluster vector");
77 
78  // FullyDistVec<IT,NT>::EWiseOut(const FullyDistVec<IT,NT> & rhs, _BinaryOperation __binary_op, FullyDistVec<IT,OUT> & result)
79  // Perform __binary_op(*this[i], rhs[i]) for every element in rhs and *this, write the result output vector
81 
82  clusters.EWiseOut(vecdict, [] (string s1, string s2) { return make_pair(s1,s2); }, newclusters);
83 
84  newclusters.ParallelWrite(voutname, base, PairReadSaveHandler<int64_t, string>(), false); // don't print the index
85  }
86  MPI_Finalize();
87  return 0;
88 }
void EWiseOut(const FullyDistVec< IT, NT > &rhs, _BinaryOperation __binary_op, FullyDistVec< IT, OUT > &result)
pair< NT, NT > read(std::basic_istream< c, t > &is, IT index)
Definition: VectorIO.cpp:24
pair< NT, NT > getNoNum(IT index)
Definition: VectorIO.cpp:20
void save(std::basic_ostream< c, t > &os, const pair< NT, NT > &pp, IT index)
Definition: VectorIO.cpp:33
int main(int argc, char *argv[])
Definition: VectorIO.cpp:40
void ParallelWrite(const std::string &filename, bool onebased, HANDLER handler, bool includeindices=true)
Definition: FullyDistVec.h:96
void PrintInfo(std::string vectorname) const
Definition: CCGrid.h:4
void ParallelRead(const std::string &filename, bool onebased, _BinaryOperation BinOp)
Definition: FullyDistVec.h:105