46 #ifndef _ZOLTAN2_COORDCOMMGRAPH_HPP_ 47 #define _ZOLTAN2_COORDCOMMGRAPH_HPP_ 56 #include "Teuchos_CommHelpers.hpp" 57 #include "Teuchos_Comm.hpp" 58 #include "Teuchos_ArrayViewDecl.hpp" 59 #include "Teuchos_RCPDecl.hpp" 65 #define Z2_ABS(x) ((x) >= 0 ? (x) : -(x)) 70 template <
typename scalar_t,
typename partId_t>
88 std::vector <partId_t> *gridIndices;
90 std::set <partId_t> neighbors;
102 gridIndices(0), neighbors(){
106 minHashIndices =
new partId_t [dim];
107 maxHashIndices =
new partId_t [dim];
108 gridIndices =
new std::vector <partId_t> ();
109 for (
int i = 0; i < dim; ++i){
110 lmins[i] = -this->maxScalar;
111 lmaxs[i] = this->maxScalar;
122 _EPSILON(
std::numeric_limits<
scalar_t>::epsilon()),
125 gridIndices(0), neighbors(){
128 minHashIndices =
new partId_t [dim];
129 maxHashIndices =
new partId_t [dim];
130 gridIndices =
new std::vector <partId_t> ();
131 for (
int i = 0; i < dim; ++i){
146 _EPSILON(
std::numeric_limits<
scalar_t>::epsilon()),
149 gridIndices(0), neighbors(){
153 minHashIndices =
new partId_t [dim];
154 maxHashIndices =
new partId_t [dim];
155 gridIndices =
new std::vector <partId_t> ();
158 for (
int i = 0; i < dim; ++i){
159 lmins[i] = othermins[i];
160 lmaxs[i] = othermaxs[i];
166 delete []this->lmins;
167 delete [] this->lmaxs;
168 delete []this->minHashIndices;
169 delete [] this->maxHashIndices;
205 return this->gridIndices;
211 return &(this->neighbors);
223 for (
int i = 0; i < dim; ++i){
225 if (omins[i] - this->lmaxs[i] > _EPSILON || this->lmins[i] - omaxs[i] > _EPSILON ){
228 else if (
Z2_ABS(omins[i] - this->lmaxs[i]) < _EPSILON ||
Z2_ABS(this->lmins[i] - omaxs[i]) < _EPSILON ){
238 std::cout <<
"something is wrong: equality:" << equality << std::endl;
247 neighbors.insert(nIndex);
253 if (neighbors.end() != neighbors.find(nIndex)){
268 for (
int j = 0; j < dim; ++j){
269 scalar_t distance = (lmins[j] - minMaxBoundaries[j]);
271 if (distance > _EPSILON && sliceSizes[j] > _EPSILON){
272 minInd = (lmins[j] - minMaxBoundaries[j])/ sliceSizes[j];
275 if(minInd >= numSlicePerDim){
276 minInd = numSlicePerDim - 1;
279 distance = (lmaxs[j] - minMaxBoundaries[j]);
280 if (distance > _EPSILON && sliceSizes[j] > _EPSILON){
281 maxInd = (lmaxs[j] - minMaxBoundaries[j])/ sliceSizes[j];
283 if(maxInd >= numSlicePerDim){
284 maxInd = numSlicePerDim - 1;
289 minHashIndices[j] = minInd;
290 maxHashIndices[j] = maxInd;
292 std::vector <partId_t> *in =
new std::vector <partId_t> ();
294 std::vector <partId_t> *out =
new std::vector <partId_t> ();
296 for (
int j = 0; j < dim; ++j){
298 partId_t minInd = minHashIndices[j];
299 partId_t maxInd = maxHashIndices[j];
306 for (
partId_t k = minInd; k <= maxInd; ++k){
307 for (
partId_t i = 0; i < inSize; ++i){
308 out->push_back((*in)[i] + k * pScale);
312 std::vector <partId_t> *tmp = in;
317 std::vector <partId_t> *tmp = in;
329 for(
int i = 0; i < this->dim; ++i){
330 cout <<
"\tbox:" << this->pID <<
" dim:" << i <<
" min:" << lmins[i] <<
" max:" << lmaxs[i] << endl;
338 lmaxs[dimInd] = newBoundary;
341 lmins[dimInd] = newBoundary;
345 template <
typename tt>
347 std::stringstream ss (std::stringstream::in |std::stringstream::out);
349 std::string tmp =
"";
358 int numCorners = (int(1)<<dim);
362 for (
int i = 0; i < dim; ++i){
374 mm << lmins[i] <<
" ";
378 for (
int i = 0; i < dim; ++i){
392 mm << lmaxs[i] <<
" ";
397 for (
int j = 0; j < numCorners; ++j){
398 std::vector <int> neighborCorners;
399 for (
int i = 0; i < dim; ++i){
400 if(
int(j & (
int(1)<<i)) == 0){
401 corner1[i] = lmins[i];
404 corner1[i] = lmaxs[i];
406 if (j % (
int(1)<<(i + 1)) >= (int(1)<<i)){
407 int c1 = j - (int(1)<<i);
410 neighborCorners.push_back(c1);
415 int c1 = j + (int(1)<<i);
416 if (c1 < (
int(1) << dim)) {
417 neighborCorners.push_back(c1);
422 for (
int m = 0; m < int (neighborCorners.size()); ++m){
424 int n = neighborCorners[m];
426 for (
int i = 0; i < dim; ++i){
427 if(
int(n & (
int(1)<<i)) == 0){
428 corner2[i] = lmins[i];
431 corner2[i] = lmaxs[i];
435 std::string arrowline =
"set arrow from ";
436 for (
int i = 0; i < dim - 1; ++i){
437 arrowline += toString<scalar_t>(corner1[i]) +
",";
439 arrowline += toString<scalar_t>(corner1[dim -1]) +
" to ";
441 for (
int i = 0; i < dim - 1; ++i){
442 arrowline += toString<scalar_t>(corner2[i]) +
",";
444 arrowline += toString<scalar_t>(corner2[dim -1]) +
" nohead\n";
460 template <
typename scalar_t,
typename partId_t>
464 RCP < std::vector <Zoltan2::coordinateModelPartBox <scalar_t, partId_t> > > pBoxes;
479 std::vector <std::vector <partId_t> > grids;
481 ArrayRCP <partId_t> comXAdj;
482 ArrayRCP <partId_t> comAdj;
492 maxMinBoundaries(0), sliceSizes(0),
495 numSlicePerDim(
partId_t(pow(double(ntasks_), 1.0 / dim))),
500 minMaxBoundaries =
new scalar_t[dim];
501 maxMinBoundaries =
new scalar_t[dim];
505 if (numSlicePerDim == 0) numSlicePerDim = 1;
507 numGrids =
partId_t(pow(
float(numSlicePerDim),
int(dim)));
510 std::vector <std::vector <partId_t> > grids_ (numGrids);
511 this->grids = grids_;
513 this->getMinMaxBoundaries();
515 this->insertToHash();
517 partId_t nCount = this->calculateNeighbors();
520 ArrayRCP <partId_t> tmpComXadj(ntasks_);
521 ArrayRCP <partId_t> tmpComAdj(nCount);
522 comXAdj = tmpComXadj;
525 this->fillAdjArrays();
533 delete []minMaxBoundaries;
534 delete []maxMinBoundaries;
545 for(
partId_t i = 0; i < this->nTasks; ++i){
546 std::set<partId_t> *neigbors = (*pBoxes)[i].getNeighbors();
552 comXAdj[i] += comXAdj[i - 1];
554 typedef typename std::set<partId_t> mySet;
555 typedef typename mySet::iterator myIT;
557 for (it=neigbors->begin(); it!=neigbors->end(); ++it)
559 comAdj[adjIndex++] = *it;
572 ArrayRCP <partId_t> &comXAdj_,
573 ArrayRCP <partId_t> &comAdj_){
574 comXAdj_ = this->comXAdj;
575 comAdj_ = this->comAdj;
583 for(
partId_t i = 0; i < this->nTasks; ++i){
584 std::vector <partId_t> *gridIndices =(*pBoxes)[i].getGridIndices();
585 partId_t gridCount = gridIndices->size();
587 for (
partId_t j = 0; j < gridCount; ++j){
589 partId_t boxCount = grids[grid].size();
590 for (
partId_t k = 0; k < boxCount; ++k){
593 if((!(*pBoxes)[i].isAlreadyNeighbor(boxIndex))&& (*pBoxes)[i].isNeighborWith((*pBoxes)[boxIndex])){
595 (*pBoxes)[i].addNeighbor(boxIndex);
596 (*pBoxes)[boxIndex].addNeighbor(i);
613 for(
partId_t i = 0; i < this->nTasks; ++i){
614 (*pBoxes)[i].setMinMaxHashIndices(minMaxBoundaries, sliceSizes, numSlicePerDim);
616 std::vector <partId_t> *gridIndices =(*pBoxes)[i].getGridIndices();
618 partId_t gridCount = gridIndices->size();
620 for (
partId_t j = 0; j < gridCount; ++j){
624 (grids)[grid].push_back(i);
645 scalar_t *mins = (*pBoxes)[0].getlmins();
646 scalar_t *maxs = (*pBoxes)[0].getlmaxs();
648 for (
int j = 0; j < dim; ++j){
649 minMaxBoundaries[j] = maxs[j];
650 maxMinBoundaries[j] = mins[j];
653 for (
partId_t i = 1; i < nTasks; ++i){
655 mins = (*pBoxes)[i].getlmins();
656 maxs = (*pBoxes)[i].getlmaxs();
658 for (
int j = 0; j < dim; ++j){
660 if (minMaxBoundaries[j] > maxs[j]){
661 minMaxBoundaries[j] = maxs[j];
663 if (maxMinBoundaries[j] < mins[j]){
664 maxMinBoundaries[j] = mins[j];
670 for (
int j = 0; j < dim; ++j){
671 sliceSizes[j] = (maxMinBoundaries[j] - minMaxBoundaries[j]) / numSlicePerDim;
672 if (sliceSizes[j] < 0) sliceSizes[j] = 0;
GridHash Class, Hashing Class for part boxes.
coordinateModelPartBox(partId_t pid, int dim_, scalar_t *lmi, scalar_t *lma)
Constructor deep copy of the maximum and minimum boundaries.
void updateMinMax(scalar_t newBoundary, int isMax, int dimInd)
function to update the boundary of the box.
int getDim() const
function to set the dimension
~GridHash()
GridHash Class, Destructor.
void fillAdjArrays()
GridHash Class, Function to fill adj arrays.
zoltan2_partId_t partId_t
void setMinMaxHashIndices(scalar_t *minMaxBoundaries, scalar_t *sliceSizes, partId_t numSlicePerDim)
function to obtain the min and max hash values along all dimensions.
zoltan2_partId_t partId_t
std::vector< partId_t > * getGridIndices()
function to get the indices of the buckets that the part is inserted to
GridHash(RCP< std::vector< Zoltan2::coordinateModelPartBox< scalar_t, partId_t > > > pBoxes_, partId_t ntasks_, int dim_)
GridHash Class, Constructor.
coordinateModelPartBox Class, represents the boundaries of the box which is a result of a geometric p...
void insertToHash()
GridHash Class, For each box calculates the buckets which it should be inserted to.
~coordinateModelPartBox()
Destructor.
partId_t getpId() const
function to get the part id
std::set< partId_t > * getNeighbors()
function to get the indices of the neighboring parts.
coordinateModelPartBox(partId_t pid, int dim_)
Constructor.
void addNeighbor(partId_t nIndex)
function to add a new neighbor to the neighbor list.
scalar_t * getlmaxs() const
function to get maximum values along all dimensions
void setpId(partId_t pid)
function to set the part id
bool isNeighborWith(const coordinateModelPartBox< scalar_t, partId_t > &other)
function to check if two boxes are neighbors.
void getMinMaxBoundaries()
GridHash Class, calculates the minimum of maximum box boundaries, and maxium of minimum box boundarie...
bool isAlreadyNeighbor(partId_t nIndex)
function to check if a given part is already in the neighbor list.
coordinateModelPartBox(const coordinateModelPartBox< scalar_t, partId_t > &other)
Copy Constructor deep copy of the maximum and minimum boundaries.
partId_t calculateNeighbors()
GridHash Class, For each box compares the adjacency against the boxes that are in the same buckets...
std::string toString(tt obj)
scalar_t * getlmins() const
function to get minimum values along all dimensions
void print()
function to print the boundaries.
void getAdjArrays(ArrayRCP< partId_t > &comXAdj_, ArrayRCP< partId_t > &comAdj_)
GridHash Class, returns the adj arrays.
void writeGnuPlot(std::ofstream &file, std::ofstream &mm)
function for visualization.