CUDA Networks
Functions
matrix_add_vector.cu File Reference

Implementation of the Matrix::add_vector method for GPU-accelerated addition of a vector to each column of a matrix. More...

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

Go to the source code of this file.

Functions

__global__ void addVectorToMatrixKernel (double *m, const double *v, int rows, int cols)
 CUDA kernel for adding a vector to each column of a matrix. More...
 

Detailed Description

Implementation of the Matrix::add_vector method for GPU-accelerated addition of a vector to each column of a matrix.

Definition in file matrix_add_vector.cu.

Function Documentation

◆ addVectorToMatrixKernel()

__global__ void addVectorToMatrixKernel ( double *  m,
const double *  v,
int  rows,
int  cols 
)

CUDA kernel for adding a vector to each column of a matrix.

Parameters
mPointer to the matrix data.
vPointer to the vector data.
rowsNumber of rows in the matrix.
colsNumber of columns in the matrix.

Definition at line 19 of file matrix_add_vector.cu.

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