OpendTect-6_4  6.4
odmemory.h
Go to the documentation of this file.
1 #ifndef odmemory_h
2 #define odmemory_h
3 /*
4 ________________________________________________________________________
5 
6  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
7  Author: K. Tingdahl
8  Date: Jan 2009
9  RCS: $Id$
10 ________________________________________________________________________
11 
12 */
13 
14 #include "commondefs.h"
15 #include "paralleltask.h"
16 
17 template <class T> class ValueSeries;
18 
19 
20 namespace OD
21 {
23 }
24 
25 
26 
27 // 1M operations min per thread
28 #define mODMemMinThreadSize 131072
29 
30 namespace OD
31 {
32  template <class T> mGlobal(Basic) void memValueSet(T*,T,od_int64,
33  TaskRunner* taskrun=0);
34 }
35 
41 template <class T>
42 mClass(Basic) MemSetter : public ParallelTask
44 public:
45 
46  MemSetter();
47  MemSetter(T*,T val,od_int64 sz);
49 
50  typedef T (*ValueFunc)();
51  void setValueFunc(ValueFunc valfunc) { valfunc_ = valfunc; }
52  void setValue( const T& val ) { val_ = val; }
53  void setTarget( T* ptr ) { ptr_ = ptr; vs_ = 0; }
54  void setTarget( ValueSeries<T>& vs ) { ptr_ = vs.arr(); vs_ = &vs; }
55  void setSize( od_int64 sz ) { sz_ = sz; }
56 
57  uiString uiMessage() const { return tr("Value setter"); }
58  uiString uiNrDoneText() const { return sPosFinished(); }
59 
60 
61 protected:
62 
63  od_int64 nrIterations() const { return sz_; }
64  int minThreadSize() const { return mODMemMinThreadSize; }
65  bool setPtr(od_int64 start,od_int64 size);
66 
68  T* ptr_;
70  T val_;
71  ValueFunc valfunc_;
72 
73 private:
74 
75  bool doPrepare(int);
76  bool doWork(od_int64,od_int64,int);
77 
78 };
79 
80 
88 template <class T>
89 mClass(Basic) MemCopier : public ParallelTask
91 public:
92 
93  MemCopier();
94  MemCopier(T*,const T*,od_int64 sz);
95  MemCopier(T*,const ValueSeries<T>&,od_int64 sz);
96  MemCopier(ValueSeries<T>&,const T*,od_int64 sz);
98 
99  void setInput( const T* ptr ) { inptr_ = ptr; invs_ = 0; }
100  void setInput( const ValueSeries<T>& vs ) { inptr_ = vs.arr();
101  invs_ = &vs; }
102  void setOutput( T* ptr ) { outptr_ = ptr; outvs_ = 0; }
103  void setOutput( ValueSeries<T>& vs ) { outptr_ = vs.arr();
104  outvs_ = &vs; }
105  void setSize(od_int64 sz) { sz_ = sz; }
106 
107  uiString uiMessage() const { return tr("Value copier"); }
108  uiString uiNrDoneText() const { return sPosFinished(); }
109 
110 
111 protected:
112 
113  od_int64 nrIterations() const { return sz_; }
114  int minThreadSize() const { return mODMemMinThreadSize; }
115  inline bool setPtr(od_int64 start,od_int64 size);
116 
117  const T* inptr_;
122 
123 private:
124 
125  bool doPrepare(int);
126  bool doWork(od_int64,od_int64,int);
127 
128 };
129 
130 
135 template <class T>
138 public:
139  MemValReplacer();
140  MemValReplacer(T*,const T& fromval,const T& toval,od_int64 sz);
141  MemValReplacer(ValueSeries<T>&,const T& from,const T& to,
142  od_int64 sz);
143 
144  void setFromValue(const T& val) { fromval_ = val; }
145  void setToValue(const T& val) { toval_ = val; }
146  void setPtr(T* ptr) { ptr_ = ptr; vs_ = 0; }
147  void setPtr(ValueSeries<T>& vs) { ptr_ = vs.arr(); vs_ = &vs; }
148  void setSize(od_int64 sz) { sz_ = sz; }
149 
150  uiString uiMessage() const { return tr("Value replacer"); }
151  uiString uiNrDoneText() const { return sPosFinished(); }
152 
153 
154 protected:
155 
156  od_int64 nrIterations() const { return sz_; }
157  int minThreadSize() const { return mODMemMinThreadSize; }
158  bool setPtr(od_int64 start,od_int64 size);
159 
161  T* ptr_;
165 
166 private:
167 
168  bool doPrepare(int);
169  bool doWork(od_int64,od_int64,int);
170 
171 };
172 
173 
174 namespace OD { mGlobal(Basic) void sysMemCopy(void*,const void*,od_int64); }
175 
176 #include "valseries.h"
177 
178 
179 template <class T> inline
181  : ptr_( 0 )
182  , vs_( 0 )
183  , sz_( -1 )
184  , valfunc_( 0 )
185 {}
186 
187 
188 template <class T> inline
189 MemSetter<T>::MemSetter( T* ptr, T val, od_int64 sz )
190  : ptr_( ptr )
191  , vs_( 0 )
192  , val_( val )
193  , sz_( sz )
194  , valfunc_( 0 )
195 {}
196 
197 
198 template <class T> inline
199 MemSetter<T>::MemSetter( ValueSeries<T>& vs, T val, od_int64 sz )
200  : ptr_( vs.arr() )
201  , vs_( &vs )
202  , val_( val )
203  , sz_( sz )
204  , valfunc_( 0 )
205 {}
206 
207 
208 template <class T> inline
210 { return ptr_ || vs_; }
211 
212 
213 template <class T> inline
214 bool MemSetter<T>::doWork( od_int64 start, od_int64 stop, int )
215 {
216  if ( ptr_ )
217  {
218  if ( valfunc_ )
219  {
220  T* ptr = ptr_+start;
221  T* stopptr = ptr_+stop;
222  while ( ptr<=stopptr )
223  {
224  *ptr = valfunc_();
225  ptr++;
226  }
227 
228  return true;
229  }
230  else
231  {
232  return setPtr( start, stop-start+1 );
233  }
234  }
235 
236  if ( valfunc_ )
237  {
238  for ( od_int64 idx=start; idx<=stop; idx++ )
239  { vs_->setValue( idx, valfunc_() ); }
240  }
241  else
242  {
243  for ( od_int64 idx=start; idx<=stop; idx++ )
244  { vs_->setValue( idx, val_ ); }
245  }
246 
247  return true;
248 }
249 
250 
251 namespace OD
252 {
253  mGlobal(Basic) void sysMemSet(void*,int,size_t);
254 
255  template <class T> mGlobal(Basic) T* sysMemValueSet(T*,T,od_int64 nrsamp);
256 }
257 
258 template <> inline
259 bool MemSetter<char>::setPtr( od_int64 start, od_int64 size )
260 {
261  OD::sysMemSet( ptr_+start, (int)val_, (size_t) size );
262  addToNrDone( size );
263  return true;
264 }
265 
266 
267 template <> inline
268 bool MemSetter<unsigned char>::setPtr( od_int64 start, od_int64 size )
269 {
270  OD::sysMemSet( ptr_+start, (int)val_, (size_t) size );
271  addToNrDone( size );
272  return true;
273 }
274 
275 
276 template <> inline
277 bool MemSetter<bool>::setPtr( od_int64 start, od_int64 size )
278 {
279  OD::sysMemSet( ptr_+start, (int)val_, (size_t) size );
280  addToNrDone( size );
281  return true;
282 }
283 
284 
285 #define mODMemSetterFullImpl(Type) \
286  Type* ptr = ptr_ + start; \
287  const Type* stopptr = ptr + size; \
288  while ( ptr != stopptr ) \
289  { \
290  *ptr = val_; \
291  ptr++; \
292  } \
293  \
294  return true;
295 
296 
297 namespace OD { mGlobal(Basic) void sysMemZero(void*,size_t); }
298 
299 #define mODMemSpecialImpl( Type ) \
300 template <> inline \
301 bool MemSetter<Type>::setPtr( od_int64 start, od_int64 size ) \
302 { \
303  if ( val_==0 ) \
304  OD::sysMemZero( ptr_+start, size*sizeof(Type) ); \
305  else \
306  OD::sysMemValueSet( ptr_+start, val_, size ); \
307  \
308  addToNrDone( size ); \
309  return true; \
310 }
311 
312 
316 mODMemSpecialImpl( unsigned int );
318 mODMemSpecialImpl( unsigned short );
319 mODMemSpecialImpl( od_int64 );
321 
322 
323 
324 template <class T> inline
325 bool MemSetter<T>::setPtr( od_int64 start, od_int64 size )
326 {
327  OD::sysMemValueSet( ptr_+start, val_, size );
328  addToNrDone( size );
329  return true;
330 }
331 
332 #undef mODMemSpecialImpl
333 #undef mODMemSetterFullImpl
334 
335 
336 template <class T> inline
338  : sz_(0), inptr_(0), invs_(0), outptr_(0), outvs_(0) {}
339 
340 template <class T> inline
341 MemCopier<T>::MemCopier( T* o, const T* i, od_int64 sz )
342  : sz_(sz), inptr_(i), invs_(0), outptr_(o), outvs_(0) {}
343 
344 template <class T> inline
345 MemCopier<T>::MemCopier( ValueSeries<T>& o, const T* i, od_int64 sz )
346  : sz_(sz), inptr_(i), invs_(0), outptr_(o.arr()), outvs_(&o) {}
347 
348 template <class T> inline
349 MemCopier<T>::MemCopier( T* o, const ValueSeries<T>& i, od_int64 sz )
350  : sz_(sz), inptr_(i.arr()), invs_(&i), outptr_(o), outvs_(0) {}
351 
352 template <class T> inline
354  od_int64 sz)
355  : sz_(sz), inptr_(i.arr), invs_(&i), outptr_(o.arr()), outvs_(&o) {}
356 
357 
358 template <class T> inline
360 { return (inptr_ || invs_) && (outptr_ || outvs_); }
361 
362 
363 template <class T> inline
364 bool MemCopier<T>::doWork( od_int64 start, od_int64 stop, int )
365 {
366  if ( inptr_ && outptr_ )
367  return setPtr( start, stop-start+1 );
368 
369  if ( outptr_ )
370  {
371  for ( od_int64 idx=start; idx<=stop; idx++ )
372  { outptr_[idx] = invs_->value( idx ); }
373  }
374  else if ( inptr_ )
375  {
376  for ( od_int64 idx=start; idx<=stop; idx++ )
377  { outvs_->setValue( idx, inptr_[idx] ); }
378  }
379  else
380  {
381  for ( od_int64 idx=start; idx<=stop; idx++ )
382  { outvs_->setValue( idx, invs_->value(idx)); }
383  }
384  return true;
385 }
386 
387 
388 
389 template <class T> inline
390 bool MemCopier<T>::setPtr( od_int64 start, od_int64 size )
391 {
392  OD::sysMemCopy( outptr_+start, inptr_+start, size * sizeof(T) );
393  addToNrDone( size );
394  return true;
395 }
396 
397 
398 template <class T> inline
399 MemValReplacer<T>::MemValReplacer( T* ptr, const T& fromval, const T& toval,
400  od_int64 sz )
401  : ptr_( ptr )
402  , vs_( 0 )
403  , fromval_( fromval )
404  , toval_( toval )
405  , sz_( sz )
406 {}
407 
408 
409 template <class T> inline
411  const T& toval,
412  od_int64 sz)
413  : ptr_( vs.arr() )
414  , vs_( &vs )
415  , toval_( toval )
416  , fromval_( fromval )
417  , sz_( sz )
418 {}
419 
420 
421 template <class T> inline
423 { return ptr_ || vs_; }
424 
425 
426 template <class T> inline
427 bool MemValReplacer<T>::doWork( od_int64 start, od_int64 stop, int )
428 {
429  if ( ptr_ )
430  return setPtr( start, stop-start+1 );
431 
432  for ( od_int64 idx=start; idx<=stop; idx++ )
433  {
434  if ( vs_->value(idx)==fromval_ )
435  vs_->setValue( idx, toval_ );
436  }
437 
438  return true;
439 }
440 
441 template <class T> inline
442 bool MemValReplacer<T>::setPtr( od_int64 start, od_int64 size )
443 {
444  T* ptr = ptr_ + start;
445  const T* stopptr = ptr + size;
446  while ( ptr != stopptr )
447  {
448  if ( *ptr==fromval_ )
449  *ptr = toval_;
450 
451  ptr++;
452  }
453 
454  return true;
455 }
456 
457 
458 
460 #define cMinMemValSetParallelSize 400
461 
462 namespace OD
463 {
464 
465 template <class T>
466 inline T* sysMemValueSet( T* ptr, T val , od_int64 size )
467 {
468  if ( !ptr || size<=0 )
469  return ptr ? ptr : 0;
470 
471  const T* stopptr = ptr + size;
472  while ( ptr != stopptr )
473  *ptr++ = val;
474 
475  return ptr;
476 }
477 
478 
479 template <class T>
480 inline void memValueSet( T* arr, T val , od_int64 sz, TaskRunner* taskrun )
481 {
482  if ( sz < cMinMemValSetParallelSize )
483  {
484  sysMemValueSet( arr, val, sz );
485  return;
486  }
487 
488  MemSetter<T> msetter( arr, val, sz );
489  taskrun ? taskrun->execute( msetter )
490  : msetter.execute();
491 }
492 
493 } // namespace OD
494 
495 
496 #endif
uiString uiMessage() const
will be message() again in 7.x
Definition: odmemory.h:107
bool doWork(int64_t, int64_t, int)
Definition: odmemory.h:364
T val_
Definition: odmemory.h:70
int64_t sz_
Definition: odmemory.h:69
T toval_
Definition: odmemory.h:163
bool doPrepare(int)
Definition: odmemory.h:209
T to(const F &fr)
Definition: convert.h:33
ValueSeries< T > * vs_
Definition: odmemory.h:67
Sets large amounts of values to a constant using multiple threads.
Definition: odmemory.h:42
#define mODTextTranslationClass(clss)
Definition: uistring.h:38
#define mGlobal(module)
Definition: commondefs.h:163
#define od_int64
Definition: plftypes.h:36
int minThreadSize() const
Definition: odmemory.h:64
void setToValue(const T &val)
Definition: odmemory.h:145
int64_t nrIterations() const
Definition: odmemory.h:63
ValueFunc valfunc_
Definition: odmemory.h:71
Definition: uistring.h:89
Goes through some mem or a ValSeries and replaces one value with another.
Definition: odmemory.h:136
void setTarget(ValueSeries< T > &vs)
Definition: odmemory.h:54
uiString uiMessage() const
will be message() again in 7.x
Definition: odmemory.h:150
void setOutput(ValueSeries< T > &vs)
Definition: odmemory.h:103
void setSize(int64_t sz)
Definition: odmemory.h:105
Generalization of a task that can be run in parallel.
Definition: paralleltask.h:66
int64_t sz_
Definition: odmemory.h:162
static bool execute(TaskRunner *tr, Task &)
Taskrunner may be zero.
MemSetter()
Definition: odmemory.h:180
int minThreadSize() const
Definition: odmemory.h:114
void setSize(int64_t sz)
Definition: odmemory.h:148
bool doPrepare(int)
Definition: odmemory.h:359
void setInput(const ValueSeries< T > &vs)
Definition: odmemory.h:100
Class that can execute a task.
Definition: task.h:169
void sysMemSet(void *, int, size_t)
bool setPtr(int64_t start, int64_t size)
Definition: odmemory.h:325
#define mODMemMinThreadSize
Definition: odmemory.h:28
const T * inptr_
Definition: odmemory.h:117
bool execute()
Definition: paralleltask.h:71
Interface to a series of values.
Definition: odmemory.h:17
Definition: odmemory.h:22
void setPtr(T *ptr)
Definition: odmemory.h:146
void sysMemZero(void *, size_t)
T * ptr_
Definition: odmemory.h:68
bool setPtr(int64_t start, int64_t size)
Definition: odmemory.h:390
int64_t nrIterations() const
Definition: odmemory.h:156
void setValueFunc(ValueFunc valfunc)
Definition: odmemory.h:51
void setOutput(T *ptr)
Definition: odmemory.h:102
void memValueSet(T *, T, int64_t, TaskRunner *taskrun=0)
Definition: odmemory.h:480
T * outptr_
Definition: odmemory.h:119
ValueSeries< T > * outvs_
Definition: odmemory.h:120
void setInput(const T *ptr)
Definition: odmemory.h:99
int64_t nrIterations() const
Definition: odmemory.h:113
int minThreadSize() const
Definition: odmemory.h:157
#define cMinMemValSetParallelSize
size determined experimentally on different Linux and Windows systems
Definition: odmemory.h:460
void setPtr(ValueSeries< T > &vs)
Definition: odmemory.h:147
#define od_uint64
Definition: plftypes.h:37
ValueSeries< T > * vs_
Definition: odmemory.h:160
void sysMemCopy(void *, const void *, int64_t)
int64_t sz_
Definition: odmemory.h:121
uiString uiNrDoneText() const
will be nrDoneText() in 7.x
Definition: odmemory.h:108
uiString uiNrDoneText() const
will be nrDoneText() in 7.x
Definition: odmemory.h:58
MemCopier()
Definition: odmemory.h:337
T * sysMemValueSet(T *, T, int64_t nrsamp)
Definition: odmemory.h:466
uiString uiNrDoneText() const
will be nrDoneText() in 7.x
Definition: odmemory.h:151
T * ptr_
Definition: odmemory.h:161
OpendTect.
Definition: commontypes.h:29
PtrPolicy
Definition: odmemory.h:22
T fromval_
Definition: odmemory.h:164
bool doWork(int64_t, int64_t, int)
Definition: odmemory.h:427
ValueSeries Copier.
Definition: odmemory.h:89
const ValueSeries< T > * invs_
Definition: odmemory.h:118
void setSize(int64_t sz)
Definition: odmemory.h:55
#define mClass(module)
Definition: commondefs.h:164
bool doWork(int64_t, int64_t, int)
Definition: odmemory.h:214
Definition: odmemory.h:22
uiString uiMessage() const
will be message() again in 7.x
Definition: odmemory.h:57
void addToNrDone(int64_t increment)
virtual T * arr()
Definition: valseries.h:55
void setTarget(T *ptr)
Definition: odmemory.h:53
#define mODMemSpecialImpl(Type)
Definition: odmemory.h:299
void setValue(const T &val)
Definition: odmemory.h:52
Definition: odmemory.h:22
bool doPrepare(int)
Definition: odmemory.h:422
void setFromValue(const T &val)
Definition: odmemory.h:144

Generated at for the OpendTect seismic interpretation project. Copyright (C): dGB Beheer B. V. 2019