Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CMake fix #67

Merged
merged 7 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ To build and install the library:
```
This will place the following directories at the install location:
* `CMAKE_INSTALL_PREFIX/include/` - contains header and mod files
* `CMAKE_INSTALL_PREFIX/lib64/` - contains `cmake` directory and `.so` files
* `CMAKE_INSTALL_PREFIX/lib/` - contains `cmake` directory and `.so` files
_Note: depending on your system and architecture `lib` may be `lib64`, and
you may have `.dll` files or similar._


## Usage
Expand Down Expand Up @@ -189,7 +191,10 @@ find_package(FTorch)
target_link_libraries( <executable> PRIVATE FTorch::ftorch )
message(STATUS "Building with Fortran PyTorch coupling")
```
and using the `-DFTorch_DIR=</path/to/install/location>` flag when running cmake.
and using the `-DCMAKE_PREFIX_PATH=</path/to/install/location>` flag when running cmake.
_Note: If you used the `CMAKE_INSTALL_PREFIX` argument when
[building and installing the library](#library-installation) above then you should use
the same path for `</path/to/install/location>`._

#### Make
To build with make we need to include the library when compiling and link the executable
Expand All @@ -203,14 +208,15 @@ FCFLAGS += -I<path/to/install/location>/include/ftorch

When compiling the final executable add the following link flag:
```
LDFLAGS += -L<path/to/install/location>/lib64 -lftorch
LDFLAGS += -L<path/to/install/location>/lib -lftorch
```

You may also need to add the location of the `.so` files to your `LD_LIBRARY_PATH`
unless installing in a default location:
```
export LD_LIBRARY_PATH = $LD_LIBRARY_PATH:<path/to/installation>/lib64
export LD_LIBRARY_PATH = $LD_LIBRARY_PATH:<path/to/install/location>/lib
```
_Note: depending on your system and architecture `lib` may be `lib64` or something similar._


## Examples
Expand Down
3 changes: 1 addition & 2 deletions examples/1_SimpleNet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,9 @@ This can be done using the included `CMakeLists.txt` as follows:
```
mkdir build
cd build
cmake .. -DFTorch_DIR=<path/to/your/installation/of/library/>lib/cmake/ -DCMAKE_BUILD_TYPE=Release
cmake .. -DCMAKE_PREFIX_PATH=<path/to/your/installation/of/library/> -DCMAKE_BUILD_TYPE=Release
make
```
Make sure that the `FTorch_DIR` flag points to the `lib/cmake/` folder within the installation of the FTorch library.

To run the compiled code calling the saved SimpleNet TorchScript from Fortran run the
executable with an argument of the saved model file:
Expand Down
2 changes: 1 addition & 1 deletion examples/2_ResNet18/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ FC = gfortran
FCFLAGS = -O3 -I</path/to/installation>/include/ftorch

# link flags
LDFLAGS = -L</path/to/installation>/lib64/ -lftorch
LDFLAGS = -L</path/to/installation>/lib/ -lftorch

PROGRAM = resnet_infer_fortran
SRC = resnet_infer_fortran.f90
Expand Down
5 changes: 2 additions & 3 deletions examples/2_ResNet18/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,9 @@ This can be done using the included `CMakeLists.txt` as follows:
```
mkdir build
cd build
cmake .. -DFTorch_DIR=<path/to/your/installation/of/library/>lib/cmake/ -DCMAKE_BUILD_TYPE=Release
cmake .. -DCMAKE_PREFIX_PATH=<path/to/your/installation/of/library/> -DCMAKE_BUILD_TYPE=Release
make
```
Make sure that the `FTorch_DIR` flag points to the `lib/cmake/` folder within the installation of the FTorch library.

To run the compiled code calling the saved ResNet-18 TorchScript from Fortran run the
executable with an argument of the saved model file:
Expand All @@ -96,7 +95,7 @@ installation of FTorch as described in the main documentation. Also check that t
You will also likely need to add the location of the `.so` files to your `LD_LIBRARY_PATH`:
```
make
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:</path/to/library/installation>/lib64
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:</path/to/library/installation>/lib
./resnet_infer_fortran saved_resnet18_model_cpu.pt
```

Expand Down
5 changes: 4 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,15 @@ find_package(Torch REQUIRED)

# Library with C and Fortran bindings
add_library(${LIB_NAME} SHARED ctorch.cpp ftorch.f90)
# Add an alias FTorch::ftorch for the library
add_library(${PROJECT_NAME}::${LIB_NAME} ALIAS ${LIB_NAME})
set_target_properties(${LIB_NAME} PROPERTIES
PUBLIC_HEADER "ctorch.h"
Fortran_MODULE_DIRECTORY "${CMAKE_BINARY_DIR}/modules"
)
# Link TorchScript
target_link_libraries(${LIB_NAME} PRIVATE ${TORCH_LIBRARIES})
# Include the Fortran mod files in the library
target_include_directories(${LIB_NAME}
PUBLIC
$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/modules>
Expand All @@ -61,7 +64,7 @@ install(TARGETS "${LIB_NAME}"
install(EXPORT ${PROJECT_NAME}
FILE ${PROJECT_NAME}Config.cmake
NAMESPACE ${PROJECT_NAME}::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
)

# Install Fortran module files
Expand Down