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

printing ApplyArray(*, A...) has exponential cost #259

Closed
putianyi889 opened this issue Jul 14, 2023 · 9 comments
Closed

printing ApplyArray(*, A...) has exponential cost #259

putianyi889 opened this issue Jul 14, 2023 · 9 comments

Comments

@putianyi889
Copy link
Contributor

julia> @time show(ApplyArray(*, [ones(10,10) for k in 1:6]...))
[100000.0 100000.0 100000.0 100000.0 100000.0 100000.0 100000.0 100000.0 100000.0 100000.0; 100000.0 100000.0 100000.0 100000.0 100000.0 100000.0 100000.0 100000.0 100000.0 100000.0; 100000.0 100000.0 100000.0 100000.0 100000.0 100000.0 100000.0 100000.0 100000.0 100000.0; 100000.0 100000.0 100000.0 100000.0 100000.0 100000.0 100000.0 100000.0 100000.0 100000.0; 100000.0 100000.0 100000.0 100000.0 100000.0 100000.0 100000.0 100000.0 100000.0 100000.0; 100000.0 100000.0 100000.0 100000.0 100000.0 100000.0 100000.0 100000.0 100000.0 100000.0; 100000.0 100000.0 100000.0 100000.0 100000.0 100000.0 100000.0 100000.0 100000.0 100000.0; 100000.0 100000.0 100000.0 
100000.0 100000.0 100000.0 100000.0 100000.0 100000.0 100000.0; 100000.0 100000.0 100000.0 100000.0 100000.0 100000.0 100000.0 100000.0 100000.0 100000.0; 100000.0 100000.0 100000.0 100000.0 100000.0 100000.0 100000.0 100000.0 100000.0 100000.0]  1.060463 seconds (1.30 M allocations: 38.767 MiB, 1.97% gc time)

julia> @time show(ApplyArray(*, [ones(10,10) for k in 1:7]...))
[1.0e6 1.0e6 1.0e6 1.0e6 1.0e6 1.0e6 1.0e6 1.0e6 1.0e6 1.0e6; 1.0e6 1.0e6 1.0e6 1.0e6 1.0e6 1.0e6 1.0e6 1.0e6 1.0e6 1.0e6; 1.0e6 1.0e6 1.0e6 1.0e6 1.0e6 1.0e6 1.0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.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7; 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7] 76.525859 seconds (125.37 M allocations: 3.429 GiB, 1.37% gc time)

However, getindex is fast

julia> @time show(ApplyArray(*, [ones(10,10) for k in 1:8]...)[:,:])
[1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7; 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7; 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7; 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7; 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7; 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7; 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7; 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7; 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7; 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7 1.0e7]  0.815893 seconds (569.25 k allocations: 37.234 MiB, 3.19% gc time)
@putianyi889
Copy link
Contributor Author

potentially related #255

@putianyi889
Copy link
Contributor Author

The cost is cut by 90% after #261, however the exponential growth is still not solved.

@dlfivefifty
Copy link
Member

Note if we want to support many operators we should add support for vector arguments eg ApplyArray(*, [A1,…,An])

but removing the need for ApplyStyle seems like a first step

@putianyi889
Copy link
Contributor Author

How do you resolve the ambiguity of ApplyArray(*, [A1,…,An])? since the vector could potentially be the only argument.

Maybe add a new type of ReduceArray which mimics reduce?

@dlfivefifty
Copy link
Member

That probably won’t be the actual constructor. Note args is usually a Tuple but I think will accept a Vector already.

Reduce doesn’t return an array does it?

@putianyi889
Copy link
Contributor Author

julia> reduce(+, [ones(5,5) for k in 1:10])
5×5 Matrix{Float64}:
 10.0  10.0  10.0  10.0  10.0
 10.0  10.0  10.0  10.0  10.0
 10.0  10.0  10.0  10.0  10.0
 10.0  10.0  10.0  10.0  10.0
 10.0  10.0  10.0  10.0  10.0

@dlfivefifty
Copy link
Member

In that case you want to support ApplyArray(reduce, +, A)

@putianyi889
Copy link
Contributor Author

the same argument could also apply to broadcast, considering ApplyArray(broadcast, +, A) instead of BroadcastArray(+, A)

@putianyi889
Copy link
Contributor Author

fixed. can't find the commit that does it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants