-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcu_mat.hcu
431 lines (294 loc) · 24.1 KB
/
cu_mat.hcu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/*
* cu_mat.hcu
*
* Created on: Jun 5, 2019
* Author: vatsal
* Header file for cuda-matrix library.
* Complete library can be found at /~https://github.com/vxj9800/cuda-matrix-lib
*/
#ifndef CU_MAT_HCU_
#define CU_MAT_HCU_
#include "imp_includes.hcu"
class cu_mat {
protected:
size_t n_rows=0, n_cols=0;
double *p=NULL;
cu_mat(){} // Default constructor
cu_mat(const size_t &r, const size_t &c, const double &n); // Two argument constructor with initialization
void init(const size_t &r, const size_t &c); // Two argument memory allocation with initialization
public:
/***** Constructors *****/
cu_mat(const cu_mat &to_b_copied); // Copy constructor
cu_mat(const std::initializer_list<std::initializer_list<double>> &mat); // Single argument constructor with 'double' values
cu_mat(const std::initializer_list<std::initializer_list<cu_mat>> &mat); // Single argument constructor with 'cu_mat' values
cu_mat(const double &n); // Single value constructor
cu_mat(cu_mat&& to_be_moved); // Move constructor
/***** Operators *****/ // Add an ultimate '()' operator.
// cu_mat operator()(const cu_mat rows, const cu_mat cols); // Sub-matrix access with 'cu_mat'
cu_mat operator()(const size_t &idx) const; // Matrix element access based on index
cu_mat operator()(const size_t &r, const size_t &c) const; // Matrix element access
cu_mat operator()(const size_t &r_begin, const size_t &r_end, const size_t &c_begin, const size_t &c_end) const; // Sub-matrix access
cu_mat& operator=(const cu_mat &b); // Assignment operator to copy 'cu_mat'
cu_mat& operator=(cu_mat &&b); // Assignment operator to move 'cu_mat' // Assignment operator for 'double' to avoid implicit type casting
cu_mat operator*(const cu_mat &b) const &; // Matrix multiplication operator
cu_mat operator*(cu_mat &&b) const &; // Matrix multiplication operator
cu_mat operator*(const cu_mat &b) &&; // Matrix multiplication operator
cu_mat operator*(cu_mat &&b) &&; // Matrix multiplication operator
cu_mat operator/(const cu_mat &b) const &; // Matrix right divide operator
cu_mat operator/(cu_mat &&b) const &; // Matrix right divide operator
cu_mat operator/(const cu_mat &b) &&; // Matrix right divide operator
cu_mat operator/(cu_mat &&b) &&; // Matrix right divide operator
cu_mat operator+(const cu_mat &b) const &; // Matrix addition operator
cu_mat operator+(cu_mat &&b) const &; // Matrix addition operator
cu_mat operator+(const cu_mat &b) &&; // Matrix addition operator
cu_mat operator+(cu_mat &&b) &&; // Matrix addition operator
cu_mat operator-(const cu_mat &b) const &; // Matrix negation operator
cu_mat operator-(cu_mat &&b) const &; // Matrix negation operator
cu_mat operator-(const cu_mat &b) &&; // Matrix negation operator
cu_mat operator-(cu_mat &&b) &&; // Matrix negation operator
cu_mat operator-() const &; // Unary matrix negation operator
cu_mat operator-() &&; // Unary matrix negation operator
cu_mat operator^(const unsigned int &n); // Matrix power operator
cu_mat operator>(const cu_mat &b) const &; // Greater than operator
cu_mat operator>(cu_mat &&b) const &; // Greater than operator
cu_mat operator>(const cu_mat &b) &&; // Greater than operator
cu_mat operator>(cu_mat &&b) &&; // Greater than operator
cu_mat operator<(const cu_mat &b) const &; // Smaller than operator
cu_mat operator<(cu_mat &&b) const &; // Smaller than operator
cu_mat operator<(const cu_mat &b) &&; // Smaller than operator
cu_mat operator<(cu_mat &&b) &&; // Smaller than operator
cu_mat operator>=(const cu_mat &b) const &; // Greater than or equal to operator
cu_mat operator>=(cu_mat &&b) const &; // Greater than or equal to operator
cu_mat operator>=(const cu_mat &b) &&; // Greater than or equal to operator
cu_mat operator>=(cu_mat &&b) &&; // Greater than or equal to operator
cu_mat operator<=(const cu_mat &b) const &; // Smaller than operator or equal to
cu_mat operator<=(cu_mat &&b) const &; // Smaller than operator or equal to
cu_mat operator<=(const cu_mat &b) &&; // Smaller than operator or equal to
cu_mat operator<=(cu_mat &&b) &&; // Smaller than operator or equal to
cu_mat operator!() const &; // NOT operator
cu_mat operator!() &&; // NOT operator
cu_mat operator==(const cu_mat &b) const &; // Comparison equal to operator
cu_mat operator==(cu_mat &&b) const &; // Comparison equal to operator
cu_mat operator==(const cu_mat &b) &&; // Comparison equal to operator
cu_mat operator==(cu_mat &&b) &&; // Comparison equal to operator
cu_mat operator!=(const cu_mat &b) const &; // Comparison not equal to operator
cu_mat operator!=(cu_mat &&b) const &; // Comparison not equal to operator
cu_mat operator!=(const cu_mat &b) &&; // Comparison not equal to operator
cu_mat operator!=(cu_mat &&b) &&; // Comparison not equal to operator
cu_mat operator&&(const cu_mat &b) const &; // Logical 'AND' operator
cu_mat operator&&(cu_mat &&b) const &; // Logical 'AND' operator
cu_mat operator&&(const cu_mat &b) &&; // Logical 'AND' operator
cu_mat operator&&(cu_mat &&b) &&; // Logical 'AND' operator
cu_mat operator||(const cu_mat &b) const &; // Logical 'OR' operator
cu_mat operator||(cu_mat &&b) const &; // Logical 'OR' operator
cu_mat operator||(const cu_mat &b) &&; // Logical 'OR' operator
cu_mat operator||(cu_mat &&b) &&; // Logical 'OR' operator
explicit operator double(); // Type conversion from cu_mat to double
/***** Member functions *****/ // Add an ultimate replace function
cu_mat div(const cu_mat &b) const &; // Element wise division
cu_mat div(cu_mat &&b) const &; // Element wise division
cu_mat div(const cu_mat &b) &&; // Element wise division
cu_mat div(cu_mat &&b) &&; // Element wise division
cu_mat mult(const cu_mat &b) const &; // Element wise multiplication
cu_mat mult(cu_mat &&b) const &; // Element wise multiplication
cu_mat mult(const cu_mat &b) &&; // Element wise multiplication
cu_mat mult(cu_mat &&b) &&; // Element wise multiplication
cu_mat pow(const double &n) const &; // Element wise power
cu_mat pow(const double &n) &&; // Element wise power
void replace(const size_t &r, const size_t &c, const cu_mat &n); // Replace an element with a 'cu_mat' value
void replace(const size_t &r_begin, const size_t &r_end, const size_t &c_begin, const size_t &c_end, const cu_mat &n); // Replace submatrix with a 'cu_mat' matrix
void get(); // Print matrix data
void print(std::ofstream &print); // Print matrix to a file
size_t rows(); // Get number of rows
size_t rows() const; // Get number of rows
size_t cols(); // Get number of columns
size_t cols() const ; // Get number of columns
double* pointer(); // Get GPU memory pointer
double* pointer() const; // Get GPU memory pointer
/***** Supported external (friend) functions *****/
friend cu_mat randn(const size_t &r, const size_t &c); // Generate a matrix with normalized random numbers
friend cu_mat randn(const size_t &n);
friend cu_mat mld(const cu_mat &a, const cu_mat &b); // Matrix left divide operator
friend cu_mat mld(const cu_mat &a, cu_mat &&b); // Matrix left divide operator
friend cu_mat mld(cu_mat &&a, const cu_mat &b); // Matrix left divide operator
friend cu_mat mld(cu_mat &&a, cu_mat &&b); // Matrix left divide operator
friend cu_mat eye(const size_t &r, const size_t &c); // Generate a non-square identity matrix
friend cu_mat eye(const size_t &n);
friend cu_mat ones(const size_t &r, const size_t &c); // Matrix with all values 1
friend cu_mat ones(const size_t &n);
friend cu_mat zeros(const size_t &r, const size_t &c); // Matrix with all values 0
friend cu_mat zeros(const size_t &n);
friend cu_mat trans(cu_mat &a); // Transpose of the matrix
friend cu_mat horzcat(cu_mat &a, cu_mat &b); // Horizontal concatenation of two matrices
friend cu_mat vertcat(cu_mat &a, cu_mat &b); // Vertical concatenation of two matrices
friend cu_mat stepspace(const double &i, const double &step, const double &f); // MATLAB colon operator
friend cu_mat sum(const cu_mat &a); // Sum of the elements of the matrix (Working on it)
friend cu_mat norm(const cu_mat &a, const double &p); // Norm of the matrix
/***** Supported math functions *****/
friend cu_mat acos(const cu_mat &a); // Calculate arc cosine of each element
friend cu_mat acos(cu_mat &&a); // Calculate arc cosine of each element
friend cu_mat acosh(const cu_mat &a); // Calculate arc hyperbolic cosine of each element
friend cu_mat acosh(cu_mat &&a); // Calculate arc hyperbolic cosine of each element
friend cu_mat asin(const cu_mat &a); // Calculate arc sine of each element
friend cu_mat asin(cu_mat &&a); // Calculate arc sine of each element
friend cu_mat asinh(const cu_mat &a); // Calculate arc hyperbolic sine of each element
friend cu_mat asinh(cu_mat &&a); // Calculate arc hyperbolic sine of each element
friend cu_mat atan(const cu_mat &a); // Calculate arc tangent of each element
friend cu_mat atan(cu_mat &&a); // Calculate arc tangent of each element
friend cu_mat atan2(const cu_mat &y, const cu_mat &x); // Calculate four quadrant arc tangent of each element
friend cu_mat atan2(const cu_mat &y, cu_mat &&x); // Calculate four quadrant arc tangent of each element
friend cu_mat atan2(cu_mat &&y, const cu_mat &x); // Calculate four quadrant arc tangent of each element
friend cu_mat atan2(cu_mat &&y, cu_mat &&x); // Calculate four quadrant arc tangent of each element
friend cu_mat atanh(const cu_mat &a); // Calculate hyperbolic arc tangent of each element
friend cu_mat atanh(cu_mat &&a); // Calculate hyperbolic arc tangent of each element
friend cu_mat ceil(const cu_mat &a); // Calculate ceiling of each element
friend cu_mat ceil(cu_mat &&a); // Calculate ceiling of each element
friend cu_mat cos(const cu_mat &a); // Calculate cosine of each element
friend cu_mat cos(cu_mat &&a); // Calculate cosine of each element
friend cu_mat cosh(const cu_mat &a); // Calculate hyperbolic cosine of each element
friend cu_mat cosh(cu_mat &&a); // Calculate hyperbolic cosine of each element
friend cu_mat exp(const cu_mat &a); // Calculate e^x of each element
friend cu_mat exp(cu_mat &&a); // Calculate e^x of each element
friend cu_mat exp10(const cu_mat &a); // Calculate 10^x of each element
friend cu_mat exp10(cu_mat &&a); // Calculate 10^x of each element
friend cu_mat exp2(const cu_mat &a); // Calculate 2^x of each element
friend cu_mat exp2(cu_mat &&a); // Calculate 2^x of each element
friend cu_mat abs(const cu_mat &a); // Calculate absolute of each element
friend cu_mat abs(cu_mat &&a); // Calculate absolute of each element
friend cu_mat floor(const cu_mat &a); // Calculate floor value of each element
friend cu_mat floor(cu_mat &&a); // Calculate floor value of each element
friend cu_mat mod(const cu_mat &a, const cu_mat &b); // Calculate modulo of a/b
friend cu_mat mod(const cu_mat &a, cu_mat &&b); // Calculate modulo of a/b
friend cu_mat mod(cu_mat &&a, const cu_mat &b); // Calculate modulo of a/b
friend cu_mat mod(cu_mat &&a, cu_mat &&b); // Calculate modulo of a/b
friend cu_mat isfinite(const cu_mat &a); // Check if each element of matrix is not inf or nan
friend cu_mat isfinite(cu_mat &&a); // Check if each element of matrix is not inf or nan
friend cu_mat isinf(const cu_mat &a); // Check if each element is inf
friend cu_mat isinf(cu_mat &&a); // Check if each element is inf
friend cu_mat isnan(const cu_mat &a); // Check if each element is nan
friend cu_mat isnan(cu_mat &&a); // Check if each element is nan
friend bool isempty(cu_mat &a); // Check if 'cu_mat' object is empty
friend bool isscalar(const cu_mat &a); // Check if 'cu_mat' object is scalar
friend bool isscalar(cu_mat &a); // Check if 'cu_mat' object is scalar
friend bool isvector(cu_mat &a); // Check if 'cu_mat' object is vector
friend bool ismatrix(cu_mat &a); // Check if 'cu_mat' object is matrix
friend cu_mat log(const cu_mat &a); // Calculate log_e of each element
friend cu_mat log(cu_mat &&a); // Calculate log_e of each element
friend cu_mat log10(const cu_mat &a); // Calculate log_10 of each element
friend cu_mat log10(cu_mat &&a); // Calculate log_10 of each element
friend cu_mat log2(const cu_mat &a); // Calculate log_2 of each element
friend cu_mat log2(cu_mat &&a); // Calculate log_2 of each element
friend cu_mat rem(const cu_mat &a, const cu_mat &b); // Calculate remainder of each element of a/b
friend cu_mat rem(const cu_mat &a, cu_mat &&b); // Calculate remainder of each element of a/b
friend cu_mat rem(cu_mat &&a, const cu_mat &b); // Calculate remainder of each element of a/b
friend cu_mat rem(cu_mat &&a, cu_mat &&b); // Calculate remainder of each element of a/b
friend cu_mat round(const cu_mat &a); // Calculate rounded value of each element
friend cu_mat round(cu_mat &&a); // Calculate rounded value of each element
friend cu_mat sign(const cu_mat &a); // Calculate sign of each element
friend cu_mat sign(cu_mat &&a); // Calculate sign of each element
friend cu_mat sin(const cu_mat &a); // Calculate sine of each element
friend cu_mat sin(cu_mat &&a); // Calculate sine of each element
friend cu_mat sinh(const cu_mat &a); // Calculate hyperbolic sine of each element
friend cu_mat sinh(cu_mat &&a); // Calculate hyperbolic sine of each element
friend cu_mat sqrt(const cu_mat &a); // Calculate square root of each element
friend cu_mat sqrt(cu_mat &&a); // Calculate square root of each element
friend cu_mat tan(const cu_mat &a); // Calculate tangent of each element
friend cu_mat tan(cu_mat &&a); // Calculate tangent of each element
friend cu_mat tanh(const cu_mat &a); // Calculate hyperbolic tangent of each element
friend cu_mat tanh(cu_mat &&a); // Calculate hyperbolic tangent of each element
/***** Destructor *****/
virtual ~cu_mat();
};
/***** Supported external (friend) functions *****/
cu_mat randn(const size_t &r, const size_t &c); // Generate a matrix with normalized random numbers
cu_mat randn(const size_t &n);
cu_mat mld(const cu_mat &a, const cu_mat &b); // Matrix left divide operator
cu_mat mld(const cu_mat &a, cu_mat &&b); // Matrix left divide operator
cu_mat mld(cu_mat &&a, const cu_mat &b); // Matrix left divide operator
cu_mat mld(cu_mat &&a, cu_mat &&b); // Matrix left divide operator
cu_mat eye(const size_t &r, const size_t &c); // Generate a non-square identity matrix
cu_mat eye(const size_t &n);
cu_mat ones(const size_t &r, const size_t &c); // Matrix with all values 1
cu_mat ones(const size_t &n);
cu_mat zeros(const size_t &r, const size_t &c); // Matrix with all values 0
cu_mat zeros(const size_t &n);
cu_mat trans(cu_mat &a); // Transpose of the matrix
cu_mat horzcat(cu_mat &a, cu_mat &b); // Horizontal concatenation of two matrices
cu_mat vertcat(cu_mat &a, cu_mat &b); // Vertical concatenation of two matrices
cu_mat stepspace(const double &i, const double &step, const double &f); // MATLAB colon operator
cu_mat sum(const cu_mat &a); // Sum of the elements of the matrix (Working on it)
cu_mat norm(const cu_mat &a, const double &p); // Norm of the matrix
/***** Supported math functions *****/
cu_mat acos(const cu_mat &a); // Calculate arc cosine of each element
cu_mat acos(cu_mat &&a); // Calculate arc cosine of each element
cu_mat acosh(const cu_mat &a); // Calculate arc hyperbolic cosine of each element
cu_mat acosh(cu_mat &&a); // Calculate arc hyperbolic cosine of each element
cu_mat asin(const cu_mat &a); // Calculate arc sine of each element
cu_mat asin(cu_mat &&a); // Calculate arc sine of each element
cu_mat asinh(const cu_mat &a); // Calculate arc hyperbolic sine of each element
cu_mat asinh(cu_mat &&a); // Calculate arc hyperbolic sine of each element
cu_mat atan(const cu_mat &a); // Calculate arc tangent of each element
cu_mat atan(cu_mat &&a); // Calculate arc tangent of each element
cu_mat atan2(const cu_mat &y, const cu_mat &x); // Calculate four quadrant arc tangent of each element
cu_mat atan2(const cu_mat &y, cu_mat &&x); // Calculate four quadrant arc tangent of each element
cu_mat atan2(cu_mat &&y, const cu_mat &x); // Calculate four quadrant arc tangent of each element
cu_mat atan2(cu_mat &&y, cu_mat &&x); // Calculate four quadrant arc tangent of each element
cu_mat atanh(const cu_mat &a); // Calculate hyperbolic arc tangent of each element
cu_mat atanh(cu_mat &&a); // Calculate hyperbolic arc tangent of each element
cu_mat ceil(const cu_mat &a); // Calculate ceiling of each element
cu_mat ceil(cu_mat &&a); // Calculate ceiling of each element
cu_mat cos(const cu_mat &a); // Calculate cosine of each element
cu_mat cos(cu_mat &&a); // Calculate cosine of each element
cu_mat cosh(const cu_mat &a); // Calculate hyperbolic cosine of each element
cu_mat cosh(cu_mat &&a); // Calculate hyperbolic cosine of each element
cu_mat exp(const cu_mat &a); // Calculate e^x of each element
cu_mat exp(cu_mat &&a); // Calculate e^x of each element
cu_mat exp10(const cu_mat &a); // Calculate 10^x of each element
cu_mat exp10(cu_mat &&a); // Calculate 10^x of each element
cu_mat exp2(const cu_mat &a); // Calculate 2^x of each element
cu_mat exp2(cu_mat &&a); // Calculate 2^x of each element
cu_mat abs(const cu_mat &a); // Calculate absolute of each element
cu_mat abs(cu_mat &&a); // Calculate absolute of each element
cu_mat floor(const cu_mat &a); // Calculate floor value of each element
cu_mat floor(cu_mat &&a); // Calculate floor value of each element
cu_mat mod(const cu_mat &a, const cu_mat &b); // Calculate modulo of a/b
cu_mat mod(const cu_mat &a, cu_mat &&b); // Calculate modulo of a/b
cu_mat mod(cu_mat &&a, const cu_mat &b); // Calculate modulo of a/b
cu_mat mod(cu_mat &&a, cu_mat &&b); // Calculate modulo of a/b
cu_mat isfinite(const cu_mat &a); // Check if each element of matrix is not inf or nan
cu_mat isfinite(cu_mat &&a); // Check if each element of matrix is not inf or nan
cu_mat isinf(const cu_mat &a); // Check if each element is inf
cu_mat isinf(cu_mat &&a); // Check if each element is inf
cu_mat isnan(const cu_mat &a); // Check if each element is nan
cu_mat isnan(cu_mat &&a); // Check if each element is nan
bool isempty(cu_mat &a); // Check if 'cu_mat' object is empty
bool isscalar(const cu_mat &a); // Check if 'cu_mat' object is scalar
bool isscalar(cu_mat &a); // Check if 'cu_mat' object is scalar
bool isvector(cu_mat &a); // Check if 'cu_mat' object is vector
bool ismatrix(cu_mat &a); // Check if 'cu_mat' object is matrix
cu_mat log(const cu_mat &a); // Calculate log_e of each element
cu_mat log(cu_mat &&a); // Calculate log_e of each element
cu_mat log10(const cu_mat &a); // Calculate log_10 of each element
cu_mat log10(cu_mat &&a); // Calculate log_10 of each element
cu_mat log2(const cu_mat &a); // Calculate log_2 of each element
cu_mat log2(cu_mat &&a); // Calculate log_2 of each element
cu_mat rem(const cu_mat &a, const cu_mat &b); // Calculate remainder of each element of a/b
cu_mat rem(const cu_mat &a, cu_mat &&b); // Calculate remainder of each element of a/b
cu_mat rem(cu_mat &&a, const cu_mat &b); // Calculate remainder of each element of a/b
cu_mat rem(cu_mat &&a, cu_mat &&b); // Calculate remainder of each element of a/b
cu_mat round(const cu_mat &a); // Calculate rounded value of each element
cu_mat round(cu_mat &&a); // Calculate rounded value of each element
cu_mat sign(const cu_mat &a); // Calculate sign of each element
cu_mat sign(cu_mat &&a); // Calculate sign of each element
cu_mat sin(const cu_mat &a); // Calculate sine of each element
cu_mat sin(cu_mat &&a); // Calculate sine of each element
cu_mat sinh(const cu_mat &a); // Calculate hyperbolic sine of each element
cu_mat sinh(cu_mat &&a); // Calculate hyperbolic sine of each element
cu_mat sqrt(const cu_mat &a); // Calculate square root of each element
cu_mat sqrt(cu_mat &&a); // Calculate square root of each element
cu_mat tan(const cu_mat &a); // Calculate tangent of each element
cu_mat tan(cu_mat &&a); // Calculate tangent of each element
cu_mat tanh(const cu_mat &a); // Calculate hyperbolic tangent of each element
cu_mat tanh(cu_mat &&a); // Calculate hyperbolic tangent of each element
void ode45(std::function<cu_mat (const cu_mat &t, const cu_mat &x, const cu_mat ¶ms)> der, double t0, double tf, cu_mat y0, cu_mat der_params);
#endif /* CU_MAT_HCU_ */