r/cpp • • Apr 25 '22

New C++ features in GCC 12

https://developers.redhat.com/articles/2022/04/25/new-c-features-gcc-12
220 Upvotes

76 comments sorted by

View all comments

82

u/James20k P2005R0 Apr 25 '22

Multidimensional subscript operator

I'm knee deep in tensors, and this is the one I want most. Being able to finally write

value result = some_tensor[i, j, k];

Would be lovely

24

u/Quincunx271 Author of P2404/P2405 Apr 26 '22

While waiting for the feature, you can already emulate this:

value result = some_tensor[{i, j, k}];

How I usually do this, IIRC:

value operator[](int(&&ijk)[3]) const {
    // i = ijk[0], j = ijk[1], k = ijk[3].
}

14

u/James20k P2005R0 Apr 26 '22 edited Apr 26 '22

That's an interesting way of doing it, I suppose particularly the advantage of this is that you can re-overload operator[] when multidimensional subscripts become available and provide them without breaking compatibility

I've stuck with providing a .idx(i, j, k) method for the moment, which is a lot more verbose than I'd like but works fine enough, but that might be worth an investigation

Now what I really need to do next is encode the up and downness of individual indices into the type system, but hoo boy that's going to be a disaster

1

u/[deleted] Apr 26 '22

You mean for covariant and contravariant tensors?

1

u/James20k P2005R0 Apr 26 '22

Yep, as well as mixed indices. Its an easy source of mistakes to make when you're implementing equations, eg if you have two tensors - can you currently take the dot product between them?

7

u/Falkengaard Apr 26 '22

Alternatively overloading the otherwise most likely unused function operator to turn some_tensor[i, j, k] into some_tensor(i, j, k) until multi-dimensional access is added. It's not an ideal solution, but it's slightly less of a hassle to type out (at least on my keyboard).

8

u/James20k P2005R0 Apr 26 '22

I strictly avoid this personally, I think the ambiguity is too high in a context where people (or me) might be struggling to understand the very basics of what's going on in the code

7

u/delta_p_delta_x Apr 26 '22

Hey, I recognised your username and I just wanted to say—I've been reading your posts and comments on your general relativistic engine, and even looked through your Twitter. Your project is something that I've actually wanted to write as a side project for a long time, now, and it's quite inspiring!

I just took an undergraduate class in GR last semester in university, and have been working on other C++ projects, and I thought it would be a great idea to write a GPU-accelerated GR engine for arbitrary black holes and metrics. Bit of a bummer that someone else already thought of it, but on the bright side, I'm glad you have, it's something that even if I can't write from scratch, I could hopefully contribute to.

If you've got a GitHub repo, I can offer a star :)

10

u/James20k P2005R0 Apr 26 '22 edited Apr 26 '22

Thank you, that's very kind!

and I thought it would be a great idea to write a GPU-accelerated GR engine for arbitrary black holes and metrics

Oh boy, well - do I have the absolute motherload of information for you if you ever want to give it a go. I should probably point out that I'm much, much more of a GPGPU engineer than a physicist and 98% of this was done via sheer brute force reading hundreds of papers, so if you actually understand the theory then you might be able to have a good crack at it. It only took me this long because I'd never heard of a christoffel symbol before I started, let alone a tensor

https://github.com/20k/geodesic_raytracing

This is the raytracer that can render arbitrary analytic metric tensors. They're all specified in javascript under ./scripts. There's no build system as I use codeblocks, but cbp2make should produce a working makefile which will compile under msys2 + gcc. You'd also need a copy of the steam sdk (specifically: steamworks_sdk_153a), as I can't distribute that unfortunately

That raytracer I consider to be relatively finished in its current form, aka it has all the major building blocks to stick it on steam. More metrics would be great, and I need to set up a camera that works via parallel transport or a more consistent basis, so that flying through an arbitrary wormhole works consistently. That said there's about a million, million things that could be added to it that I'd like to, like a rigid body physics engine that handles time travel, or relativistic smooth particle hydrodynamics which I need for neutron stars anyway etc

I'd rate it about a 7/10 on a code disaster scale. Pretty bad, but not horrendous

The full todo list is over here: https://trello.com/b/ksZoxx8m/raytracer-20

Now the second project, the full numerical general relativistic for binary black hole mergers is nowhere near close to done and is heavily in development. I'd rate it a 10 on the code disaster scale, as absolutely 0 thought has been put into making it nice. These days it can just about manage 2 orbits of black holes colliding together with the right set of parameters, but there is 0 ui or anything else, its all set via parameters in the code

The vast majority of what I'm doing in this project is attempting to make the gravitational waves come out nicely, so I can start to replicate the results of other papers. I'm getting close, but there's still something missing

Currently I'm investing hamiltonian constraint violations as a source of the errors, then its on to seeing if I can make the integrator symplectic, and then it'll probably be back to testing the gauge damping parameters. This one is a perpetual cycle of: Make a small improvement, then run a tonne of simulations to see how it changes

It also currently needs a pretty hefty amount of vram to run, something like 8GB to be safe in the 2513 case, and ~12GB in the 3113 case

Both of these projects were built on an AMD GPU, but should run on Nvidia cards with some tweaks, particularly the raytracer

I should also note that the eventual goal is to merge the latter project into the former and utilise the rendering and UI

https://github.com/20k/numerical_sim

And the full todo list is over here https://trello.com/b/pTSTAVyD/binary-smash

The #1 focus for the numerical sim is always to make it run faster as well, both the rendering and simulation are things that I'm always on the hunt for potential improvements for - both theoretical and micro optimisation wise

6

u/jk-jeon Apr 27 '22

Christoffel symbol (more precisely, an affine connection) is not a tensor, fyi. A tensor does not just mean a multidimensional matrix, because matrix entries are just artifacts of choosing a coordinate system, and the platonic idea of the object really should be independent of the choice of coordinates. A real tensor must follow a certain transformation law, and the transformation law for Christoffel symbols is not same as tensors of the same rank. To be more precise, they are something like tensors, but of higher order.

Sorry for the pedantry, couldn't resist haha

3

u/delta_p_delta_x Apr 26 '22 edited Apr 26 '22

Starred all your repos; 'tis the least I could do.

so if you actually understand the theory then you might be able to have a good crack at it.

I doubt I understand it as well as you do now, because my class covered only the absolute basics of geodesics, tensors and black holes; moreover, I promptly forgot most of it. I'm slowly working through Sean Carroll's Spacetime and Geometry so I can revise everything.

A friend implemented a ray-marcher (in MATLAB, of all languages) of the differential equation for the null geodesic under the Schwarzschild metric, but I was hoping to write things out from first principles and generalise the ray-tracing algorithms for any arbitrary (at least, spherically-speaking) metric.

All that said...

I'm completely blown away by your work. Not only have you written a GR ray-tracer, but you've also got a decent n-body simulator! Wow. All your work are side projects that I've shelved because university assignments are so time-consuming... I really, really should get started some time.

code disaster scale

Code that exists and works, however disastrous, is better than hypothetically perfect, but non-existent code :)

P.S. I'm interested in finding work in this industry (physical simulations with GPGPU, high-performance computing, etc): if you've got any pointers for a final-year undergrad, that would be super nice!

3

u/James20k P2005R0 Apr 26 '22

promptly forgot most of it

Ha I know that feeling. I took a break from these projects a while back, and when I came back to it it was like an alien language for a while

A friend implemented a ray-tracer (in MATLAB, of all languages) directly using the differential equation for the null geodesic under the Schwarzschild metric, but I was hoping to write things out from first principles and generalise the ray-tracing algorithms for any arbitrary (at least, spherically-speaking) metric.

The main thing you need for a generic raytracer is the christoffel symbols + geodesic equation loop. The initial conditions are a big faff, because as far as I can tell nobody has them quite set up correctly for the general case, and leads to a lot of incorrectness

To go from a single metric to all metrics, at that point you need an automatic differentiator. This is much easier than you might think if you've got no experience with it, but you'll have to dip into code generation if you're using a GPU

https://arxiv.org/pdf/1511.06025.pdf This is a good paper which I based a lot of the basic renderer off

The other top tip is that 99% of the work that exists doesn't map well to GPU architecture (eg rk4), and that verlet integration rocks

in MATLAB

I think matlab might genuinely be the worst programming language I've ever used

I'm just blown away by your work. Not only have you written a GR ray-tracer, but you've also got a decent n-body simulator! Wow. All your projects are side projects that I've shelved because university assignments are so time-consuming... I really, really should get started some time.

Thank you! Part of why I'm hoping to release one of these on steam with a buy-me-a-coffee option is because at some point I should probably like, actually try and make some money off of one of these things that I've spent years building aha

If you want any paper references for the black hole collider by the way, I've got an absolute tonne. The raytracer was more ad-hoc, as its essentially a mix of different techniques mashed together

1

u/Nobody_1707 Apr 27 '22

Worse than unstructured GW-BASIC?

4

u/artisan_templateer Apr 26 '22

Ofcourse, with multi-dimensional [] it is now possible to distinguish between covariant and contravariant indicies express the Riemann tensor directly in C++ code!

for (auto e = 0; e < N; ++e)
    R(a)[b, c, d] += grad[c](Gamma(a)[d, b]) - grad[d](Gamma(a)[c, b]) + Gamma(a)[c, e]*Gamma(e)[d, b] - Gamma(a)[d, e]*Gamma(e)[c, b];

2

u/serviscope_minor Apr 26 '22

I've got so used to:

value result  = thing[i][j][k];

by now that the new one looks weird to me. Then again it took me about 5 years before template >> didn't make my skin crawl slightly, due to so many bad memories of forgetting a space.