FlashGraph-ng
A new frontier in large-scale graph analysis and data mining
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
NUMA_vector.h
1 #ifndef __NUMA_VECTOR_H__
2 #define __NUMA_VECTOR_H__
3 
4 /*
5  * Copyright 2014 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 <stdlib.h>
24 #include <assert.h>
25 
26 #include <vector>
27 
28 #include "bulk_operate.h"
29 #include "mem_vec_store.h"
30 #include "matrix_config.h"
31 #include "raw_data_array.h"
32 #include "NUMA_mapper.h"
33 
34 namespace fm
35 {
36 
37 class scalar_type;
38 class local_vec_store;
39 
40 namespace detail
41 {
42 
43 class matrix_store;
44 
45 /*
46  * This is another implementation of in-memory vector. In this implementation,
47  * the data in the vector is split and stored on multiple NUMA nodes. All
48  * operations on the vector are optimized accordingly.
49  */
50 class NUMA_vec_store: public mem_vec_store
51 {
52  NUMA_mapper mapper;
53 
54  std::vector<raw_data_array> data;
55 
56  // The copy constructor performs shallow copy.
57  NUMA_vec_store(const NUMA_vec_store &vec);
58  NUMA_vec_store(size_t length, size_t num_nodes, const scalar_type &type);
59 public:
60  typedef std::shared_ptr<NUMA_vec_store> ptr;
61  typedef std::shared_ptr<const NUMA_vec_store> const_ptr;
62 
63  static ptr create(size_t length, size_t num_nodes, const scalar_type &type) {
64  return ptr(new NUMA_vec_store(length, num_nodes, type));
65  }
66 
67  static ptr cast(vec_store::ptr vec);
68 
69  virtual char *get_raw_arr() {
70  return NULL;
71  }
72  virtual const char *get_raw_arr() const {
73  return NULL;
74  }
75 
76  virtual std::shared_ptr<const local_vec_store> get_portion(off_t start,
77  size_t length) const;
78  virtual std::shared_ptr<local_vec_store> get_portion(off_t start, size_t length);
79 
80  virtual bool append(std::vector<vec_store::const_ptr>::const_iterator vec_it,
81  std::vector<vec_store::const_ptr>::const_iterator vec_end);
82  virtual bool append(const vec_store &vec);
83 
84  virtual vec_store::ptr deep_copy() const;
85  virtual vec_store::ptr shallow_copy() {
86  return vec_store::ptr(new NUMA_vec_store(*this));
87  }
88  virtual vec_store::const_ptr shallow_copy() const {
89  return vec_store::ptr(new NUMA_vec_store(*this));
90  }
91 
92  virtual void sort();
93  virtual vec_store::ptr sort_with_index();
94  virtual bool is_sorted() const;
95 
96  virtual void reset_data();
97  void set_data(const set_vec_operate &op);
98 
99  bool is_sub_vec() const;
100 
101  /*
102  * Get the node id where the data specified by `off' is located.
103  */
104  int get_node_id(off_t off) const {
105  auto phy_loc = mapper.map2physical(off);
106  return phy_loc.first;
107  }
108 
109  /*
110  * Get a subarray in [start, end), which must be in the same range.
111  */
112  const char *get_sub_arr(off_t start, off_t end) const;
113  char *get_sub_arr(off_t start, off_t end);
114 
115  /*
116  * This copies a piece of contiguous memory to the NUMA vector.
117  */
118  virtual bool copy_from(const char *buf, size_t num_bytes);
119  bool copy_from(const NUMA_vec_store &vec);
120 
121  virtual int get_num_nodes() const {
122  return data.size();
123  }
124 
125  virtual std::shared_ptr<matrix_store> conv2mat(
126  size_t nrow, size_t ncol, bool byrow);
127 
128  char *get(off_t idx) {
129  std::pair<int, size_t> loc = mapper.map2physical(idx);
130  size_t off = loc.second * get_entry_size();
131  return data[loc.first].get_raw() + off;
132  }
133 
134  const char *get(off_t idx) const {
135  std::pair<int, size_t> loc = mapper.map2physical(idx);
136  size_t off = loc.second * get_entry_size();
137  return data[loc.first].get_raw() + off;
138  }
139 
140  template<class T>
141  T get(off_t idx) const {
142  return *(const T*) get(idx);
143  }
144 
145  template<class T>
146  void set(off_t idx, T v) {
147  *(T*) get(idx) = v;
148  }
149 
150  const NUMA_mapper &get_mapper() const {
151  return mapper;
152  }
153 
154  size_t get_portion_size() const {
155  return mapper.get_range_size();
156  }
157 
158  virtual bool resize(size_t new_length) {
159  assert(0);
160  return false;
161  }
162  virtual bool set_portion(std::shared_ptr<const local_vec_store> store,
163  off_t off) {
164  assert(0);
165  return false;
166  }
167 };
168 
169 }
170 
171 }
172 
173 #endif