-
Notifications
You must be signed in to change notification settings - Fork 2
/
hotstream.c
534 lines (409 loc) · 12.9 KB
/
hotstream.c
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
#include "hotstream.h"
#include <stdio.h>
pd_device_t dev; // PCIExpress based device handler
int patternApplied = 0; // Boolean flag to indicate whether a pattern has been applied to a memory buffer
int sendTransfType = 0; // Flag to indicate the type of send transfer being initiated (0 - Host to DDR; 1 - Host to Backplane; 2 - Host to Instruction Stream
int recvTransfType = 0; // Flag to indicate the type of recv transfer being initiated (0 - DDR to Host; 1 - Backplane to Host; 2 - Instruction Stream to Host
// Core Management
/* Reads a binary file produced by the Micro16 assembler into a buffer pointed to by idata buf
* fname - Name of the binary output by the Micro16 assembler
* idata_buf - Pointer to the buffer that will hold the instruction data
* buf_size - Pointer to buffer size
* Returns: 0 on success; -1 otherwise
*/
int HotStream_readBin(char *fname, void *idata_buf, int *buf_size)
{
FILE *finstr;
char line[10];
int instrcount = 0,i;
short test;
PRINT("Opening binary file %s\n",fname);
finstr = fopen(fname, "r");
if (finstr == NULL){
PRINT("Could not open binary file %s\n",fname);
return -1;
}
while(fgets(line, 10, finstr) != NULL)
{
instrcount++;
}
*buf_size = instrcount*2;
PRINT("Allocating a %d bytes buffer\n", *buf_size);
if(posix_memalign((void**)&idata_buf, 16, instrcount*2) != 0)
return -1;
// Rewind file
rewind(finstr);
i = 0;
while(fgets(line, 10, finstr) != NULL)
{
sscanf(line, "%04X",&((short*)idata_buf)[i]);
i++;
}
fclose(finstr);
return 0;
}
int HotStream_copyInstrData(int CoreNum, void *idata0, int idata0_size, void *idata1, int idata1_size)
{
int DSSdest;
int bar0, bar1;
unsigned int ICR_data = 0;
// Configure DataStreamSwitch(DSS)
DSSdest = 2; // 10 - Shared Memory
DSSdest = DSSdest << 4;
cmuWrite(CMU_KCR, (DSSdest & CMU_KCR_Dest));
bar0 = 0; // Starting position of first instruction block
bar1 = bar0 + idata0_size/2; // Second instruction block starts right after the first
if(bar1 + idata1_size/2 > 1024){
PRINT("Total instruction size for core %d exceeds local memory capacity\n",CoreNum);
return -1;
}
ICR_data = ICR_data | ((bar0 << 4) & CMU_ICR_IBA); // Set BAR0
ICR_data = ICR_data | ((0 << 1) & CMU_ICR_IO); // Select first partition (0)
ICR_data = ICR_data | ((CoreNum << 16) & CMU_ICR_CS); // Select Instruction Block to configure
ICR_data = ICR_data | (1 & CMU_ICR_RS); // Start instruction streaming
// Copy buffer 0 to the Instruction Stream
//-------------------------------------------
// Configure DataStreamSwitch(DSS)
DSSdest = 2; // 10 - Instruction Stream
DSSdest = DSSdest << 4;
cmuWrite(CMU_KCR, (DSSdest & CMU_KCR_Dest));
// Setup data copy from the host memory to the MM2S interface (stream target - CoreNum)
if(setupSend(&dev, idata0, idata0_size,CoreNum) < 0){
PRINT("Could not setup a data copy from the Host to the MM2S interface and Instruction Stream\n");
return -1;
}
sendTransfType = 1;
if(HotStream_linear_send(0, idata0_size) < 0){
PRINT("Could not apply linear pattern to the instruction stream\n");
return -1;
}
// Start send and wait on interrupt
if(HotStream_startSend(1) < 0){
PRINT("Could not stream instruction data to the selected core\n");
return -1;
}
// Verify send transfer and terminate memory mapping
if(HotStream_checkSend(1) < 0){
PRINT("Instruction streaming was not successful\n");
return -1;
}
ICR_data = ICR_data | ((bar1 << 4) & CMU_ICR_IBA); // Set BAR1
ICR_data = ICR_data | ((1 << 1) & CMU_ICR_IO); // Select first partition (1)
ICR_data = ICR_data | ((CoreNum << 16) & CMU_ICR_CS); // Select Instruction Block to configure
ICR_data = ICR_data | (1 & CMU_ICR_RS); // Start instruction streaming
// Copy buffer 1 to the Instruction Stream
// Setup data copy from the host memory to the MM2S interface (stream target - CoreNum)
if(setupSend(&dev, idata1, idata1_size,CoreNum) < 0){
PRINT("Could not setup a data copy from the Host to the MM2S interface and Instruction Stream\n");
return -1;
}
sendTransfType = 1;
if(HotStream_linear_send(0, idata1_size) < 0){
PRINT("Could not apply linear pattern to the instruction stream\n");
return -1;
}
// Start send and wait on interrupt
if(HotStream_startSend(1) < 0){
PRINT("Could not stream instruction data to the selected core\n");
return -1;
}
// Verify send transfer and terminate memory mapping
if(HotStream_checkSend(1) < 0){
PRINT("Instruction streaming was not successful\n");
return -1;
}
return 0;
}
void HotStream_setIntVector(unsigned long intv)
{
unsigned int mask32 = 0xFFFF;
cmuWrite(CMU_KCML,(unsigned int)(intv & mask32));
cmuWrite(CMU_KCMH,(unsigned int)((intv << 32) & mask32));
cmuWrite(CMU_KCR, CMU_KCR_SetInt); // Apply interrupt vector
}
void HotStream_resetCore(int CoreNum)
{
int bit = 1;
unsigned int mask = 0;
mask = mask | (bit << CoreNum);
genReset(mask);
}
void HotStream_resetCoreVector(unsigned long rstv)
{
genReset(rstv);
}
unsigned long HotStream_getInt()
{
unsigned long retval;
retval = cmuRead(CMU_KIVL);
retval = retval | ((unsigned long)cmuRead(CMU_KIVH))<<32;
return retval;
}
void HotStream_ackInt(int CoreNum)
{
int bit = 1;
unsigned long intMask64 = 0;
unsigned int mask32 = 0xFFFF;
intMask64 = intMask64 | (bit << CoreNum);
cmuWrite(CMU_KIVL, (unsigned int)(intMask64 & mask32));
cmuWrite(CMU_KIVH, (unsigned int)((intMask64 << 32) & mask32));
}
void HotStream_ackIntVector(unsigned long rstv)
{
unsigned int mask32 = 0xFFFF;
cmuWrite(CMU_KIVL, (unsigned int)(rstv & mask32));
cmuWrite(CMU_KIVH, (unsigned int)((rstv << 32) & mask32));
}
// Framework Management
int HotStream_init()
{
if(pd_open(0,&dev) != 0){
PRINT("Failed to open file \n");
return -1;
}
PRINT("Device file opened successfully\n");
if(initDMA(&dev) < 0)
PRINT("Could not initialize DMA engine\n");
return 0;
}
int HotStream_close()
{
if(stopDMA(&dev)<0)
return -1;
return pd_close(&dev);
}
// Data Management
int HotStream_sendShMem(void *uBuf, unsigned int size, unsigned int offset)
{
int DSSdest;
// Configure DataStreamSwitch(DSS)
DSSdest = 0; // 00 - Shared Memory
DSSdest = DSSdest << 4;
cmuWrite(CMU_KCR, (DSSdest & CMU_KCR_Dest));
// Setup data copy from the host memory to the MM2S interface (stream target - 0)
if(setupSend(&dev, uBuf, size,0) < 0){
PRINT("Could not setup a data copy from the Host to the MM2S interface\n");
return -1;
}
// Setup data copy from the S2MM interface to the Shared Memory (DDR)
if(setupRecvtoDDR(&dev, offset, size) < 0){
PRINT("Could not setup a data copy from the S2MM interface to the Shared Memory\n");
return -1;
}
sendTransfType = 0;
return 0;
}
int HotStream_recvShMem(void *uBuf, unsigned int size, unsigned int offset)
{
int DSSdest;
// Configure DataStreamSwitch(DSS)
DSSdest = 0; // 00 - Shared Memory
DSSdest = DSSdest << 4;
cmuWrite(CMU_KCR, (DSSdest & CMU_KCR_Dest));
// Setup data transfer from the Shared Memory to the MM2S interface
if(setupSendfromDDR(&dev, offset, size, 0) < 0){
PRINT("Could not setup a data copy from the Shared Memory to the MM2S interface\n");
return -1;
}
// Setup data transfer from the S2MM interface to the Host memory
if(setupRecv(&dev, uBuf, size) < 0){
PRINT("Could not setup a data copy from the S2MM interface to the Host memory\n");
return -1;
}
recvTransfType = 0;
return 0;
}
int HotStream_sendBplane(void *uBuf, unsigned int size, int CoreNum)
{
int DSSdest;
// Configure DataStreamSwitch(DSS)
DSSdest = 1; // 01 - Shared Memory
DSSdest = DSSdest << 4;
cmuWrite(CMU_KCR, (DSSdest & CMU_KCR_Dest));
// Setup data copy from the host memory to the MM2S interface (stream target - CoreNum)
if(setupSend(&dev, uBuf, size,CoreNum) < 0){
PRINT("Could not setup a data copy from the Host to the MM2S interface and backplane\n");
return -1;
}
sendTransfType = 1;
return 0;
}
int HotStream_recvBplane(void *uBuf, unsigned int size, int CoreNum)
{
int DSSdest;
// Configure DataStreamSwitch(DSS)
DSSdest = 1; // 01 - Shared Memory
DSSdest = DSSdest << 4;
cmuWrite(CMU_KCR, (DSSdest & CMU_KCR_Dest));
// Setup data copy from the S2MM interface to the Host memory (stream target - CoreNum)
if(setupRecv(&dev, uBuf, size) < 0){
PRINT("Could not setup a data copy from the S2MM interface to the Host memory\n");
return -1;
}
recvTransfType = 1;
return 0;
}
int HotStream_startSend(int waitInt)
{
if (!patternApplied){
PRINT("An access pattern must be applied to the memory buffer before it can be streamed to the HotStream platform\n");
return -1;
}
if(sendTransfType == 0){ // Host to Shared Memory
// Start DMA engine. Start by enabling the S2MM so that it blocks waiting for the MM2S
if(startRecv(&dev, waitInt) < 0){
PRINT("DMA Recv failed\n");
return -1;
}
}
// Now for the MM2S...
if(startSend(&dev, waitInt) < 0){
PRINT("DMA Send failed\n");
return -1;
}
return 0;
}
int HotStream_startRecv(int waitInt)
{
if (!patternApplied){
PRINT("An access pattern must be applied to the memory buffer before it can be streamed from the HotStream platform\n");
return -1;
}
if(recvTransfType == 0){ // Shared Memory to Host
// Start DMA engine. Start MM2S channel
if(startSend(&dev, waitInt) < 0){
PRINT("DMA Recv failed\n");
return -1;
}
}
// Now for the S2MM...
if(startRecv(&dev, waitInt) < 0){
PRINT("DMA Send failed\n");
return -1;
}
return 0;
}
int HotStream_checkSend(int closeMapping)
{
if(sendTransfType == 0){ // Host to Shared Memory
if(checkRecvNoBuf() < 0){
PRINT("Could not verify transfer from S2MM interface to the Shared Memory\n");
return -1;
}
}
if(checkSend() < 0){
PRINT("Could not verify transfer from Host to MM2S interface\n");
return -1;
}
if(closeMapping){ // Terminate current mapping to bus device space
return freeSend(&dev);
}
return 0;
}
int HotStream_checkRecv(int closeMapping)
{
if(recvTransfType == 0){ // Shared Memory to Host
if(checkSend() < 0){
PRINT("Could not verify transfer from Shared Memory to MM2S interface\n");
return -1;
}
}
if(checkRecv() < 0){
PRINT("Could not verify transfer from S2MM interface to the Hosty\n");
return -1;
}
if(closeMapping){
return freeRecv(&dev);
}
return 0;
}
// Pattern Definition
int HotStream_linear_send(unsigned int offset, unsigned int size)
{
if(sendTransfType == 0){
// Apply Linear pattern to the data copied to the Shared Memory
if(applyLinear_recv(offset,size,size,size) < 0){
PRINT("Could not apply linear pattern\n");
return -1;
}
}
if(applyLinear_send(offset,size,size,size) < 0){
PRINT("Could not apply linear pattern\n");
return -1;
}
return 0;
}
int HotStream_linear_recv(unsigned int offset, unsigned int size)
{
if(recvTransfType == 0){
// Apply Linear pattern to the data retrieved from the Shared Memory
if(applyLinear_send(offset,size,size,size) < 0){
PRINT("Could not apply linear pattern\n");
return -1;
}
}
if(applyLinear_recv(offset,size,size,size) < 0){
PRINT("Could not apply linear pattern\n");
return -1;
}
return 0;
}
int HotStream_2d_send(int offset, int hsize, int stride, int vsize)
{
if(sendTransfType == 0){
// Apply Linear pattern to the data copied to the Shared Memory
if(apply2d_recv(offset,hsize,stride,vsize) < 0){
PRINT("Could not apply 2D pattenn\n");
return -1;
}
}
if(apply2d_send(offset,hsize,stride,vsize) < 0){
PRINT("Could not apply 2D pattern\n");
return -1;
}
return 0;
}
int HotStream_2d_recv(int offset, int hsize, int stride, int vsize)
{
if(recvTransfType == 0){
// Apply Linear pattern to the data retrieved from the Shared Memory
if(apply2d_send(offset,hsize,stride,vsize) < 0){
PRINT("Could not apply 2D pattern\n");
return -1;
}
}
if(apply2d_recv(offset,hsize,stride,vsize) < 0){
PRINT("Could not apply 2D pattern\n");
return -1;
}
return 0;
}
int HotStream_block_send(int bsize, int mat_size, int elem_size)
{
}
int HotStream_block_recv(int bsize, int mat_size, int elem_size)
{
if(recvTransfType == 0){
// Apply Linear pattern to the data retrieved from the Shared Memory
if(applyBlocking_send(bsize, mat_size, elem_size) < 0){
PRINT("Could not apply blocking pattern\n");
return -1;
}
}
if(applyBlocking_recv(bsize, mat_size, elem_size) < 0){
PRINT("Could not apply blocking pattern\n");
return -1;
}
return 0;
}
int HotStream_gather(void *ubuf, void *new_ubuf, int *buf_size, unsigned int offset, unsigned int hsize, unsigned int stride, unsigned int vsize)
{
return compressData(ubuf, new_ubuf, buf_size, offset, hsize, stride, vsize);
}
int main(int argc, char **argv)
{
void *idata0;
int size;
HotStream_readBin(argv[1], idata0, &size);
return 0;
}