Zoltan2  Version of the Day
Zoltan2_CoordinatePartitioningGraph.hpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // Zoltan2: A package of combinatorial algorithms for scientific computing
6 // Copyright 2012 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Karen Devine (kddevin@sandia.gov)
39 // Erik Boman (egboman@sandia.gov)
40 // Siva Rajamanickam (srajama@sandia.gov)
41 //
42 // ***********************************************************************
43 //
44 // @HEADER
45 
46 #ifndef _ZOLTAN2_COORDCOMMGRAPH_HPP_
47 #define _ZOLTAN2_COORDCOMMGRAPH_HPP_
48 
49 
50 #include <cmath>
51 #include <limits>
52 #include <iostream>
53 #include <vector>
54 #include <set>
55 #include <fstream>
56 #include "Teuchos_CommHelpers.hpp"
57 #include "Teuchos_Comm.hpp"
58 #include "Teuchos_ArrayViewDecl.hpp"
59 #include "Teuchos_RCPDecl.hpp"
60 
61 using namespace std;
62 namespace Zoltan2{
63 
64 
65 #define Z2_ABS(x) ((x) >= 0 ? (x) : -(x))
66 
70 template <typename scalar_t,typename partId_t>
72 
73  partId_t pID; //part Id
74  int dim; //dimension of the box
75  scalar_t *lmins; //minimum boundaries of the box along all dimensions.
76  scalar_t *lmaxs; //maximum boundaries of the box along all dimensions.
77  scalar_t maxScalar;
78  scalar_t _EPSILON;
79 
80  //to calculate the neighbors of the box and avoid the p^2 comparisons,
81  //we use hashing. A box can be put into multiple hash buckets.
82  //the following 2 variable holds the minimum and maximum of the
83  //hash values along all dimensions.
84  partId_t *minHashIndices;
85  partId_t *maxHashIndices;
86 
87  //result hash bucket indices.
88  std::vector <partId_t> *gridIndices;
89  //neighbors of the box.
90  std::set <partId_t> neighbors;
91 public:
95  pID(pid),
96  dim(dim_),
97  lmins(0), lmaxs(0),
98  maxScalar (std::numeric_limits<scalar_t>::max()),
99  _EPSILON(std::numeric_limits<scalar_t>::epsilon()),
100  minHashIndices(0),
101  maxHashIndices(0),
102  gridIndices(0), neighbors(){
103  lmins = new scalar_t [dim];
104  lmaxs = new scalar_t [dim];
105 
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;
112  }
113  }
117  coordinateModelPartBox(partId_t pid, int dim_, scalar_t *lmi, scalar_t *lma):
118  pID(pid),
119  dim(dim_),
120  lmins(0), lmaxs(0),
121  maxScalar (std::numeric_limits<scalar_t>::max()),
122  _EPSILON(std::numeric_limits<scalar_t>::epsilon()),
123  minHashIndices(0),
124  maxHashIndices(0),
125  gridIndices(0), neighbors(){
126  lmins = new scalar_t [dim];
127  lmaxs = new scalar_t [dim];
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){
132  lmins[i] = lmi[i];
133  lmaxs[i] = lma[i];
134  }
135  }
136 
137 
142  pID(0),
143  dim(other.getDim()),
144  lmins(0), lmaxs(0),
145  maxScalar (std::numeric_limits<scalar_t>::max()),
146  _EPSILON(std::numeric_limits<scalar_t>::epsilon()),
147  minHashIndices(0),
148  maxHashIndices(0),
149  gridIndices(0), neighbors(){
150 
151  lmins = new scalar_t [dim];
152  lmaxs = new scalar_t [dim];
153  minHashIndices = new partId_t [dim];
154  maxHashIndices = new partId_t [dim];
155  gridIndices = new std::vector <partId_t> ();
156  scalar_t *othermins = other.getlmins();
157  scalar_t *othermaxs = other.getlmaxs();
158  for (int i = 0; i < dim; ++i){
159  lmins[i] = othermins[i];
160  lmaxs[i] = othermaxs[i];
161  }
162  }
166  delete []this->lmins;
167  delete [] this->lmaxs;
168  delete []this->minHashIndices;
169  delete [] this->maxHashIndices;
170  delete gridIndices;
171  }
172 
175  void setpId(partId_t pid){
176  this->pID = pid;
177  }
180  partId_t getpId() const{
181  return this->pID;
182  }
183 
184 
187  int getDim()const{
188  return this->dim;
189  }
192  scalar_t * getlmins()const{
193  return this->lmins;
194  }
197  scalar_t * getlmaxs()const{
198  return this->lmaxs;
199  }
200 
204  std::vector <partId_t> * getGridIndices (){
205  return this->gridIndices;
206  }
207 
210  std::set<partId_t> *getNeighbors(){
211  return &(this->neighbors);
212  }
213 
217 
218 
219  scalar_t *omins = other.getlmins();
220  scalar_t *omaxs = other.getlmaxs();
221 
222  int equality = 0;
223  for (int i = 0; i < dim; ++i){
224 
225  if (omins[i] - this->lmaxs[i] > _EPSILON || this->lmins[i] - omaxs[i] > _EPSILON ){
226  return false;
227  }
228  else if (Z2_ABS(omins[i] - this->lmaxs[i]) < _EPSILON || Z2_ABS(this->lmins[i] - omaxs[i]) < _EPSILON ){
229  if (++equality > 1){
230  return false;
231  }
232  }
233  }
234  if (equality == 1) {
235  return true;
236  }
237  else {
238  std::cout << "something is wrong: equality:" << equality << std::endl;
239  return false;
240  }
241  }
242 
243 
246  void addNeighbor(partId_t nIndex){
247  neighbors.insert(nIndex);
248  }
252 
253  if (neighbors.end() != neighbors.find(nIndex)){
254  return true;
255  }
256  return false;
257 
258  }
259 
260 
264  scalar_t *minMaxBoundaries,
265  scalar_t *sliceSizes,
266  partId_t numSlicePerDim
267  ){
268  for (int j = 0; j < dim; ++j){
269  scalar_t distance = (lmins[j] - minMaxBoundaries[j]);
270  partId_t minInd = 0;
271  if (distance > _EPSILON && sliceSizes[j] > _EPSILON){
272  minInd = (lmins[j] - minMaxBoundaries[j])/ sliceSizes[j];
273  }
274 
275  if(minInd >= numSlicePerDim){
276  minInd = numSlicePerDim - 1;
277  }
278  partId_t maxInd = 0;
279  distance = (lmaxs[j] - minMaxBoundaries[j]);
280  if (distance > _EPSILON && sliceSizes[j] > _EPSILON){
281  maxInd = (lmaxs[j] - minMaxBoundaries[j])/ sliceSizes[j];
282  }
283  if(maxInd >= numSlicePerDim){
284  maxInd = numSlicePerDim - 1;
285  }
286 
287  //cout << "j:" << j << " lmins:" << lmins[j] << " lmaxs:" << lmaxs[j] << endl;
288  //cout << "j:" << j << " min:" << minInd << " max:" << maxInd << endl;
289  minHashIndices[j] = minInd;
290  maxHashIndices[j] = maxInd;
291  }
292  std::vector <partId_t> *in = new std::vector <partId_t> ();
293  in->push_back(0);
294  std::vector <partId_t> *out = new std::vector <partId_t> ();
295 
296  for (int j = 0; j < dim; ++j){
297 
298  partId_t minInd = minHashIndices[j];
299  partId_t maxInd = maxHashIndices[j];
300 
301 
302  partId_t pScale = partId_t(pow (float(numSlicePerDim), int(dim - j -1)));
303 
304  partId_t inSize = in->size();
305 
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);
309  }
310  }
311  in->clear();
312  std::vector <partId_t> *tmp = in;
313  in= out;
314  out= tmp;
315  }
316 
317  std::vector <partId_t> *tmp = in;
318  in = gridIndices;
319  gridIndices = tmp;
320 
321 
322  delete in;
323  delete out;
324  }
325 
328  void print(){
329  for(int i = 0; i < this->dim; ++i){
330  cout << "\tbox:" << this->pID << " dim:" << i << " min:" << lmins[i] << " max:" << lmaxs[i] << endl;
331  }
332  }
333 
336  void updateMinMax (scalar_t newBoundary, int isMax, int dimInd){
337  if (isMax){
338  lmaxs[dimInd] = newBoundary;
339  }
340  else {
341  lmins[dimInd] = newBoundary;
342  }
343  }
344 
345  template <typename tt>
346  std::string toString(tt obj){
347  std::stringstream ss (std::stringstream::in |std::stringstream::out);
348  ss << obj;
349  std::string tmp = "";
350  ss >> tmp;
351  return tmp;
352  }
353 
354 
357  void writeGnuPlot(std::ofstream &file,std::ofstream &mm){
358  int numCorners = (int(1)<<dim);
359  scalar_t *corner1 = new scalar_t [dim];
360  scalar_t *corner2 = new scalar_t [dim];
361 
362  for (int i = 0; i < dim; ++i){
363  /*
364  if (-maxScalar == lmins[i]){
365  if (lmaxs[i] > 0){
366  lmins[i] = lmaxs[i] / 2;
367  }
368  else{
369  lmins[i] = lmaxs[i] * 2;
370  }
371  }
372  */
373  //std::cout << lmins[i] << " ";
374  mm << lmins[i] << " ";
375  }
376  //std::cout << std::endl;
377  mm << std::endl;
378  for (int i = 0; i < dim; ++i){
379 
380  /*
381  if (maxScalar == lmaxs[i]){
382  if (lmins[i] < 0){
383  lmaxs[i] = lmins[i] / 2;
384  }
385  else{
386  lmaxs[i] = lmins[i] * 2;
387  }
388  }
389  */
390 
391  //std::cout << lmaxs[i] << " ";
392  mm << lmaxs[i] << " ";
393  }
394  //std::cout << std::endl;
395  mm << std::endl;
396 
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];
402  }
403  else {
404  corner1[i] = lmaxs[i];
405  }
406  if (j % (int(1)<<(i + 1)) >= (int(1)<<i)){
407  int c1 = j - (int(1)<<i);
408 
409  if (c1 > 0) {
410  neighborCorners.push_back(c1);
411  }
412  }
413  else {
414 
415  int c1 = j + (int(1)<<i);
416  if (c1 < (int(1) << dim)) {
417  neighborCorners.push_back(c1);
418  }
419  }
420  }
421  //std::cout << "me:" << j << " nc:" << int (neighborCorners.size()) << std::endl;
422  for (int m = 0; m < int (neighborCorners.size()); ++m){
423 
424  int n = neighborCorners[m];
425  //std::cout << "me:" << j << " n:" << n << std::endl;
426  for (int i = 0; i < dim; ++i){
427  if(int(n & (int(1)<<i)) == 0){
428  corner2[i] = lmins[i];
429  }
430  else {
431  corner2[i] = lmaxs[i];
432  }
433  }
434 
435  std::string arrowline = "set arrow from ";
436  for (int i = 0; i < dim - 1; ++i){
437  arrowline += toString<scalar_t>(corner1[i]) + ",";
438  }
439  arrowline += toString<scalar_t>(corner1[dim -1]) + " to ";
440 
441  for (int i = 0; i < dim - 1; ++i){
442  arrowline += toString<scalar_t>(corner2[i]) + ",";
443  }
444  arrowline += toString<scalar_t>(corner2[dim -1]) + " nohead\n";
445 
446  file << arrowline;
447  }
448  }
449  delete []corner1;
450  delete []corner2;
451  }
452 
453 
454 };
455 
456 
460 template <typename scalar_t, typename partId_t>
461 class GridHash{
462 private:
463 
464  RCP < std::vector <Zoltan2::coordinateModelPartBox <scalar_t, partId_t> > > pBoxes;
465 
466  //minimum of the maximum box boundaries
467  scalar_t *minMaxBoundaries;
468  //maximum of the minimum box boundaries
469  scalar_t *maxMinBoundaries;
470  //the size of each slice along dimensions
471  scalar_t *sliceSizes;
472  partId_t nTasks;
473  int dim;
474  //the number of slices per dimension
475  partId_t numSlicePerDim;
476  //the number of grids - buckets
477  partId_t numGrids;
478  //hash vector
479  std::vector <std::vector <partId_t> > grids;
480  //result communication graph.
481  ArrayRCP <partId_t> comXAdj;
482  ArrayRCP <partId_t> comAdj;
483 public:
484 
489  partId_t ntasks_, int dim_):
490  pBoxes(pBoxes_),
491  minMaxBoundaries(0),
492  maxMinBoundaries(0), sliceSizes(0),
493  nTasks(ntasks_),
494  dim(dim_),
495  numSlicePerDim(partId_t(pow(double(ntasks_), 1.0 / dim))),
496  numGrids(0),
497  grids(),
498  comXAdj(), comAdj(){
499 
500  minMaxBoundaries = new scalar_t[dim];
501  maxMinBoundaries = new scalar_t[dim];
502  sliceSizes = new scalar_t[dim];
503  //calculate the number of slices in each dimension.
504  numSlicePerDim /= 2;
505  if (numSlicePerDim == 0) numSlicePerDim = 1;
506 
507  numGrids = partId_t(pow(float(numSlicePerDim), int(dim)));
508 
509  //allocate memory for buckets.
510  std::vector <std::vector <partId_t> > grids_ (numGrids);
511  this->grids = grids_;
512  //get the boundaries of buckets.
513  this->getMinMaxBoundaries();
514  //insert boxes to buckets
515  this->insertToHash();
516  //calculate the neighbors for each bucket.
517  partId_t nCount = this->calculateNeighbors();
518 
519  //allocate memory for communication graph
520  ArrayRCP <partId_t> tmpComXadj(ntasks_);
521  ArrayRCP <partId_t> tmpComAdj(nCount);
522  comXAdj = tmpComXadj;
523  comAdj = tmpComAdj;
524  //fill communication graph
525  this->fillAdjArrays();
526  }
527 
528 
533  delete []minMaxBoundaries;
534  delete []maxMinBoundaries;
535  delete []sliceSizes;
536  }
537 
542 
543  partId_t adjIndex = 0;
544 
545  for(partId_t i = 0; i < this->nTasks; ++i){
546  std::set<partId_t> *neigbors = (*pBoxes)[i].getNeighbors();
547 
548  partId_t s = neigbors->size();
549 
550  comXAdj[i] = s;
551  if (i > 0){
552  comXAdj[i] += comXAdj[i - 1];
553  }
554  typedef typename std::set<partId_t> mySet;
555  typedef typename mySet::iterator myIT;
556  myIT it;
557  for (it=neigbors->begin(); it!=neigbors->end(); ++it)
558 
559  comAdj[adjIndex++] = *it;
560  //TODO not needed anymore.
561  neigbors->clear();
562  }
563 
564  }
565 
566 
567 
572  ArrayRCP <partId_t> &comXAdj_,
573  ArrayRCP <partId_t> &comAdj_){
574  comXAdj_ = this->comXAdj;
575  comAdj_ = this->comAdj;
576  }
577 
582  partId_t nCount = 0;
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();
586 
587  for (partId_t j = 0; j < gridCount; ++j){
588  partId_t grid = (*gridIndices)[j];
589  partId_t boxCount = grids[grid].size();
590  for (partId_t k = 0; k < boxCount; ++k){
591  partId_t boxIndex = grids[grid][k];
592  if (boxIndex > i){
593  if((!(*pBoxes)[i].isAlreadyNeighbor(boxIndex))&& (*pBoxes)[i].isNeighborWith((*pBoxes)[boxIndex])){
594  //cout << "i:" << i << " n:" << boxIndex << " are neighbors."<< endl;
595  (*pBoxes)[i].addNeighbor(boxIndex);
596  (*pBoxes)[boxIndex].addNeighbor(i);
597  nCount += 2;
598  }
599  }
600  }
601  }
602  }
603 
604  return nCount;
605  }
606 
610  void insertToHash(){
611 
612  //cout << "ntasks:" << this->nTasks << endl;
613  for(partId_t i = 0; i < this->nTasks; ++i){
614  (*pBoxes)[i].setMinMaxHashIndices(minMaxBoundaries, sliceSizes, numSlicePerDim);
615 
616  std::vector <partId_t> *gridIndices =(*pBoxes)[i].getGridIndices();
617 
618  partId_t gridCount = gridIndices->size();
619  //cout << "i:" << i << " gridsize:" << gridCount << endl;
620  for (partId_t j = 0; j < gridCount; ++j){
621  partId_t grid = (*gridIndices)[j];
622 
623  //cout << "i:" << i << " is being inserted to:" << grid << endl;
624  (grids)[grid].push_back(i);
625  }
626  }
627 
628 
629 /*
630  for(partId_t i = 0; i < grids.size(); ++i){
631  cout << "grid:" << i << " gridsuze:" << (grids)[i].size() << " elements:";
632  for(partId_t j = 0; j < (grids)[i].size(); ++j){
633  cout <<(grids)[i][j] << " ";
634  }
635  cout << endl;
636 
637  }
638 */
639  }
640 
645  scalar_t *mins = (*pBoxes)[0].getlmins();
646  scalar_t *maxs = (*pBoxes)[0].getlmaxs();
647 
648  for (int j = 0; j < dim; ++j){
649  minMaxBoundaries[j] = maxs[j];
650  maxMinBoundaries[j] = mins[j];
651  }
652 
653  for (partId_t i = 1; i < nTasks; ++i){
654 
655  mins = (*pBoxes)[i].getlmins();
656  maxs = (*pBoxes)[i].getlmaxs();
657 
658  for (int j = 0; j < dim; ++j){
659 
660  if (minMaxBoundaries[j] > maxs[j]){
661  minMaxBoundaries[j] = maxs[j];
662  }
663  if (maxMinBoundaries[j] < mins[j]){
664  maxMinBoundaries[j] = mins[j];
665  }
666  }
667  }
668 
669 
670  for (int j = 0; j < dim; ++j){
671  sliceSizes[j] = (maxMinBoundaries[j] - minMaxBoundaries[j]) / numSlicePerDim;
672  if (sliceSizes[j] < 0) sliceSizes[j] = 0;
673  /*
674  cout << "dim:" << j <<
675  " minMax:" << minMaxBoundaries[j] <<
676  " maxMin:" << maxMinBoundaries[j] <<
677  " sliceSizes:" << sliceSizes[j] << endl;
678  */
679  }
680  }
681 
682 
683 };
684 /*
685 template <typename scalar_t,typename partId_t>
686 class coordinatePartBox{
687 public:
688  partId_t pID;
689  int dim;
690  int numCorners;
691  scalar_t **corners;
692  scalar_t *lmins, *gmins;
693  scalar_t *lmaxs, *gmaxs;
694  scalar_t maxScalar;
695  std::vector <partId_t> hash_indices;
696  coordinatePartBox(partId_t pid, int dim_, scalar_t *lMins, scalar_t *gMins,
697  scalar_t *lMaxs, scalar_t *gMaxs):
698  pID(pid),
699  dim(dim_),
700  numCorners(int(pow(2, dim_))),
701  corners(0),
702  lmins(lMins), gmins(gMins), lmaxs(lMaxs), gmaxs(gMaxs),
703  maxScalar (std::numeric_limits<scalar_t>::max()){
704  this->corners = new scalar_t *[dim];
705  for (int i = 0; i < dim; ++i){
706  this->corners[i] = new scalar_t[this->numCorners];
707  lmins[i] = this->maxScalar;
708  lmaxs[i] = -this->maxScalar;
709  }
710 
711 
712  for (int j = 0; j < this->numCorners; ++j){
713  for (int i = 0; i < dim; ++i){
714  std::cout << "j:" << j << " i:" << i << " 2^i:" << pow(2,i) << " and:" << int(j & int(pow(2,i))) << std::endl;
715  if(int(j & int(pow(2,i))) == 0){
716  corners[i][j] = gmins[i];
717  }
718  else {
719  corners[i][j] = gmaxs[i];
720  }
721 
722  }
723  }
724  }
725 
726 };
727 
728 template <typename Adapter, typename partId_t>
729 class CoordinateCommGraph{
730 private:
731 
732  typedef typename Adapter::lno_t lno_t;
733  typedef typename Adapter::gno_t gno_t;
734  typedef typename Adapter::scalar_t scalar_t;
735 
736  const Environment *env;
737  const Teuchos::Comm<int> *comm;
738  const Zoltan2::CoordinateModel<typename Adapter::base_adapter_t> *coords;
739  const Zoltan2::PartitioningSolution<Adapter> *soln;
740  std::vector<coordinatePartBox, partId_t> cpb;
741  int coordDim;
742  partId_t numParts;
743 
744 
745 public:
746 
747  CoordinateCommGraph(
748  const Environment *env_,
749  const Teuchos::Comm<int> *comm_,
750  const Zoltan2::CoordinateModel<typename Adapter::base_adapter_t> *coords_,
751  const Zoltan2::PartitioningSolution<Adapter> *soln_
752  ):
753  env(env_),
754  comm(comm_),
755  coords(coords_),
756  soln(soln_),
757  coordDim (coords_->getCoordinateDim()),
758  numParts (this->soln->getActualGlobalNumberOfParts())
759  {
760  this->create_part_boxes();
761  this->hash_part_boxes();
762  this->find_neighbors();
763  }
764 
765  void create_part_boxes(){
766 
767 
768  size_t allocSize = numParts * coordDim;
769  scalar_t *lmins = new scalar_t [allocSize];
770  scalar_t *gmins = new scalar_t [allocSize];
771  scalar_t *lmaxs = new scalar_t [allocSize];
772  scalar_t *gmaxs = new scalar_t [allocSize];
773 
774  for(partId_t i = 0; i < numParts; ++i){
775  coordinatePartBox tmp(
776  i,
777  this->coordDim,
778  lmins + i * coordDim,
779  gmins + i * coordDim,
780  lmaxs + i * coordDim,
781  gmaxs + i * coordDim
782  );
783  cpb.push_back(tmp);
784  }
785 
786  typedef StridedData<lno_t, scalar_t> input_t;
787  Teuchos::ArrayView<const gno_t> gnos;
788  Teuchos::ArrayView<input_t> xyz;
789  Teuchos::ArrayView<input_t> wgts;
790  coords->getCoordinates(gnos, xyz, wgts);
791 
792  //local and global num coordinates.
793  lno_t numLocalCoords = coords->getLocalNumCoordinates();
794 
795  scalar_t **pqJagged_coordinates = new scalar_t *[coordDim];
796 
797  for (int dim=0; dim < coordDim; dim++){
798  Teuchos::ArrayRCP<const scalar_t> ar;
799  xyz[dim].getInputArray(ar);
800  //pqJagged coordinate values assignment
801  pqJagged_coordinates[dim] = (scalar_t *)ar.getRawPtr();
802  }
803 
804  partId_t *sol_part = soln->getPartList();
805  for(lno_t i = 0; i < numLocalCoords; ++i){
806  partId_t p = sol_part[i];
807  cpb[p].updateMinMax(pqJagged_coordinates, i);
808  }
809  delete []pqJagged_coordinates;
810 
811 
812  reduceAll<int, gno_t>(*comm, Teuchos::REDUCE_MIN,
813  dim * numParts, lmins, gmins
814  );
815  reduceAll<int, gno_t>(*comm, Teuchos::REDUCE_MAX,
816  dim * numParts, lmaxs, gmaxs
817  );
818  }
819 
820  void hash_part_boxes (){
821  partId_t pSingleDim = pow(double(numParts), double(1.0 / coordDim));
822  if (pSingleDim == 0) pSingleDim = 1;
823  std::vector < std::vector <partId_t> > hash
824  (
825  partId_t ( pow ( partId_t (pSingleDim),
826  partId_t(coordDim)
827  )
828  )
829  );
830 
831  //calculate the corners of the dataset.
832  scalar_t *allMins = new scalar_t [coordDim];
833  scalar_t *allMaxs = new scalar_t [coordDim];
834  partId_t *hash_scales= new scalar_t [coordDim];
835 
836  for (int j = 0; j < coordDim; ++j){
837  allMins[j] = cpb[0].gmins[j];
838  allMaxs[j] = cpb[0].gmaxs[j];
839  hash_scales[j] = partId_t ( pow ( partId_t (pSingleDim), partId_t(coordDim - j - 1)));
840  }
841 
842  for (partId_t i = 1; i < numParts; ++i){
843  for (int j = 0; j < coordDim; ++j){
844  scalar_t minC = cpb[i].gmins[i];
845  scalar_t maxC = cpb[i].gmaxs[i];
846  if (minC < allMins[j]) allMins[j] = minC;
847  if (maxC > allMaxs[j]) allMaxs[j] = maxC;
848  }
849  }
850 
851  //get size of each hash for each dimension
852  scalar_t *hash_slices_size = new scalar_t [coordDim];
853  for (int j = 0; j < coordDim; ++j){
854  hash_slices_size[j] = (allMaxs[j] - allMins[j]) / pSingleDim;
855 
856  }
857 
858  delete []allMaxs;
859  delete []allMins;
860 
861 
862 
863  std::vector <partId_t> *hashIndices = new std::vector <partId_t>();
864  std::vector <partId_t> *resultHashIndices = new std::vector <partId_t>();
865  std::vector <partId_t> *tmp_swap;
866  for (partId_t i = 0; i < numParts; ++i){
867  hashIndices->clear();
868  resultHashIndices->clear();
869 
870  hashIndices->push_back(0);
871 
872  for (int j = 0; j < coordDim; ++j){
873 
874  scalar_t minC = cpb[i].gmins[i];
875  scalar_t maxC = cpb[i].gmaxs[i];
876  partId_t minHashIndex = partId_t ((minC - allMins[j]) / hash_slices_size[j]);
877  partId_t maxHashIndex = partId_t ((maxC - allMins[j]) / hash_slices_size[j]);
878 
879  partId_t hashIndexSize = hashIndices->size();
880 
881  for (partId_t k = minHashIndex; k <= maxHashIndex; ++k ){
882 
883  for (partId_t i = 0; i < hashIndexSize; ++i){
884  resultHashIndices->push_back(hashIndices[i] + k * hash_scales[j]);
885  }
886  }
887  tmp_swap = hashIndices;
888  hashIndices = resultHashIndices;
889  resultHashIndices = tmp_swap;
890  }
891 
892  partId_t hashIndexSize = hashIndices->size();
893  for (partId_t j = 0; j < hashIndexSize; ++j){
894  hash[(*hashIndices)[j]].push_back(i);
895  }
896  cpb[i].hash_indices = (*hashIndices);
897  }
898  delete hashIndices;
899  delete resultHashIndices;
900  }
901 
902  void find_neighbors(){
903 
904  }
905 
906 
907 };
908 
909 */
910 } // namespace Zoltan2
911 
912 #endif
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.
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
double scalar_t
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...
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.