CUDA Networks
matrix_sigmoid_derivative.cu
Go to the documentation of this file.
1 /**
2  * @file matrix_sigmoid_derivative.cu
3  * @brief Implementation of the sigmoid derivative function for matrices.
4  */
5 
6 #include "matrix.h"
7 #include <cuda_runtime.h>
8 
9 /**
10  * @brief CUDA kernel for applying the sigmoid derivative function element-wise.
11  * @param input Pointer to the input matrix data.
12  * @param output Pointer to the output matrix data.
13  * @param size Total number of elements in the matrix.
14  */
15 __global__ void sigmoidDerivativeKernel(const double* input, double* output, int size) {
16  // Calculate the global thread index
17  int idx = blockIdx.x * blockDim.x + threadIdx.x;
18 
19  // Check if the thread is within the matrix bounds
20  if (idx < size) {
21  // Compute sigmoid activation
22  double sigmoid_x = 1.0 / (1.0 + exp(-input[idx]));
23 
24  // Apply sigmoid derivative: sigmoid(x) * (1 - sigmoid(x))
25  output[idx] = sigmoid_x * (1.0 - sigmoid_x);
26  }
27 }
28 
29 /**
30  * @brief Applies the sigmoid derivative function to the matrix.
31  * @return A new Matrix object with sigmoid derivative applied.
32  */
34  // Create a new matrix with the same dimensions
35  Matrix result(rows, cols);
36 
37  // Calculate the total number of elements
38  int size = rows * cols;
39 
40  // Define the number of threads per block
41  int threadsPerBlock = 256;
42 
43  // Calculate the number of blocks needed
44  int blocksPerGrid = (size + threadsPerBlock - 1) / threadsPerBlock;
45 
46  // Launch the CUDA kernel
47  sigmoidDerivativeKernel<<<blocksPerGrid, threadsPerBlock>>>(d_data, result.d_data, size);
48 
49  // Synchronize to ensure the kernel execution is complete
50  cudaDeviceSynchronize();
51 
52  return result;
53 }
Represents a matrix with GPU-accelerated operations.
Definition: matrix.h:18
Matrix sigmoid_derivative() const
Applies the derivative of the sigmoid activation function to the matrix.
Defines the Matrix class for GPU-accelerated matrix operations.
__global__ void sigmoidDerivativeKernel(const double *input, double *output, int size)
CUDA kernel for applying the sigmoid derivative function element-wise.