-
-
Notifications
You must be signed in to change notification settings - Fork 8
Adding Quaddtype #98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Adding Quaddtype #98
Changes from 1 commit
Commits
Show all changes
44 commits
Select commit
Hold shift + click to select a range
ccfc64a
initial commit, adding Float128 dtype
SwayamInSync d9005a9
sleef based quadprecision
SwayamInSync 4702764
fixing numpy absolute include path
SwayamInSync 6ec99a5
fixing numpy absolute include path
SwayamInSync a8b2599
adding quad precision support with sleef
SwayamInSync d6bdb99
fixing sleef linking issues in meson
SwayamInSync 8ff6e0c
adding NPY_SAME_KIND_CASTING
SwayamInSync 3559232
fixing NPY_SAME_CAST
SwayamInSync a481648
fixing quad precsion printing issue
SwayamInSync a33ea7f
fixing quad precsion printing issue
SwayamInSync 220e0ae
removing hardcoded paths from meson build
SwayamInSync 4dfe490
adding CI changes
SwayamInSync 64d1e9e
adding quaddtype branch in CI
SwayamInSync ae9d8b8
fixing sleefquad dep in CI
SwayamInSync 28c205b
fixing cmake sleefquad dep in CI
SwayamInSync 7dd8611
fixing sleef compiling issue with FPIC
SwayamInSync d0da3e5
adding Build position-independent
SwayamInSync 9077bec
adding Build position-independent
SwayamInSync 982fba2
adding Build position-independent
SwayamInSync 5a3ae94
adding Build position-independent
SwayamInSync 18b537a
fixing tests
SwayamInSync f53c130
fixing test namings
SwayamInSync 77e9967
debugging sleef installation issues
SwayamInSync 375a621
debugging sleef installation issues
SwayamInSync 820588d
fixing sleef linking issues
SwayamInSync 53396db
fixing sleef linking issues
SwayamInSync e91104e
fixing sleef linking issues
SwayamInSync 9639628
fixing sleef linking issues
SwayamInSync bef1cc2
Update CI branch
SwayamInSync 4f242fd
fixing pandas meson==0.13.1 dependency issue as --no-build-isolation
SwayamInSync 58fbc6f
merging CI fixes
SwayamInSync 96ee075
adding ufuncs
SwayamInSync 7f7ebc3
added ufuncs, castings and scalar ops
SwayamInSync ffaa617
more castings and ufuncs
SwayamInSync 299568b
updated meson.build
SwayamInSync d13f863
adding more scalar tests
SwayamInSync 78a6931
defining scalar ops in struct sequence
SwayamInSync 6fbe989
resolving fixing unaligned loop and casting issues
SwayamInSync 6e124e3
resolving error catching and removing initial data init for dtype
SwayamInSync 63dc446
added destructor function for tp_dealloc
SwayamInSync d2e1ded
changing quad->quad cast to NPY_NO_CASTING from NPY_SAME_CASTING
SwayamInSync 96270bc
added dtype promoter functions
SwayamInSync fb04515
fixed memory issues and more ufuncs
SwayamInSync c76cf36
fixed destructor for bad memory access
SwayamInSync File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
adding ufuncs
- Loading branch information
commit 96ee075cdc6058d0363612b678c22433a4544cf7
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include <sleef.h> | ||
#include <sleefquad.h> | ||
|
||
typedef int (*unary_op_def)(Sleef_quad *, Sleef_quad *); | ||
|
||
static inline int | ||
quad_negative(Sleef_quad *op, Sleef_quad *out) | ||
{ | ||
*out = Sleef_negq1(*op); | ||
return 0; | ||
} | ||
|
||
static inline int | ||
quad_absolute(Sleef_quad *op, Sleef_quad *out) | ||
{ | ||
*out = Sleef_fabsq1(*op); | ||
return 0; | ||
} | ||
|
||
// binary ops | ||
typedef int (*binop_def)(Sleef_quad *, Sleef_quad *, Sleef_quad *); | ||
|
||
static inline int quad_add(Sleef_quad *out, Sleef_quad *in1, Sleef_quad *in2) | ||
{ | ||
*out = Sleef_addq1_u05(*in1, *in2); | ||
return 0; | ||
} | ||
|
||
static inline int quad_sub(Sleef_quad *out, Sleef_quad *in1, Sleef_quad *in2) | ||
{ | ||
*out = Sleef_subq1_u05(*in1, *in2); | ||
return 0; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,224 @@ | ||
#define PY_ARRAY_UNIQUE_SYMBOL QuadPrecType_ARRAY_API | ||
#define PY_UFUNC_UNIQUE_SYMBOL QuadPrecType_UFUNC_API | ||
#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION | ||
#define NPY_TARGET_VERSION NPY_2_0_API_VERSION | ||
#define NO_IMPORT_ARRAY | ||
#define NO_IMPORT_UFUNC | ||
|
||
extern "C" { | ||
#include <Python.h> | ||
|
||
#include "numpy/arrayobject.h" | ||
#include "numpy/ndarraytypes.h" | ||
#include "numpy/ufuncobject.h" | ||
|
||
#include "numpy/dtype_api.h" | ||
} | ||
|
||
#include "scalar.h" | ||
#include "dtype.h" | ||
#include "umath.h" | ||
#include "ops.hpp" | ||
|
||
template <unary_op_def unary_op> | ||
int quad_generic_unary_op_strided_loop(PyArrayMethod_Context *context, | ||
char *const data[], npy_intp const dimensions[], | ||
npy_intp const strides[], NpyAuxData *auxdata) | ||
{ | ||
npy_intp N = dimensions[0]; | ||
char *in_ptr = data[0]; | ||
char *out_ptr = data[1]; | ||
npy_intp in_stride = strides[0]; | ||
npy_intp out_stride = strides[1]; | ||
|
||
while (N--) | ||
{ | ||
unary_op((Sleef_quad *)in_ptr, (Sleef_quad *)out_ptr); | ||
in_ptr += in_stride; | ||
out_ptr += out_stride; | ||
} | ||
return 0; | ||
} | ||
|
||
static NPY_CASTING | ||
quad_unary_op_resolve_descriptors(PyObject *self, | ||
PyArray_DTypeMeta *dtypes[], QuadPrecDTypeObject *given_descrs[], | ||
QuadPrecDTypeObject *loop_descrs[], npy_intp *unused) | ||
{ | ||
Py_INCREF(given_descrs[0]); | ||
loop_descrs[0] = given_descrs[0]; | ||
|
||
if (given_descrs[1] == NULL) { | ||
Py_INCREF(given_descrs[0]); | ||
loop_descrs[1] = given_descrs[0]; | ||
return NPY_NO_CASTING; | ||
} | ||
Py_INCREF(given_descrs[1]); | ||
loop_descrs[1] = given_descrs[1]; | ||
|
||
return NPY_NO_CASTING; // Quad precision is always the same precision | ||
} | ||
|
||
template <unary_op_def unary_op> | ||
int create_quad_unary_ufunc(PyObject *numpy, const char *ufunc_name) | ||
{ | ||
PyObject *ufunc = PyObject_GetAttrString(numpy, ufunc_name); | ||
if (ufunc == NULL) { | ||
return -1; | ||
} | ||
|
||
PyArray_DTypeMeta *dtypes[2] = { | ||
&QuadPrecDType, &QuadPrecDType}; | ||
|
||
PyType_Slot slots[] = { | ||
{NPY_METH_resolve_descriptors, (void *)&quad_unary_op_resolve_descriptors}, | ||
{NPY_METH_strided_loop, (void *)&quad_generic_unary_op_strided_loop<unary_op>}, | ||
{0, NULL} | ||
}; | ||
|
||
PyArrayMethod_Spec Spec = { | ||
.name = "quad_unary_op", | ||
.nin = 1, | ||
.nout = 1, | ||
.casting = NPY_NO_CASTING, | ||
.flags = (NPY_ARRAYMETHOD_FLAGS)0, | ||
.dtypes = dtypes, | ||
.slots = slots, | ||
}; | ||
|
||
if (PyUFunc_AddLoopFromSpec(ufunc, &Spec) < 0) { | ||
return -1; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
int init_quad_unary_ops(PyObject *numpy) | ||
{ | ||
if (create_quad_unary_ufunc<quad_negative>(numpy, "negative") < 0) { | ||
return -1; | ||
} | ||
if (create_quad_unary_ufunc<quad_absolute>(numpy, "absolute") < 0) { | ||
return -1; | ||
} | ||
return 0; | ||
} | ||
|
||
// Binary ufuncs | ||
|
||
template <binop_def binop> | ||
int quad_generic_binop_strided_loop(PyArrayMethod_Context *context, | ||
char *const data[], npy_intp const dimensions[], | ||
npy_intp const strides[], NpyAuxData *auxdata) | ||
{ | ||
npy_intp N = dimensions[0]; | ||
char *in1_ptr = data[0], *in2_ptr = data[1]; | ||
char *out_ptr = data[2]; | ||
npy_intp in1_stride = strides[0]; | ||
npy_intp in2_stride = strides[1]; | ||
npy_intp out_stride = strides[2]; | ||
|
||
while (N--) { | ||
binop((Sleef_quad *)out_ptr, (Sleef_quad *)in1_ptr, (Sleef_quad *)in2_ptr); | ||
SwayamInSync marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
in1_ptr += in1_stride; | ||
in2_ptr += in2_stride; | ||
out_ptr += out_stride; | ||
} | ||
return 0; | ||
} | ||
|
||
static NPY_CASTING | ||
quad_binary_op_resolve_descriptors(PyObject *self, | ||
PyArray_DTypeMeta *dtypes[], QuadPrecDTypeObject *given_descrs[], | ||
QuadPrecDTypeObject *loop_descrs[], npy_intp *unused) | ||
{ | ||
Py_INCREF(given_descrs[0]); | ||
loop_descrs[0] = given_descrs[0]; | ||
Py_INCREF(given_descrs[1]); | ||
loop_descrs[1] = given_descrs[1]; | ||
|
||
if (given_descrs[2] == NULL) { | ||
Py_INCREF(given_descrs[0]); | ||
loop_descrs[2] = given_descrs[0]; | ||
} | ||
else { | ||
Py_INCREF(given_descrs[2]); | ||
loop_descrs[2] = given_descrs[2]; | ||
SwayamInSync marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
return NPY_NO_CASTING; // Quad precision is always the same precision | ||
} | ||
|
||
// todo: skipping the promoter for now, since same type operation will be requried | ||
|
||
template <binop_def binop> | ||
int create_quad_binary_ufunc(PyObject *numpy, const char *ufunc_name) | ||
{ | ||
PyObject *ufunc = PyObject_GetAttrString(numpy, ufunc_name); | ||
SwayamInSync marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (ufunc == NULL) { | ||
return -1; | ||
} | ||
|
||
PyArray_DTypeMeta *dtypes[3] = { | ||
&QuadPrecDType, &QuadPrecDType, &QuadPrecDType}; | ||
|
||
PyType_Slot slots[] = { | ||
{NPY_METH_resolve_descriptors, | ||
(void *)&quad_binary_op_resolve_descriptors}, | ||
{NPY_METH_strided_loop, | ||
(void *)&quad_generic_binop_strided_loop<binop>}, | ||
{0, NULL} | ||
}; | ||
|
||
PyArrayMethod_Spec Spec = { | ||
.name = "quad_binop", | ||
.nin = 2, | ||
.nout = 1, | ||
.casting = NPY_NO_CASTING, | ||
.flags = (NPY_ARRAYMETHOD_FLAGS)0, | ||
.dtypes = dtypes, | ||
.slots = slots, | ||
}; | ||
|
||
if (PyUFunc_AddLoopFromSpec(ufunc, &Spec) < 0) { | ||
return -1; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
int init_quad_binary_ops(PyObject *numpy) | ||
{ | ||
if (create_quad_binary_ufunc<quad_add>(numpy, "add") < 0) { | ||
return -1; | ||
} | ||
if (create_quad_binary_ufunc<quad_sub>(numpy, "subtract") < 0) { | ||
return -1; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
int init_quad_umath(void) | ||
{ | ||
PyObject * numpy = PyImport_ImportModule("numpy"); | ||
if (!numpy) | ||
return -1; | ||
|
||
if (init_quad_unary_ops(numpy) < 0) { | ||
goto err; | ||
} | ||
|
||
if (init_quad_binary_ops(numpy) < 0) { | ||
goto err; | ||
} | ||
|
||
Py_DECREF(numpy); | ||
return 0; | ||
|
||
err: | ||
Py_DECREF(numpy); | ||
return -1; | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#ifndef _QUADDTYPE_UMATH_H | ||
#define _QUADDTYPE_UMATH_H | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
int | ||
init_quad_umath(void); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif |
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import numpy as np | ||
from quaddtype import QuadPrecDType, QuadPrecision | ||
import matplotlib.pyplot as plt | ||
|
||
def get_color(t, interior_t): | ||
epsilon = QuadPrecision("1e-10") | ||
|
||
if abs(t - QuadPrecision(1.0)) < epsilon: | ||
value = int(255 * float(interior_t)) | ||
return np.array([value, value, value], dtype=np.uint8) | ||
|
||
t = np.power(t, 0.5) | ||
t = np.mod(t * 20, 1.0) | ||
|
||
if t < 0.16: | ||
return np.array([0, int(255 * (t / 0.16)), int(128 + 127 * (t / 0.16))], dtype=np.uint8) | ||
elif t < 0.33: | ||
return np.array([0, 255, int(255 * (1 - (t - 0.16) / 0.17))], dtype=np.uint8) | ||
elif t < 0.5: | ||
return np.array([int(255 * ((t - 0.33) / 0.17)), 255, 0], dtype=np.uint8) | ||
elif t < 0.66: | ||
return np.array([255, int(255 * (1 - (t - 0.5) / 0.16)), 0], dtype=np.uint8) | ||
elif t < 0.83: | ||
return np.array([255, 0, int(255 * ((t - 0.66) / 0.17))], dtype=np.uint8) | ||
else: | ||
return np.array([int(255 * (1 - (t - 0.83) / 0.17)), 0, int(128 * ((t - 0.83) / 0.17))], dtype=np.uint8) | ||
|
||
def iterate_and_compute_derivatives(c, max_iter): | ||
z = 0 | ||
dz = 1 | ||
dc = 0 | ||
dzdz = 0 | ||
|
||
for _ in range(max_iter): | ||
dzdz = 2 * (z * dzdz + dz * dz) | ||
dz = 2 * z * dz + dc | ||
z = z * z + c | ||
dc = 1 | ||
|
||
return z, dz, dc, dzdz | ||
|
||
def estimate_interior_distance(c, max_iter): | ||
z, dz, dc, dzdz = iterate_and_compute_derivatives(c, max_iter) | ||
|
||
dz_abs_sq = np.abs(dz) ** 2 | ||
numerator = 1 - dz_abs_sq | ||
|
||
denominator = np.abs(dc * dz + dzdz * z * dc) | ||
|
||
return numerator / denominator | ||
|
||
def mandelbrot(c, max_iter, radius2): | ||
z = 0 | ||
for i in range(max_iter): | ||
z = z * z + c | ||
if np.abs(z) ** 2 > radius2: | ||
log_zn = np.log(np.abs(z)) | ||
nu = np.log(log_zn / np.log(2)) / np.log(2) | ||
return i + 1 - nu, z | ||
return max_iter, z | ||
|
||
def mandelbrot_set(width, height, max_iter, center_r, center_i, zoom): | ||
radius = 2.0 | ||
radius2 = radius * radius | ||
zoom_q = 1 / zoom | ||
|
||
x = np.linspace(center_r - radius / zoom, center_r + radius / zoom, width) | ||
y = np.linspace(center_i - radius / zoom, center_i + radius / zoom, height) | ||
c = x[np.newaxis, :] + 1j * y[:, np.newaxis] | ||
|
||
smooth_iter, final_z = np.frompyfunc(lambda c: mandelbrot(c, max_iter, radius2), 1, 2)(c) | ||
smooth_iter = smooth_iter.astype(np.float64) | ||
final_z = final_z.astype(np.complex128) | ||
|
||
img = np.zeros((height, width, 3), dtype=np.uint8) | ||
|
||
interior_mask = smooth_iter == max_iter | ||
interior_c = c[interior_mask] | ||
interior_distance = np.frompyfunc(lambda c: estimate_interior_distance(c, max_iter), 1, 1)(interior_c) | ||
interior_distance = interior_distance.astype(np.float64) | ||
interior_t = interior_distance - np.floor(interior_distance) | ||
|
||
exterior_mask = ~interior_mask | ||
t = smooth_iter[exterior_mask] / max_iter | ||
|
||
interior_colors = np.array(list(map(lambda t: get_color(1.0, t), interior_t))) | ||
exterior_colors = np.array(list(map(lambda t: get_color(t, 0.0), t))) | ||
|
||
img[interior_mask] = interior_colors | ||
img[exterior_mask] = exterior_colors | ||
|
||
return img | ||
|
||
def plot_mandelbrot(width, height, max_iter, center_r, center_i, zoom): | ||
img_array = mandelbrot_set(width, height, max_iter, center_r, center_i, zoom) | ||
|
||
plt.figure(figsize=(10, 10)) | ||
plt.imshow(img_array) | ||
plt.axis('off') | ||
plt.title(f'Mandelbrot Set (zoom: {zoom}, center: {center_r} + {center_i}i, iterations: {max_iter}, dtype: numpy.float64)') | ||
plt.show() | ||
|
||
if __name__ == "__main__": | ||
width = 800 | ||
height = 800 | ||
max_iter = 1000 | ||
center_r = -0.75 | ||
center_i = 0.0 | ||
zoom = 1.0 | ||
|
||
plot_mandelbrot(width, height, max_iter, center_r, center_i, zoom) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than a template it would be better to pass this in with the
static_data
on thecontext
. There are some examples in NumPy that do this.