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