Skip to content
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

Add support for bulk setter using primitive array #179

Open
tklinchik opened this issue Jan 9, 2024 · 0 comments
Open

Add support for bulk setter using primitive array #179

tklinchik opened this issue Jan 9, 2024 · 0 comments
Labels
api Common api enhancement New feature or request

Comments

@tklinchik
Copy link

tklinchik commented Jan 9, 2024

In order to update all values (by some dimension) in the NDArray there's no bulk copy operator that's efficient and as a result you need to iterate all dimensions:

  val array = mk.zeros<Float>(20, 5, 3)
  for (i in 0 until 20) {
    for (j in 0 until 5) {
      for (k in 0 until 3) {
        array[i, j, k] = computeSingleValue()
      }
    }
  }

For use cases with array of primitive values for a single dimension already available, it would be very useful to have a bulk support allowing to supply a vector of values (as a primitive array) for N-1 dimensions with a signatures as follows:

public operator fun MutableMultiArray<Float, D4>.setFloatArray(ind1: Int, ind2: Int, ind3: Int, values: FloatArray, valuesOffset: Int = 0, valuesSize: Int = values.size)
public operator fun MutableMultiArray<Float, D3>.setFloatArray(ind1: Int, ind2: Int, values: FloatArray, valuesOffset: Int = 0, valuesSize: Int = values.size)
public operator fun MutableMultiArray<Float, D2>.setFloatArray(ind1: Int, values: FloatArray, valuesOffset: Int = 0, valuesSize: Int = values.size)

which would eliminate expensive stride lookups for every single value set in the example above and would allow the following:

  val array = mk.zeros<Float>(20, 5, 3)
  for (i in 0 until 20) {
    for (j in 0 until 5) {
      val dim = computeOneDimension() // returning FloatArray
      ndarray.set(i, j, dim)
    }
  }

This will significantly improve performance of updating very large matrices.
Also, this would be a peer of N-1 helper to get a copy of array out allowing array.get(i, j).toFloatArray() for 3D NDArray for example.

@devcrocod devcrocod added enhancement New feature or request api Common api labels Jan 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api Common api enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants