FlashGraph-ng
A new frontier in large-scale graph analysis and data mining
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
convert_util.h
1 /*
2  * Copyright 2014 Open Connectome Project (http://openconnecto.me)
3  * Written by Disa Mhembere (disa@jhu.edu)
4  *
5  * This file is part of FlashMatrix.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 #include <fstream>
21 
22 #include "../dense_matrix.h"
23 #include "../vector.h"
24 
25 using namespace fm;
26 using namespace fm::detail;
27 
28 enum conv_layout{ROW, COL, RAWROW, RAWCOL};
29 
30 template <typename T>
31 static void print_vector(typename std::vector<T>& v)
32 {
33  std::cout << "[";
34 
35  typename std::vector<T>::iterator itr = v.begin();
36  for (; itr != v.end(); itr++) {
37  std::cout << " "<< *itr;
38  }
39  std::cout << " ]\n";
40 }
41 
42 template <typename T>
43 static void print_mat(T* matrix, const unsigned rows, const unsigned cols,
44  conv_layout lay=RAWROW) {
45  for (unsigned row = 0; row < rows; row++) {
46  if (lay == RAWROW) { std::cout << "["; }
47  for (unsigned col = 0; col < cols; col++) {
48  if (lay == RAWROW) { std::cout << " " << matrix[row*cols + col]; }
49  else { std::cout << " | " << matrix[row + (rows*col)]; }
50  }
51  if (lay == RAWROW) { std::cout << " ]\n"; }
52  else { std::cout << " |\n"; }
53  }
54 }
55 
56 static void print_dmat(dense_matrix::ptr dmat) {
57  for (size_t row = 0; row < dmat->get_num_rows(); row++) {
58  std::shared_ptr<vector> curr_row = dmat->get_row(row);
59  std::vector<double> stdvec = curr_row->conv2std<double>();
60  print_vector<double>(stdvec);
61  }
62 }
63 
64 // read_nrow_ncol - does the data have the nrow, ncol first before it?
65 // No return since outmat is malloced and populated
66 static double* read_fg(std::string filename, conv_layout lay,
67  size_t NUM_ROWS=0, size_t NUM_COLS=0) {
68  std::ifstream infile;
69  infile.open(filename, std::ios::in | std::ios::binary);
70  double* outmat;
71  if (infile.is_open()) {
72  BOOST_LOG_TRIVIAL(info) << "Beginning read ...";
73 
74  if (NUM_ROWS == 0 || NUM_COLS == 0) {
75  BOOST_LOG_TRIVIAL(info) << "Reading matrix dims from file ...";
76  infile.read((char*)&NUM_ROWS, sizeof(size_t));
77  infile.read((char*)&NUM_COLS, sizeof(size_t));
78  }
79 
80  assert(NUM_ROWS > 0 && NUM_COLS > 0);
81 
82  if (lay == RAWROW) {
83  } else if (lay == RAWCOL) {
84  // Swap dimensions
85  size_t tmp = NUM_ROWS;
86  NUM_ROWS = NUM_COLS;
87  NUM_COLS = tmp;
88  } else {
89  assert (0);
90  }
91 
92  BOOST_LOG_TRIVIAL(info) << "Number of rows = " << NUM_ROWS;
93  BOOST_LOG_TRIVIAL(info) << "Number of cols = " << NUM_COLS;
94  outmat = new double[NUM_ROWS*NUM_COLS];
95  infile.read((char*)&outmat[0], sizeof(double)*(NUM_ROWS)*(NUM_COLS));
96 
97  infile.close();
98  }
99 #if 0
100  BOOST_LOG_TRIVIAL(info) << "Print read matrix ...";
101  print_mat(outmat, NUM_ROWS, NUM_COLS);
102 #endif
103 #if 0
104  BOOST_LOG_TRIVIAL(info) << "Printing read matrix as vector ...";
105  std::cout << "[ ";
106  for (size_t i = 0; i < NUM_ROWS*NUM_COLS; i++) {
107  std::cout << outmat[i] << " ";
108  }
109  std::cout << "]\n";
110 #endif
111  return outmat;
112 }
113