46 #ifndef _PEXSI_UTILITY_HPP_
47 #define _PEXSI_UTILITY_HPP_
59 #include <sympack.hpp>
69 const int LENGTH_VAR_NAME = 8;
70 const int LENGTH_DBL_DATA = 16;
71 const int LENGTH_INT_DATA = 5;
72 const int LENGTH_VAR_UNIT = 6;
73 const int LENGTH_DBL_PREC = 8;
74 const int LENGTH_VAR_DATA = 16;
78 const std::vector<Int> NO_MASK(1);
86 inline Int PrintBlock(std::ostream &os,
const std::string name){
88 os << std::endl<<
"*********************************************************************" << std::endl;
89 os << name << std::endl;
90 os <<
"*********************************************************************" << std::endl << std::endl;
95 inline Int Print(std::ostream &os,
const std::string name) {
96 os << std::setiosflags(std::ios::left) << name << std::endl;
100 inline Int Print(std::ostream &os,
const char* name) {
101 os << std::setiosflags(std::ios::left) << std::string(name) << std::endl;
105 inline Int Print(std::ostream &os,
const std::string name, std::string val) {
106 os << std::setiosflags(std::ios::left)
107 << std::setw(LENGTH_VAR_NAME) << name
108 << std::setw(LENGTH_VAR_DATA) << val
113 inline Int Print(std::ostream &os,
const std::string name,
const char* val) {
114 os << std::setiosflags(std::ios::left)
115 << std::setw(LENGTH_VAR_NAME) << name
116 << std::setw(LENGTH_VAR_DATA) << std::string(val)
126 inline Int Print(std::ostream &os,
const std::string name, Real val) {
127 os << std::setiosflags(std::ios::left)
128 << std::setw(LENGTH_VAR_NAME) << name
129 << std::setiosflags(std::ios::scientific)
130 << std::setiosflags(std::ios::showpos)
131 << std::setw(LENGTH_DBL_DATA) << std::setprecision(LENGTH_DBL_PREC)<< val
132 << std::resetiosflags(std::ios::scientific)
133 << std::resetiosflags(std::ios::showpos)
138 inline Int Print(std::ostream &os,
const char* name, Real val) {
139 os << std::setiosflags(std::ios::left)
140 << std::setw(LENGTH_VAR_NAME) << std::string(name)
141 << std::setiosflags(std::ios::scientific)
142 << std::setiosflags(std::ios::showpos)
143 << std::setw(LENGTH_DBL_DATA) << std::setprecision(LENGTH_DBL_PREC)<< val
144 << std::resetiosflags(std::ios::scientific)
145 << std::resetiosflags(std::ios::showpos)
151 inline Int Print(std::ostream &os,
const std::string name, Real val,
const std::string unit) {
152 os << std::setiosflags(std::ios::left)
153 << std::setw(LENGTH_VAR_NAME) << name
154 << std::setiosflags(std::ios::scientific)
155 << std::setiosflags(std::ios::showpos)
156 << std::setw(LENGTH_DBL_DATA) << std::setprecision(LENGTH_DBL_PREC)<< val
157 << std::resetiosflags(std::ios::scientific)
158 << std::resetiosflags(std::ios::showpos)
159 << std::setw(LENGTH_VAR_UNIT) << unit
164 inline Int Print(std::ostream &os,
const char *name, Real val,
const char *unit) {
165 os << std::setiosflags(std::ios::left)
166 << std::setw(LENGTH_VAR_NAME) << std::string(name)
167 << std::setiosflags(std::ios::scientific)
168 << std::setiosflags(std::ios::showpos)
169 << std::setw(LENGTH_DBL_DATA) << std::setprecision(LENGTH_DBL_PREC)<< val
170 << std::resetiosflags(std::ios::scientific)
171 << std::resetiosflags(std::ios::showpos)
172 << std::setw(LENGTH_VAR_UNIT) << std::string(unit)
178 inline Int Print(std::ostream &os,
const std::string name1, Real val1,
const std::string unit1,
179 const std::string name2, Real val2,
const std::string unit2) {
180 os << std::setiosflags(std::ios::left)
181 << std::setw(LENGTH_VAR_NAME) << name1
182 << std::setiosflags(std::ios::scientific)
183 << std::setiosflags(std::ios::showpos)
184 << std::setw(LENGTH_VAR_DATA) << std::setprecision(LENGTH_DBL_PREC)<< val1
185 << std::setw(LENGTH_VAR_UNIT) << unit1
186 << std::setw(LENGTH_VAR_NAME) << name2
187 << std::setw(LENGTH_VAR_DATA) << std::setprecision(LENGTH_DBL_PREC)<< val2
188 << std::resetiosflags(std::ios::scientific)
189 << std::resetiosflags(std::ios::showpos)
190 << std::setw(LENGTH_VAR_UNIT) << unit2
195 inline Int Print(std::ostream &os,
const char *name1, Real val1,
const char *unit1,
196 char *name2, Real val2,
char *unit2) {
197 os << std::setiosflags(std::ios::left)
198 << std::setw(LENGTH_VAR_NAME) << std::string(name1)
199 << std::setiosflags(std::ios::scientific)
200 << std::setiosflags(std::ios::showpos)
201 << std::setw(LENGTH_VAR_DATA) << std::setprecision(LENGTH_DBL_PREC)<< val1
202 << std::setw(LENGTH_VAR_UNIT) << std::string(unit1)
203 << std::setw(LENGTH_VAR_NAME) << std::string(name2)
204 << std::setw(LENGTH_VAR_DATA) << std::setprecision(LENGTH_DBL_PREC)<< val2
205 << std::resetiosflags(std::ios::scientific)
206 << std::resetiosflags(std::ios::showpos)
207 << std::setw(LENGTH_VAR_UNIT) << std::string(unit2)
213 inline Int Print(std::ostream &os,
const std::string name1, Int val1,
const std::string unit1,
214 const std::string name2, Real val2,
const std::string unit2) {
215 os << std::setiosflags(std::ios::left)
216 << std::setw(LENGTH_VAR_NAME) << name1
217 << std::setw(LENGTH_INT_DATA) << val1
218 << std::setw(LENGTH_VAR_UNIT) << unit1
219 << std::setw(LENGTH_VAR_NAME) << name2
220 << std::setiosflags(std::ios::scientific)
221 << std::setiosflags(std::ios::showpos)
222 << std::setw(LENGTH_VAR_DATA) << std::setprecision(LENGTH_DBL_PREC)<< val2
223 << std::resetiosflags(std::ios::scientific)
224 << std::resetiosflags(std::ios::showpos)
225 << std::setw(LENGTH_VAR_UNIT) << unit2
230 inline Int Print(std::ostream &os,
const char *name1, Int val1,
const char *unit1,
231 char *name2, Real val2,
char *unit2) {
232 os << std::setiosflags(std::ios::left)
233 << std::setw(LENGTH_VAR_NAME) << std::string(name1)
234 << std::setw(LENGTH_INT_DATA) << val1
235 << std::setw(LENGTH_VAR_UNIT) << std::string(unit1)
236 << std::setw(LENGTH_VAR_NAME) << std::string(name2)
237 << std::setiosflags(std::ios::scientific)
238 << std::setiosflags(std::ios::showpos)
239 << std::setw(LENGTH_VAR_DATA) << std::setprecision(LENGTH_DBL_PREC)<< val2
240 << std::resetiosflags(std::ios::scientific)
241 << std::resetiosflags(std::ios::showpos)
242 << std::setw(LENGTH_VAR_UNIT) << std::string(unit2)
247 inline Int Print(std::ostream &os,
248 const char *name1, Int val1,
249 const char *name2, Real val2,
250 char *name3, Real val3) {
251 os << std::setiosflags(std::ios::left)
252 << std::setw(LENGTH_VAR_NAME) << std::string(name1)
253 << std::setw(LENGTH_INT_DATA) << val1
254 << std::setiosflags(std::ios::scientific)
255 << std::setiosflags(std::ios::showpos)
256 << std::setw(LENGTH_VAR_NAME) << std::string(name2)
257 << std::setw(LENGTH_VAR_DATA) << std::setprecision(LENGTH_DBL_PREC)<< val2
258 << std::setw(LENGTH_VAR_NAME) << std::string(name3)
259 << std::setw(LENGTH_VAR_DATA) << std::setprecision(LENGTH_DBL_PREC)<< val3
260 << std::resetiosflags(std::ios::scientific)
261 << std::resetiosflags(std::ios::showpos)
267 inline Int Print(std::ostream &os,
268 const char *name1, Int val1,
269 const char *name2, Real val2,
270 const char *name3, Real val3,
271 const char *name4, Real val4 ) {
272 os << std::setiosflags(std::ios::left)
273 << std::setw(LENGTH_VAR_NAME) << std::string(name1)
274 << std::setw(LENGTH_INT_DATA) << val1
275 << std::setiosflags(std::ios::scientific)
276 << std::setiosflags(std::ios::showpos)
277 << std::setw(LENGTH_VAR_NAME) << std::string(name2)
278 << std::setw(LENGTH_VAR_DATA) << std::setprecision(LENGTH_DBL_PREC)<< val2
279 << std::setw(LENGTH_VAR_NAME) << std::string(name3)
280 << std::setw(LENGTH_VAR_DATA) << std::setprecision(LENGTH_DBL_PREC)<< val3
281 << std::setw(LENGTH_VAR_NAME) << std::string(name4)
282 << std::setw(LENGTH_VAR_DATA) << std::setprecision(LENGTH_DBL_PREC)<< val4
283 << std::resetiosflags(std::ios::scientific)
284 << std::resetiosflags(std::ios::showpos)
292 inline Int Print(std::ostream &os, std::string name, Int val) {
293 os << std::setiosflags(std::ios::left)
294 << std::setw(LENGTH_VAR_NAME) << name
295 << std::setw(LENGTH_VAR_DATA) << val
301 inline Int Print(std::ostream &os,
const char *name, Int val) {
302 os << std::setiosflags(std::ios::left)
303 << std::setw(LENGTH_VAR_NAME) << std::string(name)
304 << std::setw(LENGTH_VAR_DATA) << val
310 inline Int Print(std::ostream &os,
const std::string name, Int val,
const std::string unit) {
311 os << std::setiosflags(std::ios::left)
312 << std::setw(LENGTH_VAR_NAME) << name
313 << std::setw(LENGTH_VAR_DATA) << val
314 << std::setw(LENGTH_VAR_UNIT) << unit
320 inline Int Print(std::ostream &os,
const char* name, Int val,
const std::string unit) {
321 os << std::setiosflags(std::ios::left)
322 << std::setw(LENGTH_VAR_NAME) << std::string(name)
323 << std::setw(LENGTH_VAR_DATA) << val
324 << std::setw(LENGTH_VAR_UNIT) << unit
332 inline Int Print(std::ostream &os,
const std::string name1, Int val1,
const std::string unit1,
333 const std::string name2, Int val2,
const std::string unit2) {
334 os << std::setiosflags(std::ios::left)
335 << std::setw(LENGTH_VAR_NAME) << name1
336 << std::setw(LENGTH_VAR_DATA) << val1
337 << std::setw(LENGTH_VAR_UNIT) << unit1
338 << std::setw(LENGTH_VAR_NAME) << name2
339 << std::setw(LENGTH_VAR_DATA) << val2
340 << std::setw(LENGTH_VAR_UNIT) << unit2
345 inline Int Print(std::ostream &os,
const char *name1, Int val1,
const char *unit1,
346 char *name2, Int val2,
char *unit2) {
347 os << std::setiosflags(std::ios::left)
348 << std::setw(LENGTH_VAR_NAME) << std::string(name1)
349 << std::setw(LENGTH_VAR_DATA) << val1
350 << std::setw(LENGTH_VAR_UNIT) << std::string(unit1)
351 << std::setw(LENGTH_VAR_NAME) << std::string(name2)
352 << std::setw(LENGTH_VAR_DATA) << val2
353 << std::setw(LENGTH_VAR_UNIT) << std::string(unit2)
361 inline Int Print(std::ostream &os,
const std::string name,
bool val) {
362 os << std::setiosflags(std::ios::left)
363 << std::setw(LENGTH_VAR_NAME) << name;
365 os << std::setw(LENGTH_VAR_NAME) <<
"true" << std::endl;
367 os << std::setw(LENGTH_VAR_NAME) <<
"false" << std::endl;
372 inline Int Print(std::ostream &os,
const char* name,
bool val) {
373 os << std::setiosflags(std::ios::left)
374 << std::setw(LENGTH_VAR_NAME) << std::string(name);
376 os << std::setw(LENGTH_VAR_NAME) <<
"true" << std::endl;
378 os << std::setw(LENGTH_VAR_NAME) <<
"false" << std::endl;
384 inline Int Print(std::ostream &os,
385 const char *name1, Index3 val ) {
386 os << std::setiosflags(std::ios::left)
387 << std::setw(LENGTH_VAR_NAME) << std::string(name1)
388 << std::setw(LENGTH_VAR_DATA) << val[0]
389 << std::setw(LENGTH_VAR_DATA) << val[1]
390 << std::setw(LENGTH_VAR_DATA) << val[2]
395 inline Int Print(std::ostream &os,
396 const char *name1, Point3 val ) {
397 os << std::setiosflags(std::ios::left)
398 << std::setw(LENGTH_VAR_NAME) << std::string(name1)
399 << std::setiosflags(std::ios::scientific)
400 << std::setiosflags(std::ios::showpos)
401 << std::setw(LENGTH_VAR_DATA) << std::setprecision(LENGTH_DBL_PREC) << val[0]
402 << std::setw(LENGTH_VAR_DATA) << std::setprecision(LENGTH_DBL_PREC) << val[1]
403 << std::setw(LENGTH_VAR_DATA) << std::setprecision(LENGTH_DBL_PREC) << val[2]
404 << std::resetiosflags(std::ios::scientific)
405 << std::resetiosflags(std::ios::showpos)
410 inline Int Print(std::ostream &os,
411 const char *name1, Int val1,
412 const char *name2, Point3 val ) {
413 os << std::setiosflags(std::ios::left)
414 << std::setw(LENGTH_VAR_NAME) << std::string(name1)
415 << std::setw(LENGTH_INT_DATA) << val1
416 << std::setw(LENGTH_VAR_NAME) << std::string(name2)
417 << std::setiosflags(std::ios::scientific)
418 << std::setiosflags(std::ios::showpos)
419 << std::setw(LENGTH_VAR_DATA) << std::setprecision(LENGTH_DBL_PREC) <<val[0]
420 << std::setw(LENGTH_VAR_DATA) << std::setprecision(LENGTH_DBL_PREC) <<val[1]
421 << std::setw(LENGTH_VAR_DATA) << std::setprecision(LENGTH_DBL_PREC) <<val[2]
422 << std::resetiosflags(std::ios::scientific)
423 << std::resetiosflags(std::ios::showpos)
433 template <
class F>
inline std::ostream& operator<<( std::ostream& os, const std::vector<F>& vec)
435 os<<vec.size()<<std::endl;
436 os.setf(std::ios_base::scientific, std::ios_base::floatfield);
437 for(Int i=0; i<vec.size(); i++)
444 template <
class F>
inline std::ostream& operator<<( std::ostream& os, const NumVec<F>& vec)
446 os<<vec.m()<<std::endl;
447 os.setf(std::ios_base::scientific, std::ios_base::floatfield);
448 for(Int i=0; i<vec.m(); i++)
463 template <
class F>
inline std::ostream& operator<<( std::ostream& os, const NumMat<F>& mat)
465 os<<mat.m()<<
" "<<mat.n()<<std::endl;
466 os.setf(std::ios_base::scientific, std::ios_base::floatfield);
467 os << std::setprecision(16);
468 for(Int i=0; i<mat.m(); i++) {
469 for(Int j=0; j<mat.n(); j++)
477 template <
class F>
inline std::ostream& operator<<( std::ostream& os, const NumTns<F>& tns)
479 os<<tns.m()<<
" "<<tns.n()<<
" "<<tns.p()<<std::endl;
480 os.setf(std::ios_base::scientific, std::ios_base::floatfield);
481 for(Int i=0; i<tns.m(); i++) {
482 for(Int j=0; j<tns.n(); j++) {
483 for(Int k=0; k<tns.p(); k++) {
503 template <
typename T>
504 inline Int serialize(
const T& val, std::ostream& os,
const std::vector<Int>& mask)
506 os.write((
char*)&val,
sizeof(T));
510 template <
typename T>
511 inline Int deserialize(T& val, std::istream& is,
const std::vector<Int>& mask)
513 is.read((
char*)&val,
sizeof(T));
520 inline Int serialize(
const bool& val, std::ostream& os,
const std::vector<Int>& mask)
522 os.write((
char*)&val,
sizeof(
bool));
526 inline Int deserialize(
bool& val, std::istream& is,
const std::vector<Int>& mask)
528 is.read((
char*)&val,
sizeof(
bool));
533 inline Int serialize(
const char& val, std::ostream& os,
const std::vector<Int>& mask)
535 os.write((
char*)&val,
sizeof(
char));
539 inline Int deserialize(
char& val, std::istream& is,
const std::vector<Int>& mask)
541 is.read((
char*)&val,
sizeof(
char));
545 inline Int combine(
char& val,
char& ext)
547 ErrorHandling(
"Combine operation not implemented." );
552 inline Int serialize(
const Int& val, std::ostream& os,
const std::vector<Int>& mask)
554 os.write((
char*)&val,
sizeof(Int));
558 inline Int deserialize(Int& val, std::istream& is,
const std::vector<Int>& mask)
560 is.read((
char*)&val,
sizeof(Int));
564 inline Int combine(Int& val, Int& ext)
572 inline Int serialize(
const LongInt& val, std::ostream& os,
const std::vector<Int>& mask)
574 os.write((
char*)&val,
sizeof(LongInt));
578 inline Int deserialize(LongInt& val, std::istream& is,
const std::vector<Int>& mask)
580 is.read((
char*)&val,
sizeof(LongInt));
584 inline Int combine(LongInt& val, LongInt& ext)
593 inline Int serialize(
const Real& val, std::ostream& os,
const std::vector<Int>& mask)
595 os.write((
char*)&val,
sizeof(Real));
599 inline Int deserialize(Real& val, std::istream& is,
const std::vector<Int>& mask)
601 is.read((
char*)&val,
sizeof(Real));
605 inline Int combine(Real& val, Real& ext)
613 inline Int serialize(
const Complex& val, std::ostream& os,
const std::vector<Int>& mask)
615 os.write((
char*)&val,
sizeof(Complex));
619 inline Int deserialize(Complex& val, std::istream& is,
const std::vector<Int>& mask)
621 is.read((
char*)&val,
sizeof(Complex));
625 inline Int combine(Complex& val, Complex& ext)
673 inline Int serialize(
const Index3& val, std::ostream& os,
const std::vector<Int>& mask)
675 os.write((
char*)&(val[0]), 3*
sizeof(Int));
679 inline Int deserialize(Index3& val, std::istream& is,
const std::vector<Int>& mask)
681 is.read((
char*)&(val[0]), 3*
sizeof(Int));
685 inline Int combine(Index3& val, Index3& ext)
687 ErrorHandling(
"Combine operation not implemented." );
692 inline Int serialize(
const Point3& val, std::ostream& os,
const std::vector<Int>& mask)
694 os.write((
char*)&(val[0]), 3*
sizeof(Real));
698 inline Int deserialize(Point3& val, std::istream& is,
const std::vector<Int>& mask)
700 is.read((
char*)&(val[0]), 3*
sizeof(Real));
704 inline Int combine(Point3& val, Point3& ext)
706 ErrorHandling(
"Combine operation not implemented." );
712 Int serialize(
const std::vector<T>& val, std::ostream& os,
const std::vector<Int>& mask)
715 os.write((
char*)&sz,
sizeof(Int));
716 for(Int k=0; k<sz; k++)
717 serialize(val[k], os, mask);
722 Int deserialize(std::vector<T>& val, std::istream& is,
const std::vector<Int>& mask)
725 is.read((
char*)&sz,
sizeof(Int));
727 for(Int k=0; k<sz; k++)
728 deserialize(val[k], is, mask);
733 Int combine(std::vector<T>& val, std::vector<T>& ext)
735 ErrorHandling(
"Combine operation not implemented." );
742 Int serialize(
const std::set<T>& val, std::ostream& os,
const std::vector<Int>& mask)
745 os.write((
char*)&sz,
sizeof(Int));
746 for(
typename std::set<T>::const_iterator mi=val.begin(); mi!=val.end(); mi++)
747 serialize((*mi), os, mask);
752 Int deserialize(std::set<T>& val, std::istream& is,
const std::vector<Int>& mask)
756 is.read((
char*)&sz,
sizeof(Int));
757 for(Int k=0; k<sz; k++) {
758 T t; deserialize(t, is, mask);
765 Int combine(std::set<T>& val, std::set<T>& ext)
767 ErrorHandling(
"Combine operation not implemented." );
773 template<
class T,
class S>
774 Int serialize(
const std::map<T,S>& val, std::ostream& os,
const std::vector<Int>& mask)
777 os.write((
char*)&sz,
sizeof(Int));
778 for(
typename std::map<T,S>::const_iterator mi=val.begin(); mi!=val.end(); mi++) {
779 serialize((*mi).first, os, mask);
780 serialize((*mi).second, os, mask);
785 template<
class T,
class S>
786 Int deserialize(std::map<T,S>& val, std::istream& is,
const std::vector<Int>& mask)
790 is.read((
char*)&sz,
sizeof(Int));
791 for(Int k=0; k<sz; k++) {
792 T t; deserialize(t, is, mask);
793 S s; deserialize(s, is, mask);
799 template<
class T,
class S>
800 Int combine(std::map<T,S>& val, std::map<T,S>& ext)
802 ErrorHandling(
"Combine operation not implemented." );
808 template<
class T,
class S>
809 Int serialize(
const std::pair<T,S>& val, std::ostream& os,
const std::vector<Int>& mask)
811 serialize(val.first, os, mask);
812 serialize(val.second, os, mask);
816 template<
class T,
class S>
817 Int deserialize(std::pair<T,S>& val, std::istream& is,
const std::vector<Int>& mask)
819 deserialize(val.first, is, mask);
820 deserialize(val.second, is, mask);
824 template<
class T,
class S>
825 Int combine(std::pair<T,S>& val, std::pair<T,S>& ext)
827 ErrorHandling(
"Combine operation not implemented." );
900 inline Int serialize(
const IntNumVec& val, std::ostream& os,
const std::vector<Int>& mask)
903 os.write((
char*)&m,
sizeof(Int));
904 os.write((
char*)(val.Data()), m*
sizeof(Int));
908 inline Int deserialize(IntNumVec& val, std::istream& is,
const std::vector<Int>& mask)
911 is.read((
char*)&m,
sizeof(Int));
913 is.read((
char*)(val.Data()), m*
sizeof(Int));
917 inline Int combine(IntNumVec& val, IntNumVec& ext)
920 assert(val.m()==ext.m());
921 for(Int i=0; i<val.m(); i++) val(i) += ext(i);
928 inline Int serialize(
const IntNumMat& val, std::ostream& os,
const std::vector<Int>& mask)
932 os.write((
char*)&m,
sizeof(Int));
933 os.write((
char*)&n,
sizeof(Int));
934 os.write((
char*)(val.Data()), m*n*
sizeof(Int));
938 inline Int deserialize(IntNumMat& val, std::istream& is,
const std::vector<Int>& mask)
942 is.read((
char*)&m,
sizeof(Int));
943 is.read((
char*)&n,
sizeof(Int));
945 is.read((
char*)(val.Data()), m*n*
sizeof(Int));
949 inline Int combine(IntNumMat& val, IntNumMat& ext)
952 assert(val.m()==ext.m() && val.n()==ext.n());
953 for(Int i=0; i<val.m(); i++)
954 for(Int j=0; j<val.n(); j++)
955 val(i,j) += ext(i,j);
961 inline Int serialize(
const IntNumTns& val, std::ostream& os,
const std::vector<Int>& mask)
963 Int m = val.m(); Int n = val.n(); Int p = val.p();
964 os.write((
char*)&m,
sizeof(Int));
965 os.write((
char*)&n,
sizeof(Int));
966 os.write((
char*)&p,
sizeof(Int));
967 os.write((
char*)(val.Data()), m*n*p*
sizeof(Int));
971 inline Int deserialize(IntNumTns& val, std::istream& is,
const std::vector<Int>& mask)
974 is.read((
char*)&m,
sizeof(Int));
975 is.read((
char*)&n,
sizeof(Int));
976 is.read((
char*)&p,
sizeof(Int));
978 is.read((
char*)(val.Data()), m*n*p*
sizeof(Int));
982 inline Int combine(IntNumTns& val, IntNumTns& ext)
985 assert(val.m()==ext.m() && val.n()==ext.n() && val.p()==ext.p());
986 for(Int i=0; i<val.m(); i++)
987 for(Int j=0; j<val.n(); j++)
988 for(Int k=0; k<val.p(); k++)
989 val(i,j,k) += ext(i,j,k);
995 inline Int serialize(
const DblNumVec& val, std::ostream& os,
const std::vector<Int>& mask)
998 os.write((
char*)&m,
sizeof(Int));
999 os.write((
char*)(val.Data()), m*
sizeof(Real));
1003 inline Int deserialize(DblNumVec& val, std::istream& is,
const std::vector<Int>& mask)
1006 is.read((
char*)&m,
sizeof(Int));
1008 is.read((
char*)(val.Data()), m*
sizeof(Real));
1012 inline Int combine(DblNumVec& val, DblNumVec& ext)
1015 assert(val.m()==ext.m());
1016 for(Int i=0; i<val.m(); i++) val(i) += ext(i);
1022 inline Int serialize(
const DblNumMat& val, std::ostream& os,
const std::vector<Int>& mask)
1026 os.write((
char*)&m,
sizeof(Int));
1027 os.write((
char*)&n,
sizeof(Int));
1028 os.write((
char*)(val.Data()), m*n*
sizeof(Real));
1032 inline Int deserialize(DblNumMat& val, std::istream& is,
const std::vector<Int>& mask)
1036 is.read((
char*)&m,
sizeof(Int));
1037 is.read((
char*)&n,
sizeof(Int));
1039 is.read((
char*)(val.Data()), m*n*
sizeof(Real));
1043 inline Int combine(DblNumMat& val, DblNumMat& ext)
1046 assert(val.m()==ext.m() && val.n()==ext.n());
1047 for(Int i=0; i<val.m(); i++)
1048 for(Int j=0; j<val.n(); j++)
1049 val(i,j) += ext(i,j);
1056 inline Int serialize(
const DblNumTns& val, std::ostream& os,
const std::vector<Int>& mask)
1058 Int m = val.m(); Int n = val.n(); Int p = val.p();
1059 os.write((
char*)&m,
sizeof(Int));
1060 os.write((
char*)&n,
sizeof(Int));
1061 os.write((
char*)&p,
sizeof(Int));
1062 os.write((
char*)(val.Data()), m*n*p*
sizeof(Real));
1066 inline Int deserialize(DblNumTns& val, std::istream& is,
const std::vector<Int>& mask)
1069 is.read((
char*)&m,
sizeof(Int));
1070 is.read((
char*)&n,
sizeof(Int));
1071 is.read((
char*)&p,
sizeof(Int));
1073 is.read((
char*)(val.Data()), m*n*p*
sizeof(Real));
1077 inline Int combine(DblNumTns& val, DblNumTns& ext)
1080 assert(val.m()==ext.m() && val.n()==ext.n() && val.p()==ext.p());
1081 for(Int i=0; i<val.m(); i++)
1082 for(Int j=0; j<val.n(); j++)
1083 for(Int k=0; k<val.p(); k++)
1084 val(i,j,k) += ext(i,j,k);
1090 inline Int serialize(
const CpxNumVec& val, std::ostream& os,
const std::vector<Int>& mask)
1093 os.write((
char*)&m,
sizeof(Int));
1094 os.write((
char*)(val.Data()), m*
sizeof(Complex));
1098 inline Int deserialize(CpxNumVec& val, std::istream& is,
const std::vector<Int>& mask)
1101 is.read((
char*)&m,
sizeof(Int));
1103 is.read((
char*)(val.Data()), m*
sizeof(Complex));
1107 inline Int combine(CpxNumVec& val, CpxNumVec& ext)
1110 assert(val.m()==ext.m());
1111 for(Int i=0; i<val.m(); i++) val(i) += ext(i);
1117 inline Int serialize(
const CpxNumMat& val, std::ostream& os,
const std::vector<Int>& mask)
1121 os.write((
char*)&m,
sizeof(Int));
1122 os.write((
char*)&n,
sizeof(Int));
1123 os.write((
char*)(val.Data()), m*n*
sizeof(Complex));
1127 inline Int deserialize(CpxNumMat& val, std::istream& is,
const std::vector<Int>& mask)
1131 is.read((
char*)&m,
sizeof(Int));
1132 is.read((
char*)&n,
sizeof(Int));
1134 is.read((
char*)(val.Data()), m*n*
sizeof(Complex));
1138 inline Int combine(CpxNumMat& val, CpxNumMat& ext)
1141 assert(val.m()==ext.m() && val.n()==ext.n());
1142 for(Int i=0; i<val.m(); i++)
1143 for(Int j=0; j<val.n(); j++)
1144 val(i,j) += ext(i,j);
1150 inline Int serialize(
const CpxNumTns& val, std::ostream& os,
const std::vector<Int>& mask)
1152 Int m = val.m(); Int n = val.n(); Int p = val.p();
1153 os.write((
char*)&m,
sizeof(Int));
1154 os.write((
char*)&n,
sizeof(Int));
1155 os.write((
char*)&p,
sizeof(Int));
1156 os.write((
char*)(val.Data()), m*n*p*
sizeof(Complex));
1160 inline Int deserialize(CpxNumTns& val, std::istream& is,
const std::vector<Int>& mask)
1163 is.read((
char*)&m,
sizeof(Int));
1164 is.read((
char*)&n,
sizeof(Int));
1165 is.read((
char*)&p,
sizeof(Int));
1167 is.read((
char*)(val.Data()), m*n*p*
sizeof(Complex));
1171 inline Int combine(CpxNumTns& val, CpxNumTns& ext)
1174 assert(val.m()==ext.m() && val.n()==ext.n() && val.p()==ext.p());
1175 for(Int i=0; i<val.m(); i++)
1176 for(Int j=0; j<val.n(); j++)
1177 for(Int k=0; k<val.p(); k++)
1178 val(i,j,k) += ext(i,j,k);
1185 Int
inline serialize(
const NumVec<T>& val, std::ostream& os,
const std::vector<Int>& mask)
1188 os.write((
char*)&m,
sizeof(Int));
1189 for(Int i=0; i<m; i++)
1190 serialize(val(i), os, mask);
1195 Int
inline deserialize(NumVec<T>& val, std::istream& is,
const std::vector<Int>& mask)
1198 is.read((
char*)&m,
sizeof(Int));
1200 for(Int i=0; i<m; i++)
1201 deserialize(val(i), is, mask);
1206 Int
inline combine(NumVec<T>& val, NumVec<T>& ext)
1208 ErrorHandling(
"Combine operation not implemented." );
1215 Int
inline serialize(
const NumMat<T>& val, std::ostream& os,
const std::vector<Int>& mask)
1219 os.write((
char*)&m,
sizeof(Int));
1220 os.write((
char*)&n,
sizeof(Int));
1221 for(Int j=0; j<n; j++)
1222 for(Int i=0; i<m; i++)
1223 serialize(val(i,j), os, mask);
1227 Int
inline deserialize(NumMat<T>& val, std::istream& is,
const std::vector<Int>& mask)
1231 is.read((
char*)&m,
sizeof(Int));
1232 is.read((
char*)&n,
sizeof(Int));
1234 for(Int j=0; j<n; j++)
1235 for(Int i=0; i<m; i++)
1236 deserialize(val(i,j), is, mask);
1241 Int
inline combine(NumMat<T>& val, NumMat<T>& ext)
1243 ErrorHandling(
"Combine operation not implemented." );
1251 Int
inline serialize(
const NumTns<T>& val, std::ostream& os,
const std::vector<Int>& mask)
1256 os.write((
char*)&m,
sizeof(Int));
1257 os.write((
char*)&n,
sizeof(Int));
1258 os.write((
char*)&p,
sizeof(Int));
1259 for(Int k=0; k<p; k++)
1260 for(Int j=0; j<n; j++)
1261 for(Int i=0; i<m; i++)
1262 serialize(val(i,j,k), os, mask);
1267 Int
inline deserialize(NumTns<T>& val, std::istream& is,
const std::vector<Int>& mask)
1272 is.read((
char*)&m,
sizeof(Int));
1273 is.read((
char*)&n,
sizeof(Int));
1274 is.read((
char*)&p,
sizeof(Int));
1276 for(Int k=0; k<p; k++)
1277 for(Int j=0; j<n; j++)
1278 for(Int i=0; i<m; i++)
1279 deserialize(val(i,j,k), is, mask);
1284 Int
inline combine(NumTns<T>& val, NumTns<T>& ext)
1286 ErrorHandling(
"Combine operation not implemented." );
1293 Int
inline serialize(
const DistSparseMatrix<T>& val, std::ostream& os,
const std::vector<Int>& mask)
1295 serialize( val.size, os, mask );
1296 serialize( val.nnz, os, mask );
1297 serialize( val.nnzLocal, os, mask );
1298 serialize( val.colptrLocal, os, mask );
1299 serialize( val.rowindLocal, os, mask );
1300 serialize( val.nzvalLocal, os, mask );
1306 Int
inline deserialize(DistSparseMatrix<T>& val, std::istream& is,
const std::vector<Int>& mask)
1308 deserialize( val.size, is, mask );
1309 deserialize( val.nnz, is, mask );
1310 deserialize( val.nnzLocal, is, mask );
1311 deserialize( val.colptrLocal, is, mask );
1312 deserialize( val.rowindLocal, is, mask );
1313 deserialize( val.nzvalLocal, is, mask );
1320 Int
inline serialize(
const symPACK::DistSparseMatrixGraph & val, std::ostream& os,
const std::vector<Int>& mask)
1322 serialize( val.size, os, mask );
1323 serialize( val.nnz, os, mask );
1324 serialize( val.bIsExpanded, os, mask );
1325 serialize( val.mpirank, os, mask );
1326 serialize( val.mpisize, os, mask );
1327 serialize( val.baseval, os, mask );
1328 serialize( val.keepDiag, os, mask );
1329 serialize( val.sorted, os, mask );
1330 serialize( val.vertexDist, os, mask );
1331 serialize( val.colptr, os, mask );
1332 serialize( val.rowind, os, mask );
1337 Int
inline deserialize(symPACK::DistSparseMatrixGraph& val, std::istream& is,
const std::vector<Int>& mask)
1339 deserialize( val.size, is, mask );
1340 deserialize( val.nnz, is, mask );
1341 deserialize( val.bIsExpanded, is, mask );
1342 deserialize( val.mpirank, is, mask );
1343 deserialize( val.mpisize, is, mask );
1344 deserialize( val.baseval, is, mask );
1345 deserialize( val.keepDiag, is, mask );
1346 deserialize( val.sorted, is, mask );
1347 deserialize( val.vertexDist, is, mask );
1348 deserialize( val.colptr, is, mask );
1349 deserialize( val.rowind, is, mask );
1355 Int
inline serialize(
const symPACK::DistSparseMatrix<T>& val, std::ostream& os,
const std::vector<Int>& mask)
1357 serialize( val.size, os, mask );
1358 serialize( val.nnz, os, mask );
1359 serialize( val.GetLocalGraph(), os, mask );
1360 serialize( val.nzvalLocal, os, mask );
1366 Int
inline deserialize(symPACK::DistSparseMatrix<T>& val, std::istream& is,
const std::vector<Int>& mask)
1368 deserialize( val.size, is, mask );
1369 deserialize( val.nnz, is, mask );
1370 deserialize( val.GetLocalGraph(), is, mask );
1371 deserialize( val.nzvalLocal, is, mask );
1377 void inline Convert(
const DistSparseMatrix<T>& A, symPACK::DistSparseMatrix<T> & B)
1379 const MPI_Comm & comm = A.comm;
1380 int mpisize,mpirank;
1381 MPI_Comm_size(comm,&mpisize);
1382 MPI_Comm_rank(comm,&mpirank);
1389 symPACK::DistSparseMatrixGraph & BGraph = B.GetLocalGraph();
1395 BGraph.size = B.size;
1397 BGraph.SetComm(B.comm);
1398 BGraph.bIsExpanded =
true;
1400 BGraph.keepDiag = 1;
1402 BGraph.colptr.resize(BGraph.LocalVertexCount()+1);
1403 symPACK::bassert(A.colptrLocal.m()== BGraph.colptr.size());
1404 std::copy(A.colptrLocal.Data(),A.colptrLocal.Data()+BGraph.LocalVertexCount()+1,BGraph.colptr.data());
1407 BGraph.rowind.resize(A.nnzLocal);
1408 std::copy(A.rowindLocal.Data(),A.rowindLocal.Data()+A.nnzLocal,BGraph.rowind.data());
1410 B.nzvalLocal.resize(A.nzvalLocal.m());
1411 std::copy(A.nzvalLocal.Data(),A.nzvalLocal.Data()+A.nzvalLocal.m(),B.nzvalLocal.data());
1416 void inline Convert(symPACK::DistSparseMatrix<T>& B, DistSparseMatrix<T> & A)
1419 MPI_Comm & comm = B.comm;
1420 int mpisize,mpirank;
1421 MPI_Comm_size(comm,&mpisize);
1422 MPI_Comm_rank(comm,&mpirank);
1423 bool wasExpanded = B.GetLocalGraph().bIsExpanded;
1425 B.ExpandSymmetric();
1427 B.GetLocalGraph().SetBaseval(1);
1431 Int colPerProc = B.size / mpisize;
1432 if(mpirank==mpisize-1){ colPerProc = B.size - mpirank*colPerProc;}
1433 symPACK::bassert(B.GetLocalGraph().LocalVertexCount() == colPerProc);
1438 A.colptrLocal.Resize(B.GetLocalGraph().colptr.size());
1439 std::copy(B.GetLocalGraph().colptr.begin(), B.GetLocalGraph().colptr.end(), &A.colptrLocal[0]);
1440 A.rowindLocal.Resize(B.GetLocalGraph().rowind.size());
1441 std::copy(B.GetLocalGraph().rowind.begin(), B.GetLocalGraph().rowind.end(), &A.rowindLocal[0]);
1442 A.nnzLocal = B.GetLocalGraph().rowind.size();
1444 A.nzvalLocal.Resize(B.nzvalLocal.size());
1445 std::copy(B.nzvalLocal.begin(),B.nzvalLocal.end(),A.nzvalLocal.Data());
1448 B.ToLowerTriangular();
1460 Int
inline combine(DistSparseMatrix<T>& val, DistSparseMatrix<T>& ext)
1462 ErrorHandling(
"Combine operation not implemented." );
1471 Int SeparateRead(std::string name, std::istringstream& is);
1473 Int SeparateWrite(std::string name, std::ostringstream& os);
1475 Int SeparateWriteAscii(std::string name, std::ostringstream& os);
1477 Int SharedRead(std::string name, std::istringstream& is);
1479 Int SharedWrite(std::string name, std::ostringstream& os);
1484 inline void IdentityCol( Int col, NumVec<Real>& vec )
1486 for(Int i=0; i<std::min(col,vec.m()); i++)
1493 for(Int i=col+1; i<vec.m(); i++)
1497 inline void IdentityCol( Int col, NumVec<Complex>& vec )
1499 for(Int i=0; i<std::min(col,vec.m()); i++)
1500 vec(i) = Complex(0.0,0.0);
1503 vec(col) = Complex(1.0,0.0);
1506 for(Int i=col+1; i<vec.m(); i++)
1507 vec(i) = Complex(0.0,0.0);
1512 inline void IdentityCol( IntNumVec & cols, NumMat<Real>& mat )
1514 for(Int j=0;j<cols.m();j++){
1516 for(Int i=0; i<std::min(col,mat.m()); i++)
1523 for(Int i=col+1; i<mat.m(); i++)
1529 inline void IdentityCol( IntNumVec & cols, NumMat<Complex>& mat )
1531 for(Int j=0;j<cols.m();j++){
1533 for(Int i=0; i<std::min(col,mat.m()); i++)
1534 mat(i,j) = Complex(0.0,0.0);
1537 mat(col,j) = Complex(1.0,0.0);
1540 for(Int i=col+1; i<mat.m(); i++)
1541 mat(i,j) = Complex(0.0,0.0);
1551 inline void SetRandomSeed(
long int seed){
1555 inline Real UniformRandom(){
1556 return (Real)drand48();
1559 inline void UniformRandom( NumVec<Real>& vec )
1561 for(Int i=0; i<vec.m(); i++)
1562 vec(i) = UniformRandom();
1565 inline void UniformRandom( NumVec<Complex>& vec )
1567 for(Int i=0; i<vec.m(); i++)
1568 vec(i) = Complex(UniformRandom(), UniformRandom());
1571 inline void UniformRandom( NumMat<Real>& M )
1573 Real *ptr = M.Data();
1574 for(Int i=0; i < M.m() * M.n(); i++)
1575 *(ptr++) = UniformRandom();
1578 inline void UniformRandom( NumMat<Complex>& M )
1580 Complex *ptr = M.Data();
1581 for(Int i=0; i < M.m() * M.n(); i++)
1582 *(ptr++) = Complex(UniformRandom(), UniformRandom());
1586 inline void UniformRandom( NumTns<Real>& T )
1588 Real *ptr = T.Data();
1589 for(Int i=0; i < T.m() * T.n() * T.p(); i++)
1590 *(ptr++) = UniformRandom();
1593 inline void UniformRandom( NumTns<Complex>& T )
1595 Complex *ptr = T.Data();
1596 for(Int i=0; i < T.m() * T.n() * T.p(); i++)
1597 *(ptr++) = Complex(UniformRandom(), UniformRandom());
1603 inline void GetTime(Real& t){
1612 inline bool PairLtComparator(
const std::pair<Real, Int>& l,
1613 const std::pair<Real, Int>& r ){
1614 return l.first < r.first;
1617 inline bool PairGtComparator(
const std::pair<Real, Int>& l,
1618 const std::pair<Real, Int>& r ){
1619 return l.first > r.first;
1630 IndexComp (
const T indices) : indices_(indices) {}
1631 bool operator()(
const size_t a,
const size_t b)
const
1632 {
return indices_[a] < indices_[b]; }
1653 void ReadDistSparseMatrixFormatted(
const char* filename,
DistSparseMatrix<Real>& pspmat, MPI_Comm comm );
1663 template <
class F1,
class F2>
1669 B.colptr = A.colptr;
1670 B.rowind = A.rowind;
1671 B.nzval.Resize( A.nnz );
1677 template<
typename T>
1679 GetDiagonal (
const DistSparseMatrix<T>& A,
1685 template <
class F1,
class F2>
1687 CopyPattern (
const DistSparseMatrix<F1>& A, DistSparseMatrix<F2>& B )
1691 B.nnzLocal = A.nnzLocal;
1692 B.colptrLocal = A.colptrLocal;
1693 B.rowindLocal = A.rowindLocal;
1694 B.nzvalLocal.Resize( A.nnzLocal );
1700 template <
class F1,
class F2>
1702 CopyPattern (
const symPACK::DistSparseMatrix<F1>& A, symPACK::DistSparseMatrix<F2>& B )
1706 const symPACK::DistSparseMatrixGraph & Agraph = A.GetLocalGraph();
1707 symPACK::DistSparseMatrixGraph & Bgraph = B.GetLocalGraph();
1710 B.nzvalLocal.resize( Bgraph.LocalEdgeCount() );
1743 if( B.m() != AMat.
size ){
1744 std::ostringstream msg;
1746 <<
"The size of the matrix A is " << AMat.
size << std::endl
1747 <<
"The size of the vector B is " << B.m() << std::endl
1748 <<
"and they must agree with each other.";
1749 ErrorHandling( msg.str().c_str() );
1751 if( C.m() != AMat.
size ){
1752 std::ostringstream msg;
1754 <<
"The size of the matrix A is " << AMat.
size << std::endl
1755 <<
"The size of the vector C is " << C.m() << std::endl
1756 <<
"and they must agree with each other.";
1757 ErrorHandling( msg.str().c_str() );
1760 MPI_Comm comm = AMat.
comm;
1762 Int mpirank, mpisize;
1763 MPI_Comm_rank( comm, &mpirank );
1764 MPI_Comm_size( comm, &mpisize );
1776 Int numColLocalFirst = AMat.
size / mpisize;
1777 Int firstCol = mpirank * numColLocalFirst;
1781 for( Int j = 0; j < numColLocal; j++ ){
1782 Int jcol = firstCol + j;
1786 DeltaClocal( irow ) += AMat.
nzvalLocal(i) * B(jcol);
1790 mpi::Allreduce( DeltaClocal.Data(), DeltaC.Data(),
1791 AMat.
size, MPI_SUM, comm );
1793 for( Int i = 0; i < AMat.
size; i++ ){
1794 C(i) = beta * C(i) + alpha * DeltaC(i);
1815 const std::vector<Real>& x,
1816 const std::vector<Real>& y,
1817 const std::vector<Real>& xx,
1818 std::vector<Real>& yy );
1837 const std::vector<Real>& x,
1838 const std::vector<Real>& y,
1841 Int numX = x.size();
1843 if( val <= y[0] || val >= y[numX-1] ){
1844 std::ostringstream msg;
1846 <<
"The root finding procedure cannot find the solution for y(x)="
1847 << val << std::endl <<
"here [min(y) max(y)] = [" << y[0] <<
", "
1848 << y[numX-1] <<
"]" << std::endl;
1849 ErrorHandling( msg.str().c_str() );
1852 std::vector<Real>::const_iterator vi = std::lower_bound(
1853 y.begin(), y.end(), val );
1855 Int idx = vi - y.begin();
1858 if( y[idx] == y[idx-1] ){
1863 root = x[idx-1] + ( x[idx] - x[idx-1] ) / ( y[idx] - y[idx-1] )
1864 * ( val - y[idx-1] );
1879 #endif // _PEXSI_UTILITY_HPP_
Numerical vector.
Definition: NumVec.hpp:60
IntNumVec colptrLocal
Dimension numColLocal + 1, storing the pointers to the nonzero row indices and nonzero values in rowp...
Definition: sparse_matrix.hpp:108
void SetValue(NumMat< F > &M, F val)
SetValue sets a numerical matrix to a constant val.
Definition: NumMat_impl.hpp:153
Implementation of utility subroutines.
NumVec< F > nzvalLocal
Dimension nnzLocal, storing the nonzero values.
Definition: sparse_matrix.hpp:116
SparseMatrix describes a sequential sparse matrix saved in compressed sparse column format...
Definition: sparse_matrix.hpp:66
IntNumVec rowindLocal
Dimension nnzLocal, storing the nonzero row indices. The indices are 1-based (FORTRAN-convention), i.e. the first row index is 1.
Definition: sparse_matrix.hpp:113
Sparse matrix and Distributed sparse matrix in compressed column format.
Interface with MPI to facilitate communication.
Int size
Matrix dimension.
Definition: sparse_matrix.hpp:93
void DistSparseMatMultGlobalVec(const F alpha, const DistSparseMatrix< F > &AMat, const NumVec< F > &B, const F beta, NumVec< F > &C)
Multiply a DistSparseMatrix with a vector that is distributed across all processors participating the...
Definition: utility.hpp:1736
MPI_Comm comm
MPI communicator.
Definition: sparse_matrix.hpp:119
Tiny vectors of dimension 3.
Real MonotoneRootFinding(const std::vector< Real > &x, const std::vector< Real > &y, Real val)
Root finding for monotonic non-decreasing functions.
Definition: utility.hpp:1836
Definition: utility.hpp:1626
void LinearInterpolation(const std::vector< Real > &x, const std::vector< Real > &y, const std::vector< Real > &xx, std::vector< Real > &yy)
Linear interpolates from (x,y) to (xx,yy)
Definition: utility.cpp:180
DistSparseMatrix describes a Sparse matrix in the compressed sparse column format (CSC) and distribut...
Definition: sparse_matrix.hpp:91