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

Update lapacke related C type usage #182

Merged
merged 1 commit into from
Oct 22, 2023

Conversation

ttytm
Copy link
Member

@ttytm ttytm commented Oct 22, 2023

Summary by CodeRabbit

  • Refactor: Updated the way certain functions handle input arguments in the matrix operations and linear algebra packages. This change enhances the software's reliability by ensuring correct data types are used, reducing the risk of type mismatch errors.
  • Style: Simplified the logic in the l_uplo function for improved readability and maintainability.

These changes are internal and do not introduce new features or directly affect user experience. They contribute to the overall robustness and maintainability of the codebase.

@coderabbitai
Copy link

coderabbitai bot commented Oct 22, 2023

Walkthrough

The changes primarily focus on modifying the type and syntax of certain function parameters across multiple files. The alterations ensure better type compatibility and cleaner code, enhancing the overall readability and maintainability of the codebase.

Changes

File Summary
la/matrix_ops.v Modified the matrix_svd function to pass string literals as references to the dgesvd function.
vlas/conversions.v Updated l_uplo to use a ternary operator and altered job_vlr to return a &char instead of a u8.
vlas/lapack.v Changed the type of several function parameters from byte to &char to improve type compatibility.

🐇💻

In the land of code, where the functions play,

A rabbit hopped by, making changes along the way.

With a twitch of the nose, and a flick of the ear,

The code became clearer, the intent more clear.

So here's to the rabbit, with its fur so white,

Making our codebase a much more delightful sight. 🎉🥕


Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • If you reply to a review comment from CodeRabbit, the bot will automatically respond.
  • To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment
  • Note: Review comments are made on code diffs or files, not on the PR overview.
  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.

Note: For conversation with the bot, please use the review comments on code diffs or files.

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.

Review Status

Actionable comments generated: 0

Commits Files that changed from the base of the PR and between 904b61d and aebb991.
Files selected for processing (3)
  • la/matrix_ops.v (1 hunks)
  • vlas/conversions.v (1 hunks)
  • vlas/lapack.v (3 hunks)
Files skipped from review due to trivial changes (1)
  • la/matrix_ops.v
Additional comments (Suppressed): 7
vlas/lapack.v (5)
  • 8-8: The function signature for C.LAPACKE_dgesvd has been updated to replace byte parameters with &char parameters. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

  • 16-16: The function signature for C.LAPACKE_dgeev has been updated to replace byte parameters with &char parameters. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

  • 18-18: The function signature for C.LAPACKE_dlange has been updated to replace the byte parameter with a &char parameter. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

  • 79-82: The function dgesvd has been updated to take &char parameters instead of byte parameters. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

  • 202-204: The function dlange has been updated to take a &char parameter instead of a byte parameter. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

vlas/conversions.v (2)
  • 23-24: The function l_uplo has been refactored to use a ternary operator instead of an if-else statement. This change improves readability and reduces the number of lines of code. However, ensure that the ternary operator is well supported and behaves as expected in your language and environment.

  • 27-32: The function job_vlr now returns a reference to a char instead of a u8. This change likely improves type compatibility with the updated LAPACKE functions. However, ensure that all calls to job_vlr throughout the codebase have been updated to handle the new return type.

@ttytm ttytm mentioned this pull request Oct 22, 2023
@@ -199,6 +199,6 @@ pub fn dgeev(calc_vl bool, calc_vr bool, n int, mut a []f64, lda int, wr []f64,
}
}

pub fn dlange(norm byte, m int, n int, a []f64, lda int, work []f64) f64 {
pub fn dlange(norm &char, m int, n int, a []f64, lda int, work []f64) f64 {
Copy link
Member

Choose a reason for hiding this comment

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

what happens if you just use str instead of &char? I think V handles that when doing interop with C but I'm not totally sure

Copy link
Member

Choose a reason for hiding this comment

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

that should simplify the code because we can use str everywhere instead of casting to &char

Copy link
Member Author

@ttytm ttytm Oct 22, 2023

Choose a reason for hiding this comment

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

Good point. The cast to char should be handled internally. The below would work:

pub fn dlange(norm string, m int, n int, a []f64, lda int, work []f64) f64 {
	return unsafe { C.LAPACKE_dlange(.row_major, &char(norm.str), m, n, &a[0], lda, &work[0]) }

The cast needs to be explicit. Just norm.str only compiles without -cstrict. Maybe this changes when the V compiler evolves. Until then &char(norm.str) is the proper usage, which is okay for internal code I guess. (I'm gonna open an issue at V regarding this, as I need to use this cast a lot in my own code bases as well when interoping with C. Just .str would add some convenience).

Regarding the norm type, it depends if string is the best. If norms are set by a single character rune would be better. I'm not too familiar with the usage of lapacke yet and can't tell if an enum is appropriate for the norms. In any case, the cast to char could and should be handled internally.

Which one should it be?

Copy link
Member

Choose a reason for hiding this comment

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

OK, makes sense! Will Merge now!

Copy link
Member Author

@ttytm ttytm Oct 22, 2023

Choose a reason for hiding this comment

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

Thanks @ulises-jeremias !

I think t was a valid point and that it's better to use &char only in the C functions.
That's why I thought it might be improved and ended the comment with a question.

So for pub fn dlange I'm changing it to rune in #184. Than it's the same usage as before for this public function. Just with internal C type handling corrected.

Regarding .str cast situation, opened an issue with a minimal repro: vlang/v#19623

🙂:+1

@ulises-jeremias ulises-jeremias merged commit 0bedacc into vlang:main Oct 22, 2023
9 checks passed
@ttytm ttytm deleted the fix/update-lapacke-c-types branch October 24, 2023 23:30
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