Changeset 27732


Ignore:
Timestamp:
05/08/23 07:00:31 (23 months ago)
Author:
Mathieu Morlighem
Message:

CHG: better way to register output J, NR will fail and new archives will be saved

Location:
issm/trunk-jpl/src/c/cores
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/c/cores/controladm1qn3_core.cpp

    r27728 r27732  
    208208        femmodel->parameters->FindParam(&solution_type,SolutionTypeEnum);
    209209
    210         /*In transient, we need to make sure we do not modify femmodel at each iteration, make a copy*/
    211         if(solution_type == TransientSolutionEnum) femmodel = input_struct->femmodel->copy();
     210        /*we need to make sure we do not modify femmodel at each iteration, make a copy*/
     211        femmodel = input_struct->femmodel->copy();
    212212
    213213        IssmPDouble*  Jlist  = input_struct->Jlist;
     
    313313
    314314                /*Get Dependents*/
    315                 IssmDouble   output_value;
    316315                int          num_dependents;
    317316                IssmPDouble *dependents;
     
    333332                        dep->RecordResponsex(femmodel);
    334333                        IssmDouble output_value = dep->GetValue();
    335 
    336                         #if defined(_HAVE_CODIPACK_)
    337                         tape_codi.registerOutput(output_value);
    338334                        dependents[i] = output_value.getValue();
    339                         #if _CODIPACK_MAJOR_==2
    340                         codi_global.output_indices.push_back(output_value.getIdentifier());
    341                         #elif _CODIPACK_MAJOR_==1
    342                         codi_global.output_indices.push_back(output_value.getGradientData());
    343                         #else
    344                         #error "_CODIPACK_MAJOR_ not supported"
     335                        #if defined(_HAVE_ADOLC_)
     336                        output_value>>=dependents[i];
    345337                        #endif
    346338
    347                         #elif defined(_HAVE_ADOLC_)
    348                         output_value>>=dependents[i];
    349 
    350                         #else
    351                         _error_("not suppoted");
    352                         #endif
    353 
    354339                        J+=output_value;
    355340                }
     341
     342                #if defined(_HAVE_CODIPACK_)
     343                tape_codi.registerOutput(J);
     344                #if _CODIPACK_MAJOR_==2
     345                codi_global.output_indices.push_back(J.getIdentifier());
     346                #elif _CODIPACK_MAJOR_==1
     347                codi_global.output_indices.push_back(J.getGradientData());
     348                #else
     349                #error "_CODIPACK_MAJOR_ not supported"
     350                #endif
     351                #endif
    356352
    357353                /*Turning off trace tape*/
     
    423419                /*Get gradient for CoDiPack{{{*/
    424420                if(VerboseAutodiff())_printf0_("   CoDiPack fos_reverse\n");
    425 
    426                 /* call the fos_reverse in a loop on the index, from 0 to num_dependents, so
    427                  * as to generate num_dependents gradients: */
    428                 for(int dep_index=0;dep_index<num_dependents_old;dep_index++){
    429 
    430                         /*initialize direction index in the weights vector: */
    431                         if(my_rank==0){
    432                                 if(dep_index<0 || dep_index>=num_dependents || codi_global.output_indices.size() <= dep_index){
    433                                         _error_("index value for dependent index should be in [0,num_dependents-1]");
    434                                 }
    435                                 tape_codi.setGradient(codi_global.output_indices[dep_index],1.0);
    436                         }
    437                         //feclearexcept(FE_ALL_EXCEPT);
    438                         //feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
    439                         tape_codi.evaluate();
    440 
    441                         /*Get gradient for this dependent */
    442                         weightVectorTimesJac=xNew<IssmPDouble>(num_independents);
    443                         auto in_size = codi_global.input_indices.size();
    444                         for(size_t i = 0; i < in_size; ++i){
    445                                 _assert_(i<num_independents);
    446                                 weightVectorTimesJac[i] = tape_codi.getGradient(codi_global.input_indices[i]);
    447                         }
    448                         if(my_rank==0) for(int i=0;i<num_independents;i++){
    449                                 totalgradient[i]+=weightVectorTimesJac[i];
    450                         }
    451                         xDelete(weightVectorTimesJac);
     421                if(my_rank==0) tape_codi.setGradient(codi_global.output_indices[0],1.0);
     422                tape_codi.evaluate();
     423
     424                auto in_size = codi_global.input_indices.size();
     425                for(size_t i = 0; i < in_size; ++i){
     426                        _assert_(i<num_independents);
     427                        totalgradient[i] = tape_codi.getGradient(codi_global.input_indices[i]);
    452428                }
    453429
     
    520496
    521497        /*Clean-up and return*/
    522         if(solution_type == TransientSolutionEnum) delete femmodel;
     498        delete femmodel;
    523499        *Jlisti = (*Jlisti) +1;
    524500        xDelete<double>(XU);
  • issm/trunk-jpl/src/c/cores/transient_core.cpp

    r27728 r27732  
    399399                J += output_value;
    400400
    401                 tape_codi.registerOutput(J);
    402                 #if _CODIPACK_MAJOR_==2
    403                 codi_global.output_indices.push_back(J.getIdentifier());
    404                 #elif _CODIPACK_MAJOR_==1
    405                 codi_global.output_indices.push_back(J.getGradientData());
    406                 #else
    407                 #error "_CODIPACK_MAJOR_ not supported"
    408                 #endif
    409 
    410401                /*Keep track of output for printing*/
    411402                Jlist[count] = output_value.getValue();
     
    415406        _assert_(count == num_responses);
    416407
     408        #if defined(_HAVE_CODIPACK_)
     409        tape_codi.registerOutput(J);
     410        #if _CODIPACK_MAJOR_==2
     411        codi_global.output_indices.push_back(J.getIdentifier());
     412        #elif _CODIPACK_MAJOR_==1
     413        codi_global.output_indices.push_back(J.getGradientData());
     414        #else
     415        #error "_CODIPACK_MAJOR_ not supported"
     416        #endif
     417        #endif
     418
    417419        tape_codi.setPassive();
    418420
    419421        if(VerboseAutodiff())_printf0_("   CoDiPack fos_reverse\n");
    420         for(int i=0;i<num_responses;i++){
    421                 if(my_rank==0) tape_codi.setGradient(codi_global.output_indices[i],1.0);
    422                 tape_codi.evaluate();
    423         }
     422        if(my_rank==0) tape_codi.setGradient(codi_global.output_indices[0],1.0);
     423        tape_codi.evaluate();
    424424
    425425        /*Initialize Xb and Yb*/
Note: See TracChangeset for help on using the changeset viewer.