17. Dot Product medium

Implement a GPU program that computes the dot product of two vectors containing 32-bit floating point numbers. The dot product is the sum of the products of the corresponding elements of two vectors.

Mathematically, the dot product of two vectors and of length is defined as:

Implementation Requirements

  • Use only GPU native features (external libraries are not permitted)
  • The solve function signature must remain unchanged
  • The final result must be stored in the output variable

Example 1:

Input:  A = [1.0, 2.0, 3.0, 4.0]
               B = [5.0, 6.0, 7.0, 8.0]
       Output: result = 70.0  (1.0*5.0 + 2.0*6.0 + 3.0*7.0 + 4.0*8.0)

Example 2:

Input:  A = [0.5, 1.5, 2.5]
               B = [2.0, 3.0, 4.0]
       Output: result = 15.5  (0.5*2.0 + 1.5*3.0 + 2.5*4.0)

Constraints

  • A and B have identical lengths
  • 1 ≤ N ≤ 100,000,000
  • Performance is measured with N = 4,194,304

Starter code (CUDA)

#include <cuda_runtime.h>

// A, B, result are device pointers
extern "C" void solve(const float* A, const float* B, float* result, int N) {}

Solve it on your GPU

From LeetGPU, CC BY-NC-ND 4.0.