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