Skip to content

Commit 42fa424

Browse files
committed
Linking to pytorch works, but there are C++ runtime errors in certain basic functions. See ./Bridge
1 parent d3c07f8 commit 42fa424

4 files changed

Lines changed: 54 additions & 31 deletions

File tree

bridge/CMakeLists.txt

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ include(ExternalProject)
3333
# ------------------------------------------------------------------------------
3434

3535
# Where to place PyTorch after installation
36-
set(PYTORCH_INSTALL_DIR "${CMAKE_BINARY_DIR}/pytorch-install")
36+
set(PYTORCH_INSTALL_DIR "${PROJECT_BINARY_DIR}/pytorch-install")
3737

3838
# ExternalProject_Add can fetch from Git, a local path, or a release tarball.
3939
# Here, for simplicity, we'll fetch from Git. In practice, you might want
@@ -45,6 +45,8 @@ ExternalProject_Add(
4545
GIT_TAG v2.6.0 # Example: specify a particular release
4646
UPDATE_COMMAND "" # Don’t auto-run 'git pull'
4747
PATCH_COMMAND "" # No custom patch step
48+
49+
# DOWNLOAD_DIR "${CMAKE_BINARY_DIR}/pytorch-download" # Where to download the repo
4850

4951
# We need all PyTorch submodules. By default, ExternalProject won't do submodule init.
5052
# So we can do that in a separate step if we want a full build. For a minimal CPU build,
@@ -66,8 +68,21 @@ ExternalProject_Add(
6668
-DCMAKE_INSTALL_PREFIX=${PYTORCH_INSTALL_DIR}
6769

6870
INSTALL_DIR ${PYTORCH_INSTALL_DIR} # Where to install
71+
72+
LOG_DOWNLOAD ON
73+
LOG_UPDATE ON
74+
LOG_PATCH ON
75+
LOG_CONFIGURE ON
76+
# LOG_BUILD ON
77+
LOG_INSTALL ON
78+
6979
)
7080

81+
82+
file(GLOB_RECURSE PYTORCH_INCLUDES "${PYTORCH_INSTALL_DIR}/include" "*.h")
83+
file(GLOB_RECURSE PYTORCH_LIBS "${PYTORCH_INSTALL_DIR}/lib" "*.a")
84+
85+
7186
# ------------------------------------------------------------------------------
7287
# 2) Create an INTERFACE library to wrap the installed static libs
7388
# ------------------------------------------------------------------------------
@@ -95,6 +110,8 @@ target_link_libraries(torch_interface INTERFACE
95110
"${PYTORCH_INSTALL_DIR}/lib/libtorch_cpu.a"
96111
"${PYTORCH_INSTALL_DIR}/lib/libc10.a"
97112

113+
${PYTORCH_LIBS}
114+
98115
# System libraries often needed:
99116
pthread
100117
dl
@@ -114,35 +131,38 @@ target_include_directories(torchbridge PUBLIC
114131
"${PYTORCH_INSTALL_DIR}/include/torch/csrc/api/include"
115132
"${PROJECT_ROOT_DIR}/include"
116133
)
117-
target_link_directories(torchbridge PUBLIC
118-
"${PYTORCH_INSTALL_DIR}/lib"
119-
)
120-
target_link_libraries(torchbridge torch_interface)
121-
122-
123-
124-
125-
126-
# add_executable(TorchBridge lib/Bridge.chpl include/bridge.h)
127-
128-
# target_link_options(TorchBridge
129-
# PRIVATE
130-
# "${PROJECT_ROOT_DIR}/include/bridge.h"
131-
# "-L${PROJECT_BINARY_DIR}"
132-
# -L.
133-
# "-ltorchbridge"
134-
# "-I${PROJECT_BINARY_DIR}"
135-
# "-L${PYTORCH_INSTALL_DIR}/lib"
136-
# "-I${PYTORCH_INSTALL_DIR}/include"
137-
# "-I${PYTORCH_INSTALL_DIR}/include/torch/csrc/api/include"
138-
# "-ltorch"
139-
# "-ltorch_cpu"
140-
# "-lcpuinfo"
141-
# "-lc10"
142-
# "-lpthread"
143-
# "-ldl"
144-
145-
# )
134+
# target_link_directories(torchbridge PUBLIC
135+
# "${PYTORCH_INSTALL_DIR}/lib"
136+
# )
137+
target_link_libraries(torchbridge
138+
torch_interface
139+
${PYTORCH_LIBS}
140+
)
141+
142+
143+
144+
145+
146+
add_executable(Bridge lib/Bridge.chpl include/bridge.h)
147+
# add_dependencies(TorchBridge torchbridge)
148+
149+
target_link_options(Bridge
150+
PRIVATE
151+
"${PROJECT_ROOT_DIR}/include/bridge.h"
152+
"-L${PROJECT_BINARY_DIR}"
153+
-L.
154+
"-ltorchbridge"
155+
"-I${PROJECT_BINARY_DIR}"
156+
"-L${PYTORCH_INSTALL_DIR}/lib"
157+
"-I${PYTORCH_INSTALL_DIR}/include"
158+
"-I${PYTORCH_INSTALL_DIR}/include/torch/csrc/api/include"
159+
"-ltorch"
160+
"-ltorch_cpu"
161+
"-lcpuinfo"
162+
"-lc10"
163+
"-lpthread"
164+
"-ldl"
165+
)
146166

147167

148168

bridge/lib/Bridge.chpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ extern proc wrHelloTorch(): void;
77
extern proc sumArray(arr: [] real(32), sizes: [] int(32), dim: int(32)): real(32);
88

99
wrHello();
10+
wrHello();
11+
1012

1113
wrHelloTorch();
1214

bridge/lib/CHPLTest.chpl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
writeln("Hello, world!");
2-

bridge/lib/bridge.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ extern "C" void wrHelloTorch(void) {
2121
extern "C" float sumArray(float* arr, int* sizes, int dim) {
2222
// Convert sizes to std::vector<int64_t>
2323

24+
printf("sumArray called with arr: %p, sizes: %p, dim: %d\n", arr, sizes, dim);
25+
2426
std::vector<int64_t> sizes_vec(sizes, sizes + dim);
2527
std::cout << sizes_vec << std::endl;
2628

0 commit comments

Comments
 (0)