/[escript]/trunk/finley/src/Mesh_read.c
ViewVC logotype

Diff of /trunk/finley/src/Mesh_read.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1887 by ksteube, Wed Oct 15 03:26:25 2008 UTC revision 2385 by gross, Wed Apr 15 03:52:38 2009 UTC
# Line 21  Line 21 
21  #include <ctype.h>  #include <ctype.h>
22  #include "Mesh.h"  #include "Mesh.h"
23    
24  #define FSCANF_CHECK(scan_ret, reason) { if (scan_ret == EOF) perror(reason); return NULL; }  #define FSCANF_CHECK(scan_ret, reason) { if (scan_ret == EOF) { perror(reason); Finley_setError(IO_ERROR,"scan error while reading finley file"); return NULL;} }
25    
26  /**************************************************************/  /**************************************************************/
27    
# Line 37  Finley_Mesh* Finley_Mesh_read(char* fnam Line 37  Finley_Mesh* Finley_Mesh_read(char* fnam
37    Finley_Mesh *mesh_p=NULL;    Finley_Mesh *mesh_p=NULL;
38    char name[LenString_MAX],element_type[LenString_MAX],frm[20];    char name[LenString_MAX],element_type[LenString_MAX],frm[20];
39    char error_msg[LenErrorMsg_MAX];    char error_msg[LenErrorMsg_MAX];
40      #ifdef Finley_TRACE
41    double time0=Finley_timer();    double time0=Finley_timer();
42      #endif
43    FILE *fileHandle_p = NULL;    FILE *fileHandle_p = NULL;
44    ElementTypeId typeID, faceTypeID, contactTypeID, pointTypeID;    ElementTypeId typeID=NoType, faceTypeID=NoType, contactTypeID=NoType, pointTypeID=NoType;
45    int scan_ret;    int scan_ret;
46        
47    Finley_resetError();    Finley_resetError();
# Line 60  Finley_Mesh* Finley_Mesh_read(char* fnam Line 62  Finley_Mesh* Finley_Mesh_read(char* fnam
62       sprintf(frm,"%%%d[^\n]",LenString_MAX-1);       sprintf(frm,"%%%d[^\n]",LenString_MAX-1);
63       scan_ret = fscanf(fileHandle_p, frm, name);       scan_ret = fscanf(fileHandle_p, frm, name);
64       FSCANF_CHECK(scan_ret, "Finley_Mesh_read")       FSCANF_CHECK(scan_ret, "Finley_Mesh_read")
65    
66        
67       /* get the nodes */       /* get the nodes */
68        
# Line 267  Finley_Mesh* Finley_Mesh_read_MPI(char* Line 270  Finley_Mesh* Finley_Mesh_read_MPI(char*
270    
271  {  {
272    
273    Paso_MPIInfo *mpi_info = Paso_MPIInfo_alloc( MPI_COMM_WORLD );    Paso_MPIInfo *mpi_info = NULL;
274    dim_t numNodes, numDim, numEle, i0, i1;    dim_t numNodes, numDim, numEle, i0, i1;
275    Finley_Mesh *mesh_p=NULL;    Finley_Mesh *mesh_p=NULL;
276    char name[LenString_MAX],element_type[LenString_MAX],frm[20];    char name[LenString_MAX],element_type[LenString_MAX],frm[20];
277    char error_msg[LenErrorMsg_MAX];    char error_msg[LenErrorMsg_MAX];
278      #ifdef Finley_TRACE
279    double time0=Finley_timer();    double time0=Finley_timer();
280      #endif
281    FILE *fileHandle_p = NULL;    FILE *fileHandle_p = NULL;
282    ElementTypeId typeID, faceTypeID, contactTypeID, pointTypeID;    ElementTypeId typeID=NoType;
   Finley_TagMap* tag_map;  
   index_t tag_key;  
283    int scan_ret;    int scan_ret;
284    
285    Finley_resetError();    Finley_resetError();
286      mpi_info = Paso_MPIInfo_alloc( MPI_COMM_WORLD );
287    
288    if (mpi_info->rank == 0) {    if (mpi_info->rank == 0) {
289       /* get file handle */       /* get file handle */
# Line 305  Finley_Mesh* Finley_Mesh_read_MPI(char* Line 309  Finley_Mesh* Finley_Mesh_read_MPI(char*
309    /* MPI Broadcast numDim, numNodes, name if there are multiple MPI procs*/    /* MPI Broadcast numDim, numNodes, name if there are multiple MPI procs*/
310    if (mpi_info->size > 1) {    if (mpi_info->size > 1) {
311      int temp1[3], error_code;      int temp1[3], error_code;
312      temp1[0] = numDim;      if (mpi_info->rank == 0) {
313      temp1[1] = numNodes;         temp1[0] = numDim;
314      temp1[2] = strlen(name) + 1;         temp1[1] = numNodes;
315           temp1[2] = strlen(name) + 1;
316        } else {
317           temp1[0] = 0;
318           temp1[1] = 0;
319           temp1[2] = 1;
320        }
321      error_code = MPI_Bcast (temp1, 3, MPI_INT,  0, mpi_info->comm);      error_code = MPI_Bcast (temp1, 3, MPI_INT,  0, mpi_info->comm);
322      if (error_code != MPI_SUCCESS) {      if (error_code != MPI_SUCCESS) {
323        Finley_setError(PASO_MPI_ERROR, "Finley_Mesh_read: broadcast of temp1 failed");        Finley_setError(PASO_MPI_ERROR, "Finley_Mesh_read: broadcast of temp1 failed");
# Line 323  Finley_Mesh* Finley_Mesh_read_MPI(char* Line 333  Finley_Mesh* Finley_Mesh_read_MPI(char*
333    }    }
334  #endif  #endif
335    
336    
337       /* allocate mesh */       /* allocate mesh */
338       mesh_p = Finley_Mesh_alloc(name,numDim,order,reduced_order,mpi_info);       mesh_p = Finley_Mesh_alloc(name,numDim,order,reduced_order,mpi_info);
339       if (Finley_noError()) {       if (Finley_noError()) {
# Line 854  Finley_Mesh* Finley_Mesh_read_MPI(char* Line 865  Finley_Mesh* Finley_Mesh_read_MPI(char*
865    
866        /* get the name tags */        /* get the name tags */
867        if (Finley_noError()) {        if (Finley_noError()) {
868          char *remainder, *ptr;          char *remainder=0, *ptr;
869          int tag_key, error_code;          size_t len=0;
870          size_t len;  #ifdef PASO_MPI
871          long cur_pos, end_pos;          int len_i;
872    #endif
873            int tag_key;
874          if (mpi_info->rank == 0) {  /* Master */          if (mpi_info->rank == 0) {  /* Master */
875        /* Read the word 'Tag' */        /* Read the word 'Tag' */
876        if (! feof(fileHandle_p)) {        if (! feof(fileHandle_p)) {
877          scan_ret = fscanf(fileHandle_p, "%s\n", name);          scan_ret = fscanf(fileHandle_p, "%s\n", name);
878          FSCANF_CHECK(scan_ret, "Finley_Mesh_read")          FSCANF_CHECK(scan_ret, "Finley_Mesh_read")
879        }        }
       /* Read rest of file in one chunk, after using seek to find length */  
880    
881  #if defined(_WIN32)  /* windows ftell lies on unix formatted text files */  #if defined(_WIN32)  /* windows ftell lies on unix formatted text files */
882    
# Line 872  Finley_Mesh* Finley_Mesh_read_MPI(char* Line 884  Finley_Mesh* Finley_Mesh_read_MPI(char*
884            len=0;            len=0;
885        while (1)        while (1)
886            {            {
887               size_t MALLOC_CHUNK = 1024;               size_t malloc_chunk = 1024;
888               size_t buff_size = 0;               size_t buff_size = 0;
889               int ch;               int ch;
890    
# Line 883  Finley_Mesh* Finley_Mesh_read_MPI(char* Line 895  Finley_Mesh* Finley_Mesh_read_MPI(char*
895               }               }
896               if( len+1 > buff_size )               if( len+1 > buff_size )
897               {               {
898                  TMPMEMREALLOC(remainder,remainder,buff_size+MALLOC_CHUNK,char);                  TMPMEMREALLOC(remainder,remainder,buff_size+malloc_chunk,char);
899               }               }
900               if( ch == EOF )               if( ch == EOF )
901               {               {
# Line 895  Finley_Mesh* Finley_Mesh_read_MPI(char* Line 907  Finley_Mesh* Finley_Mesh_read_MPI(char*
907               len++;               len++;
908            }            }
909  #else  #else
910            cur_pos = ftell(fileHandle_p);        /* Read rest of file in one chunk, after using seek to find length */
911            fseek(fileHandle_p, 0L, SEEK_END);            {
912            end_pos = ftell(fileHandle_p);               long cur_pos, end_pos;
913            fseek(fileHandle_p, (long)cur_pos, SEEK_SET);  
914        remainder = TMPMEMALLOC(end_pos-cur_pos+1, char);               cur_pos = ftell(fileHandle_p);
915        if (! feof(fileHandle_p)) {               fseek(fileHandle_p, 0L, SEEK_END);
916          scan_ret = fread(remainder, (size_t) end_pos-cur_pos, sizeof(char), fileHandle_p);               end_pos = ftell(fileHandle_p);
917              FSCANF_CHECK(scan_ret, "Finley_Mesh_read")               fseek(fileHandle_p, (long)cur_pos, SEEK_SET);
918        }               remainder = TMPMEMALLOC(end_pos-cur_pos+1, char);
919        remainder[end_pos-cur_pos] = 0;               if (! feof(fileHandle_p))
920                 {
921                    scan_ret = fread(remainder, (size_t) end_pos-cur_pos,
922                                     sizeof(char), fileHandle_p);
923    
924                    FSCANF_CHECK(scan_ret, "Finley_Mesh_read")
925                    remainder[end_pos-cur_pos] = 0;
926                }
927              }
928  #endif  #endif
929        len = strlen(remainder);        len = strlen(remainder);
930        while ((--len)>0 && isspace(remainder[len])) remainder[len]=0;  /*    while (((--len)>0) && isspace((int)(remainder[len]))) {remainder[len]=0;}  */
931          while ((len>1) && isspace(remainder[--len])) {remainder[len]=0;}
932        len = strlen(remainder);        len = strlen(remainder);
933            TMPMEMREALLOC(remainder,remainder,len+1,char);            TMPMEMREALLOC(remainder,remainder,len+1,char);
934          }          }
935  #ifdef PASO_MPI  #ifdef PASO_MPI
936          error_code = MPI_Bcast (&len, 1, MPI_INT,  0, mpi_info->comm);  
937          if (error_code != MPI_SUCCESS) {          len_i=(int) len;
938            Finley_setError(PASO_MPI_ERROR, "Finley_Mesh_read: broadcast of tag len failed");          if (MPI_Bcast (&len_i, 1, MPI_INT,  0, mpi_info->comm) != MPI_SUCCESS)
939            return NULL;          {
940               Finley_setError(PASO_MPI_ERROR,
941                               "Finley_Mesh_read: broadcast of tag len failed");
942               return NULL;
943          }          }
944            len=(size_t) len_i;
945      if (mpi_info->rank != 0) {      if (mpi_info->rank != 0) {
946        remainder = TMPMEMALLOC(len+1, char);        remainder = TMPMEMALLOC(len+1, char);
947        remainder[0] = 0;        remainder[0] = 0;
948      }      }
949          error_code = MPI_Bcast (remainder, len+1, MPI_CHAR,  0, mpi_info->comm);          if (MPI_Bcast (remainder, len+1, MPI_CHAR,  0, mpi_info->comm) !=
950          if (error_code != MPI_SUCCESS) {              MPI_SUCCESS)
951            Finley_setError(PASO_MPI_ERROR, "Finley_Mesh_read: broadcast of tags failed");          {
952            return NULL;             Finley_setError(PASO_MPI_ERROR,
953                               "Finley_Mesh_read: broadcast of tags failed");
954               return NULL;
955          }          }
956  #endif  #endif
957    
958      if (remainder[0]) {      if (remainder[0]) {
959            ptr = remainder;            ptr = remainder;
960            do {            do {

Legend:
Removed from v.1887  
changed lines
  Added in v.2385

  ViewVC Help
Powered by ViewVC 1.1.26