Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
dgl
Commits
3359c1f1
Commit
3359c1f1
authored
May 12, 2023
by
lisj
Browse files
增加GKLib
parent
f2c80b44
Changes
63
Hide whitespace changes
Inline
Side-by-side
Showing
20 changed files
with
17114 additions
and
0 deletions
+17114
-0
third_party/METIS/GKlib/gk_macros.h
third_party/METIS/GKlib/gk_macros.h
+169
-0
third_party/METIS/GKlib/gk_mkblas.h
third_party/METIS/GKlib/gk_mkblas.h
+203
-0
third_party/METIS/GKlib/gk_mkmemory.h
third_party/METIS/GKlib/gk_mkmemory.h
+142
-0
third_party/METIS/GKlib/gk_mkpqueue.h
third_party/METIS/GKlib/gk_mkpqueue.h
+440
-0
third_party/METIS/GKlib/gk_mkpqueue2.h
third_party/METIS/GKlib/gk_mkpqueue2.h
+215
-0
third_party/METIS/GKlib/gk_mkrandom.h
third_party/METIS/GKlib/gk_mkrandom.h
+123
-0
third_party/METIS/GKlib/gk_mksort.h
third_party/METIS/GKlib/gk_mksort.h
+271
-0
third_party/METIS/GKlib/gk_mkutils.h
third_party/METIS/GKlib/gk_mkutils.h
+40
-0
third_party/METIS/GKlib/gk_proto.h
third_party/METIS/GKlib/gk_proto.h
+414
-0
third_party/METIS/GKlib/gk_struct.h
third_party/METIS/GKlib/gk_struct.h
+274
-0
third_party/METIS/GKlib/gk_types.h
third_party/METIS/GKlib/gk_types.h
+38
-0
third_party/METIS/GKlib/gk_util.c
third_party/METIS/GKlib/gk_util.c
+107
-0
third_party/METIS/GKlib/gkregex.c
third_party/METIS/GKlib/gkregex.c
+10704
-0
third_party/METIS/GKlib/gkregex.h
third_party/METIS/GKlib/gkregex.h
+556
-0
third_party/METIS/GKlib/graph.c
third_party/METIS/GKlib/graph.c
+1665
-0
third_party/METIS/GKlib/htable.c
third_party/METIS/GKlib/htable.c
+247
-0
third_party/METIS/GKlib/io.c
third_party/METIS/GKlib/io.c
+621
-0
third_party/METIS/GKlib/itemsets.c
third_party/METIS/GKlib/itemsets.c
+210
-0
third_party/METIS/GKlib/mcore.c
third_party/METIS/GKlib/mcore.c
+393
-0
third_party/METIS/GKlib/memory.c
third_party/METIS/GKlib/memory.c
+282
-0
No files found.
third_party/METIS/GKlib/gk_macros.h
0 → 100644
View file @
3359c1f1
/*!
\file gk_macros.h
\brief This file contains various macros
\date Started 3/27/2007
\author George
\version\verbatim $Id: gk_macros.h 15048 2013-08-31 19:38:14Z karypis $ \endverbatim
*/
#ifndef _GK_MACROS_H_
#define _GK_MACROS_H_
/*-------------------------------------------------------------
* Usefull commands
*-------------------------------------------------------------*/
#define gk_max(a, b) ((a) >= (b) ? (a) : (b))
#define gk_min(a, b) ((a) >= (b) ? (b) : (a))
#define gk_max3(a, b, c) ((a) >= (b) && (a) >= (c) ? (a) : ((b) >= (a) && (b) >= (c) ? (b) : (c)))
#define gk_SWAP(a, b, tmp) do {(tmp) = (a); (a) = (b); (b) = (tmp);} while(0)
#define INC_DEC(a, b, val) do {(a) += (val); (b) -= (val);} while(0)
#define sign(a, b) ((a >= 0 ? b : -b))
#define ONEOVERRANDMAX (1.0/(RAND_MAX+1.0))
#define RandomInRange(u) ((int) (ONEOVERRANDMAX*(u)*rand()))
#define RandomInRange_r(s, u) ((int) (ONEOVERRANDMAX*(u)*rand_r(s)))
#define gk_abs(x) ((x) >= 0 ? (x) : -(x))
/*-------------------------------------------------------------
* Timing macros
*-------------------------------------------------------------*/
#define gk_clearcputimer(tmr) (tmr = 0.0)
#define gk_startcputimer(tmr) (tmr -= gk_CPUSeconds())
#define gk_stopcputimer(tmr) (tmr += gk_CPUSeconds())
#define gk_getcputimer(tmr) (tmr)
#define gk_clearwctimer(tmr) (tmr = 0.0)
#define gk_startwctimer(tmr) (tmr -= gk_WClockSeconds())
#define gk_stopwctimer(tmr) (tmr += gk_WClockSeconds())
#define gk_getwctimer(tmr) (tmr)
/*-------------------------------------------------------------
* dbglvl handling macros
*-------------------------------------------------------------*/
#define IFSET(a, flag, cmd) if ((a)&(flag)) (cmd);
/*-------------------------------------------------------------
* gracefull library exit macro
*-------------------------------------------------------------*/
#define GKSETJMP() (setjmp(gk_return_to_entry))
#define gk_sigcatch() (setjmp(gk_jbufs[gk_cur_jbufs]))
/*-------------------------------------------------------------
* Debuging memory leaks
*-------------------------------------------------------------*/
#ifdef DMALLOC
# define MALLOC_CHECK(ptr) \
if (malloc_verify((ptr)) == DMALLOC_VERIFY_ERROR) { \
printf("***MALLOC_CHECK failed on line %d of file %s: " #ptr "\n", \
__LINE__, __FILE__); \
abort(); \
}
#else
# define MALLOC_CHECK(ptr) ;
#endif
/*-------------------------------------------------------------
* CSR conversion macros
*-------------------------------------------------------------*/
#define MAKECSR(i, n, a) \
do { \
for (i=1; i<n; i++) a[i] += a[i-1]; \
for (i=n; i>0; i--) a[i] = a[i-1]; \
a[0] = 0; \
} while(0)
#define SHIFTCSR(i, n, a) \
do { \
for (i=n; i>0; i--) a[i] = a[i-1]; \
a[0] = 0; \
} while(0)
/*-------------------------------------------------------------
* ASSERTS that cannot be turned off!
*-------------------------------------------------------------*/
#define GKASSERT(expr) \
if (!(expr)) { \
printf("***ASSERTION failed on line %d of file %s: " #expr "\n", \
__LINE__, __FILE__); \
abort(); \
}
#define GKASSERTP(expr,msg) \
if (!(expr)) { \
printf("***ASSERTION failed on line %d of file %s: " #expr "\n", \
__LINE__, __FILE__); \
printf msg ; \
printf("\n"); \
abort(); \
}
#define GKCUASSERT(expr) \
if (!(expr)) { \
printf("***ASSERTION failed on line %d of file %s: " #expr "\n", \
__LINE__, __FILE__); \
}
#define GKWARN(expr) \
if (!(expr)) { \
printf("***ASSERTION failed on line %d of file %s: " #expr "\n", \
__LINE__, __FILE__); \
}
#define GKCUASSERTP(expr,msg) \
if (!(expr)) { \
printf("***ASSERTION failed on line %d of file %s: " #expr "\n", \
__LINE__, __FILE__); \
printf msg ; \
printf("\n"); \
}
#define GKWARNP(expr,msg) \
if (!(expr)) { \
printf("***ASSERTION failed on line %d of file %s: " #expr "\n", \
__LINE__, __FILE__); \
printf msg ; \
printf("\n"); \
}
/*-------------------------------------------------------------
* Program Assertions
*-------------------------------------------------------------*/
#ifndef NDEBUG
# define ASSERT(expr) \
if (!(expr)) { \
printf("***ASSERTION failed on line %d of file %s: " #expr "\n", \
__LINE__, __FILE__); \
assert(expr); \
}
# define ASSERTP(expr,msg) \
if (!(expr)) { \
printf("***ASSERTION failed on line %d of file %s: " #expr "\n", \
__LINE__, __FILE__); \
printf msg ; \
printf("\n"); \
assert(expr); \
}
#else
# define ASSERT(expr) ;
# define ASSERTP(expr,msg) ;
#endif
#ifndef NDEBUG2
# define ASSERT2 ASSERT
# define ASSERTP2 ASSERTP
#else
# define ASSERT2(expr) ;
# define ASSERTP2(expr,msg) ;
#endif
#endif
third_party/METIS/GKlib/gk_mkblas.h
0 → 100644
View file @
3359c1f1
/*!
\file gk_mkblas.h
\brief Templates for BLAS-like routines
\date Started 3/28/07
\author George
\version\verbatim $Id: gk_mkblas.h 16304 2014-02-25 14:27:19Z karypis $ \endverbatim
*/
#ifndef _GK_MKBLAS_H_
#define _GK_MKBLAS_H_
#define GK_MKBLAS(PRFX, TYPE, OUTTYPE) \
/*************************************************************************/
\
/*! The macro for gk_?incset()-class of routines */
\
/*************************************************************************/
\
TYPE *PRFX ## incset(size_t n, TYPE baseval, TYPE *x)\
{\
size_t i;\
\
for (i=0; i<n; i++)\
x[i] = baseval+i;\
\
return x;\
}\
\
/*************************************************************************/
\
/*! The macro for gk_?max()-class of routines */
\
/*************************************************************************/
\
TYPE PRFX ## max(size_t n, TYPE *x, size_t incx)\
{\
size_t i;\
TYPE max;\
\
if (n <= 0) return (TYPE) 0;\
\
for (max=(*x), x+=incx, i=1; i<n; i++, x+=incx)\
max = ((*x) > max ? (*x) : max);\
\
return max;\
}\
\
\
/*************************************************************************/
\
/*! The macro for gk_?min()-class of routines */
\
/*************************************************************************/
\
TYPE PRFX ## min(size_t n, TYPE *x, size_t incx)\
{\
size_t i;\
TYPE min;\
\
if (n <= 0) return (TYPE) 0;\
\
for (min=(*x), x+=incx, i=1; i<n; i++, x+=incx)\
min = ((*x) < min ? (*x) : min);\
\
return min;\
}\
\
\
/*************************************************************************/
\
/*! The macro for gk_?argmax()-class of routines */
\
/*************************************************************************/
\
size_t PRFX ## argmax(size_t n, TYPE *x, size_t incx)\
{\
size_t i, j, max=0;\
\
for (i=1, j=incx; i<n; i++, j+=incx)\
max = (x[j] > x[max] ? j : max);\
\
return (size_t)(max/incx);\
}\
\
\
/*************************************************************************/
\
/*! The macro for gk_?argmin()-class of routines */
\
/*************************************************************************/
\
size_t PRFX ## argmin(size_t n, TYPE *x, size_t incx)\
{\
size_t i, j, min=0;\
\
for (i=1, j=incx; i<n; i++, j+=incx)\
min = (x[j] < x[min] ? j : min);\
\
return (size_t)(min/incx);\
}\
\
\
/*************************************************************************/
\
/*! The macro for gk_?argmax_n()-class of routines */
\
/*************************************************************************/
\
size_t PRFX ## argmax_n(size_t n, TYPE *x, size_t incx, size_t k)\
{\
size_t i, j, max_n;\
PRFX ## kv_t *cand;\
\
cand = PRFX ## kvmalloc(n, "GK_ARGMAX_N: cand");\
\
for (i=0, j=0; i<n; i++, j+=incx) {\
cand[i].val = i;\
cand[i].key = x[j];\
}\
PRFX ## kvsortd(n, cand);\
\
max_n = cand[k-1].val;\
\
gk_free((void *)&cand, LTERM);\
\
return max_n;\
}\
\
\
/*************************************************************************/
\
/*! The macro for gk_?sum()-class of routines */
\
/**************************************************************************/
\
OUTTYPE PRFX ## sum(size_t n, TYPE *x, size_t incx)\
{\
size_t i;\
OUTTYPE sum = 0;\
\
for (i=0; i<n; i++, x+=incx)\
sum += (*x);\
\
return sum;\
}\
\
\
/*************************************************************************/
\
/*! The macro for gk_?scale()-class of routines */
\
/**************************************************************************/
\
TYPE *PRFX ## scale(size_t n, TYPE alpha, TYPE *x, size_t incx)\
{\
size_t i;\
\
for (i=0; i<n; i++, x+=incx)\
(*x) *= alpha;\
\
return x;\
}\
\
\
/*************************************************************************/
\
/*! The macro for gk_?norm2()-class of routines */
\
/**************************************************************************/
\
OUTTYPE PRFX ## norm2(size_t n, TYPE *x, size_t incx)\
{\
size_t i;\
OUTTYPE partial = 0;\
\
for (i=0; i<n; i++, x+=incx)\
partial += (*x) * (*x);\
\
return (partial > 0 ? (OUTTYPE)sqrt((double)partial) : (OUTTYPE)0);\
}\
\
\
/*************************************************************************/
\
/*! The macro for gk_?dot()-class of routines */
\
/**************************************************************************/
\
OUTTYPE PRFX ## dot(size_t n, TYPE *x, size_t incx, TYPE *y, size_t incy)\
{\
size_t i;\
OUTTYPE partial = 0.0;\
\
for (i=0; i<n; i++, x+=incx, y+=incy)\
partial += (*x) * (*y);\
\
return partial;\
}\
\
\
/*************************************************************************/
\
/*! The macro for gk_?axpy()-class of routines */
\
/**************************************************************************/
\
TYPE *PRFX ## axpy(size_t n, TYPE alpha, TYPE *x, size_t incx, TYPE *y, size_t incy)\
{\
size_t i;\
TYPE *y_in = y;\
\
for (i=0; i<n; i++, x+=incx, y+=incy)\
*y += alpha*(*x);\
\
return y_in;\
}\
#define GK_MKBLAS_PROTO(PRFX, TYPE, OUTTYPE) \
TYPE *PRFX ## incset(size_t n, TYPE baseval, TYPE *x);\
TYPE PRFX ## max(size_t n, TYPE *x, size_t incx);\
TYPE PRFX ## min(size_t n, TYPE *x, size_t incx);\
size_t PRFX ## argmax(size_t n, TYPE *x, size_t incx);\
size_t PRFX ## argmin(size_t n, TYPE *x, size_t incx);\
size_t PRFX ## argmax_n(size_t n, TYPE *x, size_t incx, size_t k);\
OUTTYPE PRFX ## sum(size_t n, TYPE *x, size_t incx);\
TYPE *PRFX ## scale(size_t n, TYPE alpha, TYPE *x, size_t incx);\
OUTTYPE PRFX ## norm2(size_t n, TYPE *x, size_t incx);\
OUTTYPE PRFX ## dot(size_t n, TYPE *x, size_t incx, TYPE *y, size_t incy);\
TYPE *PRFX ## axpy(size_t n, TYPE alpha, TYPE *x, size_t incx, TYPE *y, size_t incy);\
#endif
third_party/METIS/GKlib/gk_mkmemory.h
0 → 100644
View file @
3359c1f1
/*!
\file gk_mkmemory.h
\brief Templates for memory allocation routines
\date Started 3/29/07
\author George
\version\verbatim $Id: gk_mkmemory.h 10711 2011-08-31 22:23:04Z karypis $ \endverbatim
*/
#ifndef _GK_MKMEMORY_H_
#define _GK_MKMEMORY_H_
#define GK_MKALLOC(PRFX, TYPE)\
/*************************************************************************/
\
/*! The macro for gk_?malloc()-class of routines */
\
/**************************************************************************/
\
TYPE *PRFX ## malloc(size_t n, char *msg)\
{\
return (TYPE *)gk_malloc(sizeof(TYPE)*n, msg);\
}\
\
\
/*************************************************************************/
\
/*! The macro for gk_?realloc()-class of routines */
\
/**************************************************************************/
\
TYPE *PRFX ## realloc(TYPE *ptr, size_t n, char *msg)\
{\
return (TYPE *)gk_realloc((void *)ptr, sizeof(TYPE)*n, msg);\
}\
\
\
/*************************************************************************/
\
/*! The macro for gk_?smalloc()-class of routines */
\
/**************************************************************************/
\
TYPE *PRFX ## smalloc(size_t n, TYPE ival, char *msg)\
{\
TYPE *ptr;\
\
ptr = (TYPE *)gk_malloc(sizeof(TYPE)*n, msg);\
if (ptr == NULL) \
return NULL; \
\
return PRFX ## set(n, ival, ptr); \
}\
\
\
/*************************************************************************/
\
/*! The macro for gk_?set()-class of routines */
\
/*************************************************************************/
\
TYPE *PRFX ## set(size_t n, TYPE val, TYPE *x)\
{\
size_t i;\
\
for (i=0; i<n; i++)\
x[i] = val;\
\
return x;\
}\
\
\
/*************************************************************************/
\
/*! The macro for gk_?set()-class of routines */
\
/*************************************************************************/
\
TYPE *PRFX ## copy(size_t n, TYPE *a, TYPE *b)\
{\
return (TYPE *)memmove((void *)b, (void *)a, sizeof(TYPE)*n);\
}\
\
\
/*************************************************************************/
\
/*! The macro for gk_?AllocMatrix()-class of routines */
\
/**************************************************************************/
\
TYPE **PRFX ## AllocMatrix(size_t ndim1, size_t ndim2, TYPE value, char *errmsg)\
{\
gk_idx_t i, j;\
TYPE **matrix;\
\
matrix = (TYPE **)gk_malloc(ndim1*sizeof(TYPE *), errmsg);\
if (matrix == NULL) \
return NULL;\
\
for (i=0; i<ndim1; i++) { \
matrix[i] = PRFX ## smalloc(ndim2, value, errmsg);\
if (matrix[i] == NULL) { \
for (j=0; j<i; j++) \
gk_free((void **)&matrix[j], LTERM); \
return NULL; \
} \
}\
\
return matrix;\
}\
\
\
/*************************************************************************/
\
/*! The macro for gk_?AllocMatrix()-class of routines */
\
/**************************************************************************/
\
void PRFX ## FreeMatrix(TYPE ***r_matrix, size_t ndim1, size_t ndim2)\
{\
gk_idx_t i;\
TYPE **matrix;\
\
if (*r_matrix == NULL) \
return; \
\
matrix = *r_matrix;\
\
for (i=0; i<ndim1; i++) \
gk_free((void **)&(matrix[i]), LTERM);\
\
gk_free((void **)r_matrix, LTERM);\
}\
\
\
/*************************************************************************/
\
/*! The macro for gk_?SetMatrix()-class of routines */
\
/**************************************************************************/
\
void PRFX ## SetMatrix(TYPE **matrix, size_t ndim1, size_t ndim2, TYPE value)\
{\
gk_idx_t i, j;\
\
for (i=0; i<ndim1; i++) {\
for (j=0; j<ndim2; j++)\
matrix[i][j] = value;\
}\
}\
#define GK_MKALLOC_PROTO(PRFX, TYPE)\
TYPE *PRFX ## malloc(size_t n, char *msg);\
TYPE *PRFX ## realloc(TYPE *ptr, size_t n, char *msg);\
TYPE *PRFX ## smalloc(size_t n, TYPE ival, char *msg);\
TYPE *PRFX ## set(size_t n, TYPE val, TYPE *x);\
TYPE *PRFX ## copy(size_t n, TYPE *a, TYPE *b);\
TYPE **PRFX ## AllocMatrix(size_t ndim1, size_t ndim2, TYPE value, char *errmsg);\
void PRFX ## FreeMatrix(TYPE ***r_matrix, size_t ndim1, size_t ndim2);\
void PRFX ## SetMatrix(TYPE **matrix, size_t ndim1, size_t ndim2, TYPE value);\
#endif
third_party/METIS/GKlib/gk_mkpqueue.h
0 → 100644
View file @
3359c1f1
/*!
\file gk_mkpqueue.h
\brief Templates for priority queues
\date Started 4/09/07
\author George
\version\verbatim $Id: gk_mkpqueue.h 21742 2018-01-26 16:59:15Z karypis $ \endverbatim
*/
#ifndef _GK_MKPQUEUE_H
#define _GK_MKPQUEUE_H
#define GK_MKPQUEUE(FPRFX, PQT, KVT, KT, VT, KVMALLOC, KMAX, KEY_LT)\
/*************************************************************************/
\
/*! This function creates and initializes a priority queue */
\
/**************************************************************************/
\
PQT *FPRFX ## Create(size_t maxnodes)\
{\
PQT *queue; \
\
queue = (PQT *)gk_malloc(sizeof(PQT), "gk_pqCreate: queue");\
FPRFX ## Init(queue, maxnodes);\
\
return queue;\
}\
\
\
/*************************************************************************/
\
/*! This function initializes the data structures of the priority queue */
\
/**************************************************************************/
\
void FPRFX ## Init(PQT *queue, size_t maxnodes)\
{\
queue->nnodes = 0;\
queue->maxnodes = maxnodes;\
\
queue->heap = KVMALLOC(maxnodes, "gk_PQInit: heap");\
queue->locator = gk_idxsmalloc(maxnodes, -1, "gk_PQInit: locator");\
}\
\
\
/*************************************************************************/
\
/*! This function resets the priority queue */
\
/**************************************************************************/
\
void FPRFX ## Reset(PQT *queue)\
{\
ssize_t i;\
ssize_t *locator=queue->locator;\
KVT *heap=queue->heap;\
\
for (i=queue->nnodes-1; i>=0; i--)\
locator[heap[i].val] = -1;\
queue->nnodes = 0;\
}\
\
\
/*************************************************************************/
\
/*! This function frees the internal datastructures of the priority queue */
\
/**************************************************************************/
\
void FPRFX ## Free(PQT *queue)\
{\
if (queue == NULL) return;\
gk_free((void **)&queue->heap, &queue->locator, LTERM);\
queue->maxnodes = 0;\
}\
\
\
/*************************************************************************/
\
/*! This function frees the internal datastructures of the priority queue \
and the queue itself */
\
/**************************************************************************/
\
void FPRFX ## Destroy(PQT *queue)\
{\
if (queue == NULL) return;\
FPRFX ## Free(queue);\
gk_free((void **)&queue, LTERM);\
}\
\
\
/*************************************************************************/
\
/*! This function returns the length of the queue */
\
/**************************************************************************/
\
size_t FPRFX ## Length(PQT *queue)\
{\
return queue->nnodes;\
}\
\
\
/*************************************************************************/
\
/*! This function adds an item in the priority queue */
\
/**************************************************************************/
\
int FPRFX ## Insert(PQT *queue, VT node, KT key)\
{\
ssize_t i, j;\
ssize_t *locator=queue->locator;\
KVT *heap=queue->heap;\
\
ASSERT2(FPRFX ## CheckHeap(queue));\
\
ASSERT(locator[node] == -1);\
\
i = queue->nnodes++;\
while (i > 0) {\
j = (i-1)>>1;\
if (KEY_LT(key, heap[j].key)) {\
heap[i] = heap[j];\
locator[heap[i].val] = i;\
i = j;\
}\
else\
break;\
}\
ASSERT(i >= 0);\
heap[i].key = key;\
heap[i].val = node;\
locator[node] = i;\
\
ASSERT2(FPRFX ## CheckHeap(queue));\
\
return 0;\
}\
\
\
/*************************************************************************/
\
/*! This function deletes an item from the priority queue */
\
/**************************************************************************/
\
int FPRFX ## Delete(PQT *queue, VT node)\
{\
ssize_t i, j;\
size_t nnodes;\
KT newkey, oldkey;\
ssize_t *locator=queue->locator;\
KVT *heap=queue->heap;\
\
ASSERT(locator[node] != -1);\
ASSERT(heap[locator[node]].val == node);\
\
ASSERT2(FPRFX ## CheckHeap(queue));\
\
i = locator[node];\
locator[node] = -1;\
\
if (--queue->nnodes > 0 && heap[queue->nnodes].val != node) {\
node = heap[queue->nnodes].val;\
newkey = heap[queue->nnodes].key;\
oldkey = heap[i].key;\
\
if (KEY_LT(newkey, oldkey)) {
/* Filter-up */
\
while (i > 0) {\
j = (i-1)>>1;\
if (KEY_LT(newkey, heap[j].key)) {\
heap[i] = heap[j];\
locator[heap[i].val] = i;\
i = j;\
}\
else\
break;\
}\
}\
else {
/* Filter down */
\
nnodes = queue->nnodes;\
while ((j=(i<<1)+1) < nnodes) {\
if (KEY_LT(heap[j].key, newkey)) {\
if (j+1 < nnodes && KEY_LT(heap[j+1].key, heap[j].key))\
j++;\
heap[i] = heap[j];\
locator[heap[i].val] = i;\
i = j;\
}\
else if (j+1 < nnodes && KEY_LT(heap[j+1].key, newkey)) {\
j++;\
heap[i] = heap[j];\
locator[heap[i].val] = i;\
i = j;\
}\
else\
break;\
}\
}\
\
heap[i].key = newkey;\
heap[i].val = node;\
locator[node] = i;\
}\
\
ASSERT2(FPRFX ## CheckHeap(queue));\
\
return 0;\
}\
\
\
/*************************************************************************/
\
/*! This function updates the key values associated for a particular item */
\
/**************************************************************************/
\
void FPRFX ## Update(PQT *queue, VT node, KT newkey)\
{\
ssize_t i, j;\
size_t nnodes;\
KT oldkey;\
ssize_t *locator=queue->locator;\
KVT *heap=queue->heap;\
\
oldkey = heap[locator[node]].key;\
if (!KEY_LT(newkey, oldkey) && !KEY_LT(oldkey, newkey)) return;\
\
ASSERT(locator[node] != -1);\
ASSERT(heap[locator[node]].val == node);\
ASSERT2(FPRFX ## CheckHeap(queue));\
\
i = locator[node];\
\
if (KEY_LT(newkey, oldkey)) {
/* Filter-up */
\
while (i > 0) {\
j = (i-1)>>1;\
if (KEY_LT(newkey, heap[j].key)) {\
heap[i] = heap[j];\
locator[heap[i].val] = i;\
i = j;\
}\
else\
break;\
}\
}\
else {
/* Filter down */
\
nnodes = queue->nnodes;\
while ((j=(i<<1)+1) < nnodes) {\
if (KEY_LT(heap[j].key, newkey)) {\
if (j+1 < nnodes && KEY_LT(heap[j+1].key, heap[j].key))\
j++;\
heap[i] = heap[j];\
locator[heap[i].val] = i;\
i = j;\
}\
else if (j+1 < nnodes && KEY_LT(heap[j+1].key, newkey)) {\
j++;\
heap[i] = heap[j];\
locator[heap[i].val] = i;\
i = j;\
}\
else\
break;\
}\
}\
\
heap[i].key = newkey;\
heap[i].val = node;\
locator[node] = i;\
\
ASSERT2(FPRFX ## CheckHeap(queue));\
\
return;\
}\
\
\
/*************************************************************************/
\
/*! This function returns the item at the top of the queue and removes\
it from the priority queue */
\
/**************************************************************************/
\
VT FPRFX ## GetTop(PQT *queue)\
{\
ssize_t i, j;\
ssize_t *locator;\
KVT *heap;\
VT vtx, node;\
KT key;\
\
ASSERT2(FPRFX ## CheckHeap(queue));\
\
if (queue->nnodes == 0)\
return -1;\
\
queue->nnodes--;\
\
heap = queue->heap;\
locator = queue->locator;\
\
vtx = heap[0].val;\
locator[vtx] = -1;\
\
if ((i = queue->nnodes) > 0) {\
key = heap[i].key;\
node = heap[i].val;\
i = 0;\
while ((j=2*i+1) < queue->nnodes) {\
if (KEY_LT(heap[j].key, key)) {\
if (j+1 < queue->nnodes && KEY_LT(heap[j+1].key, heap[j].key))\
j = j+1;\
heap[i] = heap[j];\
locator[heap[i].val] = i;\
i = j;\
}\
else if (j+1 < queue->nnodes && KEY_LT(heap[j+1].key, key)) {\
j = j+1;\
heap[i] = heap[j];\
locator[heap[i].val] = i;\
i = j;\
}\
else\
break;\
}\
\
heap[i].key = key;\
heap[i].val = node;\
locator[node] = i;\
}\
\
ASSERT2(FPRFX ## CheckHeap(queue));\
return vtx;\
}\
\
\
/*************************************************************************/
\
/*! This function returns the item at the top of the queue. The item is not\
deleted from the queue. */
\
/**************************************************************************/
\
VT FPRFX ## SeeTopVal(PQT *queue)\
{\
return (queue->nnodes == 0 ? -1 : queue->heap[0].val);\
}\
\
\
/*************************************************************************/
\
/*! This function returns the key of the top item. The item is not\
deleted from the queue. */
\
/**************************************************************************/
\
KT FPRFX ## SeeTopKey(PQT *queue)\
{\
return (queue->nnodes == 0 ? KMAX : queue->heap[0].key);\
}\
\
\
/*************************************************************************/
\
/*! This function returns the key of a specific item */
\
/**************************************************************************/
\
KT FPRFX ## SeeKey(PQT *queue, VT node)\
{\
ssize_t *locator;\
KVT *heap;\
\
heap = queue->heap;\
locator = queue->locator;\
\
return heap[locator[node]].key;\
}\
\
\
/*************************************************************************/
\
/*! This function returns the first item in a breadth-first traversal of\
the heap whose key is less than maxwgt. This function is here due to\
hMETIS and is not general!*/
\
/**************************************************************************/
\
/*\
VT FPRFX ## SeeConstraintTop(PQT *queue, KT maxwgt, KT *wgts)\
{\
ssize_t i;\
\
if (queue->nnodes == 0)\
return -1;\
\
if (maxwgt <= 1000)\
return FPRFX ## SeeTopVal(queue);\
\
for (i=0; i<queue->nnodes; i++) {\
if (queue->heap[i].key > 0) {\
if (wgts[queue->heap[i].val] <= maxwgt)\
return queue->heap[i].val;\
}\
else {\
if (queue->heap[i/2].key <= 0)\
break;\
}\
}\
\
return queue->heap[0].val;\
\
}\
*/
\
\
\
/*************************************************************************/
\
/*! This functions checks the consistency of the heap */
\
/**************************************************************************/
\
int FPRFX ## CheckHeap(PQT *queue)\
{\
ssize_t i, j;\
size_t nnodes;\
ssize_t *locator;\
KVT *heap;\
\
heap = queue->heap;\
locator = queue->locator;\
nnodes = queue->nnodes;\
\
if (nnodes == 0)\
return 1;\
\
ASSERT(locator[heap[0].val] == 0);\
for (i=1; i<nnodes; i++) {\
ASSERT(locator[heap[i].val] == i);\
ASSERT(!KEY_LT(heap[i].key, heap[(i-1)/2].key));\
}\
for (i=1; i<nnodes; i++)\
ASSERT(!KEY_LT(heap[i].key, heap[0].key));\
\
for (j=i=0; i<queue->maxnodes; i++) {\
if (locator[i] != -1)\
j++;\
}\
ASSERTP(j == nnodes, ("%jd %jd\n", (intmax_t)j, (intmax_t)nnodes));\
\
return 1;\
}\
#define GK_MKPQUEUE_PROTO(FPRFX, PQT, KT, VT)\
PQT * FPRFX ## Create(size_t maxnodes);\
void FPRFX ## Init(PQT *queue, size_t maxnodes);\
void FPRFX ## Reset(PQT *queue);\
void FPRFX ## Free(PQT *queue);\
void FPRFX ## Destroy(PQT *queue);\
size_t FPRFX ## Length(PQT *queue);\
int FPRFX ## Insert(PQT *queue, VT node, KT key);\
int FPRFX ## Delete(PQT *queue, VT node);\
void FPRFX ## Update(PQT *queue, VT node, KT newkey);\
VT FPRFX ## GetTop(PQT *queue);\
VT FPRFX ## SeeTopVal(PQT *queue);\
KT FPRFX ## SeeTopKey(PQT *queue);\
KT FPRFX ## SeeKey(PQT *queue, VT node);\
VT FPRFX ## SeeConstraintTop(PQT *queue, KT maxwgt, KT *wgts);\
int FPRFX ## CheckHeap(PQT *queue);\
/* This is how these macros are used
GK_MKPQUEUE(gk_dkvPQ, gk_dkvPQ_t, double, gk_idx_t, gk_dkvmalloc, DBL_MAX)
GK_MKPQUEUE_PROTO(gk_dkvPQ, gk_dkvPQ_t, double, gk_idx_t)
*/
#endif
third_party/METIS/GKlib/gk_mkpqueue2.h
0 → 100644
View file @
3359c1f1
/*!
\file gk_mkpqueue2.h
\brief Templates for priority queues that do not utilize locators and as such
they can use different types of values.
\date Started 4/09/07
\author George
\version\verbatim $Id: gk_mkpqueue2.h 13005 2012-10-23 22:34:36Z karypis $ \endverbatim
*/
#ifndef _GK_MKPQUEUE2_H
#define _GK_MKPQUEUE2_H
#define GK_MKPQUEUE2(FPRFX, PQT, KT, VT, KMALLOC, VMALLOC, KMAX, KEY_LT)\
/*************************************************************************/
\
/*! This function creates and initializes a priority queue */
\
/**************************************************************************/
\
PQT *FPRFX ## Create2(ssize_t maxnodes)\
{\
PQT *queue; \
\
if ((queue = (PQT *)gk_malloc(sizeof(PQT), "gk_pqCreate2: queue")) != NULL) {\
memset(queue, 0, sizeof(PQT));\
queue->nnodes = 0;\
queue->maxnodes = maxnodes;\
queue->keys = KMALLOC(maxnodes, "gk_pqCreate2: keys");\
queue->vals = VMALLOC(maxnodes, "gk_pqCreate2: vals");\
\
if (queue->keys == NULL || queue->vals == NULL)\
gk_free((void **)&queue->keys, &queue->vals, &queue, LTERM);\
}\
\
return queue;\
}\
\
\
/*************************************************************************/
\
/*! This function resets the priority queue */
\
/**************************************************************************/
\
void FPRFX ## Reset2(PQT *queue)\
{\
queue->nnodes = 0;\
}\
\
\
/*************************************************************************/
\
/*! This function frees the internal datastructures of the priority queue */
\
/**************************************************************************/
\
void FPRFX ## Destroy2(PQT **r_queue)\
{\
PQT *queue = *r_queue; \
if (queue == NULL) return;\
gk_free((void **)&queue->keys, &queue->vals, &queue, LTERM);\
*r_queue = NULL;\
}\
\
\
/*************************************************************************/
\
/*! This function returns the length of the queue */
\
/**************************************************************************/
\
size_t FPRFX ## Length2(PQT *queue)\
{\
return queue->nnodes;\
}\
\
\
/*************************************************************************/
\
/*! This function adds an item in the priority queue. */
\
/**************************************************************************/
\
int FPRFX ## Insert2(PQT *queue, VT val, KT key)\
{\
ssize_t i, j;\
KT *keys=queue->keys;\
VT *vals=queue->vals;\
\
ASSERT2(FPRFX ## CheckHeap2(queue));\
\
if (queue->nnodes == queue->maxnodes) \
return 0;\
\
ASSERT2(FPRFX ## CheckHeap2(queue));\
\
i = queue->nnodes++;\
while (i > 0) {\
j = (i-1)>>1;\
if (KEY_LT(key, keys[j])) {\
keys[i] = keys[j];\
vals[i] = vals[j];\
i = j;\
}\
else\
break;\
}\
ASSERT(i >= 0);\
keys[i] = key;\
vals[i] = val;\
\
ASSERT2(FPRFX ## CheckHeap2(queue));\
\
return 1;\
}\
\
\
/*************************************************************************/
\
/*! This function returns the item at the top of the queue and removes\
it from the priority queue */
\
/**************************************************************************/
\
int FPRFX ## GetTop2(PQT *queue, VT *r_val)\
{\
ssize_t i, j;\
KT key, *keys=queue->keys;\
VT val, *vals=queue->vals;\
\
ASSERT2(FPRFX ## CheckHeap2(queue));\
\
if (queue->nnodes == 0)\
return 0;\
\
queue->nnodes--;\
\
*r_val = vals[0];\
\
if ((i = queue->nnodes) > 0) {\
key = keys[i];\
val = vals[i];\
i = 0;\
while ((j=2*i+1) < queue->nnodes) {\
if (KEY_LT(keys[j], key)) {\
if (j+1 < queue->nnodes && KEY_LT(keys[j+1], keys[j]))\
j = j+1;\
keys[i] = keys[j];\
vals[i] = vals[j];\
i = j;\
}\
else if (j+1 < queue->nnodes && KEY_LT(keys[j+1], key)) {\
j = j+1;\
keys[i] = keys[j];\
vals[i] = vals[j];\
i = j;\
}\
else\
break;\
}\
\
keys[i] = key;\
vals[i] = val;\
}\
\
ASSERT2(FPRFX ## CheckHeap2(queue));\
\
return 1;\
}\
\
\
/*************************************************************************/
\
/*! This function returns the item at the top of the queue. The item is not\
deleted from the queue. */
\
/**************************************************************************/
\
int FPRFX ## SeeTopVal2(PQT *queue, VT *r_val)\
{\
if (queue->nnodes == 0) \
return 0;\
\
*r_val = queue->vals[0];\
\
return 1;\
}\
\
\
/*************************************************************************/
\
/*! This function returns the key of the top item. The item is not\
deleted from the queue. */
\
/**************************************************************************/
\
KT FPRFX ## SeeTopKey2(PQT *queue)\
{\
return (queue->nnodes == 0 ? KMAX : queue->keys[0]);\
}\
\
\
/*************************************************************************/
\
/*! This functions checks the consistency of the heap */
\
/**************************************************************************/
\
int FPRFX ## CheckHeap2(PQT *queue)\
{\
ssize_t i;\
KT *keys=queue->keys;\
\
if (queue->nnodes == 0)\
return 1;\
\
for (i=1; i<queue->nnodes; i++) {\
ASSERT(!KEY_LT(keys[i], keys[(i-1)/2]));\
}\
for (i=1; i<queue->nnodes; i++)\
ASSERT(!KEY_LT(keys[i], keys[0]));\
\
return 1;\
}\
#define GK_MKPQUEUE2_PROTO(FPRFX, PQT, KT, VT)\
PQT * FPRFX ## Create2(ssize_t maxnodes);\
void FPRFX ## Reset2(PQT *queue);\
void FPRFX ## Destroy2(PQT **r_queue);\
size_t FPRFX ## Length2(PQT *queue);\
int FPRFX ## Insert2(PQT *queue, VT node, KT key);\
int FPRFX ## GetTop2(PQT *queue, VT *r_val);\
int FPRFX ## SeeTopVal2(PQT *queue, VT *r_val);\
KT FPRFX ## SeeTopKey2(PQT *queue);\
int FPRFX ## CheckHeap2(PQT *queue);\
#endif
third_party/METIS/GKlib/gk_mkrandom.h
0 → 100644
View file @
3359c1f1
/*!
\file
\brief Templates for portable random number generation
\date Started 5/17/07
\author George
\version\verbatim $Id: gk_mkrandom.h 10711 2011-08-31 22:23:04Z karypis $ \endverbatim
*/
#ifndef _GK_MKRANDOM_H
#define _GK_MKRANDOM_H
/*************************************************************************/
\
/*! The generator for the rand() related routines. \
\params RNGT the datatype that defines the range of values over which\
random numbers will be generated\
\params VALT the datatype that defines the contents of the array to \
be permuted by randArrayPermute() \
\params FPRFX the function prefix \
*/
\
/**************************************************************************/
\
#define GK_MKRANDOM(FPRFX, RNGT, VALT)\
/*************************************************************************/
\
/*! Initializes the generator */
\
/**************************************************************************/
\
void FPRFX ## srand(RNGT seed) \
{\
gk_randinit((uint64_t) seed);\
}\
\
\
/*************************************************************************/
\
/*! Returns a random number */
\
/**************************************************************************/
\
RNGT FPRFX ## rand() \
{\
if (sizeof(RNGT) <= sizeof(int32_t)) \
return (RNGT)gk_randint32(); \
else \
return (RNGT)gk_randint64(); \
}\
\
\
/*************************************************************************/
\
/*! Returns a random number between [0, max) */
\
/**************************************************************************/
\
RNGT FPRFX ## randInRange(RNGT max) \
{\
return (RNGT)((FPRFX ## rand())%max); \
}\
\
\
/*************************************************************************/
\
/*! Randomly permutes the elements of an array p[]. \
flag == 1, p[i] = i prior to permutation, \
flag == 0, p[] is not initialized. */
\
/**************************************************************************/
\
void FPRFX ## randArrayPermute(RNGT n, VALT *p, RNGT nshuffles, int flag)\
{\
RNGT i, u, v;\
VALT tmp;\
\
if (flag == 1) {\
for (i=0; i<n; i++)\
p[i] = (VALT)i;\
}\
\
if (n < 10) {\
for (i=0; i<n; i++) {\
v = FPRFX ## randInRange(n);\
u = FPRFX ## randInRange(n);\
gk_SWAP(p[v], p[u], tmp);\
}\
}\
else {\
for (i=0; i<nshuffles; i++) {\
v = FPRFX ## randInRange(n-3);\
u = FPRFX ## randInRange(n-3);\
/*gk_SWAP(p[v+0], p[u+0], tmp);*/
\
/*gk_SWAP(p[v+1], p[u+1], tmp);*/
\
/*gk_SWAP(p[v+2], p[u+2], tmp);*/
\
/*gk_SWAP(p[v+3], p[u+3], tmp);*/
\
gk_SWAP(p[v+0], p[u+2], tmp);\
gk_SWAP(p[v+1], p[u+3], tmp);\
gk_SWAP(p[v+2], p[u+0], tmp);\
gk_SWAP(p[v+3], p[u+1], tmp);\
}\
}\
}\
\
\
/*************************************************************************/
\
/*! Randomly permutes the elements of an array p[]. \
flag == 1, p[i] = i prior to permutation, \
flag == 0, p[] is not initialized. */
\
/**************************************************************************/
\
void FPRFX ## randArrayPermuteFine(RNGT n, VALT *p, int flag)\
{\
RNGT i, v;\
VALT tmp;\
\
if (flag == 1) {\
for (i=0; i<n; i++)\
p[i] = (VALT)i;\
}\
\
for (i=0; i<n; i++) {\
v = FPRFX ## randInRange(n);\
gk_SWAP(p[i], p[v], tmp);\
}\
}\
#define GK_MKRANDOM_PROTO(FPRFX, RNGT, VALT)\
void FPRFX ## srand(RNGT seed); \
RNGT FPRFX ## rand(); \
RNGT FPRFX ## randInRange(RNGT max); \
void FPRFX ## randArrayPermute(RNGT n, VALT *p, RNGT nshuffles, int flag);\
void FPRFX ## randArrayPermuteFine(RNGT n, VALT *p, int flag);\
#endif
third_party/METIS/GKlib/gk_mksort.h
0 → 100644
View file @
3359c1f1
/*!
\file gk_mksort.h
\brief Templates for the qsort routine
\date Started 3/28/07
\author George
\version\verbatim $Id: gk_mksort.h 21051 2017-05-25 04:36:14Z karypis $ \endverbatim
*/
#ifndef _GK_MKSORT_H_
#define _GK_MKSORT_H_
/* Adopted from GNU glibc by Mjt.
* See stdlib/qsort.c in glibc */
/* Copyright (C) 1991, 1992, 1996, 1997, 1999 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Written by Douglas C. Schmidt (schmidt@ics.uci.edu).
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA. */
/* in-line qsort implementation. Differs from traditional qsort() routine
* in that it is a macro, not a function, and instead of passing an address
* of a comparision routine to the function, it is possible to inline
* comparision routine, thus speed up sorting alot.
*
* Usage:
* #include "iqsort.h"
* #define islt(a,b) (strcmp((*a),(*b))<0)
* char *arr[];
* int n;
* GKQSORT(char*, arr, n, islt);
*
* The "prototype" and 4 arguments are:
* GKQSORT(TYPE,BASE,NELT,ISLT)
* 1) type of each element, TYPE,
* 2) address of the beginning of the array, of type TYPE*,
* 3) number of elements in the array, and
* 4) comparision routine.
* Array pointer and number of elements are referenced only once.
* This is similar to a call
* qsort(BASE,NELT,sizeof(TYPE),ISLT)
* with the difference in last parameter.
* Note the islt macro/routine (it receives pointers to two elements):
* the only condition of interest is whenever one element is less than
* another, no other conditions (greather than, equal to etc) are tested.
* So, for example, to define integer sort, use:
* #define islt(a,b) ((*a)<(*b))
* GKQSORT(int, arr, n, islt)
*
* The macro could be used to implement a sorting function (see examples
* below), or to implement the sorting algorithm inline. That is, either
* create a sorting function and use it whenever you want to sort something,
* or use GKQSORT() macro directly instead a call to such routine. Note that
* the macro expands to quite some code (compiled size of int qsort on x86
* is about 700..800 bytes).
*
* Using this macro directly it isn't possible to implement traditional
* qsort() routine, because the macro assumes sizeof(element) == sizeof(TYPE),
* while qsort() allows element size to be different.
*
* Several ready-to-use examples:
*
* Sorting array of integers:
* void int_qsort(int *arr, unsigned n) {
* #define int_lt(a,b) ((*a)<(*b))
* GKQSORT(int, arr, n, int_lt);
* }
*
* Sorting array of string pointers:
* void str_qsort(char *arr[], unsigned n) {
* #define str_lt(a,b) (strcmp((*a),(*b)) < 0)
* GKQSORT(char*, arr, n, str_lt);
* }
*
* Sorting array of structures:
*
* struct elt {
* int key;
* ...
* };
* void elt_qsort(struct elt *arr, unsigned n) {
* #define elt_lt(a,b) ((a)->key < (b)->key)
* GKQSORT(struct elt, arr, n, elt_lt);
* }
*
* And so on.
*/
/* Swap two items pointed to by A and B using temporary buffer t. */
#define _GKQSORT_SWAP(a, b, t) ((void)((t = *a), (*a = *b), (*b = t)))
/* Discontinue quicksort algorithm when partition gets below this size. */
#define _GKQSORT_MAX_THRESH 8
/* The next 4 #defines implement a very fast in-line stack abstraction. */
#define _GKQSORT_STACK_SIZE (8 * sizeof(size_t))
#define _GKQSORT_PUSH(top, low, high) (((top->_lo = (low)), (top->_hi = (high)), ++top))
#define _GKQSORT_POP(low, high, top) ((--top, (low = top->_lo), (high = top->_hi)))
#define _GKQSORT_STACK_NOT_EMPTY (_stack < _top)
/* The main code starts here... */
#define GK_MKQSORT(GKQSORT_TYPE,GKQSORT_BASE,GKQSORT_NELT,GKQSORT_LT) \
{ \
GKQSORT_TYPE *const _base = (GKQSORT_BASE); \
const size_t _elems = (GKQSORT_NELT); \
GKQSORT_TYPE _hold; \
\
if (_elems < 1) \
return; \
\
/* Don't declare two variables of type GKQSORT_TYPE in a single \
* statement: eg `TYPE a, b;', in case if TYPE is a pointer, \
* expands to `type* a, b;' wich isn't what we want. \
*/
\
\
if (_elems > _GKQSORT_MAX_THRESH) { \
GKQSORT_TYPE *_lo = _base; \
GKQSORT_TYPE *_hi = _lo + _elems - 1; \
struct { \
GKQSORT_TYPE *_hi; GKQSORT_TYPE *_lo; \
} _stack[_GKQSORT_STACK_SIZE], *_top = _stack + 1; \
\
while (_GKQSORT_STACK_NOT_EMPTY) { \
GKQSORT_TYPE *_left_ptr; GKQSORT_TYPE *_right_ptr; \
\
/* Select median value from among LO, MID, and HI. Rearrange \
LO and HI so the three values are sorted. This lowers the \
probability of picking a pathological pivot value and \
skips a comparison for both the LEFT_PTR and RIGHT_PTR in \
the while loops. */
\
\
GKQSORT_TYPE *_mid = _lo + ((_hi - _lo) >> 1); \
\
if (GKQSORT_LT (_mid, _lo)) \
_GKQSORT_SWAP (_mid, _lo, _hold); \
if (GKQSORT_LT (_hi, _mid)) \
_GKQSORT_SWAP (_mid, _hi, _hold); \
else \
goto _jump_over; \
if (GKQSORT_LT (_mid, _lo)) \
_GKQSORT_SWAP (_mid, _lo, _hold); \
_jump_over:; \
\
_left_ptr = _lo + 1; \
_right_ptr = _hi - 1; \
\
/* Here's the famous ``collapse the walls'' section of quicksort. \
Gotta like those tight inner loops! They are the main reason \
that this algorithm runs much faster than others. */
\
do { \
while (GKQSORT_LT (_left_ptr, _mid)) \
++_left_ptr; \
\
while (GKQSORT_LT (_mid, _right_ptr)) \
--_right_ptr; \
\
if (_left_ptr < _right_ptr) { \
_GKQSORT_SWAP (_left_ptr, _right_ptr, _hold); \
if (_mid == _left_ptr) \
_mid = _right_ptr; \
else if (_mid == _right_ptr) \
_mid = _left_ptr; \
++_left_ptr; \
--_right_ptr; \
} \
else if (_left_ptr == _right_ptr) { \
++_left_ptr; \
--_right_ptr; \
break; \
} \
} while (_left_ptr <= _right_ptr); \
\
/* Set up pointers for next iteration. First determine whether \
left and right partitions are below the threshold size. If so, \
ignore one or both. Otherwise, push the larger partition's \
bounds on the stack and continue sorting the smaller one. */
\
\
if (_right_ptr - _lo <= _GKQSORT_MAX_THRESH) { \
if (_hi - _left_ptr <= _GKQSORT_MAX_THRESH) \
/* Ignore both small partitions. */
\
_GKQSORT_POP (_lo, _hi, _top); \
else \
/* Ignore small left partition. */
\
_lo = _left_ptr; \
} \
else if (_hi - _left_ptr <= _GKQSORT_MAX_THRESH) \
/* Ignore small right partition. */
\
_hi = _right_ptr; \
else if (_right_ptr - _lo > _hi - _left_ptr) { \
/* Push larger left partition indices. */
\
_GKQSORT_PUSH (_top, _lo, _right_ptr); \
_lo = _left_ptr; \
} \
else { \
/* Push larger right partition indices. */
\
_GKQSORT_PUSH (_top, _left_ptr, _hi); \
_hi = _right_ptr; \
} \
} \
} \
\
/* Once the BASE array is partially sorted by quicksort the rest \
is completely sorted using insertion sort, since this is efficient \
for partitions below MAX_THRESH size. BASE points to the \
beginning of the array to sort, and END_PTR points at the very \
last element in the array (*not* one beyond it!). */
\
\
{ \
GKQSORT_TYPE *const _end_ptr = _base + _elems - 1; \
GKQSORT_TYPE *_tmp_ptr = _base; \
register GKQSORT_TYPE *_run_ptr; \
GKQSORT_TYPE *_thresh; \
\
_thresh = _base + _GKQSORT_MAX_THRESH; \
if (_thresh > _end_ptr) \
_thresh = _end_ptr; \
\
/* Find smallest element in first threshold and place it at the \
array's beginning. This is the smallest array element, \
and the operation speeds up insertion sort's inner loop. */
\
\
for (_run_ptr = _tmp_ptr + 1; _run_ptr <= _thresh; ++_run_ptr) \
if (GKQSORT_LT (_run_ptr, _tmp_ptr)) \
_tmp_ptr = _run_ptr; \
\
if (_tmp_ptr != _base) \
_GKQSORT_SWAP (_tmp_ptr, _base, _hold); \
\
/* Insertion sort, running from left-hand-side \
* up to right-hand-side. */
\
\
_run_ptr = _base + 1; \
while (++_run_ptr <= _end_ptr) { \
_tmp_ptr = _run_ptr - 1; \
while (GKQSORT_LT (_run_ptr, _tmp_ptr)) \
--_tmp_ptr; \
\
++_tmp_ptr; \
if (_tmp_ptr != _run_ptr) { \
GKQSORT_TYPE *_trav = _run_ptr + 1; \
while (--_trav >= _run_ptr) { \
GKQSORT_TYPE *_hi; GKQSORT_TYPE *_lo; \
_hold = *_trav; \
\
for (_hi = _lo = _trav; --_lo >= _tmp_ptr; _hi = _lo) \
*_hi = *_lo; \
*_hi = _hold; \
} \
} \
} \
} \
\
}
#endif
third_party/METIS/GKlib/gk_mkutils.h
0 → 100644
View file @
3359c1f1
/*!
\file
\brief Templates for various utility routines
\date Started 5/28/07
\author George
\version\verbatim $Id: gk_mkutils.h 10711 2011-08-31 22:23:04Z karypis $ \endverbatim
*/
#ifndef _GK_MKUTILS_H_
#define _GK_MKUTILS_H_
#define GK_MKARRAY2CSR(PRFX, TYPE)\
/*************************************************************************/
\
/*! The macro for gk_?array2csr() routine */
\
/**************************************************************************/
\
void PRFX ## array2csr(TYPE n, TYPE range, TYPE *array, TYPE *ptr, TYPE *ind)\
{\
TYPE i;\
\
for (i=0; i<=range; i++)\
ptr[i] = 0;\
\
for (i=0; i<n; i++)\
ptr[array[i]]++;\
\
/* Compute the ptr, ind structure */
\
MAKECSR(i, range, ptr);\
for (i=0; i<n; i++)\
ind[ptr[array[i]]++] = i;\
SHIFTCSR(i, range, ptr);\
}
#define GK_MKARRAY2CSR_PROTO(PRFX, TYPE)\
void PRFX ## array2csr(TYPE n, TYPE range, TYPE *array, TYPE *ptr, TYPE *ind);\
#endif
third_party/METIS/GKlib/gk_proto.h
0 → 100644
View file @
3359c1f1
/*!
\file gk_proto.h
\brief This file contains function prototypes
\date Started 3/27/2007
\author George
\version\verbatim $Id: gk_proto.h 21050 2017-05-25 03:53:58Z karypis $ \endverbatim
*/
#ifndef _GK_PROTO_H_
#define _GK_PROTO_H_
#ifdef __cplusplus
extern
"C"
{
#endif
/*-------------------------------------------------------------
* blas.c
*-------------------------------------------------------------*/
GK_MKBLAS_PROTO
(
gk_c
,
char
,
int
)
GK_MKBLAS_PROTO
(
gk_i
,
int
,
int
)
GK_MKBLAS_PROTO
(
gk_i8
,
int8_t
,
int8_t
)
GK_MKBLAS_PROTO
(
gk_i16
,
int16_t
,
int16_t
)
GK_MKBLAS_PROTO
(
gk_i32
,
int32_t
,
int32_t
)
GK_MKBLAS_PROTO
(
gk_i64
,
int64_t
,
int64_t
)
GK_MKBLAS_PROTO
(
gk_z
,
ssize_t
,
ssize_t
)
GK_MKBLAS_PROTO
(
gk_zu
,
size_t
,
size_t
)
GK_MKBLAS_PROTO
(
gk_f
,
float
,
float
)
GK_MKBLAS_PROTO
(
gk_d
,
double
,
double
)
GK_MKBLAS_PROTO
(
gk_idx
,
gk_idx_t
,
gk_idx_t
)
/*-------------------------------------------------------------
* io.c
*-------------------------------------------------------------*/
FILE
*
gk_fopen
(
char
*
,
char
*
,
const
char
*
);
void
gk_fclose
(
FILE
*
);
ssize_t
gk_read
(
int
fd
,
void
*
vbuf
,
size_t
count
);
ssize_t
gk_write
(
int
fd
,
void
*
vbuf
,
size_t
count
);
gk_idx_t
gk_getline
(
char
**
lineptr
,
size_t
*
n
,
FILE
*
stream
);
char
**
gk_readfile
(
char
*
fname
,
size_t
*
r_nlines
);
int32_t
*
gk_i32readfile
(
char
*
fname
,
size_t
*
r_nlines
);
int64_t
*
gk_i64readfile
(
char
*
fname
,
size_t
*
r_nlines
);
ssize_t
*
gk_zreadfile
(
char
*
fname
,
size_t
*
r_nlines
);
int32_t
*
gk_i32readfilebin
(
char
*
fname
,
size_t
*
r_nelmnts
);
size_t
gk_i32writefilebin
(
char
*
fname
,
size_t
n
,
int32_t
*
a
);
int64_t
*
gk_i64readfilebin
(
char
*
fname
,
size_t
*
r_nelmnts
);
size_t
gk_i64writefilebin
(
char
*
fname
,
size_t
n
,
int64_t
*
a
);
ssize_t
*
gk_zreadfilebin
(
char
*
fname
,
size_t
*
r_nelmnts
);
size_t
gk_zwritefilebin
(
char
*
fname
,
size_t
n
,
ssize_t
*
a
);
float
*
gk_freadfilebin
(
char
*
fname
,
size_t
*
r_nelmnts
);
size_t
gk_fwritefilebin
(
char
*
fname
,
size_t
n
,
float
*
a
);
double
*
gk_dreadfilebin
(
char
*
fname
,
size_t
*
r_nelmnts
);
size_t
gk_dwritefilebin
(
char
*
fname
,
size_t
n
,
double
*
a
);
/*-------------------------------------------------------------
* fs.c
*-------------------------------------------------------------*/
int
gk_fexists
(
char
*
);
int
gk_dexists
(
char
*
);
ssize_t
gk_getfsize
(
char
*
);
void
gk_getfilestats
(
char
*
fname
,
size_t
*
r_nlines
,
size_t
*
r_ntokens
,
size_t
*
r_max_nlntokens
,
size_t
*
r_nbytes
);
char
*
gk_getbasename
(
char
*
path
);
char
*
gk_getextname
(
char
*
path
);
char
*
gk_getfilename
(
char
*
path
);
char
*
gk_getpathname
(
char
*
path
);
int
gk_mkpath
(
char
*
);
int
gk_rmpath
(
char
*
);
/*-------------------------------------------------------------
* memory.c
*-------------------------------------------------------------*/
GK_MKALLOC_PROTO
(
gk_c
,
char
)
GK_MKALLOC_PROTO
(
gk_i
,
int
)
GK_MKALLOC_PROTO
(
gk_i8
,
int8_t
)
GK_MKALLOC_PROTO
(
gk_i16
,
int16_t
)
GK_MKALLOC_PROTO
(
gk_i32
,
int32_t
)
GK_MKALLOC_PROTO
(
gk_i64
,
int64_t
)
GK_MKALLOC_PROTO
(
gk_ui8
,
uint8_t
)
GK_MKALLOC_PROTO
(
gk_ui16
,
uint16_t
)
GK_MKALLOC_PROTO
(
gk_ui32
,
uint32_t
)
GK_MKALLOC_PROTO
(
gk_ui64
,
uint64_t
)
GK_MKALLOC_PROTO
(
gk_z
,
ssize_t
)
GK_MKALLOC_PROTO
(
gk_zu
,
size_t
)
GK_MKALLOC_PROTO
(
gk_f
,
float
)
GK_MKALLOC_PROTO
(
gk_d
,
double
)
GK_MKALLOC_PROTO
(
gk_idx
,
gk_idx_t
)
GK_MKALLOC_PROTO
(
gk_ckv
,
gk_ckv_t
)
GK_MKALLOC_PROTO
(
gk_ikv
,
gk_ikv_t
)
GK_MKALLOC_PROTO
(
gk_i8kv
,
gk_i8kv_t
)
GK_MKALLOC_PROTO
(
gk_i16kv
,
gk_i16kv_t
)
GK_MKALLOC_PROTO
(
gk_i32kv
,
gk_i32kv_t
)
GK_MKALLOC_PROTO
(
gk_i64kv
,
gk_i64kv_t
)
GK_MKALLOC_PROTO
(
gk_zkv
,
gk_zkv_t
)
GK_MKALLOC_PROTO
(
gk_zukv
,
gk_zukv_t
)
GK_MKALLOC_PROTO
(
gk_fkv
,
gk_fkv_t
)
GK_MKALLOC_PROTO
(
gk_dkv
,
gk_dkv_t
)
GK_MKALLOC_PROTO
(
gk_skv
,
gk_skv_t
)
GK_MKALLOC_PROTO
(
gk_idxkv
,
gk_idxkv_t
)
void
gk_AllocMatrix
(
void
***
,
size_t
,
size_t
,
size_t
);
void
gk_FreeMatrix
(
void
***
,
size_t
,
size_t
);
int
gk_malloc_init
();
void
gk_malloc_cleanup
(
int
showstats
);
void
*
gk_malloc
(
size_t
nbytes
,
char
*
msg
);
void
*
gk_realloc
(
void
*
oldptr
,
size_t
nbytes
,
char
*
msg
);
void
gk_free
(
void
**
ptr1
,...);
size_t
gk_GetCurMemoryUsed
();
size_t
gk_GetMaxMemoryUsed
();
void
gk_GetVMInfo
(
size_t
*
vmsize
,
size_t
*
vmrss
);
/*-------------------------------------------------------------
* seq.c
*-------------------------------------------------------------*/
gk_seq_t
*
gk_seq_ReadGKMODPSSM
(
char
*
file_name
);
gk_i2cc2i_t
*
gk_i2cc2i_create_common
(
char
*
alphabet
);
void
gk_seq_init
(
gk_seq_t
*
seq
);
/*-------------------------------------------------------------
* error.c
*-------------------------------------------------------------*/
void
gk_set_exit_on_error
(
int
value
);
void
errexit
(
char
*
,...);
void
gk_errexit
(
int
signum
,
char
*
,...);
int
gk_sigtrap
();
int
gk_siguntrap
();
void
gk_sigthrow
(
int
signum
);
void
gk_SetSignalHandlers
();
void
gk_UnsetSignalHandlers
();
void
gk_NonLocalExit_Handler
(
int
signum
);
char
*
gk_strerror
(
int
errnum
);
void
PrintBackTrace
();
/*-------------------------------------------------------------
* util.c
*-------------------------------------------------------------*/
void
gk_RandomPermute
(
size_t
,
int
*
,
int
);
void
gk_array2csr
(
size_t
n
,
size_t
range
,
int
*
array
,
int
*
ptr
,
int
*
ind
);
int
gk_log2
(
int
);
int
gk_ispow2
(
int
);
float
gk_flog2
(
float
);
/*-------------------------------------------------------------
* time.c
*-------------------------------------------------------------*/
gk_wclock_t
gk_WClockSeconds
(
void
);
double
gk_CPUSeconds
(
void
);
/*-------------------------------------------------------------
* string.c
*-------------------------------------------------------------*/
char
*
gk_strchr_replace
(
char
*
str
,
char
*
fromlist
,
char
*
tolist
);
int
gk_strstr_replace
(
char
*
str
,
char
*
pattern
,
char
*
replacement
,
char
*
options
,
char
**
new_str
);
char
*
gk_strtprune
(
char
*
,
char
*
);
char
*
gk_strhprune
(
char
*
,
char
*
);
char
*
gk_strtoupper
(
char
*
);
char
*
gk_strtolower
(
char
*
);
char
*
gk_strdup
(
char
*
orgstr
);
int
gk_strcasecmp
(
char
*
s1
,
char
*
s2
);
int
gk_strrcmp
(
char
*
s1
,
char
*
s2
);
char
*
gk_time2str
(
time_t
time
);
time_t
gk_str2time
(
char
*
str
);
int
gk_GetStringID
(
gk_StringMap_t
*
strmap
,
char
*
key
);
/*-------------------------------------------------------------
* sort.c
*-------------------------------------------------------------*/
void
gk_csorti
(
size_t
,
char
*
);
void
gk_csortd
(
size_t
,
char
*
);
void
gk_isorti
(
size_t
,
int
*
);
void
gk_isortd
(
size_t
,
int
*
);
void
gk_i32sorti
(
size_t
,
int32_t
*
);
void
gk_i32sortd
(
size_t
,
int32_t
*
);
void
gk_i64sorti
(
size_t
,
int64_t
*
);
void
gk_i64sortd
(
size_t
,
int64_t
*
);
void
gk_ui32sorti
(
size_t
,
uint32_t
*
);
void
gk_ui32sortd
(
size_t
,
uint32_t
*
);
void
gk_ui64sorti
(
size_t
,
uint64_t
*
);
void
gk_ui64sortd
(
size_t
,
uint64_t
*
);
void
gk_fsorti
(
size_t
,
float
*
);
void
gk_fsortd
(
size_t
,
float
*
);
void
gk_dsorti
(
size_t
,
double
*
);
void
gk_dsortd
(
size_t
,
double
*
);
void
gk_idxsorti
(
size_t
,
gk_idx_t
*
);
void
gk_idxsortd
(
size_t
,
gk_idx_t
*
);
void
gk_ckvsorti
(
size_t
,
gk_ckv_t
*
);
void
gk_ckvsortd
(
size_t
,
gk_ckv_t
*
);
void
gk_ikvsorti
(
size_t
,
gk_ikv_t
*
);
void
gk_ikvsortd
(
size_t
,
gk_ikv_t
*
);
void
gk_i32kvsorti
(
size_t
,
gk_i32kv_t
*
);
void
gk_i32kvsortd
(
size_t
,
gk_i32kv_t
*
);
void
gk_i64kvsorti
(
size_t
,
gk_i64kv_t
*
);
void
gk_i64kvsortd
(
size_t
,
gk_i64kv_t
*
);
void
gk_zkvsorti
(
size_t
,
gk_zkv_t
*
);
void
gk_zkvsortd
(
size_t
,
gk_zkv_t
*
);
void
gk_zukvsorti
(
size_t
,
gk_zukv_t
*
);
void
gk_zukvsortd
(
size_t
,
gk_zukv_t
*
);
void
gk_fkvsorti
(
size_t
,
gk_fkv_t
*
);
void
gk_fkvsortd
(
size_t
,
gk_fkv_t
*
);
void
gk_dkvsorti
(
size_t
,
gk_dkv_t
*
);
void
gk_dkvsortd
(
size_t
,
gk_dkv_t
*
);
void
gk_skvsorti
(
size_t
,
gk_skv_t
*
);
void
gk_skvsortd
(
size_t
,
gk_skv_t
*
);
void
gk_idxkvsorti
(
size_t
,
gk_idxkv_t
*
);
void
gk_idxkvsortd
(
size_t
,
gk_idxkv_t
*
);
/*-------------------------------------------------------------
* Selection routines
*-------------------------------------------------------------*/
int
gk_dfkvkselect
(
size_t
,
int
,
gk_fkv_t
*
);
int
gk_ifkvkselect
(
size_t
,
int
,
gk_fkv_t
*
);
/*-------------------------------------------------------------
* Priority queue
*-------------------------------------------------------------*/
GK_MKPQUEUE_PROTO
(
gk_ipq
,
gk_ipq_t
,
int
,
gk_idx_t
)
GK_MKPQUEUE_PROTO
(
gk_i32pq
,
gk_i32pq_t
,
int32_t
,
gk_idx_t
)
GK_MKPQUEUE_PROTO
(
gk_i64pq
,
gk_i64pq_t
,
int64_t
,
gk_idx_t
)
GK_MKPQUEUE_PROTO
(
gk_fpq
,
gk_fpq_t
,
float
,
gk_idx_t
)
GK_MKPQUEUE_PROTO
(
gk_dpq
,
gk_dpq_t
,
double
,
gk_idx_t
)
GK_MKPQUEUE_PROTO
(
gk_idxpq
,
gk_idxpq_t
,
gk_idx_t
,
gk_idx_t
)
/*-------------------------------------------------------------
* HTable routines
*-------------------------------------------------------------*/
gk_HTable_t
*
HTable_Create
(
int
nelements
);
void
HTable_Reset
(
gk_HTable_t
*
htable
);
void
HTable_Resize
(
gk_HTable_t
*
htable
,
int
nelements
);
void
HTable_Insert
(
gk_HTable_t
*
htable
,
int
key
,
int
val
);
void
HTable_Delete
(
gk_HTable_t
*
htable
,
int
key
);
int
HTable_Search
(
gk_HTable_t
*
htable
,
int
key
);
int
HTable_GetNext
(
gk_HTable_t
*
htable
,
int
key
,
int
*
val
,
int
type
);
int
HTable_SearchAndDelete
(
gk_HTable_t
*
htable
,
int
key
);
void
HTable_Destroy
(
gk_HTable_t
*
htable
);
int
HTable_HFunction
(
int
nelements
,
int
key
);
/*-------------------------------------------------------------
* Tokenizer routines
*-------------------------------------------------------------*/
void
gk_strtokenize
(
char
*
line
,
char
*
delim
,
gk_Tokens_t
*
tokens
);
void
gk_freetokenslist
(
gk_Tokens_t
*
tokens
);
/*-------------------------------------------------------------
* Encoder/Decoder
*-------------------------------------------------------------*/
void
encodeblock
(
unsigned
char
*
in
,
unsigned
char
*
out
);
void
decodeblock
(
unsigned
char
*
in
,
unsigned
char
*
out
);
void
GKEncodeBase64
(
int
nbytes
,
unsigned
char
*
inbuffer
,
unsigned
char
*
outbuffer
);
void
GKDecodeBase64
(
int
nbytes
,
unsigned
char
*
inbuffer
,
unsigned
char
*
outbuffer
);
/*-------------------------------------------------------------
* random.c
*-------------------------------------------------------------*/
GK_MKRANDOM_PROTO
(
gk_c
,
size_t
,
char
)
GK_MKRANDOM_PROTO
(
gk_i
,
size_t
,
int
)
GK_MKRANDOM_PROTO
(
gk_i32
,
size_t
,
int32_t
)
GK_MKRANDOM_PROTO
(
gk_f
,
size_t
,
float
)
GK_MKRANDOM_PROTO
(
gk_d
,
size_t
,
double
)
GK_MKRANDOM_PROTO
(
gk_idx
,
size_t
,
gk_idx_t
)
GK_MKRANDOM_PROTO
(
gk_z
,
size_t
,
ssize_t
)
GK_MKRANDOM_PROTO
(
gk_zu
,
size_t
,
size_t
)
void
gk_randinit
(
uint64_t
);
uint64_t
gk_randint64
(
void
);
uint32_t
gk_randint32
(
void
);
/*-------------------------------------------------------------
* OpenMP fake functions
*-------------------------------------------------------------*/
#if !defined(__OPENMP__)
void
omp_set_num_threads
(
int
num_threads
);
int
omp_get_num_threads
(
void
);
int
omp_get_max_threads
(
void
);
int
omp_get_thread_num
(
void
);
int
omp_get_num_procs
(
void
);
int
omp_in_parallel
(
void
);
void
omp_set_dynamic
(
int
num_threads
);
int
omp_get_dynamic
(
void
);
void
omp_set_nested
(
int
nested
);
int
omp_get_nested
(
void
);
#endif
/* __OPENMP__ */
/*-------------------------------------------------------------
* CSR-related functions
*-------------------------------------------------------------*/
gk_csr_t
*
gk_csr_Create
();
void
gk_csr_Init
(
gk_csr_t
*
mat
);
void
gk_csr_Free
(
gk_csr_t
**
mat
);
void
gk_csr_FreeContents
(
gk_csr_t
*
mat
);
gk_csr_t
*
gk_csr_Dup
(
gk_csr_t
*
mat
);
gk_csr_t
*
gk_csr_ExtractSubmatrix
(
gk_csr_t
*
mat
,
int
rstart
,
int
nrows
);
gk_csr_t
*
gk_csr_ExtractRows
(
gk_csr_t
*
mat
,
int
nrows
,
int
*
rind
);
gk_csr_t
*
gk_csr_ExtractPartition
(
gk_csr_t
*
mat
,
int
*
part
,
int
pid
);
gk_csr_t
**
gk_csr_Split
(
gk_csr_t
*
mat
,
int
*
color
);
int
gk_csr_DetermineFormat
(
char
*
filename
,
int
format
);
gk_csr_t
*
gk_csr_Read
(
char
*
filename
,
int
format
,
int
readvals
,
int
numbering
);
void
gk_csr_Write
(
gk_csr_t
*
mat
,
char
*
filename
,
int
format
,
int
writevals
,
int
numbering
);
gk_csr_t
*
gk_csr_Prune
(
gk_csr_t
*
mat
,
int
what
,
int
minf
,
int
maxf
);
gk_csr_t
*
gk_csr_LowFilter
(
gk_csr_t
*
mat
,
int
what
,
int
norm
,
float
fraction
);
gk_csr_t
*
gk_csr_TopKPlusFilter
(
gk_csr_t
*
mat
,
int
what
,
int
topk
,
float
keepval
);
gk_csr_t
*
gk_csr_ZScoreFilter
(
gk_csr_t
*
mat
,
int
what
,
float
zscore
);
void
gk_csr_CompactColumns
(
gk_csr_t
*
mat
);
void
gk_csr_SortIndices
(
gk_csr_t
*
mat
,
int
what
);
void
gk_csr_CreateIndex
(
gk_csr_t
*
mat
,
int
what
);
void
gk_csr_Normalize
(
gk_csr_t
*
mat
,
int
what
,
int
norm
);
void
gk_csr_Scale
(
gk_csr_t
*
mat
,
int
type
);
void
gk_csr_ComputeSums
(
gk_csr_t
*
mat
,
int
what
);
void
gk_csr_ComputeNorms
(
gk_csr_t
*
mat
,
int
what
);
void
gk_csr_ComputeSquaredNorms
(
gk_csr_t
*
mat
,
int
what
);
gk_csr_t
*
gk_csr_Shuffle
(
gk_csr_t
*
mat
,
int
what
,
int
summetric
);
gk_csr_t
*
gk_csr_Transpose
(
gk_csr_t
*
mat
);
float
gk_csr_ComputeSimilarity
(
gk_csr_t
*
mat
,
int
i1
,
int
i2
,
int
what
,
int
simtype
);
float
gk_csr_ComputePairSimilarity
(
gk_csr_t
*
mat_a
,
gk_csr_t
*
mat_b
,
int
i1
,
int
i2
,
int
what
,
int
simtype
);
int
gk_csr_GetSimilarRows
(
gk_csr_t
*
mat
,
int
nqterms
,
int
*
qind
,
float
*
qval
,
int
simtype
,
int
nsim
,
float
minsim
,
gk_fkv_t
*
hits
,
int
*
_imarker
,
gk_fkv_t
*
i_cand
);
int
gk_csr_FindConnectedComponents
(
gk_csr_t
*
mat
,
int32_t
*
cptr
,
int32_t
*
cind
,
int32_t
*
cids
);
gk_csr_t
*
gk_csr_MakeSymmetric
(
gk_csr_t
*
mat
,
int
op
);
gk_csr_t
*
gk_csr_ReorderSymmetric
(
gk_csr_t
*
mat
,
int32_t
*
perm
,
int32_t
*
iperm
);
void
gk_csr_ComputeBFSOrderingSymmetric
(
gk_csr_t
*
mat
,
int
maxdegree
,
int
v
,
int32_t
**
r_perm
,
int32_t
**
r_iperm
);
void
gk_csr_ComputeBestFOrderingSymmetric
(
gk_csr_t
*
mat
,
int
v
,
int
type
,
int32_t
**
r_perm
,
int32_t
**
r_iperm
);
/* itemsets.c */
void
gk_find_frequent_itemsets
(
int
ntrans
,
ssize_t
*
tranptr
,
int
*
tranind
,
int
minfreq
,
int
maxfreq
,
int
minlen
,
int
maxlen
,
void
(
*
process_itemset
)(
void
*
stateptr
,
int
nitems
,
int
*
itemind
,
int
ntrans
,
int
*
tranind
),
void
*
stateptr
);
/* evaluate.c */
float
ComputeAccuracy
(
int
n
,
gk_fkv_t
*
list
);
float
ComputeROCn
(
int
n
,
int
maxN
,
gk_fkv_t
*
list
);
float
ComputeMedianRFP
(
int
n
,
gk_fkv_t
*
list
);
float
ComputeMean
(
int
n
,
float
*
values
);
float
ComputeStdDev
(
int
n
,
float
*
values
);
/* mcore.c */
gk_mcore_t
*
gk_mcoreCreate
(
size_t
coresize
);
gk_mcore_t
*
gk_gkmcoreCreate
();
void
gk_mcoreDestroy
(
gk_mcore_t
**
r_mcore
,
int
showstats
);
void
gk_gkmcoreDestroy
(
gk_mcore_t
**
r_mcore
,
int
showstats
);
void
*
gk_mcoreMalloc
(
gk_mcore_t
*
mcore
,
size_t
nbytes
);
void
gk_mcorePush
(
gk_mcore_t
*
mcore
);
void
gk_gkmcorePush
(
gk_mcore_t
*
mcore
);
void
gk_mcorePop
(
gk_mcore_t
*
mcore
);
void
gk_gkmcorePop
(
gk_mcore_t
*
mcore
);
void
gk_mcoreAdd
(
gk_mcore_t
*
mcore
,
int
type
,
size_t
nbytes
,
void
*
ptr
);
void
gk_gkmcoreAdd
(
gk_mcore_t
*
mcore
,
int
type
,
size_t
nbytes
,
void
*
ptr
);
void
gk_mcoreDel
(
gk_mcore_t
*
mcore
,
void
*
ptr
);
void
gk_gkmcoreDel
(
gk_mcore_t
*
mcore
,
void
*
ptr
);
/* rw.c */
int
gk_rw_PageRank
(
gk_csr_t
*
mat
,
float
lamda
,
float
eps
,
int
max_niter
,
float
*
pr
);
/* graph.c */
gk_graph_t
*
gk_graph_Create
();
void
gk_graph_Init
(
gk_graph_t
*
graph
);
void
gk_graph_Free
(
gk_graph_t
**
graph
);
void
gk_graph_FreeContents
(
gk_graph_t
*
graph
);
gk_graph_t
*
gk_graph_Read
(
char
*
filename
,
int
format
,
int
hasvals
,
int
numbering
,
int
isfewgts
,
int
isfvwgts
,
int
isfvsizes
);
void
gk_graph_Write
(
gk_graph_t
*
graph
,
char
*
filename
,
int
format
);
gk_graph_t
*
gk_graph_Dup
(
gk_graph_t
*
graph
);
gk_graph_t
*
gk_graph_ExtractSubgraph
(
gk_graph_t
*
graph
,
int
vstart
,
int
nvtxs
);
gk_graph_t
*
gk_graph_Reorder
(
gk_graph_t
*
graph
,
int32_t
*
perm
,
int32_t
*
iperm
);
int
gk_graph_FindComponents
(
gk_graph_t
*
graph
,
int32_t
*
cptr
,
int32_t
*
cind
);
void
gk_graph_ComputeBFSOrdering
(
gk_graph_t
*
graph
,
int
v
,
int32_t
**
r_perm
,
int32_t
**
r_iperm
);
void
gk_graph_ComputeBestFOrdering0
(
gk_graph_t
*
graph
,
int
v
,
int
type
,
int32_t
**
r_perm
,
int32_t
**
r_iperm
);
void
gk_graph_ComputeBestFOrdering
(
gk_graph_t
*
graph
,
int
v
,
int
type
,
int32_t
**
r_perm
,
int32_t
**
r_iperm
);
void
gk_graph_SingleSourceShortestPaths
(
gk_graph_t
*
graph
,
int
v
,
void
**
r_sps
);
#ifdef __cplusplus
}
#endif
#endif
third_party/METIS/GKlib/gk_struct.h
0 → 100644
View file @
3359c1f1
/*!
\file gk_struct.h
\brief This file contains various datastructures used/provided by GKlib
\date Started 3/27/2007
\author George
\version\verbatim $Id: gk_struct.h 21032 2017-05-23 02:49:02Z karypis $ \endverbatim
*/
#ifndef _GK_STRUCT_H_
#define _GK_STRUCT_H_
/********************************************************************/
/*! Generator for gk_??KeyVal_t data structure */
/********************************************************************/
#define GK_MKKEYVALUE_T(NAME, KEYTYPE, VALTYPE) \
typedef struct {\
KEYTYPE key;\
VALTYPE val;\
} NAME;\
/* The actual KeyVal data structures */
GK_MKKEYVALUE_T
(
gk_ckv_t
,
char
,
ssize_t
)
GK_MKKEYVALUE_T
(
gk_ikv_t
,
int
,
ssize_t
)
GK_MKKEYVALUE_T
(
gk_i8kv_t
,
int8_t
,
ssize_t
)
GK_MKKEYVALUE_T
(
gk_i16kv_t
,
int16_t
,
ssize_t
)
GK_MKKEYVALUE_T
(
gk_i32kv_t
,
int32_t
,
ssize_t
)
GK_MKKEYVALUE_T
(
gk_i64kv_t
,
int64_t
,
ssize_t
)
GK_MKKEYVALUE_T
(
gk_zkv_t
,
ssize_t
,
ssize_t
)
GK_MKKEYVALUE_T
(
gk_zukv_t
,
size_t
,
ssize_t
)
GK_MKKEYVALUE_T
(
gk_fkv_t
,
float
,
ssize_t
)
GK_MKKEYVALUE_T
(
gk_dkv_t
,
double
,
ssize_t
)
GK_MKKEYVALUE_T
(
gk_skv_t
,
char
*
,
ssize_t
)
GK_MKKEYVALUE_T
(
gk_idxkv_t
,
gk_idx_t
,
gk_idx_t
)
/********************************************************************/
/*! Generator for gk_?pq_t data structure */
/********************************************************************/
#define GK_MKPQUEUE_T(NAME, KVTYPE)\
typedef struct {\
size_t nnodes;\
size_t maxnodes;\
\
/* Heap version of the data structure */
\
KVTYPE *heap;\
ssize_t *locator;\
} NAME;\
GK_MKPQUEUE_T
(
gk_ipq_t
,
gk_ikv_t
)
GK_MKPQUEUE_T
(
gk_i32pq_t
,
gk_i32kv_t
)
GK_MKPQUEUE_T
(
gk_i64pq_t
,
gk_i64kv_t
)
GK_MKPQUEUE_T
(
gk_fpq_t
,
gk_fkv_t
)
GK_MKPQUEUE_T
(
gk_dpq_t
,
gk_dkv_t
)
GK_MKPQUEUE_T
(
gk_idxpq_t
,
gk_idxkv_t
)
#define GK_MKPQUEUE2_T(NAME, KTYPE, VTYPE)\
typedef struct {\
ssize_t nnodes;\
ssize_t maxnodes;\
\
/* Heap version of the data structure */
\
KTYPE *keys;\
VTYPE *vals;\
} NAME;\
/*-------------------------------------------------------------
* The following data structure stores a sparse CSR format
*-------------------------------------------------------------*/
typedef
struct
gk_csr_t
{
int32_t
nrows
,
ncols
;
ssize_t
*
rowptr
,
*
colptr
;
int32_t
*
rowind
,
*
colind
;
int32_t
*
rowids
,
*
colids
;
int32_t
*
rlabels
,
*
clabels
;
int32_t
*
rmap
,
*
cmap
;
float
*
rowval
,
*
colval
;
float
*
rnorms
,
*
cnorms
;
float
*
rsums
,
*
csums
;
float
*
rsizes
,
*
csizes
;
float
*
rvols
,
*
cvols
;
float
*
rwgts
,
*
cwgts
;
}
gk_csr_t
;
/*-------------------------------------------------------------
* The following data structure stores a sparse graph
*-------------------------------------------------------------*/
typedef
struct
gk_graph_t
{
int32_t
nvtxs
;
/*!< The number of vertices in the graph */
ssize_t
*
xadj
;
/*!< The ptr-structure of the adjncy list */
int32_t
*
adjncy
;
/*!< The adjacency list of the graph */
int32_t
*
iadjwgt
;
/*!< The integer edge weights */
float
*
fadjwgt
;
/*!< The floating point edge weights */
int32_t
*
ivwgts
;
/*!< The integer vertex weights */
float
*
fvwgts
;
/*!< The floating point vertex weights */
int32_t
*
ivsizes
;
/*!< The integer vertex sizes */
float
*
fvsizes
;
/*!< The floating point vertex sizes */
int32_t
*
vlabels
;
/*!< The labels of the vertices */
}
gk_graph_t
;
/*-------------------------------------------------------------
* The following data structure stores stores a string as a
* pair of its allocated buffer and the buffer itself.
*-------------------------------------------------------------*/
typedef
struct
gk_str_t
{
size_t
len
;
char
*
buf
;
}
gk_str_t
;
/*-------------------------------------------------------------
* The following data structure implements a string-2-int mapping
* table used for parsing command-line options
*-------------------------------------------------------------*/
typedef
struct
gk_StringMap_t
{
char
*
name
;
int
id
;
}
gk_StringMap_t
;
/*------------------------------------------------------------
* This structure implements a simple hash table
*------------------------------------------------------------*/
typedef
struct
gk_HTable_t
{
int
nelements
;
/* The overall size of the hash-table */
int
htsize
;
/* The current size of the hash-table */
gk_ikv_t
*
harray
;
/* The actual hash-table */
}
gk_HTable_t
;
/*------------------------------------------------------------
* This structure implements a gk_Tokens_t list returned by the
* string tokenizer
*------------------------------------------------------------*/
typedef
struct
gk_Tokens_t
{
int
ntoks
;
/* The number of tokens in the input string */
char
*
strbuf
;
/* The memory that stores all the entries */
char
**
list
;
/* Pointers to the strbuf for each element */
}
gk_Tokens_t
;
/*------------------------------------------------------------
* This structure implements storage for an atom in a pdb file
*------------------------------------------------------------*/
typedef
struct
atom
{
int
serial
;
char
*
name
;
char
altLoc
;
char
*
resname
;
char
chainid
;
int
rserial
;
char
icode
;
char
element
;
double
x
;
double
y
;
double
z
;
double
opcy
;
double
tmpt
;
}
atom
;
/*------------------------------------------------------------
* This structure implements storage for a center of mass for
* a single residue.
*------------------------------------------------------------*/
typedef
struct
center_of_mass
{
char
name
;
double
x
;
double
y
;
double
z
;
}
center_of_mass
;
/*------------------------------------------------------------
* This structure implements storage for a pdb protein
*------------------------------------------------------------*/
typedef
struct
pdbf
{
int
natoms
;
/* Number of atoms */
int
nresidues
;
/* Number of residues based on coordinates */
int
ncas
;
int
nbbs
;
int
corruption
;
char
*
resSeq
;
/* Residue sequence based on coordinates */
char
**
threeresSeq
;
/* three-letter residue sequence */
atom
*
atoms
;
atom
**
bbs
;
atom
**
cas
;
center_of_mass
*
cm
;
}
pdbf
;
/*************************************************************
* Localization Structures for converting characters to integers
**************************************************************/
typedef
struct
gk_i2cc2i_t
{
int
n
;
char
*
i2c
;
int
*
c2i
;
}
gk_i2cc2i_t
;
/*******************************************************************
*This structure implements storage of a protein sequence
* *****************************************************************/
typedef
struct
gk_seq_t
{
int
len
;
/*Number of Residues */
int
*
sequence
;
/* Stores the sequence*/
int
**
pssm
;
/* Stores the pssm matrix */
int
**
psfm
;
/* Stores the psfm matrix */
char
*
name
;
/* Stores the name of the sequence */
int
nsymbols
;
}
gk_seq_t
;
/*************************************************************************/
/*! The following data structure stores information about a memory
allocation operation that can either be served from gk_mcore_t or by
a gk_malloc if not sufficient workspace memory is available. */
/*************************************************************************/
typedef
struct
gk_mop_t
{
int
type
;
ssize_t
nbytes
;
void
*
ptr
;
}
gk_mop_t
;
/*************************************************************************/
/*! The following structure stores information used by Metis */
/*************************************************************************/
typedef
struct
gk_mcore_t
{
/* Workspace information */
size_t
coresize
;
/*!< The amount of core memory that has been allocated */
size_t
corecpos
;
/*!< Index of the first free location in core */
void
*
core
;
/*!< Pointer to the core itself */
/* These are for implementing a stack-based allocation scheme using both
core and also dynamically allocated memory */
size_t
nmops
;
/*!< The number of maop_t entries that have been allocated */
size_t
cmop
;
/*!< Index of the first free location in maops */
gk_mop_t
*
mops
;
/*!< The array recording the maop_t operations */
/* These are for keeping various statistics for wspacemalloc */
size_t
num_callocs
;
/*!< The number of core mallocs */
size_t
num_hallocs
;
/*!< The number of heap mallocs */
size_t
size_callocs
;
/*!< The total # of bytes in core mallocs */
size_t
size_hallocs
;
/*!< The total # of bytes in heap mallocs */
size_t
cur_callocs
;
/*!< The current # of bytes in core mallocs */
size_t
cur_hallocs
;
/*!< The current # of bytes in heap mallocs */
size_t
max_callocs
;
/*!< The maximum # of bytes in core mallocs at any given time */
size_t
max_hallocs
;
/*!< The maximum # of bytes in heap mallocs at any given time */
}
gk_mcore_t
;
#endif
third_party/METIS/GKlib/gk_types.h
0 → 100644
View file @
3359c1f1
/*!
\file gk_types.h
\brief This file contains basic scalar datatype used in GKlib
\date Started 3/27/2007
\author George
\version\verbatim $Id: gk_types.h 10711 2011-08-31 22:23:04Z karypis $ \endverbatim
*/
#ifndef _GK_TYPES_H_
#define _GK_TYPES_H_
/*************************************************************************
* Basic data type definitions. These definitions allow GKlib to separate
* the following elemental types:
* - loop iterator variables, which are set to size_t
* - signed and unsigned int variables that can be set to any # of bits
* - signed and unsigned long variables that can be set to any # of bits
* - real variables, which can be set to single or double precision.
**************************************************************************/
/*typedef ptrdiff_t gk_idx_t; */
/* index variable */
typedef
ssize_t
gk_idx_t
;
/* index variable */
typedef
int32_t
gk_int_t
;
/* integer values */
typedef
uint32_t
gk_uint_t
;
/* unsigned integer values */
typedef
int64_t
gk_long_t
;
/* long integer values */
typedef
uint64_t
gk_ulong_t
;
/* unsigned long integer values */
typedef
float
gk_real_t
;
/* real type */
typedef
double
gk_dreal_t
;
/* double precission real type */
typedef
double
gk_wclock_t
;
/* wall-clock time */
/*#define GK_IDX_MAX PTRDIFF_MAX*/
#define GK_IDX_MAX ((SIZE_MAX>>1)-2)
#define PRIGKIDX "zd"
#define SCNGKIDX "zd"
#endif
third_party/METIS/GKlib/gk_util.c
0 → 100644
View file @
3359c1f1
/*!
\file util.c
\brief Various utility routines
\date Started 4/12/2007
\author George
\version\verbatim $Id: gk_util.c 16223 2014-02-15 21:34:09Z karypis $ \endverbatim
*/
#include <GKlib.h>
/*************************************************************************
* This file randomly permutes the contents of an array.
* flag == 0, don't initialize perm
* flag == 1, set p[i] = i
**************************************************************************/
void
gk_RandomPermute
(
size_t
n
,
int
*
p
,
int
flag
)
{
size_t
i
,
u
,
v
;
int
tmp
;
if
(
flag
==
1
)
{
for
(
i
=
0
;
i
<
n
;
i
++
)
p
[
i
]
=
i
;
}
for
(
i
=
0
;
i
<
n
/
2
;
i
++
)
{
v
=
RandomInRange
(
n
);
u
=
RandomInRange
(
n
);
gk_SWAP
(
p
[
v
],
p
[
u
],
tmp
);
}
}
/************************************************************************/
/*!
\brief Converts an element-based set membership into a CSR-format set-based
membership.
For example, it takes an array such as part[] that stores where each
element belongs to and returns a pair of arrays (pptr[], pind[]) that
store in CSF format the list of elements belonging in each partition.
\param n
the number of elements in the array (e.g., # of vertices)
\param range
the cardinality of the set (e.g., # of partitions)
\param array
the array that stores the per-element set membership
\param ptr
the array that will store the starting indices in ind for
the elements of each set. This is filled by the routine and
its size should be at least range+1.
\param ind
the array that stores consecutively which elements belong to
each set. The size of this array should be n.
*/
/************************************************************************/
void
gk_array2csr
(
size_t
n
,
size_t
range
,
int
*
array
,
int
*
ptr
,
int
*
ind
)
{
size_t
i
;
gk_iset
(
range
+
1
,
0
,
ptr
);
for
(
i
=
0
;
i
<
n
;
i
++
)
ptr
[
array
[
i
]]
++
;
/* Compute the ptr, ind structure */
MAKECSR
(
i
,
range
,
ptr
);
for
(
i
=
0
;
i
<
n
;
i
++
)
ind
[
ptr
[
array
[
i
]]
++
]
=
i
;
SHIFTCSR
(
i
,
range
,
ptr
);
}
/*************************************************************************
* This function returns the log2(x)
**************************************************************************/
int
gk_log2
(
int
a
)
{
size_t
i
;
for
(
i
=
1
;
a
>
1
;
i
++
,
a
=
a
>>
1
);
return
i
-
1
;
}
/*************************************************************************
* This function checks if the argument is a power of 2
**************************************************************************/
int
gk_ispow2
(
int
a
)
{
return
(
a
==
(
1
<<
gk_log2
(
a
)));
}
/*************************************************************************
* This function returns the log2(x)
**************************************************************************/
float
gk_flog2
(
float
a
)
{
return
log
(
a
)
/
log
(
2
.
0
);
}
third_party/METIS/GKlib/gkregex.c
0 → 100644
View file @
3359c1f1
This source diff could not be displayed because it is too large. You can
view the blob
instead.
third_party/METIS/GKlib/gkregex.h
0 → 100644
View file @
3359c1f1
/* Definitions for data structures and routines for the regular
expression library.
Copyright (C) 1985,1989-93,1995-98,2000,2001,2002,2003,2005,2006
Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA. */
#ifndef _REGEX_H
#define _REGEX_H 1
#include <sys/types.h>
/* Allow the use in C++ code. */
#ifdef __cplusplus
extern
"C"
{
#endif
/* The following two types have to be signed and unsigned integer type
wide enough to hold a value of a pointer. For most ANSI compilers
ptrdiff_t and size_t should be likely OK. Still size of these two
types is 2 for Microsoft C. Ugh... */
typedef
long
int
s_reg_t
;
typedef
unsigned
long
int
active_reg_t
;
/* The following bits are used to determine the regexp syntax we
recognize. The set/not-set meanings are chosen so that Emacs syntax
remains the value 0. The bits are given in alphabetical order, and
the definitions shifted by one from the previous bit; thus, when we
add or remove a bit, only one other definition need change. */
typedef
unsigned
long
int
reg_syntax_t
;
/* If this bit is not set, then \ inside a bracket expression is literal.
If set, then such a \ quotes the following character. */
#define RE_BACKSLASH_ESCAPE_IN_LISTS ((unsigned long int) 1)
/* If this bit is not set, then + and ? are operators, and \+ and \? are
literals.
If set, then \+ and \? are operators and + and ? are literals. */
#define RE_BK_PLUS_QM (RE_BACKSLASH_ESCAPE_IN_LISTS << 1)
/* If this bit is set, then character classes are supported. They are:
[:alpha:], [:upper:], [:lower:], [:digit:], [:alnum:], [:xdigit:],
[:space:], [:print:], [:punct:], [:graph:], and [:cntrl:].
If not set, then character classes are not supported. */
#define RE_CHAR_CLASSES (RE_BK_PLUS_QM << 1)
/* If this bit is set, then ^ and $ are always anchors (outside bracket
expressions, of course).
If this bit is not set, then it depends:
^ is an anchor if it is at the beginning of a regular
expression or after an open-group or an alternation operator;
$ is an anchor if it is at the end of a regular expression, or
before a close-group or an alternation operator.
This bit could be (re)combined with RE_CONTEXT_INDEP_OPS, because
POSIX draft 11.2 says that * etc. in leading positions is undefined.
We already implemented a previous draft which made those constructs
invalid, though, so we haven't changed the code back. */
#define RE_CONTEXT_INDEP_ANCHORS (RE_CHAR_CLASSES << 1)
/* If this bit is set, then special characters are always special
regardless of where they are in the pattern.
If this bit is not set, then special characters are special only in
some contexts; otherwise they are ordinary. Specifically,
* + ? and intervals are only special when not after the beginning,
open-group, or alternation operator. */
#define RE_CONTEXT_INDEP_OPS (RE_CONTEXT_INDEP_ANCHORS << 1)
/* If this bit is set, then *, +, ?, and { cannot be first in an re or
immediately after an alternation or begin-group operator. */
#define RE_CONTEXT_INVALID_OPS (RE_CONTEXT_INDEP_OPS << 1)
/* If this bit is set, then . matches newline.
If not set, then it doesn't. */
#define RE_DOT_NEWLINE (RE_CONTEXT_INVALID_OPS << 1)
/* If this bit is set, then . doesn't match NUL.
If not set, then it does. */
#define RE_DOT_NOT_NULL (RE_DOT_NEWLINE << 1)
/* If this bit is set, nonmatching lists [^...] do not match newline.
If not set, they do. */
#define RE_HAT_LISTS_NOT_NEWLINE (RE_DOT_NOT_NULL << 1)
/* If this bit is set, either \{...\} or {...} defines an
interval, depending on RE_NO_BK_BRACES.
If not set, \{, \}, {, and } are literals. */
#define RE_INTERVALS (RE_HAT_LISTS_NOT_NEWLINE << 1)
/* If this bit is set, +, ? and | aren't recognized as operators.
If not set, they are. */
#define RE_LIMITED_OPS (RE_INTERVALS << 1)
/* If this bit is set, newline is an alternation operator.
If not set, newline is literal. */
#define RE_NEWLINE_ALT (RE_LIMITED_OPS << 1)
/* If this bit is set, then `{...}' defines an interval, and \{ and \}
are literals.
If not set, then `\{...\}' defines an interval. */
#define RE_NO_BK_BRACES (RE_NEWLINE_ALT << 1)
/* If this bit is set, (...) defines a group, and \( and \) are literals.
If not set, \(...\) defines a group, and ( and ) are literals. */
#define RE_NO_BK_PARENS (RE_NO_BK_BRACES << 1)
/* If this bit is set, then \<digit> matches <digit>.
If not set, then \<digit> is a back-reference. */
#define RE_NO_BK_REFS (RE_NO_BK_PARENS << 1)
/* If this bit is set, then | is an alternation operator, and \| is literal.
If not set, then \| is an alternation operator, and | is literal. */
#define RE_NO_BK_VBAR (RE_NO_BK_REFS << 1)
/* If this bit is set, then an ending range point collating higher
than the starting range point, as in [z-a], is invalid.
If not set, then when ending range point collates higher than the
starting range point, the range is ignored. */
#define RE_NO_EMPTY_RANGES (RE_NO_BK_VBAR << 1)
/* If this bit is set, then an unmatched ) is ordinary.
If not set, then an unmatched ) is invalid. */
#define RE_UNMATCHED_RIGHT_PAREN_ORD (RE_NO_EMPTY_RANGES << 1)
/* If this bit is set, succeed as soon as we match the whole pattern,
without further backtracking. */
#define RE_NO_POSIX_BACKTRACKING (RE_UNMATCHED_RIGHT_PAREN_ORD << 1)
/* If this bit is set, do not process the GNU regex operators.
If not set, then the GNU regex operators are recognized. */
#define RE_NO_GNU_OPS (RE_NO_POSIX_BACKTRACKING << 1)
/* If this bit is set, turn on internal regex debugging.
If not set, and debugging was on, turn it off.
This only works if regex.c is compiled -DDEBUG.
We define this bit always, so that all that's needed to turn on
debugging is to recompile regex.c; the calling code can always have
this bit set, and it won't affect anything in the normal case. */
#define RE_DEBUG (RE_NO_GNU_OPS << 1)
/* If this bit is set, a syntactically invalid interval is treated as
a string of ordinary characters. For example, the ERE 'a{1' is
treated as 'a\{1'. */
#define RE_INVALID_INTERVAL_ORD (RE_DEBUG << 1)
/* If this bit is set, then ignore case when matching.
If not set, then case is significant. */
#define RE_ICASE (RE_INVALID_INTERVAL_ORD << 1)
/* This bit is used internally like RE_CONTEXT_INDEP_ANCHORS but only
for ^, because it is difficult to scan the regex backwards to find
whether ^ should be special. */
#define RE_CARET_ANCHORS_HERE (RE_ICASE << 1)
/* If this bit is set, then \{ cannot be first in an bre or
immediately after an alternation or begin-group operator. */
#define RE_CONTEXT_INVALID_DUP (RE_CARET_ANCHORS_HERE << 1)
/* If this bit is set, then no_sub will be set to 1 during
re_compile_pattern. */
#define RE_NO_SUB (RE_CONTEXT_INVALID_DUP << 1)
/* This global variable defines the particular regexp syntax to use (for
some interfaces). When a regexp is compiled, the syntax used is
stored in the pattern buffer, so changing this does not affect
already-compiled regexps. */
extern
reg_syntax_t
re_syntax_options
;
/* Define combinations of the above bits for the standard possibilities.
(The [[[ comments delimit what gets put into the Texinfo file, so
don't delete them!) */
/* [[[begin syntaxes]]] */
#define RE_SYNTAX_EMACS 0
#define RE_SYNTAX_AWK \
(RE_BACKSLASH_ESCAPE_IN_LISTS | RE_DOT_NOT_NULL \
| RE_NO_BK_PARENS | RE_NO_BK_REFS \
| RE_NO_BK_VBAR | RE_NO_EMPTY_RANGES \
| RE_DOT_NEWLINE | RE_CONTEXT_INDEP_ANCHORS \
| RE_UNMATCHED_RIGHT_PAREN_ORD | RE_NO_GNU_OPS)
#define RE_SYNTAX_GNU_AWK \
((RE_SYNTAX_POSIX_EXTENDED | RE_BACKSLASH_ESCAPE_IN_LISTS | RE_DEBUG) \
& ~(RE_DOT_NOT_NULL | RE_INTERVALS | RE_CONTEXT_INDEP_OPS \
| RE_CONTEXT_INVALID_OPS ))
#define RE_SYNTAX_POSIX_AWK \
(RE_SYNTAX_POSIX_EXTENDED | RE_BACKSLASH_ESCAPE_IN_LISTS \
| RE_INTERVALS | RE_NO_GNU_OPS)
#define RE_SYNTAX_GREP \
(RE_BK_PLUS_QM | RE_CHAR_CLASSES \
| RE_HAT_LISTS_NOT_NEWLINE | RE_INTERVALS \
| RE_NEWLINE_ALT)
#define RE_SYNTAX_EGREP \
(RE_CHAR_CLASSES | RE_CONTEXT_INDEP_ANCHORS \
| RE_CONTEXT_INDEP_OPS | RE_HAT_LISTS_NOT_NEWLINE \
| RE_NEWLINE_ALT | RE_NO_BK_PARENS \
| RE_NO_BK_VBAR)
#define RE_SYNTAX_POSIX_EGREP \
(RE_SYNTAX_EGREP | RE_INTERVALS | RE_NO_BK_BRACES \
| RE_INVALID_INTERVAL_ORD)
/* P1003.2/D11.2, section 4.20.7.1, lines 5078ff. */
#define RE_SYNTAX_ED RE_SYNTAX_POSIX_BASIC
#define RE_SYNTAX_SED RE_SYNTAX_POSIX_BASIC
/* Syntax bits common to both basic and extended POSIX regex syntax. */
#define _RE_SYNTAX_POSIX_COMMON \
(RE_CHAR_CLASSES | RE_DOT_NEWLINE | RE_DOT_NOT_NULL \
| RE_INTERVALS | RE_NO_EMPTY_RANGES)
#define RE_SYNTAX_POSIX_BASIC \
(_RE_SYNTAX_POSIX_COMMON | RE_BK_PLUS_QM | RE_CONTEXT_INVALID_DUP)
/* Differs from ..._POSIX_BASIC only in that RE_BK_PLUS_QM becomes
RE_LIMITED_OPS, i.e., \? \+ \| are not recognized. Actually, this
isn't minimal, since other operators, such as \`, aren't disabled. */
#define RE_SYNTAX_POSIX_MINIMAL_BASIC \
(_RE_SYNTAX_POSIX_COMMON | RE_LIMITED_OPS)
#define RE_SYNTAX_POSIX_EXTENDED \
(_RE_SYNTAX_POSIX_COMMON | RE_CONTEXT_INDEP_ANCHORS \
| RE_CONTEXT_INDEP_OPS | RE_NO_BK_BRACES \
| RE_NO_BK_PARENS | RE_NO_BK_VBAR \
| RE_CONTEXT_INVALID_OPS | RE_UNMATCHED_RIGHT_PAREN_ORD)
/* Differs from ..._POSIX_EXTENDED in that RE_CONTEXT_INDEP_OPS is
removed and RE_NO_BK_REFS is added. */
#define RE_SYNTAX_POSIX_MINIMAL_EXTENDED \
(_RE_SYNTAX_POSIX_COMMON | RE_CONTEXT_INDEP_ANCHORS \
| RE_CONTEXT_INVALID_OPS | RE_NO_BK_BRACES \
| RE_NO_BK_PARENS | RE_NO_BK_REFS \
| RE_NO_BK_VBAR | RE_UNMATCHED_RIGHT_PAREN_ORD)
/* [[[end syntaxes]]] */
/* Maximum number of duplicates an interval can allow. Some systems
(erroneously) define this in other header files, but we want our
value, so remove any previous define. */
#ifdef RE_DUP_MAX
# undef RE_DUP_MAX
#endif
/* If sizeof(int) == 2, then ((1 << 15) - 1) overflows. */
#define RE_DUP_MAX (0x7fff)
/* POSIX `cflags' bits (i.e., information for `regcomp'). */
/* If this bit is set, then use extended regular expression syntax.
If not set, then use basic regular expression syntax. */
#define REG_EXTENDED 1
/* If this bit is set, then ignore case when matching.
If not set, then case is significant. */
#define REG_ICASE (REG_EXTENDED << 1)
/* If this bit is set, then anchors do not match at newline
characters in the string.
If not set, then anchors do match at newlines. */
#define REG_NEWLINE (REG_ICASE << 1)
/* If this bit is set, then report only success or fail in regexec.
If not set, then returns differ between not matching and errors. */
#define REG_NOSUB (REG_NEWLINE << 1)
/* POSIX `eflags' bits (i.e., information for regexec). */
/* If this bit is set, then the beginning-of-line operator doesn't match
the beginning of the string (presumably because it's not the
beginning of a line).
If not set, then the beginning-of-line operator does match the
beginning of the string. */
#define REG_NOTBOL 1
/* Like REG_NOTBOL, except for the end-of-line. */
#define REG_NOTEOL (1 << 1)
/* Use PMATCH[0] to delimit the start and end of the search in the
buffer. */
#define REG_STARTEND (1 << 2)
/* If any error codes are removed, changed, or added, update the
`re_error_msg' table in regex.c. */
typedef
enum
{
#ifdef _XOPEN_SOURCE
REG_ENOSYS
=
-
1
,
/* This will never happen for this implementation. */
#endif
REG_NOERROR
=
0
,
/* Success. */
REG_NOMATCH
,
/* Didn't find a match (for regexec). */
/* POSIX regcomp return error codes. (In the order listed in the
standard.) */
REG_BADPAT
,
/* Invalid pattern. */
REG_ECOLLATE
,
/* Inalid collating element. */
REG_ECTYPE
,
/* Invalid character class name. */
REG_EESCAPE
,
/* Trailing backslash. */
REG_ESUBREG
,
/* Invalid back reference. */
REG_EBRACK
,
/* Unmatched left bracket. */
REG_EPAREN
,
/* Parenthesis imbalance. */
REG_EBRACE
,
/* Unmatched \{. */
REG_BADBR
,
/* Invalid contents of \{\}. */
REG_ERANGE
,
/* Invalid range end. */
REG_ESPACE
,
/* Ran out of memory. */
REG_BADRPT
,
/* No preceding re for repetition op. */
/* Error codes we've added. */
REG_EEND
,
/* Premature end. */
REG_ESIZE
,
/* Compiled pattern bigger than 2^16 bytes. */
REG_ERPAREN
/* Unmatched ) or \); not returned from regcomp. */
}
reg_errcode_t
;
/* This data structure represents a compiled pattern. Before calling
the pattern compiler, the fields `buffer', `allocated', `fastmap',
`translate', and `no_sub' can be set. After the pattern has been
compiled, the `re_nsub' field is available. All other fields are
private to the regex routines. */
#ifndef RE_TRANSLATE_TYPE
# define RE_TRANSLATE_TYPE unsigned char *
#endif
struct
re_pattern_buffer
{
/* Space that holds the compiled pattern. It is declared as
`unsigned char *' because its elements are sometimes used as
array indexes. */
unsigned
char
*
buffer
;
/* Number of bytes to which `buffer' points. */
unsigned
long
int
allocated
;
/* Number of bytes actually used in `buffer'. */
unsigned
long
int
used
;
/* Syntax setting with which the pattern was compiled. */
reg_syntax_t
syntax
;
/* Pointer to a fastmap, if any, otherwise zero. re_search uses the
fastmap, if there is one, to skip over impossible starting points
for matches. */
char
*
fastmap
;
/* Either a translate table to apply to all characters before
comparing them, or zero for no translation. The translation is
applied to a pattern when it is compiled and to a string when it
is matched. */
RE_TRANSLATE_TYPE
translate
;
/* Number of subexpressions found by the compiler. */
size_t
re_nsub
;
/* Zero if this pattern cannot match the empty string, one else.
Well, in truth it's used only in `re_search_2', to see whether or
not we should use the fastmap, so we don't set this absolutely
perfectly; see `re_compile_fastmap' (the `duplicate' case). */
unsigned
can_be_null
:
1
;
/* If REGS_UNALLOCATED, allocate space in the `regs' structure
for `max (RE_NREGS, re_nsub + 1)' groups.
If REGS_REALLOCATE, reallocate space if necessary.
If REGS_FIXED, use what's there. */
#define REGS_UNALLOCATED 0
#define REGS_REALLOCATE 1
#define REGS_FIXED 2
unsigned
regs_allocated
:
2
;
/* Set to zero when `regex_compile' compiles a pattern; set to one
by `re_compile_fastmap' if it updates the fastmap. */
unsigned
fastmap_accurate
:
1
;
/* If set, `re_match_2' does not return information about
subexpressions. */
unsigned
no_sub
:
1
;
/* If set, a beginning-of-line anchor doesn't match at the beginning
of the string. */
unsigned
not_bol
:
1
;
/* Similarly for an end-of-line anchor. */
unsigned
not_eol
:
1
;
/* If true, an anchor at a newline matches. */
unsigned
newline_anchor
:
1
;
};
typedef
struct
re_pattern_buffer
regex_t
;
/* Type for byte offsets within the string. POSIX mandates this. */
typedef
int
regoff_t
;
/* This is the structure we store register match data in. See
regex.texinfo for a full description of what registers match. */
struct
re_registers
{
unsigned
num_regs
;
regoff_t
*
start
;
regoff_t
*
end
;
};
/* If `regs_allocated' is REGS_UNALLOCATED in the pattern buffer,
`re_match_2' returns information about at least this many registers
the first time a `regs' structure is passed. */
#ifndef RE_NREGS
# define RE_NREGS 30
#endif
/* POSIX specification for registers. Aside from the different names than
`re_registers', POSIX uses an array of structures, instead of a
structure of arrays. */
typedef
struct
{
regoff_t
rm_so
;
/* Byte offset from string's start to substring's start. */
regoff_t
rm_eo
;
/* Byte offset from string's start to substring's end. */
}
regmatch_t
;
/* Declarations for routines. */
/* Sets the current default syntax to SYNTAX, and return the old syntax.
You can also simply assign to the `re_syntax_options' variable. */
extern
reg_syntax_t
re_set_syntax
(
reg_syntax_t
__syntax
);
/* Compile the regular expression PATTERN, with length LENGTH
and syntax given by the global `re_syntax_options', into the buffer
BUFFER. Return NULL if successful, and an error string if not. */
extern
const
char
*
re_compile_pattern
(
const
char
*
__pattern
,
size_t
__length
,
struct
re_pattern_buffer
*
__buffer
);
/* Compile a fastmap for the compiled pattern in BUFFER; used to
accelerate searches. Return 0 if successful and -2 if was an
internal error. */
extern
int
re_compile_fastmap
(
struct
re_pattern_buffer
*
__buffer
);
/* Search in the string STRING (with length LENGTH) for the pattern
compiled into BUFFER. Start searching at position START, for RANGE
characters. Return the starting position of the match, -1 for no
match, or -2 for an internal error. Also return register
information in REGS (if REGS and BUFFER->no_sub are nonzero). */
extern
int
re_search
(
struct
re_pattern_buffer
*
__buffer
,
const
char
*
__string
,
int
__length
,
int
__start
,
int
__range
,
struct
re_registers
*
__regs
);
/* Like `re_search', but search in the concatenation of STRING1 and
STRING2. Also, stop searching at index START + STOP. */
extern
int
re_search_2
(
struct
re_pattern_buffer
*
__buffer
,
const
char
*
__string1
,
int
__length1
,
const
char
*
__string2
,
int
__length2
,
int
__start
,
int
__range
,
struct
re_registers
*
__regs
,
int
__stop
);
/* Like `re_search', but return how many characters in STRING the regexp
in BUFFER matched, starting at position START. */
extern
int
re_match
(
struct
re_pattern_buffer
*
__buffer
,
const
char
*
__string
,
int
__length
,
int
__start
,
struct
re_registers
*
__regs
);
/* Relates to `re_match' as `re_search_2' relates to `re_search'. */
extern
int
re_match_2
(
struct
re_pattern_buffer
*
__buffer
,
const
char
*
__string1
,
int
__length1
,
const
char
*
__string2
,
int
__length2
,
int
__start
,
struct
re_registers
*
__regs
,
int
__stop
);
/* Set REGS to hold NUM_REGS registers, storing them in STARTS and
ENDS. Subsequent matches using BUFFER and REGS will use this memory
for recording register information. STARTS and ENDS must be
allocated with malloc, and must each be at least `NUM_REGS * sizeof
(regoff_t)' bytes long.
If NUM_REGS == 0, then subsequent matches should allocate their own
register data.
Unless this function is called, the first search or match using
PATTERN_BUFFER will allocate its own register data, without
freeing the old data. */
extern
void
re_set_registers
(
struct
re_pattern_buffer
*
__buffer
,
struct
re_registers
*
__regs
,
unsigned
int
__num_regs
,
regoff_t
*
__starts
,
regoff_t
*
__ends
);
#if defined _REGEX_RE_COMP || defined _LIBC
# ifndef _CRAY
/* 4.2 bsd compatibility. */
extern
char
*
re_comp
(
const
char
*
);
extern
int
re_exec
(
const
char
*
);
# endif
#endif
/* GCC 2.95 and later have "__restrict"; C99 compilers have
"restrict", and "configure" may have defined "restrict". */
#ifndef __restrict
# if ! (2 < __GNUC__ || (2 == __GNUC__ && 95 <= __GNUC_MINOR__))
# if defined restrict || 199901L <= __STDC_VERSION__
# define __restrict restrict
# else
# define __restrict
# endif
# endif
#endif
/* gcc 3.1 and up support the [restrict] syntax. */
#ifndef __restrict_arr
# if (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)) \
&& !defined __GNUG__
# define __restrict_arr __restrict
# else
# define __restrict_arr
# endif
#endif
/* POSIX compatibility. */
extern
int
regcomp
(
regex_t
*
__restrict
__preg
,
const
char
*
__restrict
__pattern
,
int
__cflags
);
extern
int
regexec
(
const
regex_t
*
__restrict
__preg
,
const
char
*
__restrict
__string
,
size_t
__nmatch
,
regmatch_t
__pmatch
[
__restrict_arr
],
int
__eflags
);
extern
size_t
regerror
(
int
__errcode
,
const
regex_t
*
__restrict
__preg
,
char
*
__restrict
__errbuf
,
size_t
__errbuf_size
);
extern
void
regfree
(
regex_t
*
__preg
);
#ifdef __cplusplus
}
#endif
/* C++ */
#endif
/* regex.h */
third_party/METIS/GKlib/graph.c
0 → 100644
View file @
3359c1f1
/*!
* \file
*
* \brief Various routines with dealing with sparse graphs
*
* \author George Karypis
* \version\verbatim $Id: graph.c 21482 2017-09-18 05:53:12Z karypis $ \endverbatim
*/
#include <GKlib.h>
#define OMPMINOPS 50000
/*************************************************************************/
/*! Allocate memory for a graph and initializes it
\returns the allocated graph. The various fields are set to NULL.
*/
/**************************************************************************/
gk_graph_t
*
gk_graph_Create
()
{
gk_graph_t
*
graph
;
graph
=
(
gk_graph_t
*
)
gk_malloc
(
sizeof
(
gk_graph_t
),
"gk_graph_Create: graph"
);
gk_graph_Init
(
graph
);
return
graph
;
}
/*************************************************************************/
/*! Initializes the graph.
\param graph is the graph to be initialized.
*/
/*************************************************************************/
void
gk_graph_Init
(
gk_graph_t
*
graph
)
{
memset
(
graph
,
0
,
sizeof
(
gk_graph_t
));
graph
->
nvtxs
=
-
1
;
}
/*************************************************************************/
/*! Frees all the memory allocated for a graph.
\param graph is the graph to be freed.
*/
/*************************************************************************/
void
gk_graph_Free
(
gk_graph_t
**
graph
)
{
if
(
*
graph
==
NULL
)
return
;
gk_graph_FreeContents
(
*
graph
);
gk_free
((
void
**
)
graph
,
LTERM
);
}
/*************************************************************************/
/*! Frees only the memory allocated for the graph's different fields and
sets them to NULL.
\param graph is the graph whose contents will be freed.
*/
/*************************************************************************/
void
gk_graph_FreeContents
(
gk_graph_t
*
graph
)
{
gk_free
((
void
*
)
&
graph
->
xadj
,
&
graph
->
adjncy
,
&
graph
->
iadjwgt
,
&
graph
->
fadjwgt
,
&
graph
->
ivwgts
,
&
graph
->
fvwgts
,
&
graph
->
ivsizes
,
&
graph
->
fvsizes
,
&
graph
->
vlabels
,
LTERM
);
}
/**************************************************************************/
/*! Reads a sparse graph from the supplied file
\param filename is the file that stores the data.
\param format is the graph format. The supported values are:
GK_GRAPH_FMT_METIS, GK_GRAPH_FMT_IJV.
\param hasvals is 1 if the input file has values
\param numbering is 1 if the input file numbering starts from one
\param isfewgts is 1 if the edge-weights should be read as floats
\param isfvwgts is 1 if the vertex-weights should be read as floats
\param isfvsizes is 1 if the vertex-sizes should be read as floats
\returns the graph that was read.
*/
/**************************************************************************/
gk_graph_t
*
gk_graph_Read
(
char
*
filename
,
int
format
,
int
hasvals
,
int
numbering
,
int
isfewgts
,
int
isfvwgts
,
int
isfvsizes
)
{
ssize_t
i
,
k
,
l
;
size_t
nfields
,
nvtxs
,
nedges
,
fmt
,
ncon
,
lnlen
;
ssize_t
*
xadj
;
int32_t
ival
,
*
iinds
=
NULL
,
*
jinds
=
NULL
,
*
ivals
=
NULL
,
*
adjncy
,
*
iadjwgt
;
float
fval
,
*
fvals
=
NULL
,
*
fadjwgt
;
int
readsizes
=
0
,
readwgts
=
0
,
readvals
=
0
;
char
*
line
=
NULL
,
*
head
,
*
tail
,
fmtstr
[
256
];
FILE
*
fpin
=
NULL
;
gk_graph_t
*
graph
=
NULL
;
if
(
!
gk_fexists
(
filename
))
gk_errexit
(
SIGERR
,
"File %s does not exist!
\n
"
,
filename
);
switch
(
format
)
{
case
GK_GRAPH_FMT_METIS
:
fpin
=
gk_fopen
(
filename
,
"r"
,
"gk_graph_Read: fpin"
);
do
{
if
(
gk_getline
(
&
line
,
&
lnlen
,
fpin
)
<=
0
)
gk_errexit
(
SIGERR
,
"Premature end of input file: file:%s
\n
"
,
filename
);
}
while
(
line
[
0
]
==
'%'
);
fmt
=
ncon
=
0
;
nfields
=
sscanf
(
line
,
"%zu %zu %zu %zu"
,
&
nvtxs
,
&
nedges
,
&
fmt
,
&
ncon
);
if
(
nfields
<
2
)
gk_errexit
(
SIGERR
,
"Header line must contain at least 2 integers (#vtxs and #edges).
\n
"
);
nedges
*=
2
;
if
(
fmt
>
111
)
gk_errexit
(
SIGERR
,
"Cannot read this type of file format [fmt=%zu]!
\n
"
,
fmt
);
sprintf
(
fmtstr
,
"%03zu"
,
fmt
%
1000
);
readsizes
=
(
fmtstr
[
0
]
==
'1'
);
readwgts
=
(
fmtstr
[
1
]
==
'1'
);
readvals
=
(
fmtstr
[
2
]
==
'1'
);
numbering
=
1
;
ncon
=
(
ncon
==
0
?
1
:
ncon
);
graph
=
gk_graph_Create
();
graph
->
nvtxs
=
nvtxs
;
graph
->
xadj
=
gk_zmalloc
(
nvtxs
+
1
,
"gk_graph_Read: xadj"
);
graph
->
adjncy
=
gk_i32malloc
(
nedges
,
"gk_graph_Read: adjncy"
);
if
(
readvals
)
{
if
(
isfewgts
)
graph
->
fadjwgt
=
gk_fmalloc
(
nedges
,
"gk_graph_Read: fadjwgt"
);
else
graph
->
iadjwgt
=
gk_i32malloc
(
nedges
,
"gk_graph_Read: iadjwgt"
);
}
if
(
readsizes
)
{
if
(
isfvsizes
)
graph
->
fvsizes
=
gk_fmalloc
(
nvtxs
,
"gk_graph_Read: fvsizes"
);
else
graph
->
ivsizes
=
gk_i32malloc
(
nvtxs
,
"gk_graph_Read: ivsizes"
);
}
if
(
readwgts
)
{
if
(
isfvwgts
)
graph
->
fvwgts
=
gk_fmalloc
(
nvtxs
*
ncon
,
"gk_graph_Read: fvwgts"
);
else
graph
->
ivwgts
=
gk_i32malloc
(
nvtxs
*
ncon
,
"gk_graph_Read: ivwgts"
);
}
/*----------------------------------------------------------------------
* Read the sparse graph file
*---------------------------------------------------------------------*/
numbering
=
(
numbering
?
-
1
:
0
);
for
(
graph
->
xadj
[
0
]
=
0
,
k
=
0
,
i
=
0
;
i
<
nvtxs
;
i
++
)
{
do
{
if
(
gk_getline
(
&
line
,
&
lnlen
,
fpin
)
==
-
1
)
gk_errexit
(
SIGERR
,
"Pregraphure end of input file: file while reading row %d
\n
"
,
i
);
}
while
(
line
[
0
]
==
'%'
);
head
=
line
;
tail
=
NULL
;
/* Read vertex sizes */
if
(
readsizes
)
{
if
(
isfvsizes
)
{
#ifdef __MSC__
graph
->
fvsizes
[
i
]
=
(
float
)
strtod
(
head
,
&
tail
);
#else
graph
->
fvsizes
[
i
]
=
strtof
(
head
,
&
tail
);
#endif
if
(
tail
==
head
)
gk_errexit
(
SIGERR
,
"The line for vertex %zd does not have size information
\n
"
,
i
+
1
);
if
(
graph
->
fvsizes
[
i
]
<
0
)
gk_errexit
(
SIGERR
,
"The size for vertex %zd must be >= 0
\n
"
,
i
+
1
);
}
else
{
graph
->
ivsizes
[
i
]
=
strtol
(
head
,
&
tail
,
0
);
if
(
tail
==
head
)
gk_errexit
(
SIGERR
,
"The line for vertex %zd does not have size information
\n
"
,
i
+
1
);
if
(
graph
->
ivsizes
[
i
]
<
0
)
gk_errexit
(
SIGERR
,
"The size for vertex %zd must be >= 0
\n
"
,
i
+
1
);
}
head
=
tail
;
}
/* Read vertex weights */
if
(
readwgts
)
{
for
(
l
=
0
;
l
<
ncon
;
l
++
)
{
if
(
isfvwgts
)
{
#ifdef __MSC__
graph
->
fvwgts
[
i
*
ncon
+
l
]
=
(
float
)
strtod
(
head
,
&
tail
);
#else
graph
->
fvwgts
[
i
*
ncon
+
l
]
=
strtof
(
head
,
&
tail
);
#endif
if
(
tail
==
head
)
gk_errexit
(
SIGERR
,
"The line for vertex %zd does not have enough weights "
"for the %d constraints.
\n
"
,
i
+
1
,
ncon
);
if
(
graph
->
fvwgts
[
i
*
ncon
+
l
]
<
0
)
gk_errexit
(
SIGERR
,
"The weight vertex %zd and constraint %zd must be >= 0
\n
"
,
i
+
1
,
l
);
}
else
{
graph
->
ivwgts
[
i
*
ncon
+
l
]
=
strtol
(
head
,
&
tail
,
0
);
if
(
tail
==
head
)
gk_errexit
(
SIGERR
,
"The line for vertex %zd does not have enough weights "
"for the %d constraints.
\n
"
,
i
+
1
,
ncon
);
if
(
graph
->
ivwgts
[
i
*
ncon
+
l
]
<
0
)
gk_errexit
(
SIGERR
,
"The weight vertex %zd and constraint %zd must be >= 0
\n
"
,
i
+
1
,
l
);
}
head
=
tail
;
}
}
/* Read the rest of the row */
while
(
1
)
{
ival
=
(
int
)
strtol
(
head
,
&
tail
,
0
);
if
(
tail
==
head
)
break
;
head
=
tail
;
if
((
graph
->
adjncy
[
k
]
=
ival
+
numbering
)
<
0
)
gk_errexit
(
SIGERR
,
"Error: Invalid column number %d at row %zd.
\n
"
,
ival
,
i
);
if
(
readvals
)
{
if
(
isfewgts
)
{
#ifdef __MSC__
fval
=
(
float
)
strtod
(
head
,
&
tail
);
#else
fval
=
strtof
(
head
,
&
tail
);
#endif
if
(
tail
==
head
)
gk_errexit
(
SIGERR
,
"Value could not be found for edge! Vertex:%zd, NNZ:%zd
\n
"
,
i
,
k
);
graph
->
fadjwgt
[
k
]
=
fval
;
}
else
{
ival
=
strtol
(
head
,
&
tail
,
0
);
if
(
tail
==
head
)
gk_errexit
(
SIGERR
,
"Value could not be found for edge! Vertex:%zd, NNZ:%zd
\n
"
,
i
,
k
);
graph
->
iadjwgt
[
k
]
=
ival
;
}
head
=
tail
;
}
k
++
;
}
graph
->
xadj
[
i
+
1
]
=
k
;
}
if
(
k
!=
nedges
)
gk_errexit
(
SIGERR
,
"gk_graph_Read: Something wrong with the number of edges in "
"the input file. nedges=%zd, Actualnedges=%zd.
\n
"
,
nedges
,
k
);
gk_fclose
(
fpin
);
gk_free
((
void
**
)
&
line
,
LTERM
);
break
;
case
GK_GRAPH_FMT_IJV
:
gk_getfilestats
(
filename
,
&
nvtxs
,
&
nedges
,
NULL
,
NULL
);
if
(
hasvals
==
1
&&
3
*
nvtxs
!=
nedges
)
gk_errexit
(
SIGERR
,
"Error: The number of numbers (%zd %d) in the input file is not a multiple of 3.
\n
"
,
nedges
,
hasvals
);
if
(
hasvals
==
0
&&
2
*
nvtxs
!=
nedges
)
gk_errexit
(
SIGERR
,
"Error: The number of numbers (%zd %d) in the input file is not a multiple of 2.
\n
"
,
nedges
,
hasvals
);
nedges
=
nvtxs
;
numbering
=
(
numbering
?
-
1
:
0
);
/* read the data into three arrays */
iinds
=
gk_i32malloc
(
nedges
,
"iinds"
);
jinds
=
gk_i32malloc
(
nedges
,
"jinds"
);
if
(
hasvals
)
{
if
(
isfewgts
)
fvals
=
gk_fmalloc
(
nedges
,
"fvals"
);
else
ivals
=
gk_i32malloc
(
nedges
,
"ivals"
);
}
fpin
=
gk_fopen
(
filename
,
"r"
,
"gk_graph_Read: fpin"
);
for
(
nvtxs
=
0
,
i
=
0
;
i
<
nedges
;
i
++
)
{
if
(
hasvals
)
{
if
(
isfewgts
)
{
if
(
fscanf
(
fpin
,
"%"
PRId32
" %"
PRId32
" %f"
,
&
iinds
[
i
],
&
jinds
[
i
],
&
fvals
[
i
])
!=
3
)
gk_errexit
(
SIGERR
,
"Error: Failed to read (i, j, val) for nedge: %zd.
\n
"
,
i
);
}
else
{
if
(
fscanf
(
fpin
,
"%"
PRId32
" %"
PRId32
" %"
PRId32
,
&
iinds
[
i
],
&
jinds
[
i
],
&
ivals
[
i
])
!=
3
)
gk_errexit
(
SIGERR
,
"Error: Failed to read (i, j, val) for nedge: %zd.
\n
"
,
i
);
}
}
else
{
if
(
fscanf
(
fpin
,
"%"
PRId32
" %"
PRId32
,
&
iinds
[
i
],
&
jinds
[
i
])
!=
2
)
gk_errexit
(
SIGERR
,
"Error: Failed to read (i, j) value for nedge: %zd.
\n
"
,
i
);
}
iinds
[
i
]
+=
numbering
;
jinds
[
i
]
+=
numbering
;
if
(
nvtxs
<
iinds
[
i
])
nvtxs
=
iinds
[
i
];
if
(
nvtxs
<
jinds
[
i
])
nvtxs
=
jinds
[
i
];
}
gk_fclose
(
fpin
);
/* convert (i, j, v) into a graph format */
graph
=
gk_graph_Create
();
graph
->
nvtxs
=
++
nvtxs
;
xadj
=
graph
->
xadj
=
gk_zsmalloc
(
nvtxs
+
1
,
0
,
"xadj"
);
adjncy
=
graph
->
adjncy
=
gk_i32malloc
(
nedges
,
"adjncy"
);
if
(
hasvals
)
{
if
(
isfewgts
)
fadjwgt
=
graph
->
fadjwgt
=
gk_fmalloc
(
nedges
,
"fadjwgt"
);
else
iadjwgt
=
graph
->
iadjwgt
=
gk_i32malloc
(
nedges
,
"iadjwgt"
);
}
for
(
i
=
0
;
i
<
nedges
;
i
++
)
xadj
[
iinds
[
i
]]
++
;
MAKECSR
(
i
,
nvtxs
,
xadj
);
for
(
i
=
0
;
i
<
nedges
;
i
++
)
{
adjncy
[
xadj
[
iinds
[
i
]]]
=
jinds
[
i
];
if
(
hasvals
)
{
if
(
isfewgts
)
fadjwgt
[
xadj
[
iinds
[
i
]]]
=
fvals
[
i
];
else
iadjwgt
[
xadj
[
iinds
[
i
]]]
=
ivals
[
i
];
}
xadj
[
iinds
[
i
]]
++
;
}
SHIFTCSR
(
i
,
nvtxs
,
xadj
);
gk_free
((
void
**
)
&
iinds
,
&
jinds
,
&
fvals
,
&
ivals
,
LTERM
);
break
;
default:
gk_errexit
(
SIGERR
,
"Unrecognized format: %d
\n
"
,
format
);
}
return
graph
;
}
/**************************************************************************/
/*! Writes a graph into a file.
\param graph is the graph to be written,
\param filename is the name of the output file.
\param format is one of GK_GRAPH_FMT_METIS specifying
the format of the output file.
*/
/**************************************************************************/
void
gk_graph_Write
(
gk_graph_t
*
graph
,
char
*
filename
,
int
format
)
{
ssize_t
i
,
j
;
int
hasvwgts
,
hasvsizes
,
hasewgts
;
FILE
*
fpout
;
if
(
format
!=
GK_GRAPH_FMT_METIS
)
gk_errexit
(
SIGERR
,
"Unknown file format. %d
\n
"
,
format
);
if
(
filename
)
fpout
=
gk_fopen
(
filename
,
"w"
,
"gk_graph_Write: fpout"
);
else
fpout
=
stdout
;
hasewgts
=
(
graph
->
iadjwgt
||
graph
->
fadjwgt
);
hasvwgts
=
(
graph
->
ivwgts
||
graph
->
fvwgts
);
hasvsizes
=
(
graph
->
ivsizes
||
graph
->
fvsizes
);
/* write the header line */
fprintf
(
fpout
,
"%d %zd"
,
graph
->
nvtxs
,
graph
->
xadj
[
graph
->
nvtxs
]
/
2
);
if
(
hasvwgts
||
hasvsizes
||
hasewgts
)
fprintf
(
fpout
,
" %d%d%d"
,
hasvsizes
,
hasvwgts
,
hasewgts
);
fprintf
(
fpout
,
"
\n
"
);
for
(
i
=
0
;
i
<
graph
->
nvtxs
;
i
++
)
{
if
(
hasvsizes
)
{
if
(
graph
->
ivsizes
)
fprintf
(
fpout
,
" %d"
,
graph
->
ivsizes
[
i
]);
else
fprintf
(
fpout
,
" %f"
,
graph
->
fvsizes
[
i
]);
}
if
(
hasvwgts
)
{
if
(
graph
->
ivwgts
)
fprintf
(
fpout
,
" %d"
,
graph
->
ivwgts
[
i
]);
else
fprintf
(
fpout
,
" %f"
,
graph
->
fvwgts
[
i
]);
}
for
(
j
=
graph
->
xadj
[
i
];
j
<
graph
->
xadj
[
i
+
1
];
j
++
)
{
fprintf
(
fpout
,
" %d"
,
graph
->
adjncy
[
j
]
+
1
);
if
(
hasewgts
)
{
if
(
graph
->
iadjwgt
)
fprintf
(
fpout
,
" %d"
,
graph
->
iadjwgt
[
j
]);
else
fprintf
(
fpout
,
" %f"
,
graph
->
fadjwgt
[
j
]);
}
}
fprintf
(
fpout
,
"
\n
"
);
}
if
(
filename
)
gk_fclose
(
fpout
);
}
/*************************************************************************/
/*! Returns a copy of a graph.
\param graph is the graph to be duplicated.
\returns the newly created copy of the graph.
*/
/**************************************************************************/
gk_graph_t
*
gk_graph_Dup
(
gk_graph_t
*
graph
)
{
gk_graph_t
*
ngraph
;
ngraph
=
gk_graph_Create
();
ngraph
->
nvtxs
=
graph
->
nvtxs
;
/* copy the adjacency structure */
if
(
graph
->
xadj
)
ngraph
->
xadj
=
gk_zcopy
(
graph
->
nvtxs
+
1
,
graph
->
xadj
,
gk_zmalloc
(
graph
->
nvtxs
+
1
,
"gk_graph_Dup: xadj"
));
if
(
graph
->
ivwgts
)
ngraph
->
ivwgts
=
gk_i32copy
(
graph
->
nvtxs
,
graph
->
ivwgts
,
gk_i32malloc
(
graph
->
nvtxs
,
"gk_graph_Dup: ivwgts"
));
if
(
graph
->
ivsizes
)
ngraph
->
ivsizes
=
gk_i32copy
(
graph
->
nvtxs
,
graph
->
ivsizes
,
gk_i32malloc
(
graph
->
nvtxs
,
"gk_graph_Dup: ivsizes"
));
if
(
graph
->
vlabels
)
ngraph
->
vlabels
=
gk_i32copy
(
graph
->
nvtxs
,
graph
->
vlabels
,
gk_i32malloc
(
graph
->
nvtxs
,
"gk_graph_Dup: ivlabels"
));
if
(
graph
->
fvwgts
)
ngraph
->
fvwgts
=
gk_fcopy
(
graph
->
nvtxs
,
graph
->
fvwgts
,
gk_fmalloc
(
graph
->
nvtxs
,
"gk_graph_Dup: fvwgts"
));
if
(
graph
->
fvsizes
)
ngraph
->
fvsizes
=
gk_fcopy
(
graph
->
nvtxs
,
graph
->
fvsizes
,
gk_fmalloc
(
graph
->
nvtxs
,
"gk_graph_Dup: fvsizes"
));
if
(
graph
->
adjncy
)
ngraph
->
adjncy
=
gk_i32copy
(
graph
->
xadj
[
graph
->
nvtxs
],
graph
->
adjncy
,
gk_i32malloc
(
graph
->
xadj
[
graph
->
nvtxs
],
"gk_graph_Dup: adjncy"
));
if
(
graph
->
iadjwgt
)
ngraph
->
iadjwgt
=
gk_i32copy
(
graph
->
xadj
[
graph
->
nvtxs
],
graph
->
iadjwgt
,
gk_i32malloc
(
graph
->
xadj
[
graph
->
nvtxs
],
"gk_graph_Dup: iadjwgt"
));
if
(
graph
->
fadjwgt
)
ngraph
->
fadjwgt
=
gk_fcopy
(
graph
->
xadj
[
graph
->
nvtxs
],
graph
->
fadjwgt
,
gk_fmalloc
(
graph
->
xadj
[
graph
->
nvtxs
],
"gk_graph_Dup: fadjwgt"
));
return
ngraph
;
}
/*************************************************************************/
/*! Returns a subgraph containing a set of consecutive vertices.
\param graph is the original graph.
\param vstart is the starting vertex.
\param nvtxs is the number of vertices from vstart to extract.
\returns the newly created subgraph.
*/
/**************************************************************************/
gk_graph_t
*
gk_graph_ExtractSubgraph
(
gk_graph_t
*
graph
,
int
vstart
,
int
nvtxs
)
{
ssize_t
i
;
gk_graph_t
*
ngraph
;
if
(
vstart
+
nvtxs
>
graph
->
nvtxs
)
return
NULL
;
ngraph
=
gk_graph_Create
();
ngraph
->
nvtxs
=
nvtxs
;
/* copy the adjancy structure */
if
(
graph
->
xadj
)
ngraph
->
xadj
=
gk_zcopy
(
nvtxs
+
1
,
graph
->
xadj
+
vstart
,
gk_zmalloc
(
nvtxs
+
1
,
"gk_graph_ExtractSubgraph: xadj"
));
for
(
i
=
nvtxs
;
i
>=
0
;
i
--
)
ngraph
->
xadj
[
i
]
-=
ngraph
->
xadj
[
0
];
ASSERT
(
ngraph
->
xadj
[
0
]
==
0
);
if
(
graph
->
ivwgts
)
ngraph
->
ivwgts
=
gk_i32copy
(
nvtxs
,
graph
->
ivwgts
+
vstart
,
gk_i32malloc
(
nvtxs
,
"gk_graph_ExtractSubgraph: ivwgts"
));
if
(
graph
->
ivsizes
)
ngraph
->
ivsizes
=
gk_i32copy
(
nvtxs
,
graph
->
ivsizes
+
vstart
,
gk_i32malloc
(
nvtxs
,
"gk_graph_ExtractSubgraph: ivsizes"
));
if
(
graph
->
vlabels
)
ngraph
->
vlabels
=
gk_i32copy
(
nvtxs
,
graph
->
vlabels
+
vstart
,
gk_i32malloc
(
nvtxs
,
"gk_graph_ExtractSubgraph: vlabels"
));
if
(
graph
->
fvwgts
)
ngraph
->
fvwgts
=
gk_fcopy
(
nvtxs
,
graph
->
fvwgts
+
vstart
,
gk_fmalloc
(
nvtxs
,
"gk_graph_ExtractSubgraph: fvwgts"
));
if
(
graph
->
fvsizes
)
ngraph
->
fvsizes
=
gk_fcopy
(
nvtxs
,
graph
->
fvsizes
+
vstart
,
gk_fmalloc
(
nvtxs
,
"gk_graph_ExtractSubgraph: fvsizes"
));
ASSERT
(
ngraph
->
xadj
[
nvtxs
]
==
graph
->
xadj
[
vstart
+
nvtxs
]
-
graph
->
xadj
[
vstart
]);
if
(
graph
->
adjncy
)
ngraph
->
adjncy
=
gk_i32copy
(
graph
->
xadj
[
vstart
+
nvtxs
]
-
graph
->
xadj
[
vstart
],
graph
->
adjncy
+
graph
->
xadj
[
vstart
],
gk_i32malloc
(
graph
->
xadj
[
vstart
+
nvtxs
]
-
graph
->
xadj
[
vstart
],
"gk_graph_ExtractSubgraph: adjncy"
));
if
(
graph
->
iadjwgt
)
ngraph
->
iadjwgt
=
gk_i32copy
(
graph
->
xadj
[
vstart
+
nvtxs
]
-
graph
->
xadj
[
vstart
],
graph
->
iadjwgt
+
graph
->
xadj
[
vstart
],
gk_i32malloc
(
graph
->
xadj
[
vstart
+
nvtxs
]
-
graph
->
xadj
[
vstart
],
"gk_graph_ExtractSubgraph: iadjwgt"
));
if
(
graph
->
fadjwgt
)
ngraph
->
fadjwgt
=
gk_fcopy
(
graph
->
xadj
[
vstart
+
nvtxs
]
-
graph
->
xadj
[
vstart
],
graph
->
fadjwgt
+
graph
->
xadj
[
vstart
],
gk_fmalloc
(
graph
->
xadj
[
vstart
+
nvtxs
]
-
graph
->
xadj
[
vstart
],
"gk_graph_ExtractSubgraph: fadjwgt"
));
return
ngraph
;
}
/*************************************************************************/
/*! Returns a graph that has been reordered according to the permutation.
\param[IN] graph is the graph to be re-ordered.
\param[IN] perm is the new ordering of the graph's vertices
\param[IN] iperm is the original ordering of the re-ordered graph's vertices
\returns the newly created copy of the graph.
\note Either perm or iperm can be NULL but not both.
*/
/**************************************************************************/
gk_graph_t
*
gk_graph_Reorder
(
gk_graph_t
*
graph
,
int32_t
*
perm
,
int32_t
*
iperm
)
{
ssize_t
j
,
jj
,
*
xadj
;
int
i
,
k
,
u
,
v
,
nvtxs
;
int
freeperm
=
0
,
freeiperm
=
0
;
int32_t
*
adjncy
;
gk_graph_t
*
ngraph
;
if
(
perm
==
NULL
&&
iperm
==
NULL
)
return
NULL
;
ngraph
=
gk_graph_Create
();
ngraph
->
nvtxs
=
nvtxs
=
graph
->
nvtxs
;
xadj
=
graph
->
xadj
;
adjncy
=
graph
->
adjncy
;
/* allocate memory for the different structures that are present in graph */
if
(
graph
->
xadj
)
ngraph
->
xadj
=
gk_zmalloc
(
nvtxs
+
1
,
"gk_graph_Reorder: xadj"
);
if
(
graph
->
ivwgts
)
ngraph
->
ivwgts
=
gk_i32malloc
(
nvtxs
,
"gk_graph_Reorder: ivwgts"
);
if
(
graph
->
ivsizes
)
ngraph
->
ivsizes
=
gk_i32malloc
(
nvtxs
,
"gk_graph_Reorder: ivsizes"
);
if
(
graph
->
vlabels
)
ngraph
->
vlabels
=
gk_i32malloc
(
nvtxs
,
"gk_graph_Reorder: ivlabels"
);
if
(
graph
->
fvwgts
)
ngraph
->
fvwgts
=
gk_fmalloc
(
nvtxs
,
"gk_graph_Reorder: fvwgts"
);
if
(
graph
->
fvsizes
)
ngraph
->
fvsizes
=
gk_fmalloc
(
nvtxs
,
"gk_graph_Reorder: fvsizes"
);
if
(
graph
->
adjncy
)
ngraph
->
adjncy
=
gk_i32malloc
(
graph
->
xadj
[
nvtxs
],
"gk_graph_Reorder: adjncy"
);
if
(
graph
->
iadjwgt
)
ngraph
->
iadjwgt
=
gk_i32malloc
(
graph
->
xadj
[
nvtxs
],
"gk_graph_Reorder: iadjwgt"
);
if
(
graph
->
fadjwgt
)
ngraph
->
fadjwgt
=
gk_fmalloc
(
graph
->
xadj
[
nvtxs
],
"gk_graph_Reorder: fadjwgt"
);
/* create perm/iperm if not provided */
if
(
perm
==
NULL
)
{
freeperm
=
1
;
perm
=
gk_i32malloc
(
nvtxs
,
"gk_graph_Reorder: perm"
);
for
(
i
=
0
;
i
<
nvtxs
;
i
++
)
perm
[
iperm
[
i
]]
=
i
;
}
if
(
iperm
==
NULL
)
{
freeiperm
=
1
;
iperm
=
gk_i32malloc
(
nvtxs
,
"gk_graph_Reorder: iperm"
);
for
(
i
=
0
;
i
<
nvtxs
;
i
++
)
iperm
[
perm
[
i
]]
=
i
;
}
/* fill-in the information of the re-ordered graph */
ngraph
->
xadj
[
0
]
=
jj
=
0
;
for
(
v
=
0
;
v
<
nvtxs
;
v
++
)
{
u
=
iperm
[
v
];
for
(
j
=
xadj
[
u
];
j
<
xadj
[
u
+
1
];
j
++
,
jj
++
)
{
ngraph
->
adjncy
[
jj
]
=
perm
[
adjncy
[
j
]];
if
(
graph
->
iadjwgt
)
ngraph
->
iadjwgt
[
jj
]
=
graph
->
iadjwgt
[
j
];
if
(
graph
->
fadjwgt
)
ngraph
->
fadjwgt
[
jj
]
=
graph
->
fadjwgt
[
j
];
}
if
(
graph
->
ivwgts
)
ngraph
->
ivwgts
[
v
]
=
graph
->
ivwgts
[
u
];
if
(
graph
->
fvwgts
)
ngraph
->
fvwgts
[
v
]
=
graph
->
fvwgts
[
u
];
if
(
graph
->
ivsizes
)
ngraph
->
ivsizes
[
v
]
=
graph
->
ivsizes
[
u
];
if
(
graph
->
fvsizes
)
ngraph
->
fvsizes
[
v
]
=
graph
->
fvsizes
[
u
];
if
(
graph
->
vlabels
)
ngraph
->
vlabels
[
v
]
=
graph
->
vlabels
[
u
];
ngraph
->
xadj
[
v
+
1
]
=
jj
;
}
/* free memory */
if
(
freeperm
)
gk_free
((
void
**
)
&
perm
,
LTERM
);
if
(
freeiperm
)
gk_free
((
void
**
)
&
iperm
,
LTERM
);
return
ngraph
;
}
/*************************************************************************/
/*! This function finds the connected components in a graph.
\param graph is the graph structure
\param cptr is the ptr structure of the CSR representation of the
components. The length of this vector must be graph->nvtxs+1.
\param cind is the indices structure of the CSR representation of
the components. The length of this vector must be graph->nvtxs.
\returns the number of components that it found.
\note The cptr and cind parameters can be NULL, in which case only the
number of connected components is returned.
*/
/*************************************************************************/
int
gk_graph_FindComponents
(
gk_graph_t
*
graph
,
int32_t
*
cptr
,
int32_t
*
cind
)
{
ssize_t
i
,
ii
,
j
,
jj
,
k
,
nvtxs
,
first
,
last
,
ntodo
,
ncmps
;
ssize_t
*
xadj
;
int32_t
*
adjncy
,
*
pos
,
*
todo
;
int32_t
mustfree_ccsr
=
0
;
nvtxs
=
graph
->
nvtxs
;
xadj
=
graph
->
xadj
;
adjncy
=
graph
->
adjncy
;
/* Deal with NULL supplied cptr/cind vectors */
if
(
cptr
==
NULL
)
{
cptr
=
gk_i32malloc
(
nvtxs
+
1
,
"gk_graph_FindComponents: cptr"
);
cind
=
gk_i32malloc
(
nvtxs
,
"gk_graph_FindComponents: cind"
);
mustfree_ccsr
=
1
;
}
/* The list of vertices that have not been touched yet.
The valid entries are from [0..ntodo). */
todo
=
gk_i32incset
(
nvtxs
,
0
,
gk_i32malloc
(
nvtxs
,
"gk_graph_FindComponents: todo"
));
/* For a vertex that has not been visited, pos[i] is the position in the
todo list that this vertex is stored.
If a vertex has been visited, pos[i] = -1. */
pos
=
gk_i32incset
(
nvtxs
,
0
,
gk_i32malloc
(
nvtxs
,
"gk_graph_FindComponents: pos"
));
/* Find the connected componends */
ncmps
=
-
1
;
ntodo
=
nvtxs
;
/* All vertices have not been visited */
first
=
last
=
0
;
/* Point to the first and last vertices that have been touched
but not explored.
These vertices are stored in cind[first]...cind[last-1]. */
while
(
1
)
{
if
(
first
==
last
)
{
/* Find another starting vertex */
cptr
[
++
ncmps
]
=
first
;
/* Mark the end of the current CC */
if
(
ntodo
>
0
)
{
/* put the first vertex in the todo list as the start of the new CC */
GKASSERT
(
pos
[
todo
[
0
]]
!=
-
1
);
cind
[
last
++
]
=
todo
[
0
];
pos
[
todo
[
0
]]
=
-
1
;
todo
[
0
]
=
todo
[
--
ntodo
];
pos
[
todo
[
0
]]
=
0
;
}
else
{
break
;
}
}
i
=
cind
[
first
++
];
/* Get the first visited but unexplored vertex */
for
(
j
=
xadj
[
i
];
j
<
xadj
[
i
+
1
];
j
++
)
{
k
=
adjncy
[
j
];
if
(
pos
[
k
]
!=
-
1
)
{
cind
[
last
++
]
=
k
;
/* Remove k from the todo list and put the last item in the todo
list at the position that k was so that the todo list will be
consequtive. The pos[] array is updated accordingly to keep track
the location of the vertices in the todo[] list. */
todo
[
pos
[
k
]]
=
todo
[
--
ntodo
];
pos
[
todo
[
pos
[
k
]]]
=
pos
[
k
];
pos
[
k
]
=
-
1
;
}
}
}
GKASSERT
(
first
==
nvtxs
);
if
(
mustfree_ccsr
)
gk_free
((
void
**
)
&
cptr
,
&
cind
,
LTERM
);
gk_free
((
void
**
)
&
pos
,
&
todo
,
LTERM
);
return
(
int
)
ncmps
;
}
/*************************************************************************/
/*! This function computes a permutation of the vertices based on a
breadth-first-traversal. It can be used for re-ordering the graph
to reduce its bandwidth for better cache locality.
The algorithm used is a simplified version of the method used to find
the connected components.
\param[IN] graph is the graph structure
\param[IN] v is the starting vertex of the BFS
\param[OUT] perm[i] stores the ID of vertex i in the re-ordered graph.
\param[OUT] iperm[i] stores the ID of the vertex that corresponds to
the ith vertex in the re-ordered graph.
\note The perm or iperm (but not both) can be NULL, at which point,
the corresponding arrays are not returned. Though the program
works fine when both are NULL, doing that is not smart.
The returned arrays should be freed with gk_free().
*/
/*************************************************************************/
void
gk_graph_ComputeBFSOrdering
(
gk_graph_t
*
graph
,
int
v
,
int32_t
**
r_perm
,
int32_t
**
r_iperm
)
{
ssize_t
j
,
*
xadj
;
int
i
,
k
,
nvtxs
,
first
,
last
;
int32_t
*
adjncy
,
*
cot
,
*
pos
;
if
(
graph
->
nvtxs
<=
0
)
return
;
nvtxs
=
graph
->
nvtxs
;
xadj
=
graph
->
xadj
;
adjncy
=
graph
->
adjncy
;
/* This array will function like pos + touched of the CC method */
pos
=
gk_i32incset
(
nvtxs
,
0
,
gk_i32malloc
(
nvtxs
,
"gk_graph_ComputeBFSOrdering: pos"
));
/* This array ([C]losed[O]pen[T]odo => cot) serves three purposes.
Positions from [0...first) is the current iperm[] vector of the explored vertices;
Positions from [first...last) is the OPEN list (i.e., visited vertices);
Positions from [last...nvtxs) is the todo list. */
cot
=
gk_i32incset
(
nvtxs
,
0
,
gk_i32malloc
(
nvtxs
,
"gk_graph_ComputeBFSOrdering: cot"
));
/* put v at the front of the todo list */
pos
[
0
]
=
cot
[
0
]
=
v
;
pos
[
v
]
=
cot
[
v
]
=
0
;
/* compute a BFS ordering from the seed vertex */
first
=
last
=
0
;
while
(
first
<
nvtxs
)
{
if
(
first
==
last
)
{
/* Find another starting vertex */
k
=
cot
[
last
];
ASSERT
(
pos
[
k
]
!=
-
1
);
pos
[
k
]
=
-
1
;
/* mark node as being visited */
last
++
;
}
i
=
cot
[
first
++
];
/* the ++ advances the explored vertices */
for
(
j
=
xadj
[
i
];
j
<
xadj
[
i
+
1
];
j
++
)
{
k
=
adjncy
[
j
];
/* if a node has already been visited, its pos[] will be -1 */
if
(
pos
[
k
]
!=
-
1
)
{
/* pos[k] is the location within cot[] where k resides (it is in the 'todo' part);
It is placed in that location cot[last] (end of OPEN list) that we
are about to overwrite and update pos[cot[last]] to reflect that. */
cot
[
pos
[
k
]]
=
cot
[
last
];
/* put the head of the todo list to
where k was in the todo list */
pos
[
cot
[
last
]]
=
pos
[
k
];
/* update perm to reflect the move */
cot
[
last
++
]
=
k
;
/* put node at the end of the OPEN list */
pos
[
k
]
=
-
1
;
/* mark node as being visited */
}
}
}
/* time to decide what to return */
if
(
r_perm
!=
NULL
)
{
/* use the 'pos' array to build the perm array */
for
(
i
=
0
;
i
<
nvtxs
;
i
++
)
pos
[
cot
[
i
]]
=
i
;
*
r_perm
=
pos
;
pos
=
NULL
;
}
if
(
r_iperm
!=
NULL
)
{
*
r_iperm
=
cot
;
cot
=
NULL
;
}
/* cleanup memory */
gk_free
((
void
**
)
&
pos
,
&
cot
,
LTERM
);
}
/*************************************************************************/
/*! This function computes a permutation of the vertices based on a
best-first-traversal. It can be used for re-ordering the graph
to reduce its bandwidth for better cache locality.
\param[IN] graph is the graph structure.
\param[IN] v is the starting vertex of the best-first traversal.
\param[IN] type indicates the criteria to use to measure the 'bestness'
of a vertex.
\param[OUT] perm[i] stores the ID of vertex i in the re-ordered graph.
\param[OUT] iperm[i] stores the ID of the vertex that corresponds to
the ith vertex in the re-ordered graph.
\note The perm or iperm (but not both) can be NULL, at which point,
the corresponding arrays are not returned. Though the program
works fine when both are NULL, doing that is not smart.
The returned arrays should be freed with gk_free().
*/
/*************************************************************************/
void
gk_graph_ComputeBestFOrdering0
(
gk_graph_t
*
graph
,
int
v
,
int
type
,
int32_t
**
r_perm
,
int32_t
**
r_iperm
)
{
ssize_t
j
,
jj
,
*
xadj
;
int
i
,
k
,
u
,
nvtxs
;
int32_t
*
adjncy
,
*
perm
,
*
degrees
,
*
minIDs
,
*
open
;
gk_i32pq_t
*
queue
;
if
(
graph
->
nvtxs
<=
0
)
return
;
nvtxs
=
graph
->
nvtxs
;
xadj
=
graph
->
xadj
;
adjncy
=
graph
->
adjncy
;
/* the degree of the vertices in the closed list */
degrees
=
gk_i32smalloc
(
nvtxs
,
0
,
"gk_graph_ComputeBestFOrdering: degrees"
);
/* the minimum vertex ID of an open vertex to the closed list */
minIDs
=
gk_i32smalloc
(
nvtxs
,
nvtxs
+
1
,
"gk_graph_ComputeBestFOrdering: minIDs"
);
/* the open list */
open
=
gk_i32malloc
(
nvtxs
,
"gk_graph_ComputeBestFOrdering: open"
);
/* if perm[i] >= 0, then perm[i] is the order of vertex i;
otherwise perm[i] == -1.
*/
perm
=
gk_i32smalloc
(
nvtxs
,
-
1
,
"gk_graph_ComputeBestFOrdering: perm"
);
/* create the queue and put everything in it */
queue
=
gk_i32pqCreate
(
nvtxs
);
for
(
i
=
0
;
i
<
nvtxs
;
i
++
)
gk_i32pqInsert
(
queue
,
i
,
0
);
gk_i32pqUpdate
(
queue
,
v
,
1
);
open
[
0
]
=
v
;
/* start processing the nodes */
for
(
i
=
0
;
i
<
nvtxs
;
i
++
)
{
if
((
v
=
gk_i32pqGetTop
(
queue
))
==
-
1
)
gk_errexit
(
SIGERR
,
"The priority queue got empty ahead of time [i=%d].
\n
"
,
i
);
if
(
perm
[
v
]
!=
-
1
)
gk_errexit
(
SIGERR
,
"The perm[%d] has already been set.
\n
"
,
v
);
perm
[
v
]
=
i
;
for
(
j
=
xadj
[
v
];
j
<
xadj
[
v
+
1
];
j
++
)
{
u
=
adjncy
[
j
];
if
(
perm
[
u
]
==
-
1
)
{
degrees
[
u
]
++
;
minIDs
[
u
]
=
(
i
<
minIDs
[
u
]
?
i
:
minIDs
[
u
]);
switch
(
type
)
{
case
1
:
/* DFS */
gk_i32pqUpdate
(
queue
,
u
,
1
);
break
;
case
2
:
/* Max in closed degree */
gk_i32pqUpdate
(
queue
,
u
,
degrees
[
u
]);
break
;
case
3
:
/* Sum of orders in closed list */
for
(
k
=
0
,
jj
=
xadj
[
u
];
jj
<
xadj
[
u
+
1
];
jj
++
)
{
if
(
perm
[
adjncy
[
jj
]]
!=
-
1
)
k
+=
perm
[
adjncy
[
jj
]];
}
gk_i32pqUpdate
(
queue
,
u
,
k
);
break
;
case
4
:
/* Sum of order-differences (w.r.t. current number) in closed
list (updated once in a while) */
for
(
k
=
0
,
jj
=
xadj
[
u
];
jj
<
xadj
[
u
+
1
];
jj
++
)
{
if
(
perm
[
adjncy
[
jj
]]
!=
-
1
)
k
+=
(
i
-
perm
[
adjncy
[
jj
]]);
}
gk_i32pqUpdate
(
queue
,
u
,
k
);
break
;
default:
;
}
}
}
}
/* time to decide what to return */
if
(
r_perm
!=
NULL
)
{
*
r_perm
=
perm
;
perm
=
NULL
;
}
if
(
r_iperm
!=
NULL
)
{
/* use the 'degrees' array to build the iperm array */
for
(
i
=
0
;
i
<
nvtxs
;
i
++
)
degrees
[
perm
[
i
]]
=
i
;
*
r_iperm
=
degrees
;
degrees
=
NULL
;
}
/* cleanup memory */
gk_i32pqDestroy
(
queue
);
gk_free
((
void
**
)
&
perm
,
&
degrees
,
&
minIDs
,
&
open
,
LTERM
);
}
/*************************************************************************/
/*! This function computes a permutation of the vertices based on a
best-first-traversal. It can be used for re-ordering the graph
to reduce its bandwidth for better cache locality.
\param[IN] graph is the graph structure.
\param[IN] v is the starting vertex of the best-first traversal.
\param[IN] type indicates the criteria to use to measure the 'bestness'
of a vertex.
\param[OUT] perm[i] stores the ID of vertex i in the re-ordered graph.
\param[OUT] iperm[i] stores the ID of the vertex that corresponds to
the ith vertex in the re-ordered graph.
\note The perm or iperm (but not both) can be NULL, at which point,
the corresponding arrays are not returned. Though the program
works fine when both are NULL, doing that is not smart.
The returned arrays should be freed with gk_free().
*/
/*************************************************************************/
void
gk_graph_ComputeBestFOrdering
(
gk_graph_t
*
graph
,
int
v
,
int
type
,
int32_t
**
r_perm
,
int32_t
**
r_iperm
)
{
ssize_t
j
,
jj
,
*
xadj
;
int
i
,
k
,
u
,
nvtxs
,
nopen
,
ntodo
;
int32_t
*
adjncy
,
*
perm
,
*
degrees
,
*
sod
,
*
level
,
*
ot
,
*
pos
;
int64_t
*
wdegrees
;
gk_i32pq_t
*
queue
;
if
(
graph
->
nvtxs
<=
0
)
return
;
nvtxs
=
graph
->
nvtxs
;
xadj
=
graph
->
xadj
;
adjncy
=
graph
->
adjncy
;
/* the degree of the vertices in the closed list */
degrees
=
gk_i32smalloc
(
nvtxs
,
0
,
"gk_graph_ComputeBestFOrdering: degrees"
);
/* the weighted degree of the vertices in the closed list for type==3 */
wdegrees
=
gk_i64smalloc
(
nvtxs
,
0
,
"gk_graph_ComputeBestFOrdering: wdegrees"
);
/* the sum of differences for type==4 */
sod
=
gk_i32smalloc
(
nvtxs
,
0
,
"gk_graph_ComputeBestFOrdering: sod"
);
/* the encountering level of a vertex type==5 */
level
=
gk_i32smalloc
(
nvtxs
,
0
,
"gk_graph_ComputeBestFOrdering: level"
);
/* The open+todo list of vertices.
The vertices from [0..nopen] are the open vertices.
The vertices from [nopen..ntodo) are the todo vertices.
*/
ot
=
gk_i32incset
(
nvtxs
,
0
,
gk_i32malloc
(
nvtxs
,
"gk_graph_FindComponents: ot"
));
/* For a vertex that has not been explored, pos[i] is the position in the ot list. */
pos
=
gk_i32incset
(
nvtxs
,
0
,
gk_i32malloc
(
nvtxs
,
"gk_graph_FindComponents: pos"
));
/* if perm[i] >= 0, then perm[i] is the order of vertex i; otherwise perm[i] == -1. */
perm
=
gk_i32smalloc
(
nvtxs
,
-
1
,
"gk_graph_ComputeBestFOrdering: perm"
);
/* create the queue and put the starting vertex in it */
queue
=
gk_i32pqCreate
(
nvtxs
);
gk_i32pqInsert
(
queue
,
v
,
1
);
/* put v at the front of the open list */
pos
[
0
]
=
ot
[
0
]
=
v
;
pos
[
v
]
=
ot
[
v
]
=
0
;
nopen
=
1
;
ntodo
=
nvtxs
;
/* start processing the nodes */
for
(
i
=
0
;
i
<
nvtxs
;
i
++
)
{
if
(
nopen
==
0
)
{
/* deal with non-connected graphs */
gk_i32pqInsert
(
queue
,
ot
[
0
],
1
);
nopen
++
;
}
if
((
v
=
gk_i32pqGetTop
(
queue
))
==
-
1
)
gk_errexit
(
SIGERR
,
"The priority queue got empty ahead of time [i=%d].
\n
"
,
i
);
if
(
perm
[
v
]
!=
-
1
)
gk_errexit
(
SIGERR
,
"The perm[%d] has already been set.
\n
"
,
v
);
perm
[
v
]
=
i
;
if
(
ot
[
pos
[
v
]]
!=
v
)
gk_errexit
(
SIGERR
,
"Something went wrong [ot[pos[%d]]!=%d.
\n
"
,
v
,
v
);
if
(
pos
[
v
]
>=
nopen
)
gk_errexit
(
SIGERR
,
"The position of v is not in open list. pos[%d]=%d is >=%d.
\n
"
,
v
,
pos
[
v
],
nopen
);
/* remove v from the open list and re-arrange the todo part of the list */
ot
[
pos
[
v
]]
=
ot
[
nopen
-
1
];
pos
[
ot
[
nopen
-
1
]]
=
pos
[
v
];
if
(
ntodo
>
nopen
)
{
ot
[
nopen
-
1
]
=
ot
[
ntodo
-
1
];
pos
[
ot
[
ntodo
-
1
]]
=
nopen
-
1
;
}
nopen
--
;
ntodo
--
;
for
(
j
=
xadj
[
v
];
j
<
xadj
[
v
+
1
];
j
++
)
{
u
=
adjncy
[
j
];
if
(
perm
[
u
]
==
-
1
)
{
/* update ot list, if u is not in the open list by putting it at the end
of the open list. */
if
(
degrees
[
u
]
==
0
)
{
ot
[
pos
[
u
]]
=
ot
[
nopen
];
pos
[
ot
[
nopen
]]
=
pos
[
u
];
ot
[
nopen
]
=
u
;
pos
[
u
]
=
nopen
;
nopen
++
;
level
[
u
]
=
level
[
v
]
+
1
;
gk_i32pqInsert
(
queue
,
u
,
0
);
}
/* update the in-closed degree */
degrees
[
u
]
++
;
/* update the queues based on the type */
switch
(
type
)
{
case
1
:
/* DFS */
gk_i32pqUpdate
(
queue
,
u
,
1000
*
(
i
+
1
)
+
degrees
[
u
]);
break
;
case
2
:
/* Max in closed degree */
gk_i32pqUpdate
(
queue
,
u
,
degrees
[
u
]);
break
;
case
3
:
/* Sum of orders in closed list */
wdegrees
[
u
]
+=
i
;
gk_i32pqUpdate
(
queue
,
u
,
(
int32_t
)
sqrt
(
wdegrees
[
u
]));
break
;
case
4
:
/* Sum of order-differences */
/* this is handled at the end of the loop */
;
break
;
case
5
:
/* BFS with in degree priority */
gk_i32pqUpdate
(
queue
,
u
,
-
(
1000
*
level
[
u
]
-
degrees
[
u
]));
break
;
case
6
:
/* Hybrid of 1+2 */
gk_i32pqUpdate
(
queue
,
u
,
(
i
+
1
)
*
degrees
[
u
]);
break
;
default:
;
}
}
}
if
(
type
==
4
)
{
/* update all the vertices in the open list */
for
(
j
=
0
;
j
<
nopen
;
j
++
)
{
u
=
ot
[
j
];
if
(
perm
[
u
]
!=
-
1
)
gk_errexit
(
SIGERR
,
"For i=%d, the open list contains a closed vertex: ot[%zd]=%d, perm[%d]=%d.
\n
"
,
i
,
j
,
u
,
u
,
perm
[
u
]);
sod
[
u
]
+=
degrees
[
u
];
if
(
i
<
1000
||
i
%
25
==
0
)
gk_i32pqUpdate
(
queue
,
u
,
sod
[
u
]);
}
}
/*
for (j=0; j<ntodo; j++) {
if (pos[ot[j]] != j)
gk_errexit(SIGERR, "pos[ot[%zd]] != %zd.\n", j, j);
}
*/
}
/* time to decide what to return */
if
(
r_perm
!=
NULL
)
{
*
r_perm
=
perm
;
perm
=
NULL
;
}
if
(
r_iperm
!=
NULL
)
{
/* use the 'degrees' array to build the iperm array */
for
(
i
=
0
;
i
<
nvtxs
;
i
++
)
degrees
[
perm
[
i
]]
=
i
;
*
r_iperm
=
degrees
;
degrees
=
NULL
;
}
/* cleanup memory */
gk_i32pqDestroy
(
queue
);
gk_free
((
void
**
)
&
perm
,
&
degrees
,
&
wdegrees
,
&
sod
,
&
ot
,
&
pos
,
&
level
,
LTERM
);
}
/*************************************************************************/
/*! This function computes the single-source shortest path lengths from the
root node to all the other nodes in the graph. If the graph is not
connected then, the sortest part to the vertices in the other components
is -1.
\param[IN] graph is the graph structure.
\param[IN] v is the root of the single-source shortest path computations.
\param[IN] type indicates the criteria to use to measure the 'bestness'
of a vertex.
\param[OUT] sps[i] stores the length of the shortest path from v to vertex i.
If no such path exists, then it is -1. Note that the returned
array will be either an array of int32_t or an array of floats.
The specific type is determined by the existance of non NULL
iadjwgt and fadjwgt arrays. If both of these arrays exist, then
priority is given to iadjwgt.
\note The returned array should be freed with gk_free().
*/
/*************************************************************************/
void
gk_graph_SingleSourceShortestPaths
(
gk_graph_t
*
graph
,
int
v
,
void
**
r_sps
)
{
ssize_t
*
xadj
;
int
i
,
u
,
nvtxs
;
int32_t
*
adjncy
,
*
inqueue
;
if
(
graph
->
nvtxs
<=
0
)
return
;
nvtxs
=
graph
->
nvtxs
;
xadj
=
graph
->
xadj
;
adjncy
=
graph
->
adjncy
;
inqueue
=
gk_i32smalloc
(
nvtxs
,
0
,
"gk_graph_SingleSourceShortestPaths: inqueue"
);
/* determine if you will be computing using int32_t or float and proceed from there */
if
(
graph
->
iadjwgt
!=
NULL
)
{
gk_i32pq_t
*
queue
;
int32_t
*
adjwgt
;
int32_t
*
sps
;
adjwgt
=
graph
->
iadjwgt
;
queue
=
gk_i32pqCreate
(
nvtxs
);
gk_i32pqInsert
(
queue
,
v
,
0
);
inqueue
[
v
]
=
1
;
sps
=
gk_i32smalloc
(
nvtxs
,
-
1
,
"gk_graph_SingleSourceShortestPaths: sps"
);
sps
[
v
]
=
0
;
/* start processing the nodes */
while
((
v
=
gk_i32pqGetTop
(
queue
))
!=
-
1
)
{
inqueue
[
v
]
=
2
;
/* relax the adjacent edges */
for
(
i
=
xadj
[
v
];
i
<
xadj
[
v
+
1
];
i
++
)
{
u
=
adjncy
[
i
];
if
(
inqueue
[
u
]
==
2
)
continue
;
if
(
sps
[
u
]
<
0
||
sps
[
v
]
+
adjwgt
[
i
]
<
sps
[
u
])
{
sps
[
u
]
=
sps
[
v
]
+
adjwgt
[
i
];
if
(
inqueue
[
u
])
gk_i32pqUpdate
(
queue
,
u
,
-
sps
[
u
]);
else
{
gk_i32pqInsert
(
queue
,
u
,
-
sps
[
u
]);
inqueue
[
u
]
=
1
;
}
}
}
}
*
r_sps
=
(
void
*
)
sps
;
gk_i32pqDestroy
(
queue
);
}
else
{
gk_fpq_t
*
queue
;
float
*
adjwgt
;
float
*
sps
;
adjwgt
=
graph
->
fadjwgt
;
queue
=
gk_fpqCreate
(
nvtxs
);
gk_fpqInsert
(
queue
,
v
,
0
);
inqueue
[
v
]
=
1
;
sps
=
gk_fsmalloc
(
nvtxs
,
-
1
,
"gk_graph_SingleSourceShortestPaths: sps"
);
sps
[
v
]
=
0
;
/* start processing the nodes */
while
((
v
=
gk_fpqGetTop
(
queue
))
!=
-
1
)
{
inqueue
[
v
]
=
2
;
/* relax the adjacent edges */
for
(
i
=
xadj
[
v
];
i
<
xadj
[
v
+
1
];
i
++
)
{
u
=
adjncy
[
i
];
if
(
inqueue
[
u
]
==
2
)
continue
;
if
(
sps
[
u
]
<
0
||
sps
[
v
]
+
adjwgt
[
i
]
<
sps
[
u
])
{
sps
[
u
]
=
sps
[
v
]
+
adjwgt
[
i
];
if
(
inqueue
[
u
])
gk_fpqUpdate
(
queue
,
u
,
-
sps
[
u
]);
else
{
gk_fpqInsert
(
queue
,
u
,
-
sps
[
u
]);
inqueue
[
u
]
=
1
;
}
}
}
}
*
r_sps
=
(
void
*
)
sps
;
gk_fpqDestroy
(
queue
);
}
gk_free
((
void
**
)
&
inqueue
,
LTERM
);
}
#ifdef XXX
/*************************************************************************/
/*! Sorts the adjacency lists in increasing vertex order
\param graph the graph itself,
*/
/**************************************************************************/
void
gk_graph_SortAdjacencies
(
gk_graph_t
*
graph
)
{
int
n
,
nn
=
0
;
ssize_t
*
ptr
;
int
*
ind
;
float
*
val
;
switch
(
what
)
{
case
GK_CSR_ROW
:
if
(
!
graph
->
rowptr
)
gk_errexit
(
SIGERR
,
"Row-based view of the graphrix does not exists.
\n
"
);
n
=
graph
->
nrows
;
ptr
=
graph
->
rowptr
;
ind
=
graph
->
rowind
;
val
=
graph
->
rowval
;
break
;
case
GK_CSR_COL
:
if
(
!
graph
->
colptr
)
gk_errexit
(
SIGERR
,
"Column-based view of the graphrix does not exists.
\n
"
);
n
=
graph
->
ncols
;
ptr
=
graph
->
colptr
;
ind
=
graph
->
colind
;
val
=
graph
->
colval
;
break
;
default:
gk_errexit
(
SIGERR
,
"Invalid index type of %d.
\n
"
,
what
);
return
;
}
#pragma omp parallel if (n > 100)
{
ssize_t
i
,
j
,
k
;
gk_ikv_t
*
cand
;
float
*
tval
;
#pragma omp single
for
(
i
=
0
;
i
<
n
;
i
++
)
nn
=
gk_max
(
nn
,
ptr
[
i
+
1
]
-
ptr
[
i
]);
cand
=
gk_ikvmalloc
(
nn
,
"gk_graph_SortIndices: cand"
);
tval
=
gk_fmalloc
(
nn
,
"gk_graph_SortIndices: tval"
);
#pragma omp for schedule(static)
for
(
i
=
0
;
i
<
n
;
i
++
)
{
for
(
k
=
0
,
j
=
ptr
[
i
];
j
<
ptr
[
i
+
1
];
j
++
)
{
if
(
j
>
ptr
[
i
]
&&
ind
[
j
]
<
ind
[
j
-
1
])
k
=
1
;
/* an inversion */
cand
[
j
-
ptr
[
i
]].
val
=
j
-
ptr
[
i
];
cand
[
j
-
ptr
[
i
]].
key
=
ind
[
j
];
tval
[
j
-
ptr
[
i
]]
=
val
[
j
];
}
if
(
k
)
{
gk_ikvsorti
(
ptr
[
i
+
1
]
-
ptr
[
i
],
cand
);
for
(
j
=
ptr
[
i
];
j
<
ptr
[
i
+
1
];
j
++
)
{
ind
[
j
]
=
cand
[
j
-
ptr
[
i
]].
key
;
val
[
j
]
=
tval
[
cand
[
j
-
ptr
[
i
]].
val
];
}
}
}
gk_free
((
void
**
)
&
cand
,
&
tval
,
LTERM
);
}
}
/*************************************************************************/
/*! Returns a subgraphrix containing a certain set of rows.
\param graph is the original graphrix.
\param nrows is the number of rows to extract.
\param rind is the set of row numbers to extract.
\returns the row structure of the newly created subgraphrix.
*/
/**************************************************************************/
gk_graph_t
*
gk_graph_ExtractRows
(
gk_graph_t
*
graph
,
int
nrows
,
int
*
rind
)
{
ssize_t
i
,
ii
,
j
,
nnz
;
gk_graph_t
*
ngraph
;
ngraph
=
gk_graph_Create
();
ngraph
->
nrows
=
nrows
;
ngraph
->
ncols
=
graph
->
ncols
;
for
(
nnz
=
0
,
i
=
0
;
i
<
nrows
;
i
++
)
nnz
+=
graph
->
rowptr
[
rind
[
i
]
+
1
]
-
graph
->
rowptr
[
rind
[
i
]];
ngraph
->
rowptr
=
gk_zmalloc
(
ngraph
->
nrows
+
1
,
"gk_graph_ExtractPartition: rowptr"
);
ngraph
->
rowind
=
gk_imalloc
(
nnz
,
"gk_graph_ExtractPartition: rowind"
);
ngraph
->
rowval
=
gk_fmalloc
(
nnz
,
"gk_graph_ExtractPartition: rowval"
);
ngraph
->
rowptr
[
0
]
=
0
;
for
(
nnz
=
0
,
j
=
0
,
ii
=
0
;
ii
<
nrows
;
ii
++
)
{
i
=
rind
[
ii
];
gk_icopy
(
graph
->
rowptr
[
i
+
1
]
-
graph
->
rowptr
[
i
],
graph
->
rowind
+
graph
->
rowptr
[
i
],
ngraph
->
rowind
+
nnz
);
gk_fcopy
(
graph
->
rowptr
[
i
+
1
]
-
graph
->
rowptr
[
i
],
graph
->
rowval
+
graph
->
rowptr
[
i
],
ngraph
->
rowval
+
nnz
);
nnz
+=
graph
->
rowptr
[
i
+
1
]
-
graph
->
rowptr
[
i
];
ngraph
->
rowptr
[
++
j
]
=
nnz
;
}
ASSERT
(
j
==
ngraph
->
nrows
);
return
ngraph
;
}
/*************************************************************************/
/*! Returns a subgraphrix corresponding to a specified partitioning of rows.
\param graph is the original graphrix.
\param part is the partitioning vector of the rows.
\param pid is the partition ID that will be extracted.
\returns the row structure of the newly created subgraphrix.
*/
/**************************************************************************/
gk_graph_t
*
gk_graph_ExtractPartition
(
gk_graph_t
*
graph
,
int
*
part
,
int
pid
)
{
ssize_t
i
,
j
,
nnz
;
gk_graph_t
*
ngraph
;
ngraph
=
gk_graph_Create
();
ngraph
->
nrows
=
0
;
ngraph
->
ncols
=
graph
->
ncols
;
for
(
nnz
=
0
,
i
=
0
;
i
<
graph
->
nrows
;
i
++
)
{
if
(
part
[
i
]
==
pid
)
{
ngraph
->
nrows
++
;
nnz
+=
graph
->
rowptr
[
i
+
1
]
-
graph
->
rowptr
[
i
];
}
}
ngraph
->
rowptr
=
gk_zmalloc
(
ngraph
->
nrows
+
1
,
"gk_graph_ExtractPartition: rowptr"
);
ngraph
->
rowind
=
gk_imalloc
(
nnz
,
"gk_graph_ExtractPartition: rowind"
);
ngraph
->
rowval
=
gk_fmalloc
(
nnz
,
"gk_graph_ExtractPartition: rowval"
);
ngraph
->
rowptr
[
0
]
=
0
;
for
(
nnz
=
0
,
j
=
0
,
i
=
0
;
i
<
graph
->
nrows
;
i
++
)
{
if
(
part
[
i
]
==
pid
)
{
gk_icopy
(
graph
->
rowptr
[
i
+
1
]
-
graph
->
rowptr
[
i
],
graph
->
rowind
+
graph
->
rowptr
[
i
],
ngraph
->
rowind
+
nnz
);
gk_fcopy
(
graph
->
rowptr
[
i
+
1
]
-
graph
->
rowptr
[
i
],
graph
->
rowval
+
graph
->
rowptr
[
i
],
ngraph
->
rowval
+
nnz
);
nnz
+=
graph
->
rowptr
[
i
+
1
]
-
graph
->
rowptr
[
i
];
ngraph
->
rowptr
[
++
j
]
=
nnz
;
}
}
ASSERT
(
j
==
ngraph
->
nrows
);
return
ngraph
;
}
/*************************************************************************/
/*! Splits the graphrix into multiple sub-graphrices based on the provided
color array.
\param graph is the original graphrix.
\param color is an array of size equal to the number of non-zeros
in the graphrix (row-wise structure). The graphrix is split into
as many parts as the number of colors. For meaningfull results,
the colors should be numbered consecutively starting from 0.
\returns an array of graphrices for each supplied color number.
*/
/**************************************************************************/
gk_graph_t
**
gk_graph_Split
(
gk_graph_t
*
graph
,
int
*
color
)
{
ssize_t
i
,
j
;
int
nrows
,
ncolors
;
ssize_t
*
rowptr
;
int
*
rowind
;
float
*
rowval
;
gk_graph_t
**
sgraphs
;
nrows
=
graph
->
nrows
;
rowptr
=
graph
->
rowptr
;
rowind
=
graph
->
rowind
;
rowval
=
graph
->
rowval
;
ncolors
=
gk_imax
(
rowptr
[
nrows
],
color
)
+
1
;
sgraphs
=
(
gk_graph_t
**
)
gk_malloc
(
sizeof
(
gk_graph_t
*
)
*
ncolors
,
"gk_graph_Split: sgraphs"
);
for
(
i
=
0
;
i
<
ncolors
;
i
++
)
{
sgraphs
[
i
]
=
gk_graph_Create
();
sgraphs
[
i
]
->
nrows
=
graph
->
nrows
;
sgraphs
[
i
]
->
ncols
=
graph
->
ncols
;
sgraphs
[
i
]
->
rowptr
=
gk_zsmalloc
(
nrows
+
1
,
0
,
"gk_graph_Split: sgraphs[i]->rowptr"
);
}
for
(
i
=
0
;
i
<
nrows
;
i
++
)
{
for
(
j
=
rowptr
[
i
];
j
<
rowptr
[
i
+
1
];
j
++
)
sgraphs
[
color
[
j
]]
->
rowptr
[
i
]
++
;
}
for
(
i
=
0
;
i
<
ncolors
;
i
++
)
MAKECSR
(
j
,
nrows
,
sgraphs
[
i
]
->
rowptr
);
for
(
i
=
0
;
i
<
ncolors
;
i
++
)
{
sgraphs
[
i
]
->
rowind
=
gk_imalloc
(
sgraphs
[
i
]
->
rowptr
[
nrows
],
"gk_graph_Split: sgraphs[i]->rowind"
);
sgraphs
[
i
]
->
rowval
=
gk_fmalloc
(
sgraphs
[
i
]
->
rowptr
[
nrows
],
"gk_graph_Split: sgraphs[i]->rowval"
);
}
for
(
i
=
0
;
i
<
nrows
;
i
++
)
{
for
(
j
=
rowptr
[
i
];
j
<
rowptr
[
i
+
1
];
j
++
)
{
sgraphs
[
color
[
j
]]
->
rowind
[
sgraphs
[
color
[
j
]]
->
rowptr
[
i
]]
=
rowind
[
j
];
sgraphs
[
color
[
j
]]
->
rowval
[
sgraphs
[
color
[
j
]]
->
rowptr
[
i
]]
=
rowval
[
j
];
sgraphs
[
color
[
j
]]
->
rowptr
[
i
]
++
;
}
}
for
(
i
=
0
;
i
<
ncolors
;
i
++
)
SHIFTCSR
(
j
,
nrows
,
sgraphs
[
i
]
->
rowptr
);
return
sgraphs
;
}
/*************************************************************************/
/*! Prunes certain rows/columns of the graphrix. The prunning takes place
by analyzing the row structure of the graphrix. The prunning takes place
by removing rows/columns but it does not affect the numbering of the
remaining rows/columns.
\param graph the graphrix to be prunned,
\param what indicates if the rows (GK_CSR_ROW) or the columns (GK_CSR_COL)
of the graphrix will be prunned,
\param minf is the minimum number of rows (columns) that a column (row) must
be present in order to be kept,
\param maxf is the maximum number of rows (columns) that a column (row) must
be present at in order to be kept.
\returns the prunned graphrix consisting only of its row-based structure.
The input graphrix is not modified.
*/
/**************************************************************************/
gk_graph_t
*
gk_graph_Prune
(
gk_graph_t
*
graph
,
int
what
,
int
minf
,
int
maxf
)
{
ssize_t
i
,
j
,
nnz
;
int
nrows
,
ncols
;
ssize_t
*
rowptr
,
*
nrowptr
;
int
*
rowind
,
*
nrowind
,
*
collen
;
float
*
rowval
,
*
nrowval
;
gk_graph_t
*
ngraph
;
ngraph
=
gk_graph_Create
();
nrows
=
ngraph
->
nrows
=
graph
->
nrows
;
ncols
=
ngraph
->
ncols
=
graph
->
ncols
;
rowptr
=
graph
->
rowptr
;
rowind
=
graph
->
rowind
;
rowval
=
graph
->
rowval
;
nrowptr
=
ngraph
->
rowptr
=
gk_zmalloc
(
nrows
+
1
,
"gk_graph_Prune: nrowptr"
);
nrowind
=
ngraph
->
rowind
=
gk_imalloc
(
rowptr
[
nrows
],
"gk_graph_Prune: nrowind"
);
nrowval
=
ngraph
->
rowval
=
gk_fmalloc
(
rowptr
[
nrows
],
"gk_graph_Prune: nrowval"
);
switch
(
what
)
{
case
GK_CSR_COL
:
collen
=
gk_ismalloc
(
ncols
,
0
,
"gk_graph_Prune: collen"
);
for
(
i
=
0
;
i
<
nrows
;
i
++
)
{
for
(
j
=
rowptr
[
i
];
j
<
rowptr
[
i
+
1
];
j
++
)
{
ASSERT
(
rowind
[
j
]
<
ncols
);
collen
[
rowind
[
j
]]
++
;
}
}
for
(
i
=
0
;
i
<
ncols
;
i
++
)
collen
[
i
]
=
(
collen
[
i
]
>=
minf
&&
collen
[
i
]
<=
maxf
?
1
:
0
);
nrowptr
[
0
]
=
0
;
for
(
nnz
=
0
,
i
=
0
;
i
<
nrows
;
i
++
)
{
for
(
j
=
rowptr
[
i
];
j
<
rowptr
[
i
+
1
];
j
++
)
{
if
(
collen
[
rowind
[
j
]])
{
nrowind
[
nnz
]
=
rowind
[
j
];
nrowval
[
nnz
]
=
rowval
[
j
];
nnz
++
;
}
}
nrowptr
[
i
+
1
]
=
nnz
;
}
gk_free
((
void
**
)
&
collen
,
LTERM
);
break
;
case
GK_CSR_ROW
:
nrowptr
[
0
]
=
0
;
for
(
nnz
=
0
,
i
=
0
;
i
<
nrows
;
i
++
)
{
if
(
rowptr
[
i
+
1
]
-
rowptr
[
i
]
>=
minf
&&
rowptr
[
i
+
1
]
-
rowptr
[
i
]
<=
maxf
)
{
for
(
j
=
rowptr
[
i
];
j
<
rowptr
[
i
+
1
];
j
++
,
nnz
++
)
{
nrowind
[
nnz
]
=
rowind
[
j
];
nrowval
[
nnz
]
=
rowval
[
j
];
}
}
nrowptr
[
i
+
1
]
=
nnz
;
}
break
;
default:
gk_graph_Free
(
&
ngraph
);
gk_errexit
(
SIGERR
,
"Unknown prunning type of %d
\n
"
,
what
);
return
NULL
;
}
return
ngraph
;
}
/*************************************************************************/
/*! Normalizes the rows/columns of the graphrix to be unit
length.
\param graph the graphrix itself,
\param what indicates what will be normalized and is obtained by
specifying GK_CSR_ROW, GK_CSR_COL, GK_CSR_ROW|GK_CSR_COL.
\param norm indicates what norm is to normalize to, 1: 1-norm, 2: 2-norm
*/
/**************************************************************************/
void
gk_graph_Normalize
(
gk_graph_t
*
graph
,
int
what
,
int
norm
)
{
ssize_t
i
,
j
;
int
n
;
ssize_t
*
ptr
;
float
*
val
,
sum
;
if
(
what
&
GK_CSR_ROW
&&
graph
->
rowval
)
{
n
=
graph
->
nrows
;
ptr
=
graph
->
rowptr
;
val
=
graph
->
rowval
;
#pragma omp parallel if (ptr[n] > OMPMINOPS)
{
#pragma omp for private(j,sum) schedule(static)
for
(
i
=
0
;
i
<
n
;
i
++
)
{
for
(
sum
=
0
.
0
,
j
=
ptr
[
i
];
j
<
ptr
[
i
+
1
];
j
++
){
if
(
norm
==
2
)
sum
+=
val
[
j
]
*
val
[
j
];
else
if
(
norm
==
1
)
sum
+=
val
[
j
];
/* assume val[j] > 0 */
}
if
(
sum
>
0
)
{
if
(
norm
==
2
)
sum
=
1
.
0
/
sqrt
(
sum
);
else
if
(
norm
==
1
)
sum
=
1
.
0
/
sum
;
for
(
j
=
ptr
[
i
];
j
<
ptr
[
i
+
1
];
j
++
)
val
[
j
]
*=
sum
;
}
}
}
}
if
(
what
&
GK_CSR_COL
&&
graph
->
colval
)
{
n
=
graph
->
ncols
;
ptr
=
graph
->
colptr
;
val
=
graph
->
colval
;
#pragma omp parallel if (ptr[n] > OMPMINOPS)
{
#pragma omp for private(j,sum) schedule(static)
for
(
i
=
0
;
i
<
n
;
i
++
)
{
for
(
sum
=
0
.
0
,
j
=
ptr
[
i
];
j
<
ptr
[
i
+
1
];
j
++
)
if
(
norm
==
2
)
sum
+=
val
[
j
]
*
val
[
j
];
else
if
(
norm
==
1
)
sum
+=
val
[
j
];
if
(
sum
>
0
)
{
if
(
norm
==
2
)
sum
=
1
.
0
/
sqrt
(
sum
);
else
if
(
norm
==
1
)
sum
=
1
.
0
/
sum
;
for
(
j
=
ptr
[
i
];
j
<
ptr
[
i
+
1
];
j
++
)
val
[
j
]
*=
sum
;
}
}
}
}
}
#endif
third_party/METIS/GKlib/htable.c
0 → 100644
View file @
3359c1f1
/*
* Copyright 2004, Regents of the University of Minnesota
*
* This file contains routines for manipulating a direct-access hash table
*
* Started 3/22/04
* George
*
*/
#include <GKlib.h>
/******************************************************************************
* This function creates the hash-table
*******************************************************************************/
gk_HTable_t
*
HTable_Create
(
int
nelements
)
{
gk_HTable_t
*
htable
;
htable
=
gk_malloc
(
sizeof
(
gk_HTable_t
),
"HTable_Create: htable"
);
htable
->
harray
=
gk_ikvmalloc
(
nelements
,
"HTable_Create: harray"
);
htable
->
nelements
=
nelements
;
HTable_Reset
(
htable
);
return
htable
;
}
/******************************************************************************
* This function resets the data-structures associated with the hash-table
*******************************************************************************/
void
HTable_Reset
(
gk_HTable_t
*
htable
)
{
int
i
;
for
(
i
=
0
;
i
<
htable
->
nelements
;
i
++
)
htable
->
harray
[
i
].
key
=
HTABLE_EMPTY
;
htable
->
htsize
=
0
;
}
/******************************************************************************
* This function resizes the hash-table
*******************************************************************************/
void
HTable_Resize
(
gk_HTable_t
*
htable
,
int
nelements
)
{
int
i
,
old_nelements
;
gk_ikv_t
*
old_harray
;
old_nelements
=
htable
->
nelements
;
old_harray
=
htable
->
harray
;
/* prepare larger hash */
htable
->
nelements
=
nelements
;
htable
->
htsize
=
0
;
htable
->
harray
=
gk_ikvmalloc
(
nelements
,
"HTable_Resize: harray"
);
for
(
i
=
0
;
i
<
nelements
;
i
++
)
htable
->
harray
[
i
].
key
=
HTABLE_EMPTY
;
/* reassign the values */
for
(
i
=
0
;
i
<
old_nelements
;
i
++
)
if
(
old_harray
[
i
].
key
!=
HTABLE_EMPTY
)
HTable_Insert
(
htable
,
old_harray
[
i
].
key
,
old_harray
[
i
].
val
);
/* remove old harray */
gk_free
((
void
**
)
&
old_harray
,
LTERM
);
}
/******************************************************************************
* This function inserts a key-value pair in the array
*******************************************************************************/
void
HTable_Insert
(
gk_HTable_t
*
htable
,
int
key
,
int
val
)
{
int
i
,
first
;
if
(
htable
->
htsize
>
htable
->
nelements
/
2
)
HTable_Resize
(
htable
,
2
*
htable
->
nelements
);
first
=
HTable_HFunction
(
htable
->
nelements
,
key
);
for
(
i
=
first
;
i
<
htable
->
nelements
;
i
++
)
{
if
(
htable
->
harray
[
i
].
key
==
HTABLE_EMPTY
||
htable
->
harray
[
i
].
key
==
HTABLE_DELETED
)
{
htable
->
harray
[
i
].
key
=
key
;
htable
->
harray
[
i
].
val
=
val
;
htable
->
htsize
++
;
return
;
}
}
for
(
i
=
0
;
i
<
first
;
i
++
)
{
if
(
htable
->
harray
[
i
].
key
==
HTABLE_EMPTY
||
htable
->
harray
[
i
].
key
==
HTABLE_DELETED
)
{
htable
->
harray
[
i
].
key
=
key
;
htable
->
harray
[
i
].
val
=
val
;
htable
->
htsize
++
;
return
;
}
}
}
/******************************************************************************
* This function deletes key from the htable
*******************************************************************************/
void
HTable_Delete
(
gk_HTable_t
*
htable
,
int
key
)
{
int
i
,
first
;
first
=
HTable_HFunction
(
htable
->
nelements
,
key
);
for
(
i
=
first
;
i
<
htable
->
nelements
;
i
++
)
{
if
(
htable
->
harray
[
i
].
key
==
key
)
{
htable
->
harray
[
i
].
key
=
HTABLE_DELETED
;
htable
->
htsize
--
;
return
;
}
}
for
(
i
=
0
;
i
<
first
;
i
++
)
{
if
(
htable
->
harray
[
i
].
key
==
key
)
{
htable
->
harray
[
i
].
key
=
HTABLE_DELETED
;
htable
->
htsize
--
;
return
;
}
}
}
/******************************************************************************
* This function returns the data associated with the key in the hastable
*******************************************************************************/
int
HTable_Search
(
gk_HTable_t
*
htable
,
int
key
)
{
int
i
,
first
;
first
=
HTable_HFunction
(
htable
->
nelements
,
key
);
for
(
i
=
first
;
i
<
htable
->
nelements
;
i
++
)
{
if
(
htable
->
harray
[
i
].
key
==
key
)
return
htable
->
harray
[
i
].
val
;
else
if
(
htable
->
harray
[
i
].
key
==
HTABLE_EMPTY
)
return
-
1
;
}
for
(
i
=
0
;
i
<
first
;
i
++
)
{
if
(
htable
->
harray
[
i
].
key
==
key
)
return
htable
->
harray
[
i
].
val
;
else
if
(
htable
->
harray
[
i
].
key
==
HTABLE_EMPTY
)
return
-
1
;
}
return
-
1
;
}
/******************************************************************************
* This function returns the next key/val
*******************************************************************************/
int
HTable_GetNext
(
gk_HTable_t
*
htable
,
int
key
,
int
*
r_val
,
int
type
)
{
int
i
;
static
int
first
,
last
;
if
(
type
==
HTABLE_FIRST
)
first
=
last
=
HTable_HFunction
(
htable
->
nelements
,
key
);
if
(
first
>
last
)
{
for
(
i
=
first
;
i
<
htable
->
nelements
;
i
++
)
{
if
(
htable
->
harray
[
i
].
key
==
key
)
{
*
r_val
=
htable
->
harray
[
i
].
val
;
first
=
i
+
1
;
return
1
;
}
else
if
(
htable
->
harray
[
i
].
key
==
HTABLE_EMPTY
)
return
-
1
;
}
first
=
0
;
}
for
(
i
=
first
;
i
<
last
;
i
++
)
{
if
(
htable
->
harray
[
i
].
key
==
key
)
{
*
r_val
=
htable
->
harray
[
i
].
val
;
first
=
i
+
1
;
return
1
;
}
else
if
(
htable
->
harray
[
i
].
key
==
HTABLE_EMPTY
)
return
-
1
;
}
return
-
1
;
}
/******************************************************************************
* This function returns the data associated with the key in the hastable
*******************************************************************************/
int
HTable_SearchAndDelete
(
gk_HTable_t
*
htable
,
int
key
)
{
int
i
,
first
;
first
=
HTable_HFunction
(
htable
->
nelements
,
key
);
for
(
i
=
first
;
i
<
htable
->
nelements
;
i
++
)
{
if
(
htable
->
harray
[
i
].
key
==
key
)
{
htable
->
harray
[
i
].
key
=
HTABLE_DELETED
;
htable
->
htsize
--
;
return
htable
->
harray
[
i
].
val
;
}
else
if
(
htable
->
harray
[
i
].
key
==
HTABLE_EMPTY
)
gk_errexit
(
SIGERR
,
"HTable_SearchAndDelete: Failed to find the key!
\n
"
);
}
for
(
i
=
0
;
i
<
first
;
i
++
)
{
if
(
htable
->
harray
[
i
].
key
==
key
)
{
htable
->
harray
[
i
].
key
=
HTABLE_DELETED
;
htable
->
htsize
--
;
return
htable
->
harray
[
i
].
val
;
}
else
if
(
htable
->
harray
[
i
].
key
==
HTABLE_EMPTY
)
gk_errexit
(
SIGERR
,
"HTable_SearchAndDelete: Failed to find the key!
\n
"
);
}
return
-
1
;
}
/******************************************************************************
* This function destroys the data structures associated with the hash-table
*******************************************************************************/
void
HTable_Destroy
(
gk_HTable_t
*
htable
)
{
gk_free
((
void
**
)
&
htable
->
harray
,
&
htable
,
LTERM
);
}
/******************************************************************************
* This is the hash-function. Based on multiplication
*******************************************************************************/
int
HTable_HFunction
(
int
nelements
,
int
key
)
{
return
(
int
)(
key
%
nelements
);
}
third_party/METIS/GKlib/io.c
0 → 100644
View file @
3359c1f1
/*!
\file io.c
\brief Various file I/O functions.
This file contains various functions that perform I/O.
\date Started 4/10/95
\author George
\version\verbatim $Id: io.c 18951 2015-08-08 20:10:46Z karypis $ \endverbatim
*/
#ifdef HAVE_GETLINE
/* Get getline to be defined. */
#define _GNU_SOURCE
#include <stdio.h>
#undef _GNU_SOURCE
#endif
#include <GKlib.h>
/*************************************************************************
* This function opens a file
**************************************************************************/
FILE
*
gk_fopen
(
char
*
fname
,
char
*
mode
,
const
char
*
msg
)
{
FILE
*
fp
;
char
errmsg
[
8192
];
fp
=
fopen
(
fname
,
mode
);
if
(
fp
!=
NULL
)
return
fp
;
sprintf
(
errmsg
,
"file: %s, mode: %s, [%s]"
,
fname
,
mode
,
msg
);
perror
(
errmsg
);
errexit
(
"Failed on gk_fopen()
\n
"
);
return
NULL
;
}
/*************************************************************************
* This function closes a file
**************************************************************************/
void
gk_fclose
(
FILE
*
fp
)
{
fclose
(
fp
);
}
/*************************************************************************/
/*! This function is a wrapper around the read() function that ensures
that all data is been read, by issuing multiple read requests.
The only time when not 'count' items are read is when the EOF has been
reached.
*/
/*************************************************************************/
ssize_t
gk_read
(
int
fd
,
void
*
vbuf
,
size_t
count
)
{
char
*
buf
=
(
char
*
)
vbuf
;
ssize_t
rsize
,
tsize
=
count
;
do
{
if
((
rsize
=
read
(
fd
,
buf
,
tsize
))
==
-
1
)
return
-
1
;
buf
+=
rsize
;
tsize
-=
rsize
;
}
while
(
tsize
>
0
&&
rsize
>
0
);
return
count
-
tsize
;
}
/*************************************************************************/
/*! This function is a wrapper around the write() function that ensures
that all data is been written, by issueing multiple write requests.
*/
/*************************************************************************/
ssize_t
gk_write
(
int
fd
,
void
*
vbuf
,
size_t
count
)
{
char
*
buf
=
(
char
*
)
vbuf
;
ssize_t
size
,
tsize
=
count
;
do
{
if
((
size
=
write
(
fd
,
buf
,
tsize
))
==
-
1
)
return
-
1
;
buf
+=
size
;
tsize
-=
size
;
}
while
(
tsize
>
0
);
return
count
;
}
/*************************************************************************/
/*! This function is the GKlib implementation of glibc's getline()
function.
\returns -1 if the EOF has been reached, otherwise it returns the
number of bytes read.
*/
/*************************************************************************/
gk_idx_t
gk_getline
(
char
**
lineptr
,
size_t
*
n
,
FILE
*
stream
)
{
#ifdef HAVE_GETLINE
return
getline
(
lineptr
,
n
,
stream
);
#else
size_t
i
;
int
ch
;
if
(
feof
(
stream
))
return
-
1
;
/* Initial memory allocation if *lineptr is NULL */
if
(
*
lineptr
==
NULL
||
*
n
==
0
)
{
*
n
=
1024
;
*
lineptr
=
gk_malloc
((
*
n
)
*
sizeof
(
char
),
"gk_getline: lineptr"
);
}
/* get into the main loop */
i
=
0
;
while
((
ch
=
getc
(
stream
))
!=
EOF
)
{
(
*
lineptr
)[
i
++
]
=
(
char
)
ch
;
/* reallocate memory if reached at the end of the buffer. The +1 is for '\0' */
if
(
i
+
1
==
*
n
)
{
*
n
=
2
*
(
*
n
);
*
lineptr
=
gk_realloc
(
*
lineptr
,
(
*
n
)
*
sizeof
(
char
),
"gk_getline: lineptr"
);
}
if
(
ch
==
'\n'
)
break
;
}
(
*
lineptr
)[
i
]
=
'\0'
;
return
(
i
==
0
?
-
1
:
i
);
#endif
}
/*************************************************************************/
/*! This function reads the contents of a text file and returns it in the
form of an array of strings.
\param fname is the name of the file
\param r_nlines is the number of lines in the file. If it is NULL,
this information is not returned.
*/
/*************************************************************************/
char
**
gk_readfile
(
char
*
fname
,
size_t
*
r_nlines
)
{
size_t
lnlen
,
nlines
=
0
;
char
*
line
=
NULL
,
**
lines
=
NULL
;
FILE
*
fpin
;
gk_getfilestats
(
fname
,
&
nlines
,
NULL
,
NULL
,
NULL
);
if
(
nlines
>
0
)
{
lines
=
(
char
**
)
gk_malloc
(
nlines
*
sizeof
(
char
*
),
"gk_readfile: lines"
);
fpin
=
gk_fopen
(
fname
,
"r"
,
"gk_readfile"
);
nlines
=
0
;
while
(
gk_getline
(
&
line
,
&
lnlen
,
fpin
)
!=
-
1
)
{
gk_strtprune
(
line
,
"
\n\r
"
);
lines
[
nlines
++
]
=
gk_strdup
(
line
);
}
gk_fclose
(
fpin
);
}
gk_free
((
void
**
)
&
line
,
LTERM
);
if
(
r_nlines
!=
NULL
)
*
r_nlines
=
nlines
;
return
lines
;
}
/*************************************************************************/
/*! This function reads the contents of a file and returns it in the
form of an array of int32_t.
\param fname is the name of the file
\param r_nlines is the number of lines in the file. If it is NULL,
this information is not returned.
*/
/*************************************************************************/
int32_t
*
gk_i32readfile
(
char
*
fname
,
size_t
*
r_nlines
)
{
size_t
lnlen
,
nlines
=
0
;
char
*
line
=
NULL
;
int32_t
*
array
=
NULL
;
FILE
*
fpin
;
gk_getfilestats
(
fname
,
&
nlines
,
NULL
,
NULL
,
NULL
);
if
(
nlines
>
0
)
{
array
=
gk_i32malloc
(
nlines
,
"gk_i32readfile: array"
);
fpin
=
gk_fopen
(
fname
,
"r"
,
"gk_readfile"
);
nlines
=
0
;
while
(
gk_getline
(
&
line
,
&
lnlen
,
fpin
)
!=
-
1
)
{
sscanf
(
line
,
"%"
SCNd32
,
&
array
[
nlines
++
]);
}
gk_fclose
(
fpin
);
}
gk_free
((
void
**
)
&
line
,
LTERM
);
if
(
r_nlines
!=
NULL
)
*
r_nlines
=
nlines
;
return
array
;
}
/*************************************************************************/
/*! This function reads the contents of a file and returns it in the
form of an array of int64_t.
\param fname is the name of the file
\param r_nlines is the number of lines in the file. If it is NULL,
this information is not returned.
*/
/*************************************************************************/
int64_t
*
gk_i64readfile
(
char
*
fname
,
size_t
*
r_nlines
)
{
size_t
lnlen
,
nlines
=
0
;
char
*
line
=
NULL
;
int64_t
*
array
=
NULL
;
FILE
*
fpin
;
gk_getfilestats
(
fname
,
&
nlines
,
NULL
,
NULL
,
NULL
);
if
(
nlines
>
0
)
{
array
=
gk_i64malloc
(
nlines
,
"gk_i64readfile: array"
);
fpin
=
gk_fopen
(
fname
,
"r"
,
"gk_readfile"
);
nlines
=
0
;
while
(
gk_getline
(
&
line
,
&
lnlen
,
fpin
)
!=
-
1
)
{
sscanf
(
line
,
"%"
SCNd64
,
&
array
[
nlines
++
]);
}
gk_fclose
(
fpin
);
}
gk_free
((
void
**
)
&
line
,
LTERM
);
if
(
r_nlines
!=
NULL
)
*
r_nlines
=
nlines
;
return
array
;
}
/*************************************************************************/
/*! This function reads the contents of a file and returns it in the
form of an array of ssize_t.
\param fname is the name of the file
\param r_nlines is the number of lines in the file. If it is NULL,
this information is not returned.
*/
/*************************************************************************/
ssize_t
*
gk_zreadfile
(
char
*
fname
,
size_t
*
r_nlines
)
{
size_t
lnlen
,
nlines
=
0
;
char
*
line
=
NULL
;
ssize_t
*
array
=
NULL
;
FILE
*
fpin
;
gk_getfilestats
(
fname
,
&
nlines
,
NULL
,
NULL
,
NULL
);
if
(
nlines
>
0
)
{
array
=
gk_zmalloc
(
nlines
,
"gk_zreadfile: array"
);
fpin
=
gk_fopen
(
fname
,
"r"
,
"gk_readfile"
);
nlines
=
0
;
while
(
gk_getline
(
&
line
,
&
lnlen
,
fpin
)
!=
-
1
)
{
sscanf
(
line
,
"%zd"
,
&
array
[
nlines
++
]);
}
gk_fclose
(
fpin
);
}
gk_free
((
void
**
)
&
line
,
LTERM
);
if
(
r_nlines
!=
NULL
)
*
r_nlines
=
nlines
;
return
array
;
}
/*************************************************************************/
/*! This function reads the contents of a binary file and returns it in the
form of an array of int32_t.
\param fname is the name of the file
\param r_nlines is the number of lines in the file. If it is NULL,
this information is not returned.
*/
/*************************************************************************/
int32_t
*
gk_i32readfilebin
(
char
*
fname
,
size_t
*
r_nelmnts
)
{
size_t
nelmnts
;
ssize_t
fsize
;
int32_t
*
array
=
NULL
;
FILE
*
fpin
;
*
r_nelmnts
=
0
;
fsize
=
gk_getfsize
(
fname
);
if
(
fsize
==
-
1
)
{
gk_errexit
(
SIGERR
,
"Failed to fstat(%s).
\n
"
,
fname
);
return
NULL
;
}
if
(
fsize
%
sizeof
(
int32_t
)
!=
0
)
{
gk_errexit
(
SIGERR
,
"The size [%zd] of the file [%s] is not in multiples of sizeof(int32_t).
\n
"
,
fsize
,
fname
);
return
NULL
;
}
nelmnts
=
fsize
/
sizeof
(
int32_t
);
array
=
gk_i32malloc
(
nelmnts
,
"gk_i32readfilebin: array"
);
fpin
=
gk_fopen
(
fname
,
"rb"
,
"gk_i32readfilebin"
);
if
(
fread
(
array
,
sizeof
(
int32_t
),
nelmnts
,
fpin
)
!=
nelmnts
)
{
gk_errexit
(
SIGERR
,
"Failed to read the number of words requested. %zd
\n
"
,
nelmnts
);
gk_free
((
void
**
)
&
array
,
LTERM
);
return
NULL
;
}
gk_fclose
(
fpin
);
*
r_nelmnts
=
nelmnts
;
return
array
;
}
/*************************************************************************/
/*! This function writes the contents of an array into a binary file.
\param fname is the name of the file
\param n the number of elements in the array.
\param a the array to be written out.
*/
/*************************************************************************/
size_t
gk_i32writefilebin
(
char
*
fname
,
size_t
n
,
int32_t
*
a
)
{
size_t
fsize
;
FILE
*
fp
;
fp
=
gk_fopen
(
fname
,
"wb"
,
"gk_writefilebin"
);
fsize
=
fwrite
(
a
,
sizeof
(
int32_t
),
n
,
fp
);
gk_fclose
(
fp
);
return
fsize
;
}
/*************************************************************************/
/*! This function reads the contents of a binary file and returns it in the
form of an array of int64_t.
\param fname is the name of the file
\param r_nlines is the number of lines in the file. If it is NULL,
this information is not returned.
*/
/*************************************************************************/
int64_t
*
gk_i64readfilebin
(
char
*
fname
,
size_t
*
r_nelmnts
)
{
size_t
nelmnts
;
ssize_t
fsize
;
int64_t
*
array
=
NULL
;
FILE
*
fpin
;
*
r_nelmnts
=
0
;
fsize
=
gk_getfsize
(
fname
);
if
(
fsize
==
-
1
)
{
gk_errexit
(
SIGERR
,
"Failed to fstat(%s).
\n
"
,
fname
);
return
NULL
;
}
if
(
fsize
%
sizeof
(
int64_t
)
!=
0
)
{
gk_errexit
(
SIGERR
,
"The size of the file is not in multiples of sizeof(int64_t).
\n
"
);
return
NULL
;
}
nelmnts
=
fsize
/
sizeof
(
int64_t
);
array
=
gk_i64malloc
(
nelmnts
,
"gk_i64readfilebin: array"
);
fpin
=
gk_fopen
(
fname
,
"rb"
,
"gk_i64readfilebin"
);
if
(
fread
(
array
,
sizeof
(
int64_t
),
nelmnts
,
fpin
)
!=
nelmnts
)
{
gk_errexit
(
SIGERR
,
"Failed to read the number of words requested. %zd
\n
"
,
nelmnts
);
gk_free
((
void
**
)
&
array
,
LTERM
);
return
NULL
;
}
gk_fclose
(
fpin
);
*
r_nelmnts
=
nelmnts
;
return
array
;
}
/*************************************************************************/
/*! This function writes the contents of an array into a binary file.
\param fname is the name of the file
\param n the number of elements in the array.
\param a the array to be written out.
*/
/*************************************************************************/
size_t
gk_i64writefilebin
(
char
*
fname
,
size_t
n
,
int64_t
*
a
)
{
size_t
fsize
;
FILE
*
fp
;
fp
=
gk_fopen
(
fname
,
"wb"
,
"gk_writefilebin"
);
fsize
=
fwrite
(
a
,
sizeof
(
int64_t
),
n
,
fp
);
gk_fclose
(
fp
);
return
fsize
;
}
/*************************************************************************/
/*! This function reads the contents of a binary file and returns it in the
form of an array of ssize_t.
\param fname is the name of the file
\param r_nlines is the number of lines in the file. If it is NULL,
this information is not returned.
*/
/*************************************************************************/
ssize_t
*
gk_zreadfilebin
(
char
*
fname
,
size_t
*
r_nelmnts
)
{
size_t
nelmnts
;
ssize_t
fsize
;
ssize_t
*
array
=
NULL
;
FILE
*
fpin
;
*
r_nelmnts
=
0
;
fsize
=
gk_getfsize
(
fname
);
if
(
fsize
==
-
1
)
{
gk_errexit
(
SIGERR
,
"Failed to fstat(%s).
\n
"
,
fname
);
return
NULL
;
}
if
(
fsize
%
sizeof
(
ssize_t
)
!=
0
)
{
gk_errexit
(
SIGERR
,
"The size of the file is not in multiples of sizeof(ssize_t).
\n
"
);
return
NULL
;
}
nelmnts
=
fsize
/
sizeof
(
ssize_t
);
array
=
gk_zmalloc
(
nelmnts
,
"gk_zreadfilebin: array"
);
fpin
=
gk_fopen
(
fname
,
"rb"
,
"gk_zreadfilebin"
);
if
(
fread
(
array
,
sizeof
(
ssize_t
),
nelmnts
,
fpin
)
!=
nelmnts
)
{
gk_errexit
(
SIGERR
,
"Failed to read the number of words requested. %zd
\n
"
,
nelmnts
);
gk_free
((
void
**
)
&
array
,
LTERM
);
return
NULL
;
}
gk_fclose
(
fpin
);
*
r_nelmnts
=
nelmnts
;
return
array
;
}
/*************************************************************************/
/*! This function writes the contents of an array into a binary file.
\param fname is the name of the file
\param n the number of elements in the array.
\param a the array to be written out.
*/
/*************************************************************************/
size_t
gk_zwritefilebin
(
char
*
fname
,
size_t
n
,
ssize_t
*
a
)
{
size_t
fsize
;
FILE
*
fp
;
fp
=
gk_fopen
(
fname
,
"wb"
,
"gk_writefilebin"
);
fsize
=
fwrite
(
a
,
sizeof
(
ssize_t
),
n
,
fp
);
gk_fclose
(
fp
);
return
fsize
;
}
/*************************************************************************/
/*! This function reads the contents of a binary file and returns it in the
form of an array of float.
\param fname is the name of the file
\param r_nlines is the number of lines in the file. If it is NULL,
this information is not returned.
*/
/*************************************************************************/
float
*
gk_freadfilebin
(
char
*
fname
,
size_t
*
r_nelmnts
)
{
size_t
nelmnts
;
ssize_t
fsize
;
float
*
array
=
NULL
;
FILE
*
fpin
;
*
r_nelmnts
=
0
;
fsize
=
gk_getfsize
(
fname
);
if
(
fsize
==
-
1
)
{
gk_errexit
(
SIGERR
,
"Failed to fstat(%s).
\n
"
,
fname
);
return
NULL
;
}
if
(
fsize
%
sizeof
(
float
)
!=
0
)
{
gk_errexit
(
SIGERR
,
"The size of the file is not in multiples of sizeof(float).
\n
"
);
return
NULL
;
}
nelmnts
=
fsize
/
sizeof
(
float
);
array
=
gk_fmalloc
(
nelmnts
,
"gk_freadfilebin: array"
);
fpin
=
gk_fopen
(
fname
,
"rb"
,
"gk_freadfilebin"
);
if
(
fread
(
array
,
sizeof
(
float
),
nelmnts
,
fpin
)
!=
nelmnts
)
{
gk_errexit
(
SIGERR
,
"Failed to read the number of words requested. %zd
\n
"
,
nelmnts
);
gk_free
((
void
**
)
&
array
,
LTERM
);
return
NULL
;
}
gk_fclose
(
fpin
);
*
r_nelmnts
=
nelmnts
;
return
array
;
}
/*************************************************************************/
/*! This function writes the contents of an array into a binary file.
\param fname is the name of the file
\param n the number of elements in the array.
\param a the array to be written out.
*/
/*************************************************************************/
size_t
gk_fwritefilebin
(
char
*
fname
,
size_t
n
,
float
*
a
)
{
size_t
fsize
;
FILE
*
fp
;
fp
=
gk_fopen
(
fname
,
"wb"
,
"gk_fwritefilebin"
);
fsize
=
fwrite
(
a
,
sizeof
(
float
),
n
,
fp
);
gk_fclose
(
fp
);
return
fsize
;
}
/*************************************************************************/
/*! This function reads the contents of a binary file and returns it in the
form of an array of double.
\param fname is the name of the file
\param r_nlines is the number of lines in the file. If it is NULL,
this information is not returned.
*/
/*************************************************************************/
double
*
gk_dreadfilebin
(
char
*
fname
,
size_t
*
r_nelmnts
)
{
size_t
nelmnts
;
ssize_t
fsize
;
double
*
array
=
NULL
;
FILE
*
fpin
;
*
r_nelmnts
=
0
;
fsize
=
gk_getfsize
(
fname
);
if
(
fsize
==
-
1
)
{
gk_errexit
(
SIGERR
,
"Failed to fstat(%s).
\n
"
,
fname
);
return
NULL
;
}
if
(
fsize
%
sizeof
(
double
)
!=
0
)
{
gk_errexit
(
SIGERR
,
"The size of the file is not in multiples of sizeof(double).
\n
"
);
return
NULL
;
}
nelmnts
=
fsize
/
sizeof
(
double
);
array
=
gk_dmalloc
(
nelmnts
,
"gk_dreadfilebin: array"
);
fpin
=
gk_fopen
(
fname
,
"rb"
,
"gk_dreadfilebin"
);
if
(
fread
(
array
,
sizeof
(
double
),
nelmnts
,
fpin
)
!=
nelmnts
)
{
gk_errexit
(
SIGERR
,
"Failed to read the number of words requested. %zd
\n
"
,
nelmnts
);
gk_free
((
void
**
)
&
array
,
LTERM
);
return
NULL
;
}
gk_fclose
(
fpin
);
*
r_nelmnts
=
nelmnts
;
return
array
;
}
/*************************************************************************/
/*! This function writes the contents of an array into a binary file.
\param fname is the name of the file
\param n the number of elements in the array.
\param a the array to be written out.
*/
/*************************************************************************/
size_t
gk_dwritefilebin
(
char
*
fname
,
size_t
n
,
double
*
a
)
{
size_t
fsize
;
FILE
*
fp
;
fp
=
gk_fopen
(
fname
,
"wb"
,
"gk_writefilebin"
);
fsize
=
fwrite
(
a
,
sizeof
(
double
),
n
,
fp
);
gk_fclose
(
fp
);
return
fsize
;
}
third_party/METIS/GKlib/itemsets.c
0 → 100644
View file @
3359c1f1
/*!
* \file
* \brief Frequent/Closed itemset discovery routines
*
* This file contains the code for finding frequent/closed itemests. These routines
* are implemented using a call-back mechanism to deal with the discovered itemsets.
*
* \date 6/13/2008
* \author George Karypis
* \version\verbatim $Id: itemsets.c 19240 2015-10-22 12:41:19Z karypis $ \endverbatim
*/
#include <GKlib.h>
/*-------------------------------------------------------------*/
/*! Data structures for use within this module */
/*-------------------------------------------------------------*/
typedef
struct
{
int
minfreq
;
/* the minimum frequency of a pattern */
int
maxfreq
;
/* the maximum frequency of a pattern */
int
minlen
;
/* the minimum length of the requested pattern */
int
maxlen
;
/* the maximum length of the requested pattern */
int
tnitems
;
/* the initial range of the item space */
/* the call-back function */
void
(
*
callback
)(
void
*
stateptr
,
int
nitems
,
int
*
itemids
,
int
ntrans
,
int
*
transids
);
void
*
stateptr
;
/* the user-supplied pointer to pass to the callback */
/* workspace variables */
int
*
rmarker
;
gk_ikv_t
*
cand
;
}
isparams_t
;
/*-------------------------------------------------------------*/
/*! Prototypes for this module */
/*-------------------------------------------------------------*/
void
itemsets_find_frequent_itemsets
(
isparams_t
*
params
,
gk_csr_t
*
mat
,
int
preflen
,
int
*
prefix
);
gk_csr_t
*
itemsets_project_matrix
(
isparams_t
*
param
,
gk_csr_t
*
mat
,
int
cid
);
/*************************************************************************/
/*! The entry point of the frequent itemset discovery code */
/*************************************************************************/
void
gk_find_frequent_itemsets
(
int
ntrans
,
ssize_t
*
tranptr
,
int
*
tranind
,
int
minfreq
,
int
maxfreq
,
int
minlen
,
int
maxlen
,
void
(
*
process_itemset
)(
void
*
stateptr
,
int
nitems
,
int
*
itemids
,
int
ntrans
,
int
*
transids
),
void
*
stateptr
)
{
ssize_t
i
;
gk_csr_t
*
mat
,
*
pmat
;
isparams_t
params
;
int
*
pattern
;
/* Create the matrix */
mat
=
gk_csr_Create
();
mat
->
nrows
=
ntrans
;
mat
->
ncols
=
tranind
[
gk_iargmax
(
tranptr
[
ntrans
],
tranind
,
1
)]
+
1
;
mat
->
rowptr
=
gk_zcopy
(
ntrans
+
1
,
tranptr
,
gk_zmalloc
(
ntrans
+
1
,
"gk_find_frequent_itemsets: mat.rowptr"
));
mat
->
rowind
=
gk_icopy
(
tranptr
[
ntrans
],
tranind
,
gk_imalloc
(
tranptr
[
ntrans
],
"gk_find_frequent_itemsets: mat.rowind"
));
mat
->
colids
=
gk_iincset
(
mat
->
ncols
,
0
,
gk_imalloc
(
mat
->
ncols
,
"gk_find_frequent_itemsets: mat.colids"
));
/* Setup the parameters */
params
.
minfreq
=
minfreq
;
params
.
maxfreq
=
(
maxfreq
==
-
1
?
mat
->
nrows
:
maxfreq
);
params
.
minlen
=
minlen
;
params
.
maxlen
=
(
maxlen
==
-
1
?
mat
->
ncols
:
maxlen
);
params
.
tnitems
=
mat
->
ncols
;
params
.
callback
=
process_itemset
;
params
.
stateptr
=
stateptr
;
params
.
rmarker
=
gk_ismalloc
(
mat
->
nrows
,
0
,
"gk_find_frequent_itemsets: rmarker"
);
params
.
cand
=
gk_ikvmalloc
(
mat
->
ncols
,
"gk_find_frequent_itemsets: cand"
);
/* Perform the initial projection */
gk_csr_CreateIndex
(
mat
,
GK_CSR_COL
);
pmat
=
itemsets_project_matrix
(
&
params
,
mat
,
-
1
);
gk_csr_Free
(
&
mat
);
pattern
=
gk_imalloc
(
pmat
->
ncols
,
"gk_find_frequent_itemsets: pattern"
);
itemsets_find_frequent_itemsets
(
&
params
,
pmat
,
0
,
pattern
);
gk_csr_Free
(
&
pmat
);
gk_free
((
void
**
)
&
pattern
,
&
params
.
rmarker
,
&
params
.
cand
,
LTERM
);
}
/*************************************************************************/
/*! The recursive routine for DFS-based frequent pattern discovery */
/*************************************************************************/
void
itemsets_find_frequent_itemsets
(
isparams_t
*
params
,
gk_csr_t
*
mat
,
int
preflen
,
int
*
prefix
)
{
ssize_t
i
;
gk_csr_t
*
cmat
;
/* Project each frequent column */
for
(
i
=
0
;
i
<
mat
->
ncols
;
i
++
)
{
prefix
[
preflen
]
=
mat
->
colids
[
i
];
if
(
preflen
+
1
>=
params
->
minlen
)
(
*
params
->
callback
)(
params
->
stateptr
,
preflen
+
1
,
prefix
,
mat
->
colptr
[
i
+
1
]
-
mat
->
colptr
[
i
],
mat
->
colind
+
mat
->
colptr
[
i
]);
if
(
preflen
+
1
<
params
->
maxlen
)
{
cmat
=
itemsets_project_matrix
(
params
,
mat
,
i
);
itemsets_find_frequent_itemsets
(
params
,
cmat
,
preflen
+
1
,
prefix
);
gk_csr_Free
(
&
cmat
);
}
}
}
/******************************************************************************/
/*! This function projects a matrix w.r.t. to a particular column.
It performs the following steps:
- Determines the length of each column that is remaining.
- Sorts the columns in increasing length.
- Creates a column-based version of the matrix with the proper
column ordering.
*/
/*******************************************************************************/
gk_csr_t
*
itemsets_project_matrix
(
isparams_t
*
params
,
gk_csr_t
*
mat
,
int
cid
)
{
ssize_t
i
,
j
,
k
,
ii
,
pnnz
;
int
nrows
,
ncols
,
pnrows
,
pncols
;
ssize_t
*
colptr
,
*
pcolptr
;
int
*
colind
,
*
colids
,
*
pcolind
,
*
pcolids
,
*
rmarker
;
gk_csr_t
*
pmat
;
gk_ikv_t
*
cand
;
nrows
=
mat
->
nrows
;
ncols
=
mat
->
ncols
;
colptr
=
mat
->
colptr
;
colind
=
mat
->
colind
;
colids
=
mat
->
colids
;
rmarker
=
params
->
rmarker
;
cand
=
params
->
cand
;
/* Allocate space for the projected matrix based on what you know thus far */
pmat
=
gk_csr_Create
();
pmat
->
nrows
=
pnrows
=
(
cid
==
-
1
?
nrows
:
colptr
[
cid
+
1
]
-
colptr
[
cid
]);
/* Mark the rows that will be kept and determine the prowids */
if
(
cid
==
-
1
)
{
/* Initial projection */
gk_iset
(
nrows
,
1
,
rmarker
);
}
else
{
/* The other projections */
for
(
i
=
colptr
[
cid
];
i
<
colptr
[
cid
+
1
];
i
++
)
rmarker
[
colind
[
i
]]
=
1
;
}
/* Determine the length of each column that will be left in the projected matrix */
for
(
pncols
=
0
,
pnnz
=
0
,
i
=
cid
+
1
;
i
<
ncols
;
i
++
)
{
for
(
k
=
0
,
j
=
colptr
[
i
];
j
<
colptr
[
i
+
1
];
j
++
)
{
k
+=
rmarker
[
colind
[
j
]];
}
if
(
k
>=
params
->
minfreq
&&
k
<=
params
->
maxfreq
)
{
cand
[
pncols
].
val
=
i
;
cand
[
pncols
++
].
key
=
k
;
pnnz
+=
k
;
}
}
/* Sort the columns in increasing order */
gk_ikvsorti
(
pncols
,
cand
);
/* Allocate space for the remaining fields of the projected matrix */
pmat
->
ncols
=
pncols
;
pmat
->
colids
=
pcolids
=
gk_imalloc
(
pncols
,
"itemsets_project_matrix: pcolids"
);
pmat
->
colptr
=
pcolptr
=
gk_zmalloc
(
pncols
+
1
,
"itemsets_project_matrix: pcolptr"
);
pmat
->
colind
=
pcolind
=
gk_imalloc
(
pnnz
,
"itemsets_project_matrix: pcolind"
);
/* Populate the projected matrix */
pcolptr
[
0
]
=
0
;
for
(
pnnz
=
0
,
ii
=
0
;
ii
<
pncols
;
ii
++
)
{
i
=
cand
[
ii
].
val
;
for
(
j
=
colptr
[
i
];
j
<
colptr
[
i
+
1
];
j
++
)
{
if
(
rmarker
[
colind
[
j
]])
pcolind
[
pnnz
++
]
=
colind
[
j
];
}
pcolids
[
ii
]
=
colids
[
i
];
pcolptr
[
ii
+
1
]
=
pnnz
;
}
/* Reset the rmarker array */
if
(
cid
==
-
1
)
{
/* Initial projection */
gk_iset
(
nrows
,
0
,
rmarker
);
}
else
{
/* The other projections */
for
(
i
=
colptr
[
cid
];
i
<
colptr
[
cid
+
1
];
i
++
)
rmarker
[
colind
[
i
]]
=
0
;
}
return
pmat
;
}
third_party/METIS/GKlib/mcore.c
0 → 100644
View file @
3359c1f1
/*!
\file
\brief Functions dealing with creating and allocating mcores
\date Started 5/30/11
\author George
\author Copyright 1997-2011, Regents of the University of Minnesota
\version $Id: mcore.c 13953 2013-03-30 16:20:07Z karypis $
*/
#include <GKlib.h>
/*************************************************************************/
/*! This function creates an mcore
*/
/*************************************************************************/
gk_mcore_t
*
gk_mcoreCreate
(
size_t
coresize
)
{
gk_mcore_t
*
mcore
;
mcore
=
(
gk_mcore_t
*
)
gk_malloc
(
sizeof
(
gk_mcore_t
),
"gk_mcoreCreate: mcore"
);
memset
(
mcore
,
0
,
sizeof
(
gk_mcore_t
));
mcore
->
coresize
=
coresize
;
mcore
->
corecpos
=
0
;
mcore
->
core
=
(
coresize
==
0
?
NULL
:
gk_malloc
(
mcore
->
coresize
,
"gk_mcoreCreate: core"
));
/* allocate the memory for keeping track of malloc ops */
mcore
->
nmops
=
2048
;
mcore
->
cmop
=
0
;
mcore
->
mops
=
(
gk_mop_t
*
)
gk_malloc
(
mcore
->
nmops
*
sizeof
(
gk_mop_t
),
"gk_mcoreCreate: mcore->mops"
);
return
mcore
;
}
/*************************************************************************/
/*! This function creates an mcore. This version is used for gkmcore.
*/
/*************************************************************************/
gk_mcore_t
*
gk_gkmcoreCreate
()
{
gk_mcore_t
*
mcore
;
if
((
mcore
=
(
gk_mcore_t
*
)
malloc
(
sizeof
(
gk_mcore_t
)))
==
NULL
)
return
NULL
;
memset
(
mcore
,
0
,
sizeof
(
gk_mcore_t
));
/* allocate the memory for keeping track of malloc ops */
mcore
->
nmops
=
2048
;
mcore
->
cmop
=
0
;
if
((
mcore
->
mops
=
(
gk_mop_t
*
)
malloc
(
mcore
->
nmops
*
sizeof
(
gk_mop_t
)))
==
NULL
)
{
free
(
mcore
);
return
NULL
;
}
return
mcore
;
}
/*************************************************************************/
/*! This function destroys an mcore.
*/
/*************************************************************************/
void
gk_mcoreDestroy
(
gk_mcore_t
**
r_mcore
,
int
showstats
)
{
gk_mcore_t
*
mcore
=
*
r_mcore
;
if
(
mcore
==
NULL
)
return
;
if
(
showstats
)
printf
(
"
\n
gk_mcore statistics
\n
"
" coresize: %12zu nmops: %12zu cmop: %6zu
\n
"
" num_callocs: %12zu num_hallocs: %12zu
\n
"
" size_callocs: %12zu size_hallocs: %12zu
\n
"
" cur_callocs: %12zu cur_hallocs: %12zu
\n
"
" max_callocs: %12zu max_hallocs: %12zu
\n
"
,
mcore
->
coresize
,
mcore
->
nmops
,
mcore
->
cmop
,
mcore
->
num_callocs
,
mcore
->
num_hallocs
,
mcore
->
size_callocs
,
mcore
->
size_hallocs
,
mcore
->
cur_callocs
,
mcore
->
cur_hallocs
,
mcore
->
max_callocs
,
mcore
->
max_hallocs
);
if
(
mcore
->
cur_callocs
!=
0
||
mcore
->
cur_hallocs
!=
0
||
mcore
->
cmop
!=
0
)
{
printf
(
"***Warning: mcore memory was not fully freed when destroyed.
\n
"
" cur_callocs: %6zu cur_hallocs: %6zu cmop: %6zu
\n
"
,
mcore
->
cur_callocs
,
mcore
->
cur_hallocs
,
mcore
->
cmop
);
}
gk_free
((
void
**
)
&
mcore
->
core
,
&
mcore
->
mops
,
&
mcore
,
LTERM
);
*
r_mcore
=
NULL
;
}
/*************************************************************************/
/*! This function destroys an mcore. This version is for gkmcore.
*/
/*************************************************************************/
void
gk_gkmcoreDestroy
(
gk_mcore_t
**
r_mcore
,
int
showstats
)
{
gk_mcore_t
*
mcore
=
*
r_mcore
;
if
(
mcore
==
NULL
)
return
;
if
(
showstats
)
printf
(
"
\n
gk_mcore statistics
\n
"
" nmops: %12zu cmop: %6zu
\n
"
" num_hallocs: %12zu
\n
"
" size_hallocs: %12zu
\n
"
" cur_hallocs: %12zu
\n
"
" max_hallocs: %12zu
\n
"
,
mcore
->
nmops
,
mcore
->
cmop
,
mcore
->
num_hallocs
,
mcore
->
size_hallocs
,
mcore
->
cur_hallocs
,
mcore
->
max_hallocs
);
if
(
mcore
->
cur_hallocs
!=
0
||
mcore
->
cmop
!=
0
)
{
printf
(
"***Warning: mcore memory was not fully freed when destroyed.
\n
"
" cur_hallocs: %6zu cmop: %6zu
\n
"
,
mcore
->
cur_hallocs
,
mcore
->
cmop
);
}
free
(
mcore
->
mops
);
free
(
mcore
);
*
r_mcore
=
NULL
;
}
/*************************************************************************/
/*! This function allocate space from the core/heap
*/
/*************************************************************************/
void
*
gk_mcoreMalloc
(
gk_mcore_t
*
mcore
,
size_t
nbytes
)
{
void
*
ptr
;
/* pad to make pointers 8-byte aligned */
nbytes
+=
(
nbytes
%
8
==
0
?
0
:
8
-
nbytes
%
8
);
if
(
mcore
->
corecpos
+
nbytes
<
mcore
->
coresize
)
{
/* service this request from the core */
ptr
=
((
char
*
)
mcore
->
core
)
+
mcore
->
corecpos
;
mcore
->
corecpos
+=
nbytes
;
gk_mcoreAdd
(
mcore
,
GK_MOPT_CORE
,
nbytes
,
ptr
);
}
else
{
/* service this request from the heap */
ptr
=
gk_malloc
(
nbytes
,
"gk_mcoremalloc: ptr"
);
gk_mcoreAdd
(
mcore
,
GK_MOPT_HEAP
,
nbytes
,
ptr
);
}
/*
printf("MCMALLOC: %zu %d %8zu\n", mcore->cmop-1,
mcore->mops[mcore->cmop-1].type, mcore->mops[mcore->cmop-1].nbytes);
*/
return
ptr
;
}
/*************************************************************************/
/*! This function sets a marker in the stack of malloc ops to be used
subsequently for freeing purposes
*/
/*************************************************************************/
void
gk_mcorePush
(
gk_mcore_t
*
mcore
)
{
gk_mcoreAdd
(
mcore
,
GK_MOPT_MARK
,
0
,
NULL
);
/* printf("MCPPUSH: %zu\n", mcore->cmop-1); */
}
/*************************************************************************/
/*! This function sets a marker in the stack of malloc ops to be used
subsequently for freeing purposes. This is the gkmcore version.
*/
/*************************************************************************/
void
gk_gkmcorePush
(
gk_mcore_t
*
mcore
)
{
gk_gkmcoreAdd
(
mcore
,
GK_MOPT_MARK
,
0
,
NULL
);
/* printf("MCPPUSH: %zu\n", mcore->cmop-1); */
}
/*************************************************************************/
/*! This function frees all mops since the last push
*/
/*************************************************************************/
void
gk_mcorePop
(
gk_mcore_t
*
mcore
)
{
while
(
mcore
->
cmop
>
0
)
{
mcore
->
cmop
--
;
switch
(
mcore
->
mops
[
mcore
->
cmop
].
type
)
{
case
GK_MOPT_MARK
:
/* push marker */
goto
DONE
;
break
;
case
GK_MOPT_CORE
:
/* core free */
if
(
mcore
->
corecpos
<
mcore
->
mops
[
mcore
->
cmop
].
nbytes
)
errexit
(
"Internal Error: wspace's core is about to be over-freed [%zu, %zu, %zd]
\n
"
,
mcore
->
coresize
,
mcore
->
corecpos
,
mcore
->
mops
[
mcore
->
cmop
].
nbytes
);
mcore
->
corecpos
-=
mcore
->
mops
[
mcore
->
cmop
].
nbytes
;
mcore
->
cur_callocs
-=
mcore
->
mops
[
mcore
->
cmop
].
nbytes
;
break
;
case
GK_MOPT_HEAP
:
/* heap free */
gk_free
((
void
**
)
&
mcore
->
mops
[
mcore
->
cmop
].
ptr
,
LTERM
);
mcore
->
cur_hallocs
-=
mcore
->
mops
[
mcore
->
cmop
].
nbytes
;
break
;
default:
gk_errexit
(
SIGMEM
,
"Unknown mop type of %d
\n
"
,
mcore
->
mops
[
mcore
->
cmop
].
type
);
}
}
DONE:
;
/*printf("MCPPOP: %zu\n", mcore->cmop); */
}
/*************************************************************************/
/*! This function frees all mops since the last push. This version is
for poping the gkmcore and it uses free instead of gk_free.
*/
/*************************************************************************/
void
gk_gkmcorePop
(
gk_mcore_t
*
mcore
)
{
while
(
mcore
->
cmop
>
0
)
{
mcore
->
cmop
--
;
switch
(
mcore
->
mops
[
mcore
->
cmop
].
type
)
{
case
GK_MOPT_MARK
:
/* push marker */
goto
DONE
;
break
;
case
GK_MOPT_HEAP
:
/* heap free */
free
(
mcore
->
mops
[
mcore
->
cmop
].
ptr
);
mcore
->
cur_hallocs
-=
mcore
->
mops
[
mcore
->
cmop
].
nbytes
;
break
;
default:
gk_errexit
(
SIGMEM
,
"Unknown mop type of %d
\n
"
,
mcore
->
mops
[
mcore
->
cmop
].
type
);
}
}
DONE:
;
}
/*************************************************************************/
/*! Adds a memory allocation at the end of the list.
*/
/*************************************************************************/
void
gk_mcoreAdd
(
gk_mcore_t
*
mcore
,
int
type
,
size_t
nbytes
,
void
*
ptr
)
{
if
(
mcore
->
cmop
==
mcore
->
nmops
)
{
mcore
->
nmops
*=
2
;
mcore
->
mops
=
realloc
(
mcore
->
mops
,
mcore
->
nmops
*
sizeof
(
gk_mop_t
));
if
(
mcore
->
mops
==
NULL
)
gk_errexit
(
SIGMEM
,
"***Memory allocation for gkmcore failed.
\n
"
);
}
mcore
->
mops
[
mcore
->
cmop
].
type
=
type
;
mcore
->
mops
[
mcore
->
cmop
].
nbytes
=
nbytes
;
mcore
->
mops
[
mcore
->
cmop
].
ptr
=
ptr
;
mcore
->
cmop
++
;
switch
(
type
)
{
case
GK_MOPT_MARK
:
break
;
case
GK_MOPT_CORE
:
mcore
->
num_callocs
++
;
mcore
->
size_callocs
+=
nbytes
;
mcore
->
cur_callocs
+=
nbytes
;
if
(
mcore
->
max_callocs
<
mcore
->
cur_callocs
)
mcore
->
max_callocs
=
mcore
->
cur_callocs
;
break
;
case
GK_MOPT_HEAP
:
mcore
->
num_hallocs
++
;
mcore
->
size_hallocs
+=
nbytes
;
mcore
->
cur_hallocs
+=
nbytes
;
if
(
mcore
->
max_hallocs
<
mcore
->
cur_hallocs
)
mcore
->
max_hallocs
=
mcore
->
cur_hallocs
;
break
;
default:
gk_errexit
(
SIGMEM
,
"Incorrect mcore type operation.
\n
"
);
}
}
/*************************************************************************/
/*! Adds a memory allocation at the end of the list. This is the gkmcore
version.
*/
/*************************************************************************/
void
gk_gkmcoreAdd
(
gk_mcore_t
*
mcore
,
int
type
,
size_t
nbytes
,
void
*
ptr
)
{
if
(
mcore
->
cmop
==
mcore
->
nmops
)
{
mcore
->
nmops
*=
2
;
mcore
->
mops
=
realloc
(
mcore
->
mops
,
mcore
->
nmops
*
sizeof
(
gk_mop_t
));
if
(
mcore
->
mops
==
NULL
)
gk_errexit
(
SIGMEM
,
"***Memory allocation for gkmcore failed.
\n
"
);
}
mcore
->
mops
[
mcore
->
cmop
].
type
=
type
;
mcore
->
mops
[
mcore
->
cmop
].
nbytes
=
nbytes
;
mcore
->
mops
[
mcore
->
cmop
].
ptr
=
ptr
;
mcore
->
cmop
++
;
switch
(
type
)
{
case
GK_MOPT_MARK
:
break
;
case
GK_MOPT_HEAP
:
mcore
->
num_hallocs
++
;
mcore
->
size_hallocs
+=
nbytes
;
mcore
->
cur_hallocs
+=
nbytes
;
if
(
mcore
->
max_hallocs
<
mcore
->
cur_hallocs
)
mcore
->
max_hallocs
=
mcore
->
cur_hallocs
;
break
;
default:
gk_errexit
(
SIGMEM
,
"Incorrect mcore type operation.
\n
"
);
}
}
/*************************************************************************/
/*! This function deletes the mop associated with the supplied pointer.
The mop has to be a heap allocation, otherwise it fails violently.
*/
/*************************************************************************/
void
gk_mcoreDel
(
gk_mcore_t
*
mcore
,
void
*
ptr
)
{
int
i
;
for
(
i
=
mcore
->
cmop
-
1
;
i
>=
0
;
i
--
)
{
if
(
mcore
->
mops
[
i
].
type
==
GK_MOPT_MARK
)
gk_errexit
(
SIGMEM
,
"Could not find pointer %p in mcore
\n
"
,
ptr
);
if
(
mcore
->
mops
[
i
].
ptr
==
ptr
)
{
if
(
mcore
->
mops
[
i
].
type
!=
GK_MOPT_HEAP
)
gk_errexit
(
SIGMEM
,
"Trying to delete a non-HEAP mop.
\n
"
);
mcore
->
cur_hallocs
-=
mcore
->
mops
[
i
].
nbytes
;
mcore
->
mops
[
i
]
=
mcore
->
mops
[
--
mcore
->
cmop
];
return
;
}
}
gk_errexit
(
SIGMEM
,
"mcoreDel should never have been here!
\n
"
);
}
/*************************************************************************/
/*! This function deletes the mop associated with the supplied pointer.
The mop has to be a heap allocation, otherwise it fails violently.
This is the gkmcore version.
*/
/*************************************************************************/
void
gk_gkmcoreDel
(
gk_mcore_t
*
mcore
,
void
*
ptr
)
{
int
i
;
for
(
i
=
mcore
->
cmop
-
1
;
i
>=
0
;
i
--
)
{
if
(
mcore
->
mops
[
i
].
type
==
GK_MOPT_MARK
)
gk_errexit
(
SIGMEM
,
"Could not find pointer %p in mcore
\n
"
,
ptr
);
if
(
mcore
->
mops
[
i
].
ptr
==
ptr
)
{
if
(
mcore
->
mops
[
i
].
type
!=
GK_MOPT_HEAP
)
gk_errexit
(
SIGMEM
,
"Trying to delete a non-HEAP mop.
\n
"
);
mcore
->
cur_hallocs
-=
mcore
->
mops
[
i
].
nbytes
;
mcore
->
mops
[
i
]
=
mcore
->
mops
[
--
mcore
->
cmop
];
return
;
}
}
gk_errexit
(
SIGMEM
,
"gkmcoreDel should never have been here!
\n
"
);
}
third_party/METIS/GKlib/memory.c
0 → 100644
View file @
3359c1f1
/*!
\file memory.c
\brief This file contains various allocation routines
The allocation routines included are for 1D and 2D arrays of the
most datatypes that GKlib support. Many of these routines are
defined with the help of the macros in gk_memory.h. These macros
can be used to define other memory allocation routines.
\date Started 4/3/2007
\author George
\version\verbatim $Id: memory.c 21050 2017-05-25 03:53:58Z karypis $ \endverbatim
*/
#include <GKlib.h>
/* This is for the global mcore that tracks all heap allocations */
static
__thread
gk_mcore_t
*
gkmcore
=
NULL
;
/*************************************************************************/
/*! Define the set of memory allocation routines for each data type */
/**************************************************************************/
GK_MKALLOC
(
gk_c
,
char
)
GK_MKALLOC
(
gk_i
,
int
)
GK_MKALLOC
(
gk_i8
,
int8_t
)
GK_MKALLOC
(
gk_i16
,
int16_t
)
GK_MKALLOC
(
gk_i32
,
int32_t
)
GK_MKALLOC
(
gk_i64
,
int64_t
)
GK_MKALLOC
(
gk_ui8
,
uint8_t
)
GK_MKALLOC
(
gk_ui16
,
uint16_t
)
GK_MKALLOC
(
gk_ui32
,
uint32_t
)
GK_MKALLOC
(
gk_ui64
,
uint64_t
)
GK_MKALLOC
(
gk_z
,
ssize_t
)
GK_MKALLOC
(
gk_zu
,
size_t
)
GK_MKALLOC
(
gk_f
,
float
)
GK_MKALLOC
(
gk_d
,
double
)
GK_MKALLOC
(
gk_idx
,
gk_idx_t
)
GK_MKALLOC
(
gk_ckv
,
gk_ckv_t
)
GK_MKALLOC
(
gk_ikv
,
gk_ikv_t
)
GK_MKALLOC
(
gk_i8kv
,
gk_i8kv_t
)
GK_MKALLOC
(
gk_i16kv
,
gk_i16kv_t
)
GK_MKALLOC
(
gk_i32kv
,
gk_i32kv_t
)
GK_MKALLOC
(
gk_i64kv
,
gk_i64kv_t
)
GK_MKALLOC
(
gk_zkv
,
gk_zkv_t
)
GK_MKALLOC
(
gk_zukv
,
gk_zukv_t
)
GK_MKALLOC
(
gk_fkv
,
gk_fkv_t
)
GK_MKALLOC
(
gk_dkv
,
gk_dkv_t
)
GK_MKALLOC
(
gk_skv
,
gk_skv_t
)
GK_MKALLOC
(
gk_idxkv
,
gk_idxkv_t
)
/*************************************************************************/
/*! This function allocates a two-dimensional matrix.
*/
/*************************************************************************/
void
gk_AllocMatrix
(
void
***
r_matrix
,
size_t
elmlen
,
size_t
ndim1
,
size_t
ndim2
)
{
size_t
i
,
j
;
void
**
matrix
;
*
r_matrix
=
NULL
;
if
((
matrix
=
(
void
**
)
gk_malloc
(
ndim1
*
sizeof
(
void
*
),
"gk_AllocMatrix: matrix"
))
==
NULL
)
return
;
for
(
i
=
0
;
i
<
ndim1
;
i
++
)
{
if
((
matrix
[
i
]
=
(
void
*
)
gk_malloc
(
ndim2
*
elmlen
,
"gk_AllocMatrix: matrix[i]"
))
==
NULL
)
{
for
(
j
=
0
;
j
<
i
;
j
++
)
gk_free
((
void
**
)
&
matrix
[
j
],
LTERM
);
return
;
}
}
*
r_matrix
=
matrix
;
}
/*************************************************************************/
/*! This function frees a two-dimensional matrix.
*/
/*************************************************************************/
void
gk_FreeMatrix
(
void
***
r_matrix
,
size_t
ndim1
,
size_t
ndim2
)
{
size_t
i
;
void
**
matrix
;
if
((
matrix
=
*
r_matrix
)
==
NULL
)
return
;
for
(
i
=
0
;
i
<
ndim1
;
i
++
)
gk_free
((
void
**
)
&
matrix
[
i
],
LTERM
);
gk_free
((
void
**
)
r_matrix
,
LTERM
);
}
/*************************************************************************/
/*! This function initializes tracking of heap allocations.
*/
/*************************************************************************/
int
gk_malloc_init
()
{
if
(
gkmcore
==
NULL
)
gkmcore
=
gk_gkmcoreCreate
();
if
(
gkmcore
==
NULL
)
return
0
;
gk_gkmcorePush
(
gkmcore
);
return
1
;
}
/*************************************************************************/
/*! This function frees the memory that has been allocated since the
last call to gk_malloc_init().
*/
/*************************************************************************/
void
gk_malloc_cleanup
(
int
showstats
)
{
if
(
gkmcore
!=
NULL
)
{
gk_gkmcorePop
(
gkmcore
);
if
(
gkmcore
->
cmop
==
0
)
{
gk_gkmcoreDestroy
(
&
gkmcore
,
showstats
);
gkmcore
=
NULL
;
}
}
}
/*************************************************************************/
/*! This function is my wrapper around malloc that provides the following
enhancements over malloc:
* It always allocates one byte of memory, even if 0 bytes are requested.
This is to ensure that checks of returned values do not lead to NULL
due to 0 bytes requested.
* It zeros-out the memory that is allocated. This is for a quick init
of the underlying datastructures.
*/
/**************************************************************************/
void
*
gk_malloc
(
size_t
nbytes
,
char
*
msg
)
{
void
*
ptr
=
NULL
;
if
(
nbytes
==
0
)
nbytes
++
;
/* Force mallocs to actually allocate some memory */
ptr
=
(
void
*
)
malloc
(
nbytes
);
if
(
ptr
==
NULL
)
{
fprintf
(
stderr
,
" Current memory used: %10zu bytes
\n
"
,
gk_GetCurMemoryUsed
());
fprintf
(
stderr
,
" Maximum memory used: %10zu bytes
\n
"
,
gk_GetMaxMemoryUsed
());
gk_errexit
(
SIGMEM
,
"***Memory allocation failed for %s. Requested size: %zu bytes"
,
msg
,
nbytes
);
return
NULL
;
}
/* add this memory allocation */
if
(
gkmcore
!=
NULL
)
gk_gkmcoreAdd
(
gkmcore
,
GK_MOPT_HEAP
,
nbytes
,
ptr
);
return
ptr
;
}
/*************************************************************************
* This function is my wrapper around realloc
**************************************************************************/
void
*
gk_realloc
(
void
*
oldptr
,
size_t
nbytes
,
char
*
msg
)
{
void
*
ptr
=
NULL
;
if
(
nbytes
==
0
)
nbytes
++
;
/* Force mallocs to actually allocate some memory */
/* remove this memory de-allocation */
if
(
gkmcore
!=
NULL
&&
oldptr
!=
NULL
)
gk_gkmcoreDel
(
gkmcore
,
oldptr
);
ptr
=
(
void
*
)
realloc
(
oldptr
,
nbytes
);
if
(
ptr
==
NULL
)
{
fprintf
(
stderr
,
" Maximum memory used: %10zu bytes
\n
"
,
gk_GetMaxMemoryUsed
());
fprintf
(
stderr
,
" Current memory used: %10zu bytes
\n
"
,
gk_GetCurMemoryUsed
());
gk_errexit
(
SIGMEM
,
"***Memory realloc failed for %s. "
"Requested size: %zu bytes"
,
msg
,
nbytes
);
return
NULL
;
}
/* add this memory allocation */
if
(
gkmcore
!=
NULL
)
gk_gkmcoreAdd
(
gkmcore
,
GK_MOPT_HEAP
,
nbytes
,
ptr
);
return
ptr
;
}
/*************************************************************************
* This function is my wrapper around free, allows multiple pointers
**************************************************************************/
void
gk_free
(
void
**
ptr1
,...)
{
va_list
plist
;
void
**
ptr
;
if
(
*
ptr1
!=
NULL
)
{
free
(
*
ptr1
);
/* remove this memory de-allocation */
if
(
gkmcore
!=
NULL
)
gk_gkmcoreDel
(
gkmcore
,
*
ptr1
);
}
*
ptr1
=
NULL
;
va_start
(
plist
,
ptr1
);
while
((
ptr
=
va_arg
(
plist
,
void
**
))
!=
LTERM
)
{
if
(
*
ptr
!=
NULL
)
{
free
(
*
ptr
);
/* remove this memory de-allocation */
if
(
gkmcore
!=
NULL
)
gk_gkmcoreDel
(
gkmcore
,
*
ptr
);
}
*
ptr
=
NULL
;
}
va_end
(
plist
);
}
/*************************************************************************
* This function returns the current ammount of dynamically allocated
* memory that is used by the system
**************************************************************************/
size_t
gk_GetCurMemoryUsed
()
{
if
(
gkmcore
==
NULL
)
return
0
;
else
return
gkmcore
->
cur_hallocs
;
}
/*************************************************************************
* This function returns the maximum ammount of dynamically allocated
* memory that was used by the system
**************************************************************************/
size_t
gk_GetMaxMemoryUsed
()
{
if
(
gkmcore
==
NULL
)
return
0
;
else
return
gkmcore
->
max_hallocs
;
}
/*************************************************************************/
/*! This function returns the VmSize and VmRSS of the calling process. */
/*************************************************************************/
void
gk_GetVMInfo
(
size_t
*
vmsize
,
size_t
*
vmrss
)
{
FILE
*
fp
;
char
fname
[
1024
];
sprintf
(
fname
,
"/proc/%d/statm"
,
getpid
());
fp
=
gk_fopen
(
fname
,
"r"
,
"proc/pid/statm"
);
if
(
fscanf
(
fp
,
"%zu %zu"
,
vmsize
,
vmrss
)
!=
2
)
errexit
(
"Failed to read to values from %s
\n
"
,
fname
);
gk_fclose
(
fp
);
/*
*vmsize *= sysconf(_SC_PAGESIZE);
*vmrss *= sysconf(_SC_PAGESIZE);
*/
return
;
}
Prev
1
2
3
4
Next
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment