Class: MemoryViewTestHelper::NDArray

Inherits:
Object
  • Object
show all
Defined in:
lib/memory-view-test-helper.rb,
ext/memory-view-test-helper/memory-view-test-helper.c

Constant Summary collapse

INTEGER_TYPES =
Set[:int8, :uint8, :int16, :uint16, :int32, :uint32, :int64, :uint64].freeze
SIZEOF_DTYPE =
{
  int8:    1,  uint8:  1,
  int16:   2,  uint16: 2,
  int32:   4,  uint32: 4,
  int64:   8,  uint64: 8,
  float32: 4,
  float64: 8
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(shape_ary, dtype_name) ⇒ Object



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
# File 'ext/memory-view-test-helper/memory-view-test-helper.c', line 272

static VALUE
ndarray_initialize(VALUE obj, VALUE shape_ary, VALUE dtype_name)
{
  int i;

  Check_Type(shape_ary, T_ARRAY);

  const ssize_t ndim = (ssize_t)RARRAY_LEN(shape_ary);
  for (i = 0; i < ndim; ++i) {
    VALUE si = RARRAY_AREF(shape_ary, i);
    Check_Type(si, T_FIXNUM);
  }

  ssize_t *shape = ALLOC_N(ssize_t, ndim);
  for (i = 0; i < ndim; ++i) {
    VALUE si = RARRAY_AREF(shape_ary, i);
    shape[i] = NUM2SSIZET(si);
  }

  ndarray_dtype_t dtype = ndarray_obj_to_dtype_t(dtype_name);

  ssize_t *strides = ALLOC_N(ssize_t, ndim);
  ndarray_init_row_major_strides(dtype, ndim, shape, strides);

  ndarray_t *nar;
  TypedData_Get_Struct(obj, ndarray_t, &ndarray_data_type, nar);

  ssize_t byte_size = strides[0] * shape[0];
  nar->data = ALLOC_N(uint8_t, byte_size);
  nar->byte_size = byte_size;
  nar->dtype = dtype;
  nar->ndim = ndim;
  nar->shape = shape;
  nar->strides = strides;

  return Qnil;
}

Class Method Details

.try_convert(obj, dtype: nil, order: :row_major) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/memory-view-test-helper.rb', line 7

def self.try_convert(obj, dtype: nil, order: :row_major)
  begin
    ary = obj.to_ary
  rescue TypeError
    raise ArgumentError, "the argument must be converted to an Array by to_ary (#{obj.class} given)"
  end

  dtype, shape, cache = detect_dtype_and_shape(ary, dtype)
  nar = new(shape, dtype)
  assign_cache(nar, cache)
  return nar
end

Instance Method Details

#==(other) ⇒ Object



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
# File 'ext/memory-view-test-helper/memory-view-test-helper.c', line 648

static VALUE
ndarray_eq(VALUE obj, VALUE other)
{
  if (obj == other)
    return Qtrue;
  else if (!rb_typeddata_is_kind_of(other, &ndarray_data_type)) {
    return Qfalse;
  }

  ndarray_t *nar1, *nar2;
  TypedData_Get_Struct(obj, ndarray_t, &ndarray_data_type, nar1);
  TypedData_Get_Struct(other, ndarray_t, &ndarray_data_type, nar2);

  const ssize_t ndim = nar1->ndim;
  if (ndim != nar2->ndim)
    return Qfalse;

  if (ndim == 1) {
    const ssize_t n = nar1->shape[0];
    if (n != nar2->shape[0])
      return Qfalse;

    ssize_t i;
    for (i = 0; i < n; ++i) {
      VALUE v1 = ndarray_1d_aref(nar1, i);
      VALUE v2 = ndarray_1d_aref(nar2, i);
      if (!rb_equal(v1, v2))
        return Qfalse;
    }

    return Qtrue;
  }
  else {
    return ndarray_md_eq(nar1, nar2);
  }
}

#[](*args) ⇒ Object



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
# File 'ext/memory-view-test-helper/memory-view-test-helper.c', line 439

static VALUE
ndarray_aref(int argc, VALUE *argv, VALUE obj)
{
  ndarray_t *nar;
  TypedData_Get_Struct(obj, ndarray_t, &ndarray_data_type, nar);

  if (nar->ndim != argc) {
    rb_raise(rb_eIndexError, "index dimension mismatched (%d for %"PRIdSIZE")", argc, nar->ndim);
  }

  const ssize_t ndim = nar->ndim;
  if (ndim == 1) {
    const ssize_t i = NUM2SSIZET(argv[0]);
    return ndarray_1d_aref(nar, i);
  }
  else {
    ssize_t inline_indices_buf[MAX_INLINE_DIM] = { 0, };
    ssize_t *indices = inline_indices_buf;

    VALUE heap_indices_buf = 0;
    if (ndim > MAX_INLINE_DIM) {
      indices = RB_ALLOCV_N(ssize_t, heap_indices_buf, ndim);
    }

    ssize_t i;
    for (i = 0; i < ndim; ++i) {
      indices[i] = NUM2SSIZET(argv[i]);
    }

    VALUE res = ndarray_md_aref(nar, indices);
    RB_ALLOCV_END(heap_indices_buf);
    return res;
  }
}

#[]=(*args) ⇒ Object



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
# File 'ext/memory-view-test-helper/memory-view-test-helper.c', line 537

static VALUE
ndarray_aset(int argc, VALUE *argv, VALUE obj)
{
  ndarray_t *nar;
  TypedData_Get_Struct(obj, ndarray_t, &ndarray_data_type, nar);

  rb_check_frozen(obj);

  if (nar->ndim != argc - 1) {
    rb_raise(rb_eIndexError, "index dimension mismatched (%d for %"PRIdSIZE")", argc - 1, nar->ndim);
  }

  const VALUE val = argv[argc-1];
  const int item_size = SIZEOF_DTYPE(nar->dtype);

  const ssize_t ndim = nar->ndim;
  if (ndim == 1) {
    /* special case for 1-D array */
    ssize_t i = NUM2SSIZET(argv[0]);
    uint8_t *p = ((uint8_t *)nar->data) + i * item_size;
    return ndarray_set_value(p, nar->dtype, val);
  }
  else {
    ssize_t inline_indices_buf[MAX_INLINE_DIM] = { 0, };
    ssize_t *indices = inline_indices_buf;

    VALUE heap_indices_buf = 0;
    if (ndim > MAX_INLINE_DIM) {
      indices = RB_ALLOCV_N(ssize_t, heap_indices_buf, ndim);
    }

    ssize_t i;
    for (i = 0; i < ndim; ++i) {
      indices[i] = NUM2SSIZET(argv[i]);
    }

    VALUE res = ndarray_md_aset(nar, indices, val);
    RB_ALLOCV_END(heap_indices_buf);
    return res;
  }
}

#byte_sizeObject



310
311
312
313
314
315
316
317
# File 'ext/memory-view-test-helper/memory-view-test-helper.c', line 310

static VALUE
ndarray_get_byte_size(VALUE obj)
{
  ndarray_t *nar;
  TypedData_Get_Struct(obj, ndarray_t, &ndarray_data_type, nar);

  return SSIZET2NUM(nar->byte_size);
}

#dtypeObject



319
320
321
322
323
324
325
326
327
328
329
# File 'ext/memory-view-test-helper/memory-view-test-helper.c', line 319

static VALUE
ndarray_get_dtype(VALUE obj)
{
  ndarray_t *nar;
  TypedData_Get_Struct(obj, ndarray_t, &ndarray_data_type, nar);

  if (ndarray_dtype_none < nar->dtype && nar->dtype < NDARRAY_NUM_DTYPES) {
    return ID2SYM(DTYPE_ID(nar->dtype));
  }
  return Qnil;
}

#ndimObject



331
332
333
334
335
336
337
338
# File 'ext/memory-view-test-helper/memory-view-test-helper.c', line 331

static VALUE
ndarray_get_ndim(VALUE obj)
{
  ndarray_t *nar;
  TypedData_Get_Struct(obj, ndarray_t, &ndarray_data_type, nar);

  return SSIZET2NUM(nar->ndim);
}

#reshape(new_shape, order: :row_major) ⇒ Object



158
159
160
# File 'lib/memory-view-test-helper.rb', line 158

def reshape(new_shape, order: :row_major)
  reshape_impl(new_shape.to_ary, order.to_sym)
end

#shapeObject



340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'ext/memory-view-test-helper/memory-view-test-helper.c', line 340

static VALUE
ndarray_get_shape(VALUE obj)
{
  ndarray_t *nar;
  TypedData_Get_Struct(obj, ndarray_t, &ndarray_data_type, nar);

  VALUE ary = rb_ary_new_capa(nar->ndim);
  int i;
  for (i = 0; i < nar->ndim; ++i) {
    rb_ary_push(ary, SSIZET2NUM(nar->shape[i]));
  }

  return ary;
}

#stridesObject



355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'ext/memory-view-test-helper/memory-view-test-helper.c', line 355

static VALUE
ndarray_get_strides(VALUE obj)
{
  ndarray_t *nar;
  TypedData_Get_Struct(obj, ndarray_t, &ndarray_data_type, nar);

  if (nar->strides == NULL) {
    return rb_ary_new_capa(0);
  }

  VALUE ary = rb_ary_new_capa(nar->ndim);
  int i;
  for (i = 0; i < nar->ndim; ++i) {
    rb_ary_push(ary, SSIZET2NUM(nar->strides[i]));
  }

  return ary;
}