FlashGraph-ng
A new frontier in large-scale graph analysis and data mining
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
mapply_matrix_store.h
1 #ifndef __MAPPLY_MATRIX_STORE_H__
2 #define __MAPPLY_MATRIX_STORE_H__
3 
4 /*
5  * Copyright 2015 Open Connectome Project (http://openconnecto.me)
6  * Written by Da Zheng (zhengda1936@gmail.com)
7  *
8  * This file is part of FlashMatrix.
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  */
22 
23 #include "virtual_matrix_store.h"
24 #include "dense_matrix.h"
25 #include "mem_matrix_store.h"
26 #include "EM_object.h"
27 
28 namespace fm
29 {
30 
31 namespace detail
32 {
33 
34 class portion_mapply_op;
35 
36 /*
37  * This class represents the result matrix of mapply operations.
38  * It partially materializes a portion of the matrix when the portion
39  * is needed. The underlying matrices that mapply runs on can be both
40  * stored in memory and on disks. Therefore, this matrix should also
41  * expose the EM object interface.
42  */
43 class mapply_matrix_store: public virtual_matrix_store, public EM_object
44 {
45  // This identifies the data in a matrix.
46  // So when a matrix is transposed, it should share the same data id.
47  const size_t data_id;
48 
49  /*
50  * This indicates whether or not we cache a portion in each worker thread.
51  * By default, this is enabled.
52  */
53  bool cache_portion;
54 
55  /*
56  * This indicates whether the input matrices are accessed in parallel
57  * when the matrix is materialized.
58  */
59  bool par_access;
60 
61  matrix_layout_t layout;
62  const std::vector<matrix_store::const_ptr> in_mats;
63  portion_mapply_op::const_ptr op;
64  // The materialized result matrix.
65  matrix_store::const_ptr res;
66 public:
67  typedef std::shared_ptr<const mapply_matrix_store> const_ptr;
68 
69  mapply_matrix_store(
70  const std::vector<matrix_store::const_ptr> &in_mats,
71  portion_mapply_op::const_ptr op,
72  matrix_layout_t layout, size_t nrow, size_t ncol,
73  size_t data_id = mat_counter++);
74 
75  virtual void set_cache_portion(bool cache_portion);
76 
77  void set_par_access(bool par_access) {
78  this->par_access = par_access;
79  }
80 
81  virtual void materialize_self() const;
82 
83  virtual matrix_store::const_ptr materialize(bool in_mem,
84  int num_nodes) const;
85 
86  virtual std::shared_ptr<const vec_store> get_col_vec(off_t idx) const;
87  virtual std::shared_ptr<const vec_store> get_row_vec(off_t idx) const;
88  virtual matrix_store::const_ptr get_cols(const std::vector<off_t> &idxs) const;
89  virtual matrix_store::const_ptr get_rows(const std::vector<off_t> &idxs) const;
90 
91  using virtual_matrix_store::get_portion;
92  virtual std::shared_ptr<const local_matrix_store> get_portion(
93  size_t start_row, size_t start_col, size_t num_rows,
94  size_t num_cols) const;
95  virtual std::shared_ptr<const local_matrix_store> get_portion(
96  size_t id) const;
97  virtual async_cres_t get_portion_async(
98  size_t start_row, size_t start_col, size_t num_rows,
99  size_t num_cols, std::shared_ptr<portion_compute> compute) const;
100  virtual std::pair<size_t, size_t> get_portion_size() const;
101  virtual int get_num_nodes() const {
102  return in_mats.front()->get_num_nodes();
103  }
104 
105  virtual matrix_store::const_ptr transpose() const;
106 
107  virtual matrix_layout_t store_layout() const {
108  return layout;
109  }
110 
111  virtual std::vector<safs::io_interface::ptr> create_ios() const;
112 
113  virtual std::string get_name() const;
114  virtual std::unordered_map<size_t, size_t> get_underlying_mats() const;
115 };
116 
117 }
118 
119 }
120 
121 #endif