Please advise how I can make my code more optimal with faster execution

Hello everyone, here I share a code that I have been working on:

defmodule GenerateMatrixRandom do
def mat() do
{:ok, file} = File.open(“MatrizA.txt”, [:write])
Enum.map(1…j(), fn _ ->
for _ <- 1 …j(), do: IO.write(file, “#{random_number()}\t”)
end)
File.close(file)

{:ok, file} = File.open(“MatrizB.txt”, [:write])
Enum.map(1…j(), fn _ ->
for _ <- 1 …j(), do: IO.write(file, “#{random_number()}\t”)
end)
File.close(file)
end

def mat_c©when is_list© do
{:ok, file} = File.open(“MatrizC.txt”, [:write])
v = Enum.concat©
Enum.reduce(v, fn x, acc -> IO.write(file, “#{x}\t”) end)
File.close(file)
end

def parse1() do
v = parseA()
Enum.chunk_every(v, j())
end

def parse2() do
v = parseB()
Enum.chunk_every(v, j())
end

def transpose([]), do: []
def transpose([[] | xss]), do: transpose(xss)
def transpose([[x | xs] | xss]) do
[[x | (for [h | ] <- xss, do: h)] | transpose([xs | (for [ | t] <- xss, do: t)])]
end

def multiply(_, []), do: []
def multiply(a = [x | _],b = [y | _]) when is_list(a) and is_list(b) and is_list(x) and is_list(y) do
multiply([], a, transpose(b))
end
def multiply(a, b = [h | t]) when is_number(a) and is_list(b) and is_list(h) do
[Enum.map(h, &(&1*a)) | multiply(a, t)]
end
def multiply(b = [h | _], a) when is_number(a) and is_list(b) and is_list(h) do
multiply(a, b)
end

def multiply(result, [], []), do: result
def multiply(result, [], _), do: result
def multiply(result, [a | rest_a], [b | rest_b]) when not is_list(a) and not is_list(b), do: multiply(a*b+result, rest_a, rest_b)
def multiply(result, [first_row_a | rest_a], b) when is_list(first_row_a) do
[Enum.reverse(Enum.reduce(b, [], fn(col_b,acc) -> [multiply(0, first_row_a, col_b) | acc] end)) | multiply(result, rest_a, b)]
end

def now, do: ({msecs, secs, musecs} = :erlang.timestamp; ((msecs*1000000 + secs)*1000000 + musecs)/1000000)

defp j(), do: 1000000

defp parseA() do
{:ok, contents} = File.read(“MatrizA.txt”)
contents |> String.split("\t", trim: true) |> Enum.map(fn n -> {v, _} = Float.parse(n); v end)
end

defp parseB() do
{:ok, contents} = File.read(“MatrizB.txt”)
contents |> String.split("\t", trim: true) |> Enum.map(fn n -> {v, _} = Float.parse(n); v end)
end

defp random_number() do
:rand.uniform() * 100
|> Float.round(8)
end

end

As you will see, I create two matrices of Millonxmillon I keep them in txt and then extract, multiply and save the answer in MatrixC.txt

Could you give me your comments and advise how I can make the code more optimal and have a faster execution.

Ah, matrix maths! There’s been a few previous discussions on the topic. The summary is that pure Elixir code isn’t ideal for matrix maths. See this thread for more info: Matrix in Elixir

If possible, I’d recommend using the Matrex library (https://github.com/versilov/matrex) as probably the most complete matrix math library in elixir. There’s some alternatives that might be good for you in the other forum posting I linked to above.

Specifically in your code replace all of the matrix multiplications, transposes with the equivalent Matrex functions. Also consider storing your matrix data in a binary format. Matrex supports the matlab binary format which should be exportable from Python, Matlab or others. The binary format should help speed up loading the data. One main limit of Matrex` is that it only uses single precision floating points.

If you really want to use pure Elixir, look into parallelizing the matrix operations into sub-matrices using multiple GenServer’s or Tasks. Or look at the Matrax library that uses OTP atomics to do matrix math: Matrax library - use :atomics as a matrix .

3 Likes