{"action":"created","issue":{"url":"https://api.github.com/repos/zdevito/terra/issues/172","repository_url":"https://api.github.com/repos/zdevito/terra","labels_url":"https://api.github.com/repos/zdevito/terra/issues/172/labels{/name}","comments_url":"https://api.github.com/repos/zdevito/terra/issues/172/comments","events_url":"https://api.github.com/repos/zdevito/terra/issues/172/events","html_url":"https://github.com/zdevito/terra/issues/172","id":155603587,"number":172,"title":"Void pointers in C structs","user":{"login":"alexbw","id":161935,"avatar_url":"https://avatars.githubusercontent.com/u/161935?v=3","gravatar_id":"","url":"https://api.github.com/users/alexbw","html_url":"https://github.com/alexbw","followers_url":"https://api.github.com/users/alexbw/followers","following_url":"https://api.github.com/users/alexbw/following{/other_user}","gists_url":"https://api.github.com/users/alexbw/gists{/gist_id}","starred_url":"https://api.github.com/users/alexbw/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alexbw/subscriptions","organizations_url":"https://api.github.com/users/alexbw/orgs","repos_url":"https://api.github.com/users/alexbw/repos","events_url":"https://api.github.com/users/alexbw/events{/privacy}","received_events_url":"https://api.github.com/users/alexbw/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2016-05-18T21:16:19Z","updated_at":"2016-05-20T04:15:15Z","closed_at":"2016-05-20T04:15:15Z","body":"**EDIT**: Torch was defining each of these types explicitly with FFI upon load. Not a Terra problem. Have some segfaults now, but those are also probably my fault.\r\n\r\nTrying to interact with the Torch numeric library using Terra. Torch is a little strange, but hopefully it's possible to have it play nicely with Terra.\r\n\r\nTorch abuses C macros to define its data types (it's all C, no C++, so it fakes templates, in a way). That actually seems to be fine with Terra, I'm able to successfully import all the function and struct definitions from the main Torch library (`TH/TH.h` if you're interested in poking around). I can see all the methods and types that I would expect to see using `terralib.includec`.\r\n\r\nThe core Torch data type, the `Tensor`, is a nested struct with the following layout:\r\n\r\n```\r\ntypedef struct THAllocator {\r\n  void* (*malloc)(void*, long);\r\n  void* (*realloc)(void*, void*, long);\r\n  void (*free)(void*, void*);\r\n} THAllocator;\r\n\r\ntypedef struct THDoubleStorage\r\n{\r\n    double *data;\r\n    long size;\r\n    int refcount;\r\n    char flag;\r\n    THAllocator *allocator;\r\n    void *allocatorContext;\r\n} THDoubleStorage;\r\n\r\ntypedef struct THDoubleTensor\r\n{\r\n   long *size;\r\n   long *stride;\r\n   int nDimension;\r\n   THDoubleStorage *storage;\r\n   long storageOffset;\r\n   int refcount;\r\n   char flag;\r\n} THDoubleTensor;\r\n```\r\n\r\n`Tensors` are optionally strided and offsetted views on contiguous memory in a `Storage` object, and `Storage` objects carry around their data, some metadata, and an `Allocator` object that points to internal memory management methods.\r\n\r\nThe following snippet of code:\r\n```\r\nlocal torch = require 'torch'\r\nterralib.includepath = 'path/to/my/torch/headers'\r\nlocal TH = terralib.includec('TH/TH.h')\r\n\r\nlocal emptyTensor = torch.zeros(10)\r\nlocal terraEmptyTensor = terralib.cast(TH.THDoubleTensor,emptyTensor) -- fails here\r\n```\r\n\r\nfails with this traceback:\r\n```\r\nsrc/terralib.lua:1337: attempt to redefine 'THAllocator'\r\nstack traceback:\r\n\t[C]: in function 'cdef'\r\n\tsrc/terralib.lua:1337: in function 'definecstruct'\r\n\tsrc/terralib.lua:1394: in function 'cstring'\r\n\tsrc/terralib.lua:1380: in function 'cstring'\r\n\tsrc/terralib.lua:1329: in function 'definecstruct'\r\n\tsrc/terralib.lua:1394: in function 'cstring'\r\n\tsrc/terralib.lua:1380: in function 'cstring'\r\n\tsrc/terralib.lua:1329: in function 'definecstruct'\r\n\tsrc/terralib.lua:1394: in function 'cstring'\r\n\tsrc/terralib.lua:4117: in function 'cast'\r\n\tffi-tensor-defs.t:9: in main chunk\r\n```\r\n\r\nAlso trying to use the Tensor type as either input or output type of terra functions fails with the same error, but the above snippet is the most minimal example.\r\n\r\nThe documentation says Terra has no void type. Is that a cause of this issue?\r\nAlso, should I expect Terra to deal well with nested structs like this?"},"comment":{"url":"https://api.github.com/repos/zdevito/terra/issues/comments/220513422","html_url":"https://github.com/zdevito/terra/issues/172#issuecomment-220513422","issue_url":"https://api.github.com/repos/zdevito/terra/issues/172","id":220513422,"user":{"login":"alexbw","id":161935,"avatar_url":"https://avatars.githubusercontent.com/u/161935?v=3","gravatar_id":"","url":"https://api.github.com/users/alexbw","html_url":"https://github.com/alexbw","followers_url":"https://api.github.com/users/alexbw/followers","following_url":"https://api.github.com/users/alexbw/following{/other_user}","gists_url":"https://api.github.com/users/alexbw/gists{/gist_id}","starred_url":"https://api.github.com/users/alexbw/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alexbw/subscriptions","organizations_url":"https://api.github.com/users/alexbw/orgs","repos_url":"https://api.github.com/users/alexbw/repos","events_url":"https://api.github.com/users/alexbw/events{/privacy}","received_events_url":"https://api.github.com/users/alexbw/received_events","type":"User","site_admin":false},"created_at":"2016-05-20T04:15:15Z","updated_at":"2016-05-20T04:15:15Z","body":"Works great, thanks"}}
{"actor":{"display_login":"alexbw"}}