openModeller  Version 1.5.0
Projector.cpp
Go to the documentation of this file.
1 
30 #include <openmodeller/Log.hh>
32 #include <openmodeller/Model.hh>
33 
38 
40 
42 
43 #ifdef MPI_FOUND
44 #include "mpi.h"
45 #endif
46 
47 #include <utility>
48 using std::pair;
49 
50 #ifdef MPI_FOUND
51 #define N_X 10
52 #define size_block 30000
53 #endif
54 
55 #ifndef MPI_FOUND
56 /******************/
57 /*** create Map ***/
58 bool
60  const EnvironmentPtr& env,
61  Map *map,
62  AreaStats *areaStats,
63  CallbackWrapper *callbackWrapper )
64 {
65  // Retrieve possible adjustments and/or additions made
66  // on the effective header.
67  Header hdr = map->getHeader();
68 
69 #ifndef GEO_TRANSFORMATIONS_OFF
70  if ( ! hdr.hasProj() ) {
71 
72  throw;
73  }
74 #endif
75 
76  // Normalize the environment.
77  model->setNormalization( env );
78 
79  if ( areaStats ) {
80 
81  areaStats->reset( areaStats->getPredictionThreshold() );
82  }
83 
84  MapIterator fin;
85  MapIterator it = map->begin();
86 
87  int pixels = 0;
88  int pixelcount = hdr.ydim * hdr.xdim;
89  int pixelstep = pixelcount/20;
90  bool abort = false;
91 
92  Coord lg;
93  Coord lt;
94  Scalar val;
95 
96  while ( it != fin ) {
97 
98  // Call the abort callback function if it is set.
99  if ( callbackWrapper && pixels%pixelstep == 0 ) {
100 
101  try {
102 
103  abort = callbackWrapper->abortionRequested();
104 
105  if ( abort ) {
106 
107  Log::instance()->info( "Projection aborted." );
108 
109  if ( ! map->deleteRaster() ) {
110 
111  Log::instance()->warn( "Could not delete map file." );
112  }
113 
114  return false;
115  }
116  }
117  catch( ... ) {}
118  }
119 
120  pair<Coord,Coord> lonlat = *it;
121 
122  pixels++;
123  ++it;
124 
125  lg = lonlat.first;
126  lt = lonlat.second;
127 
128  Sample const &amb = env->get( lg, lt );
129 
130  // Read environmental values and find the output value.
131  if ( amb.size() == 0 ) {
132 
133  // Write noval on the map.
134  map->put( lg, lt );
135 
136  val = -1; // could be used in a log
137  }
138  else {
139 
140  val = model->getValue( amb );
141 
142  if ( val < 0.0 || val > 1.0 ) {
143 
144  std::string msg = Log::format( "Suitability for point (%f, %f) is outside the range: %f", lg, lt, val );
145  throw AlgorithmException( msg.c_str() );
146  }
147 
148  if ( areaStats ) {
149 
150  areaStats->addPrediction( val );
151  }
152 
153  // Write value on map.
154  map->put( lg, lt, val );
155  }
156 
157  // Call the callback function if it is set.
158  if ( callbackWrapper && pixels%pixelstep == 0 ) {
159 
160  float progress = pixels/(float)pixelcount;
161 
162  if ( progress > 1.0 ) {
163 
164  progress = 1.0;
165  }
166 
167  try {
168 
169  callbackWrapper->notifyModelProjectionProgress( progress );
170  }
171  catch( ... ) {}
172  }
173  }
174 
175  // Call the callback function if it is set.
176  if ( callbackWrapper ) {
177 
178  try {
179 
180  callbackWrapper->notifyModelProjectionProgress( 1.0 );
181  }
182  catch ( ... ) {}
183  }
184 
185  map->finish();
186 
187  return true;
188 }
189 
190 #else
191 /***************************/
192 /*** create Map Parallel ***/
193 bool
194 Projector::createMap( const Model& model,
195  const EnvironmentPtr& env,
196  Map *map,
197  AreaStats *areaStats,
198  CallbackWrapper *callbackWrapper )
199 {
200 
201  /*********************struct buff *************************************/
202  typedef struct
203  {
204  double lt;
205  double lg;
206  Scalar val;
207  } buff;
208  /**********************************************************************/
209 
210  buff X[N_X][size_block];
211  MPI_Status status;
212  MPI_Request req_dado[N_X];
213  MPI_Request inf_N[N_X];
214  int ix;
215  int n_elem[N_X];
216  int env_dado[N_X],total_pixels;
217  int pixel_next,interval_pixels[3];
218  int count_tag,max_count_tag;
219  int myrank,mysize;
220  int j, N;
221 
222  // Retrieve possible adjustments and/or additions made
223  // on the effective header.
224  MPI_Comm_rank(MPI_COMM_WORLD,&myrank);
225 
226  Header hdr = map->getHeader();
227 
228 #ifndef GEO_TRANSFORMATIONS_OFF
229  if ( ! hdr.hasProj() )
230  {
231  throw;
232  }
233 #endif
234  Log::instance()->debug( "Parallel version of createMap\n" );
235  // Normalize the environment
236  model->setNormalization( env );
237 
238  if ( areaStats ) {
239  areaStats->reset();
240  }
241 
242  MapIterator fin;
243  MapIterator my_it = map->begin();
244 
245  j = 0; //count the lines on struct array
246 
247  MPI_Comm_rank(MPI_COMM_WORLD,&myrank);
248  MPI_Comm_size(MPI_COMM_WORLD,&mysize);
249 
250  int pixels, init_pixel, lim_pixel;
251  count_tag=10;
252  total_pixels=hdr.ydim*hdr.xdim;
253 
254  MPI_Barrier(MPI_COMM_WORLD);
255 
256  if (myrank==0) { //sif rank 0
257 
258  max_count_tag=10+total_pixels/size_block;
259  if ((total_pixels%size_block)!=0)
260  max_count_tag++;
261 
262  while (count_tag<max_count_tag) {
263  MPI_Recv(&N,1,MPI_INT,MPI_ANY_SOURCE,count_tag,MPI_COMM_WORLD,&status);
264  MPI_Recv(X[0],N*sizeof(buff),MPI_UNSIGNED_CHAR,status.MPI_SOURCE,count_tag,MPI_COMM_WORLD,&status);
265 
266  for (j=0; j< N; j++) {
267  if (X[0][j].val==100000)
268  map->put( X[0][j].lg, X[0][j].lt);
269  else {
270  map->put(X[0][j].lg,X[0][j].lt,X[0][j].val );
271  }
272  }
273 count_tag++;
274  }
275  } //end if rank 0
276  else
277  if (myrank==mysize-1) {
278  int k,req_pixels;
279  pixel_next=0;
280  interval_pixels[2]=10; //count_tag
281 
282  while (pixel_next<(total_pixels)) {
283  lim_pixel=pixel_next+size_block;
284 
285  if (lim_pixel>total_pixels)
286  lim_pixel=total_pixels;
287 
288  interval_pixels[0]=pixel_next;
289  interval_pixels[1]=lim_pixel;
290  pixel_next=lim_pixel;
291  MPI_Recv(&req_pixels,1,MPI_INT,MPI_ANY_SOURCE,1,MPI_COMM_WORLD,&status);
292  MPI_Send(interval_pixels,3,MPI_INT,status.MPI_SOURCE,2,MPI_COMM_WORLD);
293  interval_pixels[2]++;
294  }
295 
296  for (k=1;k<(mysize-1);k++) {
297  interval_pixels[0]=total_pixels;
298  MPI_Recv(&req_pixels,1,MPI_INT,MPI_ANY_SOURCE,1,MPI_COMM_WORLD,&status);
299  MPI_Send(interval_pixels,2,MPI_INT,status.MPI_SOURCE,2,MPI_COMM_WORLD);
300  }
301  }
302  else {
303  ix=0;
304  for (int k=0;k<N_X;k++)
305  env_dado[k]=0;
306  MPI_Send(&ix,1,MPI_INT,(mysize-1),1,MPI_COMM_WORLD);
307  MPI_Recv(interval_pixels,3,MPI_INT,(mysize-1),2,MPI_COMM_WORLD,&status);;
308  init_pixel=interval_pixels[0];
309  lim_pixel=interval_pixels[1];
310 
311 while (init_pixel<(total_pixels)) {
312  my_it.nextblock(init_pixel);
313 
314  j=0;
315 
316  for (pixels=init_pixel; pixels<lim_pixel; pixels++) {
317 
318  pair<Coord,Coord> lonlat = *my_it;
319  ++my_it;
320  Coord lg = lonlat.first;
321  Coord lt = lonlat.second;
322 
323  { //begin block
324  Sample const &amb = env->get( lg, lt );
325  // TODO: use mask to check if pixel should contain prediction
326  // Read environmental values and find the output value.
327  if ( amb.size() == 0 ) {
328  // Write noval on the map.
329 
330  /* map->put( lg, lt );********************************************************/
331  /*substituir pela função, coloco lat, long, 255*******************************/
332  /*****escrevo nada na struct, 255*********************************************/
333 
334  X[ix][j].lt=lt;
335  X[ix][j].lg=lg;
336  X[ix][j].val=100000;
337 
338  }
339  else {
340  Scalar val = model->getValue( amb );
341 
342  if ( val < 0.0 )
343  val = 0.0;
344  else
345  if ( val > 1.0 )
346  val = 1.0;
347 
348  if ( areaStats ) {
349  areaStats->addPrediction( val );
350  }
351  // Write value on map.
352  /********** map->put( lg, lt, val );substituir pela função***************/
353  X[ix][j].lt=lt;
354  X[ix][j].lg=lg;
355  X[ix][j].val=val;
356  }
357  } //end block
358  j++;
359  } //end do for
360 
361  n_elem[ix]=j;
362 
363  MPI_Isend(&n_elem[ix],1,MPI_INT,0,interval_pixels[2],MPI_COMM_WORLD,&inf_N[ix]);
364  //MPI_Isend(&j,1,MPI_INT,0,interval_pixels[2],MPI_COMM_WORLD,&inf_N[ix]);
365  MPI_Isend(X[ix],j*sizeof(buff),MPI_UNSIGNED_CHAR,0,interval_pixels[2],MPI_COMM_WORLD,&req_dado[ix]);
366  env_dado[ix]=1;
367  ix=(ix+1)%N_X;
368 
369  if (env_dado[ix]==1) {
370  MPI_Wait(&inf_N[ix],&status);
371  MPI_Wait(&req_dado[ix],&status);
372  env_dado[ix]=0;
373  }
374 
375  MPI_Send(&ix,1,MPI_INT,(mysize-1),1,MPI_COMM_WORLD);
376  MPI_Recv(interval_pixels,3,MPI_INT,(mysize-1),2,MPI_COMM_WORLD,&status);;
377  init_pixel=interval_pixels[0];
378  lim_pixel=interval_pixels[1];
379  } // end while
380  } // end else myrank == 0
381 
382  MPI_Barrier(MPI_COMM_WORLD);
383 
384 // Call the callback function if it is set.
385 
386  if ( callbackWrapper ) {
387 
388  try {
389 
390  callbackWrapper->notifyModelProjectionProgress( 1.0 );
391  }
392  catch ( ... ) {}
393  }
394 
395 
396  return true;
397 }
398 #endif
399 
MapIterator begin() const
Definition: Map.hh:63
void warn(const char *format,...)
'Warn' level.
Definition: Log.cpp:273
int xdim
Definition: Header.hh:72
void addPrediction(Scalar predictionValue)
Definition: AreaStats.cpp:57
double Scalar
Type of map values.
Definition: om_defs.hh:39
int ydim
Definition: Header.hh:73
static std::string format(const char *fmt,...)
Definition: Log.cpp:142
void finish()
Definition: Map.cpp:154
static Log * instance()
Returns the instance pointer, creating the object on the first call.
Definition: Log.cpp:45
int hasProj() const
Definition: Header.hh:65
Scalar getPredictionThreshold() const
Definition: AreaStats.hh:102
void reset(Scalar predictionThreshold=0.5)
Definition: AreaStats.cpp:50
Definition: Header.hh:45
int put(Coord x, Coord y, Scalar val)
Definition: Map.cpp:112
static bool createMap(const Model &model, const EnvironmentPtr &env, Map *map, AreaStats *areaStats=0, CallbackWrapper *callbackWrapper=0)
Definition: Projector.cpp:59
const Header & getHeader() const
Definition: Map.hh:68
Definition: Map.hh:49
std::size_t size() const
Definition: Sample.hh:70
int deleteRaster()
Definition: Map.cpp:162
void info(const char *format,...)
'Info' level.
Definition: Log.cpp:256
double Coord
Type of map coordinates.
Definition: om_defs.hh:38
void debug(const char *format,...)
'Debug' level.
Definition: Log.cpp:237
void notifyModelProjectionProgress(float progress)
Definition: Sample.hh:25