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

Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
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
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New

Other popular topics Top

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
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New

We're in Beta

About us Mission Statement