Changeset 20840


Ignore:
Timestamp:
07/01/16 13:05:48 (9 years ago)
Author:
Mathieu Morlighem
Message:

CHG: fixed kriging with new marshall

Location:
issm/trunk-jpl/src
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/c/classes/IoModel.cpp

    r20810 r20840  
    11421142}
    11431143/*}}}*/
    1144 void  IoModel::FetchData(Option** poption,int index){/*{{{*/
    1145 
    1146         _error_("not implemented yet");
    1147 
    1148         /*output: */
    1149         //int     code;
    1150         //char   *name        = NULL;
    1151 
    1152         ///*First get option name*/
    1153         //this->FetchData(&name,index);
    1154 
    1155         ///*Get option value*/
    1156         //fid=this->SetFilePointerToData(&code,NULL,index+1);
    1157         //switch(code){
    1158         //      case 3: {//IssmDouble
    1159         //                              GenericOption<IssmDouble>* option;
    1160         //                              IssmDouble value;
    1161         //                              FetchData(&value,index+1);
    1162         //                              option = new GenericOption<IssmDouble>();
    1163         //                              option->value = value;
    1164         //                              option->name  = name;
    1165         //                              option->numel = 1;
    1166         //                              option->ndims = 1;
    1167         //                              option->size  = NULL;
    1168         //                              /*Assign output pointers: */
    1169         //                              *poption=option;
    1170         //                              break;
    1171         //                      }
    1172         //      case 4: {//char
    1173         //                              GenericOption<char*>* option;
    1174         //                              char* value = NULL;
    1175         //                              FetchData(&value,index+1);
    1176         //                              option = new GenericOption<char*>();
    1177         //                              option->value = value;
    1178         //                              option->name  = name;
    1179         //                              option->numel = 1;
    1180         //                              option->ndims = 1;
    1181         //                              option->size  = NULL;
    1182         //                              *poption=option;
    1183         //                              break;
    1184         //                      }
    1185         //      default:
    1186         //                _error_("Option of format " << code << " not supported yet");
    1187         //}
     1144void  IoModel::FetchData(Options* options,const char* lastnonoption){/*{{{*/
     1145
     1146        /*record descriptions; */
     1147        const char* mddot = "md.";
     1148        char* record_name = NULL;
     1149        int   record_name_size;
     1150        int   record_length;
     1151        int   record_code;
     1152
     1153        /*records: */
     1154        IssmDouble   scalar = 0;
     1155        char        *string = NULL;
     1156        int          string_size;
     1157
     1158        /*recover my_rank:*/
     1159        int my_rank=IssmComm::GetRank();
     1160
     1161        /*Go find in the binary file, the position of the data we want to fetch: */
     1162        if(my_rank==0){
     1163                fseek(fid,0,SEEK_SET);
     1164                for(;;){
     1165                        /*Read size of first string name: */
     1166                        if(fread(&record_name_size,sizeof(int),1,fid)==0) _error_("could not read record_name");
     1167                        if(record_name_size<3 || record_name_size>80) _error_("error while looking in binary file. Found a string of size "<<record_name_size);
     1168
     1169                        /*Allocate string of correct size: */
     1170                        record_name=xNew<char>(record_name_size+1);
     1171                        record_name[record_name_size]='\0';
     1172
     1173                        /*Read record_name: */
     1174                        if(fread(record_name,record_name_size*sizeof(char),1,fid)==0)_error_("Could not find field "<<lastnonoption);
     1175                        if(strncmp(record_name,mddot,3)!=0) _error_("error while reading binary file: record does not start with \"md.\": "<<record_name);
     1176
     1177                        /*Is this the record sought for? : */
     1178                        if(strcmp(record_name,lastnonoption)==0){
     1179                                if(fread(&record_length,sizeof(int),1,fid)!=1) _error_("Could not read record_length");
     1180                                fseek(fid,record_length,SEEK_CUR);
     1181                                xDelete<char>(record_name);
     1182                                break;
     1183                        }
     1184                        else{
     1185                                if(fread(&record_length,sizeof(int),1,fid)!=1) _error_("Could not read record_length");
     1186                                fseek(fid,record_length,SEEK_CUR);
     1187                                xDelete<char>(record_name);
     1188                        }
     1189                }
     1190        }
     1191
     1192        /*Go find in the binary file, the position of the data we want to fetch: */
     1193        if(my_rank==0){ //cpu 0{{{
     1194
     1195                /*Now march through file looking for the correct data identifiers (bool,int,IssmDouble or string): */
     1196                for(;;){
     1197
     1198                        /*Read size of first string name: */
     1199                        if(fread(&record_name_size,sizeof(int),1,fid)==0){
     1200                                /*we have reached the end of the file. break: */
     1201                                record_code=0; //0 means bailout
     1202                                ISSM_MPI_Bcast(&record_code,1,ISSM_MPI_INT,0,IssmComm::GetComm());  /*tell others cpus we are bailing: */
     1203                                break;
     1204                        }
     1205                        if(record_name_size<3 || record_name_size>80){
     1206                                _error_("error while looking in binary file. Found a string of size "<<record_name_size);
     1207                        }
     1208
     1209                        /*Allocate string of correct size: */
     1210                        record_name=xNew<char>(record_name_size+1);
     1211                        record_name[record_name_size]='\0';
     1212
     1213                        /*Read record_name: */
     1214                        if(fread(record_name,record_name_size*sizeof(char),1,fid)==0){
     1215                                _error_("Could not read record name");
     1216                        }
     1217                        if(strncmp(record_name,mddot,3)!=0){
     1218                                _error_("error while reading binary file: record does not start with \"md.\": "<<record_name);
     1219                        }
     1220                        if(strcmp(record_name,"md.EOF")==0){
     1221                                xDelete<char>(record_name);
     1222                                record_code=0; //0 means bailout
     1223                                ISSM_MPI_Bcast(&record_code,1,ISSM_MPI_INT,0,IssmComm::GetComm());  /*tell others cpus we are bailing: */
     1224                                break;
     1225                        }
     1226
     1227                        /* Read the record length and the data type code: */
     1228                        if(fread(&record_length,sizeof(int),1,this->fid)!=1) _error_("Cound not read record_length");
     1229                        if(fread(&record_code  ,sizeof(int),1,this->fid)!=1) _error_("Cound not read record_code");
     1230
     1231                        /*Tell other cpus what we are doing: */
     1232                        ISSM_MPI_Bcast(&record_code,1,ISSM_MPI_INT,0,IssmComm::GetComm());  /*tell other cpus what we are going to do: */
     1233
     1234                        /*Tell other cpus the name of the data, then branch according to the data type: */
     1235                        ISSM_MPI_Bcast(&record_name_size,1,ISSM_MPI_INT,0,IssmComm::GetComm());
     1236                        ISSM_MPI_Bcast(record_name,record_name_size,ISSM_MPI_CHAR,0,IssmComm::GetComm());
     1237                        ISSM_MPI_Bcast(&record_length,1,ISSM_MPI_INT,0,IssmComm::GetComm()); 
     1238
     1239                        switch(record_code){
     1240                                case 3:
     1241                                          {
     1242                                                if(fread(&scalar,sizeof(IssmPDouble),1,this->fid)!=1) _error_("could not read scalar ");
     1243                                                ISSM_MPI_Bcast(&scalar,1,ISSM_MPI_PDOUBLE,0,IssmComm::GetComm());
     1244                                                GenericOption<IssmDouble>* option = new GenericOption<IssmDouble>();
     1245                                                char* optionname=xNew<char>(strlen(record_name)-3+1);
     1246                                                xMemCpy(optionname,&record_name[3],strlen(record_name)-3+1);
     1247                                                option->value = scalar;
     1248                                                option->name  = optionname;
     1249                                                option->numel = 1;
     1250                                                option->ndims = 1;
     1251                                                option->size  = NULL;
     1252                                                options->AddOption(option);
     1253                                          }
     1254                                        break;
     1255                                case 4:
     1256                                          {
     1257                                        /*We have to read a string from disk. First read the dimensions of the string, then the string: */
     1258                                        if(fread(&string_size,sizeof(int),1,this->fid)!=1) _error_("could not read length of string ");
     1259                                        ISSM_MPI_Bcast(&string_size,1,ISSM_MPI_INT,0,IssmComm::GetComm());
     1260
     1261                                        if(string_size){
     1262                                                string=xNew<char>(string_size+1);
     1263                                                string[string_size]='\0';
     1264
     1265                                                /*Read string, then broadcast: */
     1266                                                if(fread(string,string_size*sizeof(char),1,this->fid)!=1)_error_(" could not read string ");
     1267                                                ISSM_MPI_Bcast(string,string_size,ISSM_MPI_CHAR,0,IssmComm::GetComm());
     1268                                        }
     1269                                        else{
     1270                                                string=xNew<char>(1);
     1271                                                string[0]='\0';
     1272                                        }
     1273
     1274                                        /*Add string to parameters: */
     1275                                        GenericOption<char*>* option = new GenericOption<char*>();
     1276                                        char* optionname=xNew<char>(strlen(record_name)-3+1);
     1277                                        xMemCpy(optionname,&record_name[3],strlen(record_name)-3+1);
     1278                                        option->value = string;
     1279                                        option->name  = optionname;
     1280                                        option->numel = 1;
     1281                                        option->ndims = 1;
     1282                                        option->size  = NULL;
     1283                                        options->AddOption(option);
     1284
     1285                                          }
     1286                                        break;
     1287                                default:
     1288                                        _error_("record type not supported:" << record_code);
     1289                                        break;
     1290                        }
     1291                        xDelete<char>(record_name);
     1292                }
     1293        } //}}}
     1294        else{ //cpu ~0 {{{
     1295                for(;;){ //wait on cpu 0
     1296                        ISSM_MPI_Bcast(&record_code,1,ISSM_MPI_INT,0,IssmComm::GetComm());  /*get from cpu 0 what we are going to do: */
     1297                        if(record_code==0){
     1298                                break; //we are done, break from the loop
     1299                        }
     1300                        else{
     1301                                ISSM_MPI_Bcast(&record_name_size,1,ISSM_MPI_INT,0,IssmComm::GetComm());
     1302                                _assert_(record_name_size);
     1303                                record_name=xNew<char>((record_name_size+1)); record_name[record_name_size]='\0';
     1304                                ISSM_MPI_Bcast(record_name,record_name_size,ISSM_MPI_CHAR,0,IssmComm::GetComm());
     1305                                ISSM_MPI_Bcast(&record_length,1,ISSM_MPI_INT,0,IssmComm::GetComm()); 
     1306                                switch(record_code){
     1307                                        case 3:
     1308                                                  {
     1309                                                        if(fread(&scalar,sizeof(IssmPDouble),1,this->fid)!=1) _error_("could not read scalar ");
     1310                                                        ISSM_MPI_Bcast(&scalar,1,ISSM_MPI_PDOUBLE,0,IssmComm::GetComm());
     1311                                                        char* optionname=xNew<char>(strlen(record_name)-3+1);
     1312                                                        xMemCpy(optionname,&record_name[3],strlen(record_name)-3+1);
     1313                                                        GenericOption<IssmDouble>* option = new GenericOption<IssmDouble>();
     1314                                                        option->value = scalar;
     1315                                                        option->name  = optionname;
     1316                                                        option->numel = 1;
     1317                                                        option->ndims = 1;
     1318                                                        option->size  = NULL;
     1319                                                        options->AddOption(option);
     1320                                                  }
     1321                                                break;
     1322                                        case 4:
     1323                                                  {
     1324                                                /*We have to read a string from disk. First read the dimensions of the string, then the string: */
     1325                                                if(fread(&string_size,sizeof(int),1,this->fid)!=1) _error_("could not read length of string ");
     1326                                                ISSM_MPI_Bcast(&string_size,1,ISSM_MPI_INT,0,IssmComm::GetComm());
     1327
     1328                                                if(string_size){
     1329                                                        string=xNew<char>(string_size+1);
     1330                                                        string[string_size]='\0';
     1331
     1332                                                        /*Read string, then broadcast: */
     1333                                                        if(fread(string,string_size*sizeof(char),1,this->fid)!=1)_error_(" could not read string ");
     1334                                                        ISSM_MPI_Bcast(string,string_size,ISSM_MPI_CHAR,0,IssmComm::GetComm());
     1335                                                }
     1336                                                else{
     1337                                                        string=xNew<char>(1);
     1338                                                        string[0]='\0';
     1339                                                }
     1340
     1341                                                /*Add string to parameters: */
     1342                                                char* optionname=xNew<char>(strlen(record_name)-3+1);
     1343                                                xMemCpy(optionname,&record_name[3],strlen(record_name)-3+1);
     1344                                                GenericOption<char*>* option = new GenericOption<char*>();
     1345                                                option->value = string;
     1346                                                option->name  = optionname;
     1347                                                option->numel = 1;
     1348                                                option->ndims = 1;
     1349                                                option->size  = NULL;
     1350                                                options->AddOption(option);
     1351                                                  }
     1352                                                break;
     1353                                        default:
     1354                                                _error_("record type not supported:" << record_code);
     1355                                                break;
     1356                                }
     1357
     1358                        }
     1359                }
     1360        } //}}}
    11881361
    11891362}
     
    19562129
    19572130        _error_("Could not find constant \""<<constant_name <<"\"");
    1958 }
    1959 /*}}}*/
    1960 void  IoModel::LastIndex(int *pindex){/*{{{*/
    1961 
    1962         int my_rank;
    1963         int lastindex,index;
    1964         int record_length;
    1965 
    1966         /*recover my_rank:*/
    1967         my_rank=IssmComm::GetRank();
    1968 
    1969         /*Go find in the binary file, the position of the data we want to fetch: */
    1970         if(my_rank==0){
    1971 
    1972                 /*First set FILE* position to the beginning of the file: */
    1973                 fseek(fid,0,SEEK_SET);
    1974 
    1975                 /*Now march through file looking for the correct data identifier: */
    1976                 for(;;){
    1977                         /*Read name for this size of first string name: */
    1978                         if(fread(&index,sizeof(int),1,fid)==0){
    1979                                 /*Ok, we have reached the end of the file. break: */
    1980                                 break;
    1981                         }
    1982 
    1983                         /*read the record length, and use it to skip this record: */
    1984                         if(fread(&record_length,sizeof(int),1,fid)!=1) _error_("Could not read record_length");
    1985                         fseek(fid,record_length,SEEK_CUR);
    1986                         lastindex=index;
    1987                 }
    1988         }
    1989         /*Broadcast code and vector type: */
    1990         ISSM_MPI_Bcast(&lastindex,1,ISSM_MPI_INT,0,IssmComm::GetComm());
    1991 
    1992         /*Assign output pointers:*/
    1993         *pindex=lastindex;
    19942131}
    19952132/*}}}*/
  • issm/trunk-jpl/src/c/classes/IoModel.h

    r20810 r20840  
    124124                void        FetchData(IssmDouble**  pscalarmatrix,int* pM,int* pN,const char* data_name);
    125125                void        FetchData(IssmDouble*** pmatrixarray,int** pmdims,int** pndims, int* pnumrecords,const char* data_name);
    126                 void        FetchData(Option **poption,int index);
     126                void        FetchData(Options *options,const char* data_name);
    127127                void        FetchData(int num,...);
    128128                void        FetchDataToInput(Elements* elements,const char* vector_name,int input_enum);
     
    134134                void        FetchMultipleData(int** pvector, int* pnum_instances,const char* data_name);
    135135                void        FetchMultipleData(IssmDouble** pvector, int* pnum_instances,const char* data_name);
    136                 void        LastIndex(int *pindex);
    137136                fpos_t*     SetFilePointersToData(int** pcodes,int** pvector_types, int* pnum_instances, const char* data_name);
    138137                FILE*       SetFilePointerToData(int* pcode,int* pvector_type, const char* data_name);
  • issm/trunk-jpl/src/c/main/kriging.cpp

    r20690 r20840  
    132132void ProcessInputfile(IssmDouble **px,IssmDouble **py,IssmDouble **pdata,int *pnobs,IssmDouble **px_interp,IssmDouble **py_interp,int *pninterp,Options **poptions,FILE* fid){
    133133
    134         int      ninterp,nobs,numoptions;
    135         IssmDouble  *x        = NULL;
    136         IssmDouble  *y        = NULL;
    137         IssmDouble  *data     = NULL;
    138         IssmDouble  *x_interp = NULL;
    139         IssmDouble  *y_interp = NULL;
    140         Options *options  = NULL;
    141         Option  *option   = NULL;
     134        int         ninterp    ,nobs;
     135        IssmDouble *x        = NULL;
     136        IssmDouble *y        = NULL;
     137        IssmDouble *data     = NULL;
     138        IssmDouble *x_interp = NULL;
     139        IssmDouble *y_interp = NULL;
     140        Options    *options  = NULL;
    142141
    143142        int      M,N;
     
    145144        iomodel->fid=fid;
    146145        iomodel->CheckFile();
    147         iomodel->FetchData(&x,&M,&N,"md.0");        nobs=M*N;
    148         iomodel->FetchData(&y,&M,&N,"md.1");        _assert_(M*N==nobs);
    149         iomodel->FetchData(&data,&M,&N,"md.2");     _assert_(M*N==nobs);
    150         iomodel->FetchData(&x_interp,&M,&N,"md.3"); ninterp=M*N;
    151         iomodel->FetchData(&y_interp,&M,&N,"md.4"); _assert_(M*N==ninterp);
     146        iomodel->FetchData(&x,&M,&N,"md.x");        nobs=M*N;
     147        iomodel->FetchData(&y,&M,&N,"md.y");        _assert_(M*N==nobs);
     148        iomodel->FetchData(&data,&M,&N,"md.data");     _assert_(M*N==nobs);
     149        iomodel->FetchData(&x_interp,&M,&N,"md.x_interp"); ninterp=M*N;
     150        iomodel->FetchData(&y_interp,&M,&N,"md.y_interp"); _assert_(M*N==ninterp);
    152151
    153152        /*Read options*/
    154153        options = new Options();
    155         iomodel->LastIndex(&N);
    156         numoptions=(int)((N-4)/2);
    157         for(int i=0;i<numoptions;i++){
    158                 iomodel->FetchData(&option,5+2*i);
    159                 options->AddOption(option);
    160                 option=NULL;
    161         }
     154        iomodel->FetchData(options,"md.y_interp");
    162155
    163156        /*Assign output pointer*/
  • issm/trunk-jpl/src/m/classes/pairoptions.m

    r20690 r20840  
    284284                        end
    285285                end % }}}
    286                 function marshall(self,fid,firstindex)% {{{
    287 
    288                         error('needs to be revised with new marhsall format...');
     286                function marshall(self,fid)% {{{
     287
    289288                        for i=1:size(self.list,1),
    290289                                name  = self.list{i,1};
    291290                                value = self.list{i,2};
    292291
    293                                 %Write option name
    294                                 WriteData(fid,prefix,'enum',(firstindex-1)+2*i-1,'data',name,'format','String');
    295 
    296292                                %Write option value
    297293                                if (isnumeric(value) & numel(value)==1),
    298                                         WriteData(fid,prefix,'enum',(firstindex-1)+2*i,'data',value,'format','Double');
     294                                        WriteData(fid,'','name',['md.' name],'data',value,'format','Double');
    299295                                elseif ischar(value),
    300                                         WriteData(fid,prefix,'enum',(firstindex-1)+2*i,'data',value,'format','String');
     296                                        WriteData(fid,'','name',['md.' name],'data',value,'format','String');
    301297                                else
    302298                                        error(['Cannot marshall option ' name ': format not supported yet']);
  • issm/trunk-jpl/src/m/contrib/morlighem/gslib/pkriging.m

    r20446 r20840  
    1818end
    1919
    20 %First, write MaximumNumberOfDefinitionsEnum to make sure that the Enums are synchronized
    21 WriteData(fid,'enum',MaximumNumberOfDefinitionsEnum(),'data',true,'format','Boolean');
    22 
    2320%Write all data
    24 WriteData(fid,'enum',0,'data',x,'format','DoubleMat');
    25 WriteData(fid,'enum',1,'data',y,'format','DoubleMat');
    26 WriteData(fid,'enum',2,'data',observations,'format','DoubleMat');
    27 WriteData(fid,'enum',3,'data',x_interp,'format','DoubleMat');
    28 WriteData(fid,'enum',4,'data',y_interp,'format','DoubleMat');
    29 
    30 %Last, write MaximumNumberOfEnum+1 to make sure that the binary file is not corrupt
    31 WriteData(fid,'enum',MaximumNumberOfDefinitionsEnum()+1,'data',true,'format','Boolean');
     21WriteData(fid,'','name','md.x','data',x,'format','DoubleMat');
     22WriteData(fid,'','name','md.y','data',y,'format','DoubleMat');
     23WriteData(fid,'','name','md.data','data',observations,'format','DoubleMat');
     24WriteData(fid,'','name','md.x_interp','data',x_interp,'format','DoubleMat');
     25WriteData(fid,'','name','md.y_interp','data',y_interp,'format','DoubleMat');
    3226
    3327%Now, write number of options
    34 options.marshall(fid,5);
    35 st=fclose(fid);
    36 if st==-1,
    37         error(['marshall error message: could not close file ' name '.bin']);
    38 end
    39 % =========================================   MARSHALL.m =================================================
     28options.marshall(fid);
     29
     30%Last, write "md.EOF" to make sure that the binary file is not corrupt
     31WriteData(fid,'','name','md.EOF','data',true,'format','Boolean');
    4032
    4133%Launch job on remote cluster
Note: See TracChangeset for help on using the changeset viewer.