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

mdp_field_save.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_write(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      fwrite(p, psize,1, fp)!=1) return false;
00022   return true;
00023 }
00024 
00025 
00027 template<class T>
00028 bool mdp_field<T>::save(string filename, 
00029                         int processIO, 
00030                         long max_buffer_size, 
00031                         bool save_header,
00032                         long skip_bytes,
00033                         bool (*user_write)(FILE*, void*, long, long, long, const mdp_lattice&)) {
00034 
00035   long header_size=0;
00036   long psize=field_components*Tsize;
00037   long idx_gl, nvol_gl=lattice().nvol_gl, k;
00038   double mytime=mpi.time();
00039   header.reset();
00040   if(ME==processIO) {
00041     long *buffer_size=new long[Nproc];
00042     long *buffer_ptr =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     for(process=0; process<Nproc; process++) buffer_ptr[process]=0;
00047     cout << "Saving file " << filename 
00048          << " from process " << processIO 
00049          << " (buffer = " << max_buffer_size << " sites)" << '\n'; 
00050     fflush(stdout);
00051     FILE *fp=fopen(filename.c_str(), "wb+");
00052     if(fp==0) error("Unable to open file");      
00053 
00054     header.set_time();
00055     
00056     if(save_header) {
00057       header_size=sizeof(mdp_field_file_header);
00058       if(fseek(fp, skip_bytes, SEEK_SET) || 
00059          fwrite(&header, header_size, 1, fp)!=1)
00060         error("Unable to write file header");
00061     }
00062 
00063     skip_bytes+=header_size;
00064 
00065     for(idx_gl=0; idx_gl<nvol_gl; idx_gl++) {
00066       process=where_global(idx_gl);
00067       if((process!=NOWHERE) && (process!=processIO)) {
00068         if(buffer_ptr[process]==0) {
00069           mpi.get(buffer_size[process], process);
00070           mpi.get(&(large_buffer(process,0,0)), 
00071                   buffer_size[process]*field_components, process);
00072         }
00073         for(k=0; k<field_components; k++)
00074           short_buffer[k]=large_buffer(process,buffer_ptr[process],k);
00075         buffer_ptr[process]++;
00076         if(buffer_ptr[process]==buffer_size[process]) buffer_ptr[process]=0; 
00077       }
00078       if(process==processIO) {
00079         for(k=0; k<field_components; k++)
00080           short_buffer[k]=*(m+lattice().local(idx_gl)*field_components+k);
00081       }
00082       if(process!=NOWHERE) {
00083         if(user_write) {
00084           if(!user_write(fp, short_buffer, 
00085                          field_components*Tsize, 
00086                          skip_bytes, 
00087                          idx_gl, lattice()))
00088             error("propably out ofdisk space");
00089         } else {
00090           if(fseek(fp, idx_gl*psize+skip_bytes, SEEK_SET) ||
00091              fwrite(short_buffer, psize,1, fp)!=1)
00092             error("probably out of disk space");
00093         }
00094       }
00095     }
00096     delete[] buffer_size;
00097     delete[] buffer_ptr;
00098     delete[] short_buffer;
00099     fclose(fp);
00100   } else {
00101     int  process;
00102     long buffer_size=0, idx, idx_gl;
00103     long *local_index=new long[max_buffer_size];
00104     mdp_array<T,2> local_buffer(max_buffer_size,field_components);
00105     mdp_request request;
00106     for(idx_gl=0; idx_gl<nvol_gl; idx_gl++) {
00107       process=where_global(idx_gl);
00108       if(process==ME) {
00109         local_index[buffer_size]=lattice().local(idx_gl);
00110         buffer_size++;
00111       }
00112       if((buffer_size==max_buffer_size) || 
00113          ((idx_gl==nvol_gl-1) && (buffer_size>0))) {
00114         for(idx=0; idx<buffer_size; idx++)
00115           for(k=0; k<field_components; k++)
00116             local_buffer(idx,k)=*(m+local_index[idx]*field_components+k);
00117         mpi.put(buffer_size, processIO, request);
00118         mpi.wait(request);
00119         mpi.put(&(local_buffer(0,0)), buffer_size*field_components, 
00120                 processIO, request);
00121         mpi.wait(request);
00122         buffer_size=0;
00123       }
00124     }
00125     delete[] local_index;
00126   }
00127   if(ME==0 && mdp_shutup==FALSE) {
00128     printf("... Saving time: %f (sec)\n", mpi.time()-mytime);
00129     fflush(stdout);
00130   }
00131   return true;
00132 }

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