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

Enable abs and sqrt to return a param in more cases #24127

Merged
merged 13 commits into from Jan 17, 2024
Merged

Conversation

mppf
Copy link
Member

@mppf mppf commented Jan 2, 2024

For issue #24119.
Resolves #24125.

This PR adds overloads to AutoMath to implement param versions of:

  • abs for all numeric types
  • sqrt for floating-point types.

Additionally, while testing, I realized that complex.re and complex.im did not have param returning versions, so this PR also adds such param-returning versions to ChapelBase.

To implement these, I:

  • added PRIM_SQRT and PRIM_ABS. For now, these are just used for computing these functions on params and they will cause the compiler to error out one makes it to code-generation time.
  • implemented folding for PRIM_ABS, PRIM_SQRT, PRIM_GET_REAL, and PRIM_GET_IMAG in num.cpp
  • enabled postFoldPrimop to fold PRIM_ABS, PRIM_SQRT, PRIM_GET_REAL, and PRIM_GET_IMAG

This PR also updates the spec description of complex to make it clearer. While there, it removes proc complex.bits from the spec, since this method does not exist other than in the spec.

Reviewed by @DanilaFe - thanks!

  • full comm=none testing

Future Work:

@damianmoz
Copy link

Not urgent

Comment on lines 226 to 228
proc abs(param x: real(32)) param : real(32) {
return if x < 0 then -x else x;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Floating points are weird, and this worries me a little. Consider the following Python:

>>> float("-0")
-0.0
>>> float("-0") < 0
False
>>> abs(float("-0"))
0.0

In other words, numerically this differs from the behavior of Python's abs:

>>> def myabs(x): return -x if x < 0 else x
...
>>> myabs(float("-0"))
-0.0
>>> abs(float("-0"))
0.0

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 wire this up to call an abs primitive in the compiler to run fabs. Do you feel that would address the issue?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, I do -- fabs seems ideal.

@mppf
Copy link
Member Author

mppf commented Jan 5, 2024

@DanilaFe - OK, I've added the primitive. From this point it would be trivial to also add param abs support for complex as well as param sqrt support. Do you think I should proceed with those in this PR?

@DanilaFe
Copy link
Contributor

Sorry for the delayed response. I think you shouldn't worry about sqrt and complex here -- it can be left for future work (a "good first issue" perhaps?).

Comment on lines 751 to 767
primAbsGetType(Context* context, const CallInfo& ci) {
QualifiedType ret = QualifiedType();

if (ci.numActuals() != 1) return ret;

QualifiedType actualType = ci.actual(0).type();
if (auto comp = actualType.type()->toComplexType()) {
int w = comp->componentBitwidth();
ret = QualifiedType(QualifiedType::CONST_VAR, RealType::get(context, w));
} else if (auto img = actualType.type()->toImagType()) {
int w = img->bitwidth();
ret = QualifiedType(QualifiedType::CONST_VAR, RealType::get(context, w));
} else {
// otherwise just use the original type
ret = QualifiedType(QualifiedType::CONST_VAR, actualType.type());
Copy link
Contributor

Choose a reason for hiding this comment

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

Note that the production compiler treats ABS and SQRT as param-only primitives. That leads me to think that Dyno should also implement the param logic, and not support the non-param case.

Copy link
Member Author

Choose a reason for hiding this comment

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

OK, I added some comments along these lines, and will note it in Future Work.

---
Signed-off-by: Michael Ferguson <mppf@users.noreply.github.com>
---
Signed-off-by: Michael Ferguson <mppf@users.noreply.github.com>
---
Signed-off-by: Michael Ferguson <mppf@users.noreply.github.com>
---
Signed-off-by: Michael Ferguson <mppf@users.noreply.github.com>
---
Signed-off-by: Michael Ferguson <mppf@users.noreply.github.com>
---
Signed-off-by: Michael Ferguson <mppf@users.noreply.github.com>
---
Signed-off-by: Michael Ferguson <mppf@users.noreply.github.com>
---
Signed-off-by: Michael Ferguson <mppf@users.noreply.github.com>
---
Signed-off-by: Michael Ferguson <mppf@users.noreply.github.com>
---
Signed-off-by: Michael Ferguson <mppf@users.noreply.github.com>
The C++ overloading handles the size

---
Signed-off-by: Michael Ferguson <mppf@users.noreply.github.com>
---
Signed-off-by: Michael Ferguson <mppf@users.noreply.github.com>
---
Signed-off-by: Michael Ferguson <mppf@users.noreply.github.com>
@mppf mppf merged commit bfecaab into chapel-lang:main Jan 17, 2024
7 checks passed
@mppf mppf deleted the param-abs branch January 17, 2024 21:53
@bradcray
Copy link
Member

Nice, thanks!

vasslitvinov added a commit to vasslitvinov/chapel that referenced this pull request Jan 18, 2024
Signed-off-by: Vassily Litvinov <vasslitvinov@users.noreply.github.com>
@mppf mppf changed the title Enable abs of a param real/imag to produce a param real Enable abs of a param real/imag/complex to produce a param real Jan 18, 2024
@mppf mppf changed the title Enable abs of a param real/imag/complex to produce a param real Enable abs and sqrt to return a param in more cases Jan 18, 2024
vasslitvinov added a commit that referenced this pull request Jan 18, 2024
This PR reacts to #24127 analogously to #24157, following the same argument
in favor of changing the line numbers vs. introducing a prediff.

Additionally, it reflects a reduction in the allocation/freeing size of
`[domain(1,int(64),one)] int(64)` from 168 to 160 bytes.
I did not investigate where that comes from.

reviewed, although favored the prediff solution: @mppf
mppf added a commit that referenced this pull request Jan 25, 2024
Follow-up to PR #24127.

After #24127, the new `sqrtparams` test started to fail on Mac OS X
system. Upon investigating, I identified that the difference in behavior
from what the test expects is due to differences in how `std::sqrt` is
implemented for complex numbers in libc++ vs stdlibc++. Also, I noticed
that the issue is limited to the C++ implementation of the complex
square root.

This PR changes the implementation to use a C implementation of complex
square root since it appears to have more reliable precision. Note that
this C implementation required some workarounds for `CMPLX` not being
available on all systems (as with PR #24184 and #24211). In particular,
`CMPLX` does not seem to be available when using `clang` on Ubuntu
23.10, even though it is available with `gcc` on that system.

I filed an issue against libc++ about the problem, but in discussion
there it sounds like complex `sqrt` isn't required to have any
particular precision.
 * llvm/llvm-project#78738

Reviewed by @DanilaFe - thanks!

- [x] full comm=none testing
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.

feature request: abs for param complex
4 participants