CUDA Networks
matrix.h
Go to the documentation of this file.
1 /**
2  * @file matrix.h
3  * @brief Defines the Matrix class for GPU-accelerated matrix operations.
4  */
5 
6 #ifndef MATRIX_H
7 #define MATRIX_H
8 
9 #include <stdexcept>
10 #include <string>
11 
12 #include "vector.h"
13 
14 /**
15  * @class Matrix
16  * @brief Represents a matrix with GPU-accelerated operations.
17  */
18 class Matrix {
19 public:
20  /**
21  * @brief Construct a new Matrix object
22  * @param rows Number of rows in the matrix
23  * @param cols Number of columns in the matrix
24  */
25  Matrix(int rows, int cols);
26 
27  /**
28  * @brief Copy constructor
29  * @param other The matrix to copy from
30  */
31  Matrix(const Matrix& other);
32 
33  /**
34  * @brief Move constructor
35  * @param other The matrix to move from
36  */
37  Matrix(Matrix&& other) noexcept;
38 
39  /**
40  * @brief Copy assignment operator
41  * @param other The matrix to copy from
42  * @return Reference to this matrix
43  */
44  Matrix& operator=(const Matrix& other);
45 
46  /**
47  * @brief Move assignment operator
48  * @param other The matrix to move from
49  * @return Reference to this matrix
50  */
51  Matrix& operator=(Matrix&& other) noexcept;
52 
53  /**
54  * @brief Destroy the Matrix object
55  */
56  ~Matrix();
57 
58  /**
59  * @brief Initialize the matrix (typically sets all elements to zero)
60  */
61  void initialize();
62 
63  /**
64  * @brief Randomize the matrix elements with values between -0.5 and 0.5
65  */
66  void randomize();
67 
68  /**
69  * @brief Print the matrix contents
70  * @param decimals Number of decimal places to display
71  */
72  void print(int decimals);
73 
74  /**
75  * @brief Get the number of rows in the matrix
76  * @return int Number of rows
77  */
78  int get_rows() const;
79 
80  /**
81  * @brief Get the number of columns in the matrix
82  * @return int Number of columns
83  */
84  int get_cols() const;
85 
86  /**
87  * @brief Get the raw data pointer of the matrix
88  * @return double* Pointer to the matrix data on the device
89  */
90  double* get_data() const;
91 
92  /**
93  * @brief Read data from a CSV file into the matrix
94  * @param filename Path to the CSV file
95  */
96  void read_csv(const char* filename);
97 
98  /**
99  * @brief Read a subset of data from a CSV file into the matrix
100  * @param filename Path to the CSV file
101  * @param startRow Starting row to read from the CSV file (0-based index)
102  * @param endRow Ending row to read from the CSV file (exclusive)
103  * @param fileRows Total number of rows in the CSV file
104  * @param fileCols Total number of columns in the CSV file
105  */
106  void read_csv_limited(const char* filename,
107  int startRow,
108  int endRow,
109  int fileRows,
110  int fileCols);
111 
112  /**
113  * @brief Preview a single image from the matrix
114  * @param row_index Index of the row containing the image data
115  * @param image_size_x Number of rows in the image
116  * @param image_size_y Number of columns in the image
117  */
118  void preview_image(int row_index, int image_size_x, int image_size_y) const;
119 
120  /**
121  * @brief Applies the ReLU activation function to the matrix.
122  * @return A new Matrix object with ReLU applied.
123  */
124  Matrix relu() const;
125 
126  /**
127  * @brief Applies the derivative of the ReLU activation function to the matrix.
128  * @return A new Matrix object with ReLU derivative applied.
129  */
130  Matrix relu_derivative() const;
131 
132  /**
133  * @brief Applies the sigmoid activation function to the matrix.
134  * @return A new Matrix object with sigmoid applied.
135  */
136  Matrix sigmoid() const;
137 
138  /**
139  * @brief Applies the derivative of the sigmoid activation function to the matrix.
140  * @return A new Matrix object with sigmoid derivative applied.
141  */
142  Matrix sigmoid_derivative() const;
143 
144  /**
145  * @brief Applies the softmax function to the matrix column-wise.
146  * @return A new Matrix object with softmax applied.
147  */
148  Matrix softmax() const;
149 
150  /**
151  * @brief Creates a deep copy of the matrix.
152  * @return A new Matrix object with the same content as the original.
153  */
154  Matrix copy() const;
155 
156  /**
157  * @brief Multiplies this matrix with another matrix.
158  * @param other The matrix to multiply with.
159  * @return A new Matrix object containing the result of the multiplication.
160  * @throws std::invalid_argument if matrix dimensions are incompatible for multiplication.
161  */
162  Matrix multiply(const Matrix& other) const;
163 
164  /**
165  * @brief Performs element-wise multiplication with another matrix.
166  * @param other The matrix to multiply element-wise with.
167  * @return A new Matrix object containing the result of the element-wise multiplication.
168  * @throws std::invalid_argument if matrix dimensions are not identical.
169  */
170  Matrix multiply_elementwise(const Matrix& other) const;
171 
172  /**
173  * @brief Adds a vector to each column of the matrix.
174  * @param v The vector to add.
175  * @throws std::invalid_argument if vector dimension doesn't match matrix rows.
176  */
177  void add_vector(const Vector& v);
178 
179  /**
180  * @brief Subtracts another matrix from this matrix.
181  * @param other The matrix to subtract.
182  * @return A new Matrix object containing the result of the subtraction.
183  * @throws std::invalid_argument if matrix dimensions are not identical.
184  */
185  Matrix subtract(const Matrix& other) const;
186 
187  /**
188  * @brief Sums all elements in the matrix.
189  * @return The sum of all elements in the matrix.
190  */
191  double sum() const;
192 
193  /**
194  * @brief Divides all elements in the matrix by a scalar.
195  * @param scalar The scalar to divide by.
196  * @throws std::invalid_argument if scalar is zero.
197  */
198  void divide_scalar(double scalar);
199 
200  /**
201  * @brief Multiplies all elements in the matrix by a scalar.
202  * @param scalar The scalar to multiply by.
203  */
204  void multiply_scalar(double scalar);
205 
206  /**
207  * @brief Computes the argmax of each column in the matrix.
208  * @return A Vector containing the row indices of the maximum values for each column.
209  */
210  Vector argmax() const;
211 
212  /**
213  * @brief Transposes the matrix and returns a new Matrix object.
214  * @return A new Matrix object containing the transposed data.
215  */
216  Matrix transpose() const;
217 
218  /**
219  * @brief Selects a subset of the matrix based on specified row and column ranges.
220  * @param start_row Starting row index (inclusive).
221  * @param end_row Ending row index (exclusive).
222  * @param start_col Starting column index (inclusive).
223  * @param end_col Ending column index (exclusive).
224  * @return A new Matrix object containing the selected subset.
225  * @throws std::out_of_range if the specified ranges are invalid.
226  */
227  Matrix select_batch(int start_row, int end_row, int start_col, int end_col) const;
228 
229 private:
230  int rows; ///< Number of rows in the matrix
231  int cols; ///< Number of columns in the matrix
232  double* d_data; ///< Device data pointer
233 };
234 
235 #endif // MATRIX_H
Represents a matrix with GPU-accelerated operations.
Definition: matrix.h:18
int get_rows() const
Get the number of rows in the matrix.
void divide_scalar(double scalar)
Divides all elements in the matrix by a scalar.
void read_csv(const char *filename)
Read data from a CSV file into the matrix.
Matrix sigmoid_derivative() const
Applies the derivative of the sigmoid activation function to the matrix.
void add_vector(const Vector &v)
Adds a vector to each column of the matrix.
Matrix sigmoid() const
Applies the sigmoid activation function to the matrix.
double * get_data() const
Get the raw data pointer of the matrix.
int get_cols() const
Get the number of columns in the matrix.
Matrix & operator=(const Matrix &other)
Copy assignment operator.
Definition: matrix_copy.cu:16
Matrix multiply_elementwise(const Matrix &other) const
Performs element-wise multiplication with another matrix.
void print(int decimals)
Print the matrix contents.
Definition: matrix_print.cu:11
Vector argmax() const
Computes the argmax of each column in the matrix.
Matrix(int rows, int cols)
Construct a new Matrix object.
Matrix transpose() const
Transposes the matrix and returns a new Matrix object.
Matrix copy() const
Creates a deep copy of the matrix.
Definition: matrix_copy.cu:59
Matrix subtract(const Matrix &other) const
Subtracts another matrix from this matrix.
void read_csv_limited(const char *filename, int startRow, int endRow, int fileRows, int fileCols)
Read a subset of data from a CSV file into the matrix.
void initialize()
Initialize the matrix (typically sets all elements to zero)
~Matrix()
Destroy the Matrix object.
Matrix select_batch(int start_row, int end_row, int start_col, int end_col) const
Selects a subset of the matrix based on specified row and column ranges.
Matrix multiply(const Matrix &other) const
Multiplies this matrix with another matrix.
Matrix relu_derivative() const
Applies the derivative of the ReLU activation function to the matrix.
Matrix relu() const
Applies the ReLU activation function to the matrix.
Definition: matrix_relu.cu:30
void randomize()
Randomize the matrix elements with values between -0.5 and 0.5.
double sum() const
Sums all elements in the matrix.
Definition: matrix_sum.cu:15
void multiply_scalar(double scalar)
Multiplies all elements in the matrix by a scalar.
Matrix softmax() const
Applies the softmax function to the matrix column-wise.
void preview_image(int row_index, int image_size_x, int image_size_y) const
Preview a single image from the matrix.
Represents a vector with GPU-accelerated operations.
Definition: vector.h:13
Defines the Vector class for GPU-accelerated vector operations.