Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/elmergrid/src/metis-5.1.0/libmetis/wspace.c
3206 views
1
/*!
2
\file
3
\brief Functions dealing with memory allocation and workspace management
4
5
\date Started 2/24/96
6
\author George
7
\author Copyright 1997-2009, Regents of the University of Minnesota
8
\version $Id: wspace.c 10492 2011-07-06 09:28:42Z karypis $
9
*/
10
11
#include "metislib.h"
12
13
14
/*************************************************************************/
15
/*! This function allocates memory for the workspace */
16
/*************************************************************************/
17
void AllocateWorkSpace(ctrl_t *ctrl, graph_t *graph)
18
{
19
size_t coresize;
20
21
switch (ctrl->optype) {
22
case METIS_OP_PMETIS:
23
coresize = 3*(graph->nvtxs+1)*sizeof(idx_t) +
24
5*(ctrl->nparts+1)*graph->ncon*sizeof(idx_t) +
25
5*(ctrl->nparts+1)*graph->ncon*sizeof(real_t);
26
break;
27
default:
28
coresize = 4*(graph->nvtxs+1)*sizeof(idx_t) +
29
5*(ctrl->nparts+1)*graph->ncon*sizeof(idx_t) +
30
5*(ctrl->nparts+1)*graph->ncon*sizeof(real_t);
31
}
32
/*coresize = 0;*/
33
ctrl->mcore = gk_mcoreCreate(coresize);
34
35
ctrl->nbrpoolsize = 0;
36
ctrl->nbrpoolcpos = 0;
37
}
38
39
40
/*************************************************************************/
41
/*! This function allocates refinement-specific memory for the workspace */
42
/*************************************************************************/
43
void AllocateRefinementWorkSpace(ctrl_t *ctrl, idx_t nbrpoolsize)
44
{
45
ctrl->nbrpoolsize = nbrpoolsize;
46
ctrl->nbrpoolcpos = 0;
47
ctrl->nbrpoolreallocs = 0;
48
49
switch (ctrl->objtype) {
50
case METIS_OBJTYPE_CUT:
51
ctrl->cnbrpool = (cnbr_t *)gk_malloc(ctrl->nbrpoolsize*sizeof(cnbr_t),
52
"AllocateRefinementWorkSpace: cnbrpool");
53
break;
54
55
case METIS_OBJTYPE_VOL:
56
ctrl->vnbrpool = (vnbr_t *)gk_malloc(ctrl->nbrpoolsize*sizeof(vnbr_t),
57
"AllocateRefinementWorkSpace: vnbrpool");
58
break;
59
60
default:
61
gk_errexit(SIGERR, "Unknown objtype of %d\n", ctrl->objtype);
62
}
63
64
65
/* Allocate the memory for the sparse subdomain graph */
66
if (ctrl->minconn) {
67
ctrl->pvec1 = imalloc(ctrl->nparts+1, "AllocateRefinementWorkSpace: pvec1");
68
ctrl->pvec2 = imalloc(ctrl->nparts+1, "AllocateRefinementWorkSpace: pvec2");
69
ctrl->maxnads = ismalloc(ctrl->nparts, INIT_MAXNAD, "AllocateRefinementWorkSpace: maxnads");
70
ctrl->nads = imalloc(ctrl->nparts, "AllocateRefinementWorkSpace: nads");
71
ctrl->adids = iAllocMatrix(ctrl->nparts, INIT_MAXNAD, 0, "AllocateRefinementWorkSpace: adids");
72
ctrl->adwgts = iAllocMatrix(ctrl->nparts, INIT_MAXNAD, 0, "AllocateRefinementWorkSpace: adwgts");
73
}
74
}
75
76
77
/*************************************************************************/
78
/*! This function frees the workspace */
79
/*************************************************************************/
80
void FreeWorkSpace(ctrl_t *ctrl)
81
{
82
gk_mcoreDestroy(&ctrl->mcore, ctrl->dbglvl&METIS_DBG_INFO);
83
84
IFSET(ctrl->dbglvl, METIS_DBG_INFO,
85
printf(" nbrpool statistics\n"
86
" nbrpoolsize: %12zu nbrpoolcpos: %12zu\n"
87
" nbrpoolreallocs: %12zu\n\n",
88
ctrl->nbrpoolsize, ctrl->nbrpoolcpos,
89
ctrl->nbrpoolreallocs));
90
91
gk_free((void **)&ctrl->cnbrpool, &ctrl->vnbrpool, LTERM);
92
ctrl->nbrpoolsize = 0;
93
ctrl->nbrpoolcpos = 0;
94
95
if (ctrl->minconn) {
96
iFreeMatrix(&(ctrl->adids), ctrl->nparts, INIT_MAXNAD);
97
iFreeMatrix(&(ctrl->adwgts), ctrl->nparts, INIT_MAXNAD);
98
99
gk_free((void **)&ctrl->pvec1, &ctrl->pvec2,
100
&ctrl->maxnads, &ctrl->nads, LTERM);
101
}
102
}
103
104
105
/*************************************************************************/
106
/*! This function allocate space from the workspace/heap */
107
/*************************************************************************/
108
void *wspacemalloc(ctrl_t *ctrl, size_t nbytes)
109
{
110
return gk_mcoreMalloc(ctrl->mcore, nbytes);
111
}
112
113
114
/*************************************************************************/
115
/*! This function sets a marker in the stack of malloc ops to be used
116
subsequently for freeing purposes */
117
/*************************************************************************/
118
void wspacepush(ctrl_t *ctrl)
119
{
120
gk_mcorePush(ctrl->mcore);
121
}
122
123
124
/*************************************************************************/
125
/*! This function frees all mops since the last push */
126
/*************************************************************************/
127
void wspacepop(ctrl_t *ctrl)
128
{
129
gk_mcorePop(ctrl->mcore);
130
}
131
132
133
/*************************************************************************/
134
/*! This function allocate space from the core */
135
/*************************************************************************/
136
idx_t *iwspacemalloc(ctrl_t *ctrl, idx_t n)
137
{
138
return (idx_t *)wspacemalloc(ctrl, n*sizeof(idx_t));
139
}
140
141
142
/*************************************************************************/
143
/*! This function allocate space from the core */
144
/*************************************************************************/
145
real_t *rwspacemalloc(ctrl_t *ctrl, idx_t n)
146
{
147
return (real_t *)wspacemalloc(ctrl, n*sizeof(real_t));
148
}
149
150
151
/*************************************************************************/
152
/*! This function allocate space from the core */
153
/*************************************************************************/
154
ikv_t *ikvwspacemalloc(ctrl_t *ctrl, idx_t n)
155
{
156
return (ikv_t *)wspacemalloc(ctrl, n*sizeof(ikv_t));
157
}
158
159
160
/*************************************************************************/
161
/*! This function resets the cnbrpool */
162
/*************************************************************************/
163
void cnbrpoolReset(ctrl_t *ctrl)
164
{
165
ctrl->nbrpoolcpos = 0;
166
}
167
168
169
/*************************************************************************/
170
/*! This function gets the next free index from cnbrpool */
171
/*************************************************************************/
172
idx_t cnbrpoolGetNext(ctrl_t *ctrl, idx_t nnbrs)
173
{
174
ctrl->nbrpoolcpos += nnbrs;
175
176
if (ctrl->nbrpoolcpos > ctrl->nbrpoolsize) {
177
ctrl->nbrpoolsize += gk_max(10*nnbrs, ctrl->nbrpoolsize/2);
178
179
ctrl->cnbrpool = (cnbr_t *)gk_realloc(ctrl->cnbrpool,
180
ctrl->nbrpoolsize*sizeof(cnbr_t), "cnbrpoolGet: cnbrpool");
181
ctrl->nbrpoolreallocs++;
182
}
183
184
return ctrl->nbrpoolcpos - nnbrs;
185
}
186
187
188
/*************************************************************************/
189
/*! This function resets the vnbrpool */
190
/*************************************************************************/
191
void vnbrpoolReset(ctrl_t *ctrl)
192
{
193
ctrl->nbrpoolcpos = 0;
194
}
195
196
197
/*************************************************************************/
198
/*! This function gets the next free index from vnbrpool */
199
/*************************************************************************/
200
idx_t vnbrpoolGetNext(ctrl_t *ctrl, idx_t nnbrs)
201
{
202
ctrl->nbrpoolcpos += nnbrs;
203
204
if (ctrl->nbrpoolcpos > ctrl->nbrpoolsize) {
205
ctrl->nbrpoolsize += gk_max(10*nnbrs, ctrl->nbrpoolsize/2);
206
207
ctrl->vnbrpool = (vnbr_t *)gk_realloc(ctrl->vnbrpool,
208
ctrl->nbrpoolsize*sizeof(vnbr_t), "vnbrpoolGet: vnbrpool");
209
ctrl->nbrpoolreallocs++;
210
}
211
212
return ctrl->nbrpoolcpos - nnbrs;
213
}
214
215
216