Kotlin / multik

Multidimensional array library for Kotlin
https://kotlin.github.io/multik/
Apache License 2.0
633 stars 38 forks source link

[native] linalg implementations deep copy input arrays #202

Open max-hillsidetec opened 4 months ago

max-hillsidetec commented 4 months ago

Hi guys,

I saw functions in NativeLinAlgEx in multi-openblas deep copy the input arrays. Some of the examples are shown below.

 override fun <T : Number, D : Dim2> solve(a: MultiArray<T, D2>, b: MultiArray<T, D>): NDArray<Double, D> =
    solveCommon(a.toType(CopyStrategy.MEANINGFUL), b.toType(CopyStrategy.MEANINGFUL))`

 override fun <D : Dim2> solveF(a: MultiArray<Float, D2>, b: MultiArray<Float, D>): NDArray<Float, D> =
    solveCommon(a.deepCopy(), b.deepCopy())`

 override fun <T : Complex, D : Dim2> solveC(a: MultiArray<T, D2>, b: MultiArray<T, D>): NDArray<T, D> =
    solveCommon(a.deepCopy(), b.deepCopy())

I wonder whether deep copy is in fact needed. In our use cases, we slice a matrix many times and pass the sub-matrices to the solver. Deep copying them is suboptimal.

Thanks in advance for your help!

Best, Max

devcrocod commented 3 months ago

Hi Max!

We are not always able to work directly with "views", which necessitates us to copy arrays for sgesv / dgesv. The copying does not involve the entire original matrix but only the part indicated by the strides. Nonetheless, I will take a closer look at this area to see if there's potential for performance improvement.

max-hillsidetec commented 3 months ago

Hi Pavel,

Thanks very much for taking a look! It looks like if an NDArray is consistent, then deep copy may not be necessary.