#include #include #include #include #include #include CPS_START_NAMESPACE using namespace std; const char * INT_FORMAT_NAME[] = { "n/a", "AUTOMATIC", "INT32BIG", "INT32LITTLE", "INT64BIG", "INT64LITTLE" }; const int INT_FORMAT_ENTRIES = sizeof(INT_FORMAT_NAME)/sizeof(INT_FORMAT_NAME[0]); IntConv::IntConv() : fileFormat(INT_UNKNOWN), hostFormat(INT_UNKNOWN), cname("IntConv") { testHostFormat(); } IntConv::IntConv(int size) : fileFormat(INT_UNKNOWN), hostFormat(INT_UNKNOWN), cname("IntConv") { testHostFormat(size); } IntConv::~IntConv() { } const char * IntConv::name(const enum INT_FORMAT format) { return INT_FORMAT_NAME[int(format)]; } char * IntConv::file2host(char * hbuf, const char * fdat, const int fdat_len) const { // trivial case if(hostFormat == fileFormat) { memcpy(hbuf,fdat,fdat_len*size(hostFormat)); return hbuf; } assert(size(hostFormat) == size(fileFormat)) ; // if needs conversion // adjust endian (currently the only conversion needed...) if(big_endian(hostFormat) != big_endian(fileFormat)) { if(size(fileFormat)==8){ byterevn64((type64*)fdat,fdat_len); copy64((type64*)hbuf, (type64*)fdat, fdat_len); } else { byterevn((type32*)fdat,fdat_len); copy32((type32*)hbuf, (type32*)fdat, fdat_len); } } return hbuf; } char * IntConv::host2file(char *fbuf, const char * hdat, const int hdat_len) const { // trivial case if(hostFormat == fileFormat) { memcpy(fbuf,hdat,hdat_len*size(fileFormat)); return fbuf; } assert(size(hostFormat) == size(fileFormat)) ; // if needs conversion // adjust endian (currently the only conversion needed) if(big_endian(hostFormat) != big_endian(fileFormat)) { if(size(fileFormat)==8){ copy64((type64*)fbuf, (type64*)hdat, hdat_len); byterevn64((type64*)fbuf,hdat_len); } else { copy32((type32*)fbuf, (type32*)hdat, hdat_len); byterevn((type32*)fbuf,hdat_len); } } // if(big_endian(hostFormat) != big_endian(fileFormat)) { // copy32((type32*)fbuf, (type32*)hdat, hdat_len); // byterevn((type32*)fbuf, hdat_len); // } return fbuf; } void IntConv::byterevn(type32 w[], int n) const { /* char * buf = (char*)w; cout << "First 16 bytes: "; for(int i=0;i<16;i++) cout << hex << (unsigned int)buf[i] << " "; cout << dec << endl; */ // cout << "Byte reverse 32 bits" << endl; register type32 oldv, newv; for(int i=0;i>= 8; } w[i] = newv; } /* cout << "First 16 bytes: "; for(int i=0;i<16;i++) cout << hex << (unsigned int)buf[i] << " "; cout << dec << endl; */ } void IntConv::byterevn64(type64 w[], int n) const{ /* char * buf = (char*)w; cout << "First 16 bytes: "; for(int i=0;i<16;i++) cout << hex << (unsigned int)buf[i] << " "; cout << dec << endl; */ // cout << "Byte reverse 64 bits" << endl; register type64 oldv, newv; for(int i=0;i>= 8; } w[i] = newv; } /* cout << "First 16 bytes: "; for(int i=0;i<16;i++) cout << hex << (unsigned int)buf[i] << " "; cout << dec << endl; */ } void IntConv::copy64(type64 tgt[], type64 src[], int n) const{ double *s = (double*)src; double *t = (double*)tgt; for(int i=0;i= 0) { // calculate global_id via siteid int loc[5]; for(int i=0;i<5;i++) { loc[i] = sid % qio_arg.NodeSites(i); sid /= qio_arg.NodeSites(i); loc[i] += qio_arg.NodeSites(i) * qio_arg.Coor(i); if(GJP.Gparity() && i==3){ stk = sid % 2; sid /= 2; } } int nstacked = 1; if(GJP.Gparity()) nstacked =2; //CK: x + nx*y + nx*ny*z + nx*ny*nz*t + nx*ny*nz*nt*stk + nx*ny*nz*nt*nstacked*s gid = 0; if(dimension==5) gid = loc[4]; gid = gid * nstacked + stk; for(int i=3;i>=0;i--) gid = gid * qio_arg.Nodes(i) * qio_arg.NodeSites(i) + loc[i]; } return s * (gid+1); } int IntConv::size(const enum INT_FORMAT datatype) const { switch(datatype) { case INT_32BIG: case INT_32LITTLE: return 4; case INT_64BIG: case INT_64LITTLE: return 8; default: return 0; } } bool IntConv::big_endian(const enum INT_FORMAT datatype) const { switch(datatype) { case INT_32BIG: case INT_64BIG: return true; case INT_32LITTLE: case INT_64LITTLE: return false; default: return false; } } CPS_END_NAMESPACE