1. Summary
  2. Files
  3. Support
  4. Report Spam
  5. Create account
  6. Log in

Changeset 3100

Show
Ignore:
Timestamp:
04/19/12 06:22:02 (13 months ago)
Author:
mhauer
Message:

Added deferred initialization. Rank-based initialization remains unimplemented.
Added a simple test for deferred initialization.

Location:
trunk/core
Files:
2 added
5 modified

Legend:

Unmodified
Added
Removed
  • trunk/core/exec.c

    r3098 r3100  
    492492} 
    493493 
     494static int init_by_deferral_retry(OBJECT **def_array, int def_ct){ 
     495        OBJECT *obj; 
     496        int ct = 0, i = 0, obj_rv = 0; 
     497        OBJECT **next_arr = (OBJECT **)malloc(def_ct * sizeof(OBJECT *)), **tarray = 0; 
     498        int rv = SUCCESS; 
     499        char b[64]; 
     500        int retry = 1, tries = 0; 
     501 
     502        if(global_init_max_defer < 1){ 
     503                output_warning("init_max_defer is less than 1, disabling deferred initialization"); 
     504        } 
     505        while(retry){ 
     506                if(global_init_max_defer <= tries){ 
     507                        output_error("init_by_deferral_retry(): exhausted initialization attempts"); 
     508                        rv = FAILED; 
     509                        break; 
     510                } 
     511                memset(next_arr, 0, def_ct * sizeof(OBJECT *)); 
     512                // initialize each object in def_array 
     513                for(i = 0; i < def_ct; ++i){ 
     514                        obj = def_array[i]; 
     515                        LOCK_OBJECT(obj); 
     516                        obj_rv = object_init(obj); 
     517                        switch(obj_rv){ 
     518                                case 0: 
     519                                        rv = FAILED; 
     520                                        memset(b, 0, 64); 
     521                                        output_error("init_by_deferral_retry(): object %s initialization failed", object_name(obj, b, 63)); 
     522                                        break; 
     523                                case 1: 
     524                                        obj->flags |= OF_INIT; 
     525                                        obj->flags -= OF_DEFERRED; 
     526                                        break; 
     527                                case 2: 
     528                                        next_arr[ct] = obj; 
     529                                        ++ct; 
     530                                        break; 
     531                                // no default 
     532                        } 
     533                        UNLOCK_OBJECT(obj); 
     534                        if(rv == FAILED){ 
     535                                free(next_arr); 
     536                                return rv; 
     537                        } 
     538                } 
     539 
     540                if(ct == def_ct){ 
     541                        output_error("init_by_deferral_retry(): all uninitialized objects deferred, model is unable to initialize"); 
     542                        rv = FAILED; 
     543                        retry = 0; 
     544                } else if (0 == ct){ 
     545                        rv = SUCCESS; 
     546                        retry = 0; 
     547                } else { 
     548                        ++tries; 
     549                        retry = 1; 
     550                        tarray = next_arr; 
     551                        next_arr = def_array; 
     552                        def_array = tarray; 
     553                        def_ct = ct; 
     554                        // three-point turn to swap the 'next' and the 'old' arrays, memset 0'ing at the top. 
     555                } 
     556        } 
     557 
     558        free(next_arr); 
     559        return rv; 
     560} 
     561 
     562static int init_by_deferral(){ 
     563        OBJECT **def_array = 0; 
     564        int i = 0, obj_rv = 0, def_ct = 0; 
     565        OBJECT *obj = 0; 
     566        STATUS rv = SUCCESS; 
     567        char b[64]; 
     568        def_array = (OBJECT **)malloc(sizeof(OBJECT *) * object_get_count()); 
     569        obj = object_get_first(); 
     570        while(obj != 0){ 
     571                LOCK_OBJECT(obj); 
     572                obj_rv = object_init(obj); 
     573                switch (obj_rv){ 
     574                        case 0: 
     575                                rv = FAILED; 
     576                                memset(b, 0, 64); 
     577                                output_error("init_by_deferral(): object %s initialization failed", object_name(obj, b, 63)); 
     578                                break; 
     579                        case 1: 
     580                                obj->flags |= OF_INIT; 
     581                                break; 
     582                        case 2: 
     583                                def_array[def_ct] = obj; 
     584                                ++def_ct; 
     585                                obj->flags |= OF_DEFERRED; 
     586                                break; 
     587                        // no default 
     588                } 
     589                UNLOCK_OBJECT(obj); 
     590 
     591                if(rv == FAILED){ 
     592                        free(def_array); 
     593                        return rv; 
     594                } 
     595 
     596                obj = obj->next; 
     597        } 
     598 
     599        // recursecursecursive 
     600        if(def_ct > 0){ 
     601                rv = init_by_deferral_retry(def_array, def_ct); 
     602                if(rv == FAILED){ // got hung up retrying 
     603                                free(def_array); 
     604                                return FAILED; 
     605                } 
     606        } 
     607        free(def_array); 
     608 
     609        obj = object_get_first(); 
     610        while (obj != 0){ 
     611                if((obj->oclass->passconfig & PC_FORCE_NAME) == PC_FORCE_NAME){ 
     612                        if(0 == strcmp(obj->name, "")){ 
     613                                output_warning("init: object %s:%d should have a name, but doesn't", obj->oclass->name, obj->id); 
     614                                /* TROUBLESHOOT 
     615                                   The object indicated has been flagged by the module which implements its class as one which must be named 
     616                                   to work properly.  Please provide the object with a name and try again. 
     617                                 */ 
     618                        } 
     619                } 
     620                obj = obj->next; 
     621        } 
     622        return SUCCESS; 
     623} 
     624 
    494625static STATUS init_all(void) 
    495626{ 
     
    511642                        break; 
    512643                case IS_DEFERRED: 
    513                         output_fatal("Deferred initialization mode not yet supported"); 
    514                         rv = FAILED; 
     644                        rv = init_by_deferral(); 
    515645                        break; 
    516646                case IS_BOTTOMUP: 
  • trunk/core/globals.c

    r3096 r3100  
    182182        {"return_code", PT_int32, &global_return_code, PA_REFERENCE}, 
    183183        {"module_compiler_flags", PT_enumeration, &global_module_compiler_flags, PA_PUBLIC, mcf_keys}, 
     184        {"init_max_defer", PT_int32, &global_init_max_defer, PA_REFERENCE}, 
    184185        /* add new global variables here */ 
    185186}; 
  • trunk/core/globals.h

    r3096 r3100  
    230230/* system call */ 
    231231GLOBAL int global_return_code INIT(0); /**< return code from last system call */ 
     232GLOBAL int global_init_max_defer INIT(64); /**< maximum number of times objects will be deferred for initialization */ 
    232233 
    233234/* remote data access */ 
  • trunk/core/object.h

    r3090 r3100  
    2828 
    2929/* object flags */ 
    30 #define OF_NONE         0x0000 /**< Object flag; none set */ 
    31 #define OF_HASPLC       0x0001 /**< Object flag; external PLC is attached, disables local PLC */ 
    32 #define OF_LOCKED       0x0002 /**< Object flag; data write pending, reread recommended after lock clears */ 
    33 #define OF_RECALC       0x0008 /**< Object flag; recalculation of derived values is needed */ 
    34 #define OF_FOREIGN      0x0010 /**< Object flag; indicates that object was created in a DLL and memory cannot be freed by core */ 
    35 #define OF_SKIPSAFE     0x0020 /**< Object flag; indicates that skipping updates is safe */ 
    36 #define OF_FORECAST 0x0040 /**< Object flag; inidcates that the object has a valid forecast available */ 
    37 #define OF_DEFERRED 0x0080 /**< Object flag; indicates that the object started to be initialized, but requested deferral */ 
    38 #define OF_RERANK       0x4000 /**< Internal use only */ 
     30#define OF_NONE         0x0000  /**< Object flag; none set */ 
     31#define OF_HASPLC       0x0001  /**< Object flag; external PLC is attached, disables local PLC */ 
     32#define OF_LOCKED       0x0002  /**< Object flag; data write pending, reread recommended after lock clears */ 
     33#define OF_RECALC       0x0008  /**< Object flag; recalculation of derived values is needed */ 
     34#define OF_FOREIGN      0x0010  /**< Object flag; indicates that object was created in a DLL and memory cannot be freed by core */ 
     35#define OF_SKIPSAFE     0x0020  /**< Object flag; indicates that skipping updates is safe */ 
     36#define OF_FORECAST     0x0040  /**< Object flag; inidcates that the object has a valid forecast available */ 
     37#define OF_DEFERRED     0x0080  /**< Object flag; indicates that the object started to be initialized, but requested deferral */ 
     38#define OF_INIT         0x0100  /**< Object flag; indicates that the object has been successfully initialized */ 
     39#define OF_RERANK       0x4000  /**< Internal use only */ 
    3940 
    4041typedef struct s_namespace { 
  • trunk/core/rt/gridlabd.h

    r3097 r3100  
    775775#define OF_SKIPSAFE     0x0020 /**< Object flag; indicates that skipping updates is safe */ 
    776776#define OF_FORECAST 0x0040 /**< Object flag; inidcates that the object has a valid forecast available */ 
     777#define OF_DEFERRED     0x0080  /**< Object flag; indicates that the object started to be initialized, but requested deferral */ 
     778#define OF_INIT         0x0100  /**< Object flag; indicates that the object has been successfully initialized */ 
    777779#define OF_RERANK       0x4000 /**< Internal use only */ 
    778780