call_binary

Function call_binary 

Source
pub fn call_binary(
    kernel_name: Kernel,
    lhs: *const c_void,
    rhs: *const c_void,
    output: *mut c_void,
    metadata: &[usize],
) -> Result<()>
Expand description

Execute a binary operation on two tensors

Performs element-wise binary operations on tensors with arbitrary shapes and strides. The function automatically handles contiguous and strided memory layouts for optimal performance. Broadcasting is not handled here - shapes must be compatible.

§Arguments

  • kernel_name - The binary operation to perform (e.g., add::F32, mul::I32)
  • lhs - Pointer to left-hand side tensor data
  • rhs - Pointer to right-hand side tensor data
  • output - Pointer to output tensor buffer
  • metadata - Tensor metadata array (see layout below)

§Metadata layout

  • metadata[0]: num_els (total number of elements to process)
  • metadata[1]: num_dims (number of dimensions)
  • metadata[2..2+num_dims]: lhs_shape (shape of left tensor)
  • metadata[2+num_dims..2+2*num_dims]: rhs_shape (shape of right tensor)
  • metadata[2+2num_dims..2+3num_dims]: lhs_strides (stride of left tensor)
  • metadata[2+3num_dims..2+4num_dims]: rhs_strides (stride of right tensor)
  • metadata[2+4*num_dims]: lhs_offset (starting offset in left tensor)
  • metadata[2+4*num_dims+1]: rhs_offset (starting offset in right tensor)

§Safety

This function uses unsafe FFI calls to C kernels. Caller must ensure:

  • Pointers are valid and properly aligned
  • Metadata accurately describes tensor layout
  • Output buffer has sufficient capacity

§Returns

Returns Ok(()) on success. Currently does not propagate C kernel errors.