CUDA Networks
matrix_sum.cu
Go to the documentation of this file.
1 /**
2  * @file matrix_sum.cu
3  * @brief Implementation of the Matrix::sum method for GPU-accelerated summing of all elements in a matrix.
4  */
5 
6 #include "matrix.h"
7 #include <cuda_runtime.h>
8 #include <thrust/device_ptr.h>
9 #include <thrust/reduce.h>
10 
11 /**
12  * @brief Sums all elements in the matrix.
13  * @return The sum of all elements in the matrix.
14  */
15 double Matrix::sum() const {
16  // Create a thrust device pointer from the raw CUDA pointer
17  thrust::device_ptr<double> d_ptr(d_data);
18 
19  // Use thrust::reduce to sum all elements
20  double result = thrust::reduce(d_ptr, d_ptr + rows * cols);
21 
22  return result;
23 }
double sum() const
Sums all elements in the matrix.
Definition: matrix_sum.cu:15
Defines the Matrix class for GPU-accelerated matrix operations.