cloudFPGA (cF) API  1.0
The documentation of the source code of cloudFPGA (cF)
median_blur_host_fwd_tb.cpp
Go to the documentation of this file.
1 
17 
32 #include <cstdlib> // For atoi()
33 #include <iostream> // For cout and cerr
34 #include "PracticalSockets.h"
35 #include "config.h"
36 #include "opencv2/opencv.hpp"
37 #include "util.hpp"
38 
39 using namespace cv;
40 
41 
46 int main(int argc, char * argv[]) {
47 
48  if ((argc < 2) || (argc > 3)) { // Test for correct number of parameters
49  cerr << "Usage: " << argv[0] << " <Server Port> <optional simulation mode>" << endl;
50  exit(1);
51  }
52 
53  if (!isNumeric(argv[1])) {
54  cerr << "ERROR: Unidentified provided port : " << argv[1] << " . Aborting..." << endl;
55  exit(1);
56  }
57 
58  unsigned short servPort = atoi(argv[1]); // First arg: local port
59  unsigned int num_frame = 0;
60  string clean_cmd;
61 
62 #ifdef SHOW_WINDOWS
63  namedWindow("tb_recv", WINDOW_AUTOSIZE);
64 #endif // SHOW_WINDOWS
65 
66  try {
67  #if NET_TYPE == udp
68  UDPSocket sock(servPort);
69  #else
70  TCPServerSocket servSock(servPort); // Server Socket object
71  TCPSocket *servsock = servSock.accept(); // Wait for a client to connect
72  #endif
73  char buffer[BUF_LEN]; // Buffer for echo string
74  int recvMsgSize; // Size of received message
75  string sourceAddress; // Address of datagram source
76 
77  #if NET_TYPE == tcp
78  // TCP client handling
79  cout << "Handling client ";
80  try {
81  cout << servsock->getForeignAddress() << ":";
82  } catch (SocketException e) {
83  cerr << "Unable to get foreign address" << endl;
84  }
85  try {
86  cout << servsock->getForeignPort();
87  } catch (SocketException e) {
88  cerr << "Unable to get foreign port" << endl;
89  }
90  cout << endl;
91  #endif
92 
93 
94  // RX Step
95  clock_t last_cycle_rx = clock();
96  while (1) {
97  // Block until receive message from a client
98 
99  int total_pack = 1 + (FRAME_TOTAL - 1) / PACK_SIZE;
100  int bytes_in_last_pack = (FRAME_TOTAL) - (total_pack - 1) * PACK_SIZE;
101  int receiving_now = PACK_SIZE;
102  cout << " ___________________________________________________________________ " << endl;
103  cout << "/ \\" << endl;
104  cout << "INFO: Proxy tb Frame # " << ++num_frame << endl;
105  cout << "INFO: Expecting length of packs:" << total_pack << endl;
106  char * longbuf = new char[PACK_SIZE * total_pack];
107 
108  // RX Loop
109  for (int i = 0; i < total_pack; i++) {
110  if ( i == total_pack - 1 ) {
111  receiving_now = bytes_in_last_pack;
112  }
113  #if NET_TYPE == udp
114  recvMsgSize = sock.recvFrom(buffer, BUF_LEN, sourceAddress, servPort);
115  #else
116  recvMsgSize = servsock->recv(buffer, receiving_now);
117  #endif
118  if (recvMsgSize != receiving_now) {
119  cerr << "ERROR: Received unexpected size pack:" << recvMsgSize << endl;
120  continue;
121  }
122  memcpy( & longbuf[i * PACK_SIZE], buffer, receiving_now);
123  }
124 
125  cout << "INFO: Received packet from " << sourceAddress << ":" << servPort << endl;
126 
127  cv::Mat frame = cv::Mat(FRAME_HEIGHT, FRAME_WIDTH, INPUT_TYPE_HOST, longbuf); // OR vec.data() instead of ptr
128  if (frame.size().width == 0) {
129  cerr << "ERROR: receive failure!" << endl;
130  continue;
131  }
132 #ifdef SHOW_WINDOWS
133  imshow("tb_recv", frame);
134 #endif // SHOW_WINDOWS
135 
136  // We save the image received from network in order to process it with the median_blur HLS TB
137  imwrite("../../../../../../ROLE/vision/hls/median_blur/test/input_from_udp_to_fpga.png", frame);
138 
139  // Select simulation mode, default fcsim
140  string synth_cmd = " ";
141  string exec_cmd = "make fcsim -j 4";
142  string ouf_file = "../../../../../../ROLE/vision/hls/median_blur/median_blur_prj/solution1/fcsim/build/hls_out.jpg";
143  if (argc == 3) {
144  if (atoi(argv[2]) == 2) {
145  exec_cmd = "make csim";
146  ouf_file = "../../../../../../ROLE/vision/hls/median_blur/median_blur_prj/solution1/csim/build/hls_out.jpg";
147  }
148  else if (atoi(argv[2]) == 3) {
149  synth_cmd = "make csynth && ";
150  exec_cmd = "make cosim";
151  ouf_file = "../../../../../../ROLE/vision/hls/median_blur/median_blur_prj/solution1/sim/wrapc_pc/hls_out.jpg";
152  }
153  else if (atoi(argv[2]) == 4) {
154  exec_cmd = "make kcachegrind";
155  ouf_file = "../../../../../../ROLE/vision/hls/median_blur/median_blur_prj/solution1/fcsim/build/hls_out.jpg";
156  }
157  }
158  // Calling the actual TB over its typical makefile procedure, but passing the save file
159  // Skip the rebuilding phase on the 2nd run. However ensure that it's a clean recompile
160  // the first time.
161  clean_cmd = " ";
162  if (num_frame == 1) {
163  clean_cmd = "make clean && ";
164  }
165  string str_command = "cd ../../../../../../ROLE/vision/hls/median_blur/ && " + clean_cmd + synth_cmd + "\
166  INPUT_IMAGE=./test/input_from_udp_to_fpga.png " + exec_cmd + " && \
167  cd ../../../../HOST/vision/median_blur/languages/cplusplus/build/ ";
168  const char *command = str_command.c_str();
169  cout << "Calling TB with command:" << command << endl;
170  system(command);
171 
172  free(longbuf);
173 
174  clock_t next_cycle_rx = clock();
175  double duration_rx = (next_cycle_rx - last_cycle_rx) / (double) CLOCKS_PER_SEC;
176  cout << "INFO: Effective FPS RX:" << (1 / duration_rx) << " \tkbps:" << (PACK_SIZE *
177  total_pack / duration_rx / 1024 * 8) << endl;
178  last_cycle_rx = next_cycle_rx;
179 
180 
181  // TX step
182  frame = cv::imread(ouf_file, cv::IMREAD_GRAYSCALE); // reading in the image in grey scale
183  if (!frame.data) {
184  cerr << "ERROR: Failed to load the image " << ouf_file << endl;
185  return -1;
186  }
187  else {
188  cout << "INFO: Succesfully loaded image " << ouf_file << endl;
189  }
190 
191  assert(frame.total() == FRAME_WIDTH * FRAME_HEIGHT);
192 
193  imshow("tb_send", frame);
194 
195  // Ensure that the send Mat is in continuous memory space. Typically, imread or resize
196  // will return such a continuous Mat, but we should check it.
197  assert(frame.isContinuous());
198 
199  // TX Loop
200  unsigned int sending_now = PACK_SIZE;
201  clock_t last_cycle_tx = clock();
202  for (int i = 0; i < total_pack; i++) {
203  if ( i == total_pack - 1 ) {
204  sending_now = bytes_in_last_pack;
205  }
206  #if NET_TYPE == udp
207  sock.sendTo( & frame.data[i * PACK_SIZE], sending_now, sourceAddress, servPort);
208  #else
209  //sock.send( & frame.data[i * PACK_SIZE], sending_now);
210  servsock->send( & frame.data[i * PACK_SIZE], sending_now);
211  #endif
212  }
213 
214  clock_t next_cycle_tx = clock();
215  double duration_tx = (next_cycle_tx - last_cycle_tx) / (double) CLOCKS_PER_SEC;
216  cout << "INFO: Effective FPS TX:" << (1 / duration_tx) << " \tkbps:" << (PACK_SIZE *
217  total_pack / duration_tx / 1024 * 8) << endl;
218  last_cycle_tx = next_cycle_tx;
219  cout << "\\___________________________________________________________________/" << endl;
220  break;} // while loop
221  #if NET_TYPE == tcp
222  delete servsock;
223  #endif
224  } catch (SocketException & e) {
225  cerr << e.what() << endl;
226  exit(1);
227  }
228  return 0;
229 }
230 
231 
232 
233 
#define FRAME_TOTAL
Definition: config.h:74
#define INPUT_TYPE_HOST
Definition: config.h:68
#define FRAME_HEIGHT
Definition: config.h:43
#define FRAME_WIDTH
Definition: config.h:46
int main(int argc, char *argv[])
#define BUF_LEN
Definition: config.h:54
#define PACK_SIZE
Definition: config.h:51
def clock()
Definition: common.py:174
Common utility functions headers, i.e. timing, printing wrappers.
bool isNumeric(const std::string str)
Definition: util.cpp:58