Fast Auxiliary Space Preconditioning  1.8.4 Feb/15/2016
memory.c
Go to the documentation of this file.
1 
6 #include "fasp.h"
7 
8 #if DLMALLOC
9 
10 #include "dlmalloc.h"
11 
12 #elif NEDMALLOC
13 
14 #include "nedmalloc.h"
15 
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19 
20  void * nedcalloc(size_t no, size_t size);
21  void * nedrealloc(void *mem, size_t size);
22  void nedfree(void *mem);
23 
24 #ifdef __cplusplus
25 }
26 #endif
27 
28 #endif
29 
30 /*---------------------------------*/
31 /*-- Global Variables --*/
32 /*---------------------------------*/
33 
34 unsigned INT total_alloc_mem = 0;
35 unsigned INT total_alloc_count = 0;
36 const INT Million = 1048576;
37 
38 /*---------------------------------*/
39 /*-- Public Functions --*/
40 /*---------------------------------*/
41 
61  INT type)
62 {
63  const LONGLONG tsize = size*type;
64 
65  void * mem = NULL;
66 
67 #if DEBUG_MODE > 2
68  printf("### DEBUG: Trying to allocate %.3lfMB RAM!\n", (REAL)tsize/Million);
69 #endif
70 
71  if ( tsize > 0 ) {
72 
73 #if DLMALLOC
74  mem = dlcalloc(size,type);
75 #elif NEDMALLOC
76  mem = nedcalloc(size,type);
77 #else
78  mem = calloc(size,type);
79 #endif
80 
81 #if CHMEM_MODE
82  total_alloc_mem += tsize;
84 #endif
85  }
86 
87  if ( mem == NULL ) {
88  printf("### WARNING: Cannot allocate %.3lfMB RAM!\n", (REAL)tsize/Million);
89  }
90 
91  return mem;
92 }
93 
110 void * fasp_mem_realloc (void * oldmem,
111  LONGLONG tsize)
112 {
113  void * mem = NULL;
114 
115 #if DEBUG_MODE > 2
116  printf("### DEBUG: Trying to allocate %.3lfMB RAM!\n", (REAL)tsize/Million);
117 #endif
118 
119  if ( tsize > 0 ) {
120 
121 #if DLMALLOC
122  mem = dlrealloc(oldmem,tsize);
123 #elif NEDMALLOC
124  mem = nedrealloc(oldmem,tsize);
125 #else
126  mem = realloc(oldmem,tsize);
127 #endif
128 
129  }
130 
131  if ( mem == NULL ) {
132  printf("### WARNING: Cannot allocate %.3lfMB RAM!\n", (REAL)tsize/Million);
133  }
134 
135  return mem;
136 }
137 
150 void fasp_mem_free (void * mem)
151 {
152  if ( mem ) {
153 #if DLMALLOC
154  dlfree(mem);
155 #elif NEDMALLOC
156  nedfree(mem);
157 #else
158  free(mem);
159 #endif
160 
161 #if CHMEM_MODE
163 #endif
164  }
165 }
166 
176 {
177 #if CHMEM_MODE
178  printf("### DEBUG: Number of alloc = %d, allocating memory = %.3fMB.\n",
180 #endif
181 }
182 
198  const char *message,
199  INT ERR)
200 {
201  if ( ptr == NULL ) {
202  printf("### ERROR: %s", message);
203  return ERR;
204  }
205  else {
206  return FASP_SUCCESS;
207  }
208 }
209 
223 {
224  const INT memneed = 2*iludata->row; // estimated memory usage
225 
226  if ( iludata->nwork >= memneed ) {
227  return FASP_SUCCESS;
228  }
229  else {
230  printf("### ERROR: ILU needs %d RAM, only %d allocated!\n",
231  memneed, iludata->nwork);
232  return ERROR_ALLOC_MEM;
233  }
234 }
235 
249 {
250  if ( (A->IA == NULL) || (A->JA == NULL) || (A->val == NULL) ) {
251  printf("### ERROR: Something is wrong with the matrix!\n");
252  return ERROR_ALLOC_MEM;
253  }
254  else {
255  return FASP_SUCCESS;
256  }
257 }
258 
259 /*---------------------------------*/
260 /*-- End of File --*/
261 /*---------------------------------*/
INT * JA
integer array of column indexes, the size is nnz
Definition: fasp.h:163
#define REAL
Definition: fasp.h:67
void fasp_mem_usage()
Show total allocated memory currently.
Definition: memory.c:175
unsigned INT total_alloc_count
Total allocated memory amount.
Definition: memory.c:35
REAL * val
nonzero entries of A
Definition: fasp.h:166
void * fasp_mem_calloc(LONGLONG size, INT type)
1M = 1024*1024
Definition: memory.c:60
#define INT
Definition: fasp.h:64
#define FASP_SUCCESS
Definition of return status and error messages.
Definition: fasp_const.h:27
Data for ILU setup.
Definition: fasp.h:400
INT nwork
work space size
Definition: fasp.h:421
void fasp_mem_free(void *mem)
Free up previous allocated memory body.
Definition: memory.c:150
const INT Million
Total number of allocations.
Definition: memory.c:36
Main header file for FASP.
void * fasp_mem_realloc(void *oldmem, LONGLONG tsize)
Reallocate, initiate, and check memory.
Definition: memory.c:110
#define ERROR_ALLOC_MEM
Definition: fasp_const.h:37
INT row
row number of matrix LU, m
Definition: fasp.h:403
SHORT fasp_mem_check(void *ptr, const char *message, INT ERR)
Check wether a point is null or not.
Definition: memory.c:197
Sparse matrix of REAL type in CSR format.
Definition: fasp.h:148
SHORT fasp_mem_iludata_check(ILU_data *iludata)
Check wether a ILU_data has enough work space.
Definition: memory.c:222
#define SHORT
FASP integer and floating point numbers.
Definition: fasp.h:63
SHORT fasp_mem_dcsr_check(dCSRmat *A)
Check wether a dCSRmat A has sucessfully allocated memory.
Definition: memory.c:248
#define LONGLONG
Definition: fasp.h:66
unsigned INT total_alloc_mem
Definition: memory.c:34
INT * IA
integer array of row pointers, the size is m+1
Definition: fasp.h:160