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