lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Mike Pall wrote:
<snip>
> Loops over vectors are certainly faster if written in plain Lua
> and compiled with LuaJIT (provided the vectors are not too short).
> 
> [I can understand the desire to avoid rewriting all of cblas in
> Lua, but a daxpy loop seems easy. And BTW: do NOT unroll it by
> hand, this is counter-productive on modern CPUs.]
<snip>

Talking about vectors, how does luajit fare when dealing with strided vectors?
Simple example:

local ffi = require "ffi"
ffi.cdef"typedef struct { int size, stride; double *data; } Vector;"
local newvector = ffi.typeof "Vector"
local datablock = ffi.typeof "double[?]"
local _D = setmetatable({}, {__mode = "k"})

function vector (n)
  local data = datablock(n)
  local v = newvector()
  v.size = n
  v.stride = 1
  v.data = data
  _D[v] = data
  return v
end

-- assumes unitary stride
function set1 (u, v)
  for i = 0, u.size - 1 do
    u.data[i] = v.data[i]
  end
  return u
end

function set (u, v)
  for i = 0, u.size - 1 do
    u.data[i * u.stride] = v.data[i * v.stride]
  end
  return u
end

Are set1 and set comparable? (I'm not sure "set" is the best way of
implementing a strided version...)

Cheers,
Luis

-- 
Computers are useless. They can only give you answers.
                -- Pablo Picasso

-- 
Luis Carvalho (Kozure)
lua -e 'print((("lexcarvalho@NO.gmail.SPAM.com"):gsub("(%u+%.)","")))'