Skip to content

Commit d66c10a

Browse files
committed
fixed blocks_coords_from_pointer
1 parent 4aedf35 commit d66c10a

File tree

4 files changed

+13
-21
lines changed

4 files changed

+13
-21
lines changed

code/blocks/blocks.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,17 @@ size_t *blocks_coords_from_pointer(void *p1, ndarray_obj_t *ndarray) {
3333
// This is a utility function, and is not exposed to the python interpreter
3434
blocks_block_obj_t *block = ndarray->block;
3535
size_t diff = (uint8_t *)p1 - (uint8_t *)block->origin;
36-
printf("pointer: 0x%p, 0x%p, 0x%p, %ld\n", p1, ndarray->array, block->origin, diff);
37-
size_t accumulator = ndarray->itemsize;
36+
size_t stride = ndarray->itemsize;
3837
size_t *coords = m_new(size_t, ULAB_MAX_DIMS);
3938

40-
for(uint8_t i = 0; i < ndarray->ndim; i++) {
41-
accumulator *= block->shape[ULAB_MAX_DIMS - i - 1];
42-
coords[ULAB_MAX_DIMS - i] = diff % accumulator;
43-
// diff -= coords[ULAB_MAX_DIMS - i] * block->shape[ULAB_MAX_DIMS - i];
44-
diff -= coords[ULAB_MAX_DIMS - i - 1] * block->shape[ULAB_MAX_DIMS - i - 1];
45-
printf("accumulator: %ld\n", diff);
39+
// first, calculate the very first stride
40+
for(uint8_t i = 0; i < block->ndim - 1; i++) {
41+
stride *= block->shape[ULAB_MAX_DIMS - i - 1];
42+
}
43+
for(uint8_t i = block->ndim; i > 1; i--) {
44+
coords[ULAB_MAX_DIMS - i] = diff / stride;
45+
diff -= coords[ULAB_MAX_DIMS - i] * block->shape[ULAB_MAX_DIMS - i];
46+
stride /= block->shape[ULAB_MAX_DIMS - i + 1];
4647
}
4748
return coords;
4849
}
@@ -113,6 +114,7 @@ mp_obj_t blocks_new_ndarray(size_t n_args, const mp_obj_t *pos_args, mp_map_t *k
113114
ndarray->flags = BLOCK_IS_READ_ONLY;
114115
blocks_block_obj_t *block = m_new_obj(blocks_block_obj_t);
115116
block->base.type = &blocks_block_type;
117+
block->ndim = ndarray->ndim;
116118
// store a pointer to the ndarray
117119
block->ndarray = ndarray;
118120

code/ndarray.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ typedef struct _dtype_dtype {
6969

7070
typedef struct _blocks_block_obj_t {
7171
mp_obj_base_t base;
72+
uint8_t ndim;
7273
void *ndarray;
7374
void *arrfunc;
7475
uint8_t *subarray;

code/numpy/numerical/numerical.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -279,16 +279,6 @@ static mp_obj_t numerical_sum_mean_std_ndarray(ndarray_obj_t *ndarray, mp_obj_t
279279
uint8_t *rarray = NULL;
280280
mp_float_t *farray = NULL;
281281

282-
// #if ULAB_HAS_BLOCKS
283-
// blocks_block_obj_t *block = NULL;
284-
// if(ndarray->flags) {
285-
// block = ndarray->block;
286-
// // return mp_const_none;
287-
// }
288-
// #else
289-
// uint8_t block = 0;
290-
// #endif
291-
292282
if(optype == NUMERICAL_SUM) {
293283
results = ndarray_new_dense_ndarray(_shape_strides.ndim, _shape_strides.shape, ndarray->dtype.type);
294284
rarray = (uint8_t *)results->array;

code/user/user.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,10 @@ void imreader_imreader(ndarray_obj_t *ndarray, void *array, int32_t *strides, si
9191
// if necessary, get the coordinates in the original reference frame, i.e.,
9292
// in the coordinates used at the time of the creation of the object
9393
size_t *coords = blocks_coords_from_pointer(array, ndarray);
94-
uint8_t x = (uint8_t)coords[ULAB_MAX_DIMS - 2] * (uint8_t)coords[ULAB_MAX_DIMS - 1];
95-
printf("coords: %ld, %ld\n", coords[ULAB_MAX_DIMS - 2], coords[ULAB_MAX_DIMS - 1]);
94+
uint8_t x = (uint8_t)coords[ULAB_MAX_DIMS - 2] * (uint8_t)block->shape[ULAB_MAX_DIMS - 2];
9695
for(size_t i = 0; i < count; i++) {
9796
// fill up the array with dummy data
98-
*barray++ = (uint8_t)(x + i) * (x + i);
97+
*barray++ = (uint8_t)((x + i) * (x + i));
9998
}
10099
// The subarray is a forward propagating dense array, so set the strides to the itemsize
101100
*strides = ndarray->itemsize;

0 commit comments

Comments
 (0)