Skip to content

Commit

Permalink
use fixed int type sizes for portability
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Luca van den Busch committed Jan 31, 2024
1 parent 1b27c45 commit 9e01d10
Show file tree
Hide file tree
Showing 13 changed files with 84 additions and 68 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ jobs:
- name: Build wheels
uses: pypa/cibuildwheel@v2.9.0
env:
CIBW_BUILD: "*manylinux*64"
CIBW_SKIP: cp36* pp* *i686
CIBW_BUILD: "*manylinux*"
CIBW_SKIP: cp36* pp*

- uses: actions/upload-artifact@v3
with:
Expand All @@ -41,8 +41,8 @@ jobs:
- name: Build wheels
uses: pypa/cibuildwheel@v2.9.0
env:
CIBW_BUILD: "*musllinux*64"
CIBW_SKIP: cp36* pp* *i686
CIBW_BUILD: "*musllinux*"
CIBW_SKIP: cp36* pp*

- uses: actions/upload-artifact@v3
with:
Expand All @@ -57,7 +57,7 @@ jobs:
- name: Build wheels
uses: pypa/cibuildwheel@v2.9.0
env:
CIBW_BUILD: "*macosx*64"
CIBW_BUILD: "*macosx*"
CIBW_SKIP: cp36* pp*

- uses: actions/upload-artifact@v3
Expand All @@ -73,8 +73,8 @@ jobs:
- name: Build wheels
uses: pypa/cibuildwheel@v2.9.0
env:
CIBW_BUILD: "*win*64"
CIBW_SKIP: cp36* pp* *win32
CIBW_BUILD: "*win*"
CIBW_SKIP: cp36* pp*

- uses: actions/upload-artifact@v3
with:
Expand Down
13 changes: 7 additions & 6 deletions balltree/balltree.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <Python.h>
#include <numpy/arrayobject.h>

#include <stdint.h>
#include <stdio.h>

#include "point.h"
Expand Down Expand Up @@ -36,7 +37,7 @@ typedef struct {
} NpyIterHelper;

typedef struct {
long size;
int64_t size;
PyArrayObject *xyz_arr;
NpyIterHelper *xyz_iter;
PyArrayObject *weight_arr;
Expand Down Expand Up @@ -389,7 +390,7 @@ static PointBuffer *ptbuf_from_PyObjects(PyObject *xyz_obj, PyObject *weight_obj
inputiterdata_free(data);
return NULL;
}
long idx = 0;
int64_t idx = 0;
double x, y, z;
while (iter_get_next_xyz(data->xyz_iter, &x, &y, &z)) {
buffer->points[idx] = (Point){x, y, z, data->weight_buffer[idx]};
Expand Down Expand Up @@ -444,7 +445,7 @@ static DistHistogram *disthistogram_from_PyObject(PyObject *edges_obj) {
if (edges_arr == NULL) {
return NULL;
}
long num_edges = (long)PyArray_DIM(edges_arr, 0);
int64_t num_edges = (int64_t)PyArray_DIM(edges_arr, 0);
double *edges_buffer = PyArray_DATA(edges_arr);
DistHistogram *hist = hist_new(num_edges, edges_buffer);
Py_DECREF(edges_arr);
Expand All @@ -458,7 +459,7 @@ static PyObject *PyObject_from_disthistogram(DistHistogram *hist) {
return NULL;
}
double *count_buffer = PyArray_DATA(pycount);
for (long i = 0; i < hist->size; ++i) {
for (int64_t i = 0; i < hist->size; ++i) {
count_buffer[i] = hist->sum_weight[i];
}
return pycount;
Expand Down Expand Up @@ -515,7 +516,7 @@ static PyObject *PyBallTree_accumulate_radius(
}
// count neighbours for all inputs
double count = 0.0;
long idx = 0;
int64_t idx = 0;
Point point;
while (iter_get_next_xyz(data->xyz_iter, &point.x, &point.y, &point.z)) {
point.weight = data->weight_buffer[idx];
Expand Down Expand Up @@ -543,7 +544,7 @@ static PyObject *PyBallTree_accumulate_range(
return NULL;
}
// count neighbours for all inputs
long idx = 0;
int64_t idx = 0;
Point point;
while (iter_get_next_xyz(data->xyz_iter, &point.x, &point.y, &point.z)) {
point.weight = data->weight_buffer[idx];
Expand Down
20 changes: 11 additions & 9 deletions include/ballnode.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef BALLNODE_H
#define BALLNODE_H

#include <stdint.h>

#include "point.h"
#include "histogram.h"

Expand All @@ -27,25 +29,25 @@ struct BallNode {
PointSlice data; // is_leaf == 1
};
struct {
unsigned long is_leaf : 1;
unsigned long num_points : 63;
uint64_t is_leaf : 1;
uint64_t num_points : 63;
};
double sum_weight;
};
typedef struct BallNode BallNode;

typedef struct {
long depth;
long num_points;
int64_t depth;
int64_t num_points;
double sum_weight;
double x, y, z;
double radius;
} NodeStats;

typedef struct {
NodeStats *stats;
long capacity;
long size;
int64_t capacity;
int64_t size;
} StatsVector;

// from ballnode.c
Expand All @@ -61,11 +63,11 @@ double bnode_dualcount_radius(const BallNode *, const BallNode *, double);
void bnode_dualcount_range(const BallNode *, const BallNode *, DistHistogram *);

// from ballnode_stats.c
StatsVector *statvec_new_reserve(long);
StatsVector *statvec_new_reserve(int64_t);
void statvec_free(StatsVector *);
int statvec_resize(StatsVector *, long);
int statvec_resize(StatsVector *, int64_t);
int statvec_append(StatsVector *, const NodeStats *);
int bnode_collect_stats(const BallNode *, StatsVector *, int);
long bnode_count_nodes(const BallNode *);
int64_t bnode_count_nodes(const BallNode *);

#endif /* BALLNODE_H */
4 changes: 3 additions & 1 deletion include/balltree.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef BALLTREE_H
#define BALLTREE_H

#include <stdint.h>

#include "point.h"
#include "histogram.h"
#include "ballnode.h"
Expand All @@ -27,7 +29,7 @@ void balltree_count_range(const BallTree *, const Point *, DistHistogram *);
double balltree_dualcount_radius(const BallTree *, const BallTree *, double);
void balltree_dualcount_range(const BallTree *, const BallTree *, DistHistogram *);

long balltree_count_nodes(const BallTree *);
int64_t balltree_count_nodes(const BallTree *);
StatsVector *balltree_collect_stats(const BallTree *);

// from balltree_serialize.c
Expand Down
10 changes: 6 additions & 4 deletions include/histogram.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#ifndef HISTOGRAM_H
#define HISTOGRAM_H

#include <stdint.h>

typedef struct {
long size;
int64_t size;
double *sum_weight;
double *dist;
double dist_max;
Expand All @@ -12,7 +14,7 @@ typedef struct {

#define HISTOGRAM_INSERT_DIST_SQ(hist, dist_sq, weight) \
({ \
long __bin_idx = -1; \
int64_t __bin_idx = -1; \
if ((dist_sq) <= (hist)->dist_sq_max) { \
for (__bin_idx = 0; __bin_idx <= hist->size; ++__bin_idx) { \
if ((dist_sq) <= (hist)->dist_sq[__bin_idx]) { \
Expand All @@ -24,8 +26,8 @@ typedef struct {
__bin_idx; \
})

DistHistogram *hist_new(long, double *);
DistHistogram *hist_new(int64_t, double *);
void hist_free(DistHistogram *);
long hist_insert(DistHistogram *, double, double);
int64_t hist_insert(DistHistogram *, double, double);

#endif /* HISTOGRAM_H */
16 changes: 9 additions & 7 deletions include/point.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef POINT_H
#define POINT_H

#include <stdint.h>

enum Axis {X, Y, Z};

typedef struct {
Expand All @@ -12,7 +14,7 @@ typedef struct {

typedef struct {
Point *points;
long size;
int64_t size;
} PointBuffer;

typedef struct {
Expand All @@ -35,16 +37,16 @@ double point_dist(const Point *, const Point *);
double point_dist_sq(const Point *, const Point *);

// from pointbuffers.c
PointBuffer *ptbuf_new(long);
PointBuffer *ptbuf_from_buffers(long, double *, double *, double *);
PointBuffer *ptbuf_from_buffers_weighted(long, double *, double *, double *, double *);
PointBuffer *ptbuf_new(int64_t);
PointBuffer *ptbuf_from_buffers(int64_t, double *, double *, double *);
PointBuffer *ptbuf_from_buffers_weighted(int64_t, double *, double *, double *, double *);
void ptbuf_free(PointBuffer *);
int ptbuf_resize(PointBuffer *, long);
int ptbuf_resize(PointBuffer *, int64_t);
PointBuffer *ptbuf_copy(const PointBuffer *);
PointBuffer *ptbuf_gen_random(double, double, long);
PointBuffer *ptbuf_gen_random(double, double, int64_t);

PointSlice *ptslc_from_buffer(const PointBuffer *);
long ptslc_get_size(const PointSlice *);
int64_t ptslc_get_size(const PointSlice *);
double ptslc_sumw_in_radius_sq(const PointSlice *, const Point *, double);
double ptslc_sumw_in_range_sq(const PointSlice *, const Point *, double, double);
double ptslc_dualsumw_in_radius_sq(const PointSlice *, const PointSlice *, double);
Expand Down
7 changes: 4 additions & 3 deletions src/ballnode.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

Expand Down Expand Up @@ -141,7 +142,7 @@ static Point *ptslc_partition(PointSlice *slice, Point *pivot, enum Axis axis) {

static Point *ptslc_quickselect(PointSlice *slice, Point *partition, enum Axis axis) {
if (slice->start < slice->end) {
long pivot_offset = (slice->end - slice->start) / 2;
int64_t pivot_offset = (slice->end - slice->start) / 2;
Point *pivot = slice->start + pivot_offset;
pivot = ptslc_partition(slice, pivot, axis);

Expand Down Expand Up @@ -170,7 +171,7 @@ static Point *ptslc_quickselect(PointSlice *slice, Point *partition, enum Axis a

static Point *ptslc_partition_maxspread_axis(PointSlice *slice) {
enum Axis split_axis = ptslc_get_maxspread_axis(slice);
long median_offset = (slice->end - slice->start) / 2;
int64_t median_offset = (slice->end - slice->start) / 2;
Point *median = slice->start + median_offset;
median = ptslc_quickselect(slice, median, split_axis);
if (median == NULL) {
Expand All @@ -180,7 +181,7 @@ static Point *ptslc_partition_maxspread_axis(PointSlice *slice) {
}

BallNode *bnode_build(PointSlice *slice, int leafsize) {
long num_points = ptslc_get_size(slice);
int64_t num_points = ptslc_get_size(slice);

BallNode *node = calloc(1, sizeof(BallNode));
if (node == NULL) {
Expand Down
5 changes: 3 additions & 2 deletions src/ballnode_query.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>

#include "point.h"
Expand Down Expand Up @@ -83,7 +84,7 @@ void bnode_count_range(

// case: node may entirely fall into one bin
double rmin = -INFINITY; // ensure 0.0 is included at first iteration
for (long i = 0; i < hist-> size; ++i) {
for (int64_t i = 0; i < hist-> size; ++i) {
double rmax = hist->dist[i];
if (rmin + node_radius < distance && distance <= rmax - node_radius) {
hist->sum_weight[i] += point->weight * node->sum_weight;
Expand Down Expand Up @@ -168,7 +169,7 @@ void bnode_dualcount_range(

// case: nodes may entirely fall into one bin
double rmin = -INFINITY; // ensure 0.0 is included at first iteration
for (long i = 0; i < hist-> size; ++i) {
for (int64_t i = 0; i < hist-> size; ++i) {
double rmax = hist->dist[i];
if (rmin + sum_node_radii < distance && distance <= rmax - sum_node_radii) {
hist->sum_weight[i] += node1->sum_weight * node2->sum_weight;
Expand Down
9 changes: 5 additions & 4 deletions src/ballnode_stats.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <stdint.h>
#include <stdlib.h>

#include "ballnode.h"
Expand All @@ -7,7 +8,7 @@ StatsVector *statvec_new() {
return statvec_new_reserve(32L);
}

StatsVector *statvec_new_reserve(long reserve_capacity) {
StatsVector *statvec_new_reserve(int64_t reserve_capacity) {
if (reserve_capacity < 1) {
EMIT_ERR_MSG(MemoryError, "StatsVector capacity must be positive");
return NULL;
Expand Down Expand Up @@ -37,7 +38,7 @@ void statvec_free(StatsVector *vec) {
free(vec);
}

int statvec_resize(StatsVector *vec, long capacity) {
int statvec_resize(StatsVector *vec, int64_t capacity) {
if (capacity < 1) {
EMIT_ERR_MSG(ValueError, "StatsVector capacity must be positive");
return BTR_FAILED;
Expand Down Expand Up @@ -95,8 +96,8 @@ int bnode_collect_stats(const BallNode *node, StatsVector *vec, int depth) {
return BTR_SUCCESS;
}

long bnode_count_nodes(const BallNode *node) {
long count = 1;
int64_t bnode_count_nodes(const BallNode *node) {
int64_t count = 1;
if (!BALLNODE_IS_LEAF(node)) {
count += bnode_count_nodes(node->childs.left);
count += bnode_count_nodes(node->childs.right);
Expand Down
9 changes: 5 additions & 4 deletions src/balltree.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

Expand Down Expand Up @@ -27,7 +28,7 @@ BallTree *balltree_build_leafsize(const PointBuffer *buffer, int leafsize) {
}

BallTree *balltree_build_nocopy(PointBuffer *buffer, int leafsize) {
long size = buffer->size;
int64_t size = buffer->size;
if (size < 1) {
EMIT_ERR_MSG(ValueError, "need at least one input data point to build a tree");
return NULL;
Expand Down Expand Up @@ -66,7 +67,7 @@ void balltree_free(BallTree *tree) {
free(tree);
}

long balltree_count_nodes(const BallTree *tree) {
int64_t balltree_count_nodes(const BallTree *tree) {
return bnode_count_nodes(tree->root);
}

Expand All @@ -89,7 +90,7 @@ void balltree_brute_range(
DistHistogram *hist
) {
Point *points = tree->data->points;
for (long i = 0; i < tree->data->size; ++i) {
for (int64_t i = 0; i < tree->data->size; ++i) {
double dist_sq = EUCLIDEAN_DIST_SQ(point, points + i);
// increment corresponding bin by weight
HISTOGRAM_INSERT_DIST_SQ(hist, dist_sq, point->weight * points[i].weight);
Expand Down Expand Up @@ -129,7 +130,7 @@ void balltree_dualcount_range(
}

StatsVector *balltree_collect_stats(const BallTree *tree) {
long num_nodes = balltree_count_nodes(tree);
int64_t num_nodes = balltree_count_nodes(tree);
StatsVector *vec = statvec_new_reserve(num_nodes);
if (vec == NULL) {
return NULL;
Expand Down
Loading

0 comments on commit 9e01d10

Please sign in to comment.