Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Libtorch与g+合作+;,但是英特尔编译器失败了_C++_C++11_Cmake_Fortran_Pytorch - Fatal编程技术网

C++ Libtorch与g+合作+;,但是英特尔编译器失败了

C++ Libtorch与g+合作+;,但是英特尔编译器失败了,c++,c++11,cmake,fortran,pytorch,C++,C++11,Cmake,Fortran,Pytorch,我想在Fortran程序中使用Python开发的神经网络(PyTorch)。我的操作系统是Ubuntu 18.04 我在做什么: 另存为torchscript:TurbNN.pt 从C++程序调用它:Call T.S.CPP,CalqTys.H 从FORTRAN程序调用B++程序(使用BIN):MF.F90 我使用CMake(3.19.4)和g++(7.5.0)成功地编译了代码。但是,我无法使用英特尔编译器(HPCKit 2021.1.0.2684)编译它们: 在“cmake”之后,一切看起来都

我想在Fortran程序中使用Python开发的神经网络(PyTorch)。我的操作系统是Ubuntu 18.04

我在做什么:

  • 另存为torchscript:TurbNN.pt
  • 从C++程序调用它:Call T.S.CPP,CalqTys.H
  • 从FORTRAN程序调用B++程序(使用BIN):MF.F90
  • 我使用CMake(3.19.4)和g++(7.5.0)成功地编译了代码。但是,我无法使用英特尔编译器(HPCKit 2021.1.0.2684)编译它们:

    在“cmake”之后,一切看起来都很好(就像g++):

    然而,在“制造”之后,我得到:

    Scanning dependencies of target call_ts_cpp
    [ 25%] Building CXX object CMakeFiles/call_ts_cpp.dir/src/call_ts.cpp.o
    [ 50%] Linking CXX shared library lib/libcall_ts_cpp.so
    [ 50%] Built target call_ts_cpp
    Scanning dependencies of target fortran_calls_ts.x
    [ 75%] Building Fortran object CMakeFiles/fortran_calls_ts.x.dir/src/main.f90.o
    [100%] Linking Fortran executable bin/fortran_calls_ts.x
    lib/libcall_ts_cpp.so: undefined reference to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
    lib/libcall_ts_cpp.so: undefined reference to `torch::jit::load(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, c10::optional<c10::Device>, std::unordered_map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::hash<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::equal_to<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&)'
    lib/libcall_ts_cpp.so: undefined reference to `c10::DeviceTypeName[abi:cxx11](c10::DeviceType, bool)'
    lib/libcall_ts_cpp.so: undefined reference to `torch::jit::Object::find_method(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) const'
    lib/libcall_ts_cpp.so: undefined reference to `torch::jit::Method::operator()(std::vector<c10::IValue, std::allocator<c10::IValue> >, std::unordered_map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, c10::IValue, std::hash<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::equal_to<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, c10::IValue> > > const&)'
    CMakeFiles/fortran_calls_ts.x.dir/build.make:106: recipe for target 'bin/fortran_calls_ts.x' failed
    make[2]: *** [bin/fortran_calls_ts.x] Error 1
    CMakeFiles/Makefile2:123: recipe for target 'CMakeFiles/fortran_calls_ts.x.dir/all' failed
    make[1]: *** [CMakeFiles/fortran_calls_ts.x.dir/all] Error 2
    Makefile:148: recipe for target 'all' failed
    make: *** [all] Error 2
    
    梅因。f90

    ! fortran program main
    !
    ! calls c++ function invariant_nn, which calls a torchscript with
    ! a neural network
    !
    
    program main
    
        ! define interface to interact with c++
        use, intrinsic :: iso_c_binding, only: c_float
        implicit none
        interface invariant_nn
            subroutine invariant_nn(I, G) bind (c)
                import :: c_float
                real(c_float) :: I(5)
                real(c_float) :: G(10)
            end subroutine
        end interface
    
        ! fortran program
        real(4) I(5), G(10)
    
        ! invariants
        I(1) = 1.01
        I(2) = 1.01
        I(3) = 1.01
        I(4) = 1.01
        I(5) = 1.01
        print *, "Tensor invariants ", I
    
        ! tensor basis coefficients
        call invariant_nn(I, G)    
        print *, "Tensor basis coefficients ", G
    
    end program main
    
    CMakeLists.txt

    # stop configuration if cmake version is below 3.0
    cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
    
    # project name and enabled languages
    project(fortran_calls_ts CXX Fortran)
    
    # find libtorch
    find_package(Torch REQUIRED)
    
    # if CMAKE_BUILD_TYPE undefined, set it to Release
    if(NOT CMAKE_BUILD_TYPE)
        set(CMAKE_BUILD_TYPE "Release")
    endif()
    
    # compiler flags for release mode
    set(CMAKE_CXX_FLAGS_RELEASE "-O3")
    set(CMAKE_Fortran_FLAGS_RELEASE "-O3")
    
    # set default build paths
    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
    set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
    
    # generated export header will be placed here
    include_directories(${PROJECT_BINARY_DIR})
    
    # c library
    add_library(call_ts_cpp SHARED src/call_ts.cpp)
    
    # fortran executable
    add_executable(fortran_calls_ts.x src/main.f90)
    
    # linked against c library
    target_link_libraries(fortran_calls_ts.x call_ts_cpp)
    target_link_libraries(call_ts_cpp "${TORCH_LIBRARIES}")
    
    # we let cmake generate the export header
    include(GenerateExportHeader)
    generate_export_header(call_ts_cpp BASE_NAME call_ts)
    
    install(TARGETS call_ts_cpp LIBRARY DESTINATION lib ARCHIVE DESTINATION lib)
    
    install(FILES src/call_ts.h ${PROJECT_BINARY_DIR}/call_ts_export.h DESTINATION include)
    
    set_property(TARGET fortran_calls_ts.x PROPERTY CXX_STANDARD 14)
    

    您是否在链接器错误中看到
    cxx11
    ?看起来您的
    libcall\u ts\u cpp
    的编译方式需要新的C++11 ABI for std::string,但实现这些函数的库可能是用旧的ABI编译的。这里有一篇关于同样问题的PyTorch论坛帖子:


    解决方案是下载使用新的C++11 ABI构建的PyTorch库的新副本。

    在链接器错误中是否看到
    cxx11
    ?看起来您的
    libcall\u ts\u cpp
    的编译方式需要新的C++11 ABI for std::string,但实现这些函数的库可能是用旧的ABI编译的。这里有一篇关于同样问题的PyTorch论坛帖子:


    解决方案是下载使用新的C++11 ABI构建的PyTorch库的新副本。

    “我在使用旧的g++时解决了类似的问题”-当时的问题是什么,您是如何解决的?问题是:lib/libcall_ts_cpp.so:对'c10::Error::Error(c10::SourceLocation,std:xx11::basic_string')的未定义引用,等等(请参阅“make”命令之后的内容)。对于g++来说,只需更新它即可修复。“我在使用旧的g++时修复了类似的问题”-当时的问题是什么,您是如何解决的?问题是:lib/libcall\u ts\u cpp.so:未定义对`c10::Error::Error(c10::SourceLocation,std:\u cx11::basic\u string')的引用,等等(请参阅“make”命令之后的内容)。对于g++来说,它只是通过更新来修复的。谢谢你的回答。我很愚蠢。我尝试使用最新版本的Torchlib和夜间版本。但是,我不知道有两个版本可用:cxx11之前的ABI和cxx11 ABI。我总是下载第一个,但第二个对我有效!@arsen如果这解决了你的问题c单击复选标记并接受it@Yakk-AdamNevraumont谢谢,完成了。我不知道我应该这么做。谢谢你的回答。我很愚蠢。我试图使用最新版本的Torchlib和nightly版本。但是,我不知道有两个版本可用:cxx11之前的ABI和cxx11 ABI。我总是下载第一个版本,但是第二个对我有用!@arsen如果这解决了你的问题,请单击复选标记并接受it@Yakk-AdamNevraumont谢谢,完成了。我不知道我应该这么做。
    #include "call_ts.h"
    #include <torch/script.h>
    #include <iostream>
    #include <memory>
    
    // c++ function invariant_nn
    //
    // takes inputs, reads a neural network TurbNN.pt, do a forward pass
    // and returns outputs
    //
    // inputs: 5 tensor invariants I[0:4] (float)
    // outputs: 10 tensor basis coefficients G[0:9] (float)
    
    void invariant_nn(float I[], float G[])
    {
        // deserialize scriptmodule from a .pt file
        torch::jit::script::Module module;
        const char *arg;
        arg = "../src/TurbNN.pt";
        module = torch::jit::load(arg);
    
        // create inputs
        std::vector<torch::jit::IValue> inputs;
        float data[] = {I[0], I[1], I[2], I[3], I[4]};
        inputs.push_back(torch::from_blob(data, {1, 5}));
        //std::cout << "inputs\n" << inputs;
        //std::cout << "\n";
    
        // do forward pass and turn its output into a tensor
        at::Tensor outputs = module.forward(inputs).toTensor();
        //std::cout << "outputs\n" << outputs;
        //std::cout << "\n";
    
        // return values
        for (int k = 0; k < 10; k++) {
            G[k] = outputs[0][k].item().to<float>();
            //std::cout << "G\n" << G[k];
        }
    
        return;
    }
    
    
    #pragma once
    
    /* export macros for library generated by CMake */
    #ifndef CALL_TS_API
    #include "call_ts_export.h"
    #define CALL_TS_API CALL_TS_EXPORT
    #endif
    
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    CALL_TS_API
    void invariant_nn(float I[], float G[]);
    
    #ifdef __cplusplus
    }
    #endif
    
    ! fortran program main
    !
    ! calls c++ function invariant_nn, which calls a torchscript with
    ! a neural network
    !
    
    program main
    
        ! define interface to interact with c++
        use, intrinsic :: iso_c_binding, only: c_float
        implicit none
        interface invariant_nn
            subroutine invariant_nn(I, G) bind (c)
                import :: c_float
                real(c_float) :: I(5)
                real(c_float) :: G(10)
            end subroutine
        end interface
    
        ! fortran program
        real(4) I(5), G(10)
    
        ! invariants
        I(1) = 1.01
        I(2) = 1.01
        I(3) = 1.01
        I(4) = 1.01
        I(5) = 1.01
        print *, "Tensor invariants ", I
    
        ! tensor basis coefficients
        call invariant_nn(I, G)    
        print *, "Tensor basis coefficients ", G
    
    end program main
    
    # stop configuration if cmake version is below 3.0
    cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
    
    # project name and enabled languages
    project(fortran_calls_ts CXX Fortran)
    
    # find libtorch
    find_package(Torch REQUIRED)
    
    # if CMAKE_BUILD_TYPE undefined, set it to Release
    if(NOT CMAKE_BUILD_TYPE)
        set(CMAKE_BUILD_TYPE "Release")
    endif()
    
    # compiler flags for release mode
    set(CMAKE_CXX_FLAGS_RELEASE "-O3")
    set(CMAKE_Fortran_FLAGS_RELEASE "-O3")
    
    # set default build paths
    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
    set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
    
    # generated export header will be placed here
    include_directories(${PROJECT_BINARY_DIR})
    
    # c library
    add_library(call_ts_cpp SHARED src/call_ts.cpp)
    
    # fortran executable
    add_executable(fortran_calls_ts.x src/main.f90)
    
    # linked against c library
    target_link_libraries(fortran_calls_ts.x call_ts_cpp)
    target_link_libraries(call_ts_cpp "${TORCH_LIBRARIES}")
    
    # we let cmake generate the export header
    include(GenerateExportHeader)
    generate_export_header(call_ts_cpp BASE_NAME call_ts)
    
    install(TARGETS call_ts_cpp LIBRARY DESTINATION lib ARCHIVE DESTINATION lib)
    
    install(FILES src/call_ts.h ${PROJECT_BINARY_DIR}/call_ts_export.h DESTINATION include)
    
    set_property(TARGET fortran_calls_ts.x PROPERTY CXX_STANDARD 14)