Skip to content

Commit b465f94

Browse files
authored
[OpenMP][NFC] Put ExponentialBackoff in a Utils header (llvm#73816)
"private.h" will go.
1 parent 5ce5ea3 commit b465f94

File tree

3 files changed

+55
-30
lines changed

3 files changed

+55
-30
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//===-- Utils/ExponentialBackoff.h - Heuristic helper class ------*- C++ -*===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// Implement exponential backoff counting.
10+
// Linearly increments until given maximum, exponentially decrements based on
11+
// given backoff factor.
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
#ifndef OMPTARGET_UTILS_EXPONENTIAL_BACKOFF_H
16+
#define OMPTARGET_UTILS_EXPONENTIAL_BACKOFF_H
17+
18+
#include <cassert>
19+
#include <cmath>
20+
#include <cstdint>
21+
22+
namespace utils {
23+
24+
class ExponentialBackoff {
25+
int64_t Count = 0;
26+
const int64_t MaxCount = 0;
27+
const int64_t CountThreshold = 0;
28+
const double BackoffFactor = 0;
29+
30+
public:
31+
ExponentialBackoff(int64_t MaxCount, int64_t CountThreshold,
32+
double BackoffFactor)
33+
: MaxCount(MaxCount), CountThreshold(CountThreshold),
34+
BackoffFactor(BackoffFactor) {
35+
assert(MaxCount >= 0 &&
36+
"ExponentialBackoff: maximum count value should be non-negative");
37+
assert(CountThreshold >= 0 &&
38+
"ExponentialBackoff: count threshold value should be non-negative");
39+
assert(BackoffFactor >= 0 && BackoffFactor < 1 &&
40+
"ExponentialBackoff: backoff factor should be in [0, 1) interval");
41+
}
42+
43+
void increment() { Count = std::min(Count + 1, MaxCount); }
44+
45+
void decrement() { Count *= BackoffFactor; }
46+
47+
bool isAboveThreshold() const { return Count > CountThreshold; }
48+
};
49+
50+
} // namespace utils
51+
52+
#endif // OMPTARGET_UTILS_EXPONENTIAL_BACKOFF_H

openmp/libomptarget/src/interface.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include "Shared/Profile.h"
2222
#include "Shared/Utils.h"
2323

24+
#include "Utils/ExponentialBackoff.h"
25+
2426
#include <cassert>
2527
#include <cstdint>
2628
#include <cstdio>
@@ -452,7 +454,7 @@ EXTERN void __tgt_target_nowait_query(void **AsyncHandle) {
452454
// completed (use device side blocking mechanism). This allows the runtime to
453455
// adapt itself when there are a lot of long-running target regions in-flight.
454456
using namespace llvm::omp::target;
455-
static thread_local ExponentialBackoff QueryCounter(
457+
static thread_local utils::ExponentialBackoff QueryCounter(
456458
Int64Envar("OMPTARGET_QUERY_COUNT_MAX", 10),
457459
Int64Envar("OMPTARGET_QUERY_COUNT_THRESHOLD", 5),
458460
Envar<float>("OMPTARGET_QUERY_COUNT_BACKOFF_FACTOR", 0.5f));

openmp/libomptarget/src/private.h

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -382,33 +382,4 @@ class TaskAsyncInfoWrapperTy {
382382
operator AsyncInfoTy &() { return *AsyncInfo; }
383383
};
384384

385-
// Implement exponential backoff counting.
386-
// Linearly increments until given maximum, exponentially decrements based on
387-
// given backoff factor.
388-
class ExponentialBackoff {
389-
int64_t Count = 0;
390-
const int64_t MaxCount = 0;
391-
const int64_t CountThreshold = 0;
392-
const float BackoffFactor = 0.0f;
393-
394-
public:
395-
ExponentialBackoff(int64_t MaxCount, int64_t CountThreshold,
396-
float BackoffFactor)
397-
: MaxCount(MaxCount), CountThreshold(CountThreshold),
398-
BackoffFactor(BackoffFactor) {
399-
assert(MaxCount >= 0 &&
400-
"ExponentialBackoff: maximum count value should be non-negative");
401-
assert(CountThreshold >= 0 &&
402-
"ExponentialBackoff: count threshold value should be non-negative");
403-
assert(BackoffFactor >= 0 && BackoffFactor < 1 &&
404-
"ExponentialBackoff: backoff factor should be in [0, 1) interval");
405-
}
406-
407-
void increment() { Count = std::min(Count + 1, MaxCount); }
408-
409-
void decrement() { Count *= BackoffFactor; }
410-
411-
bool isAboveThreshold() const { return Count > CountThreshold; }
412-
};
413-
414385
#endif

0 commit comments

Comments
 (0)