Skip to content

Commit

Permalink
Trac #25009: update and promote spkg primecount
Browse files Browse the repository at this point in the history
The `prime_pi` function is currently a symbolic function that can hold
its argument as in
{{{
sage: n = SR.var('n')
sage: prime_pi(n)
prime_pi(n)
}}}
that also contains the Cython code for evaluating it
{{{
sage: prime_pi(13543)
1603
}}}
Unfortunately, it is buggy, see #24960 (fixed in #32894, using this
ticket). We will promote the spkg `primecount` to standard and use it
instead. This ticket does this part, and reverts deprecations from
#32412. It also removes obsolete deprecation warning, and the
corresponding no longer available parameter.

Upstream patch: kimwalisch/primesieve#107
(rejected)

-------------
Below is the original part, no longer too relevant.
-------------
We move the evaluation part as a standalone function in `arith/` and
provide alternative implementations
 - using `PARI/GP` (only efficient in the range of tabulated primes)
 - using the different algorithms in the library primecount that is
packaged in #24966

URL: https://trac.sagemath.org/25009
Reported by: vdelecroix
Ticket author(s): Dima Pasechnik
Reviewer(s): Matthias Koeppe
  • Loading branch information
Release Manager committed Dec 10, 2021
2 parents 768f8cc + b9b4eaf commit 5dd6fab
Show file tree
Hide file tree
Showing 31 changed files with 195 additions and 59 deletions.
7 changes: 4 additions & 3 deletions build/pkgs/primecount/checksums.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
tarball=primecount-VERSION.tar.gz
sha1=d7a2c660eb64d14c951feeaee1d94d220c68c23e
md5=ab925094a104499cfc21db2532e65517
cksum=2340721037
sha1=845b863f2007dc90d83c199db0af8fc20fd5092e
md5=b855038556de3f0ec8210b54e4057a65
cksum=2388953073
upstream_url=https://github.com/kimwalisch/primecount/archive/refs/tags/vVERSION.tar.gz
2 changes: 1 addition & 1 deletion build/pkgs/primecount/dependencies
Original file line number Diff line number Diff line change
@@ -1 +1 @@
cmake
primesieve | cmake
2 changes: 2 additions & 0 deletions build/pkgs/primecount/distros/fedora.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
primecount
primecount-devel
2 changes: 1 addition & 1 deletion build/pkgs/primecount/package-version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.1
7.1
13 changes: 13 additions & 0 deletions build/pkgs/primecount/patches/sieve_version.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
diff --git a/CMakeLists.txt b/CMakeLists.txt
index d1fb69a..d9d6d43 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -267,7 +267,7 @@ if(BUILD_LIBPRIMESIEVE)
set(BUILD_MANPAGE "${COPY_BUILD_MANPAGE}" CACHE BOOL "Regenerate man page using a2x" FORCE)
set(BUILD_TESTS "${COPY_BUILD_TESTS}" CACHE BOOL "Build test programs" FORCE)
else()
- find_package(primesieve REQUIRED)
+ find_package(primesieve 7.6 REQUIRED)
endif()

# Testing ############################################################
6 changes: 0 additions & 6 deletions build/pkgs/primecount/spkg-check.in
Original file line number Diff line number Diff line change
@@ -1,8 +1,2 @@
###############################################################################
#
# primecount Sage check script
#
###############################################################################

cd src
$MAKE test
32 changes: 32 additions & 0 deletions build/pkgs/primecount/spkg-configure.m4
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
SAGE_SPKG_CONFIGURE([primecount], [
m4_pushdef([SAGE_PRIMECOUNT_MINVER],[7.1])
m4_pushdef([SAGE_PRIMECOUNT_MAJOR],[7])
m4_pushdef([SAGE_PRIMECOUNT_MINOR],[1])
SAGE_SPKG_DEPCHECK([primesieve], [
dnl Checking for primecount with pkg-config
PKG_CHECK_MODULES([PRIMECOUNT], [primecount >= SAGE_PRIMECOUNT_MINVER], [ ], [
AC_CHECK_HEADER([primecount.h], [
AC_SEARCH_LIBS([primecount_pi], [primecount], [
AC_MSG_CHECKING([checking primecount version directly])
AC_RUN_IFELSE([AC_LANG_PROGRAM([
[#include <primecount.h>
]],[[
if (PRIMECOUNT_VERSION_MAJOR > ]] SAGE_PRIMECOUNT_MAJOR [[ ) return 0;
if (PRIMECOUNT_VERSION_MAJOR == ]] SAGE_PRIMECOUNT_MAJOR [[ &&
PRIMECOUNT_VERSION_MINOR >= ]] SAGE_PRIMECOUNT_MINOR [[ ) return 0;
else return 1;
]])],
[AC_MSG_RESULT([Good.])],
[AC_MSG_RESULT([Too old.])
sage_spkg_install_primecount=yes],
[]) dnl cross-compilation - noop
],
[sage_spkg_install_primecount=yes])
], [sage_spkg_install_primecount=yes])
])
])
m4_popdef([SAGE_PRIMECOUNT_MINVER])
m4_popdef([SAGE_PRIMECOUNT_MAJOR])
m4_popdef([SAGE_PRIMECOUNT_MINOR])
])

21 changes: 11 additions & 10 deletions build/pkgs/primecount/spkg-install.in
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
###############################################################################
#
# primecount Sage install script
#
###############################################################################

cd src

if [ "$SAGE_FAT_BINARY" = yes ]; then
EXTRA_OPTS="-DWITH_POPCNT=OFF"
fi

echo "Configuring primecount."
primc_config(){
echo "Configuring primecount: building primesieve $1"
sdh_cmake -DCMAKE_VERBOSE_MAKEFILE=ON \
-DBUILD_STATIC_LIBS=OFF \
-DBUILD_SHARED_LIBS=ON \
-DBUILD_TESTS=ON \
${EXTRA_OPTS}
-DBUILD_LIBPRIMESIEVE=$1 \
-DCMAKE_FIND_ROOT_PATH=$SAGE_LOCAL/lib/cmake \
-DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY=BOTH \
-DCMAKE_INSTALL_PREFIX=$SAGE_LOCAL \
${EXTRA_OPTS} && sdh_make_install
}

sdh_make
# we try to use the external primesieve first, and if
# cmake cannot find it, we build a vendored copy

sdh_make_install
(primc_config OFF) || primc_config ON
2 changes: 1 addition & 1 deletion build/pkgs/primecount/type
Original file line number Diff line number Diff line change
@@ -1 +1 @@
optional
standard
20 changes: 20 additions & 0 deletions build/pkgs/primesieve/SPKG.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
primesieve: CLI program and C/C++ library for generating primes
===============================================================

Description
-----------

A CLI program and C/C++ library for quickly generating prime numbers.
https://github.com/kimwalisch/primesieve

A dependency of the standard spkg primecount.

License
-------

BSD-2-clause

Upstream Contact
----------------

- https://github.com/kimwalisch/primesieve
5 changes: 5 additions & 0 deletions build/pkgs/primesieve/checksums.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
tarball=primesieve-VERSION.tar.gz
sha1=b78579e0c78fedb75dc9a58b4be3b092eefbcfcb
md5=65eb8972819e47f552f563dc7699be8a
cksum=51195771
upstream_url=https://github.com/kimwalisch/primesieve/archive/refs/tags/vVERSION.tar.gz
1 change: 1 addition & 0 deletions build/pkgs/primesieve/dependencies
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
| cmake
2 changes: 2 additions & 0 deletions build/pkgs/primesieve/distros/alpine.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
primesieve-dev
primesieve
1 change: 1 addition & 0 deletions build/pkgs/primesieve/distros/arch.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
primesieve
1 change: 1 addition & 0 deletions build/pkgs/primesieve/distros/debian.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
libprimesieve-dev
2 changes: 2 additions & 0 deletions build/pkgs/primesieve/distros/fedora.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
primesieve-devel
primesieve
1 change: 1 addition & 0 deletions build/pkgs/primesieve/distros/homebrew.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
primesieve
1 change: 1 addition & 0 deletions build/pkgs/primesieve/distros/opensuse.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
primesieve
1 change: 1 addition & 0 deletions build/pkgs/primesieve/distros/repology.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
primesieve
1 change: 1 addition & 0 deletions build/pkgs/primesieve/package-version.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7.6
54 changes: 54 additions & 0 deletions build/pkgs/primesieve/patches/107.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
From 1f8d0470b4e9d50393032b86d21a7a2fcc512355 Mon Sep 17 00:00:00 2001
From: Matthias Koeppe <mkoeppe@math.ucdavis.edu>
Date: Sat, 27 Nov 2021 10:50:09 -0800
Subject: [PATCH] src/MemoryPool.cpp: Work around missing std::align on GCC 4.x

---
src/MemoryPool.cpp | 27 ++++++++++++++++++++++++++-
1 file changed, 26 insertions(+), 1 deletion(-)

diff --git a/src/MemoryPool.cpp b/src/MemoryPool.cpp
index 7a4a4006..60f62101 100644
--- a/src/MemoryPool.cpp
+++ b/src/MemoryPool.cpp
@@ -26,6 +26,31 @@

using std::size_t;

+#if defined(__GNUC__) && __GNUC__ == 4
+
+// gcc 4.9 does not implement std::align.
+// Use the implementation from
+// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=57350#c11
+
+using std::uintptr_t;
+
+static inline void *align(size_t alignment, size_t size,
+ void *&ptr, size_t &space)
+{
+ uintptr_t pn = reinterpret_cast<uintptr_t>(ptr);
+ uintptr_t aligned = (pn + alignment - 1) & -alignment;
+ size_t padding = aligned - pn;
+ if (space < size + padding) return nullptr;
+ space -= padding;
+ return ptr = reinterpret_cast<void *>(aligned);
+}
+
+#else
+
+using std::align;
+
+#endif
+
namespace primesieve {

void MemoryPool::addBucket(SievingPrime*& sievingPrime)
@@ -70,7 +95,7 @@ void MemoryPool::allocateBuckets()
void* ptr = memory;

// Align pointer address to sizeof(Bucket)
- if (!std::align(sizeof(Bucket), sizeof(Bucket), ptr, bytes))
+ if (!align(sizeof(Bucket), sizeof(Bucket), ptr, bytes))
throw primesieve_error("MemoryPool: failed to align memory!");

initBuckets(ptr, bytes);
2 changes: 2 additions & 0 deletions build/pkgs/primesieve/spkg-check.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cd src
$MAKE test
8 changes: 8 additions & 0 deletions build/pkgs/primesieve/spkg-configure.m4
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
SAGE_SPKG_CONFIGURE([primesieve], [
m4_pushdef([SAGE_PRIMESIEVE_MINVER],[7.6])
dnl Checking for primesieve with pkg-config
PKG_CHECK_MODULES([PRIMESIEVE], [primesieve >= SAGE_PRIMESIEVE_MINVER], [ ], [
sage_spkg_install_primesieve=yes])
m4_popdef([SAGE_PRIMESIEVE_MINVER])
])

14 changes: 14 additions & 0 deletions build/pkgs/primesieve/spkg-install.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
cd src

if [ "$SAGE_FAT_BINARY" = yes ]; then
EXTRA_OPTS="-DWITH_POPCNT=OFF"
fi

echo "Configuring primesieve."
sdh_cmake -DCMAKE_VERBOSE_MAKEFILE=ON \
-DBUILD_STATIC_LIBS=OFF \
-DBUILD_SHARED_LIBS=ON \
-DBUILD_TESTS=ON \
${EXTRA_OPTS}

sdh_make_install
1 change: 1 addition & 0 deletions build/pkgs/primesieve/type
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
standard
2 changes: 1 addition & 1 deletion build/pkgs/sagelib/dependencies
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FORCE $(SCRIPTS) arb boost_cropped $(BLAS) brial cliquer cypari cysignals cython ecl eclib ecm flint libgd gap giac givaro glpk gmpy2 gsl iml jinja2 jupyter_core lcalc lrcalc libbraiding libhomfly libpng linbox m4ri m4rie memory_allocator mpc mpfi mpfr $(MP_LIBRARY) ntl numpy pari pip pkgconfig planarity ppl pplpy pycygwin $(PYTHON) ratpoints rw sage_conf singular symmetrica zn_poly $(PCFILES) | $(PYTHON_TOOLCHAIN) sage_setup
FORCE $(SCRIPTS) arb boost_cropped $(BLAS) brial cliquer cypari cysignals cython ecl eclib ecm flint libgd gap giac givaro glpk gmpy2 gsl iml jinja2 jupyter_core lcalc lrcalc libbraiding libhomfly libpng linbox m4ri m4rie memory_allocator mpc mpfi mpfr $(MP_LIBRARY) ntl numpy pari pip pkgconfig planarity ppl pplpy primesieve primecount pycygwin $(PYTHON) ratpoints rw sage_conf singular symmetrica zn_poly $(PCFILES) | $(PYTHON_TOOLCHAIN) sage_setup

----------
All lines of this file are ignored except the first.
Expand Down
2 changes: 1 addition & 1 deletion build/pkgs/scipoptsuite/spkg-install.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ echo "Building scipoptsuite with cmake"
cd src
mkdir build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX="${SAGE_LOCAL}"/ -DCMAKE_VERBOSE_MAKEFILE=ON -DGMP_DIR="${SAGE_LOCAL}" -DZLIB_ROOT="${SAGE_LOCAL}" -DReadline_ROOT_DIR="${SAGE_LOCAL}" -DBLISS_DIR="${SAGE_LOCAL}" -DIPOPT=FALSE
cmake .. -DCMAKE_VERBOSE_MAKEFILE=ON -DGMP_DIR="${SAGE_LOCAL}" -DZLIB_ROOT="${SAGE_LOCAL}" -DReadline_ROOT_DIR="${SAGE_LOCAL}" -DBLISS_DIR="${SAGE_LOCAL}" -DIPOPT=FALSE
make
make install

Expand Down
3 changes: 1 addition & 2 deletions build/pkgs/symengine/spkg-install.in
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
cd src
mkdir build
cd build
cmake -DCMAKE_INSTALL_PREFIX="$SAGE_LOCAL" \
-DCMAKE_PREFIX_PATH="$SAGE_LOCAL" \
cmake -DCMAKE_PREFIX_PATH="$SAGE_LOCAL" \
-DWITH_SYMENGINE_THREAD_SAFE=yes \
-DWITH_ECM=yes \
-DWITH_FLINT=yes \
Expand Down
42 changes: 10 additions & 32 deletions src/sage/interfaces/primecount.pyx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# sage_setup: distribution = sagemath-primecount

r"""
Interface to the primecount library
"""
Expand All @@ -17,12 +15,6 @@ from libcpp.string cimport string as cppstring
from cpython.int cimport PyInt_FromString

from cysignals.signals cimport sig_on, sig_off


from sage.misc.superseded import deprecation
deprecation(32412, "the module sage.interfaces.primecount is deprecated")


cimport sage.libs.primecount as primecount

cdef inline int _do_sig(int64_t n):
Expand All @@ -37,29 +29,15 @@ cpdef int64_t prime_pi(int64_t n, method=None) except -1:
- ``n`` - an integer
- ``method`` - deprecated, no longer supported in primecount 5
EXAMPLES::
sage: from sage.interfaces.primecount import prime_pi # optional - primecount
doctest:warning
...
DeprecationWarning: the module sage.interfaces.primecount is deprecated
See https://trac.sagemath.org/32412 for details.
sage: prime_pi(1000) == 168 # optional - primecount
sage: from sage.interfaces.primecount import prime_pi
sage: prime_pi(1000) == 168
True
sage: prime_pi(1000, method='deleglise_rivat') == 168 # optional - primecount
doctest:warning
...
DeprecationWarning: primecount 5 no longer supports the 'method' parameter
See https://trac.sagemath.org/28493 for details.
sage: prime_pi(1000, method='deleglise_rivat') == 168
True
"""
cdef int64_t ans
if method is not None:
from sage.misc.superseded import deprecation_cython as deprecation
deprecation(28493, "primecount 5 no longer supports the 'method' parameter")
if _do_sig(n): sig_on()
ans = primecount.pi(n)
if _do_sig(n): sig_off()
Expand All @@ -71,9 +49,9 @@ cpdef prime_pi_128(n):
EXAMPLES::
sage: from sage.interfaces.primecount import prime_pi_128 # optional - primecount
sage: from sage.interfaces.primecount import prime_pi_128
sage: prime_pi_128(1000) # optional - primecount
sage: prime_pi_128(1000)
168
sage: nth_prime_128(2**65) # not tested
?
Expand All @@ -91,9 +69,9 @@ cpdef int64_t nth_prime(int64_t n) except -1:
EXAMPLES::
sage: from sage.interfaces.primecount import nth_prime # optional - primecount
sage: from sage.interfaces.primecount import nth_prime
sage: nth_prime(168) == 997 # optional - primecount
sage: nth_prime(168) == 997
True
"""
if n <= 0:
Expand All @@ -114,11 +92,11 @@ cpdef int64_t phi(int64_t x, int64_t a):
EXAMPLES::
sage: from sage.interfaces.primecount import phi # optional - primecount
sage: from sage.interfaces.primecount import phi
sage: phi(1000, 3) == 266 # optional - primecount
sage: phi(1000, 3) == 266
True
sage: phi(2**30, 100) == 95446716 # optional - primecount
sage: phi(2**30, 100) == 95446716
True
"""
return primecount.phi(x, a)
Expand Down
2 changes: 1 addition & 1 deletion src/sage/libs/primecount.pxd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# distutils: libraries = primecount
# distutils: libraries = primecount primesieve
# distutils: language = c++

# Use of this file is deprecated.
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ setenv =
fedora-29: BASE_TAG=29
fedora-29: IGNORE_MISSING_SYSTEM_PACKAGES=yes
fedora-30: BASE_TAG=30
fedora-30: IGNORE_MISSING_SYSTEM_PACKAGES=yes
fedora-31: BASE_TAG=31
fedora-32: BASE_TAG=32
fedora-33: BASE_TAG=33
Expand Down

0 comments on commit 5dd6fab

Please sign in to comment.