Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.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/2/cmake/2.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++ 如何为CMake组织我的目录、子目录、文件和CMakeLists.txt?_C++_Cmake_Fortran_Libtorch - Fatal编程技术网

C++ 如何为CMake组织我的目录、子目录、文件和CMakeLists.txt?

C++ 如何为CMake组织我的目录、子目录、文件和CMakeLists.txt?,c++,cmake,fortran,libtorch,C++,Cmake,Fortran,Libtorch,我想在Project/build/目录中使用CMake编译一个程序,该目录具有以下组织: ---Project/ ---CMakeLists.txt ---main/ ---main.f90 ---library/ ---CMakeLists.txt ---call_ts_module.f90 ---call_ts.cpp ---call_ts.h ---torchscript.pt ---li

我想在
Project/build/
目录中使用CMake编译一个程序,该目录具有以下组织:

---Project/
   ---CMakeLists.txt
   ---main/
      ---main.f90
   ---library/
      ---CMakeLists.txt
      ---call_ts_module.f90
      ---call_ts.cpp
      ---call_ts.h
      ---torchscript.pt
      ---libtorch/
         ---<libtorch libraries>
   ---build/
Project/library/CMakeLists.txt

cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(fortran_calls_ts CXX Fortran)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE "Release")
endif()
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
set(CMAKE_Fortran_FLAGS_RELEASE "-O3")
find_package(Torch REQUIRED)
add_subdirectory(library)
add_executable(fortran_calls_ts.x main/main.f90)
target_link_libraries(fortran_calls_ts.x library)
set_property(TARGET fortran_calls_ts.x PROPERTY CXX_STANDARD 14)

set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
add_library(library SHARED call_ts_module.f90)
add_library(call_ts_cpp SHARED call_ts.cpp)
target_link_libraries(library call_ts_cpp)
target_link_libraries(call_ts_cpp "${TORCH_LIBRARIES}")
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 call_ts.h ${PROJECT_BINARY_DIR}/call_ts_export.h DESTINATION include)
Project/main/main.f90

program main
    
    use call_ts_module
    ...
    call call_ts_subroutine()
    ...

end program main 

module call_ts_module
    
    contains
    
    subroutine call_ts_subroutine()
        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

        real(4) I(5), G(10)
        ...
        call invariant_nn(I, G)
        ...
    end subroutine call_ts_subroutine

end module call_ts_module 
Project/library/call\u ts\u module.f90

program main
    
    use call_ts_module
    ...
    call call_ts_subroutine()
    ...

end program main 

module call_ts_module
    
    contains
    
    subroutine call_ts_subroutine()
        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

        real(4) I(5), G(10)
        ...
        call invariant_nn(I, G)
        ...
    end subroutine call_ts_subroutine

end module call_ts_module 
Project/library/call\u ts.cpp

#include "call_ts.h"
#include <torch/script.h>
#include <iostream>
#include <memory>

void invariant_nn(float I[], float G[])
{
    torch::jit::script::Module module;
    const char *arg;
    arg = "../library/torchscript.pt";
    module = torch::jit::load(arg);
    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}));
    at::Tensor outputs = module.forward(inputs).toTensor();
    for (int k = 0; k < 10; k++) {
        G[k] = outputs[0][k].item().to<float>();
    }
    return;
}
看看我的项目吧,它可能会给你灵感。