Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

mdp_field_load.h

Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00014 bool mdp_default_user_read(FILE *fp,
00015                            void* p,
00016                            long psize,
00017                            long header_size,
00018                            long position,
00019                            const mdp_lattice &lattice) {
00020   if(fseek(fp, position*psize+header_size, SEEK_SET) ||
00021      fread(p, psize,1, fp)!=1) return false;
00022   return true;
00023 }
00024 
00025 
00027 template<class T>
00028 bool mdp_field<T>::load(string filename, 
00029                         int processIO, 
00030                         long max_buffer_size, 
00031                         bool load_header,
00032                         long skip_bytes,
00033                         bool (*user_read)(FILE*, void*, long, long, long, const mdp_lattice&),
00034                         bool try_swicth_endianess) {
00035 
00036   long header_size=0;
00037   long idx_gl, nvol_gl=lattice().nvol_gl, k;
00038   long psize=field_components*Tsize;
00039   double mytime=mpi.time();
00040   bool reversed_header_endianess=false;
00041   if(ME==processIO) {
00042     long *buffer_size=new long[Nproc];
00043     mdp_array<T,3> large_buffer(Nproc,max_buffer_size,field_components);
00044     T *short_buffer=new T[field_components];
00045     int process;
00046     mdp_request request;
00047 
00048     for(process=0; process<Nproc; process++) buffer_size[process]=0;
00049     cout << "Loading file " << filename 
00050          << " from process " << processIO 
00051          << " (buffer = " << max_buffer_size << " sites)" << '\n'; 
00052     fflush(stdout);
00053     FILE *fp=fopen(filename.c_str(), "rb");
00054     if(fp==0) error("Unable to open file");
00055     
00056     int i;
00057     if(load_header) {
00058       mdp_field_file_header tmp_header;
00059       header_size=sizeof(mdp_field_file_header);
00060       if(fseek(fp, skip_bytes, SEEK_SET) || 
00061            fread(&tmp_header, header_size, 1, fp)!=1) {
00062         fprintf(stderr,"mdp_field.load(): Unable to load file header\n");
00063         return false;
00064       }
00065       
00066       reversed_header_endianess=switch_header_endianess(tmp_header);
00067       if(tmp_header.endianess!=header.endianess) 
00068         fprintf(stderr, "Unrecognized endianess... trying to read anyway\n");
00069 
00070       if(tmp_header.ndim!=header.ndim) {
00071         fprintf(stderr,"mdp_field.load(): wrong ndim\n");
00072         return false;
00073       }
00074       for(i=0; i<lattice().ndim; i++)  
00075         if(tmp_header.box[i]!=header.box[i]) {
00076           fprintf(stderr,"mdp_file.load(): wrong lattice size\n");
00077           return false;
00078         }
00079       if(tmp_header.bytes_per_site!=header.bytes_per_site) {
00080         fprintf(stderr, "mdp_file.load(): wrong type of field\n");
00081         return false;
00082       }
00083       if(tmp_header.sites!=header.sites) {
00084         fprintf(stderr,"mdp_field.load(): wrong number of sites\n");
00085         return false;
00086       }
00087       header=tmp_header;
00088     }
00089     
00090     skip_bytes+=header_size;
00091     
00092     for(idx_gl=0; idx_gl<nvol_gl; idx_gl++) {
00093       process=where_global(idx_gl);
00094       if(process!=NOWHERE) {
00095           if(user_read) {
00096             if(!user_read(fp, short_buffer, 
00097                           field_components*Tsize, 
00098                           skip_bytes, 
00099                           idx_gl, lattice()))
00100               error("unexpected end of file");
00101           } else {
00102             if(fseek(fp, idx_gl*psize+skip_bytes, SEEK_SET) ||
00103                fread(short_buffer, psize, 1, fp)!=1) 
00104               error("unexpected end of file");
00105           }
00106       }
00107       if((process!=NOWHERE) && (process!=processIO)) {
00108         for(k=0; k<field_components; k++)
00109           large_buffer(process,buffer_size[process],k)=short_buffer[k];
00110         buffer_size[process]++;
00111         if(buffer_size[process]==max_buffer_size) {
00112             mpi.put(&(large_buffer(process,0,0)), 
00113                     max_buffer_size*field_components, process, request); 
00114             mpi.wait(request);
00115             buffer_size[process]=0;
00116         }
00117         if(idx_gl==nvol_gl-1) 
00118           for(process=0; process<Nproc; process++)
00119             if((process!=ME) && 
00120                  (buffer_size[process]!=max_buffer_size) &&
00121                (buffer_size[process]>0)) {
00122                 mpi.put(&(large_buffer(process,0,0)), 
00123                         buffer_size[process]*field_components, 
00124                         process, request); 
00125                 mpi.wait(request);
00126             }
00127       }
00128       if(process==processIO) {
00129           for(k=0; k<field_components; k++)
00130             *(m+lattice().local(idx_gl)*field_components+k)=short_buffer[k];
00131       }
00132     }
00133     delete[] buffer_size;
00134     delete[] short_buffer;
00135     fclose(fp);
00136   } else {
00137     int  process;
00138     long buffer_size=0, idx;
00139     long *local_index=new long[max_buffer_size];
00140     mdp_array<T,2> local_buffer(max_buffer_size,field_components);
00141     for(idx_gl=0; idx_gl<nvol_gl; idx_gl++) {
00142       process=where_global(idx_gl);
00143       if(process==ME) {
00144         local_index[buffer_size]=lattice().local(idx_gl);
00145         buffer_size++;
00146       }
00147       if((buffer_size==max_buffer_size) || 
00148          ((idx_gl==nvol_gl-1) && (buffer_size>0))) {
00149         mpi.get(&(local_buffer(0,0)), buffer_size*field_components, processIO);
00150         for(idx=0; idx<buffer_size; idx++)
00151           for(k=0; k<field_components; k++)
00152             *(m+local_index[idx]*field_components+k)=local_buffer(idx,k);
00153         buffer_size=0;
00154       }
00155     }
00156     delete[] local_index;
00157   }
00158 
00159   update();
00160   mpi.broadcast(reversed_header_endianess,processIO);
00161   if(try_swicth_endianess && reversed_header_endianess) 
00162     switch_endianess_4bytes();
00163  
00164   if(ME==0 && mdp_shutup==FALSE) {
00165     printf("... Loading time: %f (sec)\n", mpi.time()-mytime);
00166     fflush(stdout);
00167   }
00168   return true;
00169 }
00170 

Generated on Sun Feb 27 15:12:20 2005 by  doxygen 1.4.1