psrdada-sys 0.2.0

Bindgen wrappers for psrdada
Documentation
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

#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

#include "config.h"
#include "dada_client.h"
#include "dada_def.h"
#include "ascii_header.h"
#include "diff_time.h"

#ifdef HAVE_CUDA
#include <cuda_runtime.h>
#endif

/*! Create a new DADA client main loop */
dada_client_t* dada_client_create ()
{
  dada_client_t* client = malloc (sizeof(dada_client_t));
  assert (client != 0);

  client -> log = 0;
  client -> data_block = 0;
  client -> header_block = 0;

  client -> open_function = 0;
  client -> io_function = 0;
  client -> io_block_function = 0;
  client -> io_block_function_cuda = 0;
  client -> close_function = 0;
  client -> direction = dada_client_undefined;

  client -> context = 0;

  client -> header = 0;
  client -> header_size = 0;
  client -> header_transfer = 1;

  client -> fd = -1;
  client -> transfer_bytes = 0;
  client -> optimal_bytes = 0;
  client -> quit = 0;
  client -> quiet = 0;

  return client;
}

/*! Destroy a DADA client main loop */
void dada_client_destroy (dada_client_t* client)
{
  if (client->header)
    free (client->header);
  client->header = 0;
  free (client);
}

/*! Transfer data between the Data Block and transfer target */
int64_t dada_client_io_loop (dada_client_t* client)
{
  /* The buffer used for transfer between Data Block and transfer target */
  static char* buffer = 0;
  static uint64_t buffer_size = 0;

  /* counters */
  uint64_t bytes_to_transfer = 0;
  uint64_t bytes_transfered = 0;
  int64_t bytes = 0;

  multilog_t* log = 0;

  assert (client != 0);
  log = client->log;
  assert (log != 0);

  if (buffer_size != client->optimal_bytes) {
    buffer_size = client->optimal_bytes;
    if (buffer)
      free (buffer);
    int rval = posix_memalign ( (void **) &buffer, 512, client->optimal_bytes);
    if (rval != 0)
      multilog (log, LOG_ERR, "io_loop posix_memalign failed\n");
    assert (buffer != 0);

#ifdef _DEBUG
    fprintf (stderr, "buffer size = %"PRIu64"\n", buffer_size);
#endif

  }

  assert (client->direction==dada_client_reader ||
          client->direction==dada_client_writer);

  while (!client->transfer_bytes || bytes_transfered < client->transfer_bytes)
  {
          
    if (!client->transfer_bytes)
      bytes = buffer_size;
    else
    {
      bytes_to_transfer = client->transfer_bytes - bytes_transfered;
#ifdef _DEBUG
      fprintf(stderr,"transfer_bytes = %"PRIu64", bytes_transfered = %"PRIu64
                      ", bytes_to_transfer = %"PRIu64"\n",
                      client->transfer_bytes,bytes_transfered,bytes_to_transfer);
#endif

      if (buffer_size > bytes_to_transfer)
        bytes = bytes_to_transfer;
      else
        bytes = buffer_size;
    }

    if (client->direction == dada_client_reader) {

      if (ipcbuf_eod((ipcbuf_t*)client->data_block))
      {
#ifdef _DEBUG
        fprintf (stderr, "io_loop: ipcbuf_eod true on data_block\n");
#endif
        break;
      }

#ifdef _DEBUG
      fprintf (stderr, "calling ipcio_read %p %"PRIi64"\n", buffer, bytes);
#endif

      bytes = ipcio_read (client->data_block, buffer, bytes);
      if (bytes < 0) {
        multilog (log, LOG_ERR, "ipcio_read error %s\n", strerror(errno));
        return -1;
      }

    }

#ifdef _DEBUG
    fprintf (stderr, "calling io_function %p %"PRIi64"\n", buffer, bytes);
#endif

    bytes = client->io_function (client, buffer, bytes);

#ifdef _DEBUG
    fprintf (stderr, "io_function returns %"PRIi64"\n", bytes);
#endif

    if (bytes < 0) {
      multilog (log, LOG_ERR, "I/O error %s\n", strerror(errno));
      break;
    }

    if (client->direction == dada_client_writer) {

      if (bytes == 0) {
        //multilog (log, LOG_INFO, "end of input\n");
        break;
      }

      bytes = ipcio_write (client->data_block, buffer, bytes);
      if (bytes < 0) {
        multilog (log, LOG_ERR, "ipcio_write error %s\n", strerror(errno));
        return -1;
      }
    }
    bytes_transfered += bytes;
  }

  return bytes_transfered;
}


/*! transfer data between the Data Block and the transfer target in a 
 *  block by block fashion operating directly on shared memory */
int64_t dada_client_io_loop_block (dada_client_t* client)
{

  /* counters */
  uint64_t bytes_to_transfer = 0;
  uint64_t bytes_transfered = 0;
  
  assert (client != 0);

  multilog_t* log = 0;
  log = client->log;
  assert (log != 0);
  
  assert (client->direction==dada_client_reader ||
          client->direction==dada_client_writer);

  // get the data block unit size
  uint64_t block_size = ipcbuf_get_bufsz((ipcbuf_t*)client->data_block);

  uint64_t block_id = 0;

  uint64_t bytes = 0;

  int64_t bytes_operated = 0;

  // current Data Block buffer on which to operate
  char * buffer;

#ifdef HAVE_CUDA
  int device_id = ipcbuf_get_device ((ipcbuf_t*)client->data_block);
  void * local_buffer;
  cudaError_t err;
  if (device_id >= 0)
  {
    cudaSetDevice (device_id);
    if (!client->io_block_function_cuda)
    {
      err = cudaMallocHost (&local_buffer, block_size);
      if (err != cudaSuccess)
      {
        multilog (log, LOG_ERR, "io_loop_block: cudaMallocHost failed %s\n", cudaGetErrorString(err));
        return -1;
      }
    }
  }
#endif
       
  while (!client->transfer_bytes || bytes_transfered < client->transfer_bytes)
  {
#ifdef _DEBUG
    fprintf(stderr, "io_loop_block: bytes_transferred=%"PRIu64", client->transfer_bytes=%"PRIu64"\n", 
            bytes_transfered, client->transfer_bytes);
#endif
   
    if (!client->transfer_bytes)
      bytes = block_size;
    else
    {
      bytes_to_transfer = client->transfer_bytes - bytes_transfered;

      if (block_size > bytes_to_transfer)
        bytes = bytes_to_transfer;
      else
        bytes = block_size;
    }
    
    buffer = 0;

    if (client->direction == dada_client_reader) {

      if (ipcbuf_eod((ipcbuf_t*)client->data_block))
        break;

      // get the pointer to the next full block, the number of bytes in it and the ID of block
      buffer = ipcio_open_block_read (client->data_block, &bytes, &block_id);

      if (!buffer) {
        multilog (log, LOG_ERR, "io_loop_block: ipcio_open_block_read error %s\n", strerror(errno));
        return -1;
      }
    }

    if (client->direction == dada_client_writer) {

      // get the pointer to the next empty block and its ID
      buffer = ipcio_open_block_write(client->data_block, &block_id);
      if (!buffer) {
        multilog (log, LOG_ERR, "io_loop_block: ipcio_open_block_write error %s\n", strerror(errno));
        return -1;
      }
    }


    // call the io_block function
    if (bytes)
    {
#ifdef HAVE_CUDA
      if (device_id >= 0)
      {
        // if the client supports cuda block transfers
        if (client->io_block_function_cuda)
        {
          bytes_operated = client->io_block_function_cuda (client, buffer, bytes, block_id); 
        }
        // the client does not support cuda block transfers, use a host buffer for staging
        else
        {
          if (client->direction == dada_client_reader)
          {
            err = cudaMemcpy (local_buffer, buffer, bytes, cudaMemcpyDeviceToHost);
            if (err != cudaSuccess)
            {
              multilog (log, LOG_ERR, "io_loop_block: cudaMemcpy failed: %s\n",
                        cudaGetErrorString(err));
            }
          }

          bytes_operated = client->io_block_function (client, local_buffer, bytes, block_id);

          if (client->direction == dada_client_writer)
          {
            err = cudaMemcpy (buffer, local_buffer, bytes, cudaMemcpyHostToDevice);
            if (err != cudaSuccess)
            {
              multilog (log, LOG_ERR, "io_loop_block: cudaMemcpy failed: %s\n",
                        cudaGetErrorString(err));
            }
          }
        }
      }
      // data block resides in host memory
      else
#endif
      {

#ifdef _DEBUG
        fprintf (stderr, "io_loop_block: io_block_function() addr=%p id=%"PRIu64", bytes=%"PRIi64"\n", buffer, block_id, bytes);
#endif
        bytes_operated = client->io_block_function (client, buffer, bytes, block_id);
      }
    }
    else
    {
#ifdef DEBUG
      multilog (log, LOG_INFO, "io_loop_block: bytes=0!\n");
#endif
      bytes_operated = 0;
    }

#ifdef _DEBUG
    fprintf (stderr, "io_loop_block: io_block_function returns %"PRIi64"\n", bytes_operated);
#endif

    if (bytes_operated < 0) {
      multilog (log, LOG_ERR, "io_loop_block: I/O error %s\n", strerror(errno));
      break;
    }

    if (client->direction == dada_client_reader) 
    {
      if (ipcio_close_block_read (client->data_block, (uint64_t) bytes_operated) < 0)
      {
        multilog (log, LOG_ERR, "io_loop_block: ipcio_close_block_read failed\n");
      }
    }

    if (client->direction == dada_client_writer) 
    {
      if (bytes_operated == 0) 
        multilog (log, LOG_INFO, "io_loop_block: end of input\n");
#ifdef _DEBUG
      multilog (log, LOG_INFO, "io_loop_block: ipcio_close_block_write (%ld)\n", bytes_operated);
#endif
      if (ipcio_close_block_write (client->data_block, (uint64_t) bytes_operated) < 0) {
        multilog (log, LOG_ERR, "io_loop_block: ipcio_close_block_write error %s\n", strerror(errno));
        return -1;
      }
      
      if (bytes_operated < block_size)
      {
#ifdef _DEBUG
        multilog (log, LOG_INFO, "io_loop_block: end of data\n");
#endif
        bytes_transfered += bytes_operated;
        break;
      }
    }

    bytes_transfered += bytes_operated;
  }

#ifdef HAVE_CUDA
  if ((device_id >= 0) && (!client->io_block_function_cuda))
  {
    cudaFreeHost (local_buffer);
  }
#endif

#ifdef DEBUG
  multilog (log, LOG_INFO, "io_loop_block: transferred %"PRIi64" bytes\n", bytes_transfered);
#endif

  return bytes_transfered;

}

int64_t dada_client_transfer (dada_client_t* client)
{
  /* pointer to the status and error logging facility */
  multilog_t* log = 0;

  /* Byte count */
  int64_t bytes_transfered = 0;

  /* the size of the header buffer */
  uint64_t header_size = 0;

  assert (client != 0);

  log = client->log;
  assert (log != 0);

#ifdef _DEBUG
  fprintf (stderr, "dada_client_transfer call open_function\n");
#endif

  if (client->open_function (client) < 0) {
    multilog (log, LOG_ERR, "Error calling open function\n");
    return -1;
  }

  if (client->quit)
  {
    multilog (log, LOG_INFO, "Client quitting\n");
    return 0;
  }

#ifdef _DEBUG
  fprintf (stderr, "open_function returned\n");
#endif

  assert (client->header != 0);
  assert (client->header_size != 0);

  header_size = client->header_size;

#ifdef _DEBUG
  fprintf (stderr, "before calling io_function:\n");
  fprintf (stderr, "HEADER START\n%sHEADER END\n", client->header);
#endif

  if (client->header_transfer)
  {
    bytes_transfered = client->io_function (client, client->header,
              header_size);
    if (bytes_transfered < header_size) 
    {
      multilog (log, LOG_ERR, "Error transfering header: %s\n", 
                strerror(errno));
      return -1;
    }
  }

  if (client->direction == dada_client_writer) {

#ifdef _DEBUG
    fprintf (stderr, "dada_client_writer HEADER START\n%sHEADER END\n", client->header);
#endif

    header_size = ipcbuf_get_bufsz (client->header_block);
    if (ipcbuf_mark_filled (client->header_block, header_size) < 0)  {
      multilog (log, LOG_ERR, "Could not mark filled Header Block\n");
      return EXIT_FAILURE;
    }
  }

  if (!client->optimal_bytes)
    client->optimal_bytes = DADA_DEFAULT_BLOCK_SIZE;

  /* Transfer data until the end of the transfer */
  if (client->io_block_function)
    bytes_transfered = dada_client_io_loop_block (client);
  else
    bytes_transfered = dada_client_io_loop (client);

#ifdef _DEBUG
  multilog (log, LOG_INFO, "dada_client_transfer: io_loop transferred %"PRIu64" bytes\n", bytes_transfered);
#endif

  if (client->close_function (client, bytes_transfered) < 0) {
    multilog (log, LOG_ERR, "Error calling close function\n");
    return -1;
  }

#ifdef _DEBUG
  multilog (log, LOG_INFO, "dada_client_transfer: end of function, returning  %"PRIu64"\n", bytes_transfered);
#endif
 
  return bytes_transfered;
}


int dada_client_read (dada_client_t* client)
{

  /* pointer to the status and error logging facility */
  multilog_t* log = 0;

  /* The header from the ring buffer */
  char* header = 0;
  uint64_t header_size = 0;

  /* header size, as defined by HDR_SIZE attribute */
  uint64_t hdr_size = 0;

  /* byte offset from start of data, as defined by OBS_OFFSET attribute */
  uint64_t obs_offset = 0;

  /* Byte count */
  int64_t bytes_written = 0;

  /* Byte count */
  uint64_t total_bytes_written = 0;

  /* Time at start and end of transfer */
  struct timeval start_loop, end_loop;

  /* Time required to transfer data */
  double transfer_time = 0;

  assert (client != 0);

  log = client->log;
  assert (log != 0);

  while (!header_size) {

    /* Wait for the next valid header sub-block */
    header = ipcbuf_get_next_read (client->header_block, &header_size);

    if (!header) {
      multilog (log, LOG_ERR, "Could not get next header\n");
      return DADA_ERROR_FATAL;
    }

    if (!header_size) {

      ipcbuf_mark_cleared (client->header_block);

      if (ipcbuf_eod (client->header_block)) {
        multilog (log, LOG_INFO, "End of data on header block\n");
        ipcbuf_reset (client->header_block);
      }
      else {
        multilog (log, LOG_ERR, "Empty header block\n");
        return -1;
      }
    }
  }

  header_size = ipcbuf_get_bufsz (client->header_block);

  /* Check that header is of advertised size */
  if (ascii_header_get (header, "HDR_SIZE", "%"PRIu64, &hdr_size) != 1) {
    multilog (log, LOG_ERR, "Header with no HDR_SIZE. Setting to %"PRIu64"\n",
              header_size);
    hdr_size = header_size;
    if (ascii_header_set (header, "HDR_SIZE", "%"PRIu64, hdr_size) < 0) {
      multilog (log, LOG_ERR, "Error setting HDR_SIZE\n");
      return -1;
    }
  }

  if (hdr_size < header_size)
    header_size = hdr_size;

  else if (hdr_size > header_size) {
    multilog (log, LOG_ERR, "HDR_SIZE=%"PRIu64
              " > Header Block size=%"PRIu64"\n", hdr_size, header_size);
    multilog (log, LOG_DEBUG, "ASCII header dump\n%s", header);
    return -1;
  }

  /* Duplicate the header */
  if (header_size > client->header_size) {
    
    if (client->header)
      free (client->header);
    int rval = posix_memalign ( (void **) &(client->header), 512, header_size);
    if (rval != 0)
    {
      multilog (log, LOG_ERR, "dada_client_read: posix_memalign failed\n");
      return -1;
    }

    assert (client->header != 0);
    client->header_size = header_size;
  }
  memcpy (client->header, header, header_size);

  ipcbuf_mark_cleared (client->header_block);

  /* Get the header offset */
  if (ascii_header_get (client->header, "OBS_OFFSET",
                        "%"PRIu64, &obs_offset) != 1) {
    multilog (log, LOG_WARNING, "Header with no OBS_OFFSET\n");
    obs_offset = 0;
  }

  if ((!client->quiet) && client->transfer_bytes)
    multilog (log, LOG_INFO, "Transfering %"PRIu64" bytes in %"PRIu64
            " byte blocks\n", client->transfer_bytes, client->optimal_bytes);

  gettimeofday (&start_loop, NULL);
  
  // Transfer data until the end of the data stream
  while (!ipcbuf_eod((ipcbuf_t*)client->data_block)) 
  {

#ifdef _DEBUG
    fprintf(stderr,"dada_client_read: not EOD, setting OBS_OFFSET=%"PRIu64"\n", obs_offset);
#endif

    /* Set the header offset */
    if (ascii_header_set (client->header, "OBS_OFFSET",
                          "%"PRIu64, obs_offset) < 0) {
      multilog (log, LOG_ERR, "Error writing OBS_OFFSET\n");
      return -1;
    }

    bytes_written = dada_client_transfer (client);

    if (bytes_written < 0)
      return -1;

    obs_offset += bytes_written;
    total_bytes_written += bytes_written;

#ifdef _DEBUG
    fprintf(stderr,"dada_client_read: wrote %"PRIu64" bytes obs_offset = %"PRIu64"\n",bytes_written, obs_offset);
#endif

  }
  gettimeofday (&end_loop, NULL);

  transfer_time = diff_time (start_loop, end_loop);

  if (!client->quiet) 
    multilog (log, LOG_INFO, "%"PRIu64" bytes transferred in %lfs "
                             "(%lg MB/s)\n", total_bytes_written, transfer_time,
                             total_bytes_written/(1024*1024*transfer_time));
    
  ipcbuf_reset ((ipcbuf_t*)client->data_block);

  return 0;

}

/*! Run the DADA client write loop */
int dada_client_write (dada_client_t* client)
{
  /* pointer to the status and error logging facility */
  multilog_t* log = 0;

  /* The header from the ring buffer */
  char* header = 0;

  uint64_t header_size = 0;

  // number of bytes read by transfer function
  int64_t bytes_read = 0;

  // Time at start and end of transfer
  struct timeval start_loop, end_loop;

  // time taken to transfer data
  double transfer_time;

  assert (client != 0);

  log = client->log;
  assert (log != 0);

  header_size = ipcbuf_get_bufsz (client->header_block);
  //multilog (log, LOG_INFO, "header block size = %"PRIu64"\n", header_size);

  if (header_size) {

    header = ipcbuf_get_next_write (client->header_block);
    if (!header)  {
      multilog (log, LOG_ERR, "Could not get next header block\n");
      return EXIT_FAILURE;
    }

    client->header = header;
    client->header_size = header_size;

    gettimeofday (&start_loop, NULL);

    // the open function of a write client should set the header_size
    // and transfer size attributes
    bytes_read = dada_client_transfer (client);

    if (bytes_read < 0)
      return -1;

    gettimeofday (&end_loop, NULL);

    transfer_time = diff_time (start_loop, end_loop);

    if (!client->quiet)
      multilog (log, LOG_INFO, "%"PRIu64" bytes transferred in %lfs "
                               "(%lg MB/s)\n", bytes_read, transfer_time,
                               bytes_read/(1024*1024*transfer_time));
  }

#ifdef _DEBUG
  fprintf(stderr,"dada_client_write: end of function, returning 0\n");
#endif

  return 0;
}

int dada_client_stop (dada_client_t* client)
{
    multilog_t* log = client->log;

  //  signal end of data on Data Block
  if (ipcio_stop (client->data_block) < 0)  {
    multilog (log, LOG_ERR, "Could not close Data Block\n");
    return -1;
  }
  return 0;
}

int dada_client_close (dada_client_t* client)
{
  multilog_t* log = client->log;

  //  signal end of data on Data Block
  if (ipcio_close (client->data_block) < 0)  {
    multilog (log, LOG_ERR, "Could not close Data Block\n");
    return -1;
  }
  return 0;
}