kimlai

kimlai

Accessing current tensor value indexes in defn

Hello,

I’ve implemented a very basic “ray tracing” algorithm in Elixir, and given that it involves float arithmetic over large 2-dimensional data I thought that I’d try to port it to Nx to give it a performance boost. However being new to this kind of things I’m not sure that it’s actually feasible.

The algorithm is the following: I have a 2d height map, which is a two dimensional list with float values (for example [[2.0, 7.2], [1.3, 2.4]). I also have a ray tracing direction vector {dx, dy, dz}. For every point in my height map, I’ll trace a ray starting from that point, moving along this vector until I’m out of the map, or the ray bumps into an obstacle (meaning it’s height z is below the value of the height map at this point).

The elixir implementation looks like this:

for {row, i} <- Enum.with_index(height_map) do
  for {z, j} <- Enum.with_index(row) do
    is_in_the_shade?(to_map(height_map), i, j, z, sun_x, sun_y, sun_z)
  end
end

def is_in_the_shade?(height_map, x, y, z, sun_x, sun_y, sun_z) do
  case Map.fetch(height_map, {trunc(x), trunc(y)}) do
    :error -> # we've reached the end of the height_map without meeting any obstacle
      0
    {:ok, height} when height > z -> # we've met an obstacle
      1
    {:ok, height } -> # continue ray tracing along sun_v
      is_in_the_shade?(height_map, x + sun_x, y + sun_y, z + sun_z, sun_x, sun_y, sun_z)
  end
end

I’ve tried to port it to Nx like this:


shade(hm, sun_v: {sun_x, sun_y, sun_z}, height_map: hm)

defn shade({x, y, z}, opts \\ []) do
  {sun_x, sun_y, sun_z} = keyword!(opts, [:sun_v])
  height_map = keyword!(opts, [:height_map])
  while {res = 0, height_map, x, y, z, sun_x, sun_y, sun_z},
    res == 0 and x <= 0 and x > 256 and y <= 0 and y > 256 do
    if height_map[x][y] > z do
      { 1, height_map, x, y, z, sun_x, sun_y, sun_z }
    else
      { 0, height_map, x + sun_x, y + sun_y, z + sun_z }
    end
  end
end

The problem is that I can’t figure out how to get access to x and y here, I’ve tried to define my tensor as a list of tuples, like [{0, 0, 2.0}, {0, 1, 3.2}, {1, 0, 3.1}, {1, 1, 3.0}] but that doesn’t seem possible (I’m getting the error invalid value given to Nx.tensor/1, got: {0, 0, 38.75}, which makes sense tensors values need to be ints or floats).

Is there a way to make this work with Nx, or should I be looking at a C port to improve performance?

/nx

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
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
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
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
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
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement