| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
#include <esl_config.h> |
| 13 |
|
| 14 |
#include <stdio.h> |
| 15 |
#include <stdlib.h> |
| 16 |
#include <stdint.h> |
| 17 |
|
| 18 |
#include "easel.h" |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#if defined(eslALLOC_TESTDRIVE) || defined (eslALLOC_BENCHMARK) || \ |
| 24 |
(! defined(HAVE_POSIX_MEMALIGN) && ! defined(HAVE_ALIGNED_ALLOC) && ! defined(HAVE__MM_MALLOC)) |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
static void * |
| 39 |
alloc_aligned_fallback(size_t size, size_t alignment) |
| 40 |
{ |
| 41 |
void *pp; |
| 42 |
uint8_t *p; |
| 43 |
int r; |
| 44 |
|
| 45 |
ESL_DASSERT1(( size >= 0 )); |
| 46 |
ESL_DASSERT1(( alignment > 0 && alignment <= 256 )); |
| 47 |
ESL_DASSERT1(( (alignment & (alignment-1)) == 0 )); |
| 48 |
|
| 49 |
if ( (pp = malloc(size + alignment)) == NULL) return NULL; |
| 50 |
p = (uint8_t *) (((uintptr_t) pp + alignment) & (~(alignment-1))); |
| 51 |
r = (int) ((uintptr_t) p - (uintptr_t) pp); |
| 52 |
p[-1] = (uint8_t) (r-1); |
| 53 |
|
| 54 |
|
| 55 |
#if 0 |
| 56 |
printf("allocating...\n"); |
| 57 |
printf("original malloc'ed ptr = %p\n", pp); |
| 58 |
printf("aligned ptr = %p\n", p); |
| 59 |
printf("offset = %d\n", r); |
| 60 |
#endif |
| 61 |
|
| 62 |
return (void *) p; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
static void |
| 67 |
alloc_aligned_free_fallback(void *p) |
| 68 |
{ |
| 69 |
void *pp; |
| 70 |
unsigned int r; |
| 71 |
|
| 72 |
r = (unsigned int) (((uint8_t *) p)[-1]) + 1; |
| 73 |
pp = (void *) ((uintptr_t) p - r); |
| 74 |
free(pp); |
| 75 |
|
| 76 |
#if 0 |
| 77 |
printf("freeing...\n"); |
| 78 |
printf("offset = %d\n", r); |
| 79 |
printf("aligned ptr = %p\n", p); |
| 80 |
printf("original malloc'ed ptr = %p\n", pp); |
| 81 |
#endif |
| 82 |
} |
| 83 |
#endif |
| 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 |
void * |
| 127 |
esl_alloc_aligned(size_t size, size_t alignment) |
| 128 |
{ |
| 129 |
void *p; |
| 130 |
|
| 131 |
ESL_DASSERT1(( size > 0 )); |
| 132 |
ESL_DASSERT1(( alignment > sizeof(void *) && alignment <= 256 )); |
| 133 |
ESL_DASSERT1(( (alignment & (alignment-1)) == 0 )); |
| 134 |
|
| 135 |
#ifdef HAVE_POSIX_MEMALIGN |
| 136 |
if (posix_memalign(&p, alignment, size) != 0) return NULL; |
| 137 |
#elif HAVE_ALIGNED_ALLOC |
| 138 |
p = aligned_alloc(alignment, size); |
| 139 |
#elif HAVE__MM_MALLOC |
| 140 |
p = _mm_malloc(size, alignment); |
| 141 |
#else |
| 142 |
p = alloc_aligned_fallback(size, alignment); |
| 143 |
#endif |
| 144 |
|
| 145 |
if (p == NULL) |
| 146 |
esl_exception(eslEMEM, FALSE, __FILE__, __LINE__, "aligned alloc of size %d failed", size); |
| 147 |
|
| 148 |
return p; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
void |
| 156 |
esl_alloc_free(void *p) |
| 157 |
{ |
| 158 |
if (p) { |
| 159 |
#ifdef HAVE_POSIX_MEMALIGN |
| 160 |
free(p); |
| 161 |
#elif HAVE_ALIGNED_ALLOC |
| 162 |
free(p); |
| 163 |
#elif HAVE__MM_MALLOC |
| 164 |
_mm_free(p); |
| 165 |
#else |
| 166 |
alloc_aligned_free_fallback(p); |
| 167 |
#endif |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
#ifdef eslALLOC_BENCHMARK |
| 177 |
|
| 178 |
#include "easel.h" |
| 179 |
#include "esl_alloc.h" |
| 180 |
#include "esl_getopts.h" |
| 181 |
#include "esl_random.h" |
| 182 |
|
| 183 |
static ESL_OPTIONS options[] = { |
| 184 |
|
| 185 |
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 }, |
| 186 |
{ "-1", eslARG_NONE,"default",NULL, NULL, "-1,-2,-3,-4", NULL, NULL, "1: malloc (unaligned), realloc (unaligned)", 0 }, |
| 187 |
{ "-2", eslARG_NONE, FALSE, NULL, NULL, "-1,-2,-3,-4", NULL, NULL, "2: malloc (unaligned), free/fresh alloc", 0 }, |
| 188 |
{ "-3", eslARG_NONE, FALSE, NULL, NULL, "-1,-2,-3,-4", NULL, NULL, "3: our aligned malloc, free/fresh alloc", 0 }, |
| 189 |
{ "-4", eslARG_NONE, FALSE, NULL, NULL, "-1,-2,-3,-4", NULL, NULL, "4: posix_memalign, free/fresh alloc", 0 }, |
| 190 |
{ "-s", eslARG_INT, "0", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 }, |
| 191 |
{ "-N", eslARG_INT, "10000", NULL, NULL, NULL, NULL, NULL, "number of iterations", 0 }, |
| 192 |
{ "-L", eslARG_INT, "100", NULL, NULL, NULL, NULL, NULL, "number of rows", 0 }, |
| 193 |
{ "-M", eslARG_INT, "500000", NULL, NULL, NULL, NULL, NULL, "maximum alloc per row (bytes)", 0 }, |
| 194 |
{ "-V", eslARG_INT, "64", NULL, NULL, NULL, NULL, NULL, "memory alignment (bytes)", 0 }, |
| 195 |
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 196 |
}; |
| 197 |
static char usage[] = "[-options]"; |
| 198 |
static char banner[] = "benchmark driver for esl_alloc"; |
| 199 |
|
| 200 |
int |
| 201 |
main(int argc, char **argv) |
| 202 |
{ |
| 203 |
ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 0, argc, argv, banner, usage); |
| 204 |
ESL_RANDOMNESS *rng = esl_randomness_CreateFast(esl_opt_GetInteger(go, "-s")); |
| 205 |
int N = esl_opt_GetInteger(go, "-N"); |
| 206 |
int L = esl_opt_GetInteger(go, "-L"); |
| 207 |
int M = esl_opt_GetInteger(go, "-M"); |
| 208 |
int V = esl_opt_GetInteger(go, "-V"); |
| 209 |
int do_realloc = esl_opt_GetBoolean(go, "-1"); |
| 210 |
int do_fresh_malloc = esl_opt_GetBoolean(go, "-2"); |
| 211 |
int do_esl_alloc = esl_opt_GetBoolean(go, "-3"); |
| 212 |
int do_posix_memalign = esl_opt_GetBoolean(go, "-4"); |
| 213 |
char **p; |
| 214 |
int size; |
| 215 |
int i,j; |
| 216 |
|
| 217 |
#ifndef HAVE_POSIX_MEMALIGN |
| 218 |
if (do_posix_memalign) esl_fatal("can't benchmark posix_memalign(): not available on this system"); |
| 219 |
#endif |
| 220 |
|
| 221 |
p = malloc(sizeof(char *) * L); |
| 222 |
for (i = 0; i < L; i++) |
| 223 |
{ |
| 224 |
size = 1 + esl_rnd_Roll(rng,M); |
| 225 |
if (do_realloc) p[i] = malloc(size); |
| 226 |
else if (do_fresh_malloc) p[i] = malloc(size); |
| 227 |
else if (do_esl_alloc) p[i] = alloc_aligned_fallback(size, V); |
| 228 |
#ifdef HAVE_POSIX_MEMALIGN |
| 229 |
else if (do_posix_memalign) { |
| 230 |
if (posix_memalign((void **) &(p[i]), V, size) != 0) esl_fatal("posix_memalign() failed"); |
| 231 |
} |
| 232 |
#endif |
| 233 |
} |
| 234 |
|
| 235 |
for (j = 0; j < N; j++) |
| 236 |
{ |
| 237 |
for (i = 0; i < L; i++) |
| 238 |
{ |
| 239 |
size = 1 + esl_rnd_Roll(rng,M); |
| 240 |
if (do_realloc) { p[i] = realloc(p[i], size); } |
| 241 |
else if (do_fresh_malloc) { free(p[i]); p[i] = malloc(size); } |
| 242 |
else if (do_esl_alloc) { alloc_aligned_free_fallback(p[i]); p[i] = alloc_aligned_fallback(size, V); } |
| 243 |
#ifdef HAVE_POSIX_MEMALIGN |
| 244 |
else if (do_posix_memalign) { |
| 245 |
free(p[i]); |
| 246 |
if (posix_memalign((void **) &(p[i]), V, size) != 0) esl_fatal("posix_memalign failed"); |
| 247 |
} |
| 248 |
#endif |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
for (i = 0; i < L; i++) |
| 253 |
if (do_esl_alloc) alloc_aligned_free_fallback(p[i]); |
| 254 |
else free(p[i]); |
| 255 |
free(p); |
| 256 |
esl_randomness_Destroy(rng); |
| 257 |
esl_getopts_Destroy(go); |
| 258 |
return 0; |
| 259 |
} |
| 260 |
#endif |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
#ifdef eslALLOC_TESTDRIVE |
| 269 |
#include <limits.h> |
| 270 |
#include "esl_random.h" |
| 271 |
|
| 272 |
|
| 273 |
static void |
| 274 |
utest_fallback(ESL_RANDOMNESS *rng) |
| 275 |
{ |
| 276 |
int N = 100; |
| 277 |
int **p; |
| 278 |
int L; |
| 279 |
int V; |
| 280 |
int i,j; |
| 281 |
|
| 282 |
ESL_DASSERT1(( sizeof(void *) < 256 )); |
| 283 |
|
| 284 |
if ((p = malloc(sizeof(int *) * N)) == NULL) esl_fatal("malloc failed"); |
| 285 |
for (i = 0; i < N; i++) |
| 286 |
{ |
| 287 |
L = esl_rnd_Roll(rng, 1000) + 1; |
| 288 |
do { V = 1 << (4 + esl_rnd_Roll(rng, 5)); } while (V < sizeof(void *)); |
| 289 |
p[i] = alloc_aligned_fallback(sizeof(int) * L, V); |
| 290 |
|
| 291 |
|
| 292 |
for (j = 0; j < L; j++) |
| 293 |
p[i][j] = 1; |
| 294 |
} |
| 295 |
|
| 296 |
for (i = 0; i < N; i++) |
| 297 |
alloc_aligned_free_fallback(p[i]); |
| 298 |
|
| 299 |
free(p); |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
static void |
| 305 |
utest_api(ESL_RANDOMNESS *rng) |
| 306 |
{ |
| 307 |
int N = 100; |
| 308 |
int **p; |
| 309 |
int L; |
| 310 |
int V; |
| 311 |
int i,j; |
| 312 |
|
| 313 |
ESL_DASSERT1(( sizeof(void *) < 256 )); |
| 314 |
|
| 315 |
if ((p = malloc(sizeof(int *) * N)) == NULL) esl_fatal("malloc failed"); |
| 316 |
for (i = 0; i < N; i++) |
| 317 |
{ |
| 318 |
L = esl_rnd_Roll(rng, 1000) + 1; |
| 319 |
do { V = 1 << (4 + esl_rnd_Roll(rng, 5)); } while (V < sizeof(void *)); |
| 320 |
p[i] = esl_alloc_aligned(sizeof(int) * L, V); |
| 321 |
|
| 322 |
for (j = 0; j < L; j++) p[i][j] = 1; |
| 323 |
} |
| 324 |
|
| 325 |
for (i = 0; i < N; i++) |
| 326 |
esl_alloc_free(p[i]); |
| 327 |
free(p); |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
static int |
| 332 |
determine_system_alignment(void) |
| 333 |
{ |
| 334 |
int N = 100; |
| 335 |
char **p; |
| 336 |
int i; |
| 337 |
uintptr_t x; |
| 338 |
int V, Vmin; |
| 339 |
|
| 340 |
if ((p = malloc(sizeof(char *) * N)) == NULL) esl_fatal("malloc failed"); |
| 341 |
for (i = 0; i < N; i++) |
| 342 |
p[i] = malloc(sizeof(char) * 1000); |
| 343 |
|
| 344 |
Vmin = INT_MAX; |
| 345 |
for (i = 0; i < N; i++) |
| 346 |
{ |
| 347 |
x = (uintptr_t) p[i]; |
| 348 |
V = 0; |
| 349 |
while ( !(x & 0x1) ) { x >>= 1; V++; } |
| 350 |
Vmin = ESL_MIN(V, Vmin); |
| 351 |
} |
| 352 |
|
| 353 |
for (i = 0; i < N; i++) |
| 354 |
free(p[i]); |
| 355 |
free(p); |
| 356 |
|
| 357 |
return 1<<Vmin; |
| 358 |
} |
| 359 |
#endif |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
#ifdef eslALLOC_TESTDRIVE |
| 369 |
|
| 370 |
#include "easel.h" |
| 371 |
#include "esl_getopts.h" |
| 372 |
#include "esl_random.h" |
| 373 |
|
| 374 |
static ESL_OPTIONS options[] = { |
| 375 |
|
| 376 |
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 }, |
| 377 |
{ "-s", eslARG_INT, "0", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 }, |
| 378 |
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 379 |
}; |
| 380 |
static char usage[] = "[-options]"; |
| 381 |
static char banner[] = "test driver for esl_alloc"; |
| 382 |
|
| 383 |
int |
| 384 |
main(int argc, char **argv) |
| 385 |
{ |
| 386 |
ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 0, argc, argv, banner, usage); |
| 387 |
ESL_RANDOMNESS *rng = esl_randomness_CreateFast(esl_opt_GetInteger(go, "-s")); |
| 388 |
|
| 389 |
fprintf(stderr, "## %s\n", argv[0]); |
| 390 |
fprintf(stderr, "# rng seed: %" PRIu32 "\n", esl_randomness_GetSeed(rng)); |
| 391 |
fprintf(stderr, "# sizeof(void*): %d\n", (int) sizeof(void *)); |
| 392 |
fprintf(stderr, "# malloc() aligns on: %d bytes\n", determine_system_alignment()); |
| 393 |
|
| 394 |
#ifdef HAVE_POSIX_MEMALIGN |
| 395 |
fprintf(stderr, "# esl_alloc is using: posix_memalign()\n"); |
| 396 |
#elif HAVE_ALIGNED_ALLOC |
| 397 |
fprintf(stderr, "# esl_alloc is using: aligned_alloc()\n"); |
| 398 |
#elif HAVE__MM_MALLOC |
| 399 |
fprintf(stderr, "# esl_alloc is using: _mm_malloc()\n"); |
| 400 |
#else |
| 401 |
fprintf(stderr, "# esl_alloc is using: alloc_aligned_fallback()\n"); |
| 402 |
#endif |
| 403 |
|
| 404 |
utest_fallback(rng); |
| 405 |
utest_api(rng); |
| 406 |
|
| 407 |
fprintf(stderr, "# status = ok\n"); |
| 408 |
esl_randomness_Destroy(rng); |
| 409 |
esl_getopts_Destroy(go); |
| 410 |
return eslOK; |
| 411 |
} |
| 412 |
#endif |