Skip to content

Use LazyBroadcast in set_velocity_quantities #3608

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

charleskawczynski
Copy link
Member

This PR is a step towards #3594.

Copy link
Member

@szy21 szy21 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks mostly correct (although there may be a bug somewhere as some tests failed), but I find it a bit hard to understand how velocity is calculated as it is separated into multiple lines now. Maybe @dennisYatunin can take a look if this is ok?

@szy21 szy21 requested a review from trontrytel February 7, 2025 18:59
@trontrytel
Copy link
Member

Agree with the above. Separating the velocity computation into a couple of separate steps may lead to some errors. Would it be possible to keep the behavior of setting them all together while still switching to lazy broadcasts?

Also a stupid question. Why do we need

        bc_ᶠu³ʲ = compute_ᶠu³(ᶜuₕ, ᶜρ, ᶠu₃ʲ)
        @. ᶠu³ʲ = bc_ᶠu³ʲ

instead of

@. ᶠu³ʲ = compute_ᶠu³(ᶜuₕ, ᶜρ, ᶠu₃ʲ)

@charleskawczynski
Copy link
Member Author

Agree with the above. Separating the velocity computation into a couple of separate steps may lead to some errors. Would it be possible to keep the behavior of setting them all together while still switching to lazy broadcasts?

I suppose we could instead pass the state variable(s), and remove the cached variable and delay inlining of set_velocity_quantities!.

Also a stupid question. Why do we need

        bc_ᶠu³ʲ = compute_ᶠu³(ᶜuₕ, ᶜρ, ᶠu₃ʲ)
        @. ᶠu³ʲ = bc_ᶠu³ʲ

instead of

@. ᶠu³ʲ = compute_ᶠu³(ᶜuₕ, ᶜρ, ᶠu₃ʲ)

That's not a stupid question-- it's because we aren't broadcasting over its arguments ((ᶜuₕ, ᶜρ, ᶠu₃ʲ))-- internally its performing a broadcast operation and returning a broadcasted object. So, it needs to be hoisted.

@dennisYatunin
Copy link
Member

Why do we need

        bc_ᶠu³ʲ = compute_ᶠu³(ᶜuₕ, ᶜρ, ᶠu₃ʲ)
        @. ᶠu³ʲ = bc_ᶠu³ʲ

instead of

@. ᶠu³ʲ = compute_ᶠu³(ᶜuₕ, ᶜρ, ᶠu₃ʲ)

I think what @trontrytel meant here was ᶠu³ʲ .= compute_ᶠu³(ᶜuₕ, ᶜρ, ᶠu₃ʲ). I am definitely in favor of keeping things on one line like this to facilitate readability. Also, I would like to request that we use lazy.( instead of @lazy(, since macro calls are not as beginner-friendly as function calls.

@charleskawczynski
Copy link
Member Author

I think what @trontrytel meant here was ᶠu³ʲ .= compute_ᶠu³(ᶜuₕ, ᶜρ, ᶠu₃ʲ). I am definitely in favor of keeping things on one line like this to facilitate readability. Also, I would like to request that we use lazy.( instead of @lazy(, since macro calls are not as beginner-friendly as function calls.

Ah, sure, I think that will work. I kind of like the pointer assignment separate from the materialization, but it's not a big deal for now.

And I'm happy to update to using lazy, I think you're right regarding simplicity. I'll open a sweeping change for that soon.

@charleskawczynski
Copy link
Member Author

I've updated to use the x .= f() syntax, cc @dennisYatunin.

@charleskawczynski
Copy link
Member Author

Also, I've opened #3613. I'll rebase this PR after that merges.

@charleskawczynski charleskawczynski force-pushed the ck/set_velocity_quantities branch 3 times, most recently from 6413832 to 4c3ed68 Compare February 12, 2025 16:28
Copy link
Member

@szy21 szy21 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me now. @trontrytel @dennisYatunin Could you take a look again? And to make sure I understand, instead of passing ᶠuₕ³ around we are calculating it multiple times when calling set_velocity_quantities - is this something we want to do?

Comment on lines 20 to 21
ᶜuₕ = Y.c.uₕ
ᶠu₃ = Y.f.u₃
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these used anywhere?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can remove it if it's not, I thought that I passed it to set_velocity_quantities!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not see them used anywhere

Comment on lines 70 to 71
ᶜuₕ = Y.c.uₕ
ᶠu₃ = Y.f.u₃
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these used anywhere?

@trontrytel
Copy link
Member

This looks good to me now. @trontrytel @dennisYatunin Could you take a look again? And to make sure I understand, instead of passing ᶠuₕ³ around we are calculating it multiple times when calling set_velocity_quantities - is this something we want to do?

Looks good to me also! Thank you.

@charleskawczynski charleskawczynski force-pushed the ck/set_velocity_quantities branch from 4c3ed68 to 685d858 Compare May 10, 2025 18:38
Copy link

coderabbitai bot commented May 10, 2025

Walkthrough

This change introduces two new functions, compute_ᶜu and compute_ᶠu³, to handle the calculation of cell-centered and cell-face contravariant velocities. The set_velocity_quantities! function is updated to use these new functions for its assignments. Additionally, in set_implicit_precomputed_quantities!, an in-place broadcast assignment is used for updating ᶠuₕ³. The update focuses on refactoring velocity computations into reusable functions and standardizing assignment style.

Possibly related PRs

Tip

⚡️ Faster reviews with caching
  • CodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure Review - Disable Cache at either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off the Data Retention setting under your Organization Settings.

Enjoy the performance boost—your workflow just got faster.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/cache/precomputed_quantities.jl (1)

321-327: Add lazy broadcasting to compute_ᶠu³

The function is correctly implemented but missing the lazy broadcasting pattern used elsewhere in the code.

-compute_ᶠu³(ᶠuₕ³, ᶠu₃) = ᶠuₕ³ + CT3(ᶠu₃)
+compute_ᶠu³(ᶠuₕ³, ᶠu₃) = @. lazy(ᶠuₕ³ + CT3(ᶠu₃))
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 799fb93 and 685d858.

📒 Files selected for processing (1)
  • src/cache/precomputed_quantities.jl (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (12)
  • GitHub Check: downstream ClimaCoupler.jl (1.10)
  • GitHub Check: downstream ClimaCoupler.jl (1.11)
  • GitHub Check: evaluate
  • GitHub Check: docbuild
  • GitHub Check: test (1.11)
  • GitHub Check: test (1.10)
  • GitHub Check: ci 1.11 - windows-latest
  • GitHub Check: ci 1.11 - ubuntu-latest
  • GitHub Check: ci 1.11 - macOS-latest
  • GitHub Check: ci 1.10 - windows-latest
  • GitHub Check: ci 1.10 - ubuntu-latest
  • GitHub Check: ci 1.10 - macOS-latest
🔇 Additional comments (3)
src/cache/precomputed_quantities.jl (3)

313-319: Well-designed implementation of compute_ᶜu

The function is correctly implementing lazy broadcasting for cell-centered velocity computation, making the code more modular and reusable.


332-333: Good use of in-place broadcasting with computed values

The refactored code properly uses the new functions with .= syntax, making the code more readable and consistent with Julia idioms.


470-470: Good conversion to in-place broadcast

Converting to .= syntax is appropriate for the lazy broadcasting approach.

@charleskawczynski
Copy link
Member Author

I've dialed back some of the changes (as #3798 absorbed some suggestions).

How do people feel about changing set_velocity_quantities! calls to:

    (; velocity_form) = p.atmos # velocity_form is a VelocityForm()
    ᶜu .= compute_ᶜu(velocity_form, ᶜuₕ, ᶠu₃)
    ᶠu³ .= compute_ᶠu³.(velocity_form, ᶠuₕ³, ᶠu₃)
    ᶜK .= compute_kinetic(velocity_form, ᶜuₕ, ᶠu₃)

? This way, we can ensure that all three of these implementations are linked, which I think was people's main concern?

@trontrytel
Copy link
Member

I've dialed back some of the changes (as #3798 absorbed some suggestions).

How do people feel about changing set_velocity_quantities! calls to:

    (; velocity_form) = p.atmos # velocity_form is a VelocityForm()
    ᶜu .= compute_ᶜu(velocity_form, ᶜuₕ, ᶠu₃)
    ᶠu³ .= compute_ᶠu³.(velocity_form, ᶠuₕ³, ᶠu₃)
    ᶜK .= compute_kinetic(velocity_form, ᶜuₕ, ᶠu₃)

? This way, we can ensure that all three of these implementations are linked, which I think was people's main concern?

What is velocity_form in your code snippet?

@charleskawczynski
Copy link
Member Author

What is velocity_form in your code snippet?

An instance of a (singleton) struct (struct VelocityForm end).

IIRC, people were concerned about getting rid of set_velocity_quantities! because of the worry that we might accidentally change change how the outputs were computed in different places.

Using this type in all of the methods (compute_ᶜu, compute_ᶠu³ and compute_kinetic), in a sense, ties these methods together.

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

Successfully merging this pull request may close these issues.

4 participants