CUDA Networks
Functions
matrix_divide_scalar.cu File Reference

Implementation of the Matrix::divide_scalar method for GPU-accelerated division of a matrix by a scalar. More...

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

Go to the source code of this file.

Functions

__global__ void divideScalarKernel (double *data, double scalar, int size)
 CUDA kernel for dividing matrix elements by a scalar. More...
 

Detailed Description

Implementation of the Matrix::divide_scalar method for GPU-accelerated division of a matrix by a scalar.

Definition in file matrix_divide_scalar.cu.

Function Documentation

◆ divideScalarKernel()

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

CUDA kernel for dividing matrix elements by a scalar.

Parameters
dataPointer to the matrix data.
scalarThe scalar to divide by.
sizeTotal number of elements in the matrix.

Definition at line 18 of file matrix_divide_scalar.cu.

18  {
19  // Calculate global thread index
20  int idx = blockIdx.x * blockDim.x + threadIdx.x;
21 
22  // Check if thread is within matrix bounds
23  if (idx < size) {
24  // Handle division by very small numbers
25  if (fabs(scalar) < DBL_EPSILON) {
26  // If data is zero, keep it zero
27  // Otherwise, set to max or min based on sign
28  data[idx] = (data[idx] == 0.0) ? 0.0 : ((data[idx] > 0.0) ? DBL_MAX : -DBL_MAX);
29  }
30  // Handle very large numbers
31  else if (fabs(data[idx]) > DBL_MAX / 2) {
32  // Preserve sign and set to max value
33  data[idx] = (data[idx] > 0.0) ? DBL_MAX : -DBL_MAX;
34  }
35  // Regular division for normal cases
36  else {
37  data[idx] /= scalar;
38  }
39  }
40 }