CUDA Networks
Functions
matrix_subtract.cu File Reference

Implementation of the Matrix::subtract method for GPU-accelerated matrix subtraction. More...

#include "matrix.h"
#include <cuda_runtime.h>
#include <stdexcept>
#include <string>
Include dependency graph for matrix_subtract.cu:

Go to the source code of this file.

Functions

__global__ void matrixSubtractKernel (const double *a, const double *b, double *c, int rows, int cols)
 CUDA kernel for element-wise matrix subtraction. More...
 

Detailed Description

Implementation of the Matrix::subtract method for GPU-accelerated matrix subtraction.

Definition in file matrix_subtract.cu.

Function Documentation

◆ matrixSubtractKernel()

__global__ void matrixSubtractKernel ( const double *  a,
const double *  b,
double *  c,
int  rows,
int  cols 
)

CUDA kernel for element-wise matrix subtraction.

Parameters
aPointer to the first input matrix data.
bPointer to the second input matrix data.
cPointer to the output matrix data.
rowsNumber of rows in the matrices.
colsNumber of columns in the matrices.

Definition at line 19 of file matrix_subtract.cu.

19  {
20  // Calculate global thread indices
21  int row = blockIdx.y * blockDim.y + threadIdx.y;
22  int col = blockIdx.x * blockDim.x + threadIdx.x;
23 
24  // Check if thread is within matrix bounds
25  if (row < rows && col < cols) {
26  // Calculate index of current element
27  int index = row * cols + col;
28 
29  // Perform element-wise subtraction
30  c[index] = a[index] - b[index];
31  }
32 }