35 #ifndef GRAPHLAB_ATOMIC_ADD_VECTOR2_HPP
36 #define GRAPHLAB_ATOMIC_ADD_VECTOR2_HPP
42 #include <graphlab/parallel/pthread_tools.hpp>
43 #include <graphlab/util/lock_free_pool.hpp>
53 template<
typename ValueType>
56 typedef ValueType value_type;
60 #define VALUE_PENDING (value_type*)(size_t)(-1)
63 atomic<size_t> joincounter;
65 class atomic_box_type {
71 atomic_box_type() : _empty(
true) { }
73 inline bool set(
const value_type& other,
74 value_type& new_value,
75 atomic<size_t>& joincounter) {
76 bool first_set =
false;
78 if(!_empty) value += other;
79 else { value = other; first_set =
true; }
90 bool empty() {
return _empty; }
93 inline bool peek(value_type& ret_val) {
105 bool success =
false;
120 typedef std::vector< atomic_box_type > atomic_box_vec_type;
121 atomic_box_vec_type atomic_box_vec;
131 atomic_box_vec(num_vertices) { }
137 atomic_box_vec.resize(num_vertices);
142 bool add(
const size_t& idx,
143 const value_type& val) {
144 ASSERT_LT(idx, atomic_box_vec.size());
145 value_type new_value;
146 return atomic_box_vec[idx].set( val, new_value, joincounter);
159 bool add(
const size_t& idx,
160 const value_type& val,
161 value_type& new_value) {
162 ASSERT_LT(idx, atomic_box_vec.size());
163 return atomic_box_vec[idx].set(val, new_value, joincounter);
193 value_type& ret_val) {
194 ASSERT_LT(idx, atomic_box_vec.size());
195 return atomic_box_vec[idx].test_and_get( ret_val);
198 bool peek(
const size_t& idx,
199 value_type& ret_val) {
200 ASSERT_LT(idx, atomic_box_vec.size());
201 return atomic_box_vec[idx].peek(ret_val);
204 bool empty(
const size_t& idx) {
205 return atomic_box_vec[idx].empty();
208 size_t size()
const {
209 return atomic_box_vec.size();
212 size_t num_joins()
const {
213 return joincounter.value;
218 for (
size_t i = 0; i < atomic_box_vec.size(); ++i) clear(i);
221 void clear(
size_t i) { atomic_box_vec[i].clear(); }