Skip to content

Instantly share code, notes, and snippets.

@lazerwalker
Last active June 29, 2016 22:41
Show Gist options
  • Save lazerwalker/5bff5d504dcfaa2fa7620b8492f2cd24 to your computer and use it in GitHub Desktop.
Save lazerwalker/5bff5d504dcfaa2fa7620b8492f2cd24 to your computer and use it in GitHub Desktop.
// ... other code as included in master
- (void)initialize
{
// initialize Lua stack
lua_executable_dir("./lua");
L = lua_open();
luaL_openlibs(L);
[self addLuaPackagePathForBundlePath:[[NSBundle mainBundle] resourcePath] subdirectory:nil];
NSString *frameworkResourcesPath = [self bundleResourcesPathForFrameworkName:@"Torch.framework"];
[self addLuaPackagePathForBundlePath:frameworkResourcesPath subdirectory:nil];
// load Torch
luaopen_libtorch(L);
[self requireFrameworkPackage:@"torch" frameworkResourcesPath:frameworkResourcesPath];
// load dok
[self requireFrameworkPackage:@"dok" frameworkResourcesPath:frameworkResourcesPath];
// load nn
luaopen_libnn(L);
[self requireFrameworkPackage:@"nn" frameworkResourcesPath:frameworkResourcesPath];
// load nnx
luaopen_libnnx(L);
[self requireFrameworkPackage:@"nnx" frameworkResourcesPath:frameworkResourcesPath];
// load image
luaopen_libimage(L);
[self requireFrameworkPackage:@"image" frameworkResourcesPath:frameworkResourcesPath];
[self requireFrameworkPackage:@"paths" frameworkResourcesPath:frameworkResourcesPath];
[self requireFrameworkPackage:@"sys" frameworkResourcesPath:frameworkResourcesPath];
[self requireFrameworkPackage:@"xlua" frameworkResourcesPath:frameworkResourcesPath];
[self requireFrameworkPackage:@"graph" frameworkResourcesPath:frameworkResourcesPath];
[self requireFrameworkPackage:@"nngraph" frameworkResourcesPath:frameworkResourcesPath];
[self requireFrameworkPackage:@"optim" frameworkResourcesPath:frameworkResourcesPath];
return;
}
#import <Foundation/Foundation.h>
#import "TorchInterface.h"
lua_State *init_torch_vm(){
lua_executable_dir("./");
lua_State *L = luaL_newstate();
luaL_openlibs(L);
/*
* Set the Lua package path to include the framework/lua folder, so that the require command in lua works as expeceted.
*/
NSString *luaPackagePath = [NSString stringWithFormat:@"package.path='/?.lua;%@/framework/lua/?.lua;'.. package.path", [[NSBundle mainBundle] resourcePath]];
if (luaL_dostring(L, [luaPackagePath UTF8String]) != 0) {
NSLog(@"Could not update LUA path, error message: %s", lua_tostring(L, -1));
}
/*
* Load the torch C library into lua and set up the wrappers,
*/
luaopen_libtorch(L);
run_torch_script(L,@"/framework/lua/torch/init.lua");
luaopen_libnn(L);
run_torch_script(L,@"/framework/lua/nn/init.lua");
run_torch_script(L,@"/framework/lua/paths/init.lua");
run_torch_script(L,@"/framework/lua/sys/init.lua");
run_torch_script(L,@"/framework/lua/xlua/init.lua");
run_torch_script(L,@"/framework/lua/graph/init.lua");
run_torch_script(L,@"/framework/lua/nngraph/init.lua");
run_torch_script(L,@"/framework/lua/optim/init.lua");
return L;
}
int run_torch_script(lua_State *L,NSString *file_name){
NSString *scriptPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingString:file_name];
int ret = luaL_dofile(L, [scriptPath UTF8String]);
if (ret != 0) {
NSLog(@"Error in Torch Script: %s", lua_tostring(L, -1));
}
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment