NESSi  v1.0.2
The NonEquilibrium Systems Simulation Library

◆ distributed_array() [3/3]

template<typename T >
cntr::distributed_array< T >::distributed_array ( int  n,
int  maxlen,
bool  mpi 
)

Initializes the distributed_array class.

Purpose

Initializes the distributed_array class, where template T is a type of the basic unit of data, with or without the MPI support.

Parameters
n

Number of blocks

maxlen

Maximum size of block

mpi

If 'true' use MPI, otherwise one task with tid_=0

Definition at line 88 of file cntr_distributed_array_impl.hpp.

88  {
89  assert(0<=maxlen && 0<=n);
90  size_t len;
91  maxlen_=maxlen;
92  blocksize_ = maxlen_;
93  n_=n;
94  tid_map_.resize(n_);
95  len=maxlen_*n_;
96  if(len==0){
97  data_=0;
98  }else{
99  data_ = new T [len];
100  memset(data_, 0, sizeof(T)*len);
101  }
102 
103  if(mpi){
104  #if CNTR_USE_MPI==1
105  MPI_Comm_size(MPI_COMM_WORLD, &ntasks_);
106  MPI_Comm_rank(MPI_COMM_WORLD, &tid_);
107  #else
108  tid_=0;
109  ntasks_=1;
110  #endif
111  }else{
112  tid_=0;
113  ntasks_=1;
114  }
115  {
116  // This distribution tries to spread evenly number of tasks
117  int nr_alloced = 0;
118  int remainer,buckets;
119  for(int i=0;i<ntasks_;i++){
120  remainer = n - nr_alloced;
121  buckets = (ntasks_ - i);
122  int size=remainer/ buckets;
123  for(int k=0;k<size;k++){
124  tid_map_[nr_alloced+k]=i;
125  }
126  nr_alloced +=size; //Ceiling division
127  }
128 
129 
130  // int rank_totblock=0,rank_firstblock=0;
131  // // Set position of first block on a given rank
132  // for(int i=0;i<n_;i++){
133  // if(tid_map_[i]==tid_){
134  // rank_firstblock=i;
135  // break;
136  // }
137  // }
138  // // Set number of all blocks on a given rank
139  // for(int i=0;i<n_;i++){
140  // if(tid_map_[i]==tid_){
141  // rank_totblock+=1;
142  // }
143  // }
144 
145  // for(int i=0;i<n_;i++){
146  // std::cout << "Tid map " << tid_ << " " << i << " " << tid_map_[i] << " " << rank_totblock << " " << rank_firstblock << std::endl;
147  // }
148 
149  // std::cout << " -------------------------------------- " << std::endl;
150 
151  }
152 }