00001 #include <algorithm>
00002 #include "charm++.h"
00003
00004 #ifndef TREE_STRATEGY_3D_TORUS_MIN_BYTES_HOPS
00005 #define TREE_STRATEGY_3D_TORUS_MIN_BYTES_HOPS
00006 namespace topo {
00007
00020 template <typename Iterator,typename ValueType = typename std::iterator_traits<Iterator>::value_type>
00021 class SpanningTreeStrategy_3dTorus_minBytesHops: public SpanningTreeStrategy<Iterator>
00022 {
00023 public:
00024 virtual SpanningTreeVertex* buildNextGen(const Iterator firstVtx, const Iterator beyondLastVtx, const int maxBranches=2) = 0;
00025 };
00026
00027
00028
00031 template <typename Iterator>
00032 class SpanningTreeStrategy_3dTorus_minBytesHops<Iterator,SpanningTreeVertex>: public SpanningTreeStrategy<Iterator>
00033 {
00034 public:
00035 virtual SpanningTreeVertex* buildNextGen(const Iterator firstVtx, const Iterator beyondLastVtx, const int maxBranches=2);
00036 };
00037
00038
00039
00045 template <typename Iterator>
00046 class SpanningTreeStrategy_3dTorus_minBytesHops<Iterator,vtxType>: public SpanningTreeStrategy<Iterator>
00047 {
00048 public:
00049 virtual SpanningTreeVertex* buildNextGen(const Iterator firstVtx, const Iterator beyondLastVtx, const int maxBranches=2)
00050 {
00052 std::vector<SpanningTreeVertex> tree;
00053 for (Iterator itr = firstVtx; itr != beyondLastVtx; itr++)
00054 tree.push_back( SpanningTreeVertex(*itr) );
00056 SpanningTreeStrategy_3dTorus_minBytesHops< std::vector<SpanningTreeVertex>::iterator > theRealBuilder;
00057 SpanningTreeVertex *result = theRealBuilder.buildNextGen(tree.begin(),tree.end(),maxBranches);
00059 int indx=0;
00060 for (Iterator itr = firstVtx; itr != beyondLastVtx; itr++)
00061 {
00062 *itr = tree[indx].id;
00063 indx++;
00064 }
00066 return result;
00067 }
00068 };
00069
00070 }
00071
00072
00073 #include "TopoManager.h"
00074
00075 namespace topo {
00076
00077 template <typename Iterator>
00078 SpanningTreeVertex* SpanningTreeStrategy_3dTorus_minBytesHops<Iterator,SpanningTreeVertex>::buildNextGen(const Iterator firstVtx, const Iterator beyondLastVtx, const int maxBranches)
00079 {
00081 (*firstVtx).childIndex.clear();
00082 (*firstVtx).childIndex.reserve(maxBranches);
00083
00085 TopoManager aTopoMgr;
00087 int numLocalDestinations = -1, numRemoteDestinations = 0;
00088 Iterator beyondLastLocal = firstVtx;
00089
00091 for (Iterator itr = firstVtx; itr != beyondLastVtx; itr++)
00092 {
00093 (*itr).X.reserve(3);
00094 (*itr).X.assign(3,0);
00095 int coreNum;
00096
00097 aTopoMgr.rankToCoordinates( (*itr).id, (*itr).X[0], (*itr).X[1], (*itr).X[2], coreNum );
00099 if (numHops(*firstVtx,*itr) > 0)
00100 numRemoteDestinations++;
00101 else
00102 {
00103 numLocalDestinations++;
00105 if (itr != beyondLastLocal)
00106 std::iter_swap(beyondLastLocal,itr);
00108 beyondLastLocal++;
00109 }
00110 }
00111
00113 int numLocalBranches = 0;
00115 if (numLocalDestinations > 0)
00116 {
00117 numLocalBranches = (numRemoteDestinations >= maxBranches)? 1 : (maxBranches - numRemoteDestinations);
00119 SpanningTreeVertex *localTree = impl::buildNextGen_topoUnaware(firstVtx,beyondLastLocal,numLocalBranches);
00121 for (int i=0,n=localTree->childIndex.size(); i<n; i++)
00122 firstVtx->childIndex.push_back( localTree->childIndex[i] );
00124 delete localTree;
00125 }
00126
00128 impl::TreeBoundingBoxOn3dTorus<Iterator> treePart;
00129 treePart.partition(firstVtx,beyondLastLocal,beyondLastVtx,maxBranches);
00130
00132 for (int i=numLocalBranches, numChildren=(*firstVtx).childIndex.size(); i<numChildren; i++)
00133 {
00134 Iterator rangeStart = firstVtx;
00135 std::advance(rangeStart,(*firstVtx).childIndex[i]);
00136 Iterator rangeEnd = firstVtx;
00137 if (i+1 == numChildren)
00138 rangeEnd = beyondLastVtx;
00139 else
00140 std::advance(rangeEnd, (*firstVtx).childIndex[i+1] );
00141 Iterator closestItr = pickClosest(*firstVtx,rangeStart,rangeEnd);
00142 std::iter_swap(rangeStart,closestItr);
00143 }
00145 return (new SpanningTreeVertex(*firstVtx) );
00146 }
00147
00148 }
00149 #endif // TREE_STRATEGY_3D_TORUS_MIN_BYTES_HOPS
00150