1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
/*
* Copyright 1997, Regents of the University of Minnesota
*
* memory.c
*
* This file contains routines that deal with memory allocation
*
* Started 2/24/96
* George
*
* $Id: memory.c,v 1.1 2003/07/24 18:39:08 karypis Exp $
*
*/
#include <metis.h>
/*************************************************************************
* This function allocates memory for the workspace
**************************************************************************/
void AllocateWorkSpace(CtrlType *ctrl, GraphType *graph, int nparts)
{
ctrl->wspace.pmat = NULL;
if (ctrl->optype == OP_KMETIS) {
ctrl->wspace.edegrees = (EDegreeType *)GKmalloc(graph->nedges*sizeof(EDegreeType), "AllocateWorkSpace: edegrees");
ctrl->wspace.vedegrees = NULL;
ctrl->wspace.auxcore = (idxtype *)ctrl->wspace.edegrees;
ctrl->wspace.pmat = idxmalloc(nparts*nparts, "AllocateWorkSpace: pmat");
/* Memory requirements for different phases
Coarsening
Matching: 4*nvtxs vectors
Contraction: 2*nvtxs vectors (from the above 4), 1*nparts, 1*Nedges
Total = MAX(4*nvtxs, 2*nvtxs+nparts+nedges)
Refinement
Random Refinement/Balance: 5*nparts + 1*nvtxs + 2*nedges
Greedy Refinement/Balance: 5*nparts + 2*nvtxs + 2*nedges + 1*PQueue(==Nvtxs)
Total = 5*nparts + 3*nvtxs + 2*nedges
Total = 5*nparts + 3*nvtxs + 2*nedges
*/
ctrl->wspace.maxcore = 3*(graph->nvtxs+1) + /* Match/Refinement vectors */
5*(nparts+1) + /* Partition weights etc */
graph->nvtxs*(sizeof(ListNodeType)/sizeof(idxtype)) + /* Greedy k-way balance/refine */
20 /* padding for 64 bit machines */
;
}
else if (ctrl->optype == OP_KVMETIS) {
ctrl->wspace.edegrees = NULL;
ctrl->wspace.vedegrees = (VEDegreeType *)GKmalloc(graph->nedges*sizeof(VEDegreeType), "AllocateWorkSpace: vedegrees");
ctrl->wspace.auxcore = (idxtype *)ctrl->wspace.vedegrees;
ctrl->wspace.pmat = idxmalloc(nparts*nparts, "AllocateWorkSpace: pmat");
/* Memory requirements for different phases are identical to KMETIS */
ctrl->wspace.maxcore = 3*(graph->nvtxs+1) + /* Match/Refinement vectors */
3*(nparts+1) + /* Partition weights etc */
graph->nvtxs*(sizeof(ListNodeType)/sizeof(idxtype)) + /* Greedy k-way balance/refine */
20 /* padding for 64 bit machines */
;
}
else {
ctrl->wspace.edegrees = (EDegreeType *)idxmalloc(graph->nedges, "AllocateWorkSpace: edegrees");
ctrl->wspace.vedegrees = NULL;
ctrl->wspace.auxcore = (idxtype *)ctrl->wspace.edegrees;
ctrl->wspace.maxcore = 5*(graph->nvtxs+1) + /* Refinement vectors */
4*(nparts+1) + /* Partition weights etc */
2*graph->ncon*graph->nvtxs*(sizeof(ListNodeType)/sizeof(idxtype)) + /* 2-way refinement */
2*graph->ncon*(NEG_GAINSPAN+PLUS_GAINSPAN+1)*(sizeof(ListNodeType *)/sizeof(idxtype)) + /* 2-way refinement */
20 /* padding for 64 bit machines */
;
}
ctrl->wspace.maxcore += HTLENGTH;
ctrl->wspace.core = idxmalloc(ctrl->wspace.maxcore, "AllocateWorkSpace: maxcore");
ctrl->wspace.ccore = 0;
}
/*************************************************************************
* This function allocates memory for the workspace
**************************************************************************/
void FreeWorkSpace(CtrlType *ctrl, GraphType *graph)
{
GKfree(&ctrl->wspace.edegrees, &ctrl->wspace.vedegrees, &ctrl->wspace.core, &ctrl->wspace.pmat, LTERM);
}
/*************************************************************************
* This function returns how may words are left in the workspace
**************************************************************************/
int WspaceAvail(CtrlType *ctrl)
{
return ctrl->wspace.maxcore - ctrl->wspace.ccore;
}
/*************************************************************************
* This function allocate space from the core
**************************************************************************/
idxtype *idxwspacemalloc(CtrlType *ctrl, int n)
{
n += n%2; /* This is a fix for 64 bit machines that require 8-byte pointer allignment */
ctrl->wspace.ccore += n;
ASSERT(ctrl->wspace.ccore <= ctrl->wspace.maxcore);
return ctrl->wspace.core + ctrl->wspace.ccore - n;
}
/*************************************************************************
* This function frees space from the core
**************************************************************************/
void idxwspacefree(CtrlType *ctrl, int n)
{
n += n%2; /* This is a fix for 64 bit machines that require 8-byte pointer allignment */
ctrl->wspace.ccore -= n;
ASSERT(ctrl->wspace.ccore >= 0);
}
/*************************************************************************
* This function allocate space from the core
**************************************************************************/
float *fwspacemalloc(CtrlType *ctrl, int n)
{
n += n%2; /* This is a fix for 64 bit machines that require 8-byte pointer allignment */
ctrl->wspace.ccore += n;
ASSERT(ctrl->wspace.ccore <= ctrl->wspace.maxcore);
return (float *) (ctrl->wspace.core + ctrl->wspace.ccore - n);
}
/*************************************************************************
* This function frees space from the core
**************************************************************************/
void fwspacefree(CtrlType *ctrl, int n)
{
n += n%2; /* This is a fix for 64 bit machines that require 8-byte pointer allignment */
ctrl->wspace.ccore -= n;
ASSERT(ctrl->wspace.ccore >= 0);
}
/*************************************************************************
* This function creates a CoarseGraphType data structure and initializes
* the various fields
**************************************************************************/
GraphType *CreateGraph(void)
{
GraphType *graph;
graph = (GraphType *)GKmalloc(sizeof(GraphType), "CreateCoarseGraph: graph");
InitGraph(graph);
return graph;
}
/*************************************************************************
* This function creates a CoarseGraphType data structure and initializes
* the various fields
**************************************************************************/
void InitGraph(GraphType *graph)
{
graph->gdata = graph->rdata = NULL;
graph->nvtxs = graph->nedges = -1;
graph->mincut = graph->minvol = -1;
graph->xadj = graph->vwgt = graph->adjncy = graph->adjwgt = NULL;
graph->adjwgtsum = NULL;
graph->label = NULL;
graph->cmap = NULL;
graph->where = graph->pwgts = NULL;
graph->id = graph->ed = NULL;
graph->bndptr = graph->bndind = NULL;
graph->rinfo = NULL;
graph->vrinfo = NULL;
graph->nrinfo = NULL;
graph->ncon = -1;
graph->nvwgt = NULL;
graph->npwgts = NULL;
graph->vsize = NULL;
graph->coarser = graph->finer = NULL;
}
/*************************************************************************
* This function deallocates any memory stored in a graph
**************************************************************************/
void FreeGraph(GraphType *graph)
{
GKfree(&graph->gdata, &graph->nvwgt, &graph->rdata, &graph->npwgts, LTERM);
free(graph);
}
|