apply clang-format
This commit is contained in:
@ -1,4 +1,3 @@
|
||||
// clang-format off
|
||||
/* -*- c++ -*- ----------------------------------------------------------
|
||||
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
|
||||
https://www.lammps.org/, Sandia National Laboratories
|
||||
@ -14,8 +13,6 @@
|
||||
|
||||
#include "my_pool_chunk.h"
|
||||
|
||||
#include <cstdlib>
|
||||
|
||||
#if defined(LMP_INTEL) && !defined(LAMMPS_MEMALIGN) && !defined(_WIN32)
|
||||
#define LAMMPS_MEMALIGN 64
|
||||
#endif
|
||||
@ -50,7 +47,8 @@ using namespace LAMMPS_NS;
|
||||
|
||||
template <class T>
|
||||
MyPoolChunk<T>::MyPoolChunk(int user_minchunk, int user_maxchunk, int user_nbin,
|
||||
int user_chunkperpage, int user_pagedelta) {
|
||||
int user_chunkperpage, int user_pagedelta)
|
||||
{
|
||||
minchunk = user_minchunk;
|
||||
maxchunk = user_maxchunk;
|
||||
nbin = user_nbin;
|
||||
@ -68,13 +66,13 @@ MyPoolChunk<T>::MyPoolChunk(int user_minchunk, int user_maxchunk, int user_nbin,
|
||||
|
||||
// insure nbin*binsize spans minchunk to maxchunk inclusive
|
||||
|
||||
binsize = (maxchunk-minchunk+1) / nbin;
|
||||
if (minchunk + nbin*binsize <= maxchunk) binsize++;
|
||||
binsize = (maxchunk - minchunk + 1) / nbin;
|
||||
if (minchunk + nbin * binsize <= maxchunk) binsize++;
|
||||
|
||||
freelist = nullptr;
|
||||
for (int ibin = 0; ibin < nbin; ibin++) {
|
||||
freehead[ibin] = -1;
|
||||
chunksize[ibin] = minchunk + (ibin+1)*binsize - 1;
|
||||
chunksize[ibin] = minchunk + (ibin + 1) * binsize - 1;
|
||||
if (chunksize[ibin] > maxchunk) chunksize[ibin] = maxchunk;
|
||||
}
|
||||
|
||||
@ -85,10 +83,10 @@ MyPoolChunk<T>::MyPoolChunk(int user_minchunk, int user_maxchunk, int user_nbin,
|
||||
}
|
||||
|
||||
/** Destroy class instance and free all allocated memory */
|
||||
template <class T>
|
||||
MyPoolChunk<T>::~MyPoolChunk() {
|
||||
delete [] freehead;
|
||||
delete [] chunksize;
|
||||
template <class T> MyPoolChunk<T>::~MyPoolChunk()
|
||||
{
|
||||
delete[] freehead;
|
||||
delete[] chunksize;
|
||||
if (npage) {
|
||||
free(freelist);
|
||||
for (int i = 0; i < npage; i++) free(pages[i]);
|
||||
@ -102,9 +100,9 @@ MyPoolChunk<T>::~MyPoolChunk() {
|
||||
* \param index Index of chunk in memory pool
|
||||
* \return Pointer to requested chunk of storage */
|
||||
|
||||
template <class T>
|
||||
T *MyPoolChunk<T>::get(int &index) {
|
||||
int ibin = nbin-1;
|
||||
template <class T> T *MyPoolChunk<T>::get(int &index)
|
||||
{
|
||||
int ibin = nbin - 1;
|
||||
if (freehead[ibin] < 0) {
|
||||
allocate(ibin);
|
||||
if (errorflag) {
|
||||
@ -116,10 +114,10 @@ T *MyPoolChunk<T>::get(int &index) {
|
||||
ndatum += maxchunk;
|
||||
nchunk++;
|
||||
index = freehead[ibin];
|
||||
int ipage = index/chunkperpage;
|
||||
int ipage = index / chunkperpage;
|
||||
int ientry = index % chunkperpage;
|
||||
freehead[ibin] = freelist[index];
|
||||
return &pages[ipage][ientry*chunksize[ibin]];
|
||||
return &pages[ipage][ientry * chunksize[ibin]];
|
||||
}
|
||||
|
||||
/** Return pointer/index of unused chunk of size N
|
||||
@ -128,15 +126,15 @@ T *MyPoolChunk<T>::get(int &index) {
|
||||
* \param index Index of chunk in memory pool
|
||||
* \return Pointer to requested chunk of storage */
|
||||
|
||||
template <class T>
|
||||
T *MyPoolChunk<T>::get(int n, int &index) {
|
||||
template <class T> T *MyPoolChunk<T>::get(int n, int &index)
|
||||
{
|
||||
if (n < minchunk || n > maxchunk) {
|
||||
errorflag = 3;
|
||||
index = -1;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int ibin = (n-minchunk) / binsize;
|
||||
int ibin = (n - minchunk) / binsize;
|
||||
if (freehead[ibin] < 0) {
|
||||
allocate(ibin);
|
||||
if (errorflag) {
|
||||
@ -148,35 +146,34 @@ T *MyPoolChunk<T>::get(int n, int &index) {
|
||||
ndatum += n;
|
||||
nchunk++;
|
||||
index = freehead[ibin];
|
||||
int ipage = index/chunkperpage;
|
||||
int ipage = index / chunkperpage;
|
||||
int ientry = index % chunkperpage;
|
||||
freehead[ibin] = freelist[index];
|
||||
return &pages[ipage][ientry*chunksize[ibin]];
|
||||
return &pages[ipage][ientry * chunksize[ibin]];
|
||||
}
|
||||
|
||||
/** Put indexed chunk back into memory pool via free list
|
||||
*
|
||||
* \param index Memory chunk index returned by call to get() */
|
||||
|
||||
template <class T>
|
||||
void MyPoolChunk<T>::put(int index) {
|
||||
if (index < 0) return;
|
||||
int ipage = index/chunkperpage;
|
||||
int ibin = whichbin[ipage];
|
||||
nchunk--;
|
||||
ndatum -= chunksize[ibin];
|
||||
freelist[index] = freehead[ibin];
|
||||
freehead[ibin] = index;
|
||||
}
|
||||
template <class T> void MyPoolChunk<T>::put(int index)
|
||||
{
|
||||
if (index < 0) return;
|
||||
int ipage = index / chunkperpage;
|
||||
int ibin = whichbin[ipage];
|
||||
nchunk--;
|
||||
ndatum -= chunksize[ibin];
|
||||
freelist[index] = freehead[ibin];
|
||||
freehead[ibin] = index;
|
||||
}
|
||||
|
||||
|
||||
template <class T>
|
||||
void MyPoolChunk<T>::allocate(int ibin) {
|
||||
template <class T> void MyPoolChunk<T>::allocate(int ibin)
|
||||
{
|
||||
int oldpage = npage;
|
||||
npage += pagedelta;
|
||||
freelist = (int *) realloc(freelist,sizeof(int)*npage*chunkperpage);
|
||||
pages = (T **) realloc(pages,sizeof(T *)*npage);
|
||||
whichbin = (int *) realloc(whichbin,sizeof(int)*npage);
|
||||
freelist = (int *) realloc(freelist, sizeof(int) * npage * chunkperpage);
|
||||
pages = (T **) realloc(pages, sizeof(T *) * npage);
|
||||
whichbin = (int *) realloc(whichbin, sizeof(int) * npage);
|
||||
if (!freelist || !pages) {
|
||||
errorflag = 2;
|
||||
return;
|
||||
@ -188,34 +185,32 @@ void MyPoolChunk<T>::allocate(int ibin) {
|
||||
whichbin[i] = ibin;
|
||||
#if defined(LAMMPS_MEMALIGN)
|
||||
void *ptr;
|
||||
if (posix_memalign(&ptr, LAMMPS_MEMALIGN,
|
||||
sizeof(T)*chunkperpage*chunksize[ibin]))
|
||||
if (posix_memalign(&ptr, LAMMPS_MEMALIGN, sizeof(T) * chunkperpage * chunksize[ibin]))
|
||||
errorflag = 2;
|
||||
pages[i] = (T *) ptr;
|
||||
#else
|
||||
pages[i] = (T *) malloc(sizeof(T)*chunkperpage*chunksize[ibin]);
|
||||
pages[i] = (T *) malloc(sizeof(T) * chunkperpage * chunksize[ibin]);
|
||||
if (!pages[i]) errorflag = 2;
|
||||
#endif
|
||||
}
|
||||
|
||||
// reset free list for unused chunks on new pages
|
||||
|
||||
freehead[ibin] = oldpage*chunkperpage;
|
||||
for (int i = freehead[ibin]; i < npage*chunkperpage; i++) freelist[i] = i+1;
|
||||
freelist[npage*chunkperpage-1] = -1;
|
||||
freehead[ibin] = oldpage * chunkperpage;
|
||||
for (int i = freehead[ibin]; i < npage * chunkperpage; i++) freelist[i] = i + 1;
|
||||
freelist[npage * chunkperpage - 1] = -1;
|
||||
}
|
||||
|
||||
/** Return total size of allocated pages
|
||||
*
|
||||
* \return total storage used in bytes */
|
||||
|
||||
template <class T>
|
||||
double MyPoolChunk<T>::size() const {
|
||||
double bytes = (double)npage*chunkperpage*sizeof(int);
|
||||
bytes += (double)npage*sizeof(T *);
|
||||
bytes += (double)npage*sizeof(int);
|
||||
for (int i=0; i < npage; ++i)
|
||||
bytes += (double)chunkperpage*chunksize[i]*sizeof(T);
|
||||
template <class T> double MyPoolChunk<T>::size() const
|
||||
{
|
||||
double bytes = (double) npage * chunkperpage * sizeof(int);
|
||||
bytes += (double) npage * sizeof(T *);
|
||||
bytes += (double) npage * sizeof(int);
|
||||
for (int i = 0; i < npage; ++i) bytes += (double) chunkperpage * chunksize[i] * sizeof(T);
|
||||
|
||||
return bytes;
|
||||
}
|
||||
@ -223,6 +218,6 @@ double MyPoolChunk<T>::size() const {
|
||||
// explicit instantiations
|
||||
|
||||
namespace LAMMPS_NS {
|
||||
template class MyPoolChunk<int>;
|
||||
template class MyPoolChunk<double>;
|
||||
}
|
||||
template class MyPoolChunk<int>;
|
||||
template class MyPoolChunk<double>;
|
||||
} // namespace LAMMPS_NS
|
||||
|
||||
Reference in New Issue
Block a user