Vowpal Wabbit
array_parameters_dense.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cstdint>
4 #include "memory.h"
5 
6 typedef float weight;
7 
8 template <typename T>
10 {
11  private:
13  T* _begin;
14  uint32_t _stride;
15 
16  public:
17  typedef std::forward_iterator_tag iterator_category;
18  typedef T value_type;
19  typedef std::ptrdiff_t difference_type;
20  typedef T* pointer;
21  typedef T& reference;
22 
23  dense_iterator(T* current, T* begin, uint32_t stride) : _current(current), _begin(begin), _stride(stride) {}
24 
25  T& operator*() { return *_current; }
26 
27  size_t index() { return _current - _begin; }
28 
30  {
31  _current += _stride;
32  return *this;
33  }
34 
35  bool operator==(const dense_iterator& rhs) const { return _current == rhs._current; }
36  bool operator!=(const dense_iterator& rhs) const { return _current != rhs._current; }
37 };
38 
40 {
41  private:
43  uint64_t _weight_mask; // (stride*(1 << num_bits) -1)
44  uint32_t _stride_shift;
45  bool _seeded; // whether the instance is sharing model state with others
46 
47  public:
50  dense_parameters(size_t length, uint32_t stride_shift = 0)
51  : _begin(calloc_mergable_or_throw<weight>(length << stride_shift))
52  , _weight_mask((length << stride_shift) - 1)
53  , _stride_shift(stride_shift)
54  , _seeded(false)
55  {
56  }
57 
58  dense_parameters() : _begin(nullptr), _weight_mask(0), _stride_shift(0), _seeded(false) {}
59 
60  bool not_null() { return (_weight_mask > 0 && _begin != nullptr); }
61 
62  dense_parameters(const dense_parameters& other) { shallow_copy(other); }
64 
66  {
67  return _begin;
68  } // TODO: Temporary fix for allreduce.
69  // iterator with stride
70  iterator begin() { return iterator(_begin, _begin, stride()); }
71  iterator end() { return iterator(_begin + _weight_mask + 1, _begin, stride()); }
72 
73  // const iterator
74  const_iterator cbegin() { return const_iterator(_begin, _begin, stride()); }
75  const_iterator cend() { return const_iterator(_begin + _weight_mask + 1, _begin, stride()); }
76 
77  inline weight& operator[](size_t i) const { return _begin[i & _weight_mask]; }
78  void shallow_copy(const dense_parameters& input)
79  {
80  if (!_seeded)
81  free(_begin);
82  _begin = input._begin;
83  _weight_mask = input._weight_mask;
84  _stride_shift = input._stride_shift;
85  _seeded = true;
86  }
87 
88  inline weight& strided_index(size_t index) { return operator[](index << _stride_shift); }
89 
90  template <class R, class T>
91  void set_default(R& info)
92  {
93  iterator iter = begin();
94  for (size_t i = 0; iter != end(); ++iter, i += stride()) T::func(*iter, info, iter.index());
95  }
96 
97  template <class T>
98  void set_default()
99  {
100  iterator iter = begin();
101  for (size_t i = 0; iter != end(); ++iter, i += stride()) T::func(*iter, iter.index());
102  }
103 
104  void set_zero(size_t offset)
105  {
106  for (iterator iter = begin(); iter != end(); ++iter) (&(*iter))[offset] = 0;
107  }
108 
109  uint64_t mask() const { return _weight_mask; }
110 
111  uint64_t seeded() const { return _seeded; }
112 
113  uint32_t stride() const { return 1 << _stride_shift; }
114 
115  uint32_t stride_shift() const { return _stride_shift; }
116 
117  void stride_shift(uint32_t stride_shift) { _stride_shift = stride_shift; }
118 
119 #ifndef _WIN32
120 #ifndef DISABLE_SHARED_WEIGHTS
121  void share(size_t length)
122  {
123  float* shared_weights = (float*)mmap(
124  0, (length << _stride_shift) * sizeof(float), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
125  size_t float_count = length << _stride_shift;
126  weight* dest = shared_weights;
127  memcpy(dest, _begin, float_count * sizeof(float));
128  free(_begin);
129  _begin = dest;
130  }
131 #endif
132 #endif
133 
135  {
136  if (_begin != nullptr && !_seeded) // don't free weight vector if it is shared with another instance
137  {
138  free(_begin);
139  _begin = nullptr;
140  }
141  }
142 };
dense_iterator(T *current, T *begin, uint32_t stride)
uint64_t seeded() const
uint64_t stride_shift(const stagewise_poly &poly, uint64_t idx)
uint32_t stride_shift() const
dense_iterator< weight > iterator
bool operator!=(const dense_iterator &rhs) const
std::forward_iterator_tag iterator_category
weight & operator[](size_t i) const
#define MAP_ANONYMOUS
Definition: parser.cc:309
weight & strided_index(size_t index)
void stride_shift(uint32_t stride_shift)
void share(size_t length)
std::ptrdiff_t difference_type
void set_zero(size_t offset)
uint32_t stride() const
#define calloc_mergable_or_throw
Definition: memory.h:91
const_iterator cend()
const_iterator cbegin()
float weight
dense_iterator< const weight > const_iterator
bool operator==(const dense_iterator &rhs) const
dense_iterator & operator++()
dense_parameters(size_t length, uint32_t stride_shift=0)
void shallow_copy(const dense_parameters &input)
dense_parameters(const dense_parameters &other)