ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
neighbor_list.h
Go to the documentation of this file.
1#pragma once
2
3#include <vector>
4#include<iostream>
5#include <cmath>
6#include <cassert>
7
9public:
10 struct Page {
11 std::vector<int> data;
13 int offset;
14 };
15
16 std::vector<Page> pages;
17 int pgsize;
18
19 static constexpr int DEFAULT_PGSIZE = 1024;
20
21 // Default constructor
25
26 PageAllocator(int pgsize_) : pgsize(pgsize_) {
27 new_page();
28 }
29
31 // no manual delete[]; vectors clean themselves
32 }
33
34 PageAllocator(const PageAllocator&) = delete;
36
37 // Allow move
40
41 void new_page() {
42 Page p;
43 p.capacity = pgsize;
44 p.offset = 0;
45 p.data.resize(pgsize);
46 pages.push_back(std::move(p));
47 }
48
49 int* allocate(int n) {
50 if (n <= 0) return nullptr;
51 // reject requests larger than a single page
52 if (n > pgsize) {
53 std::cerr << "PageAllocator::allocate error: request " << n << " larger than page size " << pgsize << std::endl;
54 return nullptr;
55 }
56 if (pages.empty()) new_page();
57 Page& p = pages.back();
58 if (p.offset + n > p.capacity) {
59 new_page();
60 return allocate(n);
61 }
62 int* ptr = p.data.data() + p.offset;
63 p.offset += n;
64 return ptr;
65 }
66
67 void reset() {
68 pages.resize(1);
69 pages[0].offset = 0;
70 }
71};
72
74// Neighbor List
76
78public:
79 NeighborList() = default;
80 ~NeighborList() = default;
81
82 int nlocal;
83
84 std::vector<int> numneigh;
85 std::vector<int*> firstneigh;
86
88
89 void initialize(int n, int pgsize) {
90 nlocal = n;
91 allocator = PageAllocator(pgsize);
92 // ensure neighbor containers are sized and initialized
93 numneigh.assign(n, 0);
94 firstneigh.assign(n, nullptr);
95 }
96
97 void reset() {
99 }
100};
Definition neighbor_list.h:77
int nlocal
Definition neighbor_list.h:82
std::vector< int > numneigh
Definition neighbor_list.h:84
std::vector< int * > firstneigh
Definition neighbor_list.h:85
void reset()
Definition neighbor_list.h:97
NeighborList()=default
PageAllocator allocator
Definition neighbor_list.h:87
~NeighborList()=default
void initialize(int n, int pgsize)
Definition neighbor_list.h:89
Definition neighbor_list.h:8
~PageAllocator()
Definition neighbor_list.h:30
PageAllocator(int pgsize_)
Definition neighbor_list.h:26
int * allocate(int n)
Definition neighbor_list.h:49
static constexpr int DEFAULT_PGSIZE
Definition neighbor_list.h:19
std::vector< Page > pages
Definition neighbor_list.h:16
void reset()
Definition neighbor_list.h:67
void new_page()
Definition neighbor_list.h:41
PageAllocator & operator=(PageAllocator &&)=default
PageAllocator()
Definition neighbor_list.h:22
PageAllocator & operator=(const PageAllocator &)=delete
PageAllocator(const PageAllocator &)=delete
PageAllocator(PageAllocator &&)=default
int pgsize
Definition neighbor_list.h:17
Definition neighbor_list.h:10
int capacity
Definition neighbor_list.h:12
std::vector< int > data
Definition neighbor_list.h:11
int offset
Definition neighbor_list.h:13