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

De-macroize step 1/n: Probably uncontroversial switches from macros to constexpr #482

Merged
merged 13 commits into from
Sep 10, 2024

Conversation

mreineck
Copy link
Collaborator

I converted macros to constexpr definitions or static inline functions wherever I considered this safe. While at it, I also removed a few macros that were unused.

@mreineck mreineck marked this pull request as ready for review July 16, 2024 11:33
include/finufft/defs.h Outdated Show resolved Hide resolved
include/finufft/defs.h Show resolved Hide resolved
include/finufft_errors.h Show resolved Hide resolved
This was referenced Jul 17, 2024
@DiamonDinoia DiamonDinoia added this to the 3.0 milestone Jul 17, 2024
@DiamonDinoia DiamonDinoia mentioned this pull request Jul 17, 2024
6 tasks
@DiamonDinoia DiamonDinoia removed this from the 2.3 milestone Jul 17, 2024
@DiamonDinoia
Copy link
Collaborator

@mreineck is it ready for review?

@mreineck
Copy link
Collaborator Author

Yes. From my point of view the open questions are

  • does doc generation work with enums?
  • what to do about the #if 1 / #else / #endif block in defs.h, which I left as a kind of reminder. We can certainly remove the #else part if that is considered nicer.

@DiamonDinoia
Copy link
Collaborator

Hi Martin, I am delaying this until 2.3 is out. We need to merge the GPU stuff and change cmake/docs. You can continue on this branching from this branch as I will get to merge this. I am delaying some changes to the CPU code for after the templating in order to not overlap.

@DiamonDinoia DiamonDinoia added this to the 2.4 milestone Jul 29, 2024
@mreineck
Copy link
Collaborator Author

I think I'll add the subsequent steps after this one is merged. It's probably easier to do this at the right time than chase potential merge conflicts if I continue now.

@DiamonDinoia
Copy link
Collaborator

DiamonDinoia commented Aug 2, 2024

I checked the doc issue. Seems okay. If it passes CI we can merge.

The #ifdef in defs.h is okay for now otherwise things would break.

It is also nice that we can offer a nice c++ interface to the user.

@DiamonDinoia
Copy link
Collaborator

The new tests found an issue it seems. Let me know if you need help investigating. It might be something related to the optimization flags.

@mreineck
Copy link
Collaborator Author

As discussed yesterday, I merged master into the branch; hopefully this will fix the remaining test failures.

@mreineck
Copy link
Collaborator Author

OK, that doesn't seem to help...

@mreineck
Copy link
Collaborator Author

Problem seems to occur only for debug builds with static linking turned off.

I can reproduce it locally, and the error is huge. Something is really fishy, but the sanitizers don't seem to catch it ...

@mreineck
Copy link
Collaborator Author

If I read it right, this happens with both ducc FFT and FFTW.

@mreineck
Copy link
Collaborator Author

Maybe related, maybe not: if I compile with static linking disabled, I get the following error from cmake:

[ 71%] Linking C executable guru1d1c
cd /home/martin/codes/finufft/build/examples && /usr/bin/cmake -E cmake_link_script CMakeFiles/guru1d1c.dir/link.txt --verbose=1
/usr/bin/cc -g CMakeFiles/guru1d1c.dir/guru1d1c.c.o -o guru1d1c  -Wl,-rpath,/home/martin/codes/finufft/build ../libfinufft.so
/usr/bin/ld: CMakeFiles/guru1d1c.dir/guru1d1c.c.o: undefined reference to symbol 'cexp@@GLIBC_2.2.5'
/usr/bin/ld: /lib/x86_64-linux-gnu/libm.so.6: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status

cexp is in libm, and I think when using the C compiler as linker, this has to be specified explicitly.

@mreineck
Copy link
Collaborator Author

I think I found it ... it's pretty nasty :-(

In principle, finufft is currently set up to be compiled as two different libraries, one for single precision, one for double precision. But cmake puts both single- and double-precision object files into a single .a or .so in the end. That means that constants like IMA and PI, which are different for both precisions end up twice in the same library, with the same name, and only the first occurrence will be used... sometimes it will be the wrong one.

I can fix this by reverting the numerical constants to macros until everything is fully templatized. Not great, but I can't think of anything better at the moment.

@mreineck
Copy link
Collaborator Author

OK, I think this is ready to merge now.

@DiamonDinoia
Copy link
Collaborator

I think I found it ... it's pretty nasty :-(

In principle, finufft is currently set up to be compiled as two different libraries, one for single precision, one for double precision. But cmake puts both single- and double-precision object files into a single .a or .so in the end. That means that constants like IMA and PI, which are different for both precisions end up twice in the same library, with the same name, and only the first occurrence will be used... sometimes it will be the wrong one.

I can fix this by reverting the numerical constants to macros until everything is fully templatized. Not great, but I can't think of anything better at the moment.

The alternative is to define the M_PI as constexpr functions:

template<typename T>
FINUFFT_ALWAYS_INLINE constexpr T PI() noexcept {
return static_cast<T>(value here);
}

@DiamonDinoia
Copy link
Collaborator

Maybe related, maybe not: if I compile with static linking disabled, I get the following error from cmake:

[ 71%] Linking C executable guru1d1c
cd /home/martin/codes/finufft/build/examples && /usr/bin/cmake -E cmake_link_script CMakeFiles/guru1d1c.dir/link.txt --verbose=1
/usr/bin/cc -g CMakeFiles/guru1d1c.dir/guru1d1c.c.o -o guru1d1c  -Wl,-rpath,/home/martin/codes/finufft/build ../libfinufft.so
/usr/bin/ld: CMakeFiles/guru1d1c.dir/guru1d1c.c.o: undefined reference to symbol 'cexp@@GLIBC_2.2.5'
/usr/bin/ld: /lib/x86_64-linux-gnu/libm.so.6: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status

cexp is in libm, and I think when using the C compiler as linker, this has to be specified explicitly.

Yes, that is a issue I noticed a while ago and fixed in my current branch.
I need to link m to cuda examples on linux.

@ahbarnett
Copy link
Collaborator

ahbarnett commented Aug 14, 2024 via email

@mreineck
Copy link
Collaborator Author

The "proper" solution for this is templated constants, which only arrived in C++20 unfortunately...

But in any case, once there are no more files that are compiled twice (except for the few lines of the standard C interface), all of this will no longer be an issue. So I propose to go with the macros for now; they will vanish in one of the subsequent pull requests.

Copy link
Collaborator

@DiamonDinoia DiamonDinoia 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 good. Merging after 2.3.0 is out.

@DiamonDinoia
Copy link
Collaborator

Hi @mreineck, we are finally ready to merge! Can you resolve the conflicts?

@mreineck
Copy link
Collaborator Author

Done (I think)!

@DiamonDinoia DiamonDinoia merged commit defdd48 into flatironinstitute:master Sep 10, 2024
167 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants