Module: Cumo::CUDA::Runtime

Defined in:
ext/cumo/cuda/runtime.c

Constant Summary collapse

CUDA_STREAM_DEFAULT =
UINT2NUM(cudaStreamDefault)
CUDA_STREAM_NON_BLOCKING =
UINT2NUM(cudaStreamNonBlocking)
CUDA_EVENT_DEFAULT =
UINT2NUM(cudaEventDefault)
CUDA_EVENT_BLOCKING_SYNC =
UINT2NUM(cudaEventBlockingSync)
CUDA_EVENT_DISABLE_TIMING =
UINT2NUM(cudaEventDisableTiming)
CUDA_HOST_ALLOC_DEFAULT =
UINT2NUM(cudaHostAllocDefault)
CUDA_HOST_ALLOC_PORTABLE =
UINT2NUM(cudaHostAllocPortable)
CUDA_HOST_ALLOC_MAPPED =
UINT2NUM(cudaHostAllocMapped)
CUDA_HOST_ALLOC_WRITE_COMBINED =
UINT2NUM(cudaHostAllocWriteCombined)

Class Method Summary collapse

Class Method Details

.cudaDeviceCanAccessPeer(device, peer_device) ⇒ Integer

Returns whether a device can directly access the memory of another.

Parameters:

  • device (Integer)

    Device from which the memory would be accessed.

  • peer_device (Integer)

    Device whose memory would be accessed.

Returns:

  • (Integer)

    1 if it can, 0 if it cannot

Raises:

See Also:



414
415
416
417
418
419
420
421
422
423
424
425
426
# File 'ext/cumo/cuda/runtime.c', line 414

static VALUE
rb_cudaDeviceCanAccessPeer(VALUE self, VALUE device, VALUE peer_device)
{
    int _device = NUM2INT(device);
    int _peer_device = NUM2INT(peer_device);
    int can_access = 0;
    cudaError_t status;

    status = cudaDeviceCanAccessPeer(&can_access, _device, _peer_device);

    check_status(status);
    return INT2NUM(can_access);
}

.cudaDeviceGetAttributes(attrib, device) ⇒ Integer

Returns information about the device.

Parameters:

  • attrib (Integer)

    Device attribute to query

  • device (Integer)

    Device number to query

Returns:

  • (Integer)

    Returned device attribute value

Raises:

See Also:



359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'ext/cumo/cuda/runtime.c', line 359

static VALUE
rb_cudaDeviceGetAttributes(VALUE self, VALUE attrib, VALUE device)
{
    int _attrib = NUM2INT(attrib);
    int _device = NUM2INT(device);
    int _ret;
    cudaError_t status;

    status = cudaDeviceGetAttribute(&_ret, _attrib, _device);

    check_status(status);
    return INT2NUM(_ret);
}

.cudaDeviceSynchronizeObject

Wait for compute device to finish.



838
839
840
841
842
843
# File 'ext/cumo/cuda/runtime.c', line 838

static VALUE
rb_cudaDeviceSynchronize(VALUE self)
{
    cumo_cuda_runtime_device_synchronize();
    return Qnil;
}

.cudaDriverGetVersionInteger

Returns the CUDA driver version.



303
304
305
306
307
308
309
310
311
312
313
# File 'ext/cumo/cuda/runtime.c', line 303

static VALUE
rb_cudaDriverGetVersion(VALUE self)
{
    int _version;
    cudaError_t status;

    status = cudaDriverGetVersion(&_version);

    check_status(status);
    return INT2NUM(_version);
}

.cudaEventCreateWithFlags(flags) ⇒ Integer

Creates an event.

Parameters:

  • flags (Integer)

    CUDA_EVENT_DEFAULT, CUDA_EVENT_BLOCKING_SYNC and CUDA_EVENT_DISABLE_TIMING, or'ed

Returns:

  • (Integer)

    the event handle

Raises:



519
520
521
522
523
524
525
526
# File 'ext/cumo/cuda/runtime.c', line 519

static VALUE
rb_cudaEventCreateWithFlags(VALUE self, VALUE flags)
{
    cudaEvent_t event;
    cumo_cuda_runtime_check_status(cudaEventCreateWithFlags(&event, NUM2UINT(flags)));
    cumo_cuda_handle_set_add(&events, (size_t)event);
    return SIZET2NUM((size_t)event);
}

.cudaEventDestroy(event) ⇒ Object

Destroys an event.

Parameters:

  • event (Integer)

Raises:



534
535
536
537
538
539
# File 'ext/cumo/cuda/runtime.c', line 534

static VALUE
rb_cudaEventDestroy(VALUE self, VALUE event)
{
    cumo_cuda_runtime_check_status(cudaEventDestroy((cudaEvent_t)cumo_cuda_handle_take(&events, event, "cudaEvent_t")));
    return Qnil;
}

.cudaEventElapsedTime(start, stop) ⇒ Float

Returns the milliseconds between two recorded events.

Parameters:

  • start (Integer)
  • stop (Integer)

Returns:

  • (Float)

    milliseconds

Raises:



588
589
590
591
592
593
594
# File 'ext/cumo/cuda/runtime.c', line 588

static VALUE
rb_cudaEventElapsedTime(VALUE self, VALUE start, VALUE stop)
{
    float ms = 0;
    cumo_cuda_runtime_check_status(cudaEventElapsedTime(&ms, event_get(start), event_get(stop)));
    return DBL2NUM((double)ms);
}

.cudaEventQuery(event) ⇒ Boolean

Returns whether an event has been reached.

Parameters:

  • event (Integer)

Returns:

  • (Boolean)


574
575
576
577
578
# File 'ext/cumo/cuda/runtime.c', line 574

static VALUE
rb_cudaEventQuery(VALUE self, VALUE event)
{
    return query_result(cudaEventQuery(event_get(event)));
}

.cudaEventRecord(event, stream) ⇒ Object

Records an event on a stream, after everything queued on it so far.

Parameters:

  • event (Integer)
  • stream (Integer)

    0 is the legacy default stream

Raises:



548
549
550
551
552
553
# File 'ext/cumo/cuda/runtime.c', line 548

static VALUE
rb_cudaEventRecord(VALUE self, VALUE event, VALUE stream)
{
    cumo_cuda_runtime_check_status(cudaEventRecord(event_get(event), cumo_cuda_stream_get(stream)));
    return Qnil;
}

.cudaEventSynchronize(event) ⇒ Object

Waits for an event to be reached.

Parameters:

  • event (Integer)

Raises:



561
562
563
564
565
566
# File 'ext/cumo/cuda/runtime.c', line 561

static VALUE
rb_cudaEventSynchronize(VALUE self, VALUE event)
{
    cumo_cuda_runtime_check_status(cudaEventSynchronize(event_get(event)));
    return Qnil;
}

.cudaFreeHost(buffer) ⇒ Object

Frees a pinned host buffer.

Parameters:

  • buffer (Integer)

Raises:



680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
# File 'ext/cumo/cuda/runtime.c', line 680

static VALUE
rb_cudaFreeHost(VALUE self, VALUE buffer)
{
    size_t handle = NUM2SIZET(buffer);
    st_data_t key = (st_data_t)handle;
    int found;
    rb_nativethread_lock_lock(&pinned.lock);
    found = st_delete(pinned.table, &key, 0);
    rb_nativethread_lock_unlock(&pinned.lock);
    if (!found) {
        rb_raise(rb_eArgError, "not a live pinned host buffer");
    }
    cumo_cuda_runtime_check_status(cudaFreeHost((void*)handle));
    return Qnil;
}

.cudaGetDeviceInteger

Returns which device is currently being used.

Returns:

  • (Integer)

    Returns the device on which the active host thread executes the device code.

Raises:

See Also:



344
345
346
347
348
# File 'ext/cumo/cuda/runtime.c', line 344

static VALUE
rb_cudaGetDevice(VALUE self)
{
    return INT2NUM(cumo_cuda_runtime_get_device());
}

.cudaGetDeviceCountInteger

Returns the number of compute-capable devices.

Returns:

  • (Integer)

    Returns the number of devices with compute capability greater or equal to 2.0

Raises:

See Also:



380
381
382
383
384
# File 'ext/cumo/cuda/runtime.c', line 380

static VALUE
rb_cudaGetDeviceCount(VALUE self)
{
    return INT2NUM(cumo_cuda_runtime_get_device_count());
}

.cudaHostAlloc(bytes, flags) ⇒ Integer

Allocates page-locked host memory, which a copy to or from the device can be asynchronous with.

Parameters:

  • bytes (Integer)
  • flags (Integer)

    CUDA_HOST_ALLOC_DEFAULT, PORTABLE, MAPPED and WRITE_COMBINED, or'ed

Returns:

  • (Integer)

    the buffer handle

Raises:



659
660
661
662
663
664
665
666
667
668
669
670
671
672
# File 'ext/cumo/cuda/runtime.c', line 659

static VALUE
rb_cudaHostAlloc(VALUE self, VALUE bytes, VALUE flags)
{
    size_t _bytes = NUM2SIZET(bytes);
    void *ptr = NULL;
    if (_bytes == 0) {
        rb_raise(rb_eArgError, "a pinned host buffer has at least one byte");
    }
    cumo_cuda_runtime_check_status(cudaHostAlloc(&ptr, _bytes, NUM2UINT(flags)));
    rb_nativethread_lock_lock(&pinned.lock);
    st_insert(pinned.table, (st_data_t)ptr, (st_data_t)_bytes);
    rb_nativethread_lock_unlock(&pinned.lock);
    return SIZET2NUM((size_t)ptr);
}

.cudaRuntimeGetVersionInteger

Returns the CUDA Runtime version.



321
322
323
324
325
326
327
328
329
330
331
# File 'ext/cumo/cuda/runtime.c', line 321

static VALUE
rb_cudaRuntimeGetVersion(VALUE self)
{
    int _version;
    cudaError_t status;

    status = cudaRuntimeGetVersion(&_version);

    check_status(status);
    return INT2NUM(_version);
}

.cudaSetDevice(device) ⇒ Object

Set device to be used for GPU executions.

Parameters:

  • device (Integer)

    Device on which the active host thread should execute the device code.

Raises:

See Also:



393
394
395
396
397
398
399
400
401
402
403
# File 'ext/cumo/cuda/runtime.c', line 393

static VALUE
rb_cudaSetDevice(VALUE self, VALUE device)
{
    int _device = NUM2INT(device);
    cudaError_t status;

    status = cudaSetDevice(_device);

    check_status(status);
    return Qnil;
}

.cudaStreamCreateWithFlags(flags) ⇒ Integer

Creates a stream. cudaStreamNonBlocking makes one that does not wait for the legacy stream 0.

Parameters:

  • flags (Integer)

    CUDA_STREAM_DEFAULT or CUDA_STREAM_NON_BLOCKING

Returns:

  • (Integer)

    the stream handle

Raises:



436
437
438
439
440
441
442
443
# File 'ext/cumo/cuda/runtime.c', line 436

static VALUE
rb_cudaStreamCreateWithFlags(VALUE self, VALUE flags)
{
    cudaStream_t stream;
    cumo_cuda_runtime_check_status(cudaStreamCreateWithFlags(&stream, NUM2UINT(flags)));
    cumo_cuda_handle_set_add(&streams, (size_t)stream);
    return SIZET2NUM((size_t)stream);
}

.cudaStreamDestroy(stream) ⇒ Object

Destroys a stream. A stream that is current in any thread cannot be destroyed.

Parameters:

  • stream (Integer)

Raises:



452
453
454
455
456
457
458
459
460
# File 'ext/cumo/cuda/runtime.c', line 452

static VALUE
rb_cudaStreamDestroy(VALUE self, VALUE stream)
{
    if (in_use_p((cudaStream_t)NUM2SIZET(stream))) {
        rb_raise(rb_eArgError, "a stream that is current in a thread cannot be destroyed");
    }
    cumo_cuda_runtime_check_status(cudaStreamDestroy((cudaStream_t)cumo_cuda_handle_take(&streams, stream, "cudaStream_t")));
    return Qnil;
}

.cudaStreamQuery(stream) ⇒ Boolean

Returns whether everything queued on a stream has finished.

Parameters:

  • stream (Integer)

Returns:

  • (Boolean)


492
493
494
495
496
# File 'ext/cumo/cuda/runtime.c', line 492

static VALUE
rb_cudaStreamQuery(VALUE self, VALUE stream)
{
    return query_result(cudaStreamQuery(cumo_cuda_stream_get(stream)));
}

.cudaStreamSynchronize(stream) ⇒ Object

Waits for everything queued on a stream. 0 is the legacy default stream.

Parameters:

  • stream (Integer)

Raises:



602
603
604
605
606
607
# File 'ext/cumo/cuda/runtime.c', line 602

static VALUE
rb_cudaStreamSynchronize(VALUE self, VALUE stream)
{
    cumo_cuda_runtime_check_status(cudaStreamSynchronize(cumo_cuda_stream_get(stream)));
    return Qnil;
}

.cudaStreamWaitEvent(stream, event) ⇒ Object

Makes everything queued on a stream after this call wait for an event.

Parameters:

  • stream (Integer)
  • event (Integer)

Raises:



505
506
507
508
509
510
# File 'ext/cumo/cuda/runtime.c', line 505

static VALUE
rb_cudaStreamWaitEvent(VALUE self, VALUE stream, VALUE event)
{
    cumo_cuda_runtime_check_status(cudaStreamWaitEvent(cumo_cuda_stream_get(stream), event_get(event), 0));
    return Qnil;
}

.current_streamInteger

Returns the stream Cumo launches its kernels and copies on in this thread. 0 until set.

Returns:

  • (Integer)


615
616
617
618
619
# File 'ext/cumo/cuda/runtime.c', line 615

static VALUE
rb_current_stream(VALUE self)
{
    return SIZET2NUM((size_t)current_stream);
}

.current_stream=(stream) ⇒ Integer

Sets the stream Cumo launches its kernels and copies on in this thread.

Parameters:

  • stream (Integer)

    a stream this process created, or 0

Returns:

  • (Integer)

    the stream



627
628
629
630
631
632
# File 'ext/cumo/cuda/runtime.c', line 627

static VALUE
rb_current_stream_set(VALUE self, VALUE stream)
{
    cumo_cuda_stream_set(cumo_cuda_stream_get(stream));
    return stream;
}

.memcpy_narray_to_pinned(narray, buffer, stream) ⇒ Object

Copies an NArray into a pinned host buffer, asynchronously on a stream.

Parameters:

  • narray (Cumo::NArray)

    contiguous, and of the buffer's size

  • buffer (Integer)
  • stream (Integer, nil)

    nil for the current stream

Raises:



822
823
824
825
826
827
828
829
830
# File 'ext/cumo/cuda/runtime.c', line 822

static VALUE
rb_memcpy_narray_to_pinned(VALUE self, VALUE narray, VALUE buffer, VALUE stream)
{
    size_t size;
    char *dst = pinned_get(buffer, &size);
    char *src = pinned_narray_pointer(narray, size, 0);
    cumo_cuda_runtime_check_status(cudaMemcpyAsync(dst, src, size, cudaMemcpyDeviceToHost, stream_or_current(stream)));
    return Qnil;
}

.memcpy_pinned_to_narray(buffer, narray, stream) ⇒ Object

Copies a pinned host buffer into an NArray, asynchronously on a stream.

Parameters:

  • buffer (Integer)
  • narray (Cumo::NArray)

    contiguous, and of the buffer's size

  • stream (Integer, nil)

    nil for the current stream

Raises:



803
804
805
806
807
808
809
810
811
812
# File 'ext/cumo/cuda/runtime.c', line 803

static VALUE
rb_memcpy_pinned_to_narray(VALUE self, VALUE buffer, VALUE narray, VALUE stream)
{
    size_t size;
    char *src = pinned_get(buffer, &size);
    char *dst = pinned_narray_pointer(narray, size, 1);
    cumo_cuda_runtime_check_status(cudaMemcpyAsync(dst, src, size, cudaMemcpyHostToDevice, stream_or_current(stream)));
    cumo_cuda_runtime_note_device_write();
    return Qnil;
}

.pinned_read(buffer, offset, length) ⇒ String

Reads bytes out of a pinned host buffer.

Parameters:

  • buffer (Integer)
  • offset (Integer)
  • length (Integer)

Returns:

  • (String)


726
727
728
729
730
731
732
733
# File 'ext/cumo/cuda/runtime.c', line 726

static VALUE
rb_pinned_read(VALUE self, VALUE buffer, VALUE offset, VALUE length)
{
    size_t size, off = NUM2SIZET(offset), len = NUM2SIZET(length);
    char *ptr = pinned_get(buffer, &size);
    pinned_range(size, off, len);
    return rb_str_new(ptr + off, (long)len);
}

.pinned_size(buffer) ⇒ Integer

Returns the size of a pinned host buffer in bytes.

Parameters:

  • buffer (Integer)

Returns:

  • (Integer)


702
703
704
705
706
707
708
# File 'ext/cumo/cuda/runtime.c', line 702

static VALUE
rb_pinned_size(VALUE self, VALUE buffer)
{
    size_t size;
    pinned_get(buffer, &size);
    return SIZET2NUM(size);
}

.pinned_write(buffer, offset, bytes) ⇒ Integer

Writes a String into a pinned host buffer.

Parameters:

  • buffer (Integer)
  • offset (Integer)
  • bytes (String)

Returns:

  • (Integer)

    the number of bytes written



743
744
745
746
747
748
749
750
751
752
753
754
755
# File 'ext/cumo/cuda/runtime.c', line 743

static VALUE
rb_pinned_write(VALUE self, VALUE buffer, VALUE offset, VALUE bytes)
{
    size_t size, off = NUM2SIZET(offset), len;
    char *ptr;
    StringValue(bytes);
    ptr = pinned_get(buffer, &size);
    len = (size_t)RSTRING_LEN(bytes);
    pinned_range(size, off, len);
    memcpy(ptr + off, RSTRING_PTR(bytes), len);
    RB_GC_GUARD(bytes);
    return SIZET2NUM(len);
}