TensorTonic
Problems
Vector Subtraction as a CUDA Kernel
GPU ProgrammingLinear Algebra
Easy

Implement element-wise subtraction of two vectors with a CUDA kernel.

C[i]=A[i]−B[i]C[i] = A[i] - B[i]C[i]=A[i]−B[i]

A is the first input vector, B is the vector subtracted from it, C is the output vector, N is their common length, and i is an integer index from zero through N minus one.

Subtract the second vector from the first at each index; reversing the operands changes the result.

The buffers are contiguous arrays of 32-bit floats in device memory. Write all N results into the preallocated C buffer; the solve function returns no value.

Loading visualization...

Examples

Input: A = [1,2,3,4], B = [5,6,7,8]

Output: [-4,-4,-4,-4]

Explanation: At every index, subtracting the second input value from the first gives negative four.

Input: A = [1.5,2.5,3.5], B = [0.5,0.5,0.5]

Output: [1,2,3]

Hint 1

Use blockIdx.x, blockDim.x, and threadIdx.x to identify a thread's global element index.

Hint 2

Check both operand order and whether the index lies inside the vector.

Requirements

  • Compute the specified operation independently at every valid index and store the result in C.
  • Leave input buffers unchanged and do not access memory outside the supplied buffers.
  • Preserve the supplied CUDA C++ function signatures and host launch wrapper.
  • Do not use external libraries.

Constraints

  • N is an integer from 1 through 10,000,000.
  • A, B, and C each contain N contiguous 32-bit floating-point elements in device memory.
  • Input values are finite and representable as 32-bit floats.
Try Similar Problems
Vector Addition as a CUDA KernelEasyOuter Product as a CUDA KernelEasyMatrix Addition as a CUDA KernelEasyHadamard Product as a CUDA KernelEasyMatrix Transpose as a CUDA KernelEasyImplement Swish (SiLU) as a CUDA KernelEasyReLU as a CUDA KernelEasyImplement Leaky ReLU as a CUDA KernelEasyImplement Sigmoid as a CUDA KernelEasyImplement Exact GELU as a CUDA KernelEasyTanh as a CUDA KernelEasyRMS Normalization as a CUDA KernelMedium

Accepts: array

Accepts: array

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Vector Subtraction as a CUDA Kernel

Vector Subtraction as a CUDA Kernel

GPU ProgrammingLinear Algebra
Easy

Implement element-wise subtraction of two vectors with a CUDA kernel.

C[i]=A[i]−B[i]C[i] = A[i] - B[i]C[i]=A[i]−B[i]

A is the first input vector, B is the vector subtracted from it, C is the output vector, N is their common length, and i is an integer index from zero through N minus one.

Subtract the second vector from the first at each index; reversing the operands changes the result.

The buffers are contiguous arrays of 32-bit floats in device memory. Write all N results into the preallocated C buffer; the solve function returns no value.

Loading visualization...

Examples

Input: A = [1,2,3,4], B = [5,6,7,8]

Output: [-4,-4,-4,-4]

Explanation: At every index, subtracting the second input value from the first gives negative four.

Input: A = [1.5,2.5,3.5], B = [0.5,0.5,0.5]

Output: [1,2,3]

Hint 1

Use blockIdx.x, blockDim.x, and threadIdx.x to identify a thread's global element index.

Hint 2

Check both operand order and whether the index lies inside the vector.

Requirements

  • Compute the specified operation independently at every valid index and store the result in C.
  • Leave input buffers unchanged and do not access memory outside the supplied buffers.
  • Preserve the supplied CUDA C++ function signatures and host launch wrapper.
  • Do not use external libraries.

Constraints

  • N is an integer from 1 through 10,000,000.
  • A, B, and C each contain N contiguous 32-bit floating-point elements in device memory.
  • Input values are finite and representable as 32-bit floats.
Try Similar Problems
Vector Addition as a CUDA KernelEasyOuter Product as a CUDA KernelEasyMatrix Addition as a CUDA KernelEasyHadamard Product as a CUDA KernelEasyMatrix Transpose as a CUDA KernelEasyImplement Swish (SiLU) as a CUDA KernelEasyReLU as a CUDA KernelEasyImplement Leaky ReLU as a CUDA KernelEasyImplement Sigmoid as a CUDA KernelEasyImplement Exact GELU as a CUDA KernelEasyTanh as a CUDA KernelEasyRMS Normalization as a CUDA KernelMedium

Accepts: array

Accepts: array

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Problems
Loading...
1 / 1