/usr/include/madness/mra/vmra1.h is in libmadness-dev 0.10.1~gite4aa500e-10.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 | /*
This file is part of MADNESS.
Copyright (C) 2007,2010 Oak Ridge National Laboratory
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
For more information please contact:
Robert J. Harrison
Oak Ridge National Laboratory
One Bethel Valley Road
P.O. Box 2008, MS-6367
email: harrisonrj@ornl.gov
tel: 865-241-3937
fax: 865-572-0680
$Id$
*/
#ifndef MADNESS_MRA_VMRA_H__INCLUDED
#define MADNESS_MRA_VMRA_H__INCLUDED
/*!
\file vmra.h
\brief Defines operations on vectors of Functions
\ingroup mra
This file defines a number of operations on vectors of functions.
Assume v is a vector of NDIM-D functions of a certain type.
Operations on array of functions
*) copying: deep copying of vectors of functions to vector of functions
\code
vector2 = copy(world, vector1,fence);
\endcode
*) compress: convert multiwavelet representation to legendre representation
\code
compress(world, vector, fence);
\endcode
*) reconstruct: convert representation to multiwavelets
\code
reconstruct(world, vector, fence);
\endcode
*) nonstandard: convert to non-standard form
\code
nonstandard(world, v, fence);
\endcode
*) standard: convert to standard form
\code
standard(world, v, fence);
\endcode
*) truncate: truncating vectors of functions to desired precision
\code
truncate(world, v, tolerance, fence);
\endcode
*) zero function: create a vector of zero functions of length n
\code
v=zero(world, n);
\endcode
*) transform: transform a representation from one basis to another
\code
transform(world, vector, tensor, tolerance, fence )
\endcode
Setting thresh-hold for precision
*) set_thresh: setting a finite thresh-hold for a vector of functions
\code
void set_thresh(World& world, std::vector< Function<T,NDIM> >& v, double thresh, bool fence=true);
\endcode
Arithmetic Operations on arrays of functions
*) conjugation: conjugate a vector of complex functions
*) add
*) sub
*) mul
- mul_sparse
*) square
*) gaxpy
*) apply
Norms, inner-products, blas-1 like operations on vectors of functions
*) inner
*) matrix_inner
*) norm_tree
*) normalize
*) norm2
- norm2s
*) scale(world, v, alpha);
*/
#include <madness/mra/mra.h>
#include <madness/mra/derivative.h>
#include <cstdio>
namespace madness {
/// Compress a vector of functions
template <typename T, std::size_t NDIM>
void compress(World& world,
const std::vector< Function<T,NDIM> >& v,
unsigned int blk=1,
bool fence=true){
PROFILE_BLOCK(Vcompress);
bool must_fence = false;
unsigned int vvsize = v.size();
for (unsigned int i=0; i<vvsize; i+= blk) {
for (unsigned int j=i; j<std::min(vvsize,(i+1)*blk); ++j) {
if (!v[j].is_compressed()) {
v[j].compress(false);
must_fence = true;
}
}
if ( blk!=1 && must_fence && fence) world.gop.fence();
}
if (fence && must_fence) world.gop.fence();
}
/// Reconstruct a vector of functions
template <typename T, std::size_t NDIM>
void reconstruct(World& world,
const std::vector< Function<T,NDIM> >& v,
unsigned int blk=1,
bool fence=true){ // reconstr
PROFILE_BLOCK(Vreconstruct);
bool must_fence = false;
unsigned int vvsize = v.size();
for (unsigned int i=0; i<vvsize; i+= blk) {
for (unsigned int j=i; j<std::min(vvsize,(i+1)*blk); ++j) {
if (v[j].is_compressed()) {
v[j].reconstruct(false);
must_fence = true;
}
}
if ( blk!=1 && must_fence && fence) world.gop.fence();
}
if (fence && must_fence) world.gop.fence();
} // reconstr
/// Generates non-standard form of a vector of functions
template <typename T, std::size_t NDIM>
void nonstandard(World& world,
std::vector< Function<T,NDIM> >& v,
unsigned int blk=1,
bool fence=true) { // nonstand
PROFILE_BLOCK(Vnonstandard);
unsigned int vvsize = v.size();
reconstruct(world, v, blk);
for (unsigned int i=0; i<vvsize; i+= blk) {
for (unsigned int j=i; j<std::min(vvsize,(i+1)*blk); ++j) {
v[j].nonstandard(false,false);
}
if ( blk!=1 && fence) world.gop.fence();
}
if (fence) world.gop.fence();
} //nonstand
/// Generates standard form of a vector of functions
template <typename T, std::size_t NDIM>
void standard(World& world,
std::vector< Function<T,NDIM> >& v,
unsigned int blk=1,
bool fence=true){ // standard
PROFILE_BLOCK(Vstandard);
unsigned int vvsize = v.size();
for (unsigned int i=0; i<vvsize; i+= blk) {
for (unsigned int j=i; j<std::min(vvsize,(i+1)*blk); ++j) {
v[j].standard(false);
}
if ( blk!=1 && fence) world.gop.fence();
}
if (fence) world.gop.fence();
} // standard
/// Truncates a vector of functions
template <typename T, std::size_t NDIM>
void truncate(World& world,
std::vector< Function<T,NDIM> >& v,
double tol=0.0,
unsigned int blk=1,
bool fence=true){ // truncate
PROFILE_BLOCK(Vtruncate);
compress(world, v, blk);
unsigned int vvsize = v.size();
for (unsigned int i=0; i<vvsize; i+= blk) {
for (unsigned int j=i; j<std::min(vvsize,(i+1)*blk); ++j) {
v[j].truncate(tol, false);
}
if ( blk!=1 && fence) world.gop.fence();
}
if (fence) world.gop.fence();
} //truncate
/// Applies a derivative operator to a vector of functions
template <typename T, std::size_t NDIM>
std::vector< Function<T,NDIM> >
apply(World& world,
const Derivative<T,NDIM>& D,
const std::vector< Function<T,NDIM> >& v,
const unsigned int blk=1,
const bool fence=true)
{
reconstruct(world, v, blk);
std::vector< Function<T,NDIM> > df(v.size());
unsigned int vvsize = v.size();
for (unsigned int i=0; i<vvsize; i+= blk) {
for (unsigned int j=i; j<std::min(vvsize,(i+1)*blk); ++j) {
df[j] = D(v[j],false);
}
if (blk!= 1 && fence) world.gop.fence();
}
if (fence) world.gop.fence();
return df;
}
/// Generates a vector of zero functions
template <typename T, std::size_t NDIM>
std::vector< Function<T,NDIM> >
zero_functions(World& world, int n) {
PROFILE_BLOCK(Vzero_functions);
std::vector< Function<T,NDIM> > r(n);
for (int i=0; i<n; ++i)
r[i] = Function<T,NDIM>(FunctionFactory<T,NDIM>(world));
return r;
}
/// Transforms a vector of functions according to new[i] = sum[j] old[j]*c[j,i]
/// Uses sparsity in the transformation matrix --- set small elements to
/// zero to take advantage of this.
template <typename T, typename R, std::size_t NDIM>
std::vector< Function<TENSOR_RESULT_TYPE(T,R),NDIM> >
transform(World& world,
const std::vector< Function<T,NDIM> >& v,
const Tensor<R>& c,
unsigned int blki=1,
unsigned int blkj=1,
const bool fence=true){
PROFILE_BLOCK(Vtransformsp);
typedef TENSOR_RESULT_TYPE(T,R) resultT;
unsigned int blk = min(blki, blkj);
unsigned int n = v.size(); // n is the old dimension
unsigned int m = c.dim(1); // m is the new dimension
MADNESS_ASSERT(n==c.dim(0));
std::vector< Function<resultT,NDIM> > vc = zero_functions_compressed<resultT,NDIM>(world, m);
compress(world, v, blk);
for (unsigned int i=0; i<m; i+= blki) {
for (unsigned int ii=i; ii<std::min(m,(i+1)*blki); ii++) {
for (unsigned int j=0; j<n; j+= blkj) {
for (unsigned int jj=j; jj<std::min(n, (j+1)*blkj); jj++)
if (c(jj,ii) != R(0.0)) vc[ii].gaxpy(1.0,v[jj],c(jj,ii),false);
if (fence && (blkj!=1)) world.gop.fence();
}
}
if (fence && (blki!=1)) world.gop.fence(); // a bit conservative
}
// for (unsigned int i=0; i<m; ++i) {
// for (unsigned int j=0; j<n; ++j) {
// if (c(j,i) != R(0.0)) vc[i].gaxpy(1.0,v[j],c(j,i),false);
// }
// }
if (fence) world.gop.fence();
return vc;
}
template <typename L, typename R, std::size_t NDIM>
std::vector< Function<TENSOR_RESULT_TYPE(L,R),NDIM> >
transform(World& world,
const std::vector< Function<L,NDIM> >& v,
const Tensor<R>& c,
const double tol,
const unsigned int blki=1,
const bool fence)
{
PROFILE_BLOCK(Vtransform);
MADNESS_ASSERT(v.size() == (unsigned int)(c.dim(0)));
std::vector< Function<TENSOR_RESULT_TYPE(L,R),NDIM> > vresult(c.dim(1));
unsigned int m=c.dim(1);
for (unsigned int i=0; i<m; i+= blki) {
for (unsigned int ii=i; ii<std::min(m,(i+1)*blki); ii++) {
vresult[ii] = Function<TENSOR_RESULT_TYPE(L,R),NDIM>(FunctionFactory<TENSOR_RESULT_TYPE(L,R),NDIM>(world));
}
if (fence && (blki!=1)) world.gop.fence(); // a bit conservative
}
// for (unsigned int i=0; i<c.dim(1); ++i) {
// vresult[i] = Function<TENSOR_RESULT_TYPE(L,R),NDIM>(FunctionFactory<TENSOR_RESULT_TYPE(L,R),NDIM>(world));
// }
compress(world, v, blki, false);
compress(world, vresult, blki, false);
world.gop.fence();
vresult[0].vtransform(v, c, vresult, tol, fence);
return vresult;
}
/// Scales inplace a vector of functions by distinct values
template <typename T, typename Q, std::size_t NDIM>
void scale(World& world,
std::vector< Function<T,NDIM> >& v,
const std::vector<Q>& factors,
const unsigned int blk=1,
const bool fence=true)
{
PROFILE_BLOCK(Vscale);
unsigned int vvsize = v.size();
for (unsigned int i=0; i<vvsize; i+= blk) {
for (unsigned int j=i; j<std::min(vvsize,(i+1)*blk); ++j) {
v[j].scale(factors[j],false);
}
if (fence && blk!=1 ) world.gop.fence();
}
if (fence) world.gop.fence();
}
/// Scales inplace a vector of functions by the same
template <typename T, typename Q, std::size_t NDIM>
void scale(World& world,
std::vector< Function<T,NDIM> >& v,
const Q factor,
const unsigned int blk=1,
const bool fence=true){
PROFILE_BLOCK(Vscale); // shouldn't need blocking since it is local
unsigned int vvsize = v.size();
for (unsigned int i=0; i<vvsize; i+= blk) {
for (unsigned int j=i; j<std::min(vvsize,(i+1)*blk); ++j) {
v[j].scale(factor,false);
}
if (fence && blk!=1 ) world.gop.fence();
}
if (fence) world.gop.fence();
}
/// Computes the 2-norms of a vector of functions
template <typename T, std::size_t NDIM>
std::vector<double> norm2s(World& world,
const std::vector< Function<T,NDIM> >& v,
const unsigned int blk=1,
const bool fence=true){
PROFILE_BLOCK(Vnorm2);
unsigned int vvsize = v.size();
std::vector<double> norms(vvsize);
for (unsigned int i=0; i<vvsize; i+= blk) {
for (unsigned int j=i; j<std::min(vvsize,(i+1)*blk); ++j) {
norms[j] = v[j].norm2sq_local();
}
if (fence && (blk!=1)) world.gop.fence();
}
if (fence ) world.gop.fence();
world.gop.sum(&norms[0], norms.size());
for (unsigned int i=0; i<vvsize; i+= blk) {
for (unsigned int j=i; j<std::min(vvsize,(i+1)*blk); ++j)
norms[j] = sqrt(norms[j]);
if (fence && (blk!=1)) world.gop.fence();
}
world.gop.fence();
return norms;
}
/// Computes the 2-norm of a vector of functions
// should be local; norms[0] contains the result
template <typename T, std::size_t NDIM>
double norm2(World& world,
const std::vector< Function<T,NDIM> >& v)
{
PROFILE_BLOCK(Vnorm2);
std::vector<double> norms(v.size());
for (unsigned int i=0; i<v.size(); ++i)
norms[i] = v[i].norm2sq_local();
world.gop.sum(&norms[0], norms.size());
for (unsigned int i=1; i<v.size(); ++i)
norms[0] += norms[i];
world.gop.fence();
return sqrt(norms[0]);
}
inline double conj(double x) {
return x;
}
inline double conj(float x) {
return x;
}
template <typename T, typename R, std::size_t NDIM>
struct MatrixInnerTask : public TaskInterface {
Tensor<TENSOR_RESULT_TYPE(T,R)> result; // Must be a copy
const Function<T,NDIM>& f;
const std::vector< Function<R,NDIM> >& g;
long jtop;
MatrixInnerTask(const Tensor<TENSOR_RESULT_TYPE(T,R)>& result,
const Function<T,NDIM>& f,
const std::vector< Function<R,NDIM> >& g,
long jtop)
: result(result), f(f), g(g), jtop(jtop) {}
void run(World& world) {
for (long j=0; j<jtop; ++j) {
result(j) = f.inner_local(g[j]);
}
}
private:
/// Get the task id
/// \param id The id to set for this task
virtual void get_id(std::pair<void*,unsigned short>& id) const {
PoolTaskInterface::make_id(id, *this);
}
}; // struct MatrixInnerTask
/// Computes the matrix inner product of two function vectors - q(i,j) = inner(f[i],g[j])
/// For complex types symmetric is interpreted as Hermitian.
///
/// The current parallel loop is non-optimal but functional.
template <typename T, typename R, std::size_t NDIM>
Tensor< TENSOR_RESULT_TYPE(T,R) > matrix_inner(World& world,
const std::vector< Function<T,NDIM> >& f,
const std::vector< Function<R,NDIM> >& g,
bool sym=false) {
PROFILE_BLOCK(Vmatrix_inner);
unsigned int n=f.size(), m=g.size();
Tensor< TENSOR_RESULT_TYPE(T,R) > r(n,m);
if (sym) MADNESS_ASSERT(n==m);
world.gop.fence();
compress(world, f);
if (&f != &g) compress(world, g);
// for (long i=0; i<n; ++i) {
// long jtop = m;
// if (sym) jtop = i+1;
// for (long j=0; j<jtop; ++j) {
// r(i,j) = f[i].inner_local(g[j]);
// if (sym) r(j,i) = conj(r(i,j));
// }
// }
for (unsigned int i=n-1; i>=0; --i) {
unsigned int jtop = m;
if (sym) jtop = i+1;
world.taskq.add(new MatrixInnerTask<T,R,NDIM>(r(i,_), f[i], g, jtop));
}
world.gop.fence();
world.gop.sum(r.ptr(),n*m);
if (sym) {
for (unsigned int i=0; i<n; ++i) {
for (unsigned int j=0; j<i; ++j) {
r(j,i) = conj(r(i,j));
}
}
}
return r;
}
/// Computes the element-wise inner product of two function vectors - q(i) = inner(f[i],g[i])
template <typename T, typename R, std::size_t NDIM>
Tensor< TENSOR_RESULT_TYPE(T,R) > inner(World& world,
const std::vector< Function<T,NDIM> >& f,
const std::vector< Function<R,NDIM> >& g) {
PROFILE_BLOCK(Vinnervv);
long n=f.size(), m=g.size();
MADNESS_ASSERT(n==m);
Tensor< TENSOR_RESULT_TYPE(T,R) > r(n);
compress(world, f);
compress(world, g);
for (long i=0; i<n; ++i) {
r(i) = f[i].inner_local(g[i]);
}
world.taskq.fence();
world.gop.sum(r.ptr(),n);
world.gop.fence();
return r;
}
/// Computes the inner product of a function with a function vector - q(i) = inner(f,g[i])
template <typename T, typename R, std::size_t NDIM>
Tensor< TENSOR_RESULT_TYPE(T,R) > inner(World& world,
const Function<T,NDIM>& f,
const std::vector< Function<R,NDIM> >& g) {
PROFILE_BLOCK(Vinner);
long n=g.size();
Tensor< TENSOR_RESULT_TYPE(T,R) > r(n);
f.compress();
compress(world, g);
for (long i=0; i<n; ++i) {
r(i) = f.inner_local(g[i]);
}
world.taskq.fence();
world.gop.sum(r.ptr(),n);
world.gop.fence();
return r;
}
/// Multiplies a function against a vector of functions --- q[i] = a * v[i]
template <typename T, typename R, std::size_t NDIM>
std::vector< Function<TENSOR_RESULT_TYPE(T,R), NDIM> >
mul(World& world,
const Function<T,NDIM>& a,
const std::vector< Function<R,NDIM> >& v,
const unsigned int blk=1,
const bool fence=true) {
PROFILE_BLOCK(Vmul);
a.reconstruct(false);
reconstruct(world, v, blk, false);
world.gop.fence();
return vmulXX(a, v, 0.0, fence);
}
/// Multiplies a function against a vector of functions using sparsity of a and v[i] --- q[i] = a * v[i]
template <typename T, typename R, std::size_t NDIM>
std::vector< Function<TENSOR_RESULT_TYPE(T,R), NDIM> >
mul_sparse(World& world,
const Function<T,NDIM>& a,
const std::vector< Function<R,NDIM> >& v,
const double tol,
const bool fence=true,
const unsigned int blk=1)
{
PROFILE_BLOCK(Vmulsp);
a.reconstruct(false);
reconstruct(world, v, blk, false);
world.gop.fence();
unsigned int vvsize = v.size();
for (unsigned int i=0; i<vvsize; i+= blk) {
for (unsigned int j=i; j<std::min(vvsize,(i+1)*blk); ++j)
v[j].norm_tree(false);
if ( fence && (blk == 1)) world.gop.fence();
}
a.norm_tree();
return vmulXX(a, v, tol, fence);
}
/// Makes the norm tree for all functions in a vector
template <typename T, std::size_t NDIM>
void norm_tree(World& world,
const std::vector< Function<T,NDIM> >& v,
bool fence=true,
unsigned int blk=1){
PROFILE_BLOCK(Vnorm_tree);
unsigned int vvsize = v.size();
for (unsigned int i=0; i<vvsize; i+= blk) {
for (unsigned int j=i; j<std::min(vvsize,(i+1)*blk); ++j)
v[j].norm_tree(false);
if (fence && blk!=1 ) world.gop.fence();
}
if (fence) world.gop.fence();
}
/// Multiplies two vectors of functions q[i] = a[i] * b[i]
template <typename T, typename R, std::size_t NDIM>
std::vector< Function<TENSOR_RESULT_TYPE(T,R), NDIM> >
mul(World& world,
const std::vector< Function<T,NDIM> >& a,
const std::vector< Function<R,NDIM> >& b,
bool fence=true,
unsigned int blk=1){
PROFILE_BLOCK(Vmulvv);
reconstruct(world, a, blk, false);
if (&a != &b) reconstruct(world, b, blk, false);
world.gop.fence();
std::vector< Function<TENSOR_RESULT_TYPE(T,R),NDIM> > q(a.size());
unsigned int vvsize = a.size();
for (unsigned int i=0; i<vvsize; i+= blk) {
for (unsigned int j=i; j<std::min(vvsize,(i+1)*blk); ++j)
q[j] = mul(a[j], b[j], false);
if (fence && (blk !=1 )) world.gop.fence();
}
if (fence) world.gop.fence();
return q;
}
/// Computes the square of a vector of functions --- q[i] = v[i]**2
template <typename T, std::size_t NDIM>
std::vector< Function<T,NDIM> >
square(World& world,
const std::vector< Function<T,NDIM> >& v,
bool fence=true) {
return mul<T,T,NDIM>(world, v, v, fence);
// std::vector< Function<T,NDIM> > vsq(v.size());
// for (unsigned int i=0; i<v.size(); ++i) {
// vsq[i] = square(v[i], false);
// }
// if (fence) world.gop.fence();
// return vsq;
}
/// Sets the threshold in a vector of functions
template <typename T, std::size_t NDIM>
void set_thresh(World& world,
std::vector< Function<T,NDIM> >& v,
double thresh,
bool fence=true) {
for (unsigned int j=0; j<v.size(); ++j) {
v[j].set_thresh(thresh,false);
}
if (fence) world.gop.fence();
}
/// Returns the complex conjugate of the vector of functions
template <typename T, std::size_t NDIM>
std::vector< Function<T,NDIM> >
conj(World& world,
const std::vector< Function<T,NDIM> >& v,
bool fence=true){
PROFILE_BLOCK(Vconj);
std::vector< Function<T,NDIM> > r = copy(world, v); // Currently don't have oop conj
for (unsigned int i=0; i<v.size(); ++i) {
r[i].conj(false);
}
if (fence) world.gop.fence();
return r;
}
/// Returns a deep copy of a vector of functions
template <typename T, std::size_t NDIM>
std::vector< Function<T,NDIM> >
copy(World& world,
const std::vector< Function<T,NDIM> >& v,
bool fence=true) {
PROFILE_BLOCK(Vcopy);
std::vector< Function<T,NDIM> > r(v.size());
for (unsigned int i=0; i<v.size(); ++i) {
r[i] = copy(v[i], false);
}
if (fence) world.gop.fence();
return r;
}
/// Returns a vector of deep copies of of a function
template <typename T, std::size_t NDIM>
std::vector< Function<T,NDIM> >
copy(World& world,
const Function<T,NDIM>& v,
const unsigned int n,
bool fence=true) {
PROFILE_BLOCK(Vcopy1);
std::vector< Function<T,NDIM> > r(n);
for (unsigned int i=0; i<n; ++i) {
r[i] = copy(v, false);
}
if (fence) world.gop.fence();
return r;
}
/// Returns new vector of functions --- q[i] = a[i] + b[i]
template <typename T, typename R, std::size_t NDIM>
std::vector< Function<TENSOR_RESULT_TYPE(T,R), NDIM> >
add(World& world,
const std::vector< Function<T,NDIM> >& a,
const std::vector< Function<R,NDIM> >& b,
bool fence=true,
unsigned int blk=1) {
PROFILE_BLOCK(Vadd);
MADNESS_ASSERT(a.size() == b.size());
compress(world, a, blk);
compress(world, b, blk);
std::vector< Function<TENSOR_RESULT_TYPE(T,R),NDIM> > r(a.size());
unsigned int vvsize = a.size();
for (unsigned int i=0; i<vvsize; i+= blk) {
for (unsigned int j=i; j<std::min(vvsize,(i+1)*blk); ++j)
r[j] = add(a[j], b[j], false);
if (fence && (blk !=1 )) world.gop.fence();
}
if (fence) world.gop.fence();
return r;
}
/// Returns new vector of functions --- q[i] = a + b[i]
template <typename T, typename R, std::size_t NDIM>
std::vector< Function<TENSOR_RESULT_TYPE(T,R), NDIM> >
add(World& world,
const Function<T,NDIM> & a,
const std::vector< Function<R,NDIM> >& b,
bool fence=true,
unsigned int blk=1) {
PROFILE_BLOCK(Vadd1);
a.compress();
compress(world, b, blk);
std::vector< Function<TENSOR_RESULT_TYPE(T,R),NDIM> > r(b.size());
unsigned int vvsize = b.size();
for (unsigned int i=0; i<vvsize; i+= blk) {
for (unsigned int j=i; j<std::min(vvsize,(i+1)*blk); ++j)
r[j] = add(a, b[j], false);
if (fence && (blk !=1 )) world.gop.fence();
}
if (fence) world.gop.fence();
return r;
}
template <typename T, typename R, std::size_t NDIM>
inline std::vector< Function<TENSOR_RESULT_TYPE(T,R), NDIM> >
add(World& world,
const std::vector< Function<R,NDIM> >& b,
const Function<T,NDIM> & a,
bool fence=true,
unsigned int blk=1) {
return add(world, a, b, fence, blk);
}
/// Returns new vector of functions --- q[i] = a[i] - b[i]
template <typename T, typename R, std::size_t NDIM>
std::vector< Function<TENSOR_RESULT_TYPE(T,R), NDIM> >
sub(World& world,
const std::vector< Function<T,NDIM> >& a,
const std::vector< Function<R,NDIM> >& b,
bool fence=true,
unsigned int blk=1) {
PROFILE_BLOCK(Vsub);
MADNESS_ASSERT(a.size() == b.size());
compress(world, a, fence, blk);
compress(world, b, fence, blk);
std::vector< Function<TENSOR_RESULT_TYPE(T,R),NDIM> > r(a.size());
unsigned int vvsize = a.size();
for (unsigned int i=0; i<vvsize; i+= blk) {
for (unsigned int j=i; j<std::min(vvsize,(i+1)*blk); ++j)
r[j] = sub(a[j], b[j], false);
if (fence && (blk !=1 )) world.gop.fence();
}
if (fence) world.gop.fence();
return r;
}
/// Generalized A*X+Y for vectors of functions ---- a[i] = alpha*a[i] + beta*b[i]
template <typename T, typename Q, typename R, std::size_t NDIM>
void gaxpy(World& world,
Q alpha,
std::vector< Function<T,NDIM> >& a,
Q beta,
const std::vector< Function<R,NDIM> >& b,
unsigned int blk=1,
bool fence=true) {
PROFILE_BLOCK(Vgaxpy);
MADNESS_ASSERT(a.size() == b.size());
compress(world, a, fence, blk);
compress(world, b, fence, blk);
unsigned int vvsize = a.size();
for (unsigned int i=0; i<vvsize; i+= blk) {
for (unsigned int j=i; j<std::min(vvsize,(i+1)*blk); ++j)
a[j].gaxpy(alpha, b[j], beta, false);
if (fence && (blk !=1 )) world.gop.fence();
}
// for (unsigned int i=0; i<a.size(); ++i) {
// a[i].gaxpy(alpha, b[i], beta, false);
// }
if (fence) world.gop.fence();
}
/// Applies a vector of operators to a vector of functions --- q[i] = apply(op[i],f[i])
template <typename opT, typename R, std::size_t NDIM>
std::vector< Function<TENSOR_RESULT_TYPE(typename opT::opT,R), NDIM> >
apply(World& world,
const std::vector< std::shared_ptr<opT> >& op,
const std::vector< Function<R,NDIM> > f,
const unsigned int blk=1){
PROFILE_BLOCK(Vapplyv);
MADNESS_ASSERT(f.size()==op.size());
std::vector< Function<R,NDIM> >& ncf = *const_cast< std::vector< Function<R,NDIM> >* >(&f);
reconstruct(world, f, blk);
nonstandard(world, ncf, blk);
std::vector< Function<TENSOR_RESULT_TYPE(typename opT::opT,R), NDIM> > result(f.size());
unsigned int ff = f.size();
for (unsigned int i=0; i<ff; ++blk) {
for (unsigned int j=i; j<std::min(ff,(i+1)*blk); ++j)
result[j] = apply_only(*op[j], f[j], false);
if (blk !=1)
world.gop.fence();
}
world.gop.fence();
standard(world, ncf, false); // restores promise of logical constness
world.gop.fence();
reconstruct(world, result, blk);
return result;
}
/// Applies an operator to a vector of functions --- q[i] = apply(op,f[i])
template <typename T, typename R, std::size_t NDIM>
std::vector< Function<TENSOR_RESULT_TYPE(T,R), NDIM> >
apply(World& world,
const SeparatedConvolution<T,NDIM>& op,
const std::vector< Function<R,NDIM> > f,
const unsigned int blk=1) {
PROFILE_BLOCK(Vapply);
std::vector< Function<R,NDIM> >& ncf = *const_cast< std::vector< Function<R,NDIM> >* >(&f);
reconstruct(world, f, blk);
nonstandard(world, ncf, blk);
std::vector< Function<TENSOR_RESULT_TYPE(T,R), NDIM> > result(f.size());
unsigned int ff = f.size();
for (unsigned int i=0; i<ff; ++blk) {
for (unsigned int j=i; j<std::min(ff,(i+1)*blk); ++j)
result[j] = apply_only(op, f[j], false);
if (blk !=1)
world.gop.fence();
}
world.gop.fence();
standard(world, ncf, blk, false); // restores promise of logical constness
world.gop.fence();
reconstruct(world, result, blk);
return result;
}
/// Normalizes a vector of functions --- v[i] = v[i].scale(1.0/v[i].norm2())
template <typename T, std::size_t NDIM>
void normalize(World& world,
std::vector< Function<T,NDIM> >& v,
bool fence=true){
PROFILE_BLOCK(Vnormalize);
std::vector<double> nn = norm2s(world, v);
for (unsigned int i=0; i<v.size(); ++i)
v[i].scale(1.0/nn[i],false);
if (fence) world.gop.fence();
}
}
#endif // MADNESS_MRA_VMRA_H__INCLUDED
|