2006-09-28 03:51:33 +08:00
|
|
|
/* ----------------------------------------------------------------------
|
|
|
|
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
|
2007-01-30 08:22:05 +08:00
|
|
|
http://lammps.sandia.gov, Sandia National Laboratories
|
|
|
|
Steve Plimpton, sjplimp@sandia.gov
|
2006-09-28 03:51:33 +08:00
|
|
|
|
|
|
|
Copyright (2003) Sandia Corporation. Under the terms of Contract
|
|
|
|
DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
|
2012-06-07 06:47:51 +08:00
|
|
|
certain rights in this software. This software is distributed under
|
2006-09-28 03:51:33 +08:00
|
|
|
the GNU General Public License.
|
|
|
|
|
|
|
|
See the README file in the top-level LAMMPS directory.
|
|
|
|
------------------------------------------------------------------------- */
|
|
|
|
|
2015-10-31 04:04:06 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2006-09-28 03:51:33 +08:00
|
|
|
#include "memory.h"
|
|
|
|
#include "error.h"
|
|
|
|
|
2016-05-13 23:46:48 +08:00
|
|
|
#if defined(LMP_USER_INTEL) && defined(__INTEL_COMPILER)
|
2016-12-14 08:14:28 +08:00
|
|
|
#ifndef LMP_INTEL_NO_TBB
|
2016-05-13 23:46:48 +08:00
|
|
|
#define LMP_USE_TBB_ALLOCATOR
|
|
|
|
#include "tbb/scalable_allocator.h"
|
2016-12-14 08:14:28 +08:00
|
|
|
#else
|
|
|
|
#include <malloc.h>
|
2016-05-13 23:46:48 +08:00
|
|
|
#endif
|
2016-11-10 05:53:39 +08:00
|
|
|
#endif
|
|
|
|
|
2016-12-15 04:24:55 +08:00
|
|
|
#if defined(LMP_USER_INTEL) && !defined(LAMMPS_MEMALIGN)
|
|
|
|
#define LAMMPS_MEMALIGN 64
|
|
|
|
#endif
|
|
|
|
|
2007-01-30 08:22:05 +08:00
|
|
|
using namespace LAMMPS_NS;
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------- */
|
|
|
|
|
2011-01-05 08:29:35 +08:00
|
|
|
Memory::Memory(LAMMPS *lmp) : Pointers(lmp) {}
|
2007-01-30 08:22:05 +08:00
|
|
|
|
2006-09-28 03:51:33 +08:00
|
|
|
/* ----------------------------------------------------------------------
|
2012-06-07 06:47:51 +08:00
|
|
|
safe malloc
|
2006-09-28 03:51:33 +08:00
|
|
|
------------------------------------------------------------------------- */
|
|
|
|
|
2011-03-18 00:06:28 +08:00
|
|
|
void *Memory::smalloc(bigint nbytes, const char *name)
|
2006-09-28 03:51:33 +08:00
|
|
|
{
|
2011-03-18 00:06:28 +08:00
|
|
|
if (nbytes == 0) return NULL;
|
|
|
|
|
2011-12-02 01:21:19 +08:00
|
|
|
#if defined(LAMMPS_MEMALIGN)
|
|
|
|
void *ptr;
|
2016-05-13 23:46:48 +08:00
|
|
|
|
|
|
|
#if defined(LMP_USE_TBB_ALLOCATOR)
|
|
|
|
ptr = scalable_aligned_malloc(nbytes, LAMMPS_MEMALIGN);
|
|
|
|
#else
|
2012-02-02 00:07:44 +08:00
|
|
|
int retval = posix_memalign(&ptr, LAMMPS_MEMALIGN, nbytes);
|
|
|
|
if (retval) ptr = NULL;
|
2016-05-13 23:46:48 +08:00
|
|
|
#endif
|
|
|
|
|
2011-12-02 01:21:19 +08:00
|
|
|
#else
|
2011-03-18 00:06:28 +08:00
|
|
|
void *ptr = malloc(nbytes);
|
2011-12-02 01:21:19 +08:00
|
|
|
#endif
|
2006-09-28 03:51:33 +08:00
|
|
|
if (ptr == NULL) {
|
|
|
|
char str[128];
|
2011-07-20 02:31:10 +08:00
|
|
|
sprintf(str,"Failed to allocate " BIGINT_FORMAT " bytes for array %s",
|
2012-06-07 06:47:51 +08:00
|
|
|
nbytes,name);
|
2011-09-24 02:06:55 +08:00
|
|
|
error->one(FLERR,str);
|
2006-09-28 03:51:33 +08:00
|
|
|
}
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------
|
2012-06-07 06:47:51 +08:00
|
|
|
safe realloc
|
2006-09-28 03:51:33 +08:00
|
|
|
------------------------------------------------------------------------- */
|
|
|
|
|
2011-03-18 00:06:28 +08:00
|
|
|
void *Memory::srealloc(void *ptr, bigint nbytes, const char *name)
|
2006-09-28 03:51:33 +08:00
|
|
|
{
|
2011-03-18 00:06:28 +08:00
|
|
|
if (nbytes == 0) {
|
2011-03-26 08:50:29 +08:00
|
|
|
destroy(ptr);
|
2009-08-07 06:21:38 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-05-13 23:46:48 +08:00
|
|
|
#if defined(LMP_USE_TBB_ALLOCATOR)
|
|
|
|
ptr = scalable_aligned_realloc(ptr, nbytes, LAMMPS_MEMALIGN);
|
2016-12-14 08:14:28 +08:00
|
|
|
#elif defined(LMP_INTEL_NO_TBB) && defined(LAMMPS_MEMALIGN)
|
|
|
|
ptr = realloc(ptr, nbytes);
|
|
|
|
uintptr_t offset = ((uintptr_t)(const void *)(ptr)) % LAMMPS_MEMALIGN;
|
|
|
|
if (offset) {
|
|
|
|
void *optr = ptr;
|
|
|
|
ptr = smalloc(nbytes, name);
|
|
|
|
memcpy(ptr, optr, MIN(nbytes,malloc_usable_size(optr)));
|
|
|
|
free(optr);
|
|
|
|
}
|
2016-05-13 23:46:48 +08:00
|
|
|
#else
|
2011-03-18 00:06:28 +08:00
|
|
|
ptr = realloc(ptr,nbytes);
|
2016-05-13 23:46:48 +08:00
|
|
|
#endif
|
2006-09-28 03:51:33 +08:00
|
|
|
if (ptr == NULL) {
|
|
|
|
char str[128];
|
2011-07-20 02:31:10 +08:00
|
|
|
sprintf(str,"Failed to reallocate " BIGINT_FORMAT " bytes for array %s",
|
2012-06-07 06:47:51 +08:00
|
|
|
nbytes,name);
|
2011-09-24 02:06:55 +08:00
|
|
|
error->one(FLERR,str);
|
2006-09-28 03:51:33 +08:00
|
|
|
}
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2011-03-16 23:21:59 +08:00
|
|
|
/* ----------------------------------------------------------------------
|
2012-06-07 06:47:51 +08:00
|
|
|
safe free
|
2011-03-16 23:21:59 +08:00
|
|
|
------------------------------------------------------------------------- */
|
|
|
|
|
2011-03-18 00:06:28 +08:00
|
|
|
void Memory::sfree(void *ptr)
|
2011-03-16 23:21:59 +08:00
|
|
|
{
|
2011-03-18 00:06:28 +08:00
|
|
|
if (ptr == NULL) return;
|
2016-05-13 23:46:48 +08:00
|
|
|
#if defined(LMP_USE_TBB_ALLOCATOR)
|
|
|
|
scalable_aligned_free(ptr);
|
|
|
|
#else
|
2011-03-18 00:06:28 +08:00
|
|
|
free(ptr);
|
2016-05-13 23:46:48 +08:00
|
|
|
#endif
|
2011-03-16 23:21:59 +08:00
|
|
|
}
|
|
|
|
|
2011-03-18 00:06:28 +08:00
|
|
|
/* ----------------------------------------------------------------------
|
|
|
|
erroneous usage of templated create/grow functions
|
|
|
|
------------------------------------------------------------------------- */
|
2011-03-16 23:21:59 +08:00
|
|
|
|
2011-03-18 00:06:28 +08:00
|
|
|
void Memory::fail(const char *name)
|
2011-03-16 23:21:59 +08:00
|
|
|
{
|
2011-03-18 00:06:28 +08:00
|
|
|
char str[128];
|
|
|
|
sprintf(str,"Cannot create/grow a vector/array of pointers for %s",name);
|
2011-09-24 02:06:55 +08:00
|
|
|
error->one(FLERR,str);
|
2011-03-16 23:21:59 +08:00
|
|
|
}
|