apply clang-format

This commit is contained in:
Axel Kohlmeyer
2021-09-07 14:20:26 -04:00
parent 9bc8e0998e
commit 9c50420c14
2 changed files with 87 additions and 98 deletions

View File

@ -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_page.h"
#include <cstdlib>
#if defined(LMP_INTEL) && !defined(LAMMPS_MEMALIGN) && !defined(_WIN32)
#define LAMMPS_MEMALIGN 64
#endif
@ -60,12 +57,12 @@ using namespace LAMMPS_NS;
* Need to call init() before use to define allocation settings */
template <class T>
MyPage<T>::MyPage() : ndatum(0), nchunk(0), pages(nullptr), page(nullptr),
npage(0), ipage(-1), index(-1), maxchunk(-1),
pagesize(-1), pagedelta(1), errorflag(0) {};
MyPage<T>::MyPage() :
ndatum(0), nchunk(0), pages(nullptr), page(nullptr), npage(0), ipage(-1), index(-1),
maxchunk(-1), pagesize(-1), pagedelta(1), errorflag(0){};
template <class T>
MyPage<T>::~MyPage() {
template <class T> MyPage<T>::~MyPage()
{
deallocate();
}
@ -79,27 +76,26 @@ MyPage<T>::~MyPage() {
* \param user_pagedelta Number of pages to allocate with one malloc
* \return 1 if there were invalid parameters, 2 if there was an allocation error or 0 if successful */
template<class T>
int MyPage<T>::init(int user_maxchunk, int user_pagesize,
int user_pagedelta) {
maxchunk = user_maxchunk;
pagesize = user_pagesize;
pagedelta = user_pagedelta;
template <class T> int MyPage<T>::init(int user_maxchunk, int user_pagesize, int user_pagedelta)
{
maxchunk = user_maxchunk;
pagesize = user_pagesize;
pagedelta = user_pagedelta;
if (maxchunk <= 0 || pagesize <= 0 || pagedelta <= 0) return 1;
if (maxchunk > pagesize) return 1;
if (maxchunk <= 0 || pagesize <= 0 || pagedelta <= 0) return 1;
if (maxchunk > pagesize) return 1;
// free storage if re-initialized
// free storage if re-initialized
deallocate();
deallocate();
// initial page allocation
// initial page allocation
allocate();
if (errorflag) return 2;
reset();
return 0;
}
allocate();
if (errorflag) return 2;
reset();
return 0;
}
/** Pointer to location that can store N items.
*
@ -110,8 +106,8 @@ int MyPage<T>::init(int user_maxchunk, int user_pagesize,
* \param n number of items for which storage is requested
* \return memory location or null pointer, if error or allocation failed */
template <class T>
T *MyPage<T>::get(int n) {
template <class T> T *MyPage<T>::get(int n)
{
if (n > maxchunk) {
errorflag = 1;
return nullptr;
@ -120,7 +116,7 @@ T *MyPage<T>::get(int n) {
nchunk++;
// return pointer from current page
if (index+n <= pagesize) {
if (index + n <= pagesize) {
int start = index;
index += n;
return &page[start];
@ -137,11 +133,10 @@ T *MyPage<T>::get(int n) {
return &page[0];
}
/** Reset state of memory pool without freeing any memory */
template <class T>
void MyPage<T>::reset() {
template <class T> void MyPage<T>::reset()
{
ndatum = nchunk = 0;
index = ipage = 0;
page = (pages != nullptr) ? pages[ipage] : nullptr;
@ -150,23 +145,22 @@ void MyPage<T>::reset() {
/* ---------------------------------------------------------------------- */
template <class T>
void MyPage<T>::allocate() {
template <class T> void MyPage<T>::allocate()
{
npage += pagedelta;
pages = (T **) realloc(pages,npage*sizeof(T *));
pages = (T **) realloc(pages, npage * sizeof(T *));
if (!pages) {
errorflag = 2;
return;
}
for (int i = npage-pagedelta; i < npage; i++) {
for (int i = npage - pagedelta; i < npage; i++) {
#if defined(LAMMPS_MEMALIGN)
void *ptr;
if (posix_memalign(&ptr, LAMMPS_MEMALIGN, pagesize*sizeof(T)))
errorflag = 2;
if (posix_memalign(&ptr, LAMMPS_MEMALIGN, pagesize * sizeof(T))) errorflag = 2;
pages[i] = (T *) ptr;
#else
pages[i] = (T *) malloc(pagesize*sizeof(T));
pages[i] = (T *) malloc(pagesize * sizeof(T));
if (!pages[i]) errorflag = 2;
#endif
}
@ -174,8 +168,8 @@ void MyPage<T>::allocate() {
/** Free all allocated pages of this class instance */
template <class T>
void MyPage<T>::deallocate() {
template <class T> void MyPage<T>::deallocate()
{
reset();
for (int i = 0; i < npage; i++) free(pages[i]);
free(pages);
@ -186,9 +180,9 @@ void MyPage<T>::deallocate() {
// explicit instantiations
namespace LAMMPS_NS {
template class MyPage<int>;
template class MyPage<long>;
template class MyPage<long long>;
template class MyPage<double>;
template class MyPage<HyperOneCoeff>;
}
template class MyPage<int>;
template class MyPage<long>;
template class MyPage<long long>;
template class MyPage<double>;
template class MyPage<HyperOneCoeff>;
} // namespace LAMMPS_NS

View File

@ -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