forked from cbassa/cdmt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cdmt_udp.cu
1568 lines (1301 loc) · 54.3 KB
/
cdmt_udp.cu
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <math.h>
#include <time.h>
#include <errno.h>
#include <cuda.h>
#include <cufft.h>
#include <helper_functions.h>
#include <helper_cuda.h>
#include <getopt.h>
#include <limits.h>
#include <omp.h>
#include "lofar_udp_reader.h"
#include "lofar_udp_misc.h"
// Timing macro
#ifndef __LOFAR_UDP_TICKTOCK_MACRO
#define __LOFAR_UDP_TICKTOCK_MACRO
// XOPEN -> strptime requirement
#define __USE_XOPEN
#include <time.h>
#define CLICK(clock) clock_gettime(CLOCK_MONOTONIC_RAW, &clock);
#define TICKTOCK(tick, tock) ((double) (tock.tv_nsec - tick.tv_nsec) / 1000000000.0) + (tock.tv_sec - tick.tv_sec)
#endif
#define HEADERSIZE 4096
#define DMCONSTANT 2.41e-10
#define VERB 1
// Struct for header information
struct header {
int nchan,nbit=0,nsub,tel=11,mach=11;
double tstart,tsamp,fch1,foff,fcen,bwchan;
double src_raj,src_dej;
char source_name[80];
char rawfname[4][1024];
};
// Prototypes
struct header read_sigproc_header(char *fname, char *dataname, int ports);
void get_channel_chirp(double fcen,double bw,float dm,int nchan,int nbin,int nsub,cufftComplex *c);
__global__ void transpose_unpadd_and_detect(cufftComplex *cp1,cufftComplex *cp2,int nbin,int nchan,int nfft,int nsub,int noverlap,int nsamp,float *fbuf);
static __device__ __host__ inline cufftComplex ComplexScale(cufftComplex a,float s);
static __device__ __host__ inline cufftComplex ComplexMul(cufftComplex a,cufftComplex b);
static __global__ void PointwiseComplexMultiply(cufftComplex *a,cufftComplex *b,cufftComplex *c,int nx,int ny,int l,float scale);
template<typename I> __global__ void unpack_and_padd(I *dbuf0,I *dbuf1,I *dbuf2,I *dbuf3,int nsamp,int nbin,int nfft,int nsub,int noverlap,cufftComplex *cp1,cufftComplex *cp2);
template<typename I> __global__ void unpack_and_padd_first_iteration(I *dbuf0,I *dbuf1,I *dbuf2,I *dbuf3,int nsamp,int nbin,int nfft,int nsub,int noverlap,cufftComplex *cp1,cufftComplex *cp2);
template<typename I> __global__ void padd_next_iteration(I *dbuf0,I *dbuf1,I *dbuf2,I *dbuf3,int nsamp,int nbin,int nfft,int nsub,int noverlap,cufftComplex *cp1,cufftComplex *cp2);
__global__ void swap_spectrum_halves(cufftComplex *cp1,cufftComplex *cp2,int nx,int ny);
__global__ void compute_chirp(double fcen,double bw,float *dm,int nchan,int nbin,int nsub,int ndm,cufftComplex *c);
__global__ void compute_block_sums(float *z,int nchan,int nblock,int nsum,float *bs1,float *bs2);
__global__ void compute_channel_statistics(int nchan,int nblock,int nsum,float *bs1,float *bs2,float *zavg,float *zstd);
__global__ void redigitize(float *z,int nchan,int nblock,int nsum,float *zavg,float *zstd,float zmin,float zmax,unsigned char *cz);
__global__ void decimate_and_redigitize(float *z,int ndec,int nchan,int nblock,int nsum,float *zavg,float *zstd,float zmin,float zmax,unsigned char *cz);
__global__ void decimate(float *z,int ndec,int nchan,int nblock,int nsum,float *cz);
void write_to_disk_float(float* outputArray, FILE** outputFile, int nsamples, cudaEvent_t* waitEvent);
void write_to_disk_char(unsigned char* outputArray, FILE** outputFile, int nsamples, cudaEvent_t* waitEvent);
void write_filterbank_header(struct header h,FILE *file);
int reshapeRawUdp(lofar_udp_reader *reader, int verbose);
long __inline__ beamformed_packno(unsigned int timestamp, unsigned int sequence);
long getStartingPacket(char inputTime[], const int clock200MHz);
long getStartingPacket(char inputTime[], const int clock200MHz) {
struct tm unixTm;
time_t unixEpoch;
if(strptime(inputTime, "%Y-%m-%dT%H:%M:%S", &unixTm) != NULL) {
unixEpoch = timegm(&unixTm);
return beamformed_packno((unsigned long int) unixEpoch, 0, clock200MHz);
} else {
fprintf(stderr, "Invalid time string, exiting.\n");
return 1;
}
}
// External prototypes from udpPacketManager
extern "C"
{
int lofar_udp_reader_step(lofar_udp_reader *reader);
lofar_udp_reader* lofar_udp_meta_file_reader_setup(FILE **inputFiles, const int numPorts, const int replayDroppedPackets, const int processingMode, const int verbose, const long packetsPerIteration, const long startingPacket, const long packetsReadMax, const int compressedReader);
int lofar_udp_file_reader_reuse(lofar_udp_reader *reader, const long startingPacket, const long packetsReadMax);
}
// Usage
void usage()
{
printf("cdmt -v -c -d <DM start,step,num> -D <GPU device> -b <ndec> -N <forward FFT size> -n <overlap region> -f <number of FFTs per operation> -o <outputname> -s <sigproc header location> -p <port nums> <fil prefix>\n\n");
printf("Compute coherently dedispersed SIGPROC filterbank files from LOFAR complex voltage data in raw udp format.\n");
printf("-D <GPU device> Select GPU device [integer, default: 0]\n");
printf("-b <ndec> Number of time samples to average [integer, default: 1]\n");
printf("-d <DM start, step, num> DM start and stepsize, number of DM trials\n");
printf("-o <outputname> Output filename [default: cdmt]\n");
printf("-N <forward FFT size> Forward FFT size [integer, default: 65536]\n");
printf("-n <overlap region> Overlap region [integer, default: 2048]\n");
printf("-s <ISOT str> Time to skip to when starting to process data [default: "", only supports 200MHz clock]\n");
printf("-r <packets> Number of packets to read in total from the -s offset [integer, default: length of file]\n");
printf("-m <hdr loc> Sigproc header to read metadata from [default: fil prefix.sigprochdr]\n");
printf("-f <FFTs per op> Number of FFTs to execute per cuFFT call [default: 128]\n");
printf("-a Disable redigitisation; output float32 [default: false]\n");
printf("-c <num chan> Channelisation Factor [default: 8]\n");
printf("-w Print warnings about input parameter sizes and packet loss [default: true]\n");
printf("-p <num> Number of ports of data to process [default: 4]\n");
printf("-l <num> Base port number to iterate from when determining raw file names [default for IE613: 16130]\n");
printf("-t Perform a dry run; proceed as expected until we would start processing data.\n");
printf("-z <subband strategy> Apply dreamBeam corrections to voltages (default: false).\n");
printf("-Z <lower>,<upper> Extract only the specific beamlets (default: all)\n");
return;
}
int main(int argc,char *argv[])
{
int i,j,nsamp,nfft,mbin,nvalid,nchan=8,nbin=65536,noverlap=2048,nsub=20,ndm,ndec=1;
int idm,nread_tmp,nread,mchan,msamp,mblock,msum=1024;
char *header,*udpbuf[4],*dudpbuf_c[4];
FILE *file;
unsigned char **cbuf[2],*dcbuf;
float **cbuff[2], *dcbuff, *dudpbuf_f[4];
float *fbuf,*dfbuf;
float *bs1,*bs2,*zavg,*zstd;
cufftComplex *cp1,*cp2,*dc,*cp1p,*cp2p;
cufftHandle ftc2cf,ftc2cb;
int idist,odist,iembed,oembed,istride,ostride;
dim3 blocksize,gridsize;
struct header hdr;
float *dm,*ddm,dm_start,dm_step;
char fname[128],fheader[1024],*udpfname,sphdrfname[1024] = "",obsid[128]="cdmt",inputTime[128]="",subbands[4096]="";
int bytes_read;
long int ts_read=LONG_MAX;
long int total_ts_read=0;
int part=0,device=0,nforward=128,redig=1,ports=4,baseport=16130,checkinputs=1,testmode=0,dreamBeam=0,beamletLower=0,beamletUpper=0;
int arg=0;
FILE **outfile;
struct timespec tick, tick0, tick1, tock, tock0;
double elapsedTime;
lofar_udp_reader *reader;
// Read options
if (argc>1) {
while ((arg=getopt(argc,argv,"tawc:p:f:d:D:ho:b:N:n:s:r:m:t:p:l:z:Z:"))!=-1) {
switch (arg) {
case 'n':
noverlap=atoi(optarg);
break;
case 'N':
nbin=atoi(optarg);
break;
case 'b':
ndec=atoi(optarg);
break;
case 'o':
strcpy(obsid,optarg);
break;
case 'D':
device=atoi(optarg);
break;
case 's':
strcpy(inputTime, optarg);
break;
case 'r':
ts_read=atol(optarg);
break;
case 'd':
sscanf(optarg,"%f,%f,%d",&dm_start,&dm_step,&ndm);
break;
case 'm':
strcpy(sphdrfname,optarg);
break;
case 'f':
nforward=atoi(optarg);
break;
case 'a':
redig=0;
break;
case 'c':
nchan=atoi(optarg);
break;
case 'w':
checkinputs=0;
break;
case 'p':
ports=atoi(optarg);
break;
case 'l':
baseport=atoi(optarg);
break;
case 't':
testmode=1;
break;
case 'z':
dreamBeam = 1;
strcpy(subbands, optarg);
break;
case 'Z':
sscanf(optarg, "%d,%d", &beamletLower,&beamletUpper);
break;
case 'h':
usage();
return 0;
}
}
} else {
printf("Unknown option '%c'\n", arg);
usage();
return 0;
}
if (argc <= optind) {
fprintf(stderr, "Failed to provide a source file, exiting.\n");
return 0;
}
udpfname=argv[optind];
// Sanity checks to avoid voids in output filterbank
if (checkinputs)
{
if (nbin % nchan != 0) {
fprintf(stderr, "ERROR: nbin must be disible by nchan (%d) (currently %d, remainder %d). Exiting.\n", nchan, nbin, nbin % nchan);
exit(1);
}
if ((nforward * (nbin-2*noverlap)) % nchan != 0 ) {
fprintf(stderr, "ERROR: Valid data length must be divisible by nchan (%d) (currently %d, remainer %d). Exiting.\n", nchan, nbin-2*noverlap, (nbin-2*noverlap) % nchan);
exit(1);
}
if ((nforward * (nbin-2*noverlap) / nchan) % msum != 0) {
printf("%d\n", (nforward * (nbin-2*noverlap) / nchan) % msum);
fprintf(stderr, "ERROR: Interal sum cannot proceed; valid samples must be divisible by msum (%d) (currently %d, remainder %d).\n", msum, (nforward * (nbin-2*noverlap) / nchan), (nforward * (nbin-2*noverlap) / nchan) % msum);
exit(1);
}
if ((nforward * (nbin-2*noverlap)) % 16 != 0) {
fprintf(stderr, "ERROR: Number of valid samples must be divisible by samples per packet (16) (currently %d, remainder %d). Exiting.\n", (nforward * (nbin-2*noverlap)), (nforward * (nbin-2*noverlap)) % 16);
exit(1);
}
}
long startingPacket;
if (strcmp(inputTime, "") != 0) {
startingPacket = getStartingPacket(inputTime, 1);
printf("Skipping to packet %ld (%s)\n", startingPacket, inputTime);
} else{
startingPacket = -1;
}
// Error if given an invalid sigproc header location
if (strcmp(sphdrfname, "") == 0) {
fprintf(stderr, "ERROR: Sigproc header not provided. Exiting.\n");
exit(1);
}
FILE* inputFiles[4];
int compressedInput = 0;
char tmpfname[1024] = "";
// Pattern check to determine if files are compressed or not
if (strstr(udpfname, ".zst") != NULL) compressedInput = 1;
// Read the provided sigproc header
hdr = read_sigproc_header(sphdrfname, udpfname, ports);
// Update to account for runtime-dropped dropped beamlets
if (beamletUpper != 0) {
hdr.fch1 += hdr.foff * (nsub - beamletUpper);
nsub = beamletUpper;
}
if (beamletLower != 0) {
nsub -= beamletLower;
}
// Check that the bin size and overlap sizes are sufficiently large to avoid issues with the convolution theorem
if (checkinputs)
{
const double stg1 = (1.0 / 2.41e-4) * abs(pow((double) hdr.fch1 + hdr.nsub * hdr.foff + hdr.foff *0.5,-2.0) - pow((double) hdr.fch1 + hdr.nsub * hdr.foff - hdr.foff *0.5, -2.0)) * (dm_start + dm_step * (ndm - 1));
const int overlapCheck = (int) (stg1 / hdr.tsamp);
if (overlapCheck > nbin) {
fprintf(stderr, "WARNING: The size of your FFT bin is too short for the given DMs and frequencies. Given bin size: %d, Suggested minimum bin size: %d (maximum dispersion delay %f).\n", nbin, overlapCheck, stg1);
} else if (overlapCheck / 2 > noverlap) {
fprintf(stderr, "WARNING: The size of your FFT overlap is too short for the given maximum DM. Given overlap: %d, Suggested minimum overlap: %d (maximum dispersion delay %f).\n", noverlap, overlapCheck / 2, stg1);
}
}
// Open raw files
for (int i = 0; i < ports; i++) {
sprintf(tmpfname, udpfname, i + baseport);
if (strcmp(udpfname, tmpfname) == 0 && ports > 1) {
fprintf(stderr, "ERROR: Input file name has not changed when attempting to substitute in port, have you correctly defined your file name?\n");
exit(1);
}
printf("Opening %s...\n", tmpfname);
inputFiles[i] = fopen(tmpfname, "r");
if (inputFiles[i] == NULL) {
printf("Input file failed to open (null pointer)\n");
}
if (i == 0)
strcpy(hdr.rawfname[i],tmpfname);
}
// Read the number of raw subbands + sampling time for later
nsub=hdr.nsub;
double timeOffset = hdr.tsamp;
// Adjust header for the target output
hdr.tsamp*=nchan*ndec;
hdr.nchan=nsub*nchan;
if (redig) hdr.nbit=8;
else hdr.nbit=32;
hdr.fch1=hdr.fcen+0.5*hdr.nsub*hdr.bwchan-0.5*hdr.bwchan/nchan;
hdr.foff=-fabs(hdr.bwchan/nchan);
// Data sizes; these control the block sizes for interall logic components
// FFTs are performed on a 3D block; of dims (nfft, nsub, nbin), there are
// overlaps of noverlap samples between each nbin element
nvalid=nbin-2*noverlap;
nsamp=nforward*nvalid;
nfft=(int) ceil(nsamp/(float) nvalid);
mbin=nbin/nchan; // nbin must be evenly divisible by nchan
mchan=nsub*nchan;
msamp=nsamp/nchan; // nforward * nvalid must be divisble by nchan
mblock=msamp/msum; // nforward * nvalid / nchan must be disible by msum
// Determine the number of packets we need to request per iteration
const long int packetGulp = nsamp / 16;
printf("Configuring reader...\n");
lofar_udp_config udp_cfg = lofar_udp_config_default;
udp_cfg.inputFiles = inputFiles;
udp_cfg.numPorts = ports;
udp_cfg.replayDroppedPackets = 1;
udp_cfg.processingMode = 11;
udp_cfg.verbose = 0;
udp_cfg.packetsPerIteration = packetGulp;
udp_cfg.startingPacket = startingPacket;
udp_cfg.packetsReadMax = LONG_MAX;
udp_cfg.readerType = compressedInput;
udp_cfg.beamletLimits[0] = beamletLower;
udp_cfg.beamletLimits[1] = beamletUpper;
udp_cfg.calibrateData = dreamBeam;
lofar_udp_calibration udp_cal = lofar_udp_calibration_default;
udp_cfg.calibrationConfiguration = &udp_cal;
char fifo[128] = "/tmp/dreamBeamCDMTFIFO", rajs[64], decjs[64];
float raj = 0.0, decj = 0.0, ras, decs;
int rah, ram, decd, decm;
if (dreamBeam == 1) {
sprintf(rajs, "%012.5f", hdr.src_raj);
sprintf(decjs, "%012.5f", hdr.src_dej);
printf("rajs %s, decjs %s\n", rajs, decjs);
sscanf(rajs, "%02d%02d%f", &rah, &ram, &ras);
sscanf(decjs, "%02d%02d%f", &decd, &decm, &decs);
raj = (float) rah * 0.26179958333 + (float) ram * 0.00436332638 + ras * 0.0000727221;
decj = (float) abs(decd) * 0.0174533 + (float) decm * 0.00029088833 + decs * 0.00000484813;
if (hdr.src_dej < 0)
decj *= -1;
printf("Raj: %f, Decj: %f\n", raj, decj);
printf("Configuring calibration...\n");
strcpy(udp_cal.calibrationFifo, fifo);
strcpy(udp_cal.calibrationSubbands, subbands);
udp_cal.calibrationPointing[0] = raj;
udp_cal.calibrationPointing[1] = decj;
strcpy(udp_cal.calibrationPointingBasis, "J2000");
}
printf("Loading %ld packets per gulp. Setting up reader...\n", packetGulp);
reader = lofar_udp_meta_file_reader_setup_struct(&udp_cfg);
printf("Reader: %d, %d.\t %d, %d.\t %d, %ld, %d\n", reader->meta->totalRawBeamlets, reader->meta->totalProcBeamlets, reader->meta->inputBitMode, reader->meta->outputBitMode, reader->meta->processingMode, reader->meta->packetsPerIteration, reader->meta->replayDroppedPackets);
if (reader == NULL) {
fprintf(stderr, "Failed to generate LOFAR UDP Reader, exiting.\n");
exit(1);
}
if (reader->meta->totalProcBeamlets != nsub) {
fprintf(stderr, "ERROR: Number of beamlets does not match number of channels in header, exiting.\n");
exit(1);
}
// Update the start time based on the first provided packet
hdr.tstart = lofar_get_packet_time_mjd(reader->meta->inputData[0]);
// Set device
checkCudaErrors(cudaSetDevice(device));
// Generate streams for asyncrnous operations
int numStreams = 1;
// Add an extra stream for the final padding operation
cudaStream_t streams[numStreams+1];
for (i = 0; i < numStreams+1; i++)
checkCudaErrors(cudaStreamCreate(&(streams[i])));
// Create 2 events; one which blocks execution (preventing new data reads) and the other waiting for compute to finish.
int numEvents = 3;
cudaEvent_t events[numEvents];
cudaEventCreateWithFlags(&(events[0]), cudaEventBlockingSync & cudaEventDisableTiming);
cudaEventCreateWithFlags(&(events[1]), cudaEventDisableTiming);
cudaEventCreateWithFlags(&(events[2]), cudaEventDisableTiming);
cudaEvent_t dmWriteEvents[numStreams][ndm];
for (i =0; i < ndm; i++)
for (j = 0; j < numStreams; j++)
cudaEventCreateWithFlags(&(dmWriteEvents[j][i]), cudaEventBlockingSync & cudaEventDisableTiming);
// DMcK: cuFFT docs say it's best practice to plan before allocating memory
// cuda-memcheck fails initialisation before this block is run? -- add CUDA_MEMCHECK_PATCH_MODULE=1 as an env flag
// Disable initial memory allocates and silence the compiler warnings;
// Nvidia uses a custom compiler frontend so GCC pragmas do not work.
// This order-of-execution follows Nvidia's usage guidance, so the warnings
// should be (safely?) ignored
#pragma diag_suppress used_before_set
cufftSetAutoAllocation(ftc2cf, 0);
cufftSetAutoAllocation(ftc2cb, 0);
#pragma diag_default used_before_set
size_t cfSize, cbSize;
// Generate FFT plan (batch in-place forward FFT)
idist=nbin; odist=nbin; iembed=nbin; oembed=nbin; istride=1; ostride=1;
checkCudaErrors(cufftPlanMany(&ftc2cf,1,&nbin,&iembed,istride,idist,&oembed,ostride,odist,CUFFT_C2C,nfft*nsub));
checkCudaErrors(cufftGetSizeMany(ftc2cf, 1,&nbin,&iembed,istride,idist,&oembed,ostride,odist,CUFFT_C2C,nfft*nsub, &cfSize));
//cufftSetStream(ftc2cf,streams[0]);
// Generate FFT plan (batch in-place backward FFT)
idist=mbin; odist=mbin; iembed=mbin; oembed=mbin; istride=1; ostride=1;
checkCudaErrors(cufftPlanMany(&ftc2cb,1,&mbin,&iembed,istride,idist,&oembed,ostride,odist,CUFFT_C2C,nchan*nfft*nsub));
checkCudaErrors(cufftGetSizeMany(ftc2cb, 1,&mbin,&iembed,istride,idist,&oembed,ostride,odist,CUFFT_C2C,nchan*nfft*nsub,&cbSize));
//cufftSetStream(ftc2cb,streams[0]);
cudaDeviceSynchronize();
// Get the maximum size needed for the FFT operations (they should be the same, check for safety)
size_t minfftSize = cfSize > cbSize ? cfSize : cbSize;
if (VERB) printf("Allocated %ldMB for cuFFT work (saving %ldMB)\n", minfftSize >> 20, (cfSize + cbSize - minfftSize) >> 20);
// Predict the overall VRAM usage
long unsigned int bytesUsed = sizeof(cufftComplex) * nbin * nfft * nsub * 4 + sizeof(cufftComplex) * nbin *nsub * ndm + sizeof(float) * mblock * mchan * 2 + (sizeof(char) * (1 - dreamBeam) + sizeof(float) * dreamBeam) * nsamp * nsub * 4 + sizeof(float) * nsamp * nsub + redig * msamp * mchan / ndec - (redig - 1) * 4 * msamp * mchan * ndec;
// Get the total / available VRAM
size_t gpuMems[2];
checkCudaErrors(cudaMemGetInfo(&(gpuMems[0]), &(gpuMems[1])));
if (VERB) printf("Preparing for GPU memory allocations. Current memory usage: %ld / %ld GB\n", (gpuMems[1] - gpuMems[0]) >> 30, gpuMems[1] >> 30);
if (VERB) printf("We anticipate %ld MB (%ld GB) to be allocated on the GPU (%ld MB for cuFFT planning).\n", (bytesUsed + minfftSize) >> 20, (bytesUsed + minfftSize) >> 30, minfftSize >> 20);
// Allocate the maxmimum memory needed for FFT operations
void* cufftWorkArea;
checkCudaErrors(cudaMalloc((void**) &cufftWorkArea, (size_t) minfftSize));
// Set the cuFFT handles to use this area
cufftSetWorkArea(ftc2cf, cufftWorkArea);
cufftSetWorkArea(ftc2cb, cufftWorkArea);
// Allocate memory for complex timeseries
checkCudaErrors(cudaMalloc((void **) &cp1, (size_t) sizeof(cufftComplex)*nbin*nfft*nsub));
checkCudaErrors(cudaMalloc((void **) &cp2, (size_t) sizeof(cufftComplex)*nbin*nfft*nsub));
checkCudaErrors(cudaMalloc((void **) &cp1p, (size_t) sizeof(cufftComplex)*nbin*nfft*nsub));
checkCudaErrors(cudaMalloc((void **) &cp2p, (size_t) sizeof(cufftComplex)*nbin*nfft*nsub));
// Allocate device memory for chirp
checkCudaErrors(cudaMalloc((void **) &dc, (size_t) sizeof(cufftComplex)*nbin*nsub*ndm));
if (redig) {
// Allocate device memory for block sums
checkCudaErrors(cudaMalloc((void **) &bs1, (size_t) sizeof(float)*mblock*mchan));
checkCudaErrors(cudaMalloc((void **) &bs2, (size_t) sizeof(float)*mblock*mchan));
// Allocate device memory for channel averages and standard deviations
checkCudaErrors(cudaMalloc((void **) &zavg, (size_t) sizeof(float)*mchan));
checkCudaErrors(cudaMalloc((void **) &zstd, (size_t) sizeof(float)*mchan));
}
// Allocate memory for input bytes and header
header=(char *) malloc(sizeof(char)*HEADERSIZE);
if (dreamBeam == 0) {
for (i=0;i<4;i++) {
udpbuf[i]= reader->meta->outputData[i];
checkCudaErrors(cudaMalloc((void **) &dudpbuf_c[i], (size_t) sizeof(char)*nsamp*nsub));
}
} else {
for (i=0;i<4;i++) {
udpbuf[i]= reader->meta->outputData[i];
checkCudaErrors(cudaMalloc((void **) &dudpbuf_f[i], (size_t) sizeof(float)*nsamp*nsub));
}
}
// Allocate output buffers
fbuf=(float *) malloc(sizeof(float)*nsamp*nsub);
checkCudaErrors(cudaMalloc((void **) &dfbuf, (size_t) sizeof(float)*nsamp*nsub));
// Allocate final data product memories; differs based on wheter we are re-digitising before writing to disk or not
if (redig) {
for(i = 0; i < numStreams; i++)
cbuf[i] = (unsigned char**) malloc(sizeof(unsigned char*)*ndm);
for (i = 0; i < ndm; i++)
for (j = 0; j < numStreams; j++)
cbuf[j][i]=(unsigned char *) malloc(sizeof(unsigned char)*msamp*mchan/ndec);
checkCudaErrors(cudaMalloc((void **) &dcbuf, (size_t) sizeof(unsigned char)*msamp*mchan/ndec));
} else {
for(i = 0; i < numStreams; i++)
cbuff[i] = (float**) malloc(sizeof(float*)*ndm);
for (i = 0; i < ndm; i++)
for (j = 0; j < numStreams; j++)
cbuff[j][i] = (float *) malloc(sizeof(float)*msamp*mchan/ndec);
if (ndec > 1) checkCudaErrors(cudaMalloc((void **) &dcbuff, (size_t) sizeof(float)*msamp*mchan/ndec));
}
// Allocate DMs and copy to device
dm=(float *) malloc(sizeof(float)*ndm);
for (idm=0;idm<ndm;idm++)
dm[idm]=dm_start+(float) idm*dm_step;
checkCudaErrors(cudaMalloc((void **) &ddm, (size_t) sizeof(float)*ndm));
checkCudaErrors(cudaMemcpy(ddm,dm,sizeof(float)*ndm,cudaMemcpyHostToDevice));
// Allow memory alloation/copy actions to finish before processing
cudaDeviceSynchronize();
// Compute chirp
blocksize.x=32; blocksize.y=32; blocksize.z=1;
gridsize.x=nsub/blocksize.x+1; gridsize.y=nchan/blocksize.y+1; gridsize.z=ndm/blocksize.z+1;
compute_chirp<<<gridsize,blocksize>>>(hdr.fcen,nsub*hdr.bwchan,ddm,nchan,nbin,nsub,ndm,dc);
// Write temporary filterbank header
file=fopen("/tmp/header.fil","w");
if (file == NULL) {
fprintf(stderr, "ERROR: Unable to open /tmp/header.fil to write temporary header; exiting.\n");
exit(1);
}
write_filterbank_header(hdr,file);
fclose(file);
file=fopen("/tmp/header.fil","r");
if (file == NULL) {
fprintf(stderr, "ERROR: Unable to re-open /tmp/header.fil to read temporary header length; exiting.\n");
exit(1);
}
bytes_read=fread(fheader,sizeof(char),1024,file);
fclose(file);
// Format file names and open
outfile=(FILE **) malloc(sizeof(FILE *)*ndm);
for (idm=0;idm<ndm;idm++) {
sprintf(fname,"%s_cDM%06.2f_P%03d.fil",obsid,dm[idm],part);
outfile[idm]=fopen(fname,"w");
if (outfile[idm] == NULL) {
fprintf(stderr, "Unable to open output file %s, exiting.\n", fname);
exit(1);
}
}
// Write headers
for (idm=0;idm<ndm;idm++) {
// Send header
fwrite(fheader,sizeof(char),bytes_read,outfile[idm]);
}
// Loop over input file contents
double timeInSeconds = 0.0;
nread = INT_MAX;
// Skip the first noverlap samples as they are 0'd
int writeOffset = noverlap;
int writeSize;
// Remnants of an attempt to paralelise the I/O; leave in for now
//int streamIdx = iblock % numStreams;
int streamIdx = 0;
cudaStream_t stream = streams[streamIdx];
cufftSetStream(ftc2cf, stream);
cufftSetStream(ftc2cb, stream);
if (testmode)
exit(0);
CLICK(tick);
float dt = 0.0;
for (int iblock=0;;iblock++) {
// Wait to finish reading in the next block
cudaEventSynchronize(events[0]);
CLICK(tick0);
nread_tmp = reshapeRawUdp(reader, checkinputs);
CLICK(tock0);
dt = TICKTOCK(tick0, tock0);
if (nread > nread_tmp) {
nread = nread_tmp;
}
// Determine the output length
writeSize = (nread-writeOffset)*nsub/ndec;
// Count up the total bytes read and calculate the read time
total_ts_read += nread;
printf("Block: %d: Read %ld MB in %.2f s\n",iblock,sizeof(char)*nread*nsub*4/(1<<20), dt);
// Sanity check the read data size
if (nread==0) {
printf("No data read from last file; assuming EOF, finishng up.\n");
break;
} else if (iblock != 0 && nread < nread_tmp) {
printf("Received less data than expected; we may have parsed out of order data or we are nearing the EOF.\n");
}
// Copy buffers to device, waiting for the previous overlap operation to finish first
cudaStreamWaitEvent(stream, events[1], 0);
CLICK(tick1);
if (dreamBeam == 0) {
for (i=0;i<4;i++) {
checkCudaErrors(cudaMemcpyAsync(dudpbuf_c[i],udpbuf[i],sizeof(char)*nread*nsub,cudaMemcpyHostToDevice,stream));
}
} else {
for (i=0;i<4;i++) {
checkCudaErrors(cudaMemcpyAsync(dudpbuf_f[i],udpbuf[i],sizeof(float)*nread*nsub,cudaMemcpyHostToDevice,stream));
}
}
cudaEventRecord(events[0], stream);
// Unpack data and padd data
blocksize.x=32; blocksize.y=32; blocksize.z=1;
gridsize.x=nbin/blocksize.x+1; gridsize.y=nfft/blocksize.y+1; gridsize.z=nsub/blocksize.z+1;
if (dreamBeam == 0) {
if (iblock > 0) {
unpack_and_padd<char><<<gridsize,blocksize,0,stream>>>(dudpbuf_c[0],dudpbuf_c[1],dudpbuf_c[2],dudpbuf_c[3],nread,nbin,nfft,nsub,noverlap,cp1p,cp2p);
} else {
unpack_and_padd_first_iteration<char><<<gridsize,blocksize,0,stream>>>(dudpbuf_c[0],dudpbuf_c[1],dudpbuf_c[2],dudpbuf_c[3],nread,nbin,nfft,nsub,noverlap,cp1p,cp2p);
}
} else {
if (iblock > 0) {
unpack_and_padd<float><<<gridsize,blocksize,0,stream>>>(dudpbuf_f[0],dudpbuf_f[1],dudpbuf_f[2],dudpbuf_f[3],nread,nbin,nfft,nsub,noverlap,cp1p,cp2p);
} else {
unpack_and_padd_first_iteration<float><<<gridsize,blocksize,0,stream>>>(dudpbuf_f[0],dudpbuf_f[1],dudpbuf_f[2],dudpbuf_f[3],nread,nbin,nfft,nsub,noverlap,cp1p,cp2p);
}
}
// Perform FFTs
cudaStreamWaitEvent(stream, events[2], 0);
//cufftSetStream(ftc2cf, stream);
checkCudaErrors(cufftExecC2C(ftc2cf,(cufftComplex *) cp1p,(cufftComplex *) cp1p,CUFFT_FORWARD));
checkCudaErrors(cufftExecC2C(ftc2cf,(cufftComplex *) cp2p,(cufftComplex *) cp2p,CUFFT_FORWARD));
// Swap spectrum halves for large FFTs
blocksize.x=32; blocksize.y=32; blocksize.z=1;
gridsize.x=nbin/blocksize.x+1; gridsize.y=nfft*nsub/blocksize.y+1; gridsize.z=1;
swap_spectrum_halves<<<gridsize,blocksize,0,stream>>>(cp1p,cp2p,nbin,nfft*nsub);
// Swap the cuFFT operation to the current stream
//cufftSetStream(ftc2cb, stream);
// Loop over dms
for (idm=0;idm<ndm;idm++) {
// Perform complex multiplication of FFT'ed data with chirp
blocksize.x=32; blocksize.y=32; blocksize.z=1;
gridsize.x=nbin*nsub/blocksize.x+1; gridsize.y=nfft/blocksize.y+1; gridsize.z=1;
PointwiseComplexMultiply<<<gridsize,blocksize,0,stream>>>(cp1p,dc,cp1,nbin*nsub,nfft,idm,1.0/(float) nbin);
PointwiseComplexMultiply<<<gridsize,blocksize,0,stream>>>(cp2p,dc,cp2,nbin*nsub,nfft,idm,1.0/(float) nbin);
// When cp1/2p are no longer needed, start overlapping the data for the next iteration on a separate stream
if (idm == ndm - 1) {
cudaEventRecord(events[2], stream);
cudaStreamWaitEvent(streams[numStreams], events[2], 0);
blocksize.x=32; blocksize.y=32; blocksize.z=1;
gridsize.x=nbin/blocksize.x+1; gridsize.y=nfft/blocksize.y+1; gridsize.z=nsub/blocksize.z+1;
if (dreamBeam == 0) {
padd_next_iteration<char><<<gridsize,blocksize,0,streams[numStreams]>>>(dudpbuf_c[0],dudpbuf_c[1],dudpbuf_c[2],dudpbuf_c[3],nread,nbin,nfft,nsub,noverlap,cp1p,cp2p);
} else {
padd_next_iteration<float><<<gridsize,blocksize,0,streams[numStreams]>>>(dudpbuf_f[0],dudpbuf_f[1],dudpbuf_f[2],dudpbuf_f[3],nread,nbin,nfft,nsub,noverlap,cp1p,cp2p);
}
cudaEventRecord(events[1], streams[numStreams]);
}
// Swap spectrum halves for small FFTs
blocksize.x=32; blocksize.y=32; blocksize.z=1;
gridsize.x=mbin/blocksize.x+1; gridsize.y=nchan*nfft*nsub/blocksize.y+1; gridsize.z=1;
swap_spectrum_halves<<<gridsize,blocksize,0,stream>>>(cp1,cp2,mbin,nchan*nfft*nsub);
// Perform FFTs
checkCudaErrors(cufftExecC2C(ftc2cb,(cufftComplex *) cp1,(cufftComplex *) cp1,CUFFT_INVERSE));
checkCudaErrors(cufftExecC2C(ftc2cb,(cufftComplex *) cp2,(cufftComplex *) cp2,CUFFT_INVERSE));
// Wait for the previous memory transfer to finish
cudaStreamWaitEvent(stream, dmWriteEvents[streamIdx][idm-1 > -1 ? idm-1 : ndm - 1], 0);
// Detect data
blocksize.x=32; blocksize.y=32; blocksize.z=1;
gridsize.x=mbin/blocksize.x+1; gridsize.y=nchan/blocksize.y+1; gridsize.z=nfft/blocksize.z+1;
transpose_unpadd_and_detect<<<gridsize,blocksize,0,stream>>>(cp1,cp2,mbin,nchan,nfft,nsub,noverlap/nchan,nread/nchan,dfbuf);
if (redig) {
// Compute block sums for redigitization
blocksize.x=32; blocksize.y=32; blocksize.z=1;
gridsize.x=mchan/blocksize.x+1; gridsize.y=mblock/blocksize.y+1; gridsize.z=1;
compute_block_sums<<<gridsize,blocksize,0,stream>>>(dfbuf,mchan,mblock,msum,bs1,bs2);
// Compute channel stats
blocksize.x=32; blocksize.y=1; blocksize.z=1;
gridsize.x=mchan/blocksize.x+1; gridsize.y=1; gridsize.z=1;
compute_channel_statistics<<<gridsize,blocksize,0,stream>>>(mchan,mblock,msum,bs1,bs2,zavg,zstd);
// Redigitize data to 8bits
blocksize.x=32; blocksize.y=32; blocksize.z=1;
gridsize.x=mchan/blocksize.x+1; gridsize.y=mblock/blocksize.y+1; gridsize.z=1;
if (ndec==1)
redigitize<<<gridsize,blocksize,0,stream>>>(dfbuf,mchan,mblock,msum,zavg,zstd,3.0,5.0,dcbuf);
else
decimate_and_redigitize<<<gridsize,blocksize,0,stream>>>(dfbuf,ndec,mchan,mblock,msum,zavg,zstd,3.0,5.0,dcbuf);
// Copy buffer to host
checkCudaErrors(cudaMemcpyAsync(cbuf[streamIdx][idm],dcbuf,sizeof(unsigned char)*msamp*mchan/ndec,cudaMemcpyDeviceToHost,stream));
} else {
if (ndec==1) {
checkCudaErrors(cudaMemcpyAsync(cbuff[streamIdx][idm], dfbuf,sizeof(float)*msamp*mchan,cudaMemcpyDeviceToHost,stream));
} else {
blocksize.x=32; blocksize.y=32; blocksize.z=1;
gridsize.x=mchan/blocksize.x+1; gridsize.y=mblock/blocksize.y+1; gridsize.z=1;
decimate<<<gridsize,blocksize,0,stream>>>(dfbuf,ndec,mchan,mblock,msum,dcbuff);
checkCudaErrors(cudaMemcpyAsync(cbuff[streamIdx][idm],dcbuff,sizeof(float)*msamp*mchan/ndec,cudaMemcpyDeviceToHost,stream));
}
}
// Record when the final memcpy finishes to block disk writes
cudaEventRecord(dmWriteEvents[streamIdx][idm], stream);
}
// Wrtie results to disk, waiting for each DM's memcpy to finish first
for (idm=0;idm<ndm;idm++) {
if (redig) {
write_to_disk_char(&(cbuf[streamIdx][idm][writeOffset*nsub/ndec]), &(outfile[idm]), writeSize, &(dmWriteEvents[streamIdx][idm]));
} else {
write_to_disk_float(&(cbuff[streamIdx][idm][writeOffset*nsub/ndec]), &(outfile[idm]), writeSize, &(dmWriteEvents[streamIdx][idm]));
}
}
CLICK(tock);
printf("Processed %d DMs in %.2f s\n",ndm, TICKTOCK(tick1, tock));
timeInSeconds += (double) (nread - writeOffset) * timeOffset;
elapsedTime = (double) TICKTOCK(tick, tock);
printf("Current data processed: %02ld:%02ld:%05.2lf (%1.2lfs) in %1.2lf seconds (%1.2lf/s)\n\n", (long int) (timeInSeconds / 3600.0), (long int) ((fmod(timeInSeconds, 3600.0)) / 60.0), fmod(timeInSeconds, 60.0), timeInSeconds, elapsedTime, (double) timeInSeconds / elapsedTime);
// Exit when we pass the read length limit
if (total_ts_read > ts_read) {
break;
}
if (iblock == 0) {
writeOffset = 0;
}
}
CLICK(tock);
printf("Finished processing %lfs of data in %fs (%lf/s). Cleaning up...\n", timeInSeconds, TICKTOCK(tick, tock), (float) timeInSeconds / (float) TICKTOCK(tick, tock));
//omp_destroy_lock(&readLock);
// Close files
for (i=0;i<ndm;i++)
fclose(outfile[i]);
// Reader cleanup
lofar_udp_reader_cleanup(reader);
// Free
free(header);
if (dreamBeam == 0) {
for (i=0;i<4;i++) {
cudaFree(dudpbuf_c[i]);
}
} else {
for (i=0;i<4;i++) {
cudaFree(dudpbuf_f[i]);
}
}
free(fbuf);
free(dm);
free(outfile);
if (redig) {
for (i = 0; i < ndm; i++)
for (j =0; j < numStreams; j++)
free(cbuf[j][i]);
cudaFree(bs1);
cudaFree(bs2);
cudaFree(zavg);
cudaFree(zstd);
cudaFree(dcbuf);
} else {
for (j =0; j < numStreams; j++)
for (i = 0; i < ndm; i++)
free(cbuff[j][i]);
free(cbuff[j]);
if (ndec > 1) cudaFree(dcbuff);
}
cudaFree(cufftWorkArea);
cudaFree(dfbuf);
cudaFree(cp1);
cudaFree(cp2);
cudaFree(cp1p);
cudaFree(cp2p);
cudaFree(dc);
cudaFree(ddm);
// Free plan
cufftDestroy(ftc2cf);
cufftDestroy(ftc2cb);
for(i = 0; i < numStreams + 1; i++)
cudaStreamDestroy(streams[i]);
for(i = 0; i < numEvents; i++)
cudaEventDestroy(events[i]);
for (i = 0; i < ndm; i++)
for (j =0; j < numStreams; j++)
cudaEventDestroy(dmWriteEvents[j][i]);
return 0;
}
void write_to_disk_float(float* outputArray, FILE** outputFile, int nsamples, cudaEvent_t* waitEvent)
{
cudaEventSynchronize(*waitEvent);
fwrite(outputArray,sizeof(float),nsamples, *outputFile);
}
void write_to_disk_char(unsigned char* outputArray, FILE** outputFile, int nsamples, cudaEvent_t* waitEvent)
{
cudaEventSynchronize(*waitEvent);
fwrite(outputArray,sizeof(char),nsamples, *outputFile);
}
// Rip out sigproc's header reader. Don't have the time to spend several hours reimplementing it; all credit to Lorimer et al.
//BEGIN SIGPROC READ_HEADER.C
//
int strings_equal (char *string1, char *string2) /* includefile */
{
if (!strcmp(string1,string2)) {
return 1;
} else {
return 0;
}
}
/* read a string from the input which looks like nchars-char[1-nchars] */
void get_string(FILE *inputfile, int *nbytes, char string[])
{
int nchar;
strcpy(string,"ERROR");
if (! fread(&nchar, sizeof(int), 1, inputfile)) fprintf(stderr, "Failed to get int at %d\n", *nbytes);
*nbytes=sizeof(int);
if (feof(inputfile)) exit(0);
if (nchar>80 || nchar<1) return;
if (! fread(string, nchar, 1, inputfile)) fprintf(stderr, "Failed to get stirng at %d\n", *nbytes);
string[nchar]='\0';
*nbytes+=nchar;
}
/* attempt to read in the general header info from a pulsar data file */
struct header read_header(FILE *inputfile) /* includefile */
{
char string[80], message[80];
int nbytes,totalbytes,expecting_rawdatafile=0,expecting_source_name=0;
int isign=0, dummyread=0;
struct header hdr;
/* try to read in the first line of the header */
get_string(inputfile,&nbytes,string);
if (!strings_equal(string, (char *) "HEADER_START")) {
/* the data file is not in standard format, rewind and return */
rewind(inputfile);
fprintf(stderr, "Unexpected input header; exiting.");
exit(1);
}
/* store total number of bytes read so far */
totalbytes=nbytes;
/* loop over and read remaining header lines until HEADER_END reached */
// David McKenna: We don't need all of these; ignore those values and just reference their lengths
while (1) {
get_string(inputfile,&nbytes,string);
if (strings_equal(string, (char *) "HEADER_END")) break;
totalbytes+=nbytes;
if (strings_equal(string, (char *) "rawdatafile")) {
expecting_rawdatafile=1;
} else if (strings_equal(string, (char *) "source_name")) {
expecting_source_name=1;
} else if (strings_equal(string, (char *) "FREQUENCY_START")) {
// pass
} else if (strings_equal(string, (char *) "FREQUENCY_END")) {
// pass
} else if (strings_equal(string, (char *) "az_start")) {
fseek(inputfile, sizeof(double), SEEK_CUR);
totalbytes+=sizeof(double);
} else if (strings_equal(string, (char *) "za_start")) {
fseek(inputfile, sizeof(double), SEEK_CUR);
totalbytes+=sizeof(double);
} else if (strings_equal(string, (char *) "src_raj")) {
dummyread = fread(&(hdr.src_raj),sizeof(hdr.src_raj),1,inputfile);
totalbytes+=sizeof(hdr.src_raj);
} else if (strings_equal(string, (char *) "src_dej")) {
dummyread = fread(&(hdr.src_dej),sizeof(hdr.src_dej),1,inputfile);
totalbytes+=sizeof(hdr.src_dej);
} else if (strings_equal(string, (char *) "tstart")) {
dummyread = fread(&(hdr.tstart),sizeof(hdr.tstart),1,inputfile);
totalbytes+=sizeof(hdr.tstart);
} else if (strings_equal(string, (char *) "tsamp")) {
dummyread = fread(&(hdr.tsamp),sizeof(hdr.tsamp),1,inputfile);
totalbytes+=sizeof(hdr.tsamp);
} else if (strings_equal(string, (char *) "period")) {
fseek(inputfile, sizeof(double), SEEK_CUR);
totalbytes+=sizeof(double);
} else if (strings_equal(string, (char *) "fch1")) {
dummyread = fread(&(hdr.fch1),sizeof(hdr.fch1),1,inputfile);
totalbytes+=sizeof(hdr.fch1);
} else if (strings_equal(string, (char *) "fchannel")) {
fseek(inputfile, sizeof(double), SEEK_CUR);
totalbytes+=sizeof(double);
} else if (strings_equal(string, (char *) "foff")) {
dummyread = fread(&(hdr.foff),sizeof(hdr.foff),1,inputfile);
totalbytes+=sizeof(hdr.foff);
} else if (strings_equal(string, (char *) "nchans")) {
// nsub seems to be nchans in the sigproc hdr
dummyread = fread(&(hdr.nsub),sizeof(hdr.nsub),1,inputfile);
totalbytes+=sizeof(hdr.nsub);
} else if (strings_equal(string, (char *) "telescope_id")) {
dummyread = fread(&(hdr.tel),sizeof(hdr.tel),1,inputfile);
totalbytes+=sizeof(hdr.tel);
} else if (strings_equal(string, (char *) "machine_id")) {
dummyread = fread(&(hdr.mach),sizeof(hdr.mach),1,inputfile);
totalbytes+=sizeof(int);
} else if (strings_equal(string, (char *) "data_type")) {
fseek(inputfile, sizeof(int), SEEK_CUR);
totalbytes+=sizeof(int);
} else if (strings_equal(string, (char *) "ibeam")) {
fseek(inputfile, sizeof(int), SEEK_CUR);
totalbytes+=sizeof(int);
} else if (strings_equal(string, (char *) "nbeams")) {
fseek(inputfile, sizeof(int), SEEK_CUR);
totalbytes+=sizeof(int);