cloudFPGA (cF) API  1.0
The documentation of the source code of cloudFPGA (cF)
gammacorrection_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  namedWindow("tb_recv", WINDOW_AUTOSIZE);
57  try {
58  UDPSocket sock(servPort);
59 
60  char buffer[BUF_LEN]; // Buffer for echo string
61  int recvMsgSize; // Size of received message
62  string sourceAddress; // Address of datagram source
63  unsigned short sourcePort; // Port of datagram source
64 
65  // RX Step
66  clock_t last_cycle_rx = clock();
67  while (1) {
68  // Block until receive message from a client
69 
70  int total_pack = 1 + (FRAME_TOTAL - 1) / PACK_SIZE;
71  int bytes_in_last_pack = (FRAME_TOTAL) - (total_pack - 1) * PACK_SIZE;
72  int receiving_now = PACK_SIZE;
73  cout << " ___________________________________________________________________ " << endl;
74  cout << "/ \\" << endl;
75  cout << "INFO: Proxy tb Frame # " << ++num_frame << endl;
76  cout << "INFO: Expecting length of packs:" << total_pack << endl;
77  char * longbuf = new char[PACK_SIZE * total_pack];
78 
79  // RX Loop
80  for (int i = 0; i < total_pack; i++) {
81  if ( i == total_pack - 1 ) {
82  receiving_now = bytes_in_last_pack;
83  }
84  recvMsgSize = sock.recvFrom(buffer, BUF_LEN, sourceAddress, sourcePort);
85  if (recvMsgSize != receiving_now) {
86  cerr << "ERROR: Received unexpected size pack:" << recvMsgSize << endl;
87  continue;
88  }
89  memcpy( & longbuf[i * PACK_SIZE], buffer, receiving_now);
90  }
91 
92  cout << "INFO: Received packet from " << sourceAddress << ":" << sourcePort << endl;
93 
94  cv::Mat frame = cv::Mat(FRAME_HEIGHT, FRAME_WIDTH, INPUT_TYPE_HOST, longbuf); // OR vec.data() instead of ptr
95  if (frame.size().width == 0) {
96  cerr << "ERROR: receive failure!" << endl;
97  continue;
98  }
99  imshow("tb_recv", frame);
100 
101  // We save the image received from network in order to process it with the gammacorrection HLS TB
102  imwrite("../../../../ROLE/vision/hls/gammacorrection/test/input_from_udp_to_fpga.png", frame);
103 
104  // Select simulation mode, default fcsim
105  string exec_cmd = "make fcsim -j 4";
106  string ouf_file = "../../../../ROLE/vision/hls/gammacorrection/gammacorrection_prj/solution1/fcsim/build/hls_out.jpg";
107  if (argc == 3) {
108  if (atoi(argv[2]) == 2) {
109  exec_cmd = "make csim";
110  ouf_file = "../../../../ROLE/vision/hls/gammacorrection/gammacorrection_prj/solution1/csim/build/hls_out.jpg";
111  }
112  else if (atoi(argv[2]) == 3) {
113  exec_cmd = "make cosim";
114  ouf_file = "../../../../ROLE/vision/hls/gammacorrection/gammacorrection_prj/solution1/cosim/build/hls_out.jpg";
115  }
116  else if (atoi(argv[2]) == 4) {
117  exec_cmd = "make kcachegrind";
118  ouf_file = "../../../../ROLE/vision/hls/gammacorrection/gammacorrection_prj/solution1/fcsim/build/hls_out.jpg";
119  }
120  }
121  // Calling the actual TB over its typical makefile procedure, but passing the save file
122  // Skip the rebuilding phase on the 2nd run. However ensure that it's a clean recompile
123  // the first time.
124  clean_cmd = " ";
125  if (num_frame == 1) {
126  clean_cmd = "make clean && ";
127  }
128  string str_command = "cd ../../../../ROLE/vision/hls/gammacorrection/ && " + clean_cmd + "\
129  INPUT_IMAGE=./test/input_from_udp_to_fpga.png " + exec_cmd + " && \
130  cd ../../../../HOST/vision/gammacorrection/build/ ";
131  const char *command = str_command.c_str();
132  cout << "Calling TB with command:" << command << endl;
133  system(command);
134 
135  free(longbuf);
136 
137  clock_t next_cycle_rx = clock();
138  double duration_rx = (next_cycle_rx - last_cycle_rx) / (double) CLOCKS_PER_SEC;
139  cout << "INFO: Effective FPS RX:" << (1 / duration_rx) << " \tkbps:" << (PACK_SIZE *
140  total_pack / duration_rx / 1024 * 8) << endl;
141  last_cycle_rx = next_cycle_rx;
142 
143 
144  // TX step
145  frame = cv::imread(ouf_file, cv::IMREAD_GRAYSCALE); // reading in the image in grey scale
146  if (!frame.data) {
147  cerr << "ERROR: Failed to load the image " << ouf_file << endl;
148  return -1;
149  }
150  else {
151  cout << "INFO: Succesfully loaded image " << ouf_file << endl;
152  }
153 
154  assert(frame.total() == FRAME_WIDTH * FRAME_HEIGHT);
155 
156  imshow("tb_send", frame);
157 
158  // Ensure that the send Mat is in continuous memory space. Typically, imread or resize
159  // will return such a continuous Mat, but we should check it.
160  assert(frame.isContinuous());
161 
162  // TX Loop
163  unsigned int sending_now = PACK_SIZE;
164  clock_t last_cycle_tx = clock();
165  for (int i = 0; i < total_pack; i++) {
166  if ( i == total_pack - 1 ) {
167  sending_now = bytes_in_last_pack;
168  }
169  sock.sendTo( & frame.data[i * PACK_SIZE], sending_now, sourceAddress, sourcePort);
170  }
171 
172  clock_t next_cycle_tx = clock();
173  double duration_tx = (next_cycle_tx - last_cycle_tx) / (double) CLOCKS_PER_SEC;
174  cout << "INFO: Effective FPS TX:" << (1 / duration_tx) << " \tkbps:" << (PACK_SIZE *
175  total_pack / duration_tx / 1024 * 8) << endl;
176  last_cycle_tx = next_cycle_tx;
177  cout << "\\___________________________________________________________________/" << endl;
178  } // while loop
179 
180  } catch (SocketException & e) {
181  cerr << e.what() << endl;
182  exit(1);
183  }
184  return 0;
185 }
186 
187 
188 
189 
int main(int argc, char *argv[])
#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
def clock()
Definition: common.py:174