Changeset 25509


Ignore:
Timestamp:
09/01/20 15:18:50 (5 years ago)
Author:
Mathieu Morlighem
Message:

CHG: preparing ground for AD

File:
1 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/c/shared/io/Marshalling/Marshalling.h

    r25507 r25509  
    1616        MARSHALLING_LOAD,
    1717        MARSHALLING_SIZE,
     18#ifdef _HAVE_CODIPACK_
     19        AD_COUNTDOUBLES,
     20        AD_REGISTERINPUT,
     21        AD_SETADJOINT,
     22#endif
    1823};
    1924
    2025/*Define virtual Marshall Handle*/
    21 class MarshallHandle{
     26class MarshallHandle{ /*{{{*/
    2227        public:
    2328                MarshallOpEnum operation_enum;
     
    2833                template<typename T> void call(T  & value);
    2934                template<typename T> void call(T* & value,int size);
    30 };
    31 
    32 /*Make sure to pass all fields by reference!*/
    33 class WriteCheckpointFunctor: public MarshallHandle{
    34         public:
     35}; /*}}}*/
     36/* !! Make sure to pass all fields by reference !! */
     37class WriteCheckpointFunctor: public MarshallHandle{ /*{{{*/
     38
     39        private:
    3540                char** pmarshalled_data;
     41
     42        public:
    3643                WriteCheckpointFunctor(char** pmarshalled_data_in) : MarshallHandle(MARSHALLING_WRITE),pmarshalled_data(pmarshalled_data_in){}
    3744                template<typename T> void call(T & value){
     
    5259                        }
    5360                }
    54 };
    55 
    56 class LoadCheckpointFunctor: public MarshallHandle{
    57         public:
     61};/*}}}*/
     62class LoadCheckpointFunctor:  public MarshallHandle{ /*{{{*/
     63
     64        private:
    5865                char** pmarshalled_data;
     66
     67        public:
    5968                LoadCheckpointFunctor(char** pmarshalled_data_in) : MarshallHandle(MARSHALLING_LOAD),pmarshalled_data(pmarshalled_data_in){}
    6069                void Echo(void){
     
    7887                        }
    7988                }
    80 };
    81 
    82 class SizeCheckpointFunctor: public MarshallHandle{
    83         public:
     89};/*}}}*/
     90class SizeCheckpointFunctor:  public MarshallHandle{ /*{{{*/
     91
     92        private:
    8493                int marshalled_data_size;
     94
     95        public:
    8596                SizeCheckpointFunctor(void) : MarshallHandle(MARSHALLING_SIZE),marshalled_data_size(0){}
    8697                int MarshalledSize(void){return this->marshalled_data_size;};
     
    100111                        }
    101112                }
    102 };
     113};/*}}}*/
     114#ifdef _HAVE_CODIPACK_
     115class CountDoublesFunctor:    public MarshallHandle{ /*{{{*/
     116
     117        private:
     118                int double_count;
     119
     120        public:
     121                CountDoublesFunctor(void) : MarshallHandle(AD_COUNTDOUBLES),double_count(0){}
     122                int DoubleCount(void){return this->double_count;};
     123                void Echo(void){
     124                        printf("CountDoublesFunctor Echo:\n");
     125                        printf("   double_count: %i\n",double_count);
     126                }
     127                template<typename T> void call(T & value){
     128                        /*General case: do nothing*/
     129                }
     130                template<typename T> void call(T* & value,int size){
     131                        /*General case: do nothing*/
     132                }
     133                void call(IssmDouble value){
     134                        this->double_count++;
     135                }
     136                void call(IssmDouble* value,int size){
     137                        if(value) this->double_count+= size;
     138                }
     139}; /*}}}*/
     140class RegisterInputFunctor:   public MarshallHandle{ /*{{{*/
     141
     142        private:
     143                int   double_count;
     144                int*  identifiers;
     145                auto* tape_codi;
     146
     147        public:
     148                RegisterInputFunctor(auto* tape_in,int* identifiers_in) : MarshallHandle(AD_REGISTERINPUT),double_count(0),identifiers(identifiers_in),tape_codi(tape_in)){}
     149                int DoubleCount(void){return this->double_count;};
     150                void Echo(void){
     151                        printf("RegisterInputFunctor Echo:\n");
     152                        printf("   double_count: %i\n",double_count);
     153                }
     154                template<typename T> void call(T & value){
     155                        /*General case: do nothing*/
     156                }
     157                template<typename T> void call(T* & value,int size){
     158                        /*General case: do nothing*/
     159                }
     160                void call(IssmDouble value){
     161                        this->tape_codi->registerInput(value);
     162                        this->identifiers[this->double_count] = value.getGradientData();
     163                        this->double_count++;
     164                }
     165                void call(IssmDouble* value,int size){
     166                        if(value){
     167                                for(int i=0;i<size;i++){
     168                                        this->tape_codi->registerInput(value[i]);
     169                                        this->identifiers[this->double_count] = value[i].getGradientData();
     170                                        this->double_count++;
     171                                }
     172                        }
     173                }
     174}; /*}}}*/
     175#endif
    103176
    104177template<typename T> void MarshallHandle::call(T & value){
     
    107180                case MARSHALLING_LOAD: {LoadCheckpointFunctor*  temp = xDynamicCast<LoadCheckpointFunctor*>(this);  temp->call(value); break;}
    108181                case MARSHALLING_SIZE: {SizeCheckpointFunctor*  temp = xDynamicCast<SizeCheckpointFunctor*>(this);  temp->call(value); break;}
    109                 default: _error_("not supported yet");
     182#ifdef _HAVE_CODIPACK_
     183                case AD_COUNTDOUBLES:  {CountDoublesFunctor*    temp = xDynamicCast<CountDoublesFunctor*>(this);    temp->call(value); break;}
     184                case AD_REGISTERINPUT: {RegisterInputFunctor*   temp = xDynamicCast<RegisterInputFunctor*>(this);   temp->call(value); break;}
     185                //case AD_SETADJOINT:    {SetAdjointFunction*     temp = xDynamicCast<SetAdjointFunction*>(this);     temp->call(value); break;}
     186#endif
     187                default: _error_("Operation "<<OperationNumber()<<" not supported yet");
    110188        }
    111189}
     
    115193                case MARSHALLING_LOAD: {LoadCheckpointFunctor*  temp = xDynamicCast<LoadCheckpointFunctor*>(this);  temp->call(value,size); break;}
    116194                case MARSHALLING_SIZE: {SizeCheckpointFunctor*  temp = xDynamicCast<SizeCheckpointFunctor*>(this);  temp->call(value,size); break;}
    117                 default: _error_("not supported yet");
     195#ifdef _HAVE_CODIPACK_
     196                case AD_COUNTDOUBLES:  {CountDoublesFunctor*    temp = xDynamicCast<CountDoublesFunctor*>(this);    temp->call(value,size); break;}
     197                case AD_REGISTERINPUT: {RegisterInputFunctor*   temp = xDynamicCast<RegisterInputFunctor*>(this);   temp->call(value,size); break;}
     198                //case AD_SETADJOINT:    {SetAdjointFunction*     temp = xDynamicCast<SetAdjointFunction*>(this);     temp->call(value,size); break;}
     199#endif
     200                default: _error_("Operation "<<OperationNumber() <<" not supported yet");
    118201        }
    119202}
Note: See TracChangeset for help on using the changeset viewer.