Skip to content

Commit b0c5927

Browse files
Merge pull request #46 from IntelPython/update-code-base-for-newer-numpy
Update code for newer NumPy
2 parents 64d9fdd + f0685e0 commit b0c5927

File tree

7 files changed

+792
-765
lines changed

7 files changed

+792
-765
lines changed

mkl_random/mklrand.pyx

Lines changed: 693 additions & 598 deletions
Large diffs are not rendered by default.

mkl_random/src/mkl_distributions.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (c) 2017-2021, Intel Corporation
2+
Copyright (c) 2017-2024, Intel Corporation
33
44
Redistribution and use in source and binary forms, with or without
55
modification, are permitted provided that the following conditions are met:
@@ -1510,7 +1510,7 @@ void irk_discrete_uniform_long_vec(irk_state *state, npy_intp len, long *res, co
15101510
int *buf = (int *)mkl_malloc(len * sizeof(int), 64);
15111511
assert(buf != nullptr);
15121512

1513-
err = viRngUniform(VSL_RNG_METHOD_UNIFORM_STD, state->stream, len, buf, -1, (const int)max);
1513+
err = viRngUniform(VSL_RNG_METHOD_UNIFORM_STD, state->stream, len, buf, -1, (int)max);
15141514
assert(err == VSL_STATUS_OK);
15151515

15161516
DIST_PRAGMA_VECTOR
@@ -1626,7 +1626,7 @@ void irk_rand_bool_vec(irk_state *state, npy_intp len, npy_bool *res, const npy_
16261626
buf = (int *)mkl_malloc(len * sizeof(int), 64);
16271627
assert(buf != nullptr);
16281628

1629-
err = viRngUniform(VSL_RNG_METHOD_UNIFORM_STD, state->stream, len, buf, (const int)lo, (const int)hi + 1);
1629+
err = viRngUniform(VSL_RNG_METHOD_UNIFORM_STD, state->stream, len, buf, (int)lo, (int)hi + 1);
16301630
assert(err == VSL_STATUS_OK);
16311631

16321632
DIST_PRAGMA_VECTOR
@@ -1666,7 +1666,7 @@ void irk_rand_uint8_vec(irk_state *state, npy_intp len, npy_uint8 *res, const np
16661666
buf = (int *)mkl_malloc(len * sizeof(int), 64);
16671667
assert(buf != nullptr);
16681668

1669-
err = viRngUniform(VSL_RNG_METHOD_UNIFORM_STD, state->stream, len, buf, (const int)lo, (const int)hi + 1);
1669+
err = viRngUniform(VSL_RNG_METHOD_UNIFORM_STD, state->stream, len, buf, (int)lo, (int)hi + 1);
16701670
assert(err == VSL_STATUS_OK);
16711671

16721672
DIST_PRAGMA_VECTOR
@@ -1706,7 +1706,7 @@ void irk_rand_int8_vec(irk_state *state, npy_intp len, npy_int8 *res, const npy_
17061706
buf = (int *)mkl_malloc(len * sizeof(int), 64);
17071707
assert(buf != nullptr);
17081708

1709-
err = viRngUniform(VSL_RNG_METHOD_UNIFORM_STD, state->stream, len, buf, (const int)lo, (const int)hi + 1);
1709+
err = viRngUniform(VSL_RNG_METHOD_UNIFORM_STD, state->stream, len, buf, (int)lo, (int)hi + 1);
17101710
assert(err == VSL_STATUS_OK);
17111711

17121712
DIST_PRAGMA_VECTOR
@@ -1746,7 +1746,7 @@ void irk_rand_uint16_vec(irk_state *state, npy_intp len, npy_uint16 *res, const
17461746
buf = (int *)mkl_malloc(len * sizeof(int), 64);
17471747
assert(buf != nullptr);
17481748

1749-
err = viRngUniform(VSL_RNG_METHOD_UNIFORM_STD, state->stream, len, buf, (const int)lo, (const int)hi + 1);
1749+
err = viRngUniform(VSL_RNG_METHOD_UNIFORM_STD, state->stream, len, buf, (int)lo, (int)hi + 1);
17501750
assert(err == VSL_STATUS_OK);
17511751

17521752
DIST_PRAGMA_VECTOR
@@ -1786,7 +1786,7 @@ void irk_rand_int16_vec(irk_state *state, npy_intp len, npy_int16 *res, const np
17861786
buf = (int *)mkl_malloc(len * sizeof(int), 64);
17871787
assert(buf != nullptr);
17881788

1789-
err = viRngUniform(VSL_RNG_METHOD_UNIFORM_STD, state->stream, len, buf, (const int)lo, (const int)hi + 1);
1789+
err = viRngUniform(VSL_RNG_METHOD_UNIFORM_STD, state->stream, len, buf, (int)lo, (int)hi + 1);
17901790
assert(err == VSL_STATUS_OK);
17911791

17921792
DIST_PRAGMA_VECTOR
@@ -1831,7 +1831,7 @@ void irk_rand_uint32_vec(irk_state *state, npy_intp len, npy_uint32 *res, const
18311831
if (lo)
18321832
shft++;
18331833

1834-
err = viRngUniform(VSL_RNG_METHOD_UNIFORM_STD, state->stream, len, (int *)res, (const int)(lo - shft), (const int)(hi - shft + 1U));
1834+
err = viRngUniform(VSL_RNG_METHOD_UNIFORM_STD, state->stream, len, (int *)res, (int)(lo - shft), (int)(hi - shft + 1U));
18351835
assert(err == VSL_STATUS_OK);
18361836

18371837
DIST_PRAGMA_VECTOR
@@ -1840,7 +1840,7 @@ void irk_rand_uint32_vec(irk_state *state, npy_intp len, npy_uint32 *res, const
18401840
}
18411841
else
18421842
{
1843-
err = viRngUniform(VSL_RNG_METHOD_UNIFORM_STD, state->stream, len, (int *)res, (const int)lo, (const int)hi + 1);
1843+
err = viRngUniform(VSL_RNG_METHOD_UNIFORM_STD, state->stream, len, (int *)res, (int)lo, (int)hi + 1);
18441844
assert(err == VSL_STATUS_OK);
18451845
}
18461846
}
@@ -1873,7 +1873,7 @@ void irk_rand_int32_vec(irk_state *state, npy_intp len, npy_int32 *res, const np
18731873
}
18741874
else
18751875
{
1876-
err = viRngUniform(VSL_RNG_METHOD_UNIFORM_STD, state->stream, len, (int *)res, (const int)lo, (const int)hi + 1);
1876+
err = viRngUniform(VSL_RNG_METHOD_UNIFORM_STD, state->stream, len, (int *)res, (int)lo, (int)hi + 1);
18771877
assert(err == VSL_STATUS_OK);
18781878
}
18791879
}
@@ -1921,7 +1921,7 @@ void irk_rand_uint64_vec(irk_state *state, npy_intp len, npy_uint64 *res, const
19211921
int *buf = (int *)mkl_malloc(len * sizeof(int), 64);
19221922
assert(buf != nullptr);
19231923

1924-
err = viRngUniform(VSL_RNG_METHOD_UNIFORM_STD, state->stream, len, buf, 0, (const int)rng);
1924+
err = viRngUniform(VSL_RNG_METHOD_UNIFORM_STD, state->stream, len, buf, 0, (int)rng);
19251925
assert(err == VSL_STATUS_OK);
19261926

19271927
DIST_PRAGMA_VECTOR

mkl_random/src/numpy.pxd

Lines changed: 0 additions & 152 deletions
This file was deleted.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
Copyright (c) 2024-2024, Intel Corporation
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are met:
6+
7+
* Redistributions of source code must retain the above copyright notice,
8+
this list of conditions and the following disclaimer.
9+
* Redistributions in binary form must reproduce the above copyright
10+
notice, this list of conditions and the following disclaimer in the
11+
documentation and/or other materials provided with the distribution.
12+
* Neither the name of Intel Corporation nor the names of its contributors
13+
may be used to endorse or promote products derived from this software
14+
without specific prior written permission.
15+
16+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
*/
27+
28+
#include "Python.h"
29+
#include "numpy/arrayobject.h"
30+
31+
/* This header file is a work-around for issue
32+
* https://github.com/numpy/numpy/issues/26990
33+
*
34+
* It is included once in mklrandom.pyx
35+
*
36+
* The work-around is needed to support building with
37+
* NumPy < 2.0.0
38+
*
39+
* Once building transitions to using NumPy 2.0 only
40+
* this file can be removed and corresponding changes
41+
* in mklrand.pyx can be applied to always use
42+
* `PyArray_MultiIter_SIZE`, PyArray_MultiIter_NDIM`,
43+
* and `PyArray_MultiIter_DIMS`.
44+
*/
45+
46+
#define WORKAROUND_NEEDED (defined(NPY_2_0_API_VERSION) && (NPY_API_VERSION >= NPY_2_0_API_VERSION))
47+
48+
#if !WORKAROUND_NEEDED
49+
typedef struct {
50+
PyObject_HEAD
51+
int numiter;
52+
npy_intp size;
53+
npy_intp index;
54+
int nd;
55+
npy_intp dimensions[32];
56+
void **iters;
57+
} multi_iter_proxy_st;
58+
#endif
59+
60+
npy_intp workaround_PyArray_MultiIter_SIZE(PyArrayMultiIterObject *multi) {
61+
#if WORKAROUND_NEEDED
62+
return PyArray_MultiIter_SIZE(multi);
63+
#else
64+
return ((multi_iter_proxy_st *)(multi))->size;
65+
#endif
66+
}
67+
68+
int workaround_PyArray_MultiIter_NDIM(PyArrayMultiIterObject *multi) {
69+
#if WORKAROUND_NEEDED
70+
return PyArray_MultiIter_NDIM(multi);
71+
#else
72+
return ((multi_iter_proxy_st *)(multi))->nd;
73+
#endif
74+
}
75+
76+
npy_intp* workaround_PyArray_MultiIter_DIMS(PyArrayMultiIterObject *multi) {
77+
#if WORKAROUND_NEEDED
78+
return PyArray_MultiIter_DIMS(multi);
79+
#else
80+
return (((multi_iter_proxy_st *)(multi))->dimensions);
81+
#endif
82+
}

mkl_random/src/randomkit.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (c) 2017-2019, Intel Corporation
2+
Copyright (c) 2017-2024, Intel Corporation
33
44
Redistribution and use in source and binary forms, with or without
55
modification, are permitted provided that the following conditions are met:

mkl_random/tests/test_random.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,8 @@ def test_randomdist_normal(randomdist):
804804
np.testing.assert_allclose(actual, desired, atol=1e-8, rtol=1e-8)
805805

806806
rnd.seed(randomdist.seed, brng=randomdist.brng)
807-
actual = rnd.normal(loc=.123456789, scale=2.0, size=(3, 2), method="BoxMuller2")
807+
workaround = rnd.normal(loc=.123456789, scale=2.0, size=(4, 2), method="BoxMuller2")
808+
actual = workaround[:3,:]
808809
desired = np.array([[0.16673479781277187, 0.48153966449249175],
809810
[-3.4809986872165952, -0.8101190082826486],
810811
[-0.051937610825354905, 2.4088402362484342]])
@@ -902,7 +903,8 @@ def test_randomdist_standard_normal(randomdist):
902903
np.testing.assert_allclose(actual, desired, atol=1e-7, rtol=1e-10)
903904

904905
rnd.seed(randomdist.seed, brng=randomdist.brng)
905-
actual = rnd.standard_normal(size=(3, 2), method='BoxMuller2')
906+
workaround = rnd.standard_normal(size=(4, 2), method='BoxMuller2')
907+
actual = workaround[:3, :]
906908
desired = np.array([[0.021639004406385935, 0.17904143774624587],
907909
[-1.8022277381082976, -0.4667878986413243],
908910
[-0.08769719991267745, 1.1426917236242171]])

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def extensions():
109109
depends = [
110110
os.path.join("mkl_random", "src", "mkl_distributions.hpp"),
111111
os.path.join("mkl_random", "src", "randomkit.h"),
112-
os.path.join("mkl_random", "src", "numpy.pxd")
112+
os.path.join("mkl_random", "src", "numpy_multiiter_workaround.h")
113113
],
114114
include_dirs = [os.path.join("mkl_random", "src"), np.get_include()] + mkl_include_dirs,
115115
libraries = libs,

0 commit comments

Comments
 (0)