|
| 1 | +/* |
| 2 | + * Copyright (c) "Neo4j" |
| 3 | + * Neo4j Sweden AB [http://neo4j.com] |
| 4 | + * |
| 5 | + * This file is part of Neo4j. |
| 6 | + * |
| 7 | + * Neo4j is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | +package org.neo4j.gds.core.utils.paged; |
| 21 | + |
| 22 | +import org.neo4j.gds.mem.BitUtil; |
| 23 | +import org.neo4j.gds.mem.HugeArrays; |
| 24 | + |
| 25 | +import java.util.concurrent.atomic.AtomicLongArray; |
| 26 | +import java.util.concurrent.atomic.AtomicReference; |
| 27 | + |
| 28 | +public final class HugeAtomicPagedBitSet { |
| 29 | + |
| 30 | + // Each page stores 2^PAGE_SHIFT_BITS entries. |
| 31 | + // Word-size is 64 bit (long), which means we |
| 32 | + // store 2^(PAGE_SHIFT_BITS - 6) words per page. |
| 33 | + static final int PAGE_SHIFT_BITS = 14; |
| 34 | + // Number of bits per word (long). |
| 35 | + private static final int NUM_BITS = Long.SIZE; |
| 36 | + private static final int BIT_MASK = NUM_BITS - 1; |
| 37 | + |
| 38 | + private final int pageSize; // words per page |
| 39 | + private final int pageShift; // word-aligned page shift |
| 40 | + private final long pageMask; // word-aligned page mask |
| 41 | + |
| 42 | + // We need to atomically update the reference to the |
| 43 | + // actual pages since multiple threads try to add a |
| 44 | + // new page at the same time and only once must succeed. |
| 45 | + private final AtomicReference<Pages> pages; |
| 46 | + |
| 47 | + public static HugeAtomicPagedBitSet create(long bitSize) { |
| 48 | + // Number of words required to represent the bit size. |
| 49 | + long wordSize = BitUtil.ceilDiv(bitSize, NUM_BITS); |
| 50 | + |
| 51 | + // Parameters for long pages representing the bits. |
| 52 | + int pageShift = PAGE_SHIFT_BITS - 6; // 2^6 == 64 Bits for a long |
| 53 | + int pageSize = 1 << pageShift; |
| 54 | + long pageMask = pageSize - 1; |
| 55 | + // We allocate in pages of fixed size, so the last page |
| 56 | + // might have extra space, which is fine as this is a |
| 57 | + // growing data structure anyway. The capacity will be |
| 58 | + // larger than the specified size. |
| 59 | + int pageCount = HugeArrays.numberOfPages(wordSize, pageShift, pageMask); |
| 60 | + |
| 61 | + return new HugeAtomicPagedBitSet(pageCount, pageSize, pageShift, pageMask); |
| 62 | + } |
| 63 | + |
| 64 | + private HugeAtomicPagedBitSet(int pageCount, int pageSize, int pageShift, long pageMask) { |
| 65 | + this.pageSize = pageSize; |
| 66 | + this.pageShift = pageShift; |
| 67 | + this.pageMask = pageMask; |
| 68 | + this.pages = new AtomicReference<>(new Pages(pageCount, pageSize)); |
| 69 | + } |
| 70 | + |
| 71 | + public void set(long index) { |
| 72 | + long longIndex = index >>> 6; |
| 73 | + int pageIndex = HugeArrays.pageIndex(longIndex, pageShift); |
| 74 | + int wordIndex = HugeArrays.indexInPage(longIndex, pageMask); |
| 75 | + int bitIndex = (int) (index & BIT_MASK); |
| 76 | + |
| 77 | + var page = getPage(pageIndex); |
| 78 | + long bitMask = 1L << bitIndex; |
| 79 | + |
| 80 | + long oldWord = page.get(wordIndex); |
| 81 | + while (true) { |
| 82 | + long newWord = oldWord | bitMask; |
| 83 | + if (newWord == oldWord) { |
| 84 | + // nothing to set |
| 85 | + return; |
| 86 | + } |
| 87 | + long currentWord = page.compareAndExchange(wordIndex, oldWord, newWord); |
| 88 | + if (currentWord == oldWord) { |
| 89 | + // CAS successful |
| 90 | + return; |
| 91 | + } |
| 92 | + // CAS unsuccessful, try again |
| 93 | + oldWord = currentWord; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + public boolean get(long index) { |
| 98 | + long longIndex = index >>> 6; |
| 99 | + int pageIndex = HugeArrays.pageIndex(longIndex, pageShift); |
| 100 | + int wordIndex = HugeArrays.indexInPage(longIndex, pageMask); |
| 101 | + int bitIndex = (int) (index & BIT_MASK); |
| 102 | + |
| 103 | + var page = getPage(pageIndex); |
| 104 | + long bitMask = 1L << bitIndex; |
| 105 | + return (page.get(wordIndex) & bitMask) != 0; |
| 106 | + } |
| 107 | + |
| 108 | + public long cardinality() { |
| 109 | + final Pages pages = this.pages.get(); |
| 110 | + final long pageCount = pages.length(); |
| 111 | + final long pageSize = this.pageSize; |
| 112 | + |
| 113 | + long setBitCount = 0; |
| 114 | + |
| 115 | + for (int pageIndex = 0; pageIndex < pageCount; pageIndex++) { |
| 116 | + var page = pages.getPage(pageIndex); |
| 117 | + for (int wordIndex = 0; wordIndex < pageSize; wordIndex++) { |
| 118 | + long word = page.get(wordIndex); |
| 119 | + setBitCount += Long.bitCount(word); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + return setBitCount; |
| 124 | + } |
| 125 | + |
| 126 | + public long capacity() { |
| 127 | + return pages.get().length() * (1L << pageShift); |
| 128 | + } |
| 129 | + |
| 130 | + private AtomicLongArray getPage(int pageIndex) { |
| 131 | + var pages = this.pages.get(); |
| 132 | + |
| 133 | + while (pages.length() <= pageIndex) { |
| 134 | + // We need to grow the number of pages to fit the requested page index. |
| 135 | + // This needs to happen in a loop since we can't guarantee that if the |
| 136 | + // current thread is not successful in updating the pages, the newly |
| 137 | + // created pages contain enough space. |
| 138 | + var newPages = new Pages(pages, pageIndex + 1, this.pageSize); |
| 139 | + // Atomically updating the reference. If we're successful, the witness will |
| 140 | + // be the prior `pages` value, and we're done. If we're unsuccessful, we |
| 141 | + // already read the new `pages` value due to CAX call and repeat with that one. |
| 142 | + var witness = this.pages.compareAndExchange(pages, newPages); |
| 143 | + |
| 144 | + if (pages == witness) { |
| 145 | + // Success. |
| 146 | + pages = newPages; |
| 147 | + } else { |
| 148 | + // Throw away the created pages and try again with the new current value. |
| 149 | + pages = witness; |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + return pages.getPage(pageIndex); |
| 154 | + } |
| 155 | + |
| 156 | + private static final class Pages { |
| 157 | + |
| 158 | + private final AtomicLongArray[] pages; |
| 159 | + |
| 160 | + private Pages(int pageCount, int pageSize) { |
| 161 | + var pages = new AtomicLongArray[pageCount]; |
| 162 | + |
| 163 | + for (int pageIndex = 0; pageIndex < pageCount; pageIndex++) { |
| 164 | + pages[pageIndex] = new AtomicLongArray(pageSize); |
| 165 | + } |
| 166 | + |
| 167 | + this.pages = pages; |
| 168 | + } |
| 169 | + |
| 170 | + private Pages(Pages oldPages, int newPageCount, int pageSize) { |
| 171 | + var pages = new AtomicLongArray[newPageCount]; |
| 172 | + |
| 173 | + // We transfer the existing pages to the new pages. |
| 174 | + final int oldPageCount = oldPages.length(); |
| 175 | + System.arraycopy(oldPages.pages, 0, pages, 0, oldPageCount); |
| 176 | + // And add new pages for the remaining ones until we reach the page count. |
| 177 | + // This is potential garbage since the thread creating those might not win |
| 178 | + // the race to grow the pages. |
| 179 | + for (int pageIndex = oldPageCount; pageIndex < newPageCount; pageIndex++) { |
| 180 | + pages[pageIndex] = new AtomicLongArray(pageSize); |
| 181 | + } |
| 182 | + |
| 183 | + this.pages = pages; |
| 184 | + } |
| 185 | + |
| 186 | + private AtomicLongArray getPage(int pageIndex) { |
| 187 | + return pages[pageIndex]; |
| 188 | + } |
| 189 | + |
| 190 | + private int length() { |
| 191 | + return this.pages.length; |
| 192 | + } |
| 193 | + } |
| 194 | +} |
0 commit comments