/usr/include/dune/pdelab/gridfunctionspace/genericdatahandle.hh is in libdune-pdelab-dev 2.0.0-1.
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 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 | // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#ifndef DUNE_PDELAB_GENERICDATAHANDLE_HH
#define DUNE_PDELAB_GENERICDATAHANDLE_HH
#include <vector>
#include <set>
#include <limits>
#include<dune/common/exceptions.hh>
#include <dune/common/parallel/mpihelper.hh>
#include <dune/common/static_assert.hh>
#include <dune/grid/common/datahandleif.hh>
#include <dune/grid/common/gridenums.hh>
#include <dune/pdelab/common/polymorphicbufferwrapper.hh>
#include <dune/pdelab/gridfunctionspace/entityindexcache.hh>
namespace Dune {
namespace PDELab {
//! Communication descriptor for sending one item of type E per DOF.
template<typename E>
struct DOFDataCommunicationDescriptor
{
typedef char DataType;
//! size type to use if communicating leaf ordering sizes
typedef std::size_t size_type;
// Wrap the grid's communication buffer to enable sending leaf ordering sizes along with the data
static const bool wrap_buffer = true;
// export original data type to fix up size information forwarded to standard gather / scatter functors
typedef E OriginalDataType;
template<typename GFS>
bool contains(const GFS& gfs, int dim, int codim) const
{
return gfs.dataHandleContains(codim);
}
template<typename GFS>
bool fixedSize(const GFS& gfs, int dim, int codim) const
{
return gfs.dataHandleFixedSize(codim);
}
template<typename GFS, typename Entity>
std::size_t size(const GFS& gfs, const Entity& e) const
{
// include size of leaf ordering offsets if necessary
return gfs.dataHandleSize(e) * sizeof(E) + (gfs.sendLeafSizes() ? TypeTree::TreeInfo<typename GFS::Ordering>::leafCount * sizeof(size_type) : 0);
}
};
//! Communication descriptor for sending count items of type E per entity with attached DOFs.
template<typename E>
struct EntityDataCommunicationDescriptor
{
typedef E DataType;
// Data is per entity, so we don't need to send leaf ordering size and thus can avoid wrapping the
// grid's communication buffer
static const bool wrap_buffer = false;
template<typename GFS>
bool contains(const GFS& gfs, int dim, int codim) const
{
return gfs.dataHandleContains(codim);
}
template<typename GFS>
bool fixedSize(const GFS& gfs, int dim, int codim) const
{
return true;
}
template<typename GFS, typename Entity>
std::size_t size(const GFS& gfs, const Entity& e) const
{
return gfs.dataHandleContains(Entity::codimension) ? _count : 0;
}
explicit EntityDataCommunicationDescriptor(std::size_t count = 1)
: _count(count)
{}
private:
const std::size_t _count;
};
//! Implement a data handle with a grid function space.
/**
* \tparam GFS a grid function space
* \tparam V a vector container associated with the GFS
* \tparam GatherScatter gather/scatter methods with argumemts buffer, and data
* \tparam CommunicationDescriptor A descriptor for the communication structure
*/
template<typename GFS, typename V, typename GatherScatter, typename CommunicationDescriptor = DOFDataCommunicationDescriptor<typename V::ElementType> >
class GFSDataHandle
: public Dune::CommDataHandleIF<GFSDataHandle<GFS,V,GatherScatter,CommunicationDescriptor>,typename CommunicationDescriptor::DataType>
{
public:
typedef typename CommunicationDescriptor::DataType DataType;
typedef typename GFS::Traits::SizeType size_type;
static const size_type leaf_count = TypeTree::TreeInfo<typename GFS::Ordering>::leafCount;
GFSDataHandle(const GFS& gfs, V& v, GatherScatter gather_scatter = GatherScatter(), CommunicationDescriptor communication_descriptor = CommunicationDescriptor())
: _gfs(gfs)
, _index_cache(gfs)
, _local_view(v)
, _gather_scatter(gather_scatter)
, _communication_descriptor(communication_descriptor)
{}
//! returns true if data for this codim should be communicated
bool contains(int dim, int codim) const
{
return _communication_descriptor.contains(_gfs,dim,codim);
}
//! \brief returns true if size per entity of given dim and codim is a constant
bool fixedsize(int dim, int codim) const
{
return _communication_descriptor.fixedSize(_gfs,dim,codim);
}
/*! \brief how many objects of type DataType have to be sent for a given entity
Note: Only the sender side needs to know this size.
*/
template<typename Entity>
size_type size(const Entity& e) const
{
return _communication_descriptor.size(_gfs,e);
}
//! \brief pack data from user to message buffer - version with support for sending leaf ordering sizes
template<typename MessageBuffer, typename Entity>
typename enable_if<
CommunicationDescriptor::wrap_buffer && AlwaysTrue<Entity>::value // we can only support this if the buffer is wrapped
>::type
gather(MessageBuffer& buff, const Entity& e) const
{
PolymorphicBufferWrapper<MessageBuffer> buf_wrapper(buff);
_index_cache.update(e);
_local_view.bind(_index_cache);
if (_gfs.sendLeafSizes())
{
// send the leaf ordering offsets as exported by the EntityIndexCache
for (auto it = _index_cache.offsets().begin() + 1,
end_it = _index_cache.offsets().end();
it != end_it;
++it)
{
buf_wrapper.write(static_cast<typename CommunicationDescriptor::size_type>(*it));
}
}
// send the normal data
if (_gather_scatter.gather(buf_wrapper,e,_local_view))
_local_view.commit();
_local_view.unbind();
}
//! \brief pack data from user to message buffer - version without support for sending leaf ordering sizes
template<typename MessageBuffer, typename Entity>
typename enable_if<
!CommunicationDescriptor::wrap_buffer && AlwaysTrue<Entity>::value
>::type
gather(MessageBuffer& buff, const Entity& e) const
{
_index_cache.update(e);
_local_view.bind(_index_cache);
if (_gather_scatter.gather(buff,e,_local_view))
_local_view.commit();
_local_view.unbind();
}
/*! \brief unpack data from message buffer to user
n is the number of objects sent by the sender
This is the version with support for receiving leaf ordering sizes
*/
template<typename MessageBuffer, typename Entity>
typename enable_if<
CommunicationDescriptor::wrap_buffer && AlwaysTrue<Entity>::value // we require the buffer to be wrapped
>::type
scatter(MessageBuffer& buff, const Entity& e, size_type n)
{
PolymorphicBufferWrapper<MessageBuffer> buf_wrapper(buff);
_index_cache.update(e);
_local_view.bind(_index_cache);
bool needs_commit = false;
if (_gfs.sendLeafSizes())
{
// receive leaf ordering offsets and store in local array
typename IndexCache::Offsets remote_offsets = {{0}};
for (auto it = remote_offsets.begin() + 1,
end_it = remote_offsets.end();
it != end_it;
++it)
{
typename CommunicationDescriptor::size_type data = 0;
buf_wrapper.read(data);
*it = data;
}
// call special version of scatter() that can handle empty leafs in the ordering tree
needs_commit = _gather_scatter.scatter(buf_wrapper,remote_offsets,_index_cache.offsets(),e,_local_view);
}
else
{
// call standard version of scatter - make sure to fix the reported communication size
needs_commit = _gather_scatter.scatter(buf_wrapper,n / sizeof(typename CommunicationDescriptor::OriginalDataType),e,_local_view);
}
if (needs_commit)
_local_view.commit();
_local_view.unbind();
}
/*! \brief unpack data from message buffer to user
n is the number of objects sent by the sender
This is the version without support for receiving leaf ordering sizes
*/
template<typename MessageBuffer, typename Entity>
typename enable_if<
!CommunicationDescriptor::wrap_buffer && AlwaysTrue<Entity>::value
>::type
scatter(MessageBuffer& buff, const Entity& e, size_type n)
{
_index_cache.update(e);
_local_view.bind(_index_cache);
if (_gather_scatter.scatter(buff,n,e,_local_view))
_local_view.commit();
_local_view.unbind();
}
private:
typedef EntityIndexCache<GFS> IndexCache;
typedef typename V::template LocalView<IndexCache> LocalView;
const GFS& _gfs;
mutable IndexCache _index_cache;
mutable LocalView _local_view;
mutable GatherScatter _gather_scatter;
CommunicationDescriptor _communication_descriptor;
};
template<typename GatherScatter>
class DataGatherScatter
{
public:
typedef std::size_t size_type;
template<typename MessageBuffer, typename Entity, typename LocalView>
bool gather(MessageBuffer& buff, const Entity& e, const LocalView& local_view) const
{
for (std::size_t i = 0; i < local_view.size(); ++i)
_gather_scatter.gather(buff,local_view[i]);
return false;
}
// default scatter - requires function space structure to be identical on sender and receiver side
template<typename MessageBuffer, typename Entity, typename LocalView>
bool scatter(MessageBuffer& buff, size_type n, const Entity& e, LocalView& local_view) const
{
if (local_view.cache().gridFunctionSpace().containsPartition(e.partitionType()))
{
if (local_view.size() != n)
DUNE_THROW(Exception,"size mismatch in GridFunctionSpace data handle, have " << local_view.size() << "DOFs, but received " << n);
for (std::size_t i = 0; i < local_view.size(); ++i)
_gather_scatter.scatter(buff,local_view[i]);
return true;
}
else
{
if (local_view.size() != 0)
DUNE_THROW(Exception,"expected no DOFs in partition '" << e.partitionType() << "', but have " << local_view.size());
for (std::size_t i = 0; i < local_view.size(); ++i)
{
typename LocalView::ElementType dummy;
buff.read(dummy);
}
return false;
}
}
// enhanced scatter with support for function spaces with different structure on sender and receiver side
template<typename MessageBuffer, typename Offsets, typename Entity, typename LocalView>
bool scatter(MessageBuffer& buff, const Offsets& remote_offsets, const Offsets& local_offsets, const Entity& e, LocalView& local_view) const
{
if (local_view.cache().gridFunctionSpace().containsPartition(e.partitionType()))
{
// the idea here is this:
// the compile time structure of the overall function space (and its ordering) will be identical on both sides
// of the communication, but one side may be missing information on some leaf orderings, e.g. because the DOF
// belongs to a MultiDomain subdomain that only has an active grid cell on one side of the communication.
// So we step through the leaves and simply ignore any block where one of the two sides is of size 0.
// Otherwise, it's business as usual: we make sure that the sizes from both sides match and then process all
// data with the DOF-local gather / scatter functor.
size_type remote_i = 0;
size_type local_i = 0;
bool needs_commit = false;
for (size_type block = 1; block < local_offsets.size(); ++block)
{
// we didn't get any data - just ignore
if (remote_offsets[block] == remote_i)
{
local_i = local_offsets[block];
continue;
}
// we got data for DOFs we don't have - drain buffer
if (local_offsets[block] == local_i)
{
for (; remote_i < remote_offsets[block]; ++remote_i)
{
typename LocalView::ElementType dummy;
buff.read(dummy);
}
continue;
}
if (remote_offsets[block] - remote_i != local_offsets[block] - local_i)
DUNE_THROW(Exception,"size mismatch in GridFunctionSpace data handle block " << block << ", have " << local_offsets[block] - local_i << "DOFs, but received " << remote_offsets[block] - remote_i);
for (; local_i < local_offsets[block]; ++local_i)
_gather_scatter.scatter(buff,local_view[local_i]);
remote_i = remote_offsets[block];
needs_commit = true;
}
return needs_commit;
}
else
{
if (local_view.size() != 0)
DUNE_THROW(Exception,"expected no DOFs in partition '" << e.partitionType() << "', but have " << local_view.size());
for (std::size_t i = 0; i < remote_offsets.back(); ++i)
{
typename LocalView::ElementType dummy;
buff.read(dummy);
}
return false;
}
}
DataGatherScatter(GatherScatter gather_scatter = GatherScatter())
: _gather_scatter(gather_scatter)
{}
private:
GatherScatter _gather_scatter;
};
template<typename GatherScatter>
class DataEntityGatherScatter
{
public:
typedef std::size_t size_type;
template<typename MessageBuffer, typename Entity, typename LocalView>
bool gather(MessageBuffer& buff, const Entity& e, const LocalView& local_view) const
{
for (std::size_t i = 0; i < local_view.size(); ++i)
_gather_scatter.gather(buff,e,local_view[i]);
return false;
}
// see documentation in DataGatherScatter for further info on the scatter() implementations
template<typename MessageBuffer, typename Entity, typename LocalView>
bool scatter(MessageBuffer& buff, size_type n, const Entity& e, LocalView& local_view) const
{
if (local_view.cache().gridFunctionSpace().containsPartition(e.partitionType()))
{
if (local_view.size() != n)
DUNE_THROW(Exception,"size mismatch in GridFunctionSpace data handle, have " << local_view.size() << "DOFs, but received " << n);
for (std::size_t i = 0; i < local_view.size(); ++i)
_gather_scatter.scatter(buff,e,local_view[i]);
return true;
}
else
{
if (local_view.size() != 0)
DUNE_THROW(Exception,"expected no DOFs in partition '" << e.partitionType() << "', but have " << local_view.size());
for (std::size_t i = 0; i < local_view.size(); ++i)
{
typename LocalView::ElementType dummy;
buff.read(dummy);
}
return false;
}
}
// see documentation in DataGatherScatter for further info on the scatter() implementations
template<typename MessageBuffer, typename Offsets, typename Entity, typename LocalView>
bool scatter(MessageBuffer& buff, const Offsets& remote_offsets, const Offsets& local_offsets, const Entity& e, LocalView& local_view) const
{
if (local_view.cache().gridFunctionSpace().containsPartition(e.partitionType()))
{
size_type remote_i = 0;
size_type local_i = 0;
bool needs_commit = false;
for (size_type block = 1; block < local_offsets.size(); ++block)
{
// we didn't get any data - just ignore
if (remote_offsets[block] == remote_i)
{
local_i = local_offsets[block];
continue;
}
// we got data for DOFs we don't have - drain buffer
if (local_offsets[block] == local_i)
{
for (; remote_i < remote_offsets[block]; ++remote_i)
{
typename LocalView::ElementType dummy;
buff.read(dummy);
}
continue;
}
if (remote_offsets[block] - remote_i != local_offsets[block] - local_i)
DUNE_THROW(Exception,"size mismatch in GridFunctionSpace data handle block " << block << ", have " << local_offsets[block] - local_i << "DOFs, but received " << remote_offsets[block] - remote_i);
for (; local_i < local_offsets[block]; ++local_i)
_gather_scatter.scatter(buff,e,local_view[local_i]);
remote_i = remote_offsets[block];
needs_commit = true;
}
return needs_commit;
}
else
{
if (local_view.size() != 0)
DUNE_THROW(Exception,"expected no DOFs in partition '" << e.partitionType() << "', but have " << local_view.size());
for (std::size_t i = 0; i < remote_offsets.back(); ++i)
{
typename LocalView::ElementType dummy;
buff.read(dummy);
}
return false;
}
}
DataEntityGatherScatter(GatherScatter gather_scatter = GatherScatter())
: _gather_scatter(gather_scatter)
{}
private:
GatherScatter _gather_scatter;
};
template<typename GatherScatter>
class DataContainerIndexGatherScatter
{
public:
typedef std::size_t size_type;
template<typename MessageBuffer, typename Entity, typename LocalView>
bool gather(MessageBuffer& buff, const Entity& e, const LocalView& local_view) const
{
for (std::size_t i = 0; i < local_view.size(); ++i)
_gather_scatter.gather(buff,local_view.cache().containerIndex(i),local_view[i]);
return false;
}
// see documentation in DataGatherScatter for further info on the scatter() implementations
template<typename MessageBuffer, typename Entity, typename LocalView>
bool scatter(MessageBuffer& buff, size_type n, const Entity& e, LocalView& local_view) const
{
if (local_view.cache().gridFunctionSpace().containsPartition(e.partitionType()))
{
if (local_view.size() != n)
DUNE_THROW(Exception,"size mismatch in GridFunctionSpace data handle, have " << local_view.size() << "DOFs, but received " << n);
for (std::size_t i = 0; i < local_view.size(); ++i)
_gather_scatter.scatter(buff,local_view.cache().containerIndex(i),local_view[i]);
return true;
}
else
{
if (local_view.size() != 0)
DUNE_THROW(Exception,"expected no DOFs in partition '" << e.partitionType() << "', but have " << local_view.size());
for (std::size_t i = 0; i < local_view.size(); ++i)
{
typename LocalView::ElementType dummy;
buff.read(dummy);
}
return false;
}
}
// see documentation in DataGatherScatter for further info on the scatter() implementations
template<typename MessageBuffer, typename Offsets, typename Entity, typename LocalView>
bool scatter(MessageBuffer& buff, const Offsets& remote_offsets, const Offsets& local_offsets, const Entity& e, LocalView& local_view) const
{
if (local_view.cache().gridFunctionSpace().containsPartition(e.partitionType()))
{
size_type remote_i = 0;
size_type local_i = 0;
bool needs_commit = false;
for (size_type block = 1; block < local_offsets.size(); ++block)
{
// we didn't get any data - just ignore
if (remote_offsets[block] == remote_i)
{
local_i = local_offsets[block];
continue;
}
// we got data for DOFs we don't have - drain buffer
if (local_offsets[block] == local_i)
{
for (; remote_i < remote_offsets[block]; ++remote_i)
{
typename LocalView::ElementType dummy;
buff.read(dummy);
}
continue;
}
if (remote_offsets[block] - remote_i != local_offsets[block] - local_i)
DUNE_THROW(Exception,"size mismatch in GridFunctionSpace data handle block " << block << ", have " << local_offsets[block] - local_i << "DOFs, but received " << remote_offsets[block] - remote_i);
for (; local_i < local_offsets[block]; ++local_i)
_gather_scatter.scatter(buff,local_view.cache().containerIndex(local_i),local_view[local_i]);
remote_i = remote_offsets[block];
needs_commit = true;
}
return needs_commit;
}
else
{
if (local_view.size() != 0)
DUNE_THROW(Exception,"expected no DOFs in partition '" << e.partitionType() << "', but have " << local_view.size());
for (std::size_t i = 0; i < remote_offsets.back(); ++i)
{
typename LocalView::ElementType dummy;
buff.read(dummy);
}
return false;
}
}
DataContainerIndexGatherScatter(GatherScatter gather_scatter = GatherScatter())
: _gather_scatter(gather_scatter)
{}
private:
GatherScatter _gather_scatter;
};
class AddGatherScatter
{
public:
template<class MessageBuffer, class DataType>
void gather (MessageBuffer& buff, DataType& data) const
{
buff.write(data);
}
template<class MessageBuffer, class DataType>
void scatter (MessageBuffer& buff, DataType& data) const
{
DataType x;
buff.read(x);
data += x;
}
};
template<class GFS, class V>
class AddDataHandle
: public GFSDataHandle<GFS,V,DataGatherScatter<AddGatherScatter> >
{
typedef GFSDataHandle<GFS,V,DataGatherScatter<AddGatherScatter> > BaseT;
public:
AddDataHandle (const GFS& gfs_, V& v_)
: BaseT(gfs_,v_)
{}
};
class AddClearGatherScatter
{
public:
template<class MessageBuffer, class DataType>
void gather (MessageBuffer& buff, DataType& data) const
{
buff.write(data);
data = (DataType) 0;
}
template<class MessageBuffer, class DataType>
void scatter (MessageBuffer& buff, DataType& data) const
{
DataType x;
buff.read(x);
data += x;
}
};
template<class GFS, class V>
class AddClearDataHandle
: public GFSDataHandle<GFS,V,DataGatherScatter<AddClearGatherScatter> >
{
typedef GFSDataHandle<GFS,V,DataGatherScatter<AddClearGatherScatter> > BaseT;
public:
AddClearDataHandle (const GFS& gfs_, V& v_)
: BaseT(gfs_,v_)
{}
};
class CopyGatherScatter
{
public:
template<class MessageBuffer, class DataType>
void gather (MessageBuffer& buff, DataType& data) const
{
buff.write(data);
}
template<class MessageBuffer, class DataType>
void scatter (MessageBuffer& buff, DataType& data) const
{
DataType x;
buff.read(x);
data = x;
}
};
template<class GFS, class V>
class CopyDataHandle
: public GFSDataHandle<GFS,V,DataGatherScatter<CopyGatherScatter> >
{
typedef GFSDataHandle<GFS,V,DataGatherScatter<CopyGatherScatter> > BaseT;
public:
CopyDataHandle (const GFS& gfs_, V& v_)
: BaseT(gfs_,v_)
{}
};
class MinGatherScatter
{
public:
template<class MessageBuffer, class DataType>
void gather (MessageBuffer& buff, DataType& data) const
{
buff.write(data);
}
template<class MessageBuffer, class DataType>
void scatter (MessageBuffer& buff, DataType& data) const
{
DataType x;
buff.read(x);
data = std::min(data,x);
}
};
template<class GFS, class V>
class MinDataHandle
: public GFSDataHandle<GFS,V,DataGatherScatter<MinGatherScatter> >
{
typedef GFSDataHandle<GFS,V,DataGatherScatter<MinGatherScatter> > BaseT;
public:
MinDataHandle (const GFS& gfs_, V& v_)
: BaseT(gfs_,v_)
{}
};
class MaxGatherScatter
{
public:
template<class MessageBuffer, class DataType>
void gather (MessageBuffer& buff, DataType& data) const
{
buff.write(data);
}
template<class MessageBuffer, class DataType>
void scatter (MessageBuffer& buff, DataType& data) const
{
DataType x;
buff.read(x);
data = std::max(data,x);
}
};
template<class GFS, class V>
class MaxDataHandle
: public GFSDataHandle<GFS,V,DataGatherScatter<MaxGatherScatter> >
{
typedef GFSDataHandle<GFS,V,DataGatherScatter<MaxGatherScatter> > BaseT;
public:
MaxDataHandle (const GFS& gfs_, V& v_)
: BaseT(gfs_,v_)
{}
};
//! GatherScatter functor for marking ghost DOFs.
/**
* This data handle will mark all ghost DOFs (more precisely, all DOFs associated
* with entities not part of either the interior or the border partition).
*
* \note In order to work correctly, the data handle must be communicated on the
* Dune::InteriorBorder_All_Interface.
*/
class GhostGatherScatter
{
public:
template<typename MessageBuffer, typename Entity, typename LocalView>
bool gather(MessageBuffer& buff, const Entity& e, LocalView& local_view) const
{
// Figure out where we are...
const bool ghost = e.partitionType()!=Dune::InteriorEntity && e.partitionType()!=Dune::BorderEntity;
// ... and send something (doesn't really matter what, we'll throw it away on the receiving side).
buff.write(ghost);
return false;
}
template<typename MessageBuffer, typename Entity, typename LocalView>
bool scatter(MessageBuffer& buff, std::size_t n, const Entity& e, LocalView& local_view) const
{
// Figure out where we are - we have to do this again on the receiving side due to the asymmetric
// communication interface!
const bool ghost = e.partitionType()!=Dune::InteriorEntity && e.partitionType()!=Dune::BorderEntity;
// drain buffer
bool dummy;
buff.read(dummy);
for (std::size_t i = 0; i < local_view.size(); ++i)
local_view[i] = ghost;
return true;
}
};
//! Data handle for marking ghost DOFs.
/**
* This data handle will mark all ghost DOFs (more precisely, all DOFs associated
* with entities not part of either the interior or the border partition).
*
* \note In order to work correctly, the data handle must be communicated on the
* Dune::InteriorBorder_All_Interface.
*/
template<class GFS, class V>
class GhostDataHandle
: public Dune::PDELab::GFSDataHandle<GFS,
V,
GhostGatherScatter,
EntityDataCommunicationDescriptor<bool> >
{
typedef Dune::PDELab::GFSDataHandle<
GFS,
V,
GhostGatherScatter,
EntityDataCommunicationDescriptor<bool>
> BaseT;
dune_static_assert((is_same<typename V::ElementType,bool>::value),
"GhostDataHandle expects a vector of bool values");
public:
//! Creates a new GhostDataHandle.
/**
* Creates a new GhostDataHandle and by default initializes the result vector
* with the correct value of false. If you have already done that externally,
* you can skip the initialization.
*
* \param gfs_ The GridFunctionSpace to operate on.
* \param v_ The result vector.
* \param init_vector Flag to control whether the result vector will be initialized.
*/
GhostDataHandle(const GFS& gfs_, V& v_, bool init_vector = true)
: BaseT(gfs_,v_)
{
if (init_vector)
v_ = false;
}
};
//! GatherScatter functor for creating a disjoint DOF partitioning.
/**
* This functor will associate each DOF with a unique rank, creating a nonoverlapping partitioning
* of the unknowns. The rank for a DOF is chosen by finding the lowest rank on which the associated
* grid entity belongs to either the interior or the border partition.
*
* \note In order to work correctly, the data handle must be communicated on the
* Dune::InteriorBorder_All_Interface and the result vector must be initialized with the MPI rank value.
*/
template<typename RankIndex>
class DisjointPartitioningGatherScatter
{
public:
template<typename MessageBuffer, typename Entity, typename LocalView>
bool gather(MessageBuffer& buff, const Entity& e, LocalView& local_view) const
{
// We only gather from interior and border entities, so we can throw in our ownership
// claim without any further checks.
buff.write(_rank);
return true;
}
template<typename MessageBuffer, typename Entity, typename LocalView>
bool scatter(MessageBuffer& buff, std::size_t n, const Entity& e, LocalView& local_view) const
{
// Value used for DOFs with currently unknown rank.
const RankIndex unknown_rank = std::numeric_limits<RankIndex>::max();
// We can only own this DOF if it is either on the interior or border partition.
const bool is_interior_or_border = (e.partitionType()==Dune::InteriorEntity || e.partitionType()==Dune::BorderEntity);
// Receive data.
RankIndex received_rank;
buff.read(received_rank);
for (std::size_t i = 0; i < local_view.size(); ++i)
{
// Get the currently stored owner rank for this DOF.
RankIndex current_rank = local_view[i];
// We only gather from interior and border entities, so we need to make sure
// we relinquish any ownership claims on overlap and ghost entities on the
// receiving side. We also need to make sure not to overwrite any data already
// received, so we only blank the rank value if the currently stored value is
// equal to our own rank.
if (!is_interior_or_border && current_rank == _rank)
current_rank = unknown_rank;
// Assign DOFs to minimum rank value.
local_view[i] = std::min(current_rank,received_rank);
}
return true;
}
//! Create a DisjointPartitioningGatherScatter object.
/**
* \param rank The MPI rank of the current process.
*/
DisjointPartitioningGatherScatter(RankIndex rank)
: _rank(rank)
{}
private:
const RankIndex _rank;
};
//! GatherScatter data handle for creating a disjoint DOF partitioning.
/**
* This data handle will associate each DOF with a unique rank, creating a nonoverlapping partitioning
* of the unknowns. The rank for a DOF is chosen by finding the lowest rank on which the associated
* grid entity belongs to either the interior or the border partition.
*
* \note In order to work correctly, the data handle must be communicated on the
* Dune::InteriorBorder_All_Interface and the result vector must be initialized with the MPI rank value.
*/
template<class GFS, class V>
class DisjointPartitioningDataHandle
: public Dune::PDELab::GFSDataHandle<GFS,
V,
DisjointPartitioningGatherScatter<
typename V::ElementType
>,
EntityDataCommunicationDescriptor<
typename V::ElementType
>
>
{
typedef Dune::PDELab::GFSDataHandle<
GFS,
V,
DisjointPartitioningGatherScatter<
typename V::ElementType
>,
EntityDataCommunicationDescriptor<
typename V::ElementType
>
> BaseT;
public:
//! Creates a new DisjointPartitioningDataHandle.
/**
* Creates a new DisjointPartitioningDataHandle and by default initializes the
* result vector with the current MPI rank. If you have already done that
* externally, you can skip the initialization.
*
* \param gfs_ The GridFunctionSpace to operate on.
* \param v_ The result vector.
* \param init_vector Flag to control whether the result vector will be initialized.
*/
DisjointPartitioningDataHandle(const GFS& gfs_, V& v_, bool init_vector = true)
: BaseT(gfs_,v_,DisjointPartitioningGatherScatter<typename V::ElementType>(gfs_.gridView().comm().rank()))
{
if (init_vector)
v_ = gfs_.gridView().comm().rank();
}
};
//! GatherScatter functor for marking shared DOFs.
/**
* This functor will mark all DOFs that exist on multiple processes.
*
* \note In order to work correctly, the data handle must be communicated on the
* Dune::All_All_Interface and the result vector must be initialized with false.
*/
struct SharedDOFGatherScatter
{
template<typename MessageBuffer, typename Entity, typename LocalView>
bool gather(MessageBuffer& buff, const Entity& e, LocalView& local_view) const
{
buff.write(local_view.size() > 0);
return false;
}
template<typename MessageBuffer, typename Entity, typename LocalView>
bool scatter(MessageBuffer& buff, std::size_t n, const Entity& e, LocalView& local_view) const
{
bool remote_entity_has_dofs;
buff.read(remote_entity_has_dofs);
for (std::size_t i = 0; i < local_view.size(); ++i)
{
local_view[i] |= remote_entity_has_dofs;
}
return true;
}
};
//! Data handle for marking shared DOFs.
/**
* This data handle will mark all DOFs that exist on multiple processes.
*
* \note In order to work correctly, the data handle must be communicated on the
* Dune::All_All_Interface and the result vector must be initialized with false.
*/
template<class GFS, class V>
class SharedDOFDataHandle
: public Dune::PDELab::GFSDataHandle<GFS,
V,
SharedDOFGatherScatter,
EntityDataCommunicationDescriptor<bool> >
{
typedef Dune::PDELab::GFSDataHandle<
GFS,
V,
SharedDOFGatherScatter,
EntityDataCommunicationDescriptor<bool>
> BaseT;
dune_static_assert((is_same<typename V::ElementType,bool>::value),
"SharedDOFDataHandle expects a vector of bool values");
public:
//! Creates a new SharedDOFDataHandle.
/**
* Creates a new SharedDOFDataHandle and by default initializes the result vector
* with the correct value of false. If you have already done that externally,
* you can skip the initialization.
*
* \param gfs_ The GridFunctionSpace to operate on.
* \param v_ The result vector.
* \param init_vector Flag to control whether the result vector will be initialized.
*/
SharedDOFDataHandle(const GFS& gfs_, V& v_, bool init_vector = true)
: BaseT(gfs_,v_)
{
if (init_vector)
v_ = false;
}
};
//! Data handle for collecting set of neighboring MPI ranks.
/**
* This data handle collects the MPI ranks of all processes that share grid entities
* with attached DOFs.
*
* \note In order to work correctly, the data handle must be communicated on the
* Dune::All_All_Interface.
*/
template<typename GFS, typename RankIndex>
class GFSNeighborDataHandle
: public Dune::CommDataHandleIF<GFSNeighborDataHandle<GFS,RankIndex>,RankIndex>
{
// We deliberately avoid using the GFSDataHandle here, as we don't want to incur the
// overhead of invoking the whole GFS infrastructure.
public:
typedef RankIndex DataType;
typedef typename GFS::Traits::SizeType size_type;
GFSNeighborDataHandle(const GFS& gfs, RankIndex rank, std::set<RankIndex>& neighbors)
: _gfs(gfs)
, _rank(rank)
, _neighbors(neighbors)
{}
bool contains(int dim, int codim) const
{
// Only create neighbor relations for codims used by the GFS.
return _gfs.dataHandleContains(codim);
}
bool fixedsize(int dim, int codim) const
{
// We always send a single value, the MPI rank.
return true;
}
template<typename Entity>
size_type size(Entity& e) const
{
return 1;
}
template<typename MessageBuffer, typename Entity>
void gather(MessageBuffer& buff, const Entity& e) const
{
buff.write(_rank);
}
template<typename MessageBuffer, typename Entity>
void scatter(MessageBuffer& buff, const Entity& e, size_type n)
{
RankIndex rank;
buff.read(rank);
_neighbors.insert(rank);
}
private:
const GFS& _gfs;
const RankIndex _rank;
std::set<RankIndex>& _neighbors;
};
} // namespace PDELab
} // namespace Dune
#endif // DUNE_PDELAB_GENERICDATAHANDLE_HH
|