00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef _AUXSTORAGE_H_
00019 #define _AUXSTORAGE_H_
00020
00021 #include "vec.h"
00022 #include "mat.h"
00023 #include "smat.h"
00024
00025 #include <stdlib.h>
00026 #include <sys/types.h>
00027 #include <sys/stat.h>
00028 #include <fcntl.h>
00029 #include <unistd.h>
00030
00031 namespace LA {
00032
00033
00034
00035
00036
00037
00038
00039
00040 template<typename T>
00041 class AuxStorage
00042 {
00043 char filename[32];
00044 int fd;
00045 bool deleteme;
00046 size_t recl;
00047 public:
00048 AuxStorage(void);
00049 AuxStorage(const char *name);
00050 ~AuxStorage(void) {close(fd); if(deleteme) unlink(filename);};
00051 void get(NRVec<T> &x, const int pos) const;
00052 void put(const NRVec<T> &x, const int pos);
00053 void get(NRMat<T> &x, const int pos) const;
00054 void put(const NRMat<T> &x, const int pos);
00055 void get(NRSMat<T> &x, const int pos) const;
00056 void put(const NRSMat<T> &x, const int pos);
00057 };
00058
00059 template<typename T>
00060 AuxStorage<T>::AuxStorage(void)
00061 {
00062
00063 strcpy(filename,"AUX_XXXXXX");
00064 mktemp(filename);
00065 unlink(filename);
00066 fd=open(filename,O_CREAT|O_LARGEFILE|O_RDWR,0777);
00067 if(fd<0) {perror(""); laerror("open failed in AuxStorage");}
00068 recl=0;
00069 deleteme=1;
00070 }
00071
00072 template<typename T>
00073 AuxStorage<T>::AuxStorage(const char *name)
00074 {
00075 strcpy(filename,name);
00076 unlink(filename);
00077 fd=open(filename,O_CREAT|O_LARGEFILE|O_RDWR,0777);
00078 if(fd<0) {perror(""); laerror("open failed in AuxStorage");}
00079 recl=0;
00080 deleteme=0;
00081 }
00082
00083
00084
00085 template<typename T>
00086 void AuxStorage<T>::get(NRVec<T> &x, const int pos) const
00087 {
00088 if(recl==0) laerror("get from an empty file in AuxStorage");
00089 if((off64_t)-1 == lseek64(fd,pos*((off64_t)recl),SEEK_SET)) {perror(""); laerror("seek failed in AuxStorage");}
00090 x.copyonwrite();
00091 if((ssize_t)recl!=read(fd,&x[0],recl)) {perror(""); laerror("read failed in AuxStorage");}
00092 }
00093
00094 template<typename T>
00095 void AuxStorage<T>::put(const NRVec<T> &x, const int pos)
00096 {
00097 if(recl) {if(recl!=x.size()*sizeof(T)) laerror("attempt to write objects of different size to one AuxStorage");}
00098 else recl=x.size()*sizeof(T);
00099 if((off64_t)-1 == lseek64(fd,pos*((off64_t)recl),SEEK_SET)) {perror(""); laerror("seek failed in AuxStorage");}
00100 if(0>write(fd,&x[0],recl)) {perror(""); laerror("write failed in AuxStorage");}
00101 }
00102
00103
00104 template<typename T>
00105 void AuxStorage<T>::get(NRMat<T> &x, const int pos) const
00106 {
00107 if(recl==0) laerror("get from an empty file in AuxStorage");
00108 if((off64_t)-1 == lseek64(fd,pos*((off64_t)recl),SEEK_SET)) {perror(""); laerror("seek failed in AuxStorage");}
00109 x.copyonwrite();
00110 if((ssize_t)recl!=read(fd,&x(0,0),recl)) {perror(""); laerror("read failed in AuxStorage");}
00111 }
00112
00113 template<typename T>
00114 void AuxStorage<T>::put(const NRMat<T> &x, const int pos)
00115 {
00116 if(recl) {if(recl!=x.nrows()*x.ncols()*sizeof(T)) laerror("attempt to write objects of different size to one AuxStorage");}
00117 else recl=x.nrows()*x.ncols()*sizeof(T);
00118 if((off64_t)-1 == lseek64(fd,pos*((off64_t)recl),SEEK_SET)) {perror(""); laerror("seek failed in AuxStorage");}
00119 if(0>write(fd,&x(0,0),recl)) {perror(""); laerror("write failed in AuxStorage");}
00120 }
00121
00122
00123 template<typename T>
00124 void AuxStorage<T>::get(NRSMat<T> &x, const int pos) const
00125 {
00126 if(recl==0) laerror("get from an empty file in AuxStorage");
00127 if((off64_t)-1 == lseek64(fd,pos*((off64_t)recl),SEEK_SET)) {perror(""); laerror("seek failed in AuxStorage");}
00128 x.copyonwrite();
00129 if((ssize_t)recl!=read(fd,&x(0,0),recl)) {perror(""); laerror("read failed in AuxStorage");}
00130 }
00131
00132 template<typename T>
00133 void AuxStorage<T>::put(const NRSMat<T> &x, const int pos)
00134 {
00135 if(recl) {if(recl!=x.nrows()*(x.nrows()+1)/2*sizeof(T)) laerror("attempt to write objects of different size to one AuxStorage");}
00136 else recl=x.nrows()*(x.nrows()+1)/2*sizeof(T);
00137 if((off64_t)-1 == lseek64(fd,pos*((off64_t)recl),SEEK_SET)) {perror(""); laerror("seek failed in AuxStorage");}
00138 if(0>write(fd,&x(0,0),recl)) {perror(""); laerror("write failed in AuxStorage");}
00139 }
00140
00141
00142 }
00143
00144 #endif