support for Orca format in binary put() and get()

This commit is contained in:
2025-11-28 14:20:05 +01:00
parent 253a554e8e
commit 3a176ddb13
6 changed files with 65 additions and 28 deletions

View File

@@ -392,10 +392,10 @@ static inline bool bigger(const X<C> &x, const X<C> &y) {return x>y;} \
static inline bool smaller(const X<C> &x, const X<C> &y) {return x<y;} \
static inline normtype norm (const X<C> &x) {return x.norm();} \
static inline void axpy (X<C>&s, const X<C> &x, const C c) {s.axpy(c,x);} \
static void put(int fd, const X<C> &x, bool dimensions=1, bool transp=0) {x.put(fd,dimensions,transp);} \
static void get(int fd, X<C> &x, bool dimensions=1, bool transp=0) {x.get(fd,dimensions,transp);} \
static void multiput(size_t n,int fd, const X<C> *x, bool dimensions=1) {for(size_t i=0; i<n; ++i) x[i].put(fd,dimensions);} \
static void multiget(size_t n,int fd, X<C> *x, bool dimensions=1) {for(size_t i=0; i<n; ++i) x[i].get(fd,dimensions);} \
static void put(int fd, const X<C> &x, bool dimensions=1, bool transp=0, bool orcaformat=false) {x.put(fd,dimensions,transp,orcaformat);} \
static void get(int fd, X<C> &x, bool dimensions=1, bool transp=0, bool orcaformat=false) {x.get(fd,dimensions,transp,orcaformat);} \
static void multiput(size_t n,int fd, const X<C> *x, bool dimensions=1, bool orcaformat=false) {for(size_t i=0; i<n; ++i) x[i].put(fd,dimensions,false,orcaformat);} \
static void multiget(size_t n,int fd, X<C> *x, bool dimensions=1, bool orcaformat=false) {for(size_t i=0; i<n; ++i) x[i].get(fd,dimensions,false,orcaformat);} \
static void copy(X<C> *dest, X<C> *src, size_t n) {for(size_t i=0; i<n; ++i) dest[i]=src[i];} \
static void clear(X<C> *dest, size_t n) {for(size_t i=0; i<n; ++i) dest[i].clear();}\
static void copyonwrite(X<C> &x) {x.copyonwrite();}\
@@ -435,10 +435,10 @@ static inline bool bigger(const C &x, const C &y) {return x>y;} \
static inline bool smaller(const C &x, const C &y) {return x<y;} \
static inline normtype norm (const X<C> &x) {return x.norm();} \
static inline void axpy (X<C>&s, const X<C> &x, const C c) {s.axpy(c,x);} \
static void put(int fd, const X<C> &x, bool dimensions=1, bool transp=0) {x.put(fd,dimensions);} \
static void get(int fd, X<C> &x, bool dimensions=1, bool transp=0) {x.get(fd,dimensions);} \
static void multiput(size_t n,int fd, const X<C> *x, bool dimensions=1) {for(size_t i=0; i<n; ++i) x[i].put(fd,dimensions);} \
static void multiget(size_t n,int fd, X<C> *x, bool dimensions=1) {for(size_t i=0; i<n; ++i) x[i].get(fd,dimensions);} \
static void put(int fd, const X<C> &x, bool dimensions=1, bool transp=0, bool orcaformat=false) {x.put(fd,dimensions,false,orcaformat);} \
static void get(int fd, X<C> &x, bool dimensions=1, bool transp=0, bool orcaformat=false) {x.get(fd,dimensions,false,orcaformat);} \
static void multiput(size_t n,int fd, const X<C> *x, bool dimensions=1, bool orcaformat=false) {for(size_t i=0; i<n; ++i) x[i].put(fd,dimensions,false,orcaformat);} \
static void multiget(size_t n,int fd, X<C> *x, bool dimensions=1, bool orcaformat=false) {for(size_t i=0; i<n; ++i) x[i].get(fd,dimensions,false,orcaformat);} \
static void copy(C *dest, C *src, size_t n) {for(size_t i=0; i<n; ++i) dest[i]=src[i];} \
static void clear(C *dest, size_t n) {for(size_t i=0; i<n; ++i) dest[i].clear();} \
static void copyonwrite(X<C> &x) {x.copyonwrite();} \

19
mat.cc
View File

@@ -121,12 +121,12 @@ const NRVec<T> NRMat<T>::row(const int i, int l) const {
* @see NRVec<T>::put()
******************************************************************************/
template <typename T>
void NRMat<T>::put(int fd, bool dim, bool transp) const {
void NRMat<T>::put(int fd, bool dim, bool transp, bool orcaformat) const {
#ifdef CUDALA
if(location != cpu) {
NRMat<T> tmp = *this;
tmp.moveto(cpu);
tmp.put(fd, dim, transp);
tmp.put(fd, dim, transp, orcaformat);
return;
}
#endif
@@ -134,6 +134,11 @@ void NRMat<T>::put(int fd, bool dim, bool transp) const {
if(dim){
if(sizeof(int) != write(fd,&(transp?mm:nn),sizeof(int))) laerror("write failed");
if(sizeof(int) != write(fd,&(transp?nn:mm),sizeof(int))) laerror("write failed");
if(orcaformat)
{
int tmp=sizeof(T);
if(sizeof(int) != write(fd,&tmp,sizeof(int))) laerror("write failed");
}
}
if(transp){ //not particularly efficient
@@ -167,12 +172,12 @@ void NRMat<T>::put(int fd, bool dim, bool transp) const {
* @see NRVec<T>::get(), copyonwrite()
******************************************************************************/
template <typename T>
void NRMat<T>::get(int fd, bool dim, bool transp){
void NRMat<T>::get(int fd, bool dim, bool transp, bool orcaformat){
#ifdef CUDALA
if(location != cpu){
NRMat<T> tmp;
tmp.moveto(cpu);
tmp.get(fd, dim, transp);
tmp.get(fd, dim, transp,orcaformat);
tmp.moveto(getlocation());
*this = tmp;
return;
@@ -183,6 +188,12 @@ void NRMat<T>::get(int fd, bool dim, bool transp){
if(dim){
if(sizeof(int) != read(fd, &nn0, sizeof(int))) laerror("read failed");
if(sizeof(int) != read(fd, &mm0, sizeof(int))) laerror("read failed");
if(orcaformat)
{
int tmp;
if(sizeof(int) != read(fd, &tmp, sizeof(int))) laerror("read failed");
if(tmp!=sizeof(T)) laerror("mismatch in orca format");
}
if(transp) resize(mm0, nn0); else resize(nn0, mm0);
}else{
copyonwrite();

4
mat.h
View File

@@ -305,9 +305,9 @@ public:
inline size_t size() const;
//! unformatted input
void get(int fd, bool dimensions = 1, bool transposed = false);
void get(int fd, bool dimensions = 1, bool transposed = false, bool orcaformat=false);
//! unformatted output
void put(int fd, bool dimensions = 1, bool transposed = false) const;
void put(int fd, bool dimensions = 1, bool transposed = false, bool orcaformat=false) const;
//! formatted output
void fprintf(FILE *f, const char *format, const int modulo) const;
//! formatted input

19
smat.cc
View File

@@ -41,12 +41,12 @@ namespace LA {
* @see NRMat<T>::get(), NRSMat<T>::copyonwrite()
******************************************************************************/
template <typename T>
void NRSMat<T>::put(int fd, bool dim, bool transp) const {
void NRSMat<T>::put(int fd, bool dim, bool transp, bool orcaformat) const {
#ifdef CUDALA
if(location != cpu){
NRSMat<T> tmp= *this;
tmp.moveto(cpu);
tmp.put(fd,dim,transp);
tmp.put(fd,dim,transp,orcaformat);
return;
}
#endif
@@ -54,6 +54,11 @@ void NRSMat<T>::put(int fd, bool dim, bool transp) const {
if(dim){
if(sizeof(int) != write(fd,&nn,sizeof(int))) laerror("cannot write");
if(sizeof(int) != write(fd,&nn,sizeof(int))) laerror("cannot write");
if(orcaformat)
{
int tmp=sizeof(T);
if(sizeof(int) != write(fd,&tmp,sizeof(int))) laerror("cannot write");
}
}
LA_traits<T>::multiput((size_t)nn*(nn+1)/2,fd,v,dim);
}
@@ -66,12 +71,12 @@ void NRSMat<T>::put(int fd, bool dim, bool transp) const {
* @see NRSMat<T>::put(), NRSMat<T>::copyonwrite()
******************************************************************************/
template <typename T>
void NRSMat<T>::get(int fd, bool dim, bool transp) {
void NRSMat<T>::get(int fd, bool dim, bool transp, bool orcaformat) {
#ifdef CUDALA
if(location != cpu){
NRSMat<T> tmp;
tmp.moveto(cpu);
tmp.get(fd,dim,transp);
tmp.get(fd,dim,transp,orcaformat);
tmp.moveto(location);
*this = tmp;
return;
@@ -82,6 +87,12 @@ void NRSMat<T>::get(int fd, bool dim, bool transp) {
errno = 0;
if(dim){
if(2*sizeof(int) != read(fd,&nn0,2*sizeof(int))) laerror("cannot read");
if(orcaformat)
{
int tmp;
if(sizeof(int) != read(fd,&tmp,sizeof(int))) laerror("cannot read");
if(tmp!=sizeof(T)) laerror("mismatch in orca format");
}
resize(nn0[0]);
}else{
copyonwrite();

4
smat.h
View File

@@ -183,8 +183,8 @@ public:
const T trace() const;
void get(int fd, bool dimensions = 1, bool transp = 0);
void put(int fd, bool dimensions = 1, bool transp = 0) const;
void get(int fd, bool dimensions = 1, bool transp = 0, bool orcaformat=false);
void put(int fd, bool dimensions = 1, bool transp = 0, bool orcaformat=false) const;
void copyonwrite(bool detachonly=false, bool deep=true);

31
vec.h
View File

@@ -455,12 +455,12 @@ public:
//! routine for formatted output
void fprintf(FILE *f, const char *format, const int modulo) const;
//! routine for unformatted output
void put(int fd, bool dimensions=1, bool transp=0) const;
void put(int fd, bool dimensions=1, bool transp=0, bool orca_format=false) const;
//! routine for formatted input
void fscanf(FILE *f, const char *format);
//! routine for unformatted input
void get(int fd, bool dimensions=1, bool transp=0);
void get(int fd, bool dimensions=1, bool transp=0, bool orca_format=false);
//! constructor creating vector from sparse matrix
explicit NRVec(const SparseMat<T> &rhs);
@@ -2015,12 +2015,12 @@ inline const std::complex<double> NRVec<std::complex<double> >::amin() const {
* @see NRMat<T>::put()
******************************************************************************/
template <typename T>
void NRVec<T>::put(int fd, bool dim, bool transp) const {
void NRVec<T>::put(int fd, bool dim, bool transp, bool orcaformat) const {
#ifdef CUDALA
if(location != cpu){
NRVec<T> tmp = *this;
tmp.moveto(cpu);
tmp.put(fd,dim,transp);
tmp.put(fd,dim,transp,orcaformat);
return;
}
#endif
@@ -2028,7 +2028,15 @@ void NRVec<T>::put(int fd, bool dim, bool transp) const {
int pad(1); //align at least 8-byte
if(dim){
if(sizeof(int) != write(fd,&nn,sizeof(int))) laerror("write failed");
if(sizeof(int) != write(fd,&pad,sizeof(int))) laerror("write failed");
if(!orcaformat)
{
if(sizeof(int) != write(fd,&pad,sizeof(int))) laerror("write failed");
}
if(orcaformat)
{
int tmp=sizeof(T);
if(sizeof(int) != write(fd,&tmp,sizeof(int))) laerror("write failed");
}
}
LA_traits<T>::multiput(nn,fd,v,dim);
}
@@ -2041,12 +2049,12 @@ void NRVec<T>::put(int fd, bool dim, bool transp) const {
* @see NRMat<T>::get(), copyonwrite()
******************************************************************************/
template <typename T>
void NRVec<T>::get(int fd, bool dim, bool transp) {
void NRVec<T>::get(int fd, bool dim, bool transp, bool orcaformat) {
#ifdef CUDALA
if(location != cpu){
NRVec<T> tmp;
tmp.moveto(cpu);
tmp.get(fd,dim,transp);
tmp.get(fd,dim,transp,orcaformat);
tmp.moveto(location);
*this = tmp;
return;
@@ -2055,7 +2063,14 @@ void NRVec<T>::get(int fd, bool dim, bool transp) {
int nn0[2]; //align at least 8-byte
errno = 0;
if(dim){
if(2*sizeof(int) != read(fd,&nn0,2*sizeof(int))) laerror("read failed");
int ndims=orcaformat?1:2;
if(ndims*sizeof(int) != read(fd,&nn0,ndims*sizeof(int))) laerror("read failed");
if(orcaformat)
{
int tmp;
if(sizeof(int) != read(fd,&tmp,sizeof(int))) laerror("read failed");
if(tmp!=sizeof(T)) laerror("mismatch in orca format");
}
resize(nn0[0]);
}else{
copyonwrite();