From cbb753fee9a37854d09ac4444ffbf2a575c307d0 Mon Sep 17 00:00:00 2001 From: saad0105050 Date: Wed, 1 Oct 2014 03:05:26 -0400 Subject: [PATCH] Incomplete. Todo: segalloc_helper,h, segalloc_epanet_arr.h Signed-off-by: saad0105050 --- dynarrc/dynarr.h | 148 +++++++++++++++++++++++++++++++++- dynarrc/dynarr_freenodelist.h | 91 +++++++++++++++++++++ 2 files changed, 237 insertions(+), 2 deletions(-) diff --git a/dynarrc/dynarr.h b/dynarrc/dynarr.h index 47a0b94..9b8fa8a 100644 --- a/dynarrc/dynarr.h +++ b/dynarrc/dynarr.h @@ -31,8 +31,6 @@ extern int seg_bank_increase_max(); #define SEG_ARR_NAME pSegArr -#define FREESEG_LIST_NAME pFreeSegList -#define FREESEG_INVALID_LIST_NAME pFreeSegInvalidList #define SEG_INSERT_POS iInsertPos @@ -78,6 +76,152 @@ struct TYPED_NAME(DynArr){ int a; }; +/* Accessor methods */ +#define SEG_ARR_GET_SEG(segIndex) \ + ( (segIndex) != EMPTY_INDEX && \ + (segIndex) >= 0 && \ + (segIndex) < iInsertPos )\ + ? ( (tempSeg1).index = (segIndex),\ + tempSeg1)\ + : NULL_SEG + + +// Region: seg banks use/create/init/cleanup +#ifdef _REGION_ + + +#define SEG_BANKS_USE() \ + extern int NumSegBanks; \ + extern int MaxSegBanks; \ + extern real_t** SEG_BANKS_ARR; \ + extern int* SEG_BANK_SIZE_ARR; \ + extern int* SEG_BANK_INDEX_LIMIT_ARR; \ + extern int SegBankAllocViolation; \ + +#define SEG_BANKS_CREATE() \ + /* NumSegBanks must be initialized at once, because it may be needed by GPU */ \ + int NumSegBanks = 1; /* Default value */ \ + int MaxSegBanks = 1; /* Default value */ \ + real_t** SEG_BANKS_ARR; \ + int* SEG_BANK_SIZE_ARR; \ + int* SEG_BANK_INDEX_LIMIT_ARR; \ + int SegBankAllocViolation = 0; \ + + +/* SEG_BANKS_INIT() must be called after/at the end of SEG_ARR_INIT() */ +#define SEG_BANKS_INIT() \ + /* In test mode, do not initalize MaxSegBanks here so that + it can be initialized by other code before calling SEG_BANKS_INIT() */ \ + if (!TestEnabled) MaxSegBanks = 1; \ + SEG_BANKS_ARR = (real_t**)malloc(MaxSegBanks * sizeof(real_t*)); \ + memset(SEG_BANKS_ARR, 0, MaxSegBanks * sizeof(real_t*)); \ + SEG_BANK_SIZE_ARR = (int*)malloc(MaxSegBanks * sizeof(int)); \ + memset(SEG_BANK_SIZE_ARR, 0, MaxSegBanks * sizeof(int)); \ + SEG_BANK_INDEX_LIMIT_ARR = (int*)malloc(MaxSegBanks * sizeof(int)); \ + memset(SEG_BANK_INDEX_LIMIT_ARR, 0, MaxSegBanks * sizeof(int)); \ + SEG_BANKS_ARR[0] = pSegArr + SEG_ARR_SEG_CONTENTS_OFFSET; \ + SEG_BANK_SIZE_ARR[0] = MAXSEG; \ + SEG_BANK_INDEX_LIMIT_ARR[0] = MAXSEG - 1; \ + NumSegBanks = 1; /* one segment bank is already created */ \ + SegBankAllocViolation = 0; \ + +#define SEG_BANKS_CLEANUP() \ + for(iTempIndex1 = 1; iTempIndex1 < NUM_SEG_BANKS; iTempIndex1++){ \ + FREE_SAFE(SEG_BANKS_ARR[iTempIndex1]); \ + } \ + FREE_SAFE(SEG_BANKS_ARR); \ + FREE_SAFE(SEG_BANK_SIZE_ARR); \ + FREE_SAFE(SEG_BANK_INDEX_LIMIT_ARR); \ + SegBankAllocViolation = 0; \ + +#endif + +/* DynArr methods*/ + +#define SEG_ARR_USE() \ + extern real_t* pSegArr; \ + extern real_t* pSegCArr; \ + extern real_t* pSegVArr; \ + extern real_t* pSegNextArr; \ + extern real_t *pFirstSegIndexForPipe, *pLastSegIndexForPipe; \ + extern int iInsertPos; \ + extern struct FreeSegNode *pFreeSegList, *pFreeSegInvalidList; \ + extern int iSizeOfFreeSegList; \ + extern struct FreeSegNode* pTempFreeNode1, *pTempFreeNode2; \ + extern int iTempIndex1, iTempIndex2; \ + extern struct Sseg tempSeg1;\ + extern const struct Sseg emptySeg;\ + /* multiple segment banks */ \ + SEG_BANKS_USE(); \ + + + +#define SEG_ARR_CREATE() \ + real_t* pSegArr = NULL; \ + real_t* pSegCArr = NULL; \ + real_t* pSegVArr = NULL; \ + real_t* pSegNextArr = NULL; \ + real_t *pFirstSegIndexForPipe = NULL; \ + real_t *pLastSegIndexForPipe = NULL; \ + int iInsertPos = SEG_INSERT_POS_BEGIN; \ + struct FreeSegNode *pFreeSegList = NULL, *pFreeSegInvalidList = NULL; \ + int iSizeOfFreeSegList = 0; \ + struct FreeSegNode* pTempFreeNode1 = NULL, *pTempFreeNode2 = NULL; \ + int iTempIndex1 = 0, iTempIndex2 = 0;\ + const struct Sseg emptySeg = EMPTY_SSEG;\ + struct Sseg tempSeg1; \ + /* multiple segment banks */ \ + SEG_BANKS_CREATE(); \ + + +#define SEG_ARR_INIT() \ + printf("maxseg: %d, offsets... fs:%d ls:%d c:%d v:%d n:%d num_seg_arr_items:%d\n", \ + MAXSEG, \ + SEG_ARR_FIRSTSEG_OFFSET, \ + SEG_ARR_LASTSEG_OFFSET, \ + SEG_ARR_C_OFFSET, \ + SEG_ARR_V_OFFSET, \ + SEG_ARR_NEXT_OFFSET, \ + NUM_SEG_ARR_ITEMS \ + ); \ + /*MaxSeg should be already assigned */ \ + assert(MaxSeg > 0 ); \ + LogMaxSeg = (unsigned int) (log(MaxSeg+0.0f)/log(2.0f)); \ + MaxSegRemainderBitMask = (unsigned int) (MaxSeg - 1); \ + pSegArr = (real_t*) malloc(sizeof(real_t) * NUM_SEG_ARR_ITEMS); \ + assert(pSegArr != NULL ); \ + /* seg indexes: first-seg, last-seg */ \ + pFirstSegIndexForPipe = (real_t*)(pSegArr + SEG_ARR_FIRSTSEG_OFFSET); \ + pLastSegIndexForPipe = (real_t*)(pSegArr + SEG_ARR_LASTSEG_OFFSET); \ + /* seg contents: c,v,next */ \ + pSegCArr = (real_t*)(pSegArr + SEG_ARR_C_OFFSET); \ + pSegVArr = (real_t*)(pSegArr + SEG_ARR_V_OFFSET); \ + pSegNextArr = (real_t*)(pSegArr + SEG_ARR_NEXT_OFFSET); \ + /* seg-arr management */ \ + iInsertPos = SEG_INSERT_POS_BEGIN; \ + pFreeSegList = NULL;\ + pFreeSegInvalidList = NULL; \ + memset(pSegArr, 0, sizeof(real_t) * NUM_SEG_ARR_ITEMS ); \ + /* multiple segment banks */ \ + SEG_BANKS_INIT(); \ + +// +// allocated arrays remain to be used by later timesteps +// + +#define SEG_ARR_CLEANUP() \ + FREE_SAFE( pSegArr ); \ + _SEG_ARR_FREE_LIST_CLEANUP(); \ + pSegCArr = pSegVArr = pSegNextArr = NULL; \ + iInsertPos = SEG_INSERT_POS_BEGIN; \ + pFirstSegIndexForPipe = NULL; \ + pLastSegIndexForPipe = NULL; \ + /* multiple segment banks */ \ + SEG_BANKS_CLEANUP(); \ + + +#define SEG_ARR_EXISTS() ( pSegArr != NULL ) +#define SEG_ARR_SIZE NUM_SEG_ARR_ITEMS #endif diff --git a/dynarrc/dynarr_freenodelist.h b/dynarrc/dynarr_freenodelist.h index dfeabd4..8212da8 100644 --- a/dynarrc/dynarr_freenodelist.h +++ b/dynarrc/dynarr_freenodelist.h @@ -8,4 +8,95 @@ struct FreeSegNode{ }; typedef struct FreeSegNode FreeSegNode; +// name of variables +#define FREESEG_LIST_NAME pFreeSegList +#define FREESEG_INVALID_LIST_NAME pFreeSegInvalidList + + +#define _FreeSegNodeMalloc() \ + ( (struct FreeSegNode*) malloc(sizeof(struct FreeSegNode)) ) +// Swaps two 32-bit intergers without using temporary variables +#define _SEG_ARR_INT_SWAP(var1, var2) (\ + (var1) = (FreeSegNode*) (((int) (void*) var1) ^ ((int) (void*) var2) ), \ + (var2) = (FreeSegNode*) (((int) (void*) var1) ^ ((int) (void*) var2) ), \ + (var1) = (FreeSegNode*) (((int) (void*) var1) ^ ((int) (void*) var2) ) ) +// Given two lists A and B, moves the head of A in front of the +// head of B. Head of A must be non-null. +// +// Before: +// A-->1-2-3-4-5 +// B-->a-b-c-d-e +// After: +// A-->2-3-4-5 +// B-->1-a-b-c-d-e +// +#define _SEG_ARR_MOVE_LIST_HEAD(listFrom, listTo) (\ + _SEG_ARR_INT_SWAP( listFrom->next, listTo), \ + _SEG_ARR_INT_SWAP( listFrom, listTo ) \ + ) + + +#define _SEG_ARR_FREE_LIST_ADD_VALIDLIST(value_seg) \ + ( ( !FREESEG_LIST_NAME ) \ + ?( \ + FREESEG_LIST_NAME = _FreeSegNodeMalloc(), \ + /*assert(FREESEG_LIST_NAME != NULL ),*/ \ + FREESEG_LIST_NAME->next = NULL, \ + FREESEG_LIST_NAME->index = ((value_seg).index) \ + ) \ + :( \ + pTempFreeNode1 = _FreeSegNodeMalloc(), \ + /*assert(pTempFreeNode1 != NULL ),*/ \ + pTempFreeNode1->next = FREESEG_LIST_NAME, \ + FREESEG_LIST_NAME = pTempFreeNode1, \ + FREESEG_LIST_NAME->index = ((value_seg).index) \ + )\ + ) + +// Algorithm: +// If (an invalid node exists) Then +// move it to the front of valid list +// set its index as the index of value_seg +// Else +// Allocate new node in the valid list +// +#define _SEG_ARR_FREE_LIST_ADD(value_seg) (\ + ( FREESEG_INVALID_LIST_NAME ) \ + ? ( \ + _SEG_ARR_MOVE_LIST_HEAD( FREESEG_INVALID_LIST_NAME, FREESEG_LIST_NAME ), \ + FREESEG_LIST_NAME->index = ((value_seg).index) \ + ) \ + : ( _SEG_ARR_FREE_LIST_ADD_VALIDLIST(value_seg) ) \ + ) + + +#define _SEG_ARR_REMOVE_LIST_HEAD(list) (\ + pTempFreeNode1 = list, \ + list = list->next, \ + FREE_SAFE( pTempFreeNode1 ) )\ + +// Algorithm: +// If (a valid node exists) Then +// output its index, and +// move it to the front of invalid list +// Else +// output null index +// +#define _SEG_ARR_FREE_LIST_GRAB_INDEX(value_seg) (\ + ( FREESEG_LIST_NAME ) ?(\ + (value_seg).index = FREESEG_LIST_NAME->index, \ + (value_seg).nextIndex = EMPTY_INDEX, \ + _SEG_ARR_MOVE_LIST_HEAD( FREESEG_LIST_NAME, FREESEG_INVALID_LIST_NAME ), \ + (value_seg) \ + ):(\ + ( (value_seg) = NULL_SEG ) \ + ) )\ + + +#define _SEG_ARR_FREE_LIST_CLEANUP() {\ + while( FREESEG_LIST_NAME ) _SEG_ARR_REMOVE_LIST_HEAD(FREESEG_LIST_NAME); \ + while( FREESEG_INVALID_LIST_NAME ) _SEG_ARR_REMOVE_LIST_HEAD(FREESEG_INVALID_LIST_NAME); \ + } \ + + #endif \ No newline at end of file