Class: Cumo::CUDA::ElementwiseKernel

Inherits:
Object
  • Object
show all
Includes:
UserKernel
Defined in:
lib/cumo/cuda/elementwise_kernel.rb

Overview

A kernel that applies one piece of CUDA C to every element, the way CuPy's ElementwiseKernel does.

squared_diff = Cumo::CUDA::ElementwiseKernel.new(
"float32 x, float32 y", "float32 z", "z = (x - y) * (x - y)", "squared_diff")
squared_diff.call(x, y)

A one-letter type is a placeholder that takes the dtype of the argument it is used with. Array arguments are broadcast against each other, a Ruby number goes as a scalar, and an argument marked raw is handed over as a pointer for the operation to index itself, with i the element index and _ind.size() the element count.

Constant Summary collapse

BLOCK =
256
MAX_GRID =
65_535

Constants included from UserKernel

UserKernel::CTYPE, UserKernel::INT_RANGE, UserKernel::PACK, UserKernel::RESERVED, UserKernel::TYPES

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(in_params, out_params, operation, name, preamble: "") ⇒ ElementwiseKernel

Returns a new instance of ElementwiseKernel.



26
27
28
29
30
31
32
33
34
# File 'lib/cumo/cuda/elementwise_kernel.rb', line 26

def initialize(in_params, out_params, operation, name, preamble: "")
  check_name(name)
  @in_params = parse_params(in_params)
  @out_params = parse_params(out_params)
  @operation = operation
  @name = name
  @preamble = preamble
  @functions = {}
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



24
25
26
# File 'lib/cumo/cuda/elementwise_kernel.rb', line 24

def name
  @name
end

Instance Method Details

#call(*args, size: nil) ⇒ Object

Applies the kernel and answers the output, or the outputs as an Array when there are several. Outputs may be given after the inputs. size: is the element count when no argument decides it.



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cumo/cuda/elementwise_kernel.rb', line 39

def call(*args, size: nil)
  ins, outs = split_args(args, @in_params, @out_params)
  types = resolve_types(@in_params, ins, @out_params, outs)
  shape = broadcast_shape(ins, outs, size)
  outs = @out_params.each_with_index.map { |p, k| outs[k] || types[p.type].new(*shape) }
  check_outputs(outs, @out_params, shape)

  n = shape.inject(1, :*)
  launch(ins, outs, types, shape, n) if n > 0
  @out_params.size == 1 ? outs[0] : outs
end