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

Make ArrayPartitions of GPU arrays work in DifferentialEquations solvers #416

Merged
merged 2 commits into from
Jan 29, 2025

Conversation

anicusan
Copy link
Contributor

Checklist

  • Appropriate tests were added
  • [Tests Pass] Any code changes were done in a way that does not break public API
  • [NA] All documentation related to code changes were updated
  • The new code follows the
    contributor guidelines, in particular the SciML Style Guide and
    COLPRAC.
  • [NA] Any new documentation only uses public API

Additional context

The only function that was preventing ArrayPartitions of GPU arrays to work in DifferentialEquations solvers was recursivefill!, which was deferring to Base.fill! with scalar indexing. This PR makes fixes two things:

  • recursivefill! calls fill! on each array within the ArrayPartitions.x tuple - which now defer to the right GPU kernels.
  • Filling does not require index recalculations for each setindex! - which was unnecessary work.

Example Code (similar to what was added to the tests):

using RecursiveArrayTools
using Metal

a = (Metal.rand(5), Metal.rand(5))
pA = ArrayPartition(a)

# This now works
RecursiveArrayTools.recursivefill!(pA, false)

Possible Issues

  • I added an unrolled_foreach(f, x::Tuple) implementation for type stability when calling fill! on each subarray in the ArrayPartition.x tuple. This now uses a closure for f which updates each subarray inline; it is type-stable when checking with Cthulhu, and it does not update any variable outside the closure; I see the SciML guidelines do not recommend closures - should I rewrite that as a struct and all that?
  • I added a specialised implementation of recursivefill! for ArrayPartitions; all tests seem to work, but I don't have the "bigger picture" of how this is used across the ecosystem - is this okay / non-ambiguous? Should the other recursivefill! implementations in utils.jl be changed?

@ChrisRackauckas
Copy link
Member

I see the SciML guidelines do not recommend closures - should I rewrite that as a struct and all that?

This should be fine here.

is this okay / non-ambiguous?

yes

Should the other recursivefill! implementations in utils.jl be changed?

Possibly some others should get looked at for GPU compatibility.

@ChrisRackauckas ChrisRackauckas merged commit 96abc3e into SciML:master Jan 29, 2025
17 of 27 checks passed
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

Successfully merging this pull request may close these issues.

2 participants