USRP_Server  2.0
A flexible, GPU-accelerated radio-frequency readout software.
USRP_server_settings.cpp
Go to the documentation of this file.
2 
3 int TCP_SYNC_PORT = 61360;
4 int TCP_ASYNC_PORT = 22001;
5 
6 std::string device_arguments = "noarg";
7 
8 std::string w_type_to_str(w_type enumerator){
9  std::string comp_string;
10  comp_string = "UNINIT";
11  switch(enumerator){
12  case(TONES):
13  comp_string = "TONES";
14  break;
15  case(CHIRP):
16  comp_string = "CHIRP";
17  break;
18  case(NOISE):
19  comp_string = "NOISE";
20  break;
21  case(RAMP):
22  comp_string = "RAMP";
23  break;
24  case(NODSP):
25  comp_string = "NODSP";
26  break;
27  case(SWONLY):
28  comp_string = "SWONLY";
29  break;
30  case(DIRECT):
31  comp_string = "DIRECT";
32  break;
33  }
34  return comp_string;
35 }
36 
37 w_type string_to_w_type(std::string input_string){
38  w_type conv = NODSP;
39  if(input_string.compare("NODSP") == 0)conv = NODSP;
40 
41  if(input_string.compare("CHIRP") == 0)conv = CHIRP;
42 
43  if(input_string.compare("NOISE") == 0)conv = NOISE;
44 
45  if(input_string.compare("TONES") == 0)conv = TONES;
46 
47  if(input_string.compare("SWONLY") == 0)conv = SWONLY;
48 
49  if(input_string.compare("DIRECT") == 0)conv = DIRECT;
50 
51  return conv;
52 
53 }
54 
55 std::vector<w_type> string_to_w_type_vector(std::vector<std::string> string_vector){
56  std::vector<w_type> res(string_vector.size());
57  for(size_t i = 0; i <string_vector.size(); i++ ){
58  res[i] = string_to_w_type(string_vector[i]);
59  }
60  return res;
61 }
62 //state of the USRP antenna
63 std::string ant_mode_to_str(ant_mode enumerator){
64  std::string comp_string;
65  comp_string = "UNINIT";
66  switch(enumerator){
67  case(TX):
68  comp_string = "TX";
69  break;
70  case(RX):
71  comp_string = "RX";
72  break;
73  case(OFF):
74  comp_string = "OFF";
75  break;
76  }
77  return comp_string;
78 }
79 
80 ant_mode ant_mode_from_string(std::string str){
81  if (not str.compare("OFF")) return (ant_mode)OFF;
82  if (not str.compare("RX")) return (ant_mode)RX;
83  if (not str.compare("TX")) return (ant_mode)TX;
84  print_warning("ant_mode from parametern conversion has not been recognised. Setting to OFF");
85  return (ant_mode)OFF;
86 }
87 
88 
89 //returns the maximum output buffer size (not all samples of that size will be always good)
90 //TODO something's wrong with this function
91 int param::get_output_buffer_size(){
92  print_warning("Using a wrong extimation of buffer size");
93  return std::ceil((float)buffer_len/(float)decim)*wave_type.size();
94 }
95 
96 //the execution of this measurement, if TX, requres a dynamical memory allocation?
97 bool param::dynamic_buffer(){
98  bool dynamic = false;
99  for(size_t i = 0; i< wave_type.size(); i++)if(wave_type[i]!=TONES)dynamic = true;
100  return dynamic;
101 }
102 
103 //how mny rx or tx to set up
104 int usrp_param::get_number(ant_mode T){
105  int num = 0;
106  if(A_TXRX.mode == T)num++;
107  if(A_RX2.mode == T)num++;
108  if(B_TXRX.mode == T)num++;
109  if(B_RX2.mode == T)num++;
110  return num;
111 }
112 
113 bool usrp_param::is_A_active(){
114  return (A_TXRX.mode != OFF or A_RX2.mode != OFF);
115 }
116 
117 bool usrp_param::is_B_active(){
118  return (B_TXRX.mode != OFF or B_RX2.mode != OFF);
119 }
120 
121 void server_settings::validate(){
122  if(clock_reference.compare("internal") != 0 and clock_reference.compare("external") != 0){
123  std::stringstream ss;
124  ss<<"Clock selection mode \""<<clock_reference<<"\" is not valid for the usrp X300. Setting mode to \"internal\".";
125  print_warning(ss.str());
126  clock_reference = "internal";
127  }
128  int num_gpus;
129  cudaGetDeviceCount(&num_gpus);
130  if (num_gpus == 0){
131  print_error("No GPU found in the system. This version of the USRP server needs at least one GPU to work.");
132  exit(-1);
133  }
134  if(GPU_device_index>num_gpus){
135  std::stringstream ss;
136  ss<<"GPU device index ("<< GPU_device_index <<") has been selected. However on the system only (" << num_gpus<< ") GPUs have been detected. Setting GPU index to "<< 0;
137  print_warning(ss.str());
138  GPU_device_index = 0;
139  }
140  if(default_rx_buffer_len < MIN_USEFULL_BUFFER or default_rx_buffer_len > MAX_USEFULL_BUFFER){
141  print_warning("RX default buffer length selected may give troubles");
142  }
143  if(default_tx_buffer_len < MIN_USEFULL_BUFFER or default_tx_buffer_len > MAX_USEFULL_BUFFER){
144  print_warning("TX default buffer length selected may give troubles");
145  }
146  if((not TCP_streaming) and (not FILE_writing)){
147  print_warning("No data will be saved to disk or streamed");
148  }
149 }
150 
151 void server_settings::autoset(){
152  TCP_streaming = false;
153  FILE_writing = true;
154  clock_reference = "internal";
155  GPU_device_index = 0;
156  default_rx_buffer_len = 1000000;
157  default_tx_buffer_len = 1000000;
158  validate();
159 }
160 
161 std::string get_front_end_name(char code){
162  switch(code){
163  case('A'):
164  return "A_TXRX";
165  case('B'):
166  return "A_RX2";
167  case('C'):
168  return "B_TXRX";
169  case('D'):
170  return "B_RX2";
171  default:
172  return "not_init";
173  }
174 }
175 
176  //attemp to controll the thread scheduling on mac osx
177  #if defined(__APPLE__)
178 
179  #include <mach/mach.h>
180  #include <mach/mach_time.h>
181  #include <pthread.h>
182 
183  #endif
184 
185 void Thread_Prioriry(boost::thread& Thread, int priority, int affinity){
186  #if not defined(__APPLE__)
187  int SYSTEM_CORES = 7;//std::thread::hardware_concurrency();
188  int retcode;
189  int policy;
190  struct sched_param scheme;
191  cpu_set_t cpuset;
192  pthread_t thread_ID = (pthread_t)Thread.native_handle();
193  policy = SCHED_FIFO;
194  scheme.sched_priority = priority;
195 
196 
197  retcode = pthread_setschedparam(thread_ID, policy, &scheme);
198  if(retcode != 0)print_warning("Cannot set thread scheduling policy");
199 
200 
201  print_debug("Setting affinity to ", affinity%SYSTEM_CORES);
202  CPU_ZERO(&cpuset);
203  CPU_SET(affinity%SYSTEM_CORES, &cpuset);
204  int rc = pthread_setaffinity_np(Thread.native_handle(),sizeof(cpu_set_t), &cpuset);
205  if (rc != 0) {
206  std::stringstream ss;
207  ss << "Error calling pthread_setaffinity_np: there may be some instability. Result: " << rc;
208  print_error(ss.str());
209  }
210  #endif
211 
212  #if defined(__APPLE__)
213  //TODO should find a way to controll the affinity policy on osx and windows
214  #endif
215 
216 }
217 
218 void SetThreadName(boost::thread* thread, const char* threadName){
219  auto handle = thread->native_handle();
220  pthread_setname_np(handle,threadName);
221 }
222 /*
223 void SetThreadName( const char* threadName)
224 {
225  prctl(PR_SET_NAME,threadName,0,0,0);
226 }
227 */
std::string device_arguments
std::string ant_mode_to_str(ant_mode enumerator)
std::string get_front_end_name(char code)
int TCP_SYNC_PORT
void SetThreadName(boost::thread *thread, const char *threadName)
std::string w_type_to_str(w_type enumerator)
void print_error(std::string text)
void Thread_Prioriry(boost::thread &Thread, int priority, int affinity)
ant_mode ant_mode_from_string(std::string str)
w_type string_to_w_type(std::string input_string)
std::vector< w_type > string_to_w_type_vector(std::vector< std::string > string_vector)
void print_debug(std::string text, double value)
void print_warning(std::string text)
int TCP_ASYNC_PORT