Changeset 19632


Ignore:
Timestamp:
10/13/15 15:56:00 (9 years ago)
Author:
Eric.Larour
Message:

CHG: committing temporary adaptation fixes for Dakota 6.2, and embryo of a new issm_dakota main. Much debugging needed. Cannot
do it on Macosx, need to transfer to Linux platform.

Location:
issm/trunk-jpl/src/c
Files:
4 added
2 deleted
3 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/c/Makefile.am

    r19554 r19632  
    263263#DAKOTA sources  {{{
    264264if DAKOTA
    265 issm_sources +=  ./modules/InputUpdateFromDakotax/InputUpdateFromDakotax.cpp\
     265issm_sources +=       ./classes/Dakota/IssmDirectApplicInterface.h\
     266                                          ./classes/Dakota/IssmParallelDirectApplicInterface.h\
     267                                          ./modules/InputUpdateFromDakotax/InputUpdateFromDakotax.cpp\
    266268                                          ./modules/InputUpdateFromVectorDakotax/InputUpdateFromVectorDakotax.cpp\
    267269                                          ./modules/InputUpdateFromMatrixDakotax/InputUpdateFromMatrixDakotax.cpp\
     
    269271                                          ./modules/ModelProcessorx/Dakota/CreateParametersDakota.cpp\
    270272                                          ./modules/ModelProcessorx/Dakota/UpdateElementsAndMaterialsDakota.cpp\
    271                                           ./cores/dakota_core.cpp\
    272                                           ./cores/DakotaSpawnCore.cpp
     273                                          ./cores/dakota_core.cpp
    273274endif
    274275#}}}
     
    659660kriging_CXXFLAGS= -fPIC $(CXXFLAGS) $(CXXOPTFLAGS) $(COPTFLAGS)
    660661endif
     662
     663if DAKOTA
     664bin_PROGRAMS += issm_dakota
     665issm_dakota_SOURCES = main/issm_dakota.cpp
     666issm_dakota_CXXFLAGS= -fPIC $(CXXFLAGS) $(CXXOPTFLAGS) $(COPTFLAGS)
     667issm_dakota_LDADD= $(LDADD)
     668endif
     669
    661670#}}}
    662671#Automatic differentiation (must be done at the end) {{{
  • issm/trunk-jpl/src/c/classes/classes.h

    r19554 r19632  
    113113#include "./kriging/krigingobjects.h"
    114114
     115/*dakota:*/
     116#include "./Dakota/IssmDirectApplicInterface.h"
     117#include "./Dakota/IssmParallelDirectApplicInterface.h"
     118
    115119/*diverse: */
    116120#include "./Hook.h"
  • issm/trunk-jpl/src/c/cores/dakota_core.cpp

    r16518 r19632  
    2727 */
    2828
    29 /*include files: {{{*/
     29 /* \brief: run core ISSM solution using Dakota inputs coming from CPU 0.
     30 * \sa qmu.cpp DakotaPlugin.cpp
     31 *
     32 * This routine needs to be understood simultaneously with qmu.cpp and DakotaPlugin.
     33 * DakotaSpawnCoreParallel is called by all CPUS, with CPU 0 holding Dakota variable values, along
     34 * with variable descriptors.
     35 *
     36 * DakotaSpawnCoreParallel takes care of broadcasting the variables and their descriptors across the MPI
     37 * ring. Once this is done, we use the variables to modify the inputs for the solution core.
     38 * For ex, if "rho_ice" is provided, for ex 920, we include "rho_ice" in the inputs, then
     39 * call the core with the modified inputs. This is the way we get Dakota to explore the parameter
     40 * spce of the core.
     41 *
     42 * Once the core is called, we process the results of the core, and using the processed results,
     43 * we compute response functions. The responses are computed on all CPUS, but they are targeted
     44 * for CPU 0, which will get these values back to the Dakota engine.
     45 *
     46 */
     47
     48/*include config: {{{*/
    3049#ifdef HAVE_CONFIG_H
    3150#include <config.h>
     
    3352#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
    3453#endif
    35 
     54/*}}}*/
     55
     56/*include ISSM files: */
    3657#include "./cores.h"
    3758#include "../toolkits/toolkits.h"
     
    3960#include "../classes/classes.h"
    4061
    41 #ifdef _HAVE_DAKOTA_ //only works if dakota library has been compiled in.
    42 #if _DAKOTA_MAJOR_ < 5 || (_DAKOTA_MAJOR_ == 5 && _DAKOTA_MINOR_ < 3)
     62#if defined(_HAVE_DAKOTA_) && (_DAKOTA_MAJOR_ < 5 || (_DAKOTA_MAJOR_ == 5 && _DAKOTA_MINOR_ < 3))
     63
     64/*include dakota files: */
    4365#include <ParallelLibrary.H>
    4466#include <ProblemDescDB.H>
     
    4668#include <DakotaModel.H>
    4769#include <DakotaInterface.H>
    48 #else
    49 #include <ParallelLibrary.hpp>
    50 #include <ProblemDescDB.hpp>
    51 #include <DakotaStrategy.hpp>
    52 #include <DakotaModel.hpp>
    53 #include <DakotaInterface.hpp>
    54 #endif
    55 #include "./DakotaSpawnCore.h"
    56 #endif
     70int  DakotaSpawnCore(double* d_responses, int d_numresponses, double* d_variables, char** d_variables_descriptors,int d_numvariables, void* void_femmodel,int counter){ /*{{{*/
     71
     72        /*Notice the d_, which prefixes anything that is being provided to us by the Dakota pluggin. Careful. some things are ours, some are dakotas!: */
     73
     74        char     **responses_descriptors    = NULL;      //these are our! there are only numresponsedescriptors of them, not d_numresponses!!!
     75        int        numresponsedescriptors;
     76        int        solution_type;
     77        bool       control_analysis         = false;
     78        void     (*solutioncore)(FemModel*) = NULL;
     79        FemModel  *femmodel                 = NULL;
     80        bool       nodakotacore             = true;
     81
     82        /*If counter==-1 on cpu0, it means that the dakota runs are done. In which case, bail out and return 0: */
     83        ISSM_MPI_Bcast(&counter,1,ISSM_MPI_INT,0,IssmComm::GetComm());
     84        if(counter==-1)return 0;
     85
     86        /*cast void_femmodel to FemModel, and at the same time, make a copy, so we start this new core run for this specific sample
     87         *with a brand new copy of the model, which has not been tempered with by previous dakota runs: */
     88        femmodel=(reinterpret_cast<FemModel*>(void_femmodel))->copy();
     89
     90        /*retrieve parameters: */
     91        femmodel->parameters->FindParam(&responses_descriptors,&numresponsedescriptors,QmuResponsedescriptorsEnum);
     92        femmodel->parameters->FindParam(&solution_type,SolutionTypeEnum);
     93        femmodel->parameters->FindParam(&control_analysis,InversionIscontrolEnum);
     94
     95        if(VerboseQmu()) _printf0_("qmu iteration: " << counter << "\n");
     96
     97        /* only cpu 0, running dakota is providing us with variables and variables_descriptors and numresponses: broadcast onto other cpus: */
     98        DakotaMPI_Bcast(&d_variables,&d_variables_descriptors,&d_numvariables,&d_numresponses);
     99
     100        /*Modify core inputs in objects contained in femmodel, to reflect the dakota variables inputs: */
     101        InputUpdateFromDakotax(femmodel,d_variables,d_variables_descriptors,d_numvariables);
     102
     103        /*Determine solution sequence: */
     104        if(VerboseQmu()) _printf0_("Starting " << EnumToStringx(solution_type) << " core:\n");
     105        WrapperCorePointerFromSolutionEnum(&solutioncore,femmodel->parameters,solution_type,nodakotacore);
     106
     107        /*Run the core solution sequence: */
     108        solutioncore(femmodel);
     109
     110        /*compute responses: */
     111        if(VerboseQmu()) _printf0_("compute dakota responses:\n");
     112        femmodel->DakotaResponsesx(d_responses,responses_descriptors,numresponsedescriptors,d_numresponses);
     113
     114        /*Free ressources:*/
     115        DakotaFree(&d_variables,&d_variables_descriptors,&responses_descriptors, d_numvariables, numresponsedescriptors);
     116
     117        /*Avoid leaks here: */
     118        delete femmodel;
     119
     120        return 1; //this is critical! do not return 0, otherwise, dakota_core will stop running!
     121}
    57122/*}}}*/
    58 /*DakotaPlugin class {{{*/
    59 #ifdef _HAVE_DAKOTA_ //only works if dakota library has been compiled in.
    60 #if _DAKOTA_MAJOR_ < 5 || (_DAKOTA_MAJOR_ == 5 && _DAKOTA_MINOR_ < 3)
    61 #include <DirectApplicInterface.H>
    62 #include <DakotaResponse.H>
    63 #include <ParamResponsePair.H>
    64 #include <system_defs.h>
    65 #include <ProblemDescDB.H>
    66 #include <ParallelLibrary.H>
    67 #else
    68 #include <DirectApplicInterface.hpp>
    69 #include <DakotaResponse.hpp>
    70 #include <ParamResponsePair.hpp>
    71 #include <ProblemDescDB.hpp>
    72 #include <ParallelLibrary.hpp>
    73 #endif
    74 namespace SIM {
    75         class DakotaPlugin: public Dakota::DirectApplicInterface{
    76                 public:
    77                         /*these fields are used by core solutions: */
    78                         void *femmodel;
    79                         int   counter;
    80                         /*Constructors/Destructors*/
    81                         DakotaPlugin(const Dakota::ProblemDescDB& problem_db,void* in_femmodel):Dakota::DirectApplicInterface(problem_db){/*{{{*/
    82                                 femmodel = in_femmodel;
    83                                 counter  = 0;
    84                         }/*}}}*/
    85                         ~DakotaPlugin(){/*{{{*/
    86                                 /* Virtual destructor handles referenceCount at Interface level. */
    87                         }/*}}}*/
    88                 protected:
    89                         /*execute the input filter portion of a direct evaluation invocation*/
    90                         //int derived_map_if(const Dakota::String& if_name);
    91                         /*execute an analysis code portion of a direct evaluation invocation*/
    92                         int derived_map_ac(const Dakota::String& driver){/*{{{*/
    93 
    94                                 int i;
    95                                 IssmDouble* variables=NULL;
    96                                 char** variable_descriptors=NULL;
    97                                 char*  variable_descriptor=NULL;
    98                                 IssmDouble* responses=NULL;
    99 
    100                                 /*increae counter: */
    101                                 counter++;
    102 
    103                                 /*Before launching analysis, we need to transfer the dakota inputs into Issm
    104                                  *readable variables: */
    105 
    106                                 /*First, the variables: */
    107                                 variables=xNew<IssmDouble>(numACV);
    108                                 for(i=0;i<numACV;i++){
    109                                         variables[i]=xC[i];
    110                                 }
    111                                 /*The descriptors: */
    112                                 variable_descriptors=xNew<char*>(numACV);
    113                                 for(i=0;i<numACV;i++){
    114                                         std::string label=xCLabels[i];
    115                                         variable_descriptor=xNew<char>(strlen(label.c_str())+1);
    116                                         memcpy(variable_descriptor,label.c_str(),(strlen(label.c_str())+1)*sizeof(char));
    117 
    118                                         variable_descriptors[i]=variable_descriptor;
    119                                 }
    120 
    121                                 /*Initialize responses: */
    122                                 responses=xNewZeroInit<IssmDouble>(numFns);
    123 
    124                                 /*run core solution: */
    125                                 DakotaSpawnCore(responses,numFns, variables,variable_descriptors,numACV,femmodel,counter);
    126 
    127                                 /*populate responses: */
    128                                 for(i=0;i<numFns;i++){
    129                                         fnVals[i]=responses[i];
    130                                 }
    131 
    132                                 /*Free ressources:*/
    133                                 xDelete<IssmDouble>(variables);
    134                                 for(i=0;i<numACV;i++){
    135                                         variable_descriptor=variable_descriptors[i];
    136                                         xDelete<char>(variable_descriptor);
    137                                 }
    138                                 xDelete<char*>(variable_descriptors);
    139                                 xDelete<IssmDouble>(responses);
    140 
    141                                 return 0;
    142                         }/*}}}*/
    143                         /*execute the output filter portion of a direct evaluation invocation*/
    144                         //int derived_map_of(const Dakota::String& of_name);
    145                         /*add for issm: */
    146                         int GetCounter(){/*{{{*/
    147                                 return counter;
    148                         }/*}}}*/
    149                 private:
    150         };
    151 }
    152 #endif
    153 /*}}}*/
    154 
    155 void dakota_core(FemModel* femmodel){
    156 
    157         #ifdef _HAVE_DAKOTA_ //only works if dakota library has been compiled in.
     123void DakotaMPI_Bcast(double** pvariables, char*** pvariables_descriptors,int* pnumvariables, int* pnumresponses){ /*{{{*/
     124
     125        /* * \brief: broadcast variables_descriptors, variables, numvariables and numresponses
     126         * from cpu 0 to all other cpus.
     127         */
     128
     129        int i;
     130        int my_rank;
     131
     132        /*inputs and outputs: */
     133        double* variables=NULL;
     134        char**  variables_descriptors=NULL;
     135        int     numvariables;
     136        int     numresponses;
     137
     138        /*intermediary: */
     139        char* string=NULL;
     140        int   string_length;
     141
     142        /*recover my_rank:*/
     143        my_rank=IssmComm::GetRank();
     144
     145        /*recover inputs from pointers: */
     146        variables=*pvariables;
     147        variables_descriptors=*pvariables_descriptors;
     148        numvariables=*pnumvariables;
     149        numresponses=*pnumresponses;
     150
     151        /*numvariables: */
     152        ISSM_MPI_Bcast(&numvariables,1,ISSM_MPI_INT,0,IssmComm::GetComm());
     153
     154        /*variables:*/
     155        if(my_rank!=0)variables=xNew<double>(numvariables);
     156        ISSM_MPI_Bcast(variables,numvariables,MPI_DOUBLE,0,IssmComm::GetComm());
     157
     158        /*variables_descriptors: */
     159        if(my_rank!=0){
     160                variables_descriptors=xNew<char*>(numvariables);
     161        }
     162        for(i=0;i<numvariables;i++){
     163                if(my_rank==0){
     164                        string=variables_descriptors[i];
     165                        string_length=(strlen(string)+1)*sizeof(char);
     166                }
     167                ISSM_MPI_Bcast(&string_length,1,ISSM_MPI_INT,0,IssmComm::GetComm());
     168                if(my_rank!=0)string=xNew<char>(string_length);
     169                ISSM_MPI_Bcast(string,string_length,ISSM_MPI_CHAR,0,IssmComm::GetComm());
     170                if(my_rank!=0)variables_descriptors[i]=string;
     171        }
     172
     173        /*numresponses: */
     174        ISSM_MPI_Bcast(&numresponses,1,ISSM_MPI_INT,0,IssmComm::GetComm());
     175
     176        /*Assign output pointers:*/
     177        *pnumvariables=numvariables;
     178        *pvariables=variables;
     179        *pvariables_descriptors=variables_descriptors;
     180        *pnumresponses=numresponses;
     181} /*}}}*/
     182void DakotaFree(double** pvariables,char*** pvariables_descriptors,char*** presponses_descriptors,int numvariables,int numresponses){ /*{{{*/
     183
     184        /*\brief DakotaFree: free allocations on other cpus, not done by Dakota.*/
     185
     186        int i;
     187        int my_rank;
     188
     189        double  *variables             = NULL;
     190        char   **variables_descriptors = NULL;
     191        char   **responses_descriptors = NULL;
     192        char    *string                = NULL;
     193
     194        /*recover pointers: */
     195        variables=*pvariables;
     196        variables_descriptors=*pvariables_descriptors;
     197        responses_descriptors=*presponses_descriptors;
     198
     199        /*recover my_rank:*/
     200        my_rank=IssmComm::GetRank();
     201
     202        /*Free variables and variables_descriptors only on cpu !=0*/
     203        if(my_rank!=0){
     204                xDelete<double>(variables);
     205                for(i=0;i<numvariables;i++){
     206                        string=variables_descriptors[i];
     207                        xDelete<char>(string);
     208                }
     209                xDelete<char*>(variables_descriptors);
     210        }
     211
     212        //responses descriptors on every cpu
     213        for(i=0;i<numresponses;i++){
     214                string=responses_descriptors[i];
     215                xDelete<char>(string);
     216        }
     217        //rest of dynamic allocations.
     218        xDelete<char*>(responses_descriptors);
     219
     220        /*Assign output pointers:*/
     221        *pvariables=variables;
     222        *pvariables_descriptors=variables_descriptors;
     223        *presponses_descriptors=responses_descriptors;
     224} /*}}}*/
     225void dakota_core(FemModel* femmodel){  /*{{{*/
     226
    158227
    159228        int                my_rank;
     
    227296        xDelete<char>(dakota_output_file);
    228297
    229         #endif //#ifdef _HAVE_DAKOTA_
     298} /*}}}*/
     299#else
     300void dakota_core(FemModel* femmodel){
     301        /*do nothing*/
    230302}
     303#endif
Note: See TracChangeset for help on using the changeset viewer.