2 #ifndef USRP_MEMORY_INCLUDED 3 #define USRP_MEMORY_INCLUDED 16 boost::condition_variable ready_cond;
17 boost::mutex ready_mutex;
48 void update(
int new_buffer_len = 0);
56 int calculate_spare();
58 int calculate_outsize();
71 void update(
int current_batch);
93 buffer_helper (
int _n_tones,
int _buffer_len,
int _average,
int _n_eff_tones);
100 int simulate_batching();
103 template <
typename vector_type>
111 boost::lockfree::queue< intptr_t, boost::lockfree::fixed_sized<(bool)true>>*
allocated;
112 boost::lockfree::queue< intptr_t, boost::lockfree::fixed_sized<(bool)true>>*
deallocated;
116 preallocator(
int init_vector_size,
int init_pipe_size,
bool prefill_init =
true,
int core = -1){
118 prefil = prefill_init;
119 vector_size = init_vector_size;
121 pipe_size = init_pipe_size;
122 allocated =
new boost::lockfree::queue< intptr_t ,boost::lockfree::fixed_sized<(bool)true>> (init_pipe_size);
123 deallocated =
new boost::lockfree::queue< intptr_t ,boost::lockfree::fixed_sized<(bool)true>>(init_pipe_size);
124 filler =
new boost::thread(boost::bind(&preallocator::queue_filler,
this));
126 deallocator =
new boost::thread(boost::bind(&preallocator::queue_deallocator,
this));
128 while(counter<pipe_size-1)boost::this_thread::sleep_for(boost::chrono::milliseconds{200});
135 intptr_t thatvalue_other;
137 while(not allocated->pop(thatvalue_other))boost::this_thread::sleep_for(boost::chrono::microseconds{10});
140 return reinterpret_cast<vector_type*
>(thatvalue_other);
144 void trash(vector_type* trash_vector){
146 while(not deallocated->push(reinterpret_cast<intptr_t>(trash_vector)))boost::this_thread::sleep_for(boost::chrono::microseconds{1});
152 deallocator->interrupt();
155 deallocator =
nullptr;
170 bool warning = (bool)
true;
171 boost::thread* filler;
172 boost::thread* deallocator;
173 std::atomic<int> counter;
175 void queue_deallocator(){
177 bool active = (bool)
true;
180 boost::this_thread::interruption_point();
181 intptr_t trash_vector = 0;
182 if(deallocated->pop(trash_vector)){
185 intptr_t thatvalue =
reinterpret_cast<intptr_t
>(trash_vector);
186 while (not allocated->push(thatvalue) or err_counter){
188 boost::this_thread::sleep_for(boost::chrono::microseconds{wait_on_full});
190 boost::this_thread::sleep_for(boost::chrono::milliseconds{3});
194 }
else boost::this_thread::sleep_for(boost::chrono::microseconds{wait_on_full});
195 }
catch (boost::thread_interrupted&){active=(bool)
false;}
198 while(not deallocated->empty()){
200 intptr_t trash_vector;
201 if(deallocated->pop(trash_vector)){
202 cudaFreeHost(static_cast<vector_type*>(reinterpret_cast<void*>(trash_vector)));
204 boost::this_thread::sleep_for(boost::chrono::milliseconds{3});
212 bool active = (bool)
true;
213 vector_type* h_aPinned=NULL;
214 intptr_t thatvalue = 1;
215 int debug_counter = 0;
217 while(counter<pipe_size-1){
219 cudaError_t status = cudaMallocHost((
void**)&h_aPinned, vector_size*
sizeof(vector_type));
220 if (status != cudaSuccess){
221 print_error(
"Memory manager cannot allocate pinned host memory!");
224 thatvalue =
reinterpret_cast<intptr_t
>(h_aPinned);
225 while(not allocated->push(thatvalue))boost::this_thread::sleep_for(boost::chrono::microseconds{10*wait_on_full});
233 boost::this_thread::interruption_point();
234 if(counter < pipe_size/10.){
235 cudaError_t status = cudaMallocHost((
void**)&h_aPinned, vector_size*
sizeof(vector_type));
236 if (status != cudaSuccess){
237 print_error(
"Memory manager cannot allocate pinned host memory!");
240 thatvalue =
reinterpret_cast<intptr_t
>(h_aPinned);
241 while(not allocated->push(thatvalue))boost::this_thread::sleep_for(boost::chrono::microseconds{10*wait_on_full});
244 }
else if(debug_counter>0 ){
245 pipe_size+=debug_counter;
246 print_debug(
"Internal memory manager had to adjust pipe size to ",pipe_size);
250 boost::this_thread::sleep_for(boost::chrono::microseconds{10*wait_on_full});
251 }
catch (boost::thread_interrupted&){active=(bool)
false;}
256 boost::this_thread::interruption_point();
257 boost::this_thread::sleep_for(boost::chrono::microseconds{1000*wait_on_full});
258 }
catch (boost::thread_interrupted&){active=(bool)
false;}
263 while(not allocated->empty()){
265 intptr_t trash_vector;
266 if(allocated->pop(trash_vector)){
267 cudaFreeHost(static_cast<vector_type*>(reinterpret_cast<void*>(trash_vector)));
269 boost::this_thread::sleep_for(boost::chrono::milliseconds{1});
void trash(vector_type *trash_vector)
preallocator(int init_vector_size, int init_pipe_size, bool prefill_init=true, int core=-1)
boost::lockfree::queue< intptr_t, boost::lockfree::fixed_sized<(bool) true > > * deallocated
boost::lockfree::queue< intptr_t, boost::lockfree::fixed_sized<(bool) true > > * allocated
void print_error(std::string text)
void Thread_Prioriry(boost::thread &Thread, int priority, int affinity)
void set_this_thread_name(std::string thread_name)
Set the htread name reported in the logging.
void print_debug(std::string text, double value)