21 #ifndef TJUTILS_CONFIG_H
22 #define TJUTILS_CONFIG_H
23 #include <tjutils/config.h>
27 #include <tjutils/tjstd.h>
28 #include <tjutils/tjcomplex.h>
29 #include <tjutils/tjfeedback.h>
37 #ifdef BLITZ_REPLACEMENT
39 #include <../replacements/blitz_replacement.h>
45 #include <blitz/blitz.h>
48 #ifdef BZ_MUTEX_DECLARE
49 #undef BZ_MUTEX_DECLARE
57 #ifdef BZ_MUTEX_UNLOCK
58 #undef BZ_MUTEX_UNLOCK
60 #ifdef BZ_MUTEX_DESTROY
61 #undef BZ_MUTEX_DESTROY
66 #define BZ_MUTEX_DECLARE(name)
67 #define BZ_MUTEX_INIT(name)
68 #define BZ_MUTEX_LOCK(name)
69 #define BZ_MUTEX_UNLOCK(name)
70 #define BZ_MUTEX_DESTROY(name)
74 #include <blitz/array.h>
75 #ifdef HAVE_BLITZ_TINYVEC_ET
76 #include <blitz/tinyvec-et.h>
78 using namespace blitz;
84 #include <tjutils/tjtypes.h>
85 #include <tjutils/tjprofiler.h>
87 #include <odinpara/protocol.h>
89 #include <odindata/converter.h>
90 #include <odindata/fileio_opts.h>
94 #ifndef BLITZ_REPLACEMENT
97 BZ_DECLARE_FUNCTION_RET (float2real, STD_complex);
98 BZ_DECLARE_FUNCTION_RET (float2imag, STD_complex);
99 BZ_DECLARE_FUNCTION_RET (creal,
float)
100 BZ_DECLARE_FUNCTION_RET (cimag,
float)
101 BZ_DECLARE_FUNCTION_RET (cabs,
float)
102 BZ_DECLARE_FUNCTION_RET (
phase,
float)
103 BZ_DECLARE_FUNCTION (logc)
104 BZ_DECLARE_FUNCTION (expc)
105 BZ_DECLARE_FUNCTION (conjc)
112 #ifdef STL_REPLACEMENT
113 BZ_DECLARE_ARRAY_ET_SCALAR_OPS(tjstd_complex)
127 struct FileMapHandle {
128 FileMapHandle() : fd(-1), offset(0), refcount(1) {}
147 template <
typename T,
int N_rank>
148 class Data :
public Array<T,N_rank> {
156 Data(
const TinyVector<int,N_rank>& dimvec,
const T& val=0) : Array<T,N_rank>(dimvec), fmap(0) {(*this)=val;}
161 Data(
int extent1) : Array<T,N_rank>(extent1), fmap(0) {}
162 Data(
int extent1,
int extent2) : Array<T,N_rank>(extent1,extent2), fmap(0) {}
163 Data(
int extent1,
int extent2,
int extent3) : Array<T,N_rank>(extent1,extent2,extent3), fmap(0) {}
164 Data(
int extent1,
int extent2,
int extent3,
int extent4) : Array<T,N_rank>(extent1,extent2,extent3,extent4), fmap(0) {}
165 Data(
int extent1,
int extent2,
int extent3,
int extent4,
int extent5) : Array<T,N_rank>(extent1,extent2,extent3,extent4,extent5), fmap(0) {}
180 Data(
const STD_string& filename,
bool readonly,
const TinyVector<int,N_rank>& shape, LONGEST_INT offset=0 );
190 Data(
const Array<T,N_rank>& a) : Array<T,N_rank>(a), fmap(0) {}
198 #ifdef BLITZ_REPLACEMENT
203 Data(
const Array<T,M_rank>& a) : Array<T,N_rank>(a), fmap(0) {}
208 template<
class T_expr>
209 Data(BZ_ETPARM(_bz_ArrayExpr<T_expr>) expr) : Array<T,N_rank>(expr), fmap(0) {}
227 Data<T,N_rank>& operator = (
const Array<T,N_rank>& a) {Array<T,N_rank>::operator=(a);
return *
this;}
232 Data<T,N_rank>& operator = (
const T& val) {Array<T,N_rank>::operator=(val);
return *
this;}
235 #ifdef BLITZ_REPLACEMENT
240 inline Data<T,N_rank>& operator = (
const RectView<
const Array<T,M_rank>&,T,M_rank>& rhs) {
241 Array<T,N_rank>::operator = (rhs);
248 template<
class T_expr>
283 int read(
const STD_string& format,
const STD_string& filename, LONGEST_INT offset=0);
291 template<
typename T2>
292 int read(
const STD_string& filename, LONGEST_INT offset=0);
301 int write(
const STD_string& format,
const STD_string& filename,
bool autoscale=
true)
const;
309 template<
typename T2>
310 int write(
const STD_string& filename,
bool autoscale=
true)
const;
330 int write_asc_file(
const STD_string& filename,
const Array<T,N_rank>& pre=defaultArray,
const Array<T,N_rank>& post=defaultArray)
const;
381 void shift(
unsigned int shift_dim,
int shift);
392 void congrid(
const TinyVector<int,N_rank>& newshape,
const TinyVector<float,N_rank>* subpixel_shift=0,
bool left_to_right=
false);
418 static Array<T,N_rank> defaultArray;
428 void interpolate1dim(
unsigned int dim,
int newsize,
float subpixel_shift);
435 template <
typename T,
int N_rank>
444 #ifdef STREAM_REPLACEMENT
446 template<
typename T,
int N_rank>
447 inline STD_ostream& operator << (STD_ostream& s,
const Array<T,N_rank>&) {
return s;}
449 template<
typename T,
int N_rank>
450 inline STD_ostream& operator << (STD_ostream& s,
const TinyVector<T,N_rank>&) {
return s;}
452 inline STD_ostream& operator << (STD_ostream& s,
const Range&) {
return s;}
463 TinyVector<int,N_rank> index2extent(
const TinyVector<int,N_rank>& shape,
unsigned int index) {
464 TinyVector<int,N_rank> result;
465 unsigned int subindex=index;
466 for(
int i=N_rank-1;i>=0;i--){
467 result(i)=subindex%shape(i);
479 template<
typename T,
int N_rank,
typename T2>
480 void convert_from_ptr(
Data<T,N_rank>& dst,
const T2* src,
const TinyVector<int, N_rank>& shape,
bool autoscale=
true) {
483 int dstsize=product(shape);
484 int srcsize=dstsize*Converter::get_elements((T)0)/Converter::get_elements((T2)0);
485 ODINLOG(odinlog,normalDebug) <<
"dstsize/srcsize=" << dstsize <<
"/" << srcsize << STD_endl;
493 template<
typename T,
int N_rank>
494 void convert_from_ptr(
Data<T,N_rank>& dst,
const T* src,
const TinyVector<int, N_rank>& shape) {
495 dst.reference(Array<T,N_rank>((T*)src, shape, duplicateData));
501 template <
typename T,
int N_rank>
502 Data<T,N_rank>::Data(
const STD_string& filename,
bool readonly,
const TinyVector<int,N_rank>& shape, LONGEST_INT offset ) : fmap(new FileMapHandle) {
503 T* ptr=(T*)
filemap(filename, (LONGEST_INT)product(shape)*
sizeof(T), offset, readonly, fmap->fd);
504 if(ptr && (fmap->fd)>=0) {
505 Array<T,N_rank>::reference(Array<T,N_rank>(ptr, shape, neverDeleteData));
515 template <
typename T,
int N_rank>
521 ODINLOG(odinlog,normalDebug) <<
"fd/offset/refcount=" << fmap->fd <<
"/" << fmap->offset <<
"/" << fmap->refcount << STD_endl;
522 if(!(fmap->refcount)) {
523 fileunmap(fmap->fd, Array<T,N_rank>::data(), (LONGEST_INT)Array<T,N_rank>::size()*
sizeof(T), fmap->offset);
524 fmap->mutex.unlock();
528 if(fmap) fmap->mutex.unlock();
534 template <
typename T,
int N_rank>
541 template <
typename T,
int N_rank>
549 ODINLOG(odinlog,normalDebug) <<
"fmap->refcount=" << fmap->refcount << STD_endl;
551 Array<T,N_rank>::reference(d);
556 template <
typename T,
int N_rank>
559 if( a.dim() <= N_rank ) {
560 ndim nn=a.get_extent();
561 int npad=N_rank-nn.
dim();
562 ODINLOG(odinlog,normalDebug) <<
"npad=" << npad << STD_endl;
563 for(
int ipad=0; ipad<npad; ipad++) nn.
add_dim(1,
true);
564 ODINLOG(odinlog,normalDebug) <<
"nn(pad)" << nn << STD_endl;
565 TinyVector<int,N_rank> tv;
566 for(
unsigned int i=0; i<N_rank; i++) tv(i)=nn[i];
568 for(
unsigned int i=0;i<a.total();i++) (*
this)(create_index(i))=a[i];
569 }
else ODINLOG(odinlog,errorLog) <<
"dimension mismatch: this=" << N_rank<<
" < tjarray=" << a.dim() << STD_endl;
576 template<
typename T,
int N_rank>
579 if(format==TypeTraits::type2label((u8bit)0))
return read<u8bit> (filename);
580 if(format==TypeTraits::type2label((s8bit)0))
return read<s8bit> (filename);
581 if(format==TypeTraits::type2label((u16bit)0))
return read<u16bit>(filename);
582 if(format==TypeTraits::type2label((s16bit)0))
return read<s16bit>(filename);
583 if(format==TypeTraits::type2label((u32bit)0))
return read<u32bit>(filename);
584 if(format==TypeTraits::type2label((s32bit)0))
return read<s32bit>(filename);
585 if(format==TypeTraits::type2label((
float)0))
return read<float> (filename);
586 if(format==TypeTraits::type2label((
double)0))
return read<double>(filename);
587 ODINLOG(odinlog,errorLog) <<
"Unable to read file " << filename <<
" with data type " << format << STD_endl;
594 template<
typename T,
int N_rank>
595 template<
typename T2>
598 LONGEST_INT fsize=
filesize(filename.c_str())-offset;
599 LONGEST_INT nelements_file=fsize/
sizeof(T2);
600 LONGEST_INT length=product(Array<T,N_rank>::shape());
601 ODINLOG(odinlog,normalDebug) <<
"fsize / nelements_file / length = " << fsize <<
" / " << nelements_file <<
" / " << length << STD_endl;
603 if(!length)
return 0;
605 if(nelements_file<length) {
606 ODINLOG(odinlog,errorLog) <<
"Size of file " << filename <<
" to small for reading" << STD_endl;
610 STD_string srctype=TypeTraits::type2label((T2)0);
611 STD_string dsttype=TypeTraits::type2label((T)0);
612 ODINLOG(odinlog,normalDebug) <<
"srctype/dsttype=" << srctype <<
"/" << dsttype << STD_endl;
614 TinyVector<int,N_rank> fileshape(Array<T,N_rank>::shape());
615 fileshape(N_rank-1) *= (Converter::get_elements((T)0)/Converter::get_elements((T2)0));
625 template<
typename T,
int N_rank>
628 if(format==TypeTraits::type2label((u8bit)0))
return write<u8bit> (filename,autoscale);
629 if(format==TypeTraits::type2label((s8bit)0))
return write<s8bit> (filename,autoscale);
630 if(format==TypeTraits::type2label((u16bit)0))
return write<u16bit>(filename,autoscale);
631 if(format==TypeTraits::type2label((s16bit)0))
return write<s16bit>(filename,autoscale);
632 if(format==TypeTraits::type2label((u32bit)0))
return write<u32bit>(filename,autoscale);
633 if(format==TypeTraits::type2label((s32bit)0))
return write<s32bit>(filename,autoscale);
634 if(format==TypeTraits::type2label((
float)0))
return write<float> (filename,autoscale);
635 if(format==TypeTraits::type2label((
double)0))
return write<double>(filename,autoscale);
636 ODINLOG(odinlog,errorLog) <<
"Unable to write file " << filename <<
" with data type " << format << STD_endl;
642 template<
typename T,
int N_rank>
645 if(filename==
"")
return 0;
647 FILE* file_ptr=ODIN_FOPEN(filename.c_str(),
modestring(mode));
650 ODINLOG(odinlog,errorLog) <<
"unable to create/open file >" << filename <<
"< - " <<
lasterr() << STD_endl;
656 LONGEST_INT nmemb=Array<T,N_rank>::numElements();
657 LONGEST_INT count=fwrite(data_copy.
c_array(),
sizeof(T),nmemb,file_ptr);
659 ODINLOG(odinlog,errorLog) <<
"unable to fwrite to file >" << filename <<
"< - " <<
lasterr() << STD_endl;
663 if(file_ptr!=NULL) fclose(file_ptr);
669 template<
typename T,
int N_rank>
670 template<
typename T2>
681 filedata=converted_data;
689 template<
typename T,
int N_rank>
692 bool have_post=
false;
696 unsigned int n=Array<T,N_rank>::numElements();
698 if(pre_data.numElements()==n) have_pre=
true;
699 if(post_data.numElements()==n) have_post=
true;
701 STD_ofstream ofs(filename.c_str());
702 if(ofs.bad())
return -1;
706 for(
unsigned int i=0; i<n; i++) {
711 val=(*this)(create_index(i));
726 template<
typename T,
int N_rank>
728 STD_ifstream ifs(filename.c_str());
729 if(ifs.bad())
return -1;
732 for(
unsigned int i=0; i<Array<T,N_rank>::numElements(); i++) {
733 if(ifs.bad())
return -1;
735 TypeTraits::string2type(valstr,(*
this)(create_index(i)));
750 template<
typename T,
int N_rank>
753 return fileio_autowrite(filedata, filename, opts, prot);
756 template<
typename T,
int N_rank>
759 int result=fileio_autoread(filedata, filename, opts, prot, progmeter);
767 template<
typename T,
int N_rank>
771 for(
unsigned int i=0; i<N_rank; i++) nn[i]=Array<T,N_rank>::extent(i);
773 for(
unsigned int i=0;i<a.
total();i++) a[i]=(*
this)(create_index(i));
781 template<
typename T,
int N_rank>
782 template<
typename T2,
int N_rank2>
786 TinyVector<int,N_rank2> newshape; newshape=1;
787 for(
int i=0; i<(N_rank-N_rank2+1); i++) {
789 if(srcindex>=0 && srcindex<N_rank) newshape(0)*=Array<T,N_rank>::extent(srcindex);
791 for(
int i=0; i<(N_rank2-1); i++) {
792 int srcindex=N_rank-N_rank2+1+i;
793 if(srcindex>=0 && srcindex<N_rank) newshape(1+i)=Array<T,N_rank>::extent(srcindex);
797 newshape(N_rank2-1)=newshape(N_rank2-1)*Converter::get_elements((T)0)/Converter::get_elements((T2)0);
799 dst.resize(newshape);
810 template<
typename T,
int N_rank>
813 if(!autoscale || !std::numeric_limits<T>::is_integer) {
814 ODINLOG(odinlog,normalDebug) <<
"Using reference" << STD_endl;
815 dst.reference(*
this);
818 dst.resize(src_copy.shape());
826 template<
typename T,
int N_rank>
828 return index2extent<N_rank>(Array<T,N_rank>::shape(), index);
833 template<
typename T,
int N_rank>
835 unsigned long result=0;
836 unsigned long subsize=1;
837 for(
int i=N_rank-1;i>=0;i--) {
838 result+=subsize*indexvec(i);
839 subsize*=Array<T,N_rank>::extent(i);
846 template<
typename T,
int N_rank>
852 if( shift_dim >= N_rank ){
853 ODINLOG(odinlog,errorLog) <<
"shift dimension(" << shift_dim <<
") >= rank of data (" << N_rank <<
") !\n";
857 int shift_extent=Array<T,N_rank>::extent(shift_dim);
858 int abs_shift=abs(shift);
859 if(shift_extent < abs_shift){
860 ODINLOG(odinlog,errorLog) <<
"extent(" << shift_extent <<
") less than shift(" << abs_shift <<
") !\n";
866 TinyVector<int,N_rank> index;
867 for(
unsigned int i=0; i<Array<T,N_rank>::numElements(); i++) {
868 index=create_index(i);
869 T val=data_copy(index);
870 int shiftindex=index(shift_dim)+shift;
871 if(shiftindex>=shift_extent) shiftindex-=shift_extent;
872 if(shiftindex<0) shiftindex+=shift_extent;
873 index(shift_dim)=shiftindex;
883 template<
typename T,
int N_rank>
884 void Data<T,N_rank>::congrid(
const TinyVector<int,N_rank>& newshape,
const TinyVector<float,N_rank>* subpixel_shift,
bool left_to_right) {
886 for(
int i=0;i<N_rank;i++) {
887 ODINLOG(odinlog,normalDebug) <<
"oldshape/newshape(" << i <<
")=" << Array<T,N_rank>::shape()(i) <<
"/" << newshape(i) << STD_endl;
889 ODINLOG(odinlog,normalDebug) <<
"subpixel_shift(" << i <<
")=" << (*subpixel_shift)(i) << STD_endl;
893 for(
int irank=0; irank<N_rank; irank++) {
895 if(!left_to_right) dim=N_rank-1-irank;
897 if(subpixel_shift) shift=(*subpixel_shift)(dim);
898 interpolate1dim(dim,newshape(dim),shift);
905 template<
typename T,
int N_rank>
908 ODINLOG(odinlog,normalDebug) <<
"dim/newsize=" << dim <<
"/" << newsize << STD_endl;
910 if(newsize==Array<T,N_rank>::shape()(dim) && subpixel_shift==0.0) {
911 ODINLOG(odinlog,normalDebug) <<
"interpolation not required" << STD_endl;
916 ODINLOG(odinlog,errorLog) <<
"dim is larger than N_rank" << STD_endl;
920 ODINLOG(odinlog,errorLog) <<
"newsize is negative" << STD_endl;
924 Array<T,N_rank> olddata(*
this);
925 olddata.makeUnique();
927 TinyVector<int,N_rank> oldshape(olddata.shape());
928 int oldsize=oldshape(dim);
930 TinyVector<int,N_rank> newshape(oldshape);
931 newshape(dim)=newsize;
935 TinyVector<int,N_rank> ortho_shape(oldshape);
939 unsigned long n_ortho=product(ortho_shape);
941 ODINLOG(odinlog,normalDebug) <<
"n_ortho=" << n_ortho << STD_endl;
943 TinyVector<int,N_rank> indexvec;
944 T* oldoneline=
new T[oldsize];
946 for(
unsigned long iortho=0; iortho<n_ortho; iortho++) {
948 indexvec=index2extent<N_rank>(ortho_shape,iortho);
950 for(
int j=0; j<oldsize; j++) {
952 oldoneline[j]=olddata(indexvec);
955 T* newoneline=interpolate1D(oldoneline,oldsize,newsize,subpixel_shift);
957 for(
int j=0; j<newsize; j++) {
959 (*this)(indexvec)=newoneline[j];
971 template<
typename T,
int N_rank>
975 bool need_copying=
false;
977 #ifndef BLITZ_REPLACEMENT
980 TinyVector<int,N_rank>ord=Array<T,N_rank>::ordering();
981 for(
int i=0; i<N_rank-1; i++) {
982 if(ord(i)<ord(i+1)) need_copying=
true;
986 for(
int i=0; i<N_rank; i++) {
987 if(!Array<T,N_rank>::isRankStoredAscending(i)) need_copying=
true;
991 if(!Array<T,N_rank>::isStorageContiguous()) need_copying=
true;
996 ODINLOG(odinlog,normalDebug) <<
"need_copying" << STD_endl;
1002 return Array<T,N_rank>::data();
static void convert_array(const Src *src, Dst *dst, unsigned int srcsize, unsigned int dstsize, bool autoscale=true)
Data(BZ_ETPARM(_bz_ArrayExpr< T_expr >) expr)
int write_asc_file(const STD_string &filename, const Array< T, N_rank > &pre=defaultArray, const Array< T, N_rank > &post=defaultArray) const
int autowrite(const STD_string &filename, const FileWriteOpts &opts=FileWriteOpts(), const Protocol *prot=0) const
Data(const STD_string &filename, bool readonly, const TinyVector< int, N_rank > &shape, LONGEST_INT offset=0)
int autoread(const STD_string &filename, const FileReadOpts &opts=FileReadOpts(), Protocol *prot=0, ProgressMeter *progmeter=0)
Data(const TinyVector< int, N_rank > &dimvec, const T &val=0)
Data< T2, N_rank2 > & convert_to(Data< T2, N_rank2 > &dst, bool autoscale=true) const
Data< T, N_rank > & operator=(const Data< T, N_rank > &d)
unsigned long create_linear_index(const TinyVector< int, N_rank > &indexvec) const
int read(const STD_string &format, const STD_string &filename, LONGEST_INT offset=0)
void congrid(const TinyVector< int, N_rank > &newshape, const TinyVector< float, N_rank > *subpixel_shift=0, bool left_to_right=false)
Data(const tjarray< tjvector< T >, T > &a)
int write(const STD_string &filename, fopenMode mode=overwriteMode) const
Data(const Data< T, N_rank > &d)
bool is_filemapped() const
void shift(unsigned int shift_dim, int shift)
int write(const STD_string &filename, bool autoscale=true) const
Data(const Array< T, N_rank > &a)
int read(const STD_string &filename, LONGEST_INT offset=0)
int write(const STD_string &format, const STD_string &filename, bool autoscale=true) const
int read_asc_file(const STD_string &filename)
TinyVector< int, N_rank > create_index(unsigned long index) const
static STD_string get_memory_usage()
unsigned long dim() const
ndim & add_dim(unsigned long e, bool first=false)
unsigned long total() const
tjarray< V, T > & redim(const ndim &nn)
double secureInv(double denominator)
const char * modestring(fopenMode mode)
LONGEST_INT filesize(const char *filename)
double secureDivision(double numerator, double denominator)
void fileunmap(int fd, void *start, LONGEST_INT nbytes, LONGEST_INT offset)
void * filemap(const STD_string &filename, LONGEST_INT nbytes, LONGEST_INT offset, bool readonly, int &fd)
fvector phase(const cvector &cv)
int rmfile(const char *fname)