opened
brianmcconnel
username_0
I wasn't able to determine the reason, but it seems that when importing mpmath with gmp=6.2.0 it creates a Memory Error during this operation:

mpmath/mpmath/libmp/libelefun.py
```
# ~= next power of two + 20
cache_prec_steps = [22,22]
for k in xrange(1, bitcount(LOG_TAYLOR_PREC)+1):
cache_prec_steps += [min(2**k,LOG_TAYLOR_PREC)+20] * 2**(k-1)
```


When I downgraded gmp<=6.1.2.

mpmath imported successfully.

Stack trace is unavailable.  If I am able to reproduce behavior I will attach.

python 3.7.6
gmpy2 2.1.0b1
mpmath creates Memory Error on import
issue
created
brianmcconnel
username_0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/$HOME/miniconda3/envs/mpmath/lib/python3.7/site-packages/mpmath/__init__.py", line 5, in <module>
    from .ctx_fp import FPContext
  File "/home/$HOME/miniconda3/envs/mpmath/lib/python3.7/site-packages/mpmath/ctx_fp.py", line 1, in <module>
    from .ctx_base import StandardBaseContext
  File "/home/$HOME/miniconda3/envs/mpmath/lib/python3.7/site-packages/mpmath/ctx_base.py", line 3, in <module>
    from .libmp.backend import xrange
  File "/home/$HOME/miniconda3/envs/mpmath/lib/python3.7/site-packages/mpmath/libmp/__init__.py", line 17, in <module>
    from .libmpc import (mpc_one, mpc_zero, mpc_two, mpc_half,       File "/home/$HOME/miniconda3/envs/mpmath/lib/python3.7/site-packages/mpmath/libmp/libmpc.py", line 23, in <module>
    from .libelefun import (\
  File "/home/$HOME/miniconda3/envs/mpmath/lib/python3.7/site-packages/mpmath/libmp/libelefun.py", line 76, in <module>
    cache_prec_steps += [min(2**k,LOG_TAYLOR_PREC)+20] * 2**(k-1)  MemoryError
comment
created
brianmcconnel
username_0
Does this matter to anyone?  Seems like a bit dangerous operation to have right at import.  Also there isn't a good error message or trail of breadcrumbs to lead a developer to a solution.
comment
created
tjb900
username_1
53
```

and so the code snippet above goes on to try and create a list of length 2**52 or so.
comment
created
ngoldbaum
username_2
I opened https://github.com/conda-forge/gmp-feedstock/issues/17 which will hopefully get the attention of the conda-forge maintainers.
comment
created
isuruf
username_3
I can't reproduce this. What's your CPU model?
comment
created
isuruf
username_3
Also, try running the following C program,
```C
#include <gmp.h>
#include <stdio.h>

int main() {
  mpz_t m;
  mpz_init_set_si(m, 2500);
  size_t d = mpz_sizeinbase(m, 2);
  printf("%ld\n", d);
  mpz_clear(m);
}
```
comment
created
tjb900
username_1
```
processor       : 0
vendor_id       : GenuineIntel
cpu family      : 6
model           : 44
model name      : Intel(R) Xeon(R) CPU           X5650  @ 2.67GHz
stepping        : 2
microcode       : 0x1a
cpu MHz         : 1729.000
cache size      : 12288 KB
physical id     : 0
siblings        : 12
core id         : 0
cpu cores       : 6
apicid          : 0
initial apicid  : 0
fpu             : yes
fpu_exception   : yes
cpuid level     : 11
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid dca sse4_1 sse4_2 popcnt aes lahf_lm epb tpr_shadow vnmi flexpriority ept vpid dtherm ida arat
bogomips        : 5327.73
clflush size    : 64
cache_alignment : 64
address sizes   : 40 bits physical, 48 bits virtual
power management:
```

The C code snippet above also outputs 53 when using the library from the 6.2.0 build:

```
# 6.1.2
(mpmath_debug) xxx$ LD_LIBRARY_PATH=xxx/envs/mpmath_debug2/lib ./testmp
12
# 6.2.0
(mpmath_debug) xxx$ LD_LIBRARY_PATH=xxx/envs/mpmath_debug/lib ./testmp
53
```

And interestingly:

```
(mpmath_debug) xxx$ python -c "import gmpy2 as gmpy; print(gmpy.mpz(2500));"
Illegal instruction
```
comment
created
isuruf
username_3
@username_1, can you report this to the GMP mailing list? https://gmplib.org/manual/Reporting-Bugs.html (There are some information required mentioned in the page above when making a bug report)

Can you also try building GMP from source and installing it into the conda environment? Here are the options we use to build GMP https://github.com/conda-forge/gmp-feedstock/blob/master/recipe/build.sh.
comment
created
ngoldbaum
username_2
I can't reproduce this either on a AMD Ryzen 2970WX. If anyone has an intel CPU (particularly a westmere one like @username_1) it would be great if you could reproduce this and report it upstream.
comment
created
jjhelmus
username_4
On a Wolfdale processor:

```
(base) jhelmus@geezer:~$ lscpu 
Architecture:        x86_64
CPU op-mode(s):      32-bit, 64-bit
Byte Order:          Little Endian
CPU(s):              2
On-line CPU(s) list: 0,1
Thread(s) per core:  1
Core(s) per socket:  2
Socket(s):           1
NUMA node(s):        1
Vendor ID:           GenuineIntel
CPU family:          6
Model:               23
Model name:          Intel(R) Core(TM)2 Duo CPU     E8400  @ 3.00GHz
Stepping:            10
CPU MHz:             1994.972
CPU max MHz:         3000.0000
CPU min MHz:         1998.0000
BogoMIPS:            5984.79
Virtualization:      VT-x
L1d cache:           32K
L1i cache:           32K
L2 cache:            6144K
NUMA node0 CPU(s):   0,1
Flags:               fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx lm constant_tsc arch_perfmon pebs bts rep_good nopl cpuid aperfmperf pni dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm sse4_1 xsave lahf_lm pti tpr_shadow vnmi flexpriority dtherm
```

I'm seeing the same behavior as @username_1  from the test program:

```
(mpmath_dev_620) jhelmus@geezer:~$ $CC -I ${CONDA_PREFIX}/include/ -L ${CONDA_PREFIX}/lib -l gmp ~/test.c 
(mpmath_dev_620) jhelmus@geezer:~$ ./a.out 
53
(mpmath_dev_612) jhelmus@geezer:~$ $CC -I ${CONDA_PREFIX}/include/ -L ${CONDA_PREFIX}/lib -l gmp ~/test.c 
(mpmath_dev_612) jhelmus@geezer:~$ ./a.out 
12
```

The 6.2.0 package does produce an illegal instruction, 6.1.2 does not:
```
(mpmath_dev_620) jhelmus@geezer:~$ python -c "import gmpy2 as gmpy; print(gmpy.mpz(2500));"
Illegal instruction (core dumped)
(mpmath_dev_612) jhelmus@geezer:~$ python -c "import gmpy2 as gmpy; print(gmpy.mpz(2500));"
2500
```
comment
created
jjhelmus
username_4
Build `gmp` package from the conda-forge recipe on the E8400 machine produces a package that gives the expected results:

```
(mpmath_dev_local) jhelmus@geezer:~$ $CC -I ${CONDA_PREFIX}/include/ -L ${CONDA_PREFIX}/lib -l gmp ~/test.c
(mpmath_dev_local) jhelmus@geezer:~$ ./a.out
12
(mpmath_dev_local) jhelmus@geezer:~$ python -c "import gmpy2 as gmpy; print(gmpy.mpz(2500));"
2500
```
comment
created
jjhelmus
username_4
For reference here is the build log for gmp: [gmp_build_log.tar.gz](https://github.com/fredrik-johansson/mpmath/files/4135621/gmp_build_log.tar.gz)
comment
created
isuruf
username_3
This was an issue with how we were building GMP at conda-forge and has been fixed now. Updating gmp should fix this issue.
comment
created
jjhelmus
username_4
I can confirm that the 6.2.0 build 1 packages (conda-forge/linux-64::gmp-6.2.0-he1b5a44_1) give the expect results for the test program and the python test.
comment
created
brianmcconnel
username_0
I can also confirm that this now works after updating to the latest version of gmp=6.2.0
gmp                       6.2.0                he1b5a44_1    conda-forge

I was able to get it to work on two intel cpus:
An old Intel(R) Xeon(R) CPU E5-2643 0 @ 3.30GHz
And a newer Intel(R) Core(TM) i5-10210U CPU @ 1.60GHz

Thanks for the help in getting to the bottom of the issue.
comment
closed
brianmcconnel
username_0
issue