| 1 | /*BHEADER**********************************************************************
|
|---|
| 2 | * Copyright (c) 2008, Lawrence Livermore National Security, LLC.
|
|---|
| 3 | * Produced at the Lawrence Livermore National Laboratory.
|
|---|
| 4 | * This file is part of HYPRE. See file COPYRIGHT for details.
|
|---|
| 5 | *
|
|---|
| 6 | * HYPRE is free software; you can redistribute it and/or modify it under the
|
|---|
| 7 | * terms of the GNU Lesser General Public License (as published by the Free
|
|---|
| 8 | * Software Foundation) version 2.1 dated February 1999.
|
|---|
| 9 | *
|
|---|
| 10 | * $Revision: 2.4 $
|
|---|
| 11 | ***********************************************************************EHEADER*/
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 | /******************************************************************************
|
|---|
| 15 | *
|
|---|
| 16 | * Memory management utilities
|
|---|
| 17 | *
|
|---|
| 18 | *****************************************************************************/
|
|---|
| 19 | #include <stdlib.h>
|
|---|
| 20 | #include <stdio.h>
|
|---|
| 21 | #include "utilities.h"
|
|---|
| 22 |
|
|---|
| 23 | #ifdef HYPRE_USE_PTHREADS
|
|---|
| 24 | #include "threading.h"
|
|---|
| 25 |
|
|---|
| 26 | #ifdef HYPRE_USE_UMALLOC
|
|---|
| 27 | #include "umalloc_local.h"
|
|---|
| 28 |
|
|---|
| 29 | #define _umalloc_(size) (threadid == hypre_NumThreads) ? \
|
|---|
| 30 | (char *) malloc(size) : \
|
|---|
| 31 | (char *) _umalloc(_uparam[threadid].myheap, size)
|
|---|
| 32 | #define _ucalloc_(count, size) (threadid == hypre_NumThreads) ? \
|
|---|
| 33 | (char *) calloc(count, size) : \
|
|---|
| 34 | (char *) _ucalloc(_uparam[threadid].myheap,\
|
|---|
| 35 | count, size)
|
|---|
| 36 | #define _urealloc_(ptr, size) (threadid == hypre_NumThreads) ? \
|
|---|
| 37 | (char *) realloc(ptr, size) : \
|
|---|
| 38 | (char *) _urealloc(ptr, size)
|
|---|
| 39 | #define _ufree_(ptr) (threadid == hypre_NumThreads) ? \
|
|---|
| 40 | free(ptr) : _ufree(ptr)
|
|---|
| 41 | #endif
|
|---|
| 42 | #else
|
|---|
| 43 | #ifdef HYPRE_USE_UMALLOC
|
|---|
| 44 | #undef HYPRE_USE_UMALLOC
|
|---|
| 45 | #endif
|
|---|
| 46 | #endif
|
|---|
| 47 |
|
|---|
| 48 | /******************************************************************************
|
|---|
| 49 | *
|
|---|
| 50 | * Standard routines
|
|---|
| 51 | *
|
|---|
| 52 | *****************************************************************************/
|
|---|
| 53 |
|
|---|
| 54 | /*--------------------------------------------------------------------------
|
|---|
| 55 | * hypre_OutOfMemory
|
|---|
| 56 | *--------------------------------------------------------------------------*/
|
|---|
| 57 |
|
|---|
| 58 | int
|
|---|
| 59 | hypre_OutOfMemory( int size )
|
|---|
| 60 | {
|
|---|
| 61 | printf("Out of memory trying to allocate %d bytes\n", size);
|
|---|
| 62 | fflush(stdout);
|
|---|
| 63 |
|
|---|
| 64 | hypre_error(HYPRE_ERROR_MEMORY);
|
|---|
| 65 |
|
|---|
| 66 | return 0;
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | /*--------------------------------------------------------------------------
|
|---|
| 70 | * hypre_MAlloc
|
|---|
| 71 | *--------------------------------------------------------------------------*/
|
|---|
| 72 |
|
|---|
| 73 | char *
|
|---|
| 74 | hypre_MAlloc( int size )
|
|---|
| 75 | {
|
|---|
| 76 | char *ptr;
|
|---|
| 77 |
|
|---|
| 78 | if (size > 0)
|
|---|
| 79 | {
|
|---|
| 80 | #ifdef HYPRE_USE_UMALLOC
|
|---|
| 81 | int threadid = hypre_GetThreadID();
|
|---|
| 82 |
|
|---|
| 83 | ptr = _umalloc_(size);
|
|---|
| 84 | #else
|
|---|
| 85 | ptr = malloc(size);
|
|---|
| 86 | #endif
|
|---|
| 87 |
|
|---|
| 88 | #if 1
|
|---|
| 89 | if (ptr == NULL)
|
|---|
| 90 | {
|
|---|
| 91 | hypre_OutOfMemory(size);
|
|---|
| 92 | }
|
|---|
| 93 | #endif
|
|---|
| 94 | }
|
|---|
| 95 | else
|
|---|
| 96 | {
|
|---|
| 97 | ptr = NULL;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | return ptr;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | /*--------------------------------------------------------------------------
|
|---|
| 104 | * hypre_CAlloc
|
|---|
| 105 | *--------------------------------------------------------------------------*/
|
|---|
| 106 |
|
|---|
| 107 | char *
|
|---|
| 108 | hypre_CAlloc( int count,
|
|---|
| 109 | int elt_size )
|
|---|
| 110 | {
|
|---|
| 111 | char *ptr;
|
|---|
| 112 | int size = count*elt_size;
|
|---|
| 113 |
|
|---|
| 114 | if (size > 0)
|
|---|
| 115 | {
|
|---|
| 116 | #ifdef HYPRE_USE_UMALLOC
|
|---|
| 117 | int threadid = hypre_GetThreadID();
|
|---|
| 118 |
|
|---|
| 119 | ptr = _ucalloc_(count, elt_size);
|
|---|
| 120 | #else
|
|---|
| 121 | ptr = calloc(count, elt_size);
|
|---|
| 122 | #endif
|
|---|
| 123 |
|
|---|
| 124 | #if 1
|
|---|
| 125 | if (ptr == NULL)
|
|---|
| 126 | {
|
|---|
| 127 | hypre_OutOfMemory(size);
|
|---|
| 128 | }
|
|---|
| 129 | #endif
|
|---|
| 130 | }
|
|---|
| 131 | else
|
|---|
| 132 | {
|
|---|
| 133 | ptr = NULL;
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | return ptr;
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | /*--------------------------------------------------------------------------
|
|---|
| 140 | * hypre_ReAlloc
|
|---|
| 141 | *--------------------------------------------------------------------------*/
|
|---|
| 142 |
|
|---|
| 143 | char *
|
|---|
| 144 | hypre_ReAlloc( char *ptr,
|
|---|
| 145 | int size )
|
|---|
| 146 | {
|
|---|
| 147 | #ifdef HYPRE_USE_UMALLOC
|
|---|
| 148 | if (ptr == NULL)
|
|---|
| 149 | {
|
|---|
| 150 | ptr = hypre_MAlloc(size);
|
|---|
| 151 | }
|
|---|
| 152 | else if (size == 0)
|
|---|
| 153 | {
|
|---|
| 154 | hypre_Free(ptr);
|
|---|
| 155 | }
|
|---|
| 156 | else
|
|---|
| 157 | {
|
|---|
| 158 | int threadid = hypre_GetThreadID();
|
|---|
| 159 | ptr = _urealloc_(ptr, size);
|
|---|
| 160 | }
|
|---|
| 161 | #else
|
|---|
| 162 | if (ptr == NULL)
|
|---|
| 163 | {
|
|---|
| 164 | ptr = malloc(size);
|
|---|
| 165 | }
|
|---|
| 166 | else
|
|---|
| 167 | {
|
|---|
| 168 | ptr = realloc(ptr, size);
|
|---|
| 169 | }
|
|---|
| 170 | #endif
|
|---|
| 171 |
|
|---|
| 172 | #if 1
|
|---|
| 173 | if ((ptr == NULL) && (size > 0))
|
|---|
| 174 | {
|
|---|
| 175 | hypre_OutOfMemory(size);
|
|---|
| 176 | }
|
|---|
| 177 | #endif
|
|---|
| 178 |
|
|---|
| 179 | return ptr;
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | /*--------------------------------------------------------------------------
|
|---|
| 183 | * hypre_Free
|
|---|
| 184 | *--------------------------------------------------------------------------*/
|
|---|
| 185 |
|
|---|
| 186 | void
|
|---|
| 187 | hypre_Free( char *ptr )
|
|---|
| 188 | {
|
|---|
| 189 | if (ptr)
|
|---|
| 190 | {
|
|---|
| 191 | #ifdef HYPRE_USE_UMALLOC
|
|---|
| 192 | int threadid = hypre_GetThreadID();
|
|---|
| 193 |
|
|---|
| 194 | _ufree_(ptr);
|
|---|
| 195 | #else
|
|---|
| 196 | free(ptr);
|
|---|
| 197 | #endif
|
|---|
| 198 | }
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 |
|
|---|
| 202 | /*--------------------------------------------------------------------------
|
|---|
| 203 | * These Shared routines are for one thread to allocate memory for data
|
|---|
| 204 | * will be visible to all threads. The file-scope pointer
|
|---|
| 205 | * global_alloc_ptr is used in these routines.
|
|---|
| 206 | *--------------------------------------------------------------------------*/
|
|---|
| 207 |
|
|---|
| 208 | #ifdef HYPRE_USE_PTHREADS
|
|---|
| 209 |
|
|---|
| 210 | char *global_alloc_ptr;
|
|---|
| 211 | double *global_data_ptr;
|
|---|
| 212 |
|
|---|
| 213 | /*--------------------------------------------------------------------------
|
|---|
| 214 | * hypre_SharedMAlloc
|
|---|
| 215 | *--------------------------------------------------------------------------*/
|
|---|
| 216 |
|
|---|
| 217 | char *
|
|---|
| 218 | hypre_SharedMAlloc( int size )
|
|---|
| 219 | {
|
|---|
| 220 | char *ptr;
|
|---|
| 221 | int unthreaded = pthread_equal(initial_thread, pthread_self());
|
|---|
| 222 | int I_call_malloc = unthreaded ||
|
|---|
| 223 | pthread_equal(hypre_thread[0],pthread_self());
|
|---|
| 224 |
|
|---|
| 225 | if (I_call_malloc) {
|
|---|
| 226 | global_alloc_ptr = hypre_MAlloc( size );
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | hypre_barrier(&talloc_mtx, unthreaded);
|
|---|
| 230 | ptr = global_alloc_ptr;
|
|---|
| 231 | hypre_barrier(&talloc_mtx, unthreaded);
|
|---|
| 232 |
|
|---|
| 233 | return ptr;
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | /*--------------------------------------------------------------------------
|
|---|
| 237 | * hypre_SharedCAlloc
|
|---|
| 238 | *--------------------------------------------------------------------------*/
|
|---|
| 239 |
|
|---|
| 240 | char *
|
|---|
| 241 | hypre_SharedCAlloc( int count,
|
|---|
| 242 | int elt_size )
|
|---|
| 243 | {
|
|---|
| 244 | char *ptr;
|
|---|
| 245 | int unthreaded = pthread_equal(initial_thread, pthread_self());
|
|---|
| 246 | int I_call_calloc = unthreaded ||
|
|---|
| 247 | pthread_equal(hypre_thread[0],pthread_self());
|
|---|
| 248 |
|
|---|
| 249 | if (I_call_calloc) {
|
|---|
| 250 | global_alloc_ptr = hypre_CAlloc( count, elt_size );
|
|---|
| 251 | }
|
|---|
| 252 |
|
|---|
| 253 | hypre_barrier(&talloc_mtx, unthreaded);
|
|---|
| 254 | ptr = global_alloc_ptr;
|
|---|
| 255 | hypre_barrier(&talloc_mtx, unthreaded);
|
|---|
| 256 |
|
|---|
| 257 | return ptr;
|
|---|
| 258 | }
|
|---|
| 259 |
|
|---|
| 260 | /*--------------------------------------------------------------------------
|
|---|
| 261 | * hypre_SharedReAlloc
|
|---|
| 262 | *--------------------------------------------------------------------------*/
|
|---|
| 263 |
|
|---|
| 264 | char *
|
|---|
| 265 | hypre_SharedReAlloc( char *ptr,
|
|---|
| 266 | int size )
|
|---|
| 267 | {
|
|---|
| 268 | int unthreaded = pthread_equal(initial_thread, pthread_self());
|
|---|
| 269 | int I_call_realloc = unthreaded ||
|
|---|
| 270 | pthread_equal(hypre_thread[0],pthread_self());
|
|---|
| 271 |
|
|---|
| 272 | if (I_call_realloc) {
|
|---|
| 273 | global_alloc_ptr = hypre_ReAlloc( ptr, size );
|
|---|
| 274 | }
|
|---|
| 275 |
|
|---|
| 276 | hypre_barrier(&talloc_mtx, unthreaded);
|
|---|
| 277 | ptr = global_alloc_ptr;
|
|---|
| 278 | hypre_barrier(&talloc_mtx, unthreaded);
|
|---|
| 279 |
|
|---|
| 280 | return ptr;
|
|---|
| 281 | }
|
|---|
| 282 |
|
|---|
| 283 | /*--------------------------------------------------------------------------
|
|---|
| 284 | * hypre_SharedFree
|
|---|
| 285 | *--------------------------------------------------------------------------*/
|
|---|
| 286 |
|
|---|
| 287 | void
|
|---|
| 288 | hypre_SharedFree( char *ptr )
|
|---|
| 289 | {
|
|---|
| 290 | int unthreaded = pthread_equal(initial_thread, pthread_self());
|
|---|
| 291 | int I_call_free = unthreaded ||
|
|---|
| 292 | pthread_equal(hypre_thread[0],pthread_self());
|
|---|
| 293 |
|
|---|
| 294 | hypre_barrier(&talloc_mtx, unthreaded);
|
|---|
| 295 | if (I_call_free) {
|
|---|
| 296 | hypre_Free(ptr);
|
|---|
| 297 | }
|
|---|
| 298 | hypre_barrier(&talloc_mtx, unthreaded);
|
|---|
| 299 | }
|
|---|
| 300 |
|
|---|
| 301 | /*--------------------------------------------------------------------------
|
|---|
| 302 | * hypre_IncrementSharedDataPtr
|
|---|
| 303 | *--------------------------------------------------------------------------*/
|
|---|
| 304 |
|
|---|
| 305 | double *
|
|---|
| 306 | hypre_IncrementSharedDataPtr( double *ptr, int size )
|
|---|
| 307 | {
|
|---|
| 308 | int unthreaded = pthread_equal(initial_thread, pthread_self());
|
|---|
| 309 | int I_increment = unthreaded ||
|
|---|
| 310 | pthread_equal(hypre_thread[0],pthread_self());
|
|---|
| 311 |
|
|---|
| 312 | if (I_increment) {
|
|---|
| 313 | global_data_ptr = ptr + size;
|
|---|
| 314 | }
|
|---|
| 315 |
|
|---|
| 316 | hypre_barrier(&talloc_mtx, unthreaded);
|
|---|
| 317 | ptr = global_data_ptr;
|
|---|
| 318 | hypre_barrier(&talloc_mtx, unthreaded);
|
|---|
| 319 |
|
|---|
| 320 | return ptr;
|
|---|
| 321 | }
|
|---|
| 322 |
|
|---|
| 323 | #endif
|
|---|
| 324 |
|
|---|