How do I pass Torch Tensor data to C code?

548 views
Skip to first unread message

Marc

unread,
Aug 21, 2016, 3:50:43 PM8/21/16
to torch7
I am binding C (and C++) code to Lua 5.2. I have been able to pass primitives back and forth no problem with the Lua 5.2 C API. However, I've struggled to figure out how to pass a Torch Tensor (like torch.DoubleTensor({1,2,3,4})) to my C program in order to operate on the data.

For example, my wrapped class now allows something like

mylib =require('mylib')
obj
= mylib.new()

I would like to be able to have functions like setdata(data) and getdata() as follows.

data = torch.DoubleTensor({1,2,3,4})
obj
:setdata(data)
--stores [1,2,3,4] in the back-end C++ object
obj
:getdata()
--prints [1,2,3,4]

I know how to write the functions and make them work, but it's the passage of Tensors that I can't seem to figure out. I suspect luaT_checkudata(...) from Torch7's luaT API might be the right tool, but I can't figure it out. Can someone provide a simple example as to how to access the underlying array of a Torch Tensor in C?

Thanks!

Jonghoon Jin

unread,
Aug 21, 2016, 4:09:37 PM8/21/16
to torch7 on behalf of Marc
luaT API does the job but perhaps using FFI is much simpler to use

--
You received this message because you are subscribed to the Google Groups "torch7" group.
To unsubscribe from this group and stop receiving emails from it, send an email to torch7+unsubscribe@googlegroups.com.
To post to this group, send email to tor...@googlegroups.com.
Visit this group at https://groups.google.com/group/torch7.
For more options, visit https://groups.google.com/d/optout.

Marc

unread,
Aug 21, 2016, 6:30:08 PM8/21/16
to torch7
I'll keep that in mind, but I have already done a fair bit of work using the Lua C APIs and I'd like to finish in that realm. Do you have a solution to the OP?



On Sunday, August 21, 2016 at 4:09:37 PM UTC-4, Jonghoon Jin wrote:
luaT API does the job but perhaps using FFI is much simpler to use
On Sun, Aug 21, 2016 at 12:50 PM, Marc via torch7 <torch7+APn2wQdnymDJozVf8_V0EtywE2hwOHxh0LaHwrQ2ReSJTZnRirA-...@googlegroups.com> wrote:
I am binding C (and C++) code to Lua 5.2. I have been able to pass primitives back and forth no problem with the Lua 5.2 C API. However, I've struggled to figure out how to pass a Torch Tensor (like torch.DoubleTensor({1,2,3,4})) to my C program in order to operate on the data.

For example, my wrapped class now allows something like

mylib =require('mylib')
obj
= mylib.new()

I would like to be able to have functions like setdata(data) and getdata() as follows.

data = torch.DoubleTensor({1,2,3,4})
obj
:setdata(data)
--stores [1,2,3,4] in the back-end C++ object
obj
:getdata()
--prints [1,2,3,4]

I know how to write the functions and make them work, but it's the passage of Tensors that I can't seem to figure out. I suspect luaT_checkudata(...) from Torch7's luaT API might be the right tool, but I can't figure it out. Can someone provide a simple example as to how to access the underlying array of a Torch Tensor in C?

Thanks!

--
You received this message because you are subscribed to the Google Groups "torch7" group.
To unsubscribe from this group and stop receiving emails from it, send an email to torch7+un...@googlegroups.com.

alban desmaison

unread,
Aug 22, 2016, 4:54:28 AM8/22/16
to torch7

Marc

unread,
Aug 25, 2016, 12:02:44 AM8/25/16
to torch7
This did the trick. Thanks for the pointers!

I am having a new, related issue however. Perhaps you have a suggestion for that as well? (link: https://groups.google.com/forum/#!topic/torch7/rmIcBYCiFG8)



On Monday, August 22, 2016 at 4:54:28 AM UTC-4, alban desmaison wrote:
Hi,
You can use luaT functions to get the elements in C with:
Here is a small example how to get the arguments from the stack to C objects https://github.com/albanD/c-in-lua/blob/refacto/cModule/src.c#L14-L26

You can then use https://github.com/torch/torch7/tree/master/lib/luaT#void-luat_pushudatalua_state-l-void-udata-const-char-tname to return a tensor.

On Sunday, August 21, 2016 at 11:30:08 PM UTC+1, Marc wrote:
I'll keep that in mind, but I have already done a fair bit of work using the Lua C APIs and I'd like to finish in that realm. Do you have a solution to the OP?



On Sunday, August 21, 2016 at 4:09:37 PM UTC-4, Jonghoon Jin wrote:
luaT API does the job but perhaps using FFI is much simpler to use
Reply all
Reply to author
Forward
0 new messages