-
Notifications
You must be signed in to change notification settings - Fork 11k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[flang] Allow more concurrently open NEWUNIT= values, with recycling
Add a header-only implementation of Briggs & Torczon's fast small integer set data structure to flang/include/flang/Common, and use it in the runtime to manage a pool of Fortran unit numbers with recycling. This replaces the bit set previously used for that purpose. The set is initialized on demand with the negations of all the NEWUNIT= unit numbers that can be returned to any kind of integer variable. For programs that require more concurrently open NEWUNIT= unit numbers than the pool can hold, they are now allocated with a non-recycling counter. This allows as many open units as the operating system provides. Many of the top-line comments in flang/unittests/Runtime had the wrong path name. I noticed this while adding a unit test for the fast integer set data structure, and cleaned them up. Differential Revision: https://reviews.llvm.org/D120685
- Loading branch information
Showing
27 changed files
with
282 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
//===-- include/flang/Common/fast-int-set.h --------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// Implements a Briggs-Torczon fast set of integers in a fixed small range | ||
// [0..(n-1)] This is a data structure with no dynamic memory allocation and all | ||
// O(1) elemental operations. It does not need to initialize its internal state | ||
// arrays, but you can call its InitializeState() member function to avoid | ||
// complaints from valgrind. | ||
|
||
// The set is implemented with two arrays and an element count. | ||
// 1) The distinct values in the set occupy the leading elements of | ||
// value_[0 .. size_-1] in arbitrary order. Their positions may change | ||
// when other values are removed from the set with Remove(). | ||
// 2) For 0 <= j < size_, index_[value_[j]] == j. | ||
// 3) If only Add() and PopValue() are used, the popped values will be the | ||
// most recently Add()ed distinct unpopped values; i.e., the value_ array | ||
// will function as a stack whose top is at (size_-1). | ||
|
||
#ifndef FORTRAN_COMMON_FAST_INT_SET_H_ | ||
#define FORTRAN_COMMON_FAST_INT_SET_H_ | ||
|
||
#include <optional> | ||
|
||
namespace Fortran::common { | ||
|
||
template <int N> class FastIntSet { | ||
public: | ||
static_assert(N > 0); | ||
static constexpr int maxValue{N - 1}; | ||
|
||
int size() const { return size_; } | ||
const int *value() const { return &value_[0]; } | ||
|
||
bool IsValidValue(int n) const { return n >= 0 && n <= maxValue; } | ||
|
||
void Clear() { size_ = 0; } | ||
|
||
bool IsEmpty() const { return size_ == 0; } | ||
|
||
void InitializeState() { | ||
if (!isFullyInitialized_) { | ||
for (int j{size_}; j < N; ++j) { | ||
value_[j] = index_[j] = 0; | ||
} | ||
isFullyInitialized_ = true; | ||
} | ||
} | ||
|
||
bool Contains(int n) const { | ||
if (IsValidValue(n)) { | ||
int j{index_[n]}; | ||
return j >= 0 && j < size_ && value_[j] == n; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
bool Add(int n) { | ||
if (IsValidValue(n)) { | ||
if (!UncheckedContains(n)) { | ||
value_[index_[n] = size_++] = n; | ||
} | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
bool Remove(int n) { | ||
if (IsValidValue(n)) { | ||
if (UncheckedContains(n)) { | ||
int last{value_[--size_]}; | ||
value_[index_[last] = index_[n]] = last; | ||
} | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
std::optional<int> PopValue() { | ||
if (IsEmpty()) { | ||
return std::nullopt; | ||
} else { | ||
return value_[--size_]; | ||
} | ||
} | ||
|
||
private: | ||
bool UncheckedContains(int n) const { | ||
int j{index_[n]}; | ||
return j >= 0 && j < size_ && value_[j] == n; | ||
} | ||
|
||
int value_[N]; | ||
int index_[N]; | ||
int size_{0}; | ||
bool isFullyInitialized_{false}; // memory was cleared | ||
}; | ||
} // namespace Fortran::common | ||
#endif // FORTRAN_COMMON_FAST_INT_SET_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
add_flang_unittest(FlangCommonTests | ||
FastIntSetTest.cpp | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
//===-- flang/unittests/Common/FastIntSetTest.cpp ---------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "gtest/gtest.h" | ||
#include "flang/Common/fast-int-set.h" | ||
#include <optional> | ||
|
||
TEST(FastIntSetTests, Sanity) { | ||
static constexpr int N{100}; | ||
Fortran::common::FastIntSet<N> set; | ||
|
||
ASSERT_FALSE(set.IsValidValue(-1)); | ||
ASSERT_TRUE(set.IsValidValue(0)); | ||
ASSERT_TRUE(set.IsValidValue(N - 1)); | ||
ASSERT_FALSE(set.IsValidValue(N)); | ||
ASSERT_TRUE(set.IsEmpty()); | ||
ASSERT_EQ(set.size(), 0); | ||
ASSERT_FALSE(set.Contains(0)); | ||
ASSERT_FALSE(set.Contains(N - 1)); | ||
|
||
ASSERT_TRUE(set.Add(0)); | ||
ASSERT_FALSE(set.IsEmpty()); | ||
ASSERT_EQ(set.size(), 1); | ||
ASSERT_TRUE(set.Contains(0)); | ||
|
||
ASSERT_TRUE(set.Add(0)); // duplicate | ||
ASSERT_EQ(set.size(), 1); | ||
ASSERT_TRUE(set.Contains(0)); | ||
|
||
ASSERT_TRUE(set.Remove(0)); | ||
ASSERT_TRUE(set.IsEmpty()); | ||
ASSERT_EQ(set.size(), 0); | ||
ASSERT_FALSE(set.Contains(0)); | ||
|
||
ASSERT_FALSE(set.Add(N)); | ||
ASSERT_TRUE(set.IsEmpty()); | ||
ASSERT_EQ(set.size(), 0); | ||
ASSERT_FALSE(set.Contains(N)); | ||
|
||
ASSERT_TRUE(set.Add(N - 1)); | ||
ASSERT_FALSE(set.IsEmpty()); | ||
ASSERT_EQ(set.size(), 1); | ||
ASSERT_TRUE(set.Contains(N - 1)); | ||
|
||
std::optional<int> x; | ||
x = set.PopValue(); | ||
ASSERT_TRUE(x.has_value()); | ||
ASSERT_EQ(*x, N - 1); | ||
ASSERT_TRUE(set.IsEmpty()); | ||
ASSERT_EQ(set.size(), 0); | ||
|
||
x = set.PopValue(); | ||
ASSERT_FALSE(x.has_value()); | ||
|
||
for (int j{0}; j < N; ++j) { | ||
ASSERT_TRUE(set.Add(j)) << j; | ||
} | ||
ASSERT_FALSE(set.IsEmpty()); | ||
ASSERT_EQ(set.size(), N); | ||
for (int j{0}; j < N; ++j) { | ||
ASSERT_TRUE(set.Contains(j)) << j; | ||
} | ||
|
||
for (int j{0}; j < N; ++j) { | ||
ASSERT_TRUE(set.Remove(j)) << j; | ||
ASSERT_EQ(set.size(), N - j - 1) << j; | ||
ASSERT_FALSE(set.Contains(j)) << j; | ||
} | ||
|
||
ASSERT_TRUE(set.IsEmpty()); | ||
ASSERT_EQ(set.size(), 0); | ||
|
||
for (int j{N - 1}; j >= 0; --j) { | ||
ASSERT_TRUE(set.Add(j)) << j; | ||
} | ||
for (int j{0}; j < N; j++) { | ||
x = set.PopValue(); | ||
ASSERT_TRUE(x.has_value()); | ||
ASSERT_EQ(*x, j) << j; | ||
} | ||
ASSERT_TRUE(set.IsEmpty()); | ||
ASSERT_EQ(set.size(), 0); | ||
|
||
for (int j{0}; j < N; j++) { | ||
ASSERT_TRUE(set.Add(j)) << j; | ||
} | ||
ASSERT_FALSE(set.IsEmpty()); | ||
ASSERT_EQ(set.size(), N); | ||
for (int j{0}; j < N; j += 2) { | ||
ASSERT_TRUE(set.Remove(j)) << j; | ||
} | ||
ASSERT_FALSE(set.IsEmpty()); | ||
ASSERT_EQ(set.size(), N / 2); | ||
for (int j{0}; j < N; j++) { | ||
ASSERT_EQ(set.Contains(j), (j & 1) == 1); | ||
} | ||
|
||
set.Clear(); | ||
ASSERT_TRUE(set.IsEmpty()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.