[ create a new paste ] login | about

Link: http://codepad.org/hEJOhpAJ    [ raw code | fork ]

Lua, pasted on Mar 18:
local floor = math.floor
local rand = math.random
local ffi = require("ffi")
local clock = os.clock
local write = io.write

local darray = ffi.typeof("double[?]")
local iarray = ffi.typeof("int[?]")

local function random_vector(n)
    local v = darray(n)
    for x = 0, n - 1 do v[x] = rand() end
    return v
end

local n, nz = 1000, 5000

ffi.cdef [[
    void matmult( int M, double *y, double *val, int *row,
        int *col, double *x, int NUM_ITERATIONS);
]]

local lc = ffi.load("./loopc.so")

local nr  = floor(nz/n)
local anz = nr * n
local vx  = random_vector(n)
local val = random_vector(anz)
local vy, col, row = darray(n), iarray(nz), iarray(n + 1)
row[0] = 0
for r = 0, n - 1 do
    local step = floor(r / nr)
    if step < 1 then step = 1 end
    local rr = row[r]
    row[r + 1] = rr + nr
    for i = 0, nr - 1 do
        col[rr+i] = i*step
    end
end

local cycles = 1

while true do
    local tm = clock()
    --lc.matmult(n, vy, val, row, col, vx, cycles)
    for p=0,cycles-1 do
        for r=0,n-1 do
            local sum = 0
            for i=row[r],row[r+1]-1 do sum = sum + vx[col[i] ] * val[i] end
            vy[r] = sum
        end
    end
    tm = clock() - tm
    if tm >= 2.0 then
      local res = (anz*cycles*2) / tm * 1.0e-6
      write(res, "\n")
      return
    end
    cycles = cycles * 2
end


Create a new paste based on this one


Comments: