📄 zmemory.c.bak
字号:
} } if ( ! *dworkptr ) { fprintf(stderr, "malloc fails for local dworkptr[]."); return (isize + dsize + n); } return 0;}/* * Set up pointers for real working arrays. */voidzSetRWork(int m, int panel_size, doublecomplex *dworkptr, doublecomplex **dense, doublecomplex **tempv){ doublecomplex zero = {0.0, 0.0}; int maxsuper = sp_ienv(3), rowblk = sp_ienv(4); *dense = dworkptr; *tempv = *dense + panel_size*m; zfill (*dense, m * panel_size, zero); zfill (*tempv, NUM_TEMPV(m,panel_size,maxsuper,rowblk), zero); } /* * Free the working storage used by factor routines. */void zLUWorkFree(int *iwork, doublecomplex *dwork, GlobalLU_t *Glu){ if ( Glu->MemModel == SYSTEM ) { SUPERLU_FREE (iwork); SUPERLU_FREE (dwork); } else { stack.used -= (stack.size - stack.top2); stack.top2 = stack.size;/* zStackCompress(Glu); */ } SUPERLU_FREE (expanders); expanders = 0;}/* Expand the data structures for L and U during the factorization. * Return value: 0 - successful return * > 0 - number of bytes allocated when run out of space */intzLUMemXpand(int jcol, int next, /* number of elements currently in the factors */ MemType mem_type, /* which type of memory to expand */ int *maxlen, /* modified - maximum length of a data structure */ GlobalLU_t *Glu /* modified - global LU data structures */ ){ void *new_mem; #ifdef DEBUG printf("zLUMemXpand(): jcol %d, next %d, maxlen %d, MemType %d\n", jcol, next, *maxlen, mem_type);#endif if (mem_type == USUB) new_mem = zexpand(maxlen, mem_type, next, 1, Glu); else new_mem = zexpand(maxlen, mem_type, next, 0, Glu); if ( !new_mem ) { int nzlmax = Glu->nzlmax; int nzumax = Glu->nzumax; int nzlumax = Glu->nzlumax; fprintf(stderr, "Can't expand MemType %d: jcol %d\n", mem_type, jcol); return (zmemory_usage(nzlmax, nzumax, nzlumax, Glu->n) + Glu->n); } switch ( mem_type ) { case LUSUP: Glu->lusup = (doublecomplex *) new_mem; Glu->nzlumax = *maxlen; break; case UCOL: Glu->ucol = (doublecomplex *) new_mem; Glu->nzumax = *maxlen; break; case LSUB: Glu->lsub = (int *) new_mem; Glu->nzlmax = *maxlen; break; case USUB: Glu->usub = (int *) new_mem; Glu->nzumax = *maxlen; break; } return 0; }voidcopy_mem_doublecomplex(int howmany, void *old, void *new){ register int i; doublecomplex *dold = old; doublecomplex *dnew = new; for (i = 0; i < howmany; i++) dnew[i] = dold[i];}/* * Expand the existing storage to accommodate more fill-ins. */void*zexpand ( int *prev_len, /* length used from previous call */ MemType type, /* which part of the memory to expand */ int len_to_copy, /* size of the memory to be copied to new store */ int keep_prev, /* = 1: use prev_len; = 0: compute new_len to expand */ GlobalLU_t *Glu /* modified - global LU data structures */ ){ float EXPAND = 1.5; float alpha; void *new_mem, *old_mem; int new_len, tries, lword, extra, bytes_to_copy; alpha = EXPAND; if ( no_expand == 0 || keep_prev ) /* First time allocate requested */ new_len = *prev_len; else { new_len = alpha * *prev_len; } if ( type == LSUB || type == USUB ) lword = sizeof(int); else lword = sizeof(doublecomplex); if ( Glu->MemModel == SYSTEM ) { new_mem = (void *) SUPERLU_MALLOC(new_len * lword);/* new_mem = (void *) calloc(new_len, lword); */ if ( no_expand != 0 ) { tries = 0; if ( keep_prev ) { if ( !new_mem ) return (NULL); } else { while ( !new_mem ) { if ( ++tries > 10 ) return (NULL); alpha = Reduce(alpha); new_len = alpha * *prev_len; new_mem = (void *) SUPERLU_MALLOC(new_len * lword); /* new_mem = (void *) calloc(new_len, lword); */ } } if ( type == LSUB || type == USUB ) { copy_mem_int(len_to_copy, expanders[type].mem, new_mem); } else { copy_mem_doublecomplex(len_to_copy, expanders[type].mem, new_mem); } SUPERLU_FREE (expanders[type].mem); } expanders[type].mem = (void *) new_mem; } else { /* MemModel == USER */ if ( no_expand == 0 ) { new_mem = zuser_malloc(new_len * lword, HEAD); if ( NotDoubleAlign(new_mem) && (type == LUSUP || type == UCOL) ) { old_mem = new_mem; new_mem = (void *)DoubleAlign(new_mem); extra = (char*)new_mem - (char*)old_mem;#ifdef DEBUG printf("expand(): not aligned, extra %d\n", extra);#endif stack.top1 += extra; stack.used += extra; } expanders[type].mem = (void *) new_mem; } else { tries = 0; extra = (new_len - *prev_len) * lword; if ( keep_prev ) { if ( StackFull(extra) ) return (NULL); } else { while ( StackFull(extra) ) { if ( ++tries > 10 ) return (NULL); alpha = Reduce(alpha); new_len = alpha * *prev_len; extra = (new_len - *prev_len) * lword; } } if ( type != USUB ) { new_mem = (void*)((char*)expanders[type + 1].mem + extra); bytes_to_copy = (char*)stack.array + stack.top1 - (char*)expanders[type + 1].mem; user_bcopy(expanders[type+1].mem, new_mem, bytes_to_copy); if ( type < USUB ) { Glu->usub = expanders[USUB].mem = (void*)((char*)expanders[USUB].mem + extra); } if ( type < LSUB ) { Glu->lsub = expanders[LSUB].mem = (void*)((char*)expanders[LSUB].mem + extra); } if ( type < UCOL ) { Glu->ucol = expanders[UCOL].mem = (void*)((char*)expanders[UCOL].mem + extra); } stack.top1 += extra; stack.used += extra; if ( type == UCOL ) { stack.top1 += extra; /* Add same amount for USUB */ stack.used += extra; } } /* if ... */ } /* else ... */ } expanders[type].size = new_len; *prev_len = new_len; if ( no_expand ) ++no_expand; return (void *) expanders[type].mem; } /* zexpand *//* * Compress the work[] array to remove fragmentation. */voidzStackCompress(GlobalLU_t *Glu){ register int iword, dword, ndim; char *last, *fragment; int *ifrom, *ito; doublecomplex *dfrom, *dto; int *xlsub, *lsub, *xusub, *usub, *xlusup; doublecomplex *ucol, *lusup; iword = sizeof(int); dword = sizeof(doublecomplex); ndim = Glu->n; xlsub = Glu->xlsub; lsub = Glu->lsub; xusub = Glu->xusub; usub = Glu->usub; xlusup = Glu->xlusup; ucol = Glu->ucol; lusup = Glu->lusup; dfrom = ucol; dto = (doublecomplex *)((char*)lusup + xlusup[ndim] * dword); copy_mem_doublecomplex(xusub[ndim], dfrom, dto); ucol = dto; ifrom = lsub; ito = (int *) ((char*)ucol + xusub[ndim] * iword); copy_mem_int(xlsub[ndim], ifrom, ito); lsub = ito; ifrom = usub; ito = (int *) ((char*)lsub + xlsub[ndim] * iword); copy_mem_int(xusub[ndim], ifrom, ito); usub = ito; last = (char*)usub + xusub[ndim] * iword; fragment = (char*) (((char*)stack.array + stack.top1) - last); stack.used -= (long int) fragment; stack.top1 -= (long int) fragment; Glu->ucol = ucol; Glu->lsub = lsub; Glu->usub = usub; #ifdef DEBUG printf("zStackCompress: fragment %d\n", fragment); /* for (last = 0; last < ndim; ++last) print_lu_col("After compress:", last, 0);*/#endif }/* * Allocate storage for original matrix A */voidzallocateA(int n, int nnz, doublecomplex **a, int **asub, int **xa){ *a = (doublecomplex *) doublecomplexMalloc(nnz); *asub = (int *) intMalloc(nnz); *xa = (int *) intMalloc(n+1);}doublecomplex *doublecomplexMalloc(int n){ doublecomplex *buf; buf = (doublecomplex *) SUPERLU_MALLOC(n * sizeof(doublecomplex)); if ( !buf ) { ABORT("SUPERLU_MALLOC failed for buf in doublecomplexMalloc()\n"); } return (buf);}doublecomplex *doublecomplexCalloc(int n){ doublecomplex *buf; register int i; doublecomplex zero = {0.0, 0.0}; buf = (doublecomplex *) SUPERLU_MALLOC(n * sizeof(doublecomplex)); if ( !buf ) { ABORT("SUPERLU_MALLOC failed for buf in doublecomplexCalloc()\n"); } for (i = 0; i < n; ++i) buf[i] = zero; return (buf);}int zmemory_usage(const int nzlmax, const int nzumax, const int nzlumax, const int n){ register int iword, dword; iword = sizeof(int); dword = sizeof(doublecomplex); return (10 * n * iword + nzlmax * iword + nzumax * (iword + dword) + nzlumax * dword);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -