This is supposed to be a high-performance neural network analysis tool but the authors decided to write it in C and use jagged arrays for everything.
Just look at this shit. I bet it could be twice as fast an infinitely more readable by using C++ with a proper fucking linear algebra library like Armadillo. Yet, there are still boomers who promote C.
>but the authors decided to write it in C and use jagged arrays for everything. Welcome to the enjoyable world of code written by mathematicians, OP. They can't code for shit.
At least it looks like they've used proper variable names, instead of regular math notation.
Noah Russell
>I bet it could be twice as fast Prove it or STFU.
Leo Lee
That's some sexy ass triple indirection, I'm not gonna lie.
This is straightforward and readable. I don't know why you're complaining, OP.
Josiah Reed
"Proper variable names" tend to make numerical programs less readable if anything, as it deviates from the literature. I get that it's difficult to grasp for "outsiders", but trust me, when verbose variable names are tacked onto often terrible code, it gets even worse.
I do agree that a lot of people doing numerics suck at programming. Most of the time programming is something that's picked up on the side to solve problems in their main area of study, so it makes sense really.
Wyatt Myers
boomers love casting malloc and typedef'ing their structs and function pointers
Zachary Butler
he should use lib that handles allocations rather than implementing his own shit.
also isn't there some requirements for memory alignment for most SIMD instructions? Does just asking for malloc fulfills this?
William Cooper
it's very good for C++ compiler compatibility.
Easton Sanders
so is extern "C" {}
Grayson Mitchell
ok, but what if you wanted to branch your code to be C++ then? rewrite all of your mallocs or just cast them from the get go?
Jayden Rivera
yes, rewrite them to proper C++
Gabriel Ramirez
>I bet it could be twice as fast an infinitely more readable by using C++ Or a better language for numerical computing, like Fortran or Julia.
Kevin Bennett
While mathematicians often write terrible code, this doesn't look like code written by mathematicians, and the developer is a CS PhD.
Mason Parker
If they cared about that they wouldn't be using a (float ****) they would have 1 flat array and use math to convert a 4D index to a 1D index for lookups / insertions.
And there are still better ways to do this then that.
Lucas Sanders
that code is terrible for cache locality. it's just pointer chasing all the time.
Thomas Edwards
>It's probably written that way to minimize -(cache misses) Sorry, forgot the parens.
Ian Richardson
Why is he calling malloc like 100 times for the same object? Every single data point in the entire net is going to be in a different place on the heap. What the fuck is wrong with making a struct neuron? Having a layer be an array of neurons? With pointers between layers? At least allocate each LAYER in one malloc, Jesus.
Flat, singly malloc'd multdimensional arrays with array offsets calculated manually are the only way to write dynamically allocated arrays without destroying cache locality.
So I looked at it and it's a bit confusing. I realize this is in part due to not understanding how neural networks work. I think if I know how they should work, I would make a layer struct like this, store everything inside the member bytes, and use pointer arithmetic to get the other members. What do you think? struct layer { struct layer *prev, *next; int size; char bytes[]; };