forked from LLNL/libROM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector.h
662 lines (610 loc) · 17 KB
/
Vector.h
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
/******************************************************************************
*
* Copyright (c) 2013-2019, Lawrence Livermore National Security, LLC
* and other libROM project developers. See the top-level COPYRIGHT
* file for details.
*
* SPDX-License-Identifier: (Apache-2.0 OR MIT)
*
*****************************************************************************/
// Description: A simple, parallel Vector class with the utility needed to
// support the basis generation methods of this library. A
// distributed Vector has its rows distributed across processors.
#ifndef included_Vector_h
#define included_Vector_h
#include "Utilities.h"
namespace CAROM {
/**
* A simple vector class in which the dimensions may be distributed across
* multiple processes. This class supports only the basic operations that are
* needed by the SVD library.
*/
class Vector
{
public:
Vector();
/**
* @brief Constructor creating a Vector with uninitialized values.
*
* @pre dim > 0
*
* @param[in] dim When undistributed, the total dimension of the Vector.
* When distributed, the part of the total dimension of
* the Vector on this processor.
* @param[in] distributed If true the dimensions of the Vector are spread
* over all processors.
*/
Vector(
int dim,
bool distributed);
/**
* @brief Constructor in which the Vector is given its initial values.
*
* @pre vec != 0
* @pre dim > 0
*
* @param[in] vec The initial values of the Vector.
* @param[in] dim When undistributed, the total dimension of the Vector.
* When distributed, the part of the total dimension of
* the Vector on this processor.
* @param[in] distributed If true the dimensions of the Vector are spread
* over all processors.
* @param[in] copy_data If true the vector allocates is own storage and
* copies the contents of vec into its own storage.
* Otherwise it uses vec as its storage.
*/
Vector(
double* vec,
int dim,
bool distributed,
bool copy_data = true);
/**
* @brief Copy constructor.
*
* @param[in] other The Vector to copy.
*/
Vector(
const Vector& other);
/**
* @brief Destructor.
*/
~Vector();
/**
* @brief Assignment operator.
*
* @param[in] rhs The Vector to assign to this.
*
* @return This after rhs has been assigned to it.
*/
Vector&
operator = (
const Vector& rhs);
/**
* @brief Addition operator.
*
* @param[in] rhs The Vector to add to this.
*
* @return This after rhs has been added to it.
*/
Vector&
operator += (
const Vector& rhs);
/**
* @brief Equal operator.
*
* @param[in] rhs The Vector to add to this.
*
* @return This after rhs has been added to it.
*/
Vector&
operator = (
const double& a);
/**
* @brief Sets the length of the vector and reallocates storage if
* needed.
*
* @param[in] dim When undistributed, the total dimension of the Vector.
* When distributed, the part of the total dimension of
* the Vector on this processor.
*/
void
setSize(
int dim)
{
if (dim > d_alloc_size) {
if (!d_owns_data) {
CAROM_ERROR("Can not reallocate externally owned storage.");
}
if (d_vec) {
delete [] d_vec;
}
d_vec = new double [dim];
d_alloc_size = dim;
}
d_dim = dim;
}
/**
* @brief Returns true if the Vector is distributed.
*
* @return True if the Vector is distributed.
*/
bool
distributed() const
{
return d_distributed;
}
/**
* @brief Returns the dimension of the Vector on this processor.
*
* @return The part of the Vector's dimension on this processor.
*/
int
dim() const
{
return d_dim;
}
/**
* @brief Inner product, reference form.
*
* For distributed Vectors this is a parallel operation.
*
* @pre dim() == other.dim()
* @pre distributed() == other.distributed()
*
* @param[in] other The Vector to form the inner product with this.
*
* @return The inner product of this and other.
*/
double
inner_product(
const Vector& other) const;
/**
* @brief Inner product, pointer version.
*
* For distributed Vectors this is a parallel operation.
*
* @pre other != 0
* @pre dim() == other->dim()
* @pre distributed() == other->distributed()
*
* @param[in] other The Vector to form the inner product with this.
*
* @return The inner product of this and other.
*/
double
inner_product(
const Vector* other) const
{
CAROM_ASSERT(other != 0);
return inner_product(*other);
}
/**
* @brief Form the norm of this.
*
* For a distributed Vector this is a parallel operation.
*
* @return The norm of this.
*/
double
norm() const;
/**
* @brief Normalizes the Vector and returns its norm.
*
* For a distributed Vector this is a parallel operation.
*
* @return The norm of this.
*/
double
normalize();
/**
* @brief Adds other and this and returns the result, reference version.
*
* @pre distributed() == other.distributed()
* @pre dim() == other.dim()
*
* @param[in] other The other summand.
*
* @return this + other
*/
Vector*
plus(
const Vector& other) const
{
Vector* result = 0;
plus(other, result);
return result;
}
/**
* @brief Adds other and this and returns the result, pointer version.
*
* @pre other != 0
* @pre distributed() == other->distributed()
* @pre dim() == other->dim()
*
* @param[in] other The other summand.
*
* @return this + other
*/
Vector*
plus(
const Vector* other) const
{
CAROM_ASSERT(other != 0);
return plus(*other);
}
/**
* @brief Adds other and this and fills result with the answer.
*
* Result will be allocated if unallocated or resized appropriately if
* already allocated.
*
* @pre result == 0 || result->distributed() == distributed()
* @pre distributed() == other.distributed()
* @pre dim() == other.dim()
*
* @param[in] other The other summand.
* @param[out] result this + other
*/
void
plus(
const Vector& other,
Vector*& result) const;
/**
* @brief Adds other and this and fills result with the answer.
*
* Result will be resized appropriately.
*
* @pre result.distributed() == distributed()
* @pre distributed() == other.distributed()
* @pre dim() == other.dim()
*
* @param[in] other The other summand.
* @param[out] result this + other
*/
void
plus(
const Vector& other,
Vector& result) const;
/**
* @brief Adds factor*other and this and returns the result, reference
* version.
*
* @pre distributed() == other.distributed()
* @pre dim() == other.dim()
*
* @param[in] factor Multiplicative factor applied to other.
* @param[in] other The other summand.
*
* @return this + factor*other
*/
Vector*
plusAx(
double factor,
const Vector& other)
{
Vector* result = 0;
plusAx(factor, other, result);
return result;
}
/**
* @brief Adds factor*other and this and returns the result, pointer
* version.
*
* @pre distributed() == other->distributed()
* @pre dim() == other->dim()
*
* @param[in] factor Multiplicative factor applied to other.
* @param[in] other The other summand.
*
* @return this + factor*other
*/
Vector*
plusAx(
double factor,
const Vector* other)
{
CAROM_ASSERT(other != 0);
return plusAx(factor, *other);
}
/**
* @brief Adds factor*other and this and fills result with the answer.
*
* Result will be allocated if unallocated or resized appropriately if
* already allocated.
*
* @pre result == 0 || result->distributed() == distributed()
* @pre distributed() == other.distributed()
* @pre dim() == other.dim()
*
* @param[in] other The other summand.
* @param[out] result this + factor*other
*/
void
plusAx(
double factor,
const Vector& other,
Vector*& result) const;
/**
* @brief Adds factor*other and this and fills result with the answer.
*
* Result will be resized appropriately.
*
* @pre result.distributed() == distributed()
* @pre distributed() == other.distributed()
* @pre dim() == other.dim()
*
* @param[in] other The other summand.
* @param[out] result this + factor*other
*/
void
plusAx(
double factor,
const Vector& other,
Vector& result) const;
/**
* @brief Adds factor*other to this, reference version.
*
* @pre distributed() == other.distributed()
* @pre dim() == other.dim()
*
* @param[in] factor Multiplicative factor applied to other.
* @param[in] other The other summand.
*/
void
plusEqAx(
double factor,
const Vector& other);
/**
* @brief Adds factor*other to this, pointer version.
*
* @pre other != 0
* @pre distributed() == other->distributed()
* @pre dim() == other->dim()
*
* @param[in] factor Multiplicative factor applied to other.
* @param[in] other The other summand.
*/
void
plusEqAx(
double factor,
const Vector* other)
{
CAROM_ASSERT(other != 0);
plusEqAx(factor, *other);
}
/**
* @brief Subtracts other and this and returns the result, reference
* version.
*
* @pre distributed() == other.distributed()
* @pre dim() == other.dim()
*
* @param[in] other The other subtrahand.
*
* @return this - other
*/
Vector*
minus(
const Vector& other) const
{
Vector* result = 0;
minus(other, result);
return result;
}
/**
* @brief Subtracts other and this and returns the result, pointer
* version.
*
* @pre other != 0
* @pre distributed() == other->distributed()
* @pre dim() == other->dim()
*
* @param[in] other The other subtrahand.
*
* @return this - other
*/
Vector*
minus(
const Vector* other) const
{
CAROM_ASSERT(other != 0);
return minus(*other);
}
/**
* @brief Subtracts other and this and fills result with the answer.
*
* Result will be allocated if unallocated or resized appropriately if
* already allocated.
*
* @pre result == 0 || result->distributed() == distributed()
* @pre distributed() == other.distributed()
* @pre dim() == other.dim()
*
* @param[in] other The other subtrahend.
* @param[out] result this - other
*/
void
minus(
const Vector& other,
Vector*& result) const;
/**
* @brief Subtracts other and this and fills result with the answer.
*
* Result will be resized appropriately.
*
* @pre result.distributed() == distributed()
* @pre distributed() == other.distributed()
* @pre dim() == other.dim()
*
* @param[in] other The other subtrahend.
* @param[out] result this - other
*/
void
minus(
const Vector& other,
Vector& result) const;
/**
* @brief Multiplies this by the supplied constant and returns the
* result.
*
* @param[in] factor Factor to multiply by.
*
* @return factor*this
*/
Vector*
mult(
double factor) const
{
Vector* result = 0;
mult(factor, result);
return result;
}
/**
* @brief Multiplies this by the supplied constant and fills result with
* the answer.
*
* @pre result == 0 || result->distributed() == distributed()
*
* @param[in] factor Factor to multiply by.
* @param[out] result factor*this
*/
void
mult(
double factor,
Vector*& result) const;
/**
* @brief Multiplies this by the supplied constant and fills result with
* the answer.
*
* @pre result.distributed() == distributed()
*
* @param[in] factor Factor to multiply by.
* @param[out] result factor*this
*/
void
mult(
double factor,
Vector& result) const;
/**
* @brief Const Vector member access.
*
* @pre (0 <= i) && (i < dim())
*
* @param[in] i The component of the Vector on this processor requested.
*
* @return The requested component of the Vector on this processor.
*/
const double&
item(
int i) const
{
CAROM_ASSERT((0 <= i) && (i < dim()));
return d_vec[i];
}
/**
* @brief Non-const Vector member access.
*
* Allows constructs of the form vec[i] = val;
*
* @pre (0 <= i) && (i < dim())
*
* @param[in] i The component of the Vector on this processor requested.
*
* @return The requested component of the Vector on this processor.
*/
double&
item(
int i)
{
CAROM_ASSERT((0 <= i) && (i < dim()));
return d_vec[i];
}
/**
* @brief Const Vector member access.
*
* @pre (0 <= i) && (i < dim())
*
* @param[in] i The component of the Vector on this processor requested.
*
* @return The requested component of the Vector on this processor.
*/
const double& operator() (int i) const
{
return item(i);
}
/**
* @brief Non-const Vector member access.
*
* Allows constructs of the form vec[i] = val;
*
* @pre (0 <= i) && (i < dim())
*
* @param[in] i The component of the Vector on this processor requested.
*
* @return The requested component of the Vector on this processor.
*/
double& operator() (int i)
{
return item(i);
}
/**
* @brief print Vector into (a) ascii file(s).
*
* @param[in] prefix The name of the prefix of the file name.
*
*/
void print(const char * prefix);
/**
* @brief write Vector into (a) HDF file(s).
*
* @param[in] prefix The name of the prefix of the file name.
*
*/
void write(const std::string& base_file_name);
/**
* @brief read Vector from (a) HDF file(s).
*
* @param[in] prefix The name of the prefix of the file name.
*
*/
void read(const std::string& base_file_name);
private:
/**
* @brief Default constructor is not implemented.
*/
// Vector();
/**
* @brief The storage for the Vector's values on this processor.
*/
double* d_vec;
/**
* @brief The part of the Vector's dimension on this processor.
*/
int d_dim;
/**
* @brief The currently allocated size.
*
* d_dim <= d_alloc_size
*/
int d_alloc_size;
/**
* @brief If true, the Vector's dimensions are distributed over all
* processors.
*
* Each processor does not need to hold the same number of dimensions.
*/
bool d_distributed;
/**
* @brief The number of processors being run on.
*/
int d_num_procs;
/**
* @brief If true, this object owns its underlying data, d_vec, and
* is responsible for its deletion.
*
* If d_owns_data is false, then the object may not reallocate d_vec.
*/
bool d_owns_data;
};
}
#endif