cloudFPGA (cF) API  1.0
The documentation of the source code of cloudFPGA (cF)
warp_transform_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 
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 warp_transform HLS TB
131  imwrite("../../../../../../ROLE/vision/hls/warp_transform/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/warp_transform/warp_transform_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/warp_transform/warp_transform_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/warp_transform/warp_transform_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/warp_transform/warp_transform_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 = "";
158  //clean_cmd = "make clean && ";
159  }
160  string str_command = "cd ../../../../../../ROLE/vision/hls/warp_transform/ && " + clean_cmd + synth_cmd + "\
161  INPUT_IMAGE=./test/input_from_udp_to_fpga.png " + exec_cmd + " && \
162  cd ../../../../HOST/vision/warp_transform/languages/cplusplus/build/ ";
163  const char *command = str_command.c_str();
164  cout << "Calling TB with command:" << command << endl;
165  system(command);
166 
167  free(longbuf);
168 
169  clock_t next_cycle_rx = clock();
170  double duration_rx = (next_cycle_rx - last_cycle_rx) / (double) CLOCKS_PER_SEC;
171  cout << "INFO: Effective FPS RX:" << (1 / duration_rx) << " \tkbps:" << (PACK_SIZE *
172  total_pack / duration_rx / 1024 * 8) << endl;
173  last_cycle_rx = next_cycle_rx;
174 
175 
176  // TX step
177  frame = cv::imread(ouf_file, cv::IMREAD_GRAYSCALE); // reading in the image in grey scale
178  if (!frame.data) {
179  cerr << "ERROR: Failed to load the image " << ouf_file << endl;
180  return -1;
181  }
182  else {
183  cout << "INFO: Succesfully loaded image " << ouf_file << endl;
184  }
185 
186  assert(frame.total() == FRAME_WIDTH * FRAME_HEIGHT);
187 
188  imshow("tb_send", frame);
189 
190  // Ensure that the send Mat is in continuous memory space. Typically, imread or resize
191  // will return such a continuous Mat, but we should check it.
192  assert(frame.isContinuous());
193 
194  // TX Loop
195  unsigned int sending_now = PACK_SIZE;
196  clock_t last_cycle_tx = clock();
197  for (int i = 0; i < total_pack; i++) {
198  if ( i == total_pack - 1 ) {
199  sending_now = bytes_in_last_pack;
200  }
201  #if NET_TYPE == udp
202  sock.sendTo( & frame.data[i * PACK_SIZE], sending_now, sourceAddress, servPort);
203  #else
204  //sock.send( & frame.data[i * PACK_SIZE], sending_now);
205  servsock->send( & frame.data[i * PACK_SIZE], sending_now);
206  #endif
207  }
208 
209  clock_t next_cycle_tx = clock();
210  double duration_tx = (next_cycle_tx - last_cycle_tx) / (double) CLOCKS_PER_SEC;
211  cout << "INFO: Effective FPS TX:" << (1 / duration_tx) << " \tkbps:" << (PACK_SIZE *
212  total_pack / duration_tx / 1024 * 8) << endl;
213  last_cycle_tx = next_cycle_tx;
214  cout << "\\___________________________________________________________________/" << endl;
215  break;} // while loop
216  #if NET_TYPE == tcp
217  delete servsock;
218  #endif
219  } catch (SocketException & e) {
220  cerr << e.what() << endl;
221  exit(1);
222  }
223  return 0;
224 }
225 
226 
227 
228 
#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
#define BUF_LEN
Definition: config.h:54
#define PACK_SIZE
Definition: config.h:51
int main(int argc, char *argv[])
def clock()
Definition: common.py:174