Skip to content

Many bug and warning fixes#537

Open
JanNiklasB wants to merge 23 commits intoCRPropa:masterfrom
JanNiklasB:CPP23CRPropa
Open

Many bug and warning fixes#537
JanNiklasB wants to merge 23 commits intoCRPropa:masterfrom
JanNiklasB:CPP23CRPropa

Conversation

@JanNiklasB
Copy link
Copy Markdown
Contributor

Hey,

with this PR I want to introduce the new CXX standard 23 to CRPropa, it enables us to use more modern code in future additions which should make it easier to contribute.
Furthermore CXX23 introduces the new deducing this which enables us to use CRTP (Curious Reoccurring Template Patterns) instead of vtables while keeping the fundamental structures of CRPropa, this is important for an eventual GPU extension.

I also tried to tackle many warnings that arose in the past, in the process of that I discovered some major bugs which I also fixed here (see comments in code for more details):

  • type instead of to_type in Variant.cpp
  • missing return in Variant.cpp
  • deprecated / missing ENDDO statements in sophia_interface.f
  • broken doxygen commands
  • depricated sprintf replaced with snprintf
  • missing include path in FindGooglePerfTools.cmake
  • missing brackets in Random.cpp
  • not initialized constScaleBendover in preconstructor
  • extends never set in PeriodicMagneticField::setExtends
  • never set Bphi in CMZField.cpp if r<=r1
  • issue between clang and gcc when formatting uint64_t, now using macro "%10" PRIu64 "\t" which set it automatically
  • added more modern build tool ninja to workflows

{
typedef std::pair<typename C::first_argument_type, Value>
typedef K first_argument_type;
typedef std::pair<first_argument_type, Value>
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

first_argument is removed in CXX23 so we need an additional template argument, the rest of the code is adjusted, user code is not affected as far as I see

typedef typename A::reference reference;
typedef typename A::const_reference const_reference;
typedef typename A::value_type& reference;
typedef const typename A::value_type* const_reference;
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

reference and const_reference are removed in CXX23, value_type is equivalent

class value_compare
: public std::binary_function<value_type, value_type, bool>
, private key_compare
: private key_compare
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

binary_function is not needed in newer CXX versions, its functionality is now done automatically and therefore it was removed

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ENDDO is the more modern approach, the old one also works but throws warnings

#ifdef ENABLE_FAST_WAVES
#include <immintrin.h>
#include <memory.h>
#include <memory>
Copy link
Copy Markdown
Contributor Author

@JanNiklasB JanNiklasB Apr 1, 2026

Choose a reason for hiding this comment

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

<memory> is more general, we had vocally reported issues in the past and <memory> seemed to fix them

}
else{
- B1*exp(-z*z/Hc/Hc)*R/r1*(3*r/r1- 2*r*r/r1*r1);
Bphi = - B1*exp(-z*z/Hc/Hc)*R/r1*(3*r/r1- 2*r*r/r1*r1);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

was forgotten

}

void PeriodicMagneticField::setExtends(const Vector3d &origin) {
void PeriodicMagneticField::setExtends(const Vector3d &extends) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

propably a typo


if (fields.test(SerialNumberColumn))
p += std::sprintf(buffer + p, "%10lu\t",
p += std::snprintf(buffer + p, buffersize - p, "%10" PRIu64 "\t",
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

"%10" PRIu64 "\t" is the same as "%10lu\t" in case of gcc and the same as "%10llu\t" in case of clang (that was a problem before)

int64_t b = randInt();
return (b + a << 32);
// a is shifted to the left to create a proper 64 bit integer
return (b + (a << 32));
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

same as the old one, but does not throw a warning

*p = twist(p[M - N], p[0], p[1]);
*p = twist(p[M - N], p[0], state[0]);
*p = twist(p[+M - N], p[0], p[1]);
*p = twist(p[+M - N], p[0], state[0]);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

enum-enum-conversion was removed, the + fixes that

std::string s;
v.push_back(s);
}
return Variant(v);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

was missing for some reason

#define INT_FUNCTION(to_type, fun, to) \
to Variant::fun() const { \
switch (type) { \
switch (to_type) { \
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

fatal typo

CMakeLists.txt Outdated

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED True)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Throws an error during configure step if the cmake standard is not met instead of some undefined behavior during build time, that should help the user to determine potentially outdated CXX standards

@rafaelab
Copy link
Copy Markdown
Member

rafaelab commented Apr 1, 2026

Hi @JanNiklasB

Thanks for the PR.

  • AssocVector is actually not needed, and neither is ref_ptr. A good redesign (e.g., CRPropa 4) would get rid of them and use the smart pointers from C++. The same is true for variants, since newer C++ standards already handle them quite well.

  • Ninja is NOT available by default in some systems, so this might be adding more complexity to CRPropa and making it harder for some people to install, especially in clusters.

  • You say you introduced C++23, but I don't see this strict requirement in cmakelists. If you say C++23 thinking of CRTP only, CRTP is a pattern applicable to other C++ standards. If C++23 it becomes a strict requirement, it might be problematic for older computing clusters. Therefore, while I'm totally in favour of updating CRPropa and use ≥ C++20, I have mixed feelings about doing that now instead of investing in a redesign.

@JanNiklasB
Copy link
Copy Markdown
Contributor Author

Hey,
thanks for the quick answer.

  • I would also be in favor of a redesign regarding AssocVector and ref_ptr for CRPropa4. But it feels like CRPropa 4 is still not that close, or is there something planned for the close future?

  • True, Ninja is not a system default, but it is easily installable over pip or local package managers like conda.
    Since it speeds up the compilation process of our workflows and is as easily available as numpy which we already require I thought it would be better to use it in the workflows, but I could of course reverse that change.

  • I was mainly thinking about "deducing this" or Explicit object member functions which where introduced in CXX23.
    I thought setting set(CMAKE_CXX_STANDARD 23) would be enough to make the requirement, we certainly can use the new features with this flag.
    I'm only working on newer clusters, but it seems to me that when working on clusters it is common to use package managers like conda, which normally enable the possibility of higher CXX standards despite an older system.
    Or would that be a hardware issue?
    However, I would be ok to reduce the CXX standard to 20 for now, as long as we get rid of the deprications.

@lukasmerten
Copy link
Copy Markdown
Member

Hi @JanNiklasB

I agree with @rafaelab that we should be careful with backward compatibility on older compute clusters. At a minimum we should include a warning in the next v3.3 release notes that we will increase the minimum required version in the future. In this way, people will have a relatively recent official release as a fall back option, when their cluster does not support cxx20/23.

How far CRPropa v4 is in the future depends in the end on us as the developers and can/should probably discussed in the next in person meeting.

For ninja, I guess that people will not have issues to install that if they manage to install numpy. However, I suggest to wait with that until after the v3.3 release.

@JanNiklasB
Copy link
Copy Markdown
Contributor Author

Ok, I still would like to at least get rid of the warnings and the few typo caused bugs, should I modify this PR or make a new one without the changes to CMAKE_CXX_STANDARD and workflows?

@lukasmerten
Copy link
Copy Markdown
Member

Fair enough. Fixing the typos and small bugs sounds good to me.
Do what ever is easier for you: updating this PR or creating a new one does not matter to me.

@JanNiklasB JanNiklasB changed the title New CXX standard 23 plus many bug and warning fixes Many bug and warning fixes Apr 1, 2026
@JanNiklasB
Copy link
Copy Markdown
Contributor Author

JanNiklasB commented Apr 1, 2026

I reversed the CXX23 requirement and ninja from the worklows, and changed the title accordingly.

@JanNiklasB
Copy link
Copy Markdown
Contributor Author

JanNiklasB commented Apr 1, 2026

I am not sure why the ubuntu64 / 64x test failed, it happened in testInteraction in line 533 where the secondary size was equal to zero, but since this was no problem in the other tests and there is a probability* (even though it is small) that no secondary is generated in this test, I think it is fine.

*:
ElasticScattering.cpp line 104-108:

// check for interaction
Random &random = Random::instance();
double randDist = -log(random.rand()) / rate;
if (step < randDist)
	return;

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.

3 participants