pat_rick

pat_rick

Can Elixir (with Nx) be used for Computational Calculations (e.g. linear equation solvers, FEM simulations)

I have been trying to find resources for implementing computational calculations with elixir (and possibly Nx), namely something like the Gauss-Seidel method for solving linear equations as well as using elixir for FEM simulations, maybe with a library such as The FEniCSx computing platform.

However, since I couldn’t find anything, I was wondering if elixir is even a good fit for such a use case.

I also found the following clip from the BEAM Channel about When should you not use Erlang/Elixir from Zack where he states that for numeric computations’ elixir would not be a good choice.

Could you guys give me advice about the above-mentioned use case?

Did the Nx library change the use case for elixir?

And if you have any resources, I would greatly appreciate them!

Thank you in advance.

/nx

Most Liked

al2o3cr

al2o3cr

In “normal” BEAM code, both arrays and floating-point values are expensive:

  • a plain list like [0.1, 0.2, 0.3, 0.4] is a linked list, so it takes about 2x as long to access the 4th element as it does the second. This makes large lists like in simulations impossibly slow, adding an extra factor of N to runtimes in general. (good luck with multidimensional data :skull: )

  • updating an element in the middle of a list is expensive, since it requires updating all the linked list cells before the element. This collides badly with a lot of scientific algorithms that assume that updating a value in-place is cheap.

  • floating-point values themselves are expensive, because they are boxed

The boxing and the linked-list combined end up making a single element of a floating-point list like [1.0, 2.0, 3.0, 4.0] cost 28 bytes (on a 64-bit machine):

  • two pointers for the cons cell
  • a 4-byte header + 8-byte value for the float

Nx removes all that overhead, because it stores tensors with fixed datatype (u64, f32, etc) as contiguous blocks of memory. The Nx intro has more.

There’s a ton of other useful features for numerical computing in Nx, but even if all you used it for was to get constant-time access to multidimensonal arrays it’s a huge increase in Elixir’s capabilities.

al2o3cr

al2o3cr

I haven’t seen anything specific along those lines; Nx is new enough that it mostly has the features that somebody needed to get the thing done they wanted to do, so a lot of the focus has been on ML-adjacent problems and things that parallelize well to GPUs.

There’s some basic algorithms in Nx.LinAlg, but you won’t find optimizations like “store L and U from LU decomposition together in the same array to save space that would otherwise be zeroes” there.

On the positive side, that means there’s plenty of unclaimed library real-estate just waiting for an author to put some code in it! :stuck_out_tongue:

One other option: since Nx stores values in contiguous blocks, it would be much easier to interface to a Serious Science numeric library with Rustler (versus trying to convert to and from linked-lists).

josevalim

josevalim

Creator of Elixir

Scholar can also provide some examples/ideas: https://hexdocs.pm/scholar/Scholar.html

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement