diff --git a/src/my_page.cpp b/src/my_page.cpp index efad82d3d6..b407e279a9 100644 --- a/src/my_page.cpp +++ b/src/my_page.cpp @@ -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 - #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 -MyPage::MyPage() : ndatum(0), nchunk(0), pages(nullptr), page(nullptr), - npage(0), ipage(-1), index(-1), maxchunk(-1), - pagesize(-1), pagedelta(1), errorflag(0) {}; +MyPage::MyPage() : + ndatum(0), nchunk(0), pages(nullptr), page(nullptr), npage(0), ipage(-1), index(-1), + maxchunk(-1), pagesize(-1), pagedelta(1), errorflag(0){}; -template -MyPage::~MyPage() { +template MyPage::~MyPage() +{ deallocate(); } @@ -79,27 +76,26 @@ MyPage::~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 -int MyPage::init(int user_maxchunk, int user_pagesize, - int user_pagedelta) { - maxchunk = user_maxchunk; - pagesize = user_pagesize; - pagedelta = user_pagedelta; +template int MyPage::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::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 -T *MyPage::get(int n) { +template T *MyPage::get(int n) +{ if (n > maxchunk) { errorflag = 1; return nullptr; @@ -120,7 +116,7 @@ T *MyPage::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::get(int n) { return &page[0]; } - /** Reset state of memory pool without freeing any memory */ -template -void MyPage::reset() { +template void MyPage::reset() +{ ndatum = nchunk = 0; index = ipage = 0; page = (pages != nullptr) ? pages[ipage] : nullptr; @@ -150,23 +145,22 @@ void MyPage::reset() { /* ---------------------------------------------------------------------- */ -template -void MyPage::allocate() { +template void MyPage::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::allocate() { /** Free all allocated pages of this class instance */ -template -void MyPage::deallocate() { +template void MyPage::deallocate() +{ reset(); for (int i = 0; i < npage; i++) free(pages[i]); free(pages); @@ -186,9 +180,9 @@ void MyPage::deallocate() { // explicit instantiations namespace LAMMPS_NS { - template class MyPage; - template class MyPage; - template class MyPage; - template class MyPage; - template class MyPage; -} +template class MyPage; +template class MyPage; +template class MyPage; +template class MyPage; +template class MyPage; +} // namespace LAMMPS_NS diff --git a/src/my_pool_chunk.cpp b/src/my_pool_chunk.cpp index 484a4b3fcf..1aa63b48af 100644 --- a/src/my_pool_chunk.cpp +++ b/src/my_pool_chunk.cpp @@ -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 - #if defined(LMP_INTEL) && !defined(LAMMPS_MEMALIGN) && !defined(_WIN32) #define LAMMPS_MEMALIGN 64 #endif @@ -50,7 +47,8 @@ using namespace LAMMPS_NS; template MyPoolChunk::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::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::MyPoolChunk(int user_minchunk, int user_maxchunk, int user_nbin, } /** Destroy class instance and free all allocated memory */ -template -MyPoolChunk::~MyPoolChunk() { - delete [] freehead; - delete [] chunksize; +template MyPoolChunk::~MyPoolChunk() +{ + delete[] freehead; + delete[] chunksize; if (npage) { free(freelist); for (int i = 0; i < npage; i++) free(pages[i]); @@ -102,9 +100,9 @@ MyPoolChunk::~MyPoolChunk() { * \param index Index of chunk in memory pool * \return Pointer to requested chunk of storage */ -template -T *MyPoolChunk::get(int &index) { - int ibin = nbin-1; +template T *MyPoolChunk::get(int &index) +{ + int ibin = nbin - 1; if (freehead[ibin] < 0) { allocate(ibin); if (errorflag) { @@ -116,10 +114,10 @@ T *MyPoolChunk::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::get(int &index) { * \param index Index of chunk in memory pool * \return Pointer to requested chunk of storage */ -template -T *MyPoolChunk::get(int n, int &index) { +template T *MyPoolChunk::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::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 -void MyPoolChunk::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 void MyPoolChunk::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 -void MyPoolChunk::allocate(int ibin) { +template void MyPoolChunk::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::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 -double MyPoolChunk::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 double MyPoolChunk::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::size() const { // explicit instantiations namespace LAMMPS_NS { - template class MyPoolChunk; - template class MyPoolChunk; -} +template class MyPoolChunk; +template class MyPoolChunk; +} // namespace LAMMPS_NS