Skip to content

Commit 95b3dd4

Browse files
committed
Factor out itemsize function
1 parent 118fedd commit 95b3dd4

File tree

7 files changed

+18
-12
lines changed

7 files changed

+18
-12
lines changed

cubed/array_api/array_object.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from cubed.backend_array_api import namespace as nxp
2323
from cubed.core.array import CoreArray
2424
from cubed.core.ops import elemwise
25-
from cubed.utils import memory_repr
25+
from cubed.utils import itemsize, memory_repr
2626
from cubed.vendor.dask.widgets import get_template
2727

2828
ARRAY_SVG_SIZE = (
@@ -55,7 +55,7 @@ def _repr_html_(self):
5555

5656
if not math.isnan(self.nbytes):
5757
nbytes = memory_repr(self.nbytes)
58-
cbytes = memory_repr(math.prod(self.chunksize) * self.dtype.itemsize)
58+
cbytes = memory_repr(math.prod(self.chunksize) * itemsize(self.dtype))
5959
else:
6060
nbytes = "unknown"
6161
cbytes = "unknown"

cubed/core/array.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from cubed.runtime.types import Callback, Executor
1010
from cubed.spec import Spec, spec_from_config
1111
from cubed.storage.zarr import open_if_lazy_zarr_array
12-
from cubed.utils import array_memory
12+
from cubed.utils import array_memory, itemsize
1313
from cubed.vendor.dask.array.core import normalize_chunks
1414

1515
from .plan import arrays_to_plan
@@ -102,12 +102,12 @@ def size(self):
102102
@property
103103
def nbytes(self) -> int:
104104
"""Number of bytes in array"""
105-
return self.size * self.dtype.itemsize
105+
return self.size * itemsize(self.dtype)
106106

107107
@property
108108
def itemsize(self) -> int:
109109
"""Length of one array element in bytes"""
110-
return self.dtype.itemsize
110+
return itemsize(self.dtype)
111111

112112
def _read_stored(self):
113113
# Only works if the array has been computed

cubed/core/ops.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
array_memory,
3131
array_size,
3232
get_item,
33+
itemsize,
3334
offset_to_block_id,
3435
to_chunksize,
3536
)
@@ -1007,7 +1008,7 @@ def _rechunk_plan(x, chunks, *, min_mem=None):
10071008
shape=x.shape,
10081009
source_chunks=source_chunks,
10091010
target_chunks=target_chunks,
1010-
itemsize=x.dtype.itemsize,
1011+
itemsize=itemsize(x.dtype),
10111012
min_mem=min_mem,
10121013
max_mem=rechunker_max_mem,
10131014
)

cubed/core/plan.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
chunk_memory,
2222
extract_stack_summaries,
2323
is_local_path,
24+
itemsize,
2425
join_path,
2526
memory_repr,
2627
)
@@ -584,7 +585,7 @@ def create_zarr_array(lazy_zarr_array, *, config=None):
584585
def create_zarr_arrays(lazy_zarr_arrays, allowed_mem, reserved_mem):
585586
# projected memory is size of largest dtype size (for a fill value)
586587
projected_mem = (
587-
max([lza.dtype.itemsize for lza in lazy_zarr_arrays], default=0) + reserved_mem
588+
max([itemsize(lza.dtype) for lza in lazy_zarr_arrays], default=0) + reserved_mem
588589
)
589590
num_tasks = len(lazy_zarr_arrays)
590591

cubed/primitive/rechunk.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from cubed.runtime.types import CubedPipeline
1010
from cubed.storage.zarr import T_ZarrArray, lazy_zarr_array
1111
from cubed.types import T_RegularChunks, T_Shape, T_Store
12+
from cubed.utils import itemsize
1213
from cubed.vendor.rechunker.algorithm import rechunking_plan
1314

1415
sym_counter = 0
@@ -127,13 +128,12 @@ def _setup_array_rechunk(
127128
shape = source_array.shape
128129
source_chunks = source_array.chunks
129130
dtype = source_array.dtype
130-
itemsize = dtype.itemsize
131131

132132
read_chunks, int_chunks, write_chunks = rechunking_plan(
133133
shape,
134134
source_chunks,
135135
target_chunks,
136-
itemsize,
136+
itemsize(dtype),
137137
max_mem,
138138
)
139139

cubed/storage/types.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from operator import mul
55

66
from cubed.types import T_DType, T_RegularChunks, T_Shape
7-
from cubed.utils import normalize_dtype
7+
from cubed.utils import itemsize, normalize_dtype
88

99

1010
class ArrayMetadata:
@@ -26,7 +26,7 @@ def size(self) -> int:
2626
@property
2727
def nbytes(self) -> int:
2828
"""Number of bytes in array"""
29-
return self.size * self.dtype.itemsize
29+
return self.size * itemsize(self.dtype)
3030

3131
@property
3232
def _cdata_shape(self) -> T_Shape:

cubed/utils.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
def array_memory(dtype: T_DType, shape: T_Shape) -> int:
3131
"""Calculate the amount of memory in bytes that an array uses."""
32-
return normalize_dtype(dtype).itemsize * prod(shape)
32+
return itemsize(normalize_dtype(dtype)) * prod(shape)
3333

3434

3535
def chunk_memory(arr) -> int:
@@ -365,3 +365,7 @@ def normalize_dtype(dtype, device=None) -> T_DType:
365365
"""
366366

367367
return np.dtype(dtype)
368+
369+
370+
def itemsize(dtype: T_DType) -> int:
371+
return dtype.itemsize

0 commit comments

Comments
 (0)