Class: MPipe

Inherits:
Data
  • Object
show all
Defined in:
ext/mpipe/mpipe.c

Defined Under Namespace

Modules: Comm

Constant Summary collapse

VERSION =
rb_str_new2(MPIPE_VERSION)
MPI_VERSION =
INT2NUM(MPI_VERSION)
MPI_SUBVERSION =
INT2NUM(MPI_SUBVERSION)
SUCCESS =
INT2NUM(MPI_SUCCESS)
PROC_NULL =
INT2NUM(MPI_PROC_NULL)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#new(string = ""[, mode]) ⇒ Object

Creates new MPipe instance from with string and mode.



227
228
229
230
231
232
233
234
235
236
237
# File 'ext/mpipe/mpipe.c', line 227

static VALUE
mp_initialize(VALUE self, VALUE rank)
{
    struct MPipe *ptr = check_mpipe(self);

    if (!ptr) {
	DATA_PTR(self) = ptr = mp_alloc();
    }
    rb_call_super(0, 0);
    return mp_init(ptr, self, rank);
}

Class Method Details

.abort(rerror) ⇒ Object



83
84
85
86
87
88
89
90
# File 'ext/mpipe/mpipe.c', line 83

static VALUE
mp_mpi_abort(VALUE klass, VALUE rerror)
{
    int ierror;

    ierror = MPI_Abort(MPI_COMM_WORLD, NUM2INT(rerror));
    return INT2NUM(ierror);
}

.buffer_sizeObject



92
93
94
95
96
# File 'ext/mpipe/mpipe.c', line 92

static VALUE
mp_mpi_buffer_size(VALUE mod)
{
    return INT2NUM(mp_buffer_size);
}

.buffer_size=(size) ⇒ Object



98
99
100
101
102
103
104
105
106
# File 'ext/mpipe/mpipe.c', line 98

static VALUE
mp_mpi_set_buffer_size(VALUE mod, VALUE size)
{
    if (mp_initialized) {
        rb_raise(rb_eStandardError,"buffer_size must be set before MPipe.init");
    }
    mp_buffer_size = NUM2INT(size);
    return size;
}

.finalizeObject



76
77
78
79
80
81
# File 'ext/mpipe/mpipe.c', line 76

static VALUE
mp_mpi_finalize(VALUE klass)
{
    mp_finalize();
    return Qnil;
}

.init(*args) ⇒ Object

MPI



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
# File 'ext/mpipe/mpipe.c', line 40

static VALUE
mp_mpi_init(int argc, VALUE *argv, VALUE klass)
{
    char **cargv;
    VALUE progname, mpipe_ary;
    int i, size;

    cargv = ALLOCA_N(char *, argc+1);
    progname = rb_gv_get("$0");
    cargv[0] = StringValueCStr(progname);

    for(i=0; i<argc; i++) {
        if (TYPE(argv[i]) == T_STRING) {
            cargv[i+1] = StringValueCStr(argv[i]);
        } else {
            rb_raise(rb_eArgError, "argument must be string");
        }
    }
    argc++;

    MPI_Init(&argc, &cargv);

    if (mp_initialized) {
        return Qnil;
    } else {
        mp_initialized = 1;
    }
    atexit(mp_finalize);

    MPI_Comm_size(MPI_COMM_WORLD, &size);
    mpipe_ary = rb_ary_new2(size);
    rb_ivar_set(klass, id_allocated_mpipe, mpipe_ary);

    return Qnil;
}

.new(vrank) ⇒ Object

:nodoc:



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'ext/mpipe/mpipe.c', line 240

static VALUE
mp_s_new(VALUE klass, VALUE vrank)
{
    VALUE mpipe, mpipe_ary;
    int rank;

    rank = NUM2INT(vrank);
    mpipe_ary = rb_ivar_get(klass, id_allocated_mpipe);
    mpipe = rb_ary_entry(mpipe_ary, rank);
    if (NIL_P(mpipe)) {
        mpipe = rb_class_new_instance(1, &vrank, klass);
        rb_ary_store(mpipe_ary, rank, mpipe);
    }
    return mpipe;
}

.select(*args) ⇒ Object



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
# File 'ext/mpipe/mpipe.c', line 536

static VALUE
mp_s_select(int argc, VALUE *argv, VALUE mod)
{
    struct MPipe *ptr;
    MPI_Request *ary_of_requests;
    int *ary_of_indices;
    MPI_Status *ary_of_statuses;
    int incount, outcount;
    int i, count, istat;
    VALUE rd_ary, result_ary, item;

    if (argc==0) {
        rb_raise(rb_eArgError, "no argument");
    }
    incount = RARRAY_LEN(argv[0]);

    result_ary = rb_ary_new();
    rd_ary = rb_ary_new();
    rb_ary_push(result_ary, rd_ary);
    for (i=0; i < incount; i++) {
        item = RARRAY_AREF(argv[0], i);
        ptr = MPipe(item);
        if (ptr->recv_count > 0) {
            rb_ary_push(rd_ary, item);
        }
    }
    if (RARRAY_LEN(rd_ary) > 0) {
        return result_ary;
    }

    ary_of_requests = ALLOCA_N(MPI_Request, incount);
    ary_of_statuses = ALLOCA_N(MPI_Status, incount);
    ary_of_indices  = ALLOCA_N(int, incount);

    for (i=0; i < incount; i++) {
        item = RARRAY_AREF(argv[0], i);
        ptr = MPipe(item);
        call_irecv(ptr);
        ary_of_requests[i] = ptr->recv_request;
    }

    istat = MPI_Waitsome(incount, ary_of_requests,
                         &outcount, ary_of_indices, ary_of_statuses);
    if (istat != MPI_SUCCESS) {
        rb_raise(rb_eStandardError,"MPI_Waitany failed with status=%d",istat);
    }

    for (i=0; i < outcount; i++) {
        item = RARRAY_AREF(argv[0], ary_of_indices[i]);
        MPI_Get_count(&ary_of_statuses[i], MPI_BYTE, &count);
        ptr = MPipe(item);
        ptr->recv_count = count;
        rb_ary_push(rd_ary, item);
    }
    return result_ary;
}

Instance Method Details

#closeObject



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
# File 'ext/mpipe/mpipe.c', line 325

static VALUE
mp_write(VALUE self, VALUE str)
{
    struct MPipe *ptr = MPipe(self);
    int istat;
    int pos, count;

    str = StringValue(str);
    pos = 0;

    while (pos < RSTRING_LEN(str)) {
        count = RSTRING_LEN(str) - pos;
        if (count > mp_buffer_size) {
            count = mp_buffer_size;
        }
        memcpy(ptr->send_buffer, RSTRING_PTR(str)+pos, count);

        istat = MPI_Send(ptr->send_buffer, count, MPI_CHAR, ptr->rank,
                         0, MPI_COMM_WORLD);
        if (istat != MPI_SUCCESS) {
            rb_raise(rb_eStandardError,"MPI_send failed with status=%d\n",istat);
        }

        pos += count;
    }
    return self;
}

#read(length[, outbuf]) ⇒ String?

Reads length bytes from the I/O stream.

length must be a non-negative integer.

Returns:

  • (String, nil)


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
# File 'ext/mpipe/mpipe.c', line 381

static VALUE
mp_read(int argc, VALUE *argv, VALUE self)
{
    struct MPipe *ptr = MPipe(self);
    MPI_Status status;
    int istat;
    int outbuf_pos = 0;
    int max_len;
    VALUE maxlen = Qnil;
    VALUE outbuf = Qnil;

    rb_scan_args(argc, argv, "11", &maxlen, &outbuf);

    max_len = NUM2INT(maxlen);
    if (max_len < 0) {
        rb_raise(rb_eArgError, "negative length %d given", max_len);
    }

    if (NIL_P(outbuf)) {
        outbuf = rb_str_new(0, 0);
    }
    rb_str_resize(outbuf, max_len);

    if (ptr->recv_count == -1) { // requesting
        istat = MPI_Wait(&ptr->recv_request, &status);
        if (istat != MPI_SUCCESS) {
            rb_raise(rb_eStandardError,"MPI_Wait failed with status=%d",istat);
        }
        MPI_Get_count(&status, MPI_CHAR, &ptr->recv_count);
    }

    if (ptr->recv_count > 0) {
        outbuf_pos += copy_substr(ptr, max_len, outbuf, outbuf_pos);
    }

    while (outbuf_pos < max_len) {
        istat = MPI_Recv(ptr->recv_buffer, mp_buffer_size, MPI_CHAR, ptr->rank,
                         0, MPI_COMM_WORLD, &status);
        if (istat != MPI_SUCCESS) {
            rb_raise(rb_eStandardError,"MPI_recv failed with status=%d\n",istat);
        }
        MPI_Get_count(&status, MPI_CHAR, &ptr->recv_count);

        outbuf_pos += copy_substr(ptr, max_len, outbuf, outbuf_pos);
    }

    return outbuf;
}

#read_nonblock(maxlen[, outbuf [, opts]]) ⇒ String

Similar to #read, but raises EOFError at end of string unless the exception: false option is passed in.

Returns:

  • (String)


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
# File 'ext/mpipe/mpipe.c', line 478

static VALUE
mp_read_nonblock(int argc, VALUE *argv, VALUE self)
{
    struct MPipe *ptr = MPipe(self);
    int outbuf_pos = 0;
    int max_len;
    VALUE maxlen = Qnil;
    VALUE outbuf = Qnil;
    VALUE opts = Qnil;

    rb_scan_args(argc, argv, "11:", &maxlen, &outbuf, &opts);

    max_len = NUM2INT(maxlen);
    if (max_len < 0) {
        rb_raise(rb_eArgError, "negative length %d given", max_len);
    }

    if (NIL_P(outbuf)) {
        outbuf = rb_str_new(0, 0);
    }
    rb_str_resize(outbuf, max_len);

    if (maxlen == 0) {
        return outbuf;
    }

    if (ptr->recv_count == -1) { // requesting
        if (call_test(ptr) == 0) {
            if (!NIL_P(opts) && rb_hash_lookup2(opts, sym_exception, Qundef) == Qfalse) {
                return Qnil;
            } else {
                rb_raise(eEAGAINWaitReadable,"MPI_Irecv would block");
            }
        }
    }
    if (ptr->recv_count > 0) {
        outbuf_pos += copy_substr(ptr, max_len, outbuf, outbuf_pos);
    }

    while (outbuf_pos < max_len) {
        call_irecv(ptr);
        if (call_test(ptr) == 0) {
            if (outbuf_pos > 0) {
                rb_str_resize(outbuf, outbuf_pos);
                return outbuf;
            } else
            if (!NIL_P(opts) && rb_hash_lookup2(opts, sym_exception, Qundef) == Qfalse) {
                return Qnil;
            } else {
                rb_raise(eEAGAINWaitReadable,"MPI_Irecv would block");
            }
        }
        outbuf_pos += copy_substr(ptr, max_len, outbuf, outbuf_pos);
    }
    return outbuf;
}

#write(str) ⇒ Object



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
# File 'ext/mpipe/mpipe.c', line 325

static VALUE
mp_write(VALUE self, VALUE str)
{
    struct MPipe *ptr = MPipe(self);
    int istat;
    int pos, count;

    str = StringValue(str);
    pos = 0;

    while (pos < RSTRING_LEN(str)) {
        count = RSTRING_LEN(str) - pos;
        if (count > mp_buffer_size) {
            count = mp_buffer_size;
        }
        memcpy(ptr->send_buffer, RSTRING_PTR(str)+pos, count);

        istat = MPI_Send(ptr->send_buffer, count, MPI_CHAR, ptr->rank,
                         0, MPI_COMM_WORLD);
        if (istat != MPI_SUCCESS) {
            rb_raise(rb_eStandardError,"MPI_send failed with status=%d\n",istat);
        }

        pos += count;
    }
    return self;
}

#write_nonblock(str) ⇒ Object



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
# File 'ext/mpipe/mpipe.c', line 325

static VALUE
mp_write(VALUE self, VALUE str)
{
    struct MPipe *ptr = MPipe(self);
    int istat;
    int pos, count;

    str = StringValue(str);
    pos = 0;

    while (pos < RSTRING_LEN(str)) {
        count = RSTRING_LEN(str) - pos;
        if (count > mp_buffer_size) {
            count = mp_buffer_size;
        }
        memcpy(ptr->send_buffer, RSTRING_PTR(str)+pos, count);

        istat = MPI_Send(ptr->send_buffer, count, MPI_CHAR, ptr->rank,
                         0, MPI_COMM_WORLD);
        if (istat != MPI_SUCCESS) {
            rb_raise(rb_eStandardError,"MPI_send failed with status=%d\n",istat);
        }

        pos += count;
    }
    return self;
}