Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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 44 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
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 Jul 29, 2024
d9005a9
sleef based quadprecision
SwayamInSync Jul 29, 2024
4702764
fixing numpy absolute include path
SwayamInSync Jul 31, 2024
6ec99a5
fixing numpy absolute include path
SwayamInSync Jul 31, 2024
a8b2599
adding quad precision support with sleef
SwayamInSync Aug 1, 2024
d6bdb99
fixing sleef linking issues in meson
SwayamInSync Aug 2, 2024
8ff6e0c
adding NPY_SAME_KIND_CASTING
SwayamInSync Aug 2, 2024
3559232
fixing NPY_SAME_CAST
SwayamInSync Aug 2, 2024
a481648
fixing quad precsion printing issue
SwayamInSync Aug 3, 2024
a33ea7f
fixing quad precsion printing issue
SwayamInSync Aug 3, 2024
220e0ae
removing hardcoded paths from meson build
SwayamInSync Aug 5, 2024
4dfe490
adding CI changes
SwayamInSync Aug 7, 2024
64d1e9e
adding quaddtype branch in CI
SwayamInSync Aug 7, 2024
ae9d8b8
fixing sleefquad dep in CI
SwayamInSync Aug 7, 2024
28c205b
fixing cmake sleefquad dep in CI
SwayamInSync Aug 7, 2024
7dd8611
fixing sleef compiling issue with FPIC
SwayamInSync Aug 7, 2024
d0da3e5
adding Build position-independent
SwayamInSync Aug 7, 2024
9077bec
adding Build position-independent
SwayamInSync Aug 7, 2024
982fba2
adding Build position-independent
SwayamInSync Aug 7, 2024
5a3ae94
adding Build position-independent
SwayamInSync Aug 7, 2024
18b537a
fixing tests
SwayamInSync Aug 7, 2024
f53c130
fixing test namings
SwayamInSync Aug 7, 2024
77e9967
debugging sleef installation issues
SwayamInSync Aug 7, 2024
375a621
debugging sleef installation issues
SwayamInSync Aug 7, 2024
820588d
fixing sleef linking issues
SwayamInSync Aug 7, 2024
53396db
fixing sleef linking issues
SwayamInSync Aug 7, 2024
e91104e
fixing sleef linking issues
SwayamInSync Aug 7, 2024
9639628
fixing sleef linking issues
SwayamInSync Aug 7, 2024
bef1cc2
Update CI branch
SwayamInSync Aug 7, 2024
4f242fd
fixing pandas meson==0.13.1 dependency issue as --no-build-isolation
SwayamInSync Aug 8, 2024
58fbc6f
merging CI fixes
SwayamInSync Aug 9, 2024
96ee075
adding ufuncs
SwayamInSync Aug 9, 2024
7f7ebc3
added ufuncs, castings and scalar ops
SwayamInSync Aug 10, 2024
ffaa617
more castings and ufuncs
SwayamInSync Aug 10, 2024
299568b
updated meson.build
SwayamInSync Aug 11, 2024
d13f863
adding more scalar tests
SwayamInSync Aug 11, 2024
78a6931
defining scalar ops in struct sequence
SwayamInSync Aug 13, 2024
6fbe989
resolving fixing unaligned loop and casting issues
SwayamInSync Aug 15, 2024
6e124e3
resolving error catching and removing initial data init for dtype
SwayamInSync Aug 15, 2024
63dc446
added destructor function for tp_dealloc
SwayamInSync Aug 15, 2024
d2e1ded
changing quad->quad cast to NPY_NO_CASTING from NPY_SAME_CASTING
SwayamInSync Aug 16, 2024
96270bc
added dtype promoter functions
SwayamInSync Aug 16, 2024
fb04515
fixed memory issues and more ufuncs
SwayamInSync Aug 16, 2024
c76cf36
fixed destructor for bad memory access
SwayamInSync Aug 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
adding ufuncs
  • Loading branch information
SwayamInSync committed Aug 9, 2024
commit 96ee075cdc6058d0363612b678c22433a4544cf7
2 changes: 1 addition & 1 deletion quaddtype/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ py = py_mod.find_installation()

c = meson.get_compiler('c')

sleef_dep = c.find_library('sleef')
sleef_dep = c.find_library('sleef', dirs:['/usr/local/lib'])
sleefquad_dep = c.find_library('sleefquad')

incdir_numpy = run_command(py,
Expand Down
33 changes: 33 additions & 0 deletions quaddtype/quaddtype/src/ops.hpp
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;
}
224 changes: 224 additions & 0 deletions quaddtype/quaddtype/src/umath.cpp
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>
Copy link
Member

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 the context. There are some examples in NumPy that do this.

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);

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];
}

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);
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;

}
15 changes: 15 additions & 0 deletions quaddtype/quaddtype/src/umath.h
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.
111 changes: 111 additions & 0 deletions temp.py
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)