1/*
2 * vector_dist_operators_list_ker.hpp
3 *
4 * Created on: Jun 7, 2019
5 * Author: i-bird
6 */
7
8#ifndef VECTOR_DIST_OPERATORS_LIST_KER_HPP_
9#define VECTOR_DIST_OPERATORS_LIST_KER_HPP_
10
11template<typename T>
12struct ref_wrap
13{
14 bool is_sorted;
15 T & v;
16
17 ref_wrap(T & v, bool is_sorted)
18 :v(v),is_sorted(is_sorted)
19 {}
20
21 ref_wrap & operator=(const ref_wrap<T> & rw)
22 {
23 v = rw.v;
24 return *this;
25 }
26};
27
28/*! \brief This class contain a list of all tracked vector_dist_ker around.
29 *
30 * In short suppose to do a auto vdk = vd.toKernel() vdk wrap the cuda pointer of vd. On the other hand
31 * if in vd we use instruction that produce reallocations vdk contain invalid pointers. this class contail a
32 * list of all vector_dist_kernel, such that if a reallocation happen the pointer are updated on the vector_dist_kernel
33 *
34 */
35template<typename vector_dist_ker_type>
36class vector_dist_ker_list
37{
38 openfpm::vector<ref_wrap<vector_dist_ker_type>> vkers;
39
40public:
41
42 /*! \brief Add a new vector_dist_kernel to track
43 *
44 * \param v vector_dist_kernel to track
45 *
46 *
47 */
48 void add(vector_dist_ker_type & v, bool is_sorted)
49 {
50 ref_wrap<vector_dist_ker_type> rw(v,is_sorted);
51
52 vkers.add(rw);
53
54 if (vkers.size() >= 64)
55 {
56 std::cout << __FILE__ << ":" << __LINE__ << " The array of tracked vector_dist_ker become suspiciously big, are there memory leak ? " << std::endl;
57 }
58 }
59
60 /*! \brief Update the addresses of all vector_dist_kernels around
61 *
62 * \param v vector_dist_kernel to track
63 *
64 *
65 */
66 void update(const vector_dist_ker_type & v)
67 {
68 for (size_t i = 0 ; i < vkers.size() ; i++)
69 {
70 if (vkers.get(i).is_sorted == false)
71 {vkers.get(i).v = v;}
72 }
73 }
74
75 void update_sort(const vector_dist_ker_type & vs)
76 {
77 for (size_t i = 0 ; i < vkers.size() ; i++)
78 {
79 if (vkers.get(i).is_sorted == true)
80 {vkers.get(i).v = vs;}
81 }
82 }
83
84 /*! \brief Remove one vector_dist_kernels entry
85 *
86 * \param v vector_dist_kernel to remove
87 *
88 *
89 */
90 void remove(vector_dist_ker_type & v)
91 {
92 for (size_t i = 0 ; i < vkers.size() ; i++)
93 {
94 if (&vkers.get(i).v == &v)
95 {
96 vkers.remove(i);
97 break;
98 }
99 }
100 }
101
102 /*! \brief Return the number of entries
103 *
104 * \return the number of entries
105 *
106 */
107 size_t n_entry()
108 {
109 return vkers.size();
110 }
111
112 /*! \brief Check that all the entries are aligned to the latest vector_dist_ker_type
113 *
114 * \return true if all entries are alligned
115 *
116 */
117 bool check(const vector_dist_ker_type & v)
118 {
119 for (size_t i = 0 ; i < vkers.size() ; i++)
120 {
121 if (!(vkers.get(i).v == v))
122 {
123 return false;
124 }
125 }
126
127 return true;
128 }
129};
130
131#endif /* VECTOR_DIST_OPERATORS_LIST_KER_HPP_ */
132