MPQC 3.0.0-alpha
Loading...
Searching...
No Matches
elemop.h
1//
2// elemop.h
3//
4// Copyright (C) 1996 Limit Point Systems, Inc.
5//
6// Author: Curtis Janssen <cljanss@limitpt.com>
7// Maintainer: LPS
8//
9// This file is part of the SC Toolkit.
10//
11// The SC Toolkit is free software; you can redistribute it and/or modify
12// it under the terms of the GNU Library General Public License as published by
13// the Free Software Foundation; either version 2, or (at your option)
14// any later version.
15//
16// The SC Toolkit is distributed in the hope that it will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19// GNU Library General Public License for more details.
20//
21// You should have received a copy of the GNU Library General Public License
22// along with the SC Toolkit; see the file COPYING.LIB. If not, write to
23// the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24//
25// The U.S. Government is granted a limited license as per AL 91-7.
26//
27
28#ifndef _math_scmat_elemop_h
29#define _math_scmat_elemop_h
30
31#include <cmath>
32#include <stdexcept>
33#include <functional>
34#include <util/state/state.h>
35#include <util/state/statein.h>
36#include <util/state/stateout.h>
37#include <util/group/message.h>
38#include <math/scmat/blkiter.h>
39#include <math/scmat/predicate.h>
40
41namespace sc {
42
43class SCMatrixBlock;
44class SCMatrixBlockIter;
45class SCMatrixRectBlock;
46class SCMatrixLTriBlock;
47class SCMatrixDiagBlock;
48class SCVectorSimpleBlock;
49class SCMatrixRectSubBlock;
50class SCMatrixLTriSubBlock;
51class SCMatrixDiagSubBlock;
52class SCVectorSimpleSubBlock;
53
54class SCMatrix;
55class SymmSCMatrix;
56class DiagSCMatrix;
57class SCVector;
58
60 enum Values {AllElements = 0,
61 Columns = 1,
62 Rows = 2
63 };
64};
65struct SCElement {
66 typedef double value_type;
67 SCElement() : irow(null.irow), icol(null.icol), value(null.value) {}
68 SCElement(int ir, int ic, value_type v) : irow(ir), icol(ic), value(v) {}
69 int irow;
70 int icol;
71 value_type value;
72
73 static SCElement null;
74
75 operator value_type() const {
76 return value;
77 }
78};
79
83template <class BinaryPredicate>
84struct SCElementBinaryPredicateAdapter : public std::binary_function<SCElement, SCElement, bool> {
85 bool operator()(const sc::SCElement& a, const sc::SCElement& b) {
86 return BinaryPredicate()(a.value,b.value);
87 }
88};
89
95 public:
98 virtual ~SCElementOp();
106 virtual int has_collect();
107 virtual void defer_collect(int);
108 virtual void collect(const Ref<MessageGrp>&);
112 virtual void collect(const Ref<SCElementOp>&);
116 virtual int has_side_effects();
117
120 virtual bool threadsafe() const;
121
124 virtual bool cloneable() const;
125
130
133 virtual void process(SCMatrixBlockIter&) = 0;
134
140
146 virtual void process_spec_ltri(SCMatrixLTriBlock*);
147 virtual void process_spec_diag(SCMatrixDiagBlock*);
148 virtual void process_spec_vsimp(SCVectorSimpleBlock*);
149 virtual void process_spec_rectsub(SCMatrixRectSubBlock*);
150 virtual void process_spec_ltrisub(SCMatrixLTriSubBlock*);
151 virtual void process_spec_diagsub(SCMatrixDiagSubBlock*);
152 virtual void process_spec_vsimpsub(SCVectorSimpleSubBlock*);
153};
154
160 public:
161 SCElementOp2();
163 virtual ~SCElementOp2();
164 virtual int has_collect();
165 virtual void defer_collect(int);
166 virtual int has_side_effects();
167 virtual int has_side_effects_in_arg();
168 virtual void collect(const Ref<MessageGrp>&);
169 virtual void process(SCMatrixBlockIter&,SCMatrixBlockIter&) = 0;
170 void process_base(SCMatrixBlock*,SCMatrixBlock*);
171 virtual void process_spec_rect(SCMatrixRectBlock*,SCMatrixRectBlock*);
172 virtual void process_spec_ltri(SCMatrixLTriBlock*,SCMatrixLTriBlock*);
173 virtual void process_spec_diag(SCMatrixDiagBlock*,SCMatrixDiagBlock*);
174 virtual void process_spec_vsimp(SCVectorSimpleBlock*,SCVectorSimpleBlock*);
175};
176
182 public:
183 SCElementOp3();
185 virtual ~SCElementOp3();
186 virtual int has_collect();
187 virtual void defer_collect(int);
188 virtual int has_side_effects();
189 virtual int has_side_effects_in_arg1();
190 virtual int has_side_effects_in_arg2();
191 virtual void collect(const Ref<MessageGrp>&);
192 virtual void process(SCMatrixBlockIter&,
194 SCMatrixBlockIter&) = 0;
195 void process_base(SCMatrixBlock*,SCMatrixBlock*,SCMatrixBlock*);
196 virtual void process_spec_rect(SCMatrixRectBlock*,
199 virtual void process_spec_ltri(SCMatrixLTriBlock*,
202 virtual void process_spec_diag(SCMatrixDiagBlock*,
205 virtual void process_spec_vsimp(SCVectorSimpleBlock*,
208};
209
214 private:
215 int deferred_;
216 double product;
217 public:
222 void process(SCMatrixBlockIter&,SCMatrixBlockIter&);
223 int has_collect();
224 void defer_collect(int);
225 void collect(const Ref<MessageGrp>&);
226 double result();
227 void init() { product = 0.0; }
228};
229
230
243
248 public:
249 SCElementDAXPY(double a);
252 int has_side_effects();
254 void process(SCMatrixBlockIter&,SCMatrixBlockIter&);
255 private:
256 double a_;
257};
258
260 private:
261 double scale;
262 public:
263 SCElementScale(double a);
269};
270
272 private:
273 double assign;
274 public:
281};
282
284 private:
285 double assign;
286 public:
287 SCElementAssign(double a);
293};
294
305
307 private:
308 double threshold_;
309 int nbelowthreshold_;
310 int deferred_;
311 public:
312 SCElementInvert(double threshold = 0.0);
319 void defer_collect(int);
320 void collect(const Ref<MessageGrp>&);
322 int result() { return nbelowthreshold_; }
323};
324
325
327 private:
328 double scale_diagonal;
329 public:
330 SCElementScaleDiagonal(double a);
336};
337
339 private:
340 double shift_diagonal;
341 public:
342 SCElementShiftDiagonal(double a);
348};
349
357template <class BinaryPredicate = std::less<double>, SCMatrixIterationRanges::Values IterationRanges = SCMatrixIterationRanges::AllElements>
359 public:
361
362 SCElementFindExtremum(double init_value = 0.0):deferred_(0), init_value_(init_value), r(1, SCElement(0,0,init_value)) {}
364 {
365 {
366 int nchar; s.get(nchar);
367 r.resize(nchar / sizeof(SCElement));
368 char* r_char;
369 s.get(r_char);
370 s.get(init_value_);
371 memcpy(&r[0], r_char, nchar);
372 delete[] r_char;
373 }
374 s.get(deferred_);
375 }
376
378
380 {
381 {
382 int nchar = r.size() * sizeof(SCElement);
383 s.put(nchar);
384 s.put(init_value_);
385 s.put((char*)(&r[0]), nchar);
386 }
387 s.put(deferred_);
388 }
389
391 for (i.reset(); i; ++i) {
392 int range;
393 switch(IterationRanges) {
394 case SCMatrixIterationRanges::AllElements: range = 0; break;
395 case SCMatrixIterationRanges::Columns: range = i.j(); break;
396 case SCMatrixIterationRanges::Rows: range = i.i(); break;
397 }
398 if (range >= r.size()) {
399 const size_t old_size = r.size();
400 r.resize(range+1);
401 for(size_t i=old_size; i<r.size(); ++i) {
402 r[i] = SCElement(0,0,init_value_);
403 }
404 }
405 double value = i.get();
406 if (Op(r[range].value, value)) {
407 r[range] = SCElement(i.i(), i.j(), i.get());
408 }
409 }
410 }
411
412 int has_collect() { return 1; }
413 void defer_collect(int h) { deferred_=h; }
414
415 void collect(const Ref<MessageGrp>& msg) {
416 if (!deferred_) {
417 GrpCompareReduce<SCElement, SCElementBinaryPredicateAdapter<BinaryPredicate> > reduce;
418 msg->reduce<SCElement>(&r[0], r.size(), reduce);
419 }
420 }
421 void collect(const Ref<SCElementOp>& op) {
422 throw std::runtime_error(
423 "SCElementMaxAbs::collect(const Ref<SCElementOp> &): not implemented");
424 }
425
426 const std::vector<SCElement>& result() { return r; }
427
428 private:
429 int deferred_;
430 double init_value_;
431 std::vector<SCElement> r;
432 BinaryPredicate Op;
433
434 static ClassDesc class_desc_;
435};
436
437template <class BinaryPredicate, SCMatrixIterationRanges::Values IterationRanges>
438ClassDesc SCElementFindExtremum<BinaryPredicate,IterationRanges>::class_desc_(
439 typeid(this_type),
440 typeid(this_type).name(),
441 1,"public SCElementOp",
442 0, 0, sc::create<typename SCElementFindExtremum<BinaryPredicate,IterationRanges>::this_type>);
443
445 private:
446 int deferred_;
447 double r;
448 public:
455 void defer_collect(int);
456 void collect(const Ref<MessageGrp>&);
458 double result();
459};
460
461
463 private:
464 int deferred_;
465 double r;
466 public:
467 // rinit must be greater than the magnitude of the smallest element
468 SCElementMinAbs(double rinit);
474 void defer_collect(int);
475 void collect(const Ref<MessageGrp>&);
477 double result();
478};
479
481 private:
482 int deferred_;
483 double r;
484 public:
485 SCElementSum();
491 void defer_collect(int);
492 void collect(const Ref<MessageGrp>&);
494 double result();
495 void init() { r = 0.0; }
496};
497
500 private:
501 int deferred_;
502 double r_; // result
503 unsigned int k_; // norm parameter
504
505 static double _process(SCMatrixBlockIter& i, int k);
506 static double _process1(SCMatrixBlockIter& i);
507 static double _process2(SCMatrixBlockIter& i);
508
509 public:
511 SCElementKNorm(unsigned int k = 2);
517 void defer_collect(int);
518 void collect(const Ref<MessageGrp>&);
520 double result();
521 void init() { r_ = 0.0; }
522};
523
525 private:
526 double** avects;
527 double** bvects;
528 int length;
529 public:
532 SCElementDot(double**a, double**b, int length);
535};
536
545
554
563
572
573}
574
575#endif
576
577// Local Variables:
578// mode: c++
579// c-file-style: "CLJ"
580// End:
The SymmSCMatrix class is the abstract base class for diagonal double valued matrices.
Definition abstract.h:555
A template class that maintains references counts.
Definition ref.h:361
int has_side_effects()
By default this returns nonzero.
void process(SCMatrixBlockIter &)
This is the fallback routine to process blocks and is called by process_spec members that are not ove...
Definition elemop.h:537
int has_side_effects()
By default this returns nonzero.
void process(SCMatrixBlockIter &)
This is the fallback routine to process blocks and is called by process_spec members that are not ove...
Definition elemop.h:564
void process(SCMatrixBlockIter &)
This is the fallback routine to process blocks and is called by process_spec members that are not ove...
int has_side_effects()
By default this returns nonzero.
void process(SCMatrixBlockIter &)
This is the fallback routine to process blocks and is called by process_spec members that are not ove...
int has_side_effects()
By default this returns nonzero.
Definition elemop.h:283
int has_side_effects()
By default this returns nonzero.
void process(SCMatrixBlockIter &)
This is the fallback routine to process blocks and is called by process_spec members that are not ove...
void save_data_state(StateOut &)
Save the base classes (with save_data_state) and the members in the same order that the StateIn CTOR ...
does
Definition elemop.h:247
void save_data_state(StateOut &)
Save the base classes (with save_data_state) and the members in the same order that the StateIn CTOR ...
does
Definition elemop.h:234
void save_data_state(StateOut &)
Save the base classes (with save_data_state) and the members in the same order that the StateIn CTOR ...
Definition elemop.h:524
void process(SCMatrixBlockIter &)
This is the fallback routine to process blocks and is called by process_spec members that are not ove...
void save_data_state(StateOut &)
Save the base classes (with save_data_state) and the members in the same order that the StateIn CTOR ...
int has_side_effects()
By default this returns nonzero.
Searches each range in IterationRanges for element i so that there is no element j in that Range for ...
Definition elemop.h:358
void process(SCMatrixBlockIter &i)
This is the fallback routine to process blocks and is called by process_spec members that are not ove...
Definition elemop.h:390
void save_data_state(StateOut &s)
Save the base classes (with save_data_state) and the members in the same order that the StateIn CTOR ...
Definition elemop.h:379
int has_collect()
If duplicates of the SCElementOp exist (that is, there is more than one node), then if has_collect re...
Definition elemop.h:412
void collect(const Ref< SCElementOp > &op)
Multithreaded use of cloneable SCElementOp objects requires that data from cloned objects be collecte...
Definition elemop.h:421
Definition elemop.h:306
void collect(const Ref< SCElementOp > &)
Multithreaded use of cloneable SCElementOp objects requires that data from cloned objects be collecte...
int has_collect()
If duplicates of the SCElementOp exist (that is, there is more than one node), then if has_collect re...
void process(SCMatrixBlockIter &)
This is the fallback routine to process blocks and is called by process_spec members that are not ove...
int has_side_effects()
By default this returns nonzero.
void save_data_state(StateOut &)
Save the base classes (with save_data_state) and the members in the same order that the StateIn CTOR ...
Computes k-norm of matrix.
Definition elemop.h:499
void save_data_state(StateOut &)
Save the base classes (with save_data_state) and the members in the same order that the StateIn CTOR ...
int has_collect()
If duplicates of the SCElementOp exist (that is, there is more than one node), then if has_collect re...
void process(SCMatrixBlockIter &)
This is the fallback routine to process blocks and is called by process_spec members that are not ove...
SCElementKNorm(unsigned int k=2)
by default compute 2-norm
void collect(const Ref< SCElementOp > &)
Multithreaded use of cloneable SCElementOp objects requires that data from cloned objects be collecte...
Definition elemop.h:444
void process(SCMatrixBlockIter &)
This is the fallback routine to process blocks and is called by process_spec members that are not ove...
void collect(const Ref< SCElementOp > &)
Multithreaded use of cloneable SCElementOp objects requires that data from cloned objects be collecte...
int has_collect()
If duplicates of the SCElementOp exist (that is, there is more than one node), then if has_collect re...
void save_data_state(StateOut &)
Save the base classes (with save_data_state) and the members in the same order that the StateIn CTOR ...
Definition elemop.h:462
void save_data_state(StateOut &)
Save the base classes (with save_data_state) and the members in the same order that the StateIn CTOR ...
int has_collect()
If duplicates of the SCElementOp exist (that is, there is more than one node), then if has_collect re...
void collect(const Ref< SCElementOp > &)
Multithreaded use of cloneable SCElementOp objects requires that data from cloned objects be collecte...
void process(SCMatrixBlockIter &)
This is the fallback routine to process blocks and is called by process_spec members that are not ove...
The SCElementOp2 class is very similar to the SCElementOp class except that pairs of blocks are treat...
Definition elemop.h:159
The SCElementOp3 class is very similar to the SCElementOp class except that a triplet of blocks is tr...
Definition elemop.h:181
Objects of class SCElementOp are used to perform operations on the elements of matrices.
Definition elemop.h:94
virtual Ref< SCElementOp > clone()
Returns a clone of this object.
virtual int has_collect()
If duplicates of the SCElementOp exist (that is, there is more than one node), then if has_collect re...
virtual void process_spec_rect(SCMatrixRectBlock *)
Matrices should call these members when the type of block is known.
virtual bool cloneable() const
Returns true if this SCElementOp supports the clone() member.
virtual void collect(const Ref< SCElementOp > &)
Multithreaded use of cloneable SCElementOp objects requires that data from cloned objects be collecte...
virtual bool threadsafe() const
Returns true if this SCElementOp is threadsafe.
void process_base(SCMatrixBlock *block)
Lazy matrix implementors can call this member when the type of block specialization is unknown.
virtual int has_side_effects()
By default this returns nonzero.
virtual void process(SCMatrixBlockIter &)=0
This is the fallback routine to process blocks and is called by process_spec members that are not ove...
Definition elemop.h:271
void process(SCMatrixBlockIter &)
This is the fallback routine to process blocks and is called by process_spec members that are not ove...
void save_data_state(StateOut &)
Save the base classes (with save_data_state) and the members in the same order that the StateIn CTOR ...
int has_side_effects()
By default this returns nonzero.
evaluates
Definition elemop.h:213
void save_data_state(StateOut &)
Save the base classes (with save_data_state) and the members in the same order that the StateIn CTOR ...
Definition elemop.h:326
void save_data_state(StateOut &)
Save the base classes (with save_data_state) and the members in the same order that the StateIn CTOR ...
void process(SCMatrixBlockIter &)
This is the fallback routine to process blocks and is called by process_spec members that are not ove...
int has_side_effects()
By default this returns nonzero.
Definition elemop.h:259
int has_side_effects()
By default this returns nonzero.
void process(SCMatrixBlockIter &)
This is the fallback routine to process blocks and is called by process_spec members that are not ove...
void save_data_state(StateOut &)
Save the base classes (with save_data_state) and the members in the same order that the StateIn CTOR ...
Definition elemop.h:338
int has_side_effects()
By default this returns nonzero.
void save_data_state(StateOut &)
Save the base classes (with save_data_state) and the members in the same order that the StateIn CTOR ...
void process(SCMatrixBlockIter &)
This is the fallback routine to process blocks and is called by process_spec members that are not ove...
Definition elemop.h:295
void save_data_state(StateOut &)
Save the base classes (with save_data_state) and the members in the same order that the StateIn CTOR ...
int has_side_effects()
By default this returns nonzero.
void process(SCMatrixBlockIter &)
This is the fallback routine to process blocks and is called by process_spec members that are not ove...
Definition elemop.h:480
void save_data_state(StateOut &)
Save the base classes (with save_data_state) and the members in the same order that the StateIn CTOR ...
int has_collect()
If duplicates of the SCElementOp exist (that is, there is more than one node), then if has_collect re...
void collect(const Ref< SCElementOp > &)
Multithreaded use of cloneable SCElementOp objects requires that data from cloned objects be collecte...
void process(SCMatrixBlockIter &)
This is the fallback routine to process blocks and is called by process_spec members that are not ove...
The SCMatrixBlockIter class is used to described iterates that loop through the elements in a block.
Definition blkiter.h:46
virtual int j()=0
Returns the column index.
virtual int i()=0
Returns the row index.
virtual void reset()=0
Start the iteration over.
virtual double get()=0
Return the value of the current element.
SCMatrixBlock is the base clase for all types of blocks that comprise matrices and vectors.
Definition block.h:41
The SCMatrixDiagBlock describes a diagonal piece of a matrix.
Definition block.h:318
The SCMatrixDiagSubBlock describes a diagonal subblock of a matrix.
Definition block.h:351
The SCMatrixLTriBlock describes a triangular piece of a matrix.
Definition block.h:253
The SCMatrixLTriSubBlock describes a triangular subblock of a matrix.
Definition block.h:288
The SCMatrixRectBlock describes a rectangular piece of a matrix.
Definition block.h:183
The SCMatrixRectSubBlock describes a rectangular piece of a matrix.
Definition block.h:219
The SCMatrix class is the abstract base class for general double valued n by m matrices.
Definition abstract.h:199
The SCVectorSimpleBlock describes a piece of a vector.
Definition block.h:123
The SCVectorSimpleSubBlock describes a subblock of a vector.
Definition block.h:154
The SCVector class is the abstract base class for double valued vectors.
Definition abstract.h:97
Base class for objects that can save/restore state.
Definition state.h:45
Restores fundamental and user-defined types from images created with StateOut.
Definition statein.h:79
virtual int get(const ClassDesc **)
This restores ClassDesc's.
Serializes fundamental and user-defined types.
Definition stateout.h:71
virtual int put(const ClassDesc *)
Write out information about the given ClassDesc.
The SymmSCMatrix class is the abstract base class for symmetric double valued matrices.
Definition abstract.h:389
Contains all MPQC code up to version 3.
Definition mpqcin.h:14
DescribedClass * create()
This is used to pass a function that make void constructor calls to the ClassDesc constructor.
Definition class.h:102
Definition range.hpp:25
Adapts a binary predicate that acts on SCElement::value_type.
Definition elemop.h:84
Definition elemop.h:65
Definition elemop.h:59

Generated at Wed Sep 25 2024 02:45:30 for MPQC 3.0.0-alpha using the documentation package Doxygen 1.12.0.