How to use torch in C++ code

3,922 views
Skip to first unread message

Wuxia Zhao

unread,
Feb 26, 2014, 3:24:58 AM2/26/14
to tor...@googlegroups.com
For example, the testTorch.lua is as followings:
require 'torch'

function test(img)
  -- img is torch.FloatTensor(3,256,256)
  img[1][1][1] = 0
  return img
end

How to call function named test(img) in C++ code?
Could you help me to give an C++ example?

soumith

unread,
Feb 26, 2014, 4:45:02 AM2/26/14
to torch7 on behalf of Wuxia Zhao

--
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.
To post to this group, send email to tor...@googlegroups.com.
Visit this group at http://groups.google.com/group/torch7.
For more options, visit https://groups.google.com/groups/opt_out.

Wuxia Zhao

unread,
Feb 26, 2014, 7:18:33 AM2/26/14
to tor...@googlegroups.com
my lua file is :
function testTensor(img)
img[1] = 0.5;
print(img[1])
return img;
end

and my C++ file is:
 
int main()
{
 lua_State* L = luaL_newstate();
 luaL_openlibs(L);
 if (luaL_dofile(L,"func.lua") != 0)
 {
  std::cout<<"Load Lua File Error"<<std::endl;
 }

int numElements = 100;
float *mydata = new float[numElements];
THFloatStorage *mystorage =  THFloatStorage_newWithData(mydata, numElements);
THFloatTensor* mytensor = THFloatTensor_newWithStorage1d(mystorage, 0, numElements, 1);

lua_getglobal(L,"testTensor");    //testTensor

【const char* torch_Tensor_id = luaT_typenameid(L, "torch.Tensor");】——which got SegmentFault when running:
//luaT_newmetatable(L, "torch.Tensor", NULL, NULL, NULL, NULL);——which got SegmentFault when running as well:

luaT_pushudata(L, (void *)mytensor, torch_Tensor_id);

lua_call(L,1,1);
delete[] mydata;
 return 0;
}
 
   So I add  luaT_newmetatable, but failed. And then I use luaT_typenameid, filed again.

Could you help me to figure out why? And how to write the code to be accepted?
 

Wuxia Zhao

unread,
Feb 26, 2014, 7:32:20 AM2/26/14
to tor...@googlegroups.com
Besides, when I add a line:
require "torch"
in the lua file, luaL_dofile(L,"func.lua") fails.
How to fix it?

My make command is : g++ -I /usr/local/include -o test test.c -llua -lluaT -lTH -lm -ldl -L /usr/local/lib

Konstantinos Mentzelos

unread,
Apr 11, 2016, 10:01:31 AM4/11/16
to torch7
It's being a long time since you asked the question, but I have the same problem. How did you fix the problem with 'require'?
Reply all
Reply to author
Forward
0 new messages