Skip to content

Commit

Permalink
CMake fix (#67)
Browse files Browse the repository at this point in the history
* Amend installation location of CMake files allowing easier detection by other CMake processes.

* Update README for new CMake file install - use prefix_path not torch_dir.

* Update examples instructions for new cmake files install location.

* Correct references to lib* and lib64 to lib

* Add notes about alternative lib and sybmols files.

* Update README.md

Co-authored-by: ElliottKasoar <[email protected]>

* Update readme to stylise note as suggested by @ElliottKasoar.

---------

Co-authored-by: ElliottKasoar <[email protected]>
  • Loading branch information
jatkinson1000 and ElliottKasoar authored Nov 21, 2023
1 parent abfa5ca commit a6c57fa
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 11 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,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 @@ -198,7 +200,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 @@ -212,14 +217,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._

### 4. Running on GPUs

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 @@ -46,12 +46,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 @@ -71,7 +74,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

0 comments on commit a6c57fa

Please sign in to comment.