Skip to content

Commit

Permalink
[Lang] Warn users if ndarray size is out of int32 boundary (taichi-de…
Browse files Browse the repository at this point in the history
…v#6846)

Issue: fix taichi-dev#6758

### Brief Summary

This PR adds a boundary check for ndarray similar to
https://github.com/taichi-dev/taichi/blob/a87b98fd40fc93dbe1bdf8c4e3932a6078379fa3/taichi/ir/snode.cpp#L89-L93

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
2 people authored and quadpixels committed May 13, 2023
1 parent a49e570 commit 3e97141
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
7 changes: 7 additions & 0 deletions python/taichi/lang/kernel_impl.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import ast
import functools
import inspect
import operator
import re
import sys
import textwrap
import warnings
import weakref

import numpy as np
Expand Down Expand Up @@ -653,6 +655,11 @@ def func__(*args):
# so that it only holds "real" array shapes.
is_soa = needed.layout == Layout.SOA
array_shape = v.shape
if functools.reduce(operator.mul, array_shape,
1) > np.iinfo(np.int32).max:
warnings.warn(
"Ndarray index might be out of int32 boundary but int64 indexing is not supported yet."
)
if needed.dtype is None or id(
needed.dtype) in primitive_types.type_ids:
element_dim = 0
Expand Down
17 changes: 16 additions & 1 deletion taichi/program/ndarray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,14 @@ Ndarray::Ndarray(Program *prog,
total_shape_.insert(total_shape_.begin(), element_shape.begin(),
element_shape.end());
}

auto total_num_scalar =
std::accumulate(std::begin(total_shape_), std::end(total_shape_), 1LL,
std::multiplies<>());
if (total_num_scalar > std::numeric_limits<int>::max()) {
TI_WARN(
"Ndarray index might be out of int32 boundary but int64 indexing is "
"not supported yet.");
}
ndarray_alloc_ = prog->allocate_memory_ndarray(nelement_ * element_size_,
prog->result_buffer);
}
Expand Down Expand Up @@ -84,6 +91,14 @@ Ndarray::Ndarray(DeviceAllocation &devalloc,
total_shape_.insert(total_shape_.begin(), element_shape.begin(),
element_shape.end());
}
auto total_num_scalar =
std::accumulate(std::begin(total_shape_), std::end(total_shape_), 1LL,
std::multiplies<>());
if (total_num_scalar > std::numeric_limits<int>::max()) {
TI_WARN(
"Ndarray index might be out of int32 boundary but int64 indexing is "
"not supported yet.");
}
}

Ndarray::Ndarray(DeviceAllocation &devalloc,
Expand Down

0 comments on commit 3e97141

Please sign in to comment.