CUDA Networks
Functions
vector_subtract_scalar.cu File Reference

Implementation of the Vector::subtract_scalar method for GPU-accelerated subtraction of a scalar from a vector. More...

#include "vector.h"
#include <cuda_runtime.h>
#include <stdexcept>
#include <cfloat>
#include <cmath>
Include dependency graph for vector_subtract_scalar.cu:

Go to the source code of this file.

Functions

__global__ void vectorSubtractScalarKernel (double *data, double scalar, int size)
 CUDA kernel for subtracting a scalar from vector elements. More...
 

Detailed Description

Implementation of the Vector::subtract_scalar method for GPU-accelerated subtraction of a scalar from a vector.

Definition in file vector_subtract_scalar.cu.

Function Documentation

◆ vectorSubtractScalarKernel()

__global__ void vectorSubtractScalarKernel ( double *  data,
double  scalar,
int  size 
)

CUDA kernel for subtracting a scalar from vector elements.

Parameters
dataPointer to the vector data.
scalarThe scalar to subtract.
sizeTotal number of elements in the vector.

Definition at line 18 of file vector_subtract_scalar.cu.

18  {
19  // Calculate global thread index
20  int idx = blockIdx.x * blockDim.x + threadIdx.x;
21 
22  // Check if thread is within vector bounds
23  if (idx < size) {
24  // Perform subtraction
25  double result = data[idx] - scalar;
26 
27  // Handle underflow
28  if (!isfinite(result)) {
29  result = (result > 0.0) ? DBL_MAX : -DBL_MAX;
30  }
31 
32  // Store the result
33  data[idx] = result;
34  }
35 }