Search code examples
type-conversioncdataffitorchluajit

Best conversion between LuaJIT ffi cdata and torch Tensor


What's the best way to perform the conversion between LuaJIT ffi cdata [1] and Torch Tensor [2].

According to Mike's reply in lua-user mail list [3], if we really want to convert cdata to lua plain table, we have to do a loop to copy each item to a new created one. And actually Torch Tensor do provide some interface for better LuaJIT ffi access [4]. So, my current solution is do a loop first and convert the cdata to a lua plain table and then call the tensor construction function that create a tensor from a table [5].

But actually in my case, I need to do similar conversion between LuaJIT ffi cdata and Torch Tensor very frequently, is there any better approach rather than loop copy?


Solution

  • If your cdata represents a contiguous array of data then you can use ffi.copy. Here is a toy example:

    require 'torch'
    ffi = require 'ffi'
    
    -- create a random float array
    n = 3
    x = torch.rand(n):float()
    cdata = x:data()
    assert(type(cdata) == 'cdata')
    
    -- copy this cdata into a destination tensor
    y = torch.FloatTensor(n)
    ffi.copy(y:data(), cdata, n*ffi.sizeof('float'))
    
    assert(x:equal(y))