FlashGraph-ng
A new frontier in large-scale graph analysis and data mining
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
raw_data_array.h
1 #ifndef __RAW_DATA_ARRAY_H__
2 #define __RAW_DATA_ARRAY_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 <string.h>
24 #include <assert.h>
25 
26 #include <memory>
27 #include <vector>
28 
29 #include "NUMA_mapper.h"
30 
31 namespace fm
32 {
33 
34 class set_operate;
35 class bulk_operate;
36 class scalar_variable;
37 
38 namespace detail
39 {
40 
41 /*
42  * This class holds the allocated memory and helps other classes access
43  * the data in the memory. We shouldn't pass the object of this class
44  * around, which is relatively expensive.
45  */
46 class raw_data_array
47 {
48  int node_id;
49  // The total number of bytes in the allocated memory.
50  size_t num_bytes;
51  // This points to the beginning of the allocated memory.
52  std::shared_ptr<char> data;
53 public:
54  raw_data_array() {
55  node_id = -1;
56  num_bytes = 0;
57  }
58 
59  raw_data_array(size_t num_bytes, int node_id = -1);
60 
61  bool is_empty() const {
62  return num_bytes == 0;
63  }
64 
65  /*
66  * Get the number of bytes allocated.
67  */
68  size_t get_num_bytes() const {
69  return num_bytes;
70  }
71 
72  void reset_data() {
73  memset(data.get(), 0, num_bytes);
74  }
75 
76  std::shared_ptr<char> get_raw_data() {
77  return data;
78  }
79 
80  std::shared_ptr<const char> get_raw_data() const {
81  return data;
82  }
83 
84  char *get_raw() {
85  return data.get();
86  }
87 
88  const char *get_raw() const {
89  return data.get();
90  }
91 
92  int get_node_id() const {
93  return node_id;
94  }
95 
96  raw_data_array deep_copy() const;
97 
98  bool copy_from(const raw_data_array &arr);
99  /*
100  * Copy the data in the buffer to the start location.
101  * @start and @size are in the number of bytes.
102  */
103  bool set_sub_arr(off_t start, const char *arr, size_t size);
104 
105  void expand(size_t min);
106 };
107 
108 /*
109  * This interface is to set the data in the vector of arrays.
110  */
111 class set_range_operate
112 {
113 public:
114  /*
115  * @buf: the buffer where to initialize data.
116  * @size: the size of the buffer (the number of bytes).
117  * @local_off: the offset of the buffer in an array (the number of bytes).
118  * @arr_id: the id of the array where the buffer is.
119  */
120  virtual void set(char *buf, size_t size, off_t local_off,
121  int arr_id) const = 0;
122 };
123 
124 /*
125  * Reset all elements in the arrays.
126  */
127 void reset_arrays(std::vector<raw_data_array> &arrs);
128 /*
129  * Set the elements in the arrays.
130  * @mapper: defines how the elements are distributed in the arrays.
131  * @length: the total number of elements in the arrays.
132  * @entry_size: the size of an element.
133  * @set_range: the function to set elements.
134  * @arrs: the arrays.
135  */
136 void set_array_ranges(const NUMA_mapper &mapper, size_t length,
137  size_t entry_size, const set_range_operate &set_range,
138  std::vector<raw_data_array> &arrs);
139 
140 }
141 
142 }
143 
144 #endif