43 getAllOwnedRanges(
const dftfe::uInt ownedRangeStart,
45 std::vector<dftfe::uInt> &allOwnedRanges,
46 const MPI_Comm &mpiComm)
49 int err = MPI_Comm_size(mpiComm, &nprocs);
50 std::string errMsg =
"Error occured while using MPI_Comm_size. "
54 std::vector<int> recvCounts(nprocs, 2);
55 std::vector<int> displs(nprocs, 0);
56 allOwnedRanges.resize(2 * nprocs);
57 for (
int i = 0; i < nprocs; ++i)
60 std::vector<dftfe::uInt> ownedRanges = {ownedRangeStart,
62 MPI_Allgatherv(&ownedRanges[0],
73 getGhostProcIdToLocalGhostIndicesMap(
74 const std::vector<dftfe::uInt> &ghostIndices,
76 const std::vector<dftfe::uInt> &allOwnedRanges,
78 &ghostProcIdToLocalGhostIndices,
79 const MPI_Comm &mpiComm)
82 int err = MPI_Comm_size(mpiComm, &nprocs);
83 std::string errMsg =
"Error occured while using MPI_Comm_size. "
99 std::vector<dftfe::uInt> locallyOwnedRangesEnd(nprocs);
104 std::vector<dftfe::uInt> locallyOwnedRangesEndProcIds(nprocs);
107 locallyOwnedRangesEnd[i] = allOwnedRanges[2 * i + 1];
111 if (allOwnedRanges[2 * i + 1] == allOwnedRanges[2 * i])
112 locallyOwnedRangesEnd[i] = (nGlobalIndices + 1 + i);
113 locallyOwnedRangesEndProcIds[i] = i;
116 std::sort(locallyOwnedRangesEndProcIds.begin(),
117 locallyOwnedRangesEndProcIds.end(),
119 return locallyOwnedRangesEnd[x] <
120 locallyOwnedRangesEnd[y];
123 std::sort(locallyOwnedRangesEnd.begin(), locallyOwnedRangesEnd.end());
126 for (
dftfe::uInt iGhost = 0; iGhost < numGhosts; ++iGhost)
129 auto up = std::upper_bound(locallyOwnedRangesEnd.begin(),
130 locallyOwnedRangesEnd.end(),
132 std::string msg =
"Ghost index " + std::to_string(ghostIndex) +
133 " not found in any of the processors";
136 std::distance(locallyOwnedRangesEnd.begin(), up);
137 dftfe::uInt procId = locallyOwnedRangesEndProcIds[upPos];
138 ghostProcIdToLocalGhostIndices[procId].push_back(iGhost);
143 checkContiguity(
const std::vector<dftfe::uInt> &v)
146 bool returnValue =
true;
149 if ((v[i] - 1) != v[i - 1])
166 compareRangeMetaData(
const RangeMetaData &x,
const RangeMetaData &y)
169 return (!x.isRangeStart);
173 std::vector<dftfe::uInt>
174 getOverlappingRangeIds(
const std::vector<dftfe::uInt> &ranges)
177 std::vector<RangeMetaData> rangeMetaDataVec(0);
181 left.Id = ranges[2 * i];
183 left.isRangeStart =
true;
186 right.Id = ranges[2 * i + 1];
188 right.isRangeStart =
false;
191 if (left.Id != right.Id)
193 rangeMetaDataVec.push_back(left);
194 rangeMetaDataVec.push_back(right);
197 std::sort(rangeMetaDataVec.begin(),
198 rangeMetaDataVec.end(),
199 compareRangeMetaData);
202 std::vector<dftfe::uInt> returnValue(0);
203 for (
dftfe::uInt i = 0; i < rangeMetaDataVec.size(); ++i)
206 if (rangeMetaDataVec[i].isRangeStart)
208 if (currentOpen == -1)
210 currentOpen = rangeId;
217 returnValue.push_back(currentOpen);
220 returnValue.push_back(rangeId);
221 if (ranges[2 * rangeId + 1] > ranges[2 * currentOpen + 1])
223 currentOpen = rangeId;
230 if (rangeId == currentOpen)
245 template <dftfe::utils::MemorySpace memorySpace>
247 const std::pair<dftfe::uInt, dftfe::uInt> &locallyOwnedRange,
248 const std::vector<dftfe::uInt> &ghostIndices,
249 const MPI_Comm &mpiComm)
270 std::string errMsg =
"Error occured while using MPI_Comm_size. "
276 errMsg =
"Error occured while using MPI_Comm_rank. "
283 "In processor " + std::to_string(
d_myRank) +
284 ", invalid locally owned range found "
285 "(i.e., the second value in the range is less than the first value).");
295 bool isStrictlyIncreasing = std::is_sorted(ghostIndices.begin(),
297 std::less_equal<>());
299 isStrictlyIncreasing,
300 "In processor " + std::to_string(
d_myRank) +
301 ", the ghost indices passed to MPIPatternP2P is not a strictly increasing set.");
307 std::copy(ghostIndices.begin(),
324 std::vector<dftfe::uInt> overlappingRangeIds =
327 overlappingRangeIds.size() == 0,
328 "Detected overlapping ranges among the locallyOwnedRanges passed "
338 if (ghostIndices.size() > 0)
342 "Detected global ghost index to be larger than (nGlobalIndices-1)");
345 std::map<dftfe::uInt, std::vector<dftfe::uInt>>
346 ghostProcIdToLocalGhostIndices;
347 getGhostProcIdToLocalGhostIndicesMap(ghostIndices,
350 ghostProcIdToLocalGhostIndices,
358 std::vector<dftfe::uInt> flattenedLocalGhostIndicesTmp(0);
359 auto it = ghostProcIdToLocalGhostIndices.begin();
361 for (; it != ghostProcIdToLocalGhostIndices.end(); ++it)
364 const std::vector<dftfe::uInt> localGhostIndicesInGhostProc =
366 bool isContiguous = checkContiguity(localGhostIndicesInGhostProc);
367 std::string msg =
"In rank " + std::to_string(
d_myRank) +
368 ", the local ghost indices that are owned"
371 " does not form a contiguous set.";
374 localGhostIndicesInGhostProc.size();
377 *(localGhostIndicesInGhostProc.begin());
379 *(localGhostIndicesInGhostProc.end() - 1) + 1;
384 std::copy(localGhostIndicesInGhostProc.begin(),
385 localGhostIndicesInGhostProc.end(),
386 back_inserter(flattenedLocalGhostIndicesTmp));
390 std::string msg =
"In rank " + std::to_string(
d_myRank) +
392 " the sizes of ghost indices. Expected size: " +
395 std::to_string(flattenedLocalGhostIndicesTmp.size());
405 &flattenedLocalGhostIndicesTmp[0]);
435 &sendRequests[iGhost]);
436 std::string errMsg =
"Error occured while using MPI_Isend. "
452 &recvRequests[iTarget]);
453 std::string errMsg =
"Error occured while using MPI_Irecv. "
459 if (sendRequests.size() > 0)
463 sendStatuses.data());
464 errMsg =
"Error occured while using MPI_Waitall. "
470 if (recvRequests.size() > 0)
474 recvStatuses.data());
475 errMsg =
"Error occured while using MPI_Waitall. "
488 std::vector<dftfe::uInt> flattenedLocalTargetIndicesTmp(
489 totalOwnedIndicesForTargetProcs, 0);
503 for (
dftfe::uInt iIndex = 0; iIndex < numGhostIndicesInProc;
507 flattenedLocalGhostIndicesTmp[startIndex + iIndex];
514 ghostIndices[ghostLocalIndex];
517 localIndicesForGhostProc[startIndex + iIndex] =
518 (
dftfe::uInt)(ghostGlobalIndex - ghostProcOwnedIndicesStart);
521 localIndicesForGhostProc[startIndex + iIndex] <
527 err = MPI_Isend(&localIndicesForGhostProc[startIndex],
528 numGhostIndicesInProc,
530 localIndicesForGhostProc.data()),
534 &sendRequests[iGhost]);
535 std::string errMsg =
"Error occured while using MPI_Isend. "
539 startIndex += numGhostIndicesInProc;
548 err = MPI_Irecv(&flattenedLocalTargetIndicesTmp[startIndex],
549 numOwnedIndicesForTarget,
551 flattenedLocalTargetIndicesTmp.data()),
555 &recvRequests[iTarget]);
556 std::string errMsg =
"Error occured while using MPI_Irecv. "
560 startIndex += numOwnedIndicesForTarget;
563 if (sendRequests.size() > 0)
567 sendStatuses.data());
568 errMsg =
"Error occured while using MPI_Waitall. "
574 if (recvRequests.size() > 0)
578 recvStatuses.data());
579 errMsg =
"Error occured while using MPI_Waitall. "
585 for (
dftfe::uInt i = 0; i < totalOwnedIndicesForTargetProcs; ++i)
589 "Detected local owned target index to be larger than (nLocallyOwnedIndices-1)");
593 if (totalOwnedIndicesForTargetProcs > 0)
594 memoryTransfer.
copy(totalOwnedIndicesForTargetProcs,
596 &flattenedLocalTargetIndicesTmp[0]);
607 template <dftfe::utils::MemorySpace memorySpace>
630 "In processor " + std::to_string(
d_myRank) +
631 ", invalid locally owned range found "
632 "(i.e., the second value in the range is less than the first value).");
647 template <dftfe::utils::MemorySpace memorySpace>
648 std::pair<dftfe::uInt, dftfe::uInt>
654 template <dftfe::utils::MemorySpace memorySpace>
655 const std::vector<dftfe::uInt> &
661 template <dftfe::utils::MemorySpace memorySpace>
662 const std::vector<dftfe::uInt> &
668 template <dftfe::utils::MemorySpace memorySpace>
669 const std::vector<dftfe::uInt> &
676 template <dftfe::utils::MemorySpace memorySpace>
677 const std::vector<dftfe::uInt> &
683 template <dftfe::utils::MemorySpace memorySpace>
693 numGhostIndicesInProc = *itNumGhostIndices;
694 if (procId == *itProcIds)
701 "The processor Id " + std::to_string(procId) +
702 " does not contain any ghost indices for the current processor"
703 " (i.e., processor Id " +
707 return numGhostIndicesInProc;
710 template <dftfe::utils::MemorySpace memorySpace>
721 numGhostIndicesInProc = *itNumGhostIndices;
722 if (procId == *itProcIds)
725 cumulativeIndices += numGhostIndicesInProc;
730 "The processor Id " + std::to_string(procId) +
731 " does not contain any ghost indices for the current processor"
732 " (i.e., processor Id " +
738 numGhostIndicesInProc,
745 template <dftfe::utils::MemorySpace memorySpace>
746 const std::vector<dftfe::uInt> &
752 template <dftfe::utils::MemorySpace memorySpace>
753 const std::vector<dftfe::uInt> &
759 template <dftfe::utils::MemorySpace memorySpace>
766 template <dftfe::utils::MemorySpace memorySpace>
776 numOwnedIndicesForProc = *itNumOwnedIndices;
777 if (procId == *itProcIds)
783 std::string msg =
"There are no owned indices for "
784 " target processor Id " +
785 std::to_string(procId) +
786 " in the current processor"
787 " (i.e., processor Id " +
791 return numOwnedIndicesForProc;
794 template <dftfe::utils::MemorySpace memorySpace>
805 numOwnedIndicesForProc = *itNumOwnedIndices;
806 if (procId == *itProcIds)
809 cumulativeIndices += numOwnedIndicesForProc;
813 std::string msg =
"There are no owned indices for "
814 " target processor Id " +
815 std::to_string(procId) +
816 " in the current processor"
817 " (i.e., processor Id " +
824 numOwnedIndicesForProc,
833 template <dftfe::utils::MemorySpace memorySpace>
840 template <dftfe::utils::MemorySpace memorySpace>
847 template <dftfe::utils::MemorySpace memorySpace>
854 template <dftfe::utils::MemorySpace memorySpace>
861 template <dftfe::utils::MemorySpace memorySpace>
868 template <dftfe::utils::MemorySpace memorySpace>
876 template <dftfe::utils::MemorySpace memorySpace>
894 "In processor " + std::to_string(
d_myRank) +
895 ", the local index " + std::to_string(localId) +
896 " passed to localToGlobal() in MPIPatternP2P is"
897 " larger than number of locally owned plus ghost indices.";
903 template <dftfe::utils::MemorySpace memorySpace>
921 "In processor " + std::to_string(
d_myRank) +
922 ", the global index " + std::to_string(globalId) +
923 " passed to globalToLocal() in MPIPatternP2P is"
924 " neither present in its locally owned range nor in its "
933 template <dftfe::utils::MemorySpace memorySpace>
942 template <dftfe::utils::MemorySpace memorySpace>
952 template <dftfe::utils::MemorySpace memorySpace>
iterator begin()
Return iterator pointing to the beginning of point data.
Definition MemoryStorage.t.cc:130
Definition MemoryTransfer.h:33
static void copy(std::size_t size, ValueType *dst, const ValueType *src)
Copy array from the memory space of source to the memory space of destination.
Definition OptimizedIndexSet.h:44
SizeTypeVector getGhostLocalIndices(const dftfe::uInt procId) const
Definition MPIPatternP2P.t.cc:712
dftfe::uInt localToGlobal(const dftfe::uInt localId) const
Definition MPIPatternP2P.t.cc:878
bool inLocallyOwnedRange(const dftfe::uInt globalId) const
Definition MPIPatternP2P.t.cc:935
OptimizedIndexSet< dftfe::uInt > d_ghostIndicesOptimizedIndexSet
Definition MPIPatternP2P.h:242
std::vector< dftfe::uInt > d_allOwnedRanges
Definition MPIPatternP2P.h:213
dftfe::uInt getNumGhostIndicesInProc(const dftfe::uInt procId) const
Definition MPIPatternP2P.t.cc:685
const std::vector< dftfe::uInt > & getNumGhostIndicesInProcs() const
Definition MPIPatternP2P.t.cc:670
std::vector< dftfe::uInt > d_localGhostIndicesRanges
A vector of size 2 times the number of ghost processors to store the range of local ghost indices tha...
Definition MPIPatternP2P.h:319
MPIPatternP2P(const std::pair< dftfe::uInt, dftfe::uInt > &locallyOwnedRange, const std::vector< dftfe::uInt > &ghostIndices, const MPI_Comm &mpiComm)
Constructor. This constructor is the typical way of creation of an MPI pattern.
Definition MPIPatternP2P.t.cc:246
const std::vector< dftfe::uInt > & getGhostLocalIndicesRanges() const
Definition MPIPatternP2P.t.cc:678
SizeTypeVector d_flattenedLocalTargetIndices
Definition MPIPatternP2P.h:363
dftfe::uInt d_numTargetProcs
Definition MPIPatternP2P.h:326
bool isGhostEntry(const dftfe::uInt globalId) const
Definition MPIPatternP2P.t.cc:944
int d_myRank
Rank of the current processor.
Definition MPIPatternP2P.h:369
int d_nprocs
Number of processors in the MPI Communicator.
Definition MPIPatternP2P.h:366
std::vector< dftfe::uInt > d_ghostProcIds
Definition MPIPatternP2P.h:255
std::vector< dftfe::uInt > d_numOwnedIndicesForTargetProcs
Definition MPIPatternP2P.h:341
dftfe::uInt getNumOwnedIndicesForTargetProc(const dftfe::uInt procId) const
Definition MPIPatternP2P.t.cc:768
std::pair< dftfe::uInt, dftfe::uInt > d_locallyOwnedRange
Definition MPIPatternP2P.h:202
const SizeTypeVector & getOwnedLocalIndicesForTargetProcs() const
Definition MPIPatternP2P.t.cc:761
dftfe::uInt nGlobalIndices() const
Definition MPIPatternP2P.t.cc:856
dftfe::uInt localOwnedSize() const
Definition MPIPatternP2P.t.cc:863
std::vector< dftfe::uInt > d_targetProcIds
Definition MPIPatternP2P.h:333
dftfe::uInt d_numGhostProcs
Definition MPIPatternP2P.h:249
std::vector< dftfe::uInt > d_ghostIndices
Definition MPIPatternP2P.h:229
dftfe::uInt thisProcessId() const
Definition MPIPatternP2P.t.cc:849
bool isCompatible(const MPIPatternP2P< memorySpace > &rhs) const
Definition MPIPatternP2P.t.cc:954
dftfe::uInt nmpiProcesses() const
Definition MPIPatternP2P.t.cc:842
SizeTypeVector d_flattenedLocalGhostIndices
Definition MPIPatternP2P.h:297
utils::MemoryStorage< dftfe::uInt, memorySpace > SizeTypeVector
Definition MPIPatternP2P.h:62
std::pair< dftfe::uInt, dftfe::uInt > getLocallyOwnedRange() const
Definition MPIPatternP2P.t.cc:649
dftfe::uInt localGhostSize() const
Definition MPIPatternP2P.t.cc:870
const MPI_Comm & mpiCommunicator() const
Definition MPIPatternP2P.t.cc:835
std::vector< dftfe::uInt > d_numGhostIndicesInGhostProcs
Definition MPIPatternP2P.h:261
dftfe::uInt globalToLocal(const dftfe::uInt globalId) const
Definition MPIPatternP2P.t.cc:905
SizeTypeVector getOwnedLocalIndices(const dftfe::uInt procId) const
Definition MPIPatternP2P.t.cc:796
const std::vector< dftfe::uInt > & getGhostIndices() const
Definition MPIPatternP2P.t.cc:656
dftfe::uInt d_nGlobalIndices
Definition MPIPatternP2P.h:374
MPI_Comm d_mpiComm
MPI Communicator object.
Definition MPIPatternP2P.h:377
const std::vector< dftfe::uInt > & getNumOwnedIndicesForTargetProcs() const
Definition MPIPatternP2P.t.cc:754
std::set< dftfe::uInt > d_ghostIndicesSetSTL
Definition MPIPatternP2P.h:234
const std::vector< dftfe::uInt > & getTargetProcIds() const
Definition MPIPatternP2P.t.cc:747
dftfe::uInt d_numGhostIndices
Definition MPIPatternP2P.h:223
dftfe::uInt d_numLocallyOwnedIndices
Definition MPIPatternP2P.h:218
const std::vector< dftfe::uInt > & getGhostProcIds() const
Definition MPIPatternP2P.t.cc:663
Definition MPIRequestersNBX.h:38
std::vector< dftfe::uInt > getRequestingRankIds() override
MPI_Datatype mpi_type_id(const int *)
Definition dftfeDataTypes.h:49
Definition MPICommunicatorP2P.h:46
@ MPI_P2P_PATTERN_TAG
Definition MPITags.h:41
void throwException(bool condition, std::string msg="")
Definition pseudoPotentialToDftfeConverter.cc:34
std::uint32_t uInt
Definition TypeConfig.h:10
std::int32_t Int
Definition TypeConfig.h:11