CUDA Networks
Functions
matrix_multiply_scalar.cu File Reference

Implementation of the Matrix::multiply_scalar method for GPU-accelerated multiplication 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_multiply_scalar.cu:

Go to the source code of this file.

Functions

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

Detailed Description

Implementation of the Matrix::multiply_scalar method for GPU-accelerated multiplication of a matrix by a scalar.

Definition in file matrix_multiply_scalar.cu.

Function Documentation

◆ multiplyScalarKernel()

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

CUDA kernel for multiplying matrix elements by a scalar.

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

Definition at line 18 of file matrix_multiply_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  // Perform multiplication
25  double result = data[idx] * scalar;
26 
27  // Handle overflow
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 }