Agora  1.2.0
Agora project
channel_sim.h
Go to the documentation of this file.
1 
5 #ifndef CHANNEL_SIM_H_
6 #define CHANNEL_SIM_H_
7 
8 #include <array>
9 #include <cstddef>
10 #include <memory>
11 #include <thread>
12 #include <vector>
13 
14 #include "channel.h"
15 #include "chsim_worker_storage.h"
16 #include "concurrentqueue.h"
17 #include "config.h"
18 #include "message.h"
19 #include "time_frame_counters.h"
20 #include "udp_comm.h"
21 
29 class ChannelSim {
30  public:
31  ChannelSim(const Config* const config, size_t bs_thread_num,
32  size_t user_thread_num, size_t worker_thread_num,
33  size_t in_core_offset = 30,
34  std::string in_chan_type = std::string("RAYLEIGH"),
35  double in_chan_snr = 20);
36  ~ChannelSim();
37 
38  void Run();
39 
40  static void* RxLoop(ChSimRxStorage* rx_storage);
41  // Loop thread receiving symbols from client antennas
42  void* UeRxLoop(size_t tid);
43 
44  // Loop thread receiving symbols from BS antennas
45  void* BsRxLoop(size_t tid);
46 
47  void ScheduleTask(EventData do_task,
49  moodycamel::ProducerToken const& ptok);
50 
51  // Calls DoTxBs / DoTxUser
52  void* TaskThread(size_t tid);
53  // Transmits symbol to BS antennas after applying channel
54  void DoTxBs(ChSimWorkerStorage* local, size_t tag);
55 
56  // Transmit symbols to client antennas after applying channel
57  void DoTxUser(ChSimWorkerStorage* local, size_t tag);
58 
59  private:
60  void DoTx(size_t frame_id, size_t symbol_id, size_t max_ant,
61  size_t ant_per_socket, const arma::cx_float* source_data,
62  SimdAlignByteVector* udp_pkt_buf,
63  std::vector<std::unique_ptr<UDPComm>>& udp_senders);
64 
65  std::vector<std::pair<std::thread, std::unique_ptr<ChSimRxStorage>>>
67  size_t AddRxThreads(
68  size_t desired_threads, size_t total_interfaces,
69  std::vector<std::unique_ptr<UDPComm>>& comm, ChSimRxBuffer* rx_buffer,
70  std::vector<std::pair<std::thread, std::unique_ptr<ChSimRxStorage>>>&
71  rx_threads_out);
72 
73  // BS-facing sockets
74  std::vector<std::unique_ptr<UDPComm>> bs_comm_;
75  // UE-facing sockets
76  std::vector<std::unique_ptr<UDPComm>> ue_comm_;
77 
78  const Config* const cfg_;
79  std::unique_ptr<Channel> channel_;
80 
81  // Data buffer for received symbols from BS antennas (downlink)
82  std::unique_ptr<ChSimRxBuffer> rx_buffer_bs_;
83 
84  // Data buffer for received symbols from client antennas (uplink)
85  std::unique_ptr<ChSimRxBuffer> rx_buffer_ue_;
86 
87  // Task Queue for tasks related to incoming BS packets
89 
90  // Task Queue for tasks related to incoming Users' packets
92 
93  // Master thread's message queue for event completions;
95  std::array<std::unique_ptr<moodycamel::ProducerToken>, kMaxThreads>
97 
98  std::vector<std::thread> task_threads_;
99 
103 
109  size_t core_offset_;
110 
111  std::string channel_type_;
112  double channel_snr_;
113 
118 };
119 
120 #endif // CHANNEL_SIM_H_
FrameStats::NumPilotSyms
size_t NumPilotSyms() const
Definition: framestats.cc:91
chsim_worker_storage.h
Declaration file for the ChSimWorkerStorage class.
fmt::v8::detail::byte
byte
Definition: core.h:388
udp_comm.h
Declaration file for the UDPComm class. This class is used to send messages and receive messages from...
ChannelSim::DoTxUser
void DoTxUser(ChSimWorkerStorage *local, size_t tag)
Definition: channel_sim.cc:560
moodycamel::ProducerToken
Definition: concurrentqueue.h:630
ChannelSim::bs_thread_num_
size_t bs_thread_num_
Definition: channel_sim.h:104
Packet::frame_id_
uint32_t frame_id_
Definition: message.h:168
Config::UeAntNum
size_t UeAntNum() const
Definition: config.h:41
ChannelSim::task_queue_bs_
moodycamel::ConcurrentQueue< EventData > task_queue_bs_
Definition: channel_sim.h:88
PinToCoreWithOffset
void PinToCoreWithOffset(ThreadType thread_type, size_t core_offset, size_t thread_id, bool allow_reuse, bool verbose)
Definition: utils.cc:157
Config::SampsPerSymbol
size_t SampsPerSymbol() const
Definition: config.h:234
datatype_conversion.h
Config::NumChannels
size_t NumChannels() const
Definition: config.h:98
FrameStats::NumBeaconSyms
size_t NumBeaconSyms() const
Definition: framestats.cc:87
channel_sim.h
Declaration file for the channel simulator class.
ChannelSim::ue_tx_
TimeFrameCounters ue_tx_
Definition: channel_sim.h:115
ChSimRxStorage::SocketOffset
size_t SocketOffset() const
Definition: chsim_worker_storage.h:190
channel.h
Implementation file for the channel class.
ChannelSim::ScheduleTask
void ScheduleTask(EventData do_task, moodycamel::ConcurrentQueue< EventData > *in_queue, moodycamel::ProducerToken const &ptok)
Definition: channel_sim.cc:136
moodycamel::ConcurrentQueue::try_dequeue_bulk
size_t try_dequeue_bulk(It itemFirst, size_t max)
Definition: concurrentqueue.h:1210
SymbolType::kPilot
@ kPilot
gen_tag_t::symbol_id_
uint16_t symbol_id_
Definition: message.h:33
ChannelSim::core_offset_
size_t core_offset_
Definition: channel_sim.h:109
kFrameWnd
static constexpr size_t kFrameWnd
Definition: symbols.h:18
ChannelSim::message_queue_
moodycamel::ConcurrentQueue< EventData > message_queue_
Definition: channel_sim.h:94
SocketRxBuffer::DataSize
size_t DataSize() const
Definition: channel_sim.cc:35
Packet::data_
short data_[]
Definition: message.h:173
ThreadType::kWorkerTXRX
@ kWorkerTXRX
GetTime::GetTimeUs
static double GetTimeUs()
Definition: gettime.h:14
ChannelSim::bs_comm_
std::vector< std::unique_ptr< UDPComm > > bs_comm_
Definition: channel_sim.h:74
SignalHandler::GotExitSignal
static bool GotExitSignal()
Definition: signal_handler.cc:23
ChannelSim::task_threads_
std::vector< std::thread > task_threads_
Definition: channel_sim.h:98
Config::BsAntNum
size_t BsAntNum() const
Definition: config.h:35
Packet::ant_id_
uint32_t ant_id_
Definition: message.h:171
ChannelSim::payload_length_
size_t payload_length_
Definition: channel_sim.h:102
ChannelSim::~ChannelSim
~ChannelSim()
Definition: channel_sim.cc:126
EventType::kPacketTX
@ kPacketTX
ChannelSim::DoTx
void DoTx(size_t frame_id, size_t symbol_id, size_t max_ant, size_t ant_per_socket, const arma::cx_float *source_data, SimdAlignByteVector *udp_pkt_buf, std::vector< std::unique_ptr< UDPComm >> &udp_senders)
Warning: Threads are sharing these sender sockets.
Definition: channel_sim.cc:433
ChannelSim::cfg_
const Config *const cfg_
Definition: channel_sim.h:78
SocketRxBuffer::StorageSize
size_t StorageSize() const
Definition: channel_sim.cc:36
ChannelSim::ue_comm_
std::vector< std::unique_ptr< UDPComm > > ue_comm_
Definition: channel_sim.h:76
SocketRxBuffer::RemoveData
void RemoveData(size_t remove)
Definition: channel_sim.cc:47
EventData::tags_
std::array< size_t, kMaxTags > tags_
Definition: message.h:146
ChannelSim::DoTxBs
void DoTxBs(ChSimWorkerStorage *local, size_t tag)
Definition: channel_sim.cc:471
AGORA_LOG_ERROR
#define AGORA_LOG_ERROR(...)
Definition: logger.h:39
SocketRxBuffer::data_size_
size_t data_size_
Definition: channel_sim.cc:60
signal_handler.h
Config::Frame
const FrameStats & Frame() const
Definition: config.h:340
ChSimRxStorage::ResponseQueue
moodycamel::ConcurrentQueue< EventData > & ResponseQueue()
Definition: chsim_worker_storage.h:197
kPrintDebugTxUser
static constexpr bool kPrintDebugTxUser
Definition: channel_sim.cc:19
ChannelSim::RxLoop
static void * RxLoop(ChSimRxStorage *rx_storage)
Definition: channel_sim.cc:363
Config::UeServerPort
int UeServerPort() const
Definition: config.h:306
ThreadType::kWorker
@ kWorker
AGORA_LOG_TRACE
#define AGORA_LOG_TRACE(...)
Definition: logger.h:92
ChSimRxStorage::PacketLength
size_t PacketLength() const
Definition: chsim_worker_storage.h:189
CreateCommSockets
static std::vector< std::unique_ptr< UDPComm > > CreateCommSockets(const std::string &local_address, int local_port, const std::string &remote_address, int remote_port, size_t interface_count)
Definition: channel_sim.cc:651
gen_tag_t
Definition: message.h:22
ChannelSim::ChannelSim
ChannelSim(const Config *const config, size_t bs_thread_num, size_t user_thread_num, size_t worker_thread_num, size_t in_core_offset=30, std::string in_chan_type=std::string("RAYLEIGH"), double in_chan_snr=20)
Definition: channel_sim.cc:64
TimeFrameCounters::CompleteSymbol
bool CompleteSymbol(size_t frame_id)
Definition: time_frame_counters.h:23
ChannelSim::ue_rx_
TimeFrameCounters ue_rx_
Definition: channel_sim.h:114
ChannelSim::channel_type_
std::string channel_type_
Definition: channel_sim.h:111
kDebugPrintInTask
static constexpr bool kDebugPrintInTask
Definition: symbols.h:201
SimdConvertShortToFloat
static void SimdConvertShortToFloat(const short *in_buf, float *out_buf, size_t n_elems)
Definition: datatype_conversion.h:126
ChannelSim::channel_
std::unique_ptr< Channel > channel_
Definition: channel_sim.h:79
EventData
Definition: message.h:142
TimeFrameCounters::GetTaskTotalTimeMs
double GetTaskTotalTimeMs(size_t frame_id, size_t symbol_id) const
Definition: time_frame_counters.h:59
ChannelSim::CreateRxThreads
std::vector< std::pair< std::thread, std::unique_ptr< ChSimRxStorage > > > CreateRxThreads()
Definition: channel_sim.cc:709
Config::BsRruPort
int BsRruPort() const
Definition: config.h:305
ChSimRxStorage::CoreId
size_t CoreId() const
Definition: chsim_worker_storage.h:188
ChannelSim::bs_tx_
TimeFrameCounters bs_tx_
Definition: channel_sim.h:117
gen_tag_t::ant_id_
uint16_t ant_id_
Definition: message.h:38
SocketRxBuffer::Rx
std::byte * Rx()
Definition: channel_sim.cc:37
message.h
Self defined functions for message storage and passing.
TimeFrameCounters
Definition: time_frame_counters.h:15
Config::BsServerPort
int BsServerPort() const
Definition: config.h:304
gen_tag_t::FrmSymAnt
static gen_tag_t FrmSymAnt(size_t frame_id, size_t symbol_id, size_t ant_id)
Definition: message.h:106
TimeFrameCounters::Reset
void Reset(size_t frame_id)
Definition: time_frame_counters.h:22
kChannelTimeWarning
static constexpr double kChannelTimeWarning
Definition: channel_sim.cc:22
ChSimRxBuffer
Definition: chsim_worker_storage.h:115
i
for i
Definition: generate_data.m:107
ChSimRxStorage::Id
size_t Id() const
Definition: chsim_worker_storage.h:187
Packet::kOffsetOfData
static constexpr size_t kOffsetOfData
Definition: message.h:166
moodycamel::ConcurrentQueue::enqueue
bool enqueue(T const &item)
Definition: concurrentqueue.h:974
ChSimRxStorage
Definition: chsim_worker_storage.h:171
SymbolType::kBeacon
@ kBeacon
u
Plot Rx waveform for u
Definition: inspect_single_frame.m:108
ChSimRxStorage::TransferRxData
void TransferRxData(size_t frame, size_t symbol, size_t ant, const short *input, size_t data_size)
Definition: chsim_worker_storage.h:193
ChannelSim::Run
void Run()
Definition: channel_sim.cc:148
ChSimRxStorage::Socket
UDPComm * Socket(size_t id)
Definition: chsim_worker_storage.h:192
Config::BsServerAddr
std::string BsServerAddr() const
Definition: config.h:299
ChSimWorkerStorage
Definition: chsim_worker_storage.h:19
ChannelSim::user_thread_num_
size_t user_thread_num_
Definition: channel_sim.h:105
Packet
Definition: message.h:164
UDPComm::Recv
ssize_t Recv(std::byte *buf, size_t len) const
Try to receive up to len bytes in buf by default this will not block.
Definition: udp_comm.cc:372
SocketRxBuffer::SocketRxBuffer
SocketRxBuffer(size_t num_bytes)
Definition: channel_sim.cc:32
EventType::kPacketRX
@ kPacketRX
Packet::symbol_id_
uint32_t symbol_id_
Definition: message.h:169
ChannelSim::BsRxLoop
void * BsRxLoop(size_t tid)
ChannelSim::rx_buffer_ue_
std::unique_ptr< ChSimRxBuffer > rx_buffer_ue_
Definition: channel_sim.h:85
ChannelSim::task_queue_user_
moodycamel::ConcurrentQueue< EventData > task_queue_user_
Definition: channel_sim.h:91
ChannelSim::dl_data_plus_beacon_symbols_
size_t dl_data_plus_beacon_symbols_
Definition: channel_sim.h:101
TimeFrameCounters::GetTaskEndTimeUs
double GetTaskEndTimeUs(size_t frame_id, size_t symbol_id) const
Definition: time_frame_counters.h:81
TimeFrameCounters::CompleteTask
bool CompleteTask(size_t frame_id, size_t symbol_id)
Definition: time_frame_counters.h:35
ChannelSim::channel_snr_
double channel_snr_
Definition: channel_sim.h:112
SocketRxBuffer::At
const std::byte * At(size_t location) const
Definition: channel_sim.cc:38
ChannelSim::ul_data_plus_pilot_symbols_
size_t ul_data_plus_pilot_symbols_
Definition: channel_sim.h:100
SimdAlignByteVector
std::vector< std::byte, boost::alignment::aligned_allocator< std::byte, kSimdAlignment > > SimdAlignByteVector
Definition: simd_types.h:18
FrameStats::NumDLSyms
size_t NumDLSyms() const
Definition: framestats.cc:83
moodycamel::ConcurrentQueue< EventData >
extract_version.data
dictionary data
Definition: extract_version.py:8
SocketRxBuffer::AddData
void AddData(size_t data)
Definition: channel_sim.cc:41
Config::GetSymbolType
SymbolType GetSymbolType(size_t symbol_id) const
Return the symbol type of this symbol in this frame.
Definition: config.cc:1557
ChannelSim::user_socket_num_
size_t user_socket_num_
Definition: channel_sim.h:107
FrameStats::NumULSyms
size_t NumULSyms() const
Definition: framestats.cc:85
ChannelSim::worker_thread_num_
size_t worker_thread_num_
Definition: channel_sim.h:108
AGORA_LOG_SYMBOL
#define AGORA_LOG_SYMBOL(...)
Definition: logger.h:82
kMaxThreads
static constexpr size_t kMaxThreads
Definition: symbols.h:309
kEnableChannelTiming
static constexpr bool kEnableChannelTiming
Definition: channel_sim.cc:21
kUdpMTU
static constexpr size_t kUdpMTU
Definition: channel_sim.cc:24
Config::NumUeChannels
size_t NumUeChannels() const
Definition: config.h:99
Config
Definition: config.h:26
AGORA_LOG_INFO
#define AGORA_LOG_INFO(...)
Definition: logger.h:62
std
Definition: json.hpp:5213
SocketRxBuffer
Definition: channel_sim.cc:30
AGORA_LOG_FRAME
#define AGORA_LOG_FRAME(...)
Definition: logger.h:72
RtAssert
static void RtAssert(bool condition, const char *throw_str)
Definition: utils.h:104
kDebugPrintPerSymbolDone
static constexpr bool kDebugPrintPerSymbolDone
Definition: symbols.h:198
AGORA_LOG_WARN
#define AGORA_LOG_WARN(...)
Definition: logger.h:53
ChannelSim::bs_rx_
TimeFrameCounters bs_rx_
Definition: channel_sim.h:116
ChannelSim::TaskThread
void * TaskThread(size_t tid)
Definition: channel_sim.cc:340
spdlog::details::os::remove
SPDLOG_INLINE int remove(const filename_t &filename) SPDLOG_NOEXCEPT
Definition: os-inl.h:163
kPrintDebugTxBs
static constexpr bool kPrintDebugTxBs
Definition: channel_sim.cc:20
TimeFrameCounters::Init
void Init(size_t max_symbol_count, size_t max_task_count=0)
Definition: time_frame_counters.h:19
SocketRxBuffer::~SocketRxBuffer
~SocketRxBuffer()=default
to_string
std::string to_string() const
Definition: eth_common.h:64
SimdConvertFloatToShort
static void SimdConvertFloatToShort(const float *in_buf, short *out_buf, size_t n_elems, size_t n_prefix=0, float scale_down_factor=1.0f)
Definition: datatype_conversion.h:266
ThreadType::kMaster
@ kMaster
Config::UeRruPort
int UeRruPort() const
Definition: config.h:307
config.h
Declaration file for the configuration class which importants json configuration values into class va...
kDequeueBulkSize
static constexpr size_t kDequeueBulkSize
Definition: channel_sim.cc:25
kPrintChannelOutput
static constexpr bool kPrintChannelOutput
Definition: channel_sim.cc:17
moodycamel::ConcurrentQueue::try_dequeue
bool try_dequeue(U &item)
Definition: concurrentqueue.h:1104
running
static std::atomic< bool > running
Definition: channel_sim.cc:16
moodycamel::ConsumerToken
Definition: concurrentqueue.h:695
time_frame_counters.h
Declaration file for the TimeFrameCounters class.
moodycamel::ConcurrentQueue::try_enqueue
bool try_enqueue(T const &item)
Definition: concurrentqueue.h:1039
gettime.h
Config::PacketLength
size_t PacketLength() const
Definition: config.h:238
ChannelSim
Simualtor for many-antenna MU-MIMO channel to work with Agora BS and UE applications....
Definition: channel_sim.h:29
ChannelSim::rx_buffer_bs_
std::unique_ptr< ChSimRxBuffer > rx_buffer_bs_
Definition: channel_sim.h:82
gen_tag_t::frame_id_
uint32_t frame_id_
Definition: message.h:32
Utils::PrintMat
static void PrintMat(const arma::cx_fmat &c, const std::string &ss)
Definition: utils.cc:452
ChannelSim::AddRxThreads
size_t AddRxThreads(size_t desired_threads, size_t total_interfaces, std::vector< std::unique_ptr< UDPComm >> &comm, ChSimRxBuffer *rx_buffer, std::vector< std::pair< std::thread, std::unique_ptr< ChSimRxStorage >>> &rx_threads_out)
Definition: channel_sim.cc:673
ChannelSim::bs_socket_num_
size_t bs_socket_num_
Definition: channel_sim.h:106
ChannelSim::task_ptok_
std::array< std::unique_ptr< moodycamel::ProducerToken >, kMaxThreads > task_ptok_
Definition: channel_sim.h:96
SymbolType::kUL
@ kUL
Config::UeRruAddr
std::string UeRruAddr() const
Definition: config.h:301
kDebugPrintPerFrameDone
static constexpr bool kDebugPrintPerFrameDone
Definition: symbols.h:196
concurrentqueue.h
kSockBufSize
static constexpr size_t kSockBufSize
Definition: channel_sim.cc:26
Config::UeServerAddr
std::string UeServerAddr() const
Definition: config.h:298
Config::BsRruAddr
std::string BsRruAddr() const
Definition: config.h:302
FrameStats::NumTotalSyms
size_t NumTotalSyms() const
Definition: framestats.cc:93
SymbolType::kDL
@ kDL
ChannelSim::UeRxLoop
void * UeRxLoop(size_t tid)
ChSimRxStorage::SocketNumber
size_t SocketNumber() const
Definition: chsim_worker_storage.h:191
SocketRxBuffer::data_
SimdAlignByteVector data_
Definition: channel_sim.cc:61
kDefaultQueueSize
static constexpr size_t kDefaultQueueSize
Definition: channel_sim.cc:18