Skip to content

Commit 6856f5f

Browse files
committed
docs
1 parent 476ac4d commit 6856f5f

File tree

4 files changed

+64
-13
lines changed

4 files changed

+64
-13
lines changed

quaddtype/README.md

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ np.array([1,2,3], dtype=QuadPrecDType("longdouble"))
3131
- **CMake** (≥3.15)
3232
- **Python 3.10+**
3333
- **Git**
34+
- **NumPy >= 2.4** (build from source)
3435

3536
### Linux/Unix/macOS
3637

@@ -48,11 +49,11 @@ pip install numpy pytest
4849
# export CFLAGS="-DDISABLE_QUADBLAS"
4950
# export CXXFLAGS="-DDISABLE_QUADBLAS"
5051

51-
python -m pip install . -v
52+
python -m pip install . -v --no-build-isolation
5253

5354
# Run the tests
5455
cd ..
55-
python -m pytest
56+
python -m pytest/quaddtype/tests/
5657
```
5758

5859
### Windows
@@ -94,14 +95,14 @@ python -m pytest
9495

9596
```powershell
9697
# Build and install the package
97-
python -m pip install . -v
98+
python -m pip install . -v --no-build-isolation
9899
```
99100

100101
5. **Test Installation**
101102

102103
```powershell
103104
# Run tests
104-
pytest -s tests/
105+
pytest -s ..\quaddtype\tests\
105106
```
106107

107108
6. **QBLAS Disabled**: QuadBLAS optimization is automatically disabled on Windows builds due to MSVC compatibility issues. This is handled by the `-DDISABLE_QUADBLAS` compiler flag.
@@ -112,3 +113,58 @@ python -m pytest
112113
- VS 2017: `"Visual Studio 15 2017"`
113114

114115
8. **Architecture**: The instructions are for x64. For x86 builds, change `-A x64` to `-A Win32`.
116+
117+
## Building with ThreadSanitizer (TSan)
118+
119+
This is a development feature to help detect threading issues. To build `numpy-quaddtype` with TSan enabled, follow these steps:
120+
121+
> Use of clang is recommended with machine NOT supporting `libquadmath` (like ARM64). Set the compiler to clang/clang++ before proceeding.
122+
> ```bash
123+
> export CC=clang
124+
> export CXX=clang++
125+
> ```
126+
127+
1. Compile free-threaded CPython with TSan support. Follow the [Python Free-Threading Guide](https://py-free-threading.github.io/thread_sanitizer/#compile-free-threaded-cpython-with-tsan) for detailed instructions.
128+
2. Create and activate a virtual environment using the TSan-enabled Python build.
129+
3. Installing dependencies:
130+
131+
```bash
132+
python -m pip install meson meson-python wheel ninja
133+
# Need NumPy built with TSan as well
134+
python -m pip install "numpy @ git+https://github.com/numpy/numpy" -C'setup-args=-Db_sanitize=thread'
135+
```
136+
4. Building SLEEF with TSan:
137+
138+
```bash
139+
# clone the repository
140+
git clone -b 3.8 https://github.com/shibatch/sleef.git
141+
cd sleef
142+
143+
# Build SLEEF with TSan
144+
cmake \
145+
-DCMAKE_C_COMPILER=clang \
146+
-DCMAKE_CXX_COMPILER=clang++ \
147+
-DCMAKE_C_FLAGS="-fsanitize=thread -g -O1" \
148+
-DCMAKE_CXX_FLAGS="-fsanitize=thread -g -O1" \
149+
-DCMAKE_EXE_LINKER_FLAGS="-fsanitize=thread" \
150+
-DCMAKE_SHARED_LINKER_FLAGS="-fsanitize=thread" \
151+
-DSLEEF_BUILD_QUAD=ON \
152+
-DSLEEF_BUILD_TESTS=OFF \
153+
-S . -B build
154+
155+
cmake --build build -j
156+
157+
# Install the built library and headers into the system path (/usr/local)
158+
sudo cmake --install build --prefix=/usr/local
159+
```
160+
5. Build and install `numpy-quaddtype` with TSan:
161+
162+
```bash
163+
# SLEEF is already installed with TSan, we need to provide proper flags to numpy-quaddtype's meson file
164+
# So that it does not build SLEEF again and use the installed one.
165+
166+
export CFLAGS="-fsanitize=thread -g -O0"
167+
export CXXFLAGS="-fsanitize=thread -g -O0"
168+
export LDFLAGS="-fsanitize=thread"
169+
python -m pip install . -vv --no-build-isolation -Csetup-args=-Db_sanitize=thread
170+
```

quaddtype/numpy_quaddtype/src/scalar.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,6 @@ PyObject* quad_to_pylong(Sleef_quad value)
461461

462462
// inspired by the CPython implementation
463463
// https://github.com/python/cpython/blob/ac1ffd77858b62d169a08040c08aa5de26e145ac/Objects/floatobject.c#L1503C1-L1572C2
464-
// NOTE: a 128-bit
465464
static PyObject *
466465
QuadPrecision_as_integer_ratio(QuadPrecisionObject *self, PyObject *Py_UNUSED(ignored))
467466
{
@@ -520,7 +519,6 @@ QuadPrecision_as_integer_ratio(QuadPrecisionObject *self, PyObject *Py_UNUSED(ig
520519

521520
// numerator and denominators can't fit in int
522521
// convert items to PyLongObject from string instead
523-
524522
PyObject *py_exp = PyLong_FromLongLong(Py_ABS(exponent));
525523
if(py_exp == NULL)
526524
{
@@ -530,11 +528,12 @@ QuadPrecision_as_integer_ratio(QuadPrecisionObject *self, PyObject *Py_UNUSED(ig
530528
PyObject *numerator = quad_to_pylong(mantissa);
531529
if(numerator == NULL)
532530
{
533-
Py_DECREF(numerator);
531+
Py_DECREF(py_exp);
534532
return NULL;
535533
}
536534
PyObject *denominator = PyLong_FromLong(1);
537535
if (denominator == NULL) {
536+
Py_DECREF(py_exp);
538537
Py_DECREF(numerator);
539538
return NULL;
540539
}

quaddtype/reinstall.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ python -m pip install . -vv --no-build-isolation 2>&1 | tee build_log.txt
1515
# export CFLAGS="-fsanitize=thread -g -O0"
1616
# export CXXFLAGS="-fsanitize=thread -g -O0"
1717
# export LDFLAGS="-fsanitize=thread"
18-
# python -m pip install . -vv --no-build-isolation -Csetup-args=-Db_sanitize=thread 2>&1 | tee build_log.txt
18+
# CC=clang CXX=clang++ python -m pip install . -vv --no-build-isolation -Csetup-args=-Db_sanitize=thread 2>&1 | tee build_log.txt

quaddtype/subprojects/packagefiles/sleef/meson.build

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ if host_machine.system() == 'windows'
1212
parallel_flag = []
1313
endif
1414

15-
# uncomment below lines for TSAN builds (in case compiler flags are not picked up from meson)
15+
# For building sleef with TSan, delete the sleef subproject and follow the README instructions to build sleef externally.
1616
sleef_configure = run_command([
1717
cmake,
1818
'-S', meson.current_source_dir(),
@@ -23,10 +23,6 @@ sleef_configure = run_command([
2323
'-DSLEEF_BUILD_TESTS=OFF',
2424
'-DSLEEF_BUILD_INLINE_HEADERS=OFF',
2525
'-DCMAKE_POSITION_INDEPENDENT_CODE=ON',
26-
# '-DCMAKE_C_FLAGS=-fsanitize=thread -g',
27-
# '-DCMAKE_CXX_FLAGS=-fsanitize=thread -g',
28-
# '-DCMAKE_EXE_LINKER_FLAGS=-fsanitize=thread',
29-
# '-DCMAKE_SHARED_LINKER_FLAGS=-fsanitize=thread',
3026
'-DCMAKE_INSTALL_PREFIX=' + meson.current_build_dir() / sleef_install_dir
3127
], check: false, capture: true)
3228

0 commit comments

Comments
 (0)