Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions CGAL_Core/include/CGAL/CORE/CoreDefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#define _CORE_COREDEFS_H_

#include <CGAL/CORE/extLong.h>
#include <CGAL/tss.h>
#include <CGAL/disable_warnings.h>

#include <atomic>
Expand All @@ -42,6 +43,15 @@
return NAME; \
}

// State of which each thread has its own copy (with the given initial value), for the precisions, which
// computations set temporarily; were they shared, concurrent computations would use the precisions of each other.
#define CGAL_THREAD_LOCAL_STATE_VAR(TYPE, NAME, VALUE) \
inline TYPE & get_static_##NAME() \
{ \
CGAL_STATIC_THREAD_LOCAL_VARIABLE(TYPE, NAME, VALUE); \
return NAME; \
}

#else // CGAL_HEADER_ONLY

#define CGAL_GLOBAL_STATE_VAR(TYPE, NAME, VALUE) \
Expand All @@ -51,6 +61,11 @@
return NAME; \
}

// (Thread-local variables cannot be exported from a DLL; hence, the accessor is exported instead, and it is defined,
// with the variable, in CoreDefs_impl.h.)
#define CGAL_THREAD_LOCAL_STATE_VAR(TYPE, NAME, VALUE) \
CGAL_CORE_EXPORT TYPE& get_static_##NAME();

#endif // CGAL_HEADER_ONLY

namespace CORE {
Expand Down Expand Up @@ -107,12 +122,13 @@ CGAL_GLOBAL_STATE_VAR(std::atomic<bool>, EscapePrecWarning, true)
#endif

// These following two values determine the precision of computing
// approximations in Expr.
// approximations in Expr. They, and the precisions of BigFloat division and
// sqrt below, are thread-local: setting them affects only the calling thread.

/// default Relative Precision in bits
CGAL_GLOBAL_STATE_VAR(extLong, defRelPrec, 60)
CGAL_THREAD_LOCAL_STATE_VAR(extLong, defRelPrec, 60)
/// default Absolute Precision in bits
CGAL_GLOBAL_STATE_VAR(extLong, defAbsPrec, CORE_posInfty)
CGAL_THREAD_LOCAL_STATE_VAR(extLong, defAbsPrec, CORE_posInfty)

/// default # of decimal digits for conversion from a BF to string.
/** This value cannot be CORE_INFTY.
Expand Down Expand Up @@ -158,9 +174,9 @@ getBigFloatInputDigits()
}

/// default BigFloat Division Relative Precision
CGAL_GLOBAL_STATE_VAR(extLong, defBFdivRelPrec, 54)
CGAL_THREAD_LOCAL_STATE_VAR(extLong, defBFdivRelPrec, 54)
/// default BigFloat Sqrt Absolute Precision
CGAL_GLOBAL_STATE_VAR(extLong, defBFsqrtAbsPrec, 54)
CGAL_THREAD_LOCAL_STATE_VAR(extLong, defBFsqrtAbsPrec, 54)

//////////////////////////////////////////////////////////////
// Mode parameters: incremental, progressive, filters
Expand Down Expand Up @@ -208,6 +224,7 @@ CGAL_GLOBAL_STATE_VAR(std::atomic<long>, defInitialProgressivePrec, 64)
// methods for setting global precision parameters
// including: scientific vs. positional format
// All the set methods return the previous global value if any
// (the precisions are thread-local: they are set for the calling thread)
//////////////////////////////////////////////////////////////

/// set default composite precision [defAbsPrec, defRelPrec]
Expand Down Expand Up @@ -333,5 +350,6 @@ inline void setPositionalFormat(std::ostream& o = std::cout) {
#include <CGAL/enable_warnings.h>

#undef CGAL_GLOBAL_STATE_VAR
#undef CGAL_THREAD_LOCAL_STATE_VAR

#endif // _CORE_COREDEFS_H_
20 changes: 16 additions & 4 deletions CGAL_Core/include/CGAL/CORE/CoreDefs_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,20 @@ std::atomic<int> InvalidFlag(0);
* -- this is used by BigFloat division when the arguments are error-free.
*/

extLong defBFdivRelPrec = 54;
CGAL_CORE_EXPORT extLong& get_static_defBFdivRelPrec() {
CGAL_STATIC_THREAD_LOCAL_VARIABLE(extLong, defBFdivRelPrec, 54); // one per thread
return defBFdivRelPrec;
}

/**
* Default BigFloat Sqrt Absolute Precision
* -- this is used by BigFloat sqrt when the argument is error-free.
*/

extLong defBFsqrtAbsPrec = 54;
CGAL_CORE_EXPORT extLong& get_static_defBFsqrtAbsPrec() {
CGAL_STATIC_THREAD_LOCAL_VARIABLE(extLong, defBFsqrtAbsPrec, 54); // one per thread
return defBFsqrtAbsPrec;
}

/**
* Escape Precision
Expand All @@ -107,13 +113,19 @@ std::atomic<bool> EscapePrecWarning(true);
* (exact, implicit) constant value. */

/** absolute precision = 2^31 - 1 */
extLong defAbsPrec = CORE_posInfty;
CGAL_CORE_EXPORT extLong& get_static_defAbsPrec() {
CGAL_STATIC_THREAD_LOCAL_VARIABLE(extLong, defAbsPrec, CORE_posInfty); // one per thread
return defAbsPrec;
}
/** default relative precision is 60 relative bits.
* Why 60? We would really like this to be 54, so that the default
* conversion duplicates the IEEE double precision. But it turns out
* (see README file under BUGS), we need 59 to ensure this.
* Chee Yap (7/1/01) */
extLong defRelPrec = 60;
CGAL_CORE_EXPORT extLong& get_static_defRelPrec() {
CGAL_STATIC_THREAD_LOCAL_VARIABLE(extLong, defRelPrec, 60); // one per thread
return defRelPrec;
}

/** number of BigFloat digits to print out */
#ifdef CGAL_NO_ATOMIC
Expand Down
9 changes: 9 additions & 0 deletions Installation/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ Release date: December 2026
and "protected" edges that may not be modified at all by remeshing.
- Fix an API bug in `CGAL::Polygon_mesh_processing::remesh_planar_patches()`: when coplanarity/collinearity criteria are too relaxed, it might happen that some patches cannot be triangulated. To handle such cases, a Boolean is now returned to indicate if all patches could be triangulated (same behavior as `CGAL::Polygon_mesh_processing::remesh_almost_planar_patches()`). Also those two functions can now produce a (self-intersecting) mesh in case the simplified mesh turned out to be non-manifold (internally calling the function `CGAL::Polygon_mesh_processing::orient_polygon_soup()` on the corners and patches detected). In order to check if a non-manifoldness was worked around, the new named parameter `number_of_corners()` can be used and the filled value shall be compared to the number of vertices of the output mesh.

### Number Types

- **Breaking change**: The precisions of `CORE` (the default relative and absolute precisions of `CORE::Expr`, and the
precisions of `CORE::BigFloat` division and square root), which `CGAL::set_precision()` sets for `CORE::BigFloat`
intervals, are now thread-local: setting them affects only the calling thread, and every thread starts with the default
precisions. Previously they were shared by all threads, so concurrent computations with algebraic numbers (e.g.,
arrangements of algebraic curves or of rational functions constructed in different threads) used the precisions of each
other, which could produce wrong results or failed assertions.

## [Release 6.2](https://github.com/CGAL/cgal/releases/tag/v6.2)

Release date: June 2026
Expand Down
1 change: 1 addition & 0 deletions Number_types/test/Number_types/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ create_single_source_cgal_program("CORE_BigFloat.cpp")
create_single_source_cgal_program("CORE_BigInt.cpp")
create_single_source_cgal_program("CORE_BigRat.cpp")
create_single_source_cgal_program("CORE_Expr.cpp")
create_single_source_cgal_program("CORE_thread_local_precision.cpp")
find_package(MPFI QUIET)
if( MPFI_FOUND )
include( ${MPFI_USE_FILE} )
Expand Down
100 changes: 100 additions & 0 deletions Number_types/test/Number_types/CORE_thread_local_precision.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Tests that the precisions of CORE (the default relative and absolute precisions, and those of BigFloat division and
// square root) are thread-local: a precision set in one thread does not affect other threads, each thread starts with
// the default precisions, and threads that compute concurrently, each with precisions of its own, obtain the results
// that they obtain sequentially. (The bigfloat intervals of the algebraic kernels set the precisions temporarily;
// shared precisions made concurrent computations use the precisions of each other.)

#include <iostream>

#include <CGAL/config.h>

#if ! defined(CGAL_USE_CORE) || ! defined(CGAL_HAS_THREADS)

int main() {
std::cout << "This test requires CORE and threads; it is skipped." << std::endl;
return 0;
}

#else

#include <thread>
#include <vector>

#include <CGAL/CORE_BigFloat.h>

using BigFloat = CORE::BigFloat;
using BFI = CGAL::Bigfloat_interval_traits<BigFloat>::Type;

/*! checks that the precisions of the calling thread are the given ones.
*/
bool check(long rel, long div, const char* where) {
const long r = CORE::get_static_defRelPrec().toLong();
const long d = CORE::get_static_defBFdivRelPrec().toLong();
if (r == rel && d == div) return true;
std::cerr << "Error: " << where << ": relative precision " << r << " (expected " << rel << "), division precision "
<< d << " (expected " << div << ")" << std::endl;
return false;
}

/*! computes quotients and square roots at the given precision (set as the bigfloat intervals set it), repeatedly.
*/
std::vector<BigFloat> compute(long prec, int rounds) {
std::vector<BigFloat> res;
for (int i = 0; i < rounds; ++i) {
const long old = CGAL::set_precision(BFI(), prec);
BigFloat x(1 + i);
BigFloat y(3 + 2 * i);
res.push_back(x / y);
res.push_back(CORE::sqrt(x / y));
CGAL::set_precision(BFI(), old);
}
return res;
}

bool same(const std::vector<BigFloat>& a, const std::vector<BigFloat>& b) {
if (a.size() != b.size()) return false;
for (std::size_t i = 0; i < a.size(); ++i) {
// the same mantissa, error and exponent
if (a[i].m() != b[i].m() || a[i].err() != b[i].err() || a[i].exp() != b[i].exp()) return false;
}
return true;
}

int main() {
bool ok = true;
const long def_rel = CORE::get_static_defRelPrec().toLong();
const long def_div = CORE::get_static_defBFdivRelPrec().toLong();

// a precision set in one thread does not affect other threads, which start with the default precisions
CGAL::set_precision(BFI(), 300);
ok &= check(300, 300, "the main thread, after setting the precision");
std::thread t([&ok, def_rel, def_div]() {
ok &= check(def_rel, def_div, "a new thread");
CGAL::set_precision(BFI(), 500);
ok &= check(500, 500, "a new thread, after setting the precision");
});
t.join();
ok &= check(300, 300, "the main thread, after another thread set the precision");
CGAL::set_precision(BFI(), def_rel);

// concurrent computations, each with precisions of its own, obtain their sequential results
const int num_threads = 8;
const int rounds = 2000;
std::vector<std::vector<BigFloat>> expected(num_threads);
for (int i = 0; i < num_threads; ++i) expected[i] = compute(40 + 60 * i, rounds);
std::vector<std::vector<BigFloat>> results(num_threads);
std::vector<std::thread> threads;
for (int i = 0; i < num_threads; ++i)
threads.emplace_back([&results, i, rounds]() { results[i] = compute(40 + 60 * i, rounds); });
for (auto& th : threads) th.join();
for (int i = 0; i < num_threads; ++i) {
if (same(results[i], expected[i])) continue;
std::cerr << "Error: thread " << i << " obtains results that differ from the sequential ones" << std::endl;
ok = false;
}

std::cout << (ok ? "passed" : "failed") << std::endl;
return ok ? 0 : 1;
}

#endif
Loading