UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1/*
2* =====================================================================================
3*
4* Filename: main.cpp
5*
6* Description:
7*
8* Version: 1.0
9* Created: 04/12/2012 03:02:58 PM
10* Revision: none
11* Compiler: gcc
12*
13* Author: Milan Kabat (), kabat@ics.muni.cz
14* Organization:
15*
16* =====================================================================================
17*/
18#include <stdlib.h>
19#include <stdio.h>
20#include <time.h>
21#include <set>
22#include <unistd.h>
23#include <ctype.h>
24#include <string.h>
25
26#include <iostream>
27#include <fstream>
28
29#include "ldgm-session-cpu.h"
30#include "ldgm-session-gpu.h"
31#include "timer-util.h"
32
33using namespace std;
34
35//#define DATA_FNAME "data/data15.csv"
36//#define M 64 /* Number of parity symbols */
37//#define K 256 /* Number of source symbols */
38#define ROW_WEIGHT 3 /* Number of 1's in a row of parity matrix */
39#define COLUMN_WEIGHT 3 /* Number of 1's in a columns of parity matrix */
40//#define PACKET_SIZE 16384 /* Number of bytes in a packet */
41#define PACKET_LOSS 0.08 /* Packet loss rate */
42#define ITERATIONS 100
43
44void printMatrix ( char **m, int height, int width );
45void printData ( char *data, int count, int packet_size );
46void fillParityMatrix ( char** matrix, int height, int width );
47int demo( int m, int k, int frame_size, char* matrix_fname, char* data_fname, int cpu, int gpu);
48void demo_gpu();
49
50/*
51* === FUNCTION ======================================================================
52* Name: main
53* Description:
54* =====================================================================================
55*/
56 int
57main ( int argc, char *argv[] )
58{
59 short m, k, column_weight;
60 int frame_size;
61
62 opterr = 0;
63 int c;
64 int gpu = 0;
65 int cpu = 0;
66 char fname[32];
67 char matrix_fname[32];
68
69 while ( ( c = getopt ( argc, argv, "cf:gk:m:o:t:w:")) != -1 ) {
70 switch(c) {
71 case 'w':
72 column_weight = atoi ( optarg );
73 break;
74 case 'c':
75 if(gpu)
76 {
77 fprintf (stderr, "Only one of the options 'c' 'g' can be chosen.\n" );
78 return EXIT_FAILURE;
79 }
80 cpu = 1;
81 break;
82 case 'f':
83 frame_size = atoi ( optarg );
84 break;
85 case 'g':
86 if(cpu)
87 {
88 fprintf (stderr, "Only one of the options 'c' 'g' can be chosen.\n" );
89 return EXIT_FAILURE;
90 }
91 gpu = 1;
92 break;
93 case 'k':
94 k = atoi ( optarg );
95 break;
96 case 'm':
97 m = atoi ( optarg );
98 break;
99 case 'o':
100 sprintf(fname, optarg, 32);
101 break;
102 case 't':
103 sprintf(matrix_fname, optarg, 32);
104 break;
105 case '?':
106 if ( optopt == 'c' || optopt == 'k' || optopt == 'm' )
107 fprintf ( stderr, "Option -%c requires an argument.\n", optopt);
108 else if ( isprint ( optopt))
109 fprintf ( stderr, "Unknown option '-%c'.\n", optopt);
110 else
111 fprintf ( stderr, "Uknown option character '\\x%x'.\n", optopt);
112
113 return 1;
114 default:
115 abort();
116 }
117 }
118
119 demo( k, m, frame_size, matrix_fname, fname, cpu, gpu);
120
121 // demo_gpu();
122
123 return EXIT_SUCCESS;
124} /* ---------- end of function main ---------- */
125
126
127/*
128 * === FUNCTION ======================================================================
129 * Name: usage
130 * Description: Prints usage info
131 * =====================================================================================
132 */
133 void
135{
136 printf ( "\nUsage:\n" );
137
138}
139
140void compare_int_arrays ( int *a, int*b, int size )
141{
142 bool equal = true;
143 int idx = -1;
144 for ( int i = 0; i < size; i++ )
145 {
146 if ( a[i] != b[i] )
147 {
148 equal = false;
149 idx = i;
150 printf ( "p1: %d, p2: %d\n", a[i], b[i] );
151 break;
152// printf ( "Difference at integer position: %d\n", idx );
153// printf ( "Difference at packet position: %d\n", idx/2048 );
154 }
155 }
156 printf ( "Arrays are %s.\n", (equal)?"equal":"not equal" );
157
158}
159/*
160 * === FUNCTION ======================================================================
161 * Name: demo
162 * Description: Demonstration of coding
163 * =====================================================================================
164 */
165 int
166demo(int k, int m, int frame_size, char* matrix_fname, char* data_fname, int cpu, int gpu )
167{
168 Timer_util t;
169
170// LDGM_session_gpu* coding_session_gpu = new LDGM_session_gpu();
171 LDGM_session_cpu* coding_session_cpu = new LDGM_session_cpu();
172 LDGM_session_cpu* coding_session_cpu_2 = new LDGM_session_cpu();
173
174 // coding_session_gpu->set_params ( k, m, COLUMN_WEIGHT);
175 coding_session_cpu->set_params ( k, m, COLUMN_WEIGHT);
176 coding_session_cpu_2->set_params ( k, m, COLUMN_WEIGHT);
177 //coding_session_gpu->set_data_fname(data_fname);
178
179 coding_session_cpu->set_pcMatrix ( matrix_fname);
180 coding_session_cpu_2->set_pcMatrix ( matrix_fname);
181 //coding_session_gpu->set_pcMatrix ( matrix_fname);
182
183 //generate data
184 void *data;
185
186 //allocate memory with address aligned to 16 bytes
187 int e = posix_memalign(&data, 16, frame_size);
188
189 if ( e != 0 ) {
190 fprintf ( stderr, "Unable to allocate memory using posix_memalign.\n");
191 return EXIT_FAILURE;
192 }
193
194 for ( int i = 0; i < frame_size; ++i)
195 memset((char*)data+i, rand() % 256, 1);
196
197 //alocate space for parity packets
198
199// void *parity;
200 //allocate memory with address aligned to 16 bytes
201// e = posix_memalign(&parity, 16, (size_t)m*packet_size);
202//
203// if ( e != 0 ) {
204// fprintf ( stderr, "Unable to allocate memory using posix_memalign.\n");
205// return EXIT_FAILURE;
206// }
207
208
209// coding_session->add_src_head ( (char*) data, src_block, (uint16_t)0);
210
211 //print first K data packets
212// printData ( (char*) data, K, PACKET_SIZE);
213// printData ( (char*) parity, M, PACKET_SIZE);
214// void *parity2;
215 //allocate memory with address aligned to 16 bytes
216// e = posix_memalign(&parity2, 16, (size_t)m*packet_size);
217
218// if ( e != 0 ) {
219// fprintf ( stderr, "Unable to allocate memory using posix_memalign.\n");
220// return EXIT_FAILURE;
221// }
222
223// printf ( "k %d m %d frame_size %d, matrix %s data %s cpu %d gpu %d\n",
224// k, m, frame_size, matrix_fname, data_fname, cpu, gpu);
225
226// char *output = coding_session_cpu->encode_frame( (char*) data, packet_size);
227
228
229 char* output;
230 int buf_size;
231 int f_size;
232 char *decoded;
233 map<int, int> valid_data;
234 int ps;
235 srand(time(NULL));
236 if (cpu)
237 {
238 t.start();
239
240 for ( int i = 0; i < ITERATIONS; i++)
241 {
242 printf ( "encoding\n" );
243 output = coding_session_cpu->encode_frame ( (char*) data, frame_size, &buf_size );
244 ps = 1392; // coding_session_cpu->get_packet_size();
245 int total = 0;
246
247 valid_data.clear();
248
249 for ( int j = 0; j < buf_size ; j += ps) {
250 int size = ps;
251 if(j + ps > buf_size) {
252 size = buf_size - j;
253 }
254 if(rand() % 100 > PACKET_LOSS * 100 ) {
255 valid_data.insert(pair<int,int>(j, size));
256 total += size;
257 } else {
258 if(j == 0) {
259 fprintf(stderr, "Dropping first packet!!!!!!!!!!!!\n");
260 }
261 memset(output + j, 0x0, size);
262 }
263 }
264
265 int c;
266 printf ( "received bytes: %d\n", total );
267 printf ( "decoding\n" );
268 decoded = coding_session_cpu_2->decode_frame(output, buf_size, &f_size, valid_data);
269 printf ( "f_size: %d\n", f_size );
270 static int good = 0;
271 static int bad = 0;
272 if(f_size) good++; else bad++;
273 fprintf(stderr, "good: %d bad: %d\n", good, bad);
274
275 for (int x= 0; x < f_size; ++x) {
276 if(((char *)data)[x] != decoded[x]) {
277 fprintf(stderr, "Different character %d / %d (original size %d)\n", x, f_size, frame_size);
278 break;
279 }
280 }
281 //memset(decoded, 0, f_size);
282 coding_session_cpu->free_out_buf(output);
283 }
284 t.end();
285 // printf ( "CPU encoded in: %.6lf\n", t.elapsed_time(start, end) );
286 }
287 if (gpu)
288 {
289 t.start();
290
291 for ( int i = 0; i < ITERATIONS; i++)
292 // coding_session_gpu->encode ( (char*) data, (char*) frame_size );
293
294 t.end();
295 printf ( "GPU encoded in: %.6lf\n", t.elapsed_time() );
296 }
297
298
299#if 0
300 ofstream out;
301 out.open(data_fname, ios::out | ios::app);
302
303 out << t.elapsed_time() << ",";
304
305 out.close();
306
307 union int_bytes {
308 unsigned int number;
309 unsigned char bytes[4];
310 };
311 union int_bytes fs;
312 memcpy(&(fs.bytes), output, 4);
313
314 // printf ( "frame_size: \t\t%d\n", fs.number );
315 // printf ( "packet_size: \t\t%d\n", coding_session_cpu->get_packet_size() );
316
317
318 // printData ( (char*) output, 5, coding_session_cpu->get_packet_size());
319
320
321 coding_session_cpu->free_out_buf(output);
322#endif
323 delete coding_session_cpu;
324 delete coding_session_cpu_2;
325 // delete coding_session_gpu;
326 free ( data );
327
328 return EXIT_SUCCESS;
329}
330
331
332/*
333 * === FUNCTION ======================================================================
334 * Name: printData
335 * Description: Prints given data, each number represents a byte
336 * =====================================================================================
337 */
338 void
339printData ( char *data, int count, int packet_size )
340{
341 for ( int k = 0; k < count ; ++k) {
342 for ( int i = 0; i < packet_size; ++i )
343 printf ( "%d|", *((unsigned char*)(data) + k*packet_size + i ));
344 printf ( "---\n" );
345 }
346}
347
348/*
349 * === FUNCTION ======================================================================
350 * Name: printMatrix
351 * Description: Prints given matrix
352 * =====================================================================================
353 */
354void printMatrix ( char **m, int height, int width )
355{
356 for(int i = 0; i < height; ++i) {
357 for(int j = 0; j < width; ++j) {
358 printf("%3d", (unsigned char)m[i][j]);
359 }
360 printf("\n");
361 }
362 return;
363} /* ----- end of function printMatrix ----- */
364
365
366
char * decode_frame(char *received_data, int buf_size, int *frame_size, std::map< int, int > valid_data)
void free_out_buf(char *buf)
void set_pcMatrix(char *matrix)
char * encode_frame(char *frame, int frame_size, int *out_buf_size)
void set_params(unsigned short k, unsigned short m, unsigned short rw)
This class implements timer utils.
Definition timer-util.h:26
void end()
Definition timer-util.h:36
double elapsed_time() const
Definition timer-util.h:46
void start()
Definition timer-util.h:32
int count
Definition hd-rum.c:44
void fillParityMatrix(char **matrix, int height, int width)
#define PACKET_LOSS
Definition main.cpp:41
void usage()
Definition main.cpp:134
void printData(char *data, int count, int packet_size)
Definition main.cpp:339
int demo(int m, int k, int frame_size, char *matrix_fname, char *data_fname, int cpu, int gpu)
Definition main.cpp:166
void demo_gpu()
#define ITERATIONS
Definition main.cpp:42
void printMatrix(char **m, int height, int width)
Definition main.cpp:354
void compare_int_arrays(int *a, int *b, int size)
Definition main.cpp:140
#define COLUMN_WEIGHT
Definition main.cpp:39