AMROC Main     Blockstructured Adaptive Mesh Refinement in object-oriented C++


Main Page   Class Hierarchy   Compound List   File List  

GridFunctionIO2.h

Go to the documentation of this file.
00001 #ifndef _included_GridFunctionIO2_h
00002 #define _included_GridFunctionIO2_h
00003 
00009 #include "DAGHIOParams.h"
00010 
00011 /************************************************************************************/
00012 /* Writes */
00013 /************************************************************************************/
00014 template <class Type>
00015 void GridFunction(2)<Type>::GF_Write(const int time, const int l, const int gfdtype, 
00016                                          char* ioname)
00017   {
00018    if (!io()) return;
00019 
00020    const int t = dagh_timeindex(time,l);
00021 
00022    if (comm_service::io_enabled()) {
00023      int cnt = 0;
00024      unsigned *bktsize = new unsigned [length[l]];
00025      unsigned size = 0;
00026 
00027      register int i;
00028      for (i=0; i<length[l]; i++) if (gdb[t][l][i]) {
00029        BBox bb = gdb[t][l][i]->interiorbox();
00030        if (!bb.empty()) {
00031          bktsize[cnt] = sizeof(Type)*bb.size();
00032          size += gdhdr::gdbsize(bktsize[cnt]);
00033          cnt++;
00034        }
00035      }
00036      /* If I dont have message to sent return */
00037      if (size == 0) return;
00038 
00039 #ifdef DAGH_NO_MPI
00040 #else  
00041      const int dest = comm_service::proc_io();
00042      const MPI_Comm &comm = comm_service::comm_io();
00043      if (comm_service::proc_world() > 1) {
00044 #ifdef DEBUG_PRINT_COMM_IO
00045      ( comm_service::log() << "GridFunction::GF_Write: " 
00046                            << comm_service::proc_me() << " "
00047                            << "MPI_Isend: " << " "
00048                            <<  (DAGHIOTag|DAGHIOWriteReqTag) << " "
00049                            << "Size: " << 1 << " "
00050                            << "Dest: " << dest << " "
00051                            << endl ).flush();
00052 #endif
00053 
00054        MPI_Request req ;
00055        int sndsize = size;
00056        int R = MPI_Isend(&sndsize, 1 , MPI_INT , dest,
00057                          (DAGHIOTag|DAGHIOWriteReqTag), 
00058                          comm , &req );
00059        if ( MPI_SUCCESS != R )
00060           comm_service::error_die("GridFunction::GF_Write","MPI_Isend",R);
00061   
00062        R = comm_service::serve( req );
00063        if ( MPI_SUCCESS != R )
00064           comm_service::error_die( "GridFunction::GF_Write" , 
00065                                    "comm_service::serve" , R );
00066      }
00067 #endif
00068 
00069      if (!ioname) ioname = gfname;
00070      GridDataBucket<Type>* gdbkt = 
00071              new GridDataBucket<Type>(cnt,bktsize,DAGHPacked);
00072      cnt = 0;
00073      for (i=0; i<length[l]; i++) 
00074        if (gdb[t][l][i] && !gdb[t][l][i]->interiorbox().empty()) {
00075          gdb[t][l][i]->gdbWriteData(time,gdb[t][l][i]->interiorbox(),*gdbkt,cnt);
00076          gdbkt->setgfname(ioname,cnt);
00077          (gdbkt->head(cnt))->gfdatatype = gfdtype;
00078          (gdbkt->head(cnt))->gfstaggertype = gftype;
00079          (gdbkt->head(cnt))->time = time;
00080          double Current_Time = get_phystime_timeindex(t,l);
00081          memcpy((gdbkt->head(cnt))->physical_time,
00082                  (char *)&Current_Time,sizeof(double));
00083          (gdbkt->head(cnt))->level = l;
00084          cnt++;
00085        }
00086      if (bktsize) delete [] bktsize;
00087      void *package = 0;
00088      gdbkt->pack(package,size);
00089 
00090      if (comm_service::proc_world() > 1) {
00091 #ifdef DAGH_NO_MPI
00092 #else
00093 #ifdef DEBUG_PRINT_COMM_IO
00094      ( comm_service::log() << "GridFunction::GF_Write: " 
00095                            << comm_service::proc_me() << " "
00096                            << "MPI_Isend: " << " "
00097                            <<  (DAGHIOTag|DAGHIOWriteDataTag) << " "
00098                            << "Size: " << size << " "
00099                            << "Dest: " << dest << " "
00100                            << endl ).flush();
00101 #endif
00102 
00103        MPI_Request req ;
00104        int R = MPI_Isend(package, size, MPI_BYTE, dest,
00105                         (DAGHIOTag|DAGHIOWriteDataTag), comm, &req );
00106        if ( MPI_SUCCESS != R )
00107          comm_service::error_die("GridFunction::GF_Write","MPI_Isend",R);
00108 
00109        R = comm_service::serve( req );
00110        if ( MPI_SUCCESS != R )
00111          comm_service::error_die("GridFunction::GF_Write","comm_service::serve",R);
00112 #endif
00113      }
00114      else {
00115        DAGHIO_WriteFunc wf = DAGHIO_Write[dagh.io_type];
00116        if (wf) {
00117         if (gdbkt->type() == DAGHPacked) {
00118           int num = gdbkt->num();
00119           for (int ii=0;ii<num;ii++) {
00120             (wf)(dagh, gdbkt->head(ii), gdbkt->data(ii));
00121           }
00122         }
00123         else {
00124           (wf)(dagh, gdbkt->head(),gdbkt->data());
00125         }
00126        }
00127        else {
00128          cerr << "GridFunctionIO::GF_Write: WriteFunc not set" << "\n";
00129        }
00130      }
00131      if (gdbkt) delete gdbkt;
00132    }
00133    else {
00134       cerr << "GridFunction::GF_Write: I/O Not Enabled" << "\n";
00135    }
00136   }
00137 
00138 template <class Type>
00139 void GridFunction(2)<Type>::GF_Write(const int time, const int l, 
00140                                          const BBox& where, const int gfdtype, 
00141                                          char* ioname)
00142   {
00143    if (!io()) return;
00144 
00145    assert (where.rank == dagh.rank);
00146    BBox alignedwhere = where;
00147    gdbAlignBBox(gfrank,alignedwhere,alignment);
00148 
00149    const int t = dagh_timeindex(time,l);
00150 
00151    if (comm_service::io_enabled()) {
00152      int cnt = 0;
00153      unsigned *bktsize = new unsigned [length[l]];
00154      unsigned size = 0;
00155 
00156      register int i;
00157      for (i=0; i<length[l]; i++) if (gdb[t][l][i]) {
00158        BBox bb = gdb[t][l][i]->interiorbox() * alignedwhere;
00159        bktsize[cnt] = sizeof(Type)*bb.size();
00160        size += gdhdr::gdbsize(bktsize[cnt]);
00161        cnt++;
00162      }
00163 
00164      /* If I dont have message to sent return */
00165      if (size == 0) return;
00166 
00167 #ifdef DAGH_NO_MPI
00168 #else
00169      const int dest = comm_service::proc_io();
00170      const MPI_Comm &comm = comm_service::comm_io();
00171      if (comm_service::proc_world() > 1) {
00172 #ifdef DEBUG_PRINT_COMM_IO
00173      ( comm_service::log() << "GridFunction::GF_Write: " 
00174                            << comm_service::proc_me() << " "
00175                            << "MPI_Isend: " << " "
00176                            <<  (DAGHIOTag|DAGHIOWriteReqTag) << " "
00177                            << "Size: " << 1 << " "
00178                            << "Dest: " << dest << " "
00179                            << endl ).flush();
00180 #endif
00181 
00182        MPI_Request req ;
00183        int sndsize = size;
00184        int R = MPI_Isend(&sndsize, 1 , MPI_INT , dest,
00185                          (DAGHIOTag|DAGHIOWriteReqTag), 
00186                          comm , &req );
00187        if ( MPI_SUCCESS != R )
00188           comm_service::error_die("GridFunction::GF_Write","MPI_Isend",R);
00189   
00190        R = comm_service::serve( req );
00191        if ( MPI_SUCCESS != R )
00192           comm_service::error_die( "GridFunction::GF_Write" , 
00193                                    "comm_service::serve" , R );
00194      }
00195 #endif
00196 
00197      if (!ioname) ioname = gfname;
00198      GridDataBucket<Type> *gdbkt = 
00199              new GridDataBucket<Type>(cnt,bktsize,DAGHPacked);
00200      cnt = 0;
00201      for (i=0; i<length[l]; i++) if (gdb[t][l][i]) {
00202        gdb[t][l][i]->gdbWriteData(time,alignedwhere*gdb[t][l][i]->interiorbox(),*gdbkt,cnt);
00203        gdbkt->setgfname(ioname,cnt);
00204        (gdbkt->head(cnt))->gfdatatype = gfdtype;
00205        (gdbkt->head(cnt))->gfstaggertype = gftype;
00206        (gdbkt->head(cnt))->time = time;
00207        double Current_Time = get_phystime_timeindex(t,l);
00208        memcpy((gdbkt->head(cnt))->physical_time,
00209               (char *)&Current_Time,sizeof(double));
00210        (gdbkt->head(cnt))->level = l;
00211        cnt++;
00212      }
00213      if (bktsize) delete [] bktsize;
00214      void *package = 0;
00215      gdbkt->pack(package,size);
00216 
00217      if (comm_service::proc_world() > 1) {
00218 #ifdef DAGH_NO_MPI
00219 #else
00220 #ifdef DEBUG_PRINT_COMM_IO
00221      ( comm_service::log() << "GridFunction::GF_Write: " 
00222                            << comm_service::proc_me() << " "
00223                            << "MPI_Isend: " << " "
00224                            <<  (DAGHIOTag|DAGHIOWriteDataTag) << " "
00225                            << "Size: " << size << " "
00226                            << "Dest: " << dest << " "
00227                            << endl ).flush();
00228 #endif
00229 
00230        MPI_Request req ;
00231        int R = MPI_Isend(package, size, MPI_BYTE, dest,
00232                         (DAGHIOTag|DAGHIOWriteDataTag), comm , &req );
00233        if ( MPI_SUCCESS != R )
00234           comm_service::error_die("GridFunction::GF_Write","MPI_Isend",R);
00235 
00236        R = comm_service::serve( req );
00237        if ( MPI_SUCCESS != R )
00238          comm_service::error_die( "GridFunction::GF_Write" , 
00239                                   "comm_service::serve" , R );
00240 #endif
00241      }
00242      else {
00243        DAGHIO_WriteFunc wf = DAGHIO_Write[dagh.io_type];
00244        if (wf) {
00245         if (gdbkt->type() == DAGHPacked) {
00246           int num = gdbkt->num();
00247           for (int ii=0;ii<num;ii++) {
00248             (wf)(dagh, gdbkt->head(ii), gdbkt->data(ii));
00249           }
00250         }
00251         else {
00252           (wf)(dagh, gdbkt->head(),gdbkt->data());
00253         }
00254        }
00255        else {
00256          cerr << "GridFunctionIO::GF_Write: WriteFunc not set" << "\n";
00257 
00258        }
00259      }
00260      if (gdbkt) delete gdbkt;
00261    }
00262    else {
00263       cerr << "GridFunction::GF_Write: I/O Not Enabled" << "\n";
00264    }
00265   }
00266 
00267 /************************************************************************************/
00268 /* Reads */
00269 /************************************************************************************/
00270 template <class Type>
00271 void GridFunction(2)<Type>::GF_Read(const int time, const int l, const int gfdtype,
00272                                         char* ioname)
00273   {
00274    if (!io()) return;
00275 
00276    const int t = dagh_timeindex(time,l);
00277 
00278    GridTableDataRcv** read_server = new GridTableDataRcv*[length[l]];
00279 
00280    if (comm_service::io_enabled()) {
00281      const int pworld = comm_service::proc_world();
00282      const int me = comm_service::proc_me();
00283 
00284      if (!ioname) ioname = gfname;
00285      register int i;
00286      /* Send Read Requests */
00287      for (i=0; i<length[l]; i++) if (gdb[t][l][i]) {
00288        read_server[i] = GridTableDataRcvNULL;
00289        BBox bb = gdb[t][l][i]->interiorbox();
00290        if (bb.empty()) continue;
00291        unsigned size = gdhdr::gdbsize(sizeof(Type)*bb.size());
00292        struct gdhdr* reqhdr == 
00293          new struct gdhdr(bb, gfid, gfdtype, gftype, ioname, t, time, l, i, 
00294                           me, DAGHSingle);
00295 
00296        if (pworld > 1) {
00297 #ifdef DAGH_NO_MPI
00298 #else
00299          const int dest = comm_service::proc_io();
00300          const MPI_Comm &comm = comm_service::comm_io();
00301 
00302          /* Post receive */
00303          read_server[i] = 
00304          new GridTableDataRcv(*gt,(DAGHIOTag|DAGHIOReadDataTag|i),size,dest);
00305 
00306          /* Send Read Request */
00307 #ifdef DEBUG_PRINT_COMM_IO
00308          ( comm_service::log() << "GridFunction::GF_Read: " 
00309                                << comm_service::proc_me() << " "
00310                                << "MPI_Isend: " << " "
00311                                << (DAGHIOTag|DAGHIOReadReqTag) << " "
00312                                << "Dest: " << dest << " "
00313                                << "( " << *reqhdr << ") "
00314                                << endl ).flush();
00315 #endif
00316          MPI_Request req ;
00317          int R = MPI_Isend((void *)reqhdr, sizeof(struct gdhdr), MPI_BYTE, dest,
00318                            (DAGHIOTag|DAGHIOReadReqTag), comm, &req);
00319          if ( MPI_SUCCESS != R )
00320            comm_service::error_die( "GridFunction::GF_Read" , "MPI_Isend" , R );
00321 
00322          R = comm_service::serve( req );
00323          if ( MPI_SUCCESS != R )
00324            comm_service::error_die("GridFunction::GF_Read","comm_service::serve",R);
00325 #endif
00326        }
00327        else {
00328          GridDataBucket<Type> *gdbkt = new GridDataBucket<Type>(reqhdr,sizeof(Type));
00329          DAGHIO_ReadFunc rf = DAGHIO_Read[dagh.io_type];
00330          if (rf) {
00331            (rf)(dagh,gdbkt->head(),gdbkt->data());
00332            (gdb[t][l][i]->griddata()).copy(*gdbkt);
00333          }
00334          else {
00335            cerr << "GridFunctionIO::GF_Read: ReadFunc not set" << "\n";
00336          }
00337          if (gdbkt) delete gdbkt; 
00338          gdbkt = 0;
00339        }
00340        if (reqhdr) delete reqhdr;
00341      }
00342 #ifdef DAGH_NON_MPI
00343 #else
00344      if (pworld > 1) { /* Serve till I get the data */
00345        for (i=0; i<length[l]; i++) if (gdb[t][l][i]) {
00346          if (read_server[i] && !read_server[i]->received()) {
00347            comm_service::serve(*read_server[i]->req());
00348            delete read_server[i];
00349            read_server[i] = 0;
00350          }
00351        }
00352        if (read_server) delete [] read_server;  
00353        GF_ReadData(t);
00354        //gt->flushdata(t);
00355      }
00356 #endif
00357    }
00358    else {
00359       cerr << "GridFunction::GF_Read: I/O Not Enabled" << "\n";
00360    }
00361   }
00362 
00363 template <class Type>
00364 void GridFunction(2)<Type>::GF_Read(const int time, const int l, 
00365                                         const BBox& where, const int gfdtype,
00366                                         char* ioname)
00367   {
00368    if (!io()) return;
00369 
00370    assert (where.rank == dagh.rank);
00371    BBox alignedwhere = where;
00372    gdbAlignBBox(gfrank,alignedwhere,alignment);
00373 
00374    const int t = dagh_timeindex(time,l);
00375 
00376    GridTableDataRcv** read_server = new GridTableDataRcv*[length[l]];
00377 
00378    if (comm_service::io_enabled()) {
00379      const int pworld = comm_service::proc_world();
00380      const int me = comm_service::proc_me();
00381 
00382      if (!ioname) ioname = gfname;
00383      register int i;
00384      /* Send Read Requests */
00385      for (i=0; i<length[l]; i++) if (gdb[t][l][i]) {
00386        read_server[i] = GridTableDataRcvNULL;
00387        BBox bb = gdb[t][l][i]->interiorbox() * alignedwhere;
00388        if (bb.empty()) continue;
00389        unsigned size = gdhdr::gdbsize(sizeof(Type)*bb.size());
00390        struct gdhdr* reqhdr == 
00391          new struct gdhdr(bb, gfid, gfdtype, gftype, ioname, t, time, l, i, 
00392                           me, DAGHSingle);
00393 
00394        if (pworld > 1) {
00395 #ifdef DAGH_NO_MPI
00396 #else
00397          const int dest = comm_service::proc_io();
00398          const MPI_Comm &comm = comm_service::comm_io();
00399 
00400          /* Post receive */
00401          read_server[i] = 
00402          new GridTableDataRcv(*gt,(DAGHIOTag|DAGHIOReadDataTag|i),size,dest);
00403 
00404          /* Send Read Request */
00405 #ifdef DEBUG_PRINT_COMM_IO
00406          ( comm_service::log() << "GridFunction::GF_Read: " 
00407                                << comm_service::proc_me() << " "
00408                                << "MPI_Isend: " << " "
00409                                << (DAGHIOTag|DAGHIOReadReqTag) << " "
00410                                << "Dest: " << dest << " "
00411                                << "( " << *reqhdr << ") "
00412                                << endl ).flush();
00413 #endif
00414          MPI_Request req ;
00415          int R = MPI_Isend((void *)reqhdr, sizeof(struct gdhdr), MPI_BYTE, dest,
00416                            (DAGHIOTag|DAGHIOReadReqTag), comm, &req);
00417          if ( MPI_SUCCESS != R )
00418            comm_service::error_die( "GridFunction::GF_Read" , "MPI_Isend" , R );
00419 
00420          R = comm_service::serve( req );
00421          if ( MPI_SUCCESS != R )
00422            comm_service::error_die("GridFunction::GF_Read","comm_service::serve",R);
00423 #endif     
00424        }
00425        else {
00426          GridDataBucket<Type> *gdbkt = new GridDataBucket<Type>(reqhdr,sizeof(Type));
00427          DAGHIO_ReadFunc rf = DAGHIO_Read[dagh.io_type];
00428          if (rf) {
00429            (rf)(dagh,gdbkt->head(),gdbkt->data());
00430            (gdb[t][l][i]->griddata()).copy(*gdbkt);
00431          }
00432          else {
00433            cerr << "GridFunctionIO::GF_Read: ReadFunc not set" << "\n";
00434          }
00435          if (gdbkt) delete gdbkt; 
00436          gdbkt = 0;
00437        }
00438        if (reqhdr) delete reqhdr;
00439      }
00440 #ifdef DAGH_NO_MPI
00441 #else
00442      if (pworld > 1) { /* Serve till I get the data */
00443        for (i=0; i<length[l]; i++) if (gdb[t][l][i]) {
00444          if (read_server[i] && !read_server[i]->received()) {
00445            comm_service::serve(*read_server[i]->req());
00446            delete read_server[i];
00447            read_server[i] = 0;
00448          }
00449        }
00450        if (read_server) delete [] read_server; 
00451      
00452        GF_ReadData(t);
00453        //gt->flushdata(t);
00454      }
00455 #endif
00456    }
00457    else {
00458       cerr << "GridFunction::GF_Read: I/O Not Enabled" << "\n";
00459    }
00460   }
00461 
00462 #endif
00463 
00464 
00465 
00466 
00467 
00468 
00469 
00470 


Quickstart     Users Guide     Programmers Reference     Installation      Examples     Download



AMROC Main      Home      Contact
last update: 06/01/04