如何调用使用C ++中的Torch7包的lua脚本?

时间:2016-06-16 09:19:20

标签: c++ lua cmake torch

我有一个名为DeepQ.cpp的c ++源代码,我试图在lua脚本DeepQLearner.lua中调用某些函数。这个lua脚本使用Torch7包。我知道如何从C ++调用一个简单的Lua脚本,但似乎没有太多资源来学习如何在其中包含Torch内容。我的脚本DeepQ.cpp和DeepQLearner.lua如下:

DeepQ.cpp

using System;
using System.Diagnostics;

static class Program
{
    static void Main(string[] args)
    {

        byte[] results = new byte[6 * 5245786];
        byte[] current = new byte[6];
        int offset = 0;
        var watch = Stopwatch.StartNew();
        Populate(results, ref offset, current, 0);
        watch.Stop();
        Console.WriteLine("Time to generate: {0}ms", watch.ElapsedMilliseconds);
        Console.WriteLine("Data size: {0}MiB",
            (results.Length * sizeof(byte)) / (1024 * 1024));
        Console.WriteLine("All generated; press any key to show them");
        Console.ReadKey();
        for (int i = 0; i < 5245786; i++)
        {
            Console.WriteLine(Format(results, i));
        }
    }
    static string Format(byte[] results, int index)
    {
        int offset = 6 * index;
        return results[offset++] + "," + results[offset++] + "," +
           results[offset++] + "," + results[offset++] + "," +
           results[offset++] + "," + results[offset++];
    }

    static void Populate(byte[] results, ref int offset, byte[] current, int level)
    {
        // pick a new candidate; note since we're doing C not P, assume ascending order
        int last = level == 0 ? 0 : current[level - 1];
        for (byte i = (byte)(last + 1); i <= 42; i++)
        {
            current[level] = i;
            if (level == 5)
            {
                // write the results
                results[offset++] = current[0];
                results[offset++] = current[1];
                results[offset++] = current[2];
                results[offset++] = current[3];
                results[offset++] = current[4];
                results[offset++] = current[5];
            }
            else
            {
                // dive down
                Populate(results, ref offset, current, level + 1);
            }
        }
    }
}

DeepQLearner.lua

#include <iostream>
#include <cstdlib>

extern "C" {
    #include "lua.h"
    #include "lualib.h"
    #include "lauxlib.h"
}

using namespace std;
lua_State* L;
int main (int argc, char** argv)
{  
  L = lua_open();

  //load Lua base libraries 
  luaL_openlibs(L);

  // initialize Lua 
  luaL_dofile(L, "/home/sp/DeepL/DeepQLearner.lua");

  lua_getglobal(L, "initialize");
  lua_call(L, 0, 1);

  return(0)
}

这是我的CMakeLists.txt,只要没有使用Torch模块,它就可以用于调用lua脚本:

require 'cutorch'
require 'cunn'
require 'image

function initialize()
  -- some code here
end

我认为这在Torch的情况下不起作用,因为Torch默认使用LuaJIT。在CMakeLists.txt中,我尝试了cmake_minimum_required (VERSION 2.4.6) project (DeepL) set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin) set(CMAKE_BUILD_TYPE RelWithDebInfo) find_package(Lua51 REQUIRED) include_directories ( ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR} ${LUA_INCLUDE_DIR} ) add_executable (${PROJECT_NAME} src/DeepQ.cpp) target_link_libraries (${PROJECT_NAME} lua5.1) ,但没有成功。如何修改CMakeLists.txt以便使用LuaJIT而不是Lua5.1执行lua代码并正确找到Torch包,以及.cpp文件中需要包含哪些其他头文件?提前致谢。

0 个答案:

没有答案