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.



221
222
223
224
225
226
227
228
229
230
231
# File 'ext/mpipe/mpipe.c', line 221

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:



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'ext/mpipe/mpipe.c', line 234

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



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

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);
        request_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



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

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(*args) ⇒ Object



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

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

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

    if (ptr->recv_count > 0) {
        return mp_substr(ptr, maxlen, outbuf);
    }
    if (ptr->recv_count == 0) {
        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);
        }
    } else { // 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, &count);
    ptr->recv_count = count;
    return mp_substr(ptr, maxlen, outbuf);
}

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

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

Returns:

  • (String)


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

static VALUE
mp_read_nonblock(int argc, VALUE *argv, VALUE self)
{
    struct MPipe *ptr = MPipe(self);
    MPI_Status status;
    int istat;
    int count;
    int complete = 0;
    VALUE maxlen = Qnil;
    VALUE outbuf = Qnil;
    VALUE opts = Qnil;
    VALUE val;

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

    if (ptr->recv_count > 0) {
        return mp_substr(ptr, maxlen, outbuf);
    }
    request_irecv(ptr);
    istat = MPI_Test(&ptr->recv_request, &complete, &status);
    if (istat != MPI_SUCCESS) {
        rb_raise(rb_eStandardError,"MPI_Test failed with status=%d",istat);
    }
    if (complete) {
        MPI_Get_count(&status, MPI_CHAR, &count);
        ptr->recv_count = count;
        val = mp_substr(ptr, maxlen, outbuf);
    } else {
  if (!NIL_P(opts) &&
            rb_hash_lookup2(opts, sym_exception, Qundef) == Qfalse) {
      return Qnil;
        } else {
            rb_raise(eEAGAINWaitReadable,"MPI_Irecv would block");
        }
    }
    return val;
}

#write(str) ⇒ Object



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

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



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

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;
}