Skip to content

Commit d3c07f8

Browse files
committed
Add initial code for TorchBridge.
1 parent ac5a2f3 commit d3c07f8

19 files changed

Lines changed: 539 additions & 2 deletions

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,8 @@ new_sun.png
3232
new_sun*.png
3333
new_sun_hello.png
3434
test/correspondence/gh.out
35-
!Makefile
35+
!Makefile
36+
!modules/*
37+
modules/build
38+
!bridge/*
39+
bridge/build

bridge/CMakeLists.txt

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
cmake_minimum_required(VERSION 3.31 FATAL_ERROR)
2+
# project(MyProject LANGUAGES CXX)
3+
4+
include(CMakePrintHelpers)
5+
6+
# set(CMAKE_VERBOSE_MAKEFILE ON)
7+
8+
if(UNIX AND NOT APPLE)
9+
set(LINUX TRUE)
10+
endif()
11+
12+
set(CMAKE_C_COMPILER "clang")
13+
set(CMAKE_CXX_COMPILER "clang++")
14+
set(CMAKE_CXX_STANDARD 17)
15+
16+
17+
set(PROJECT_ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
18+
set(PROJECT_BINARY_DIR "${CMAKE_BINARY_DIR}")
19+
20+
find_package(chpl REQUIRED HINTS ${PROJECT_ROOT_DIR}/cmake/chapel)
21+
list(APPEND CMAKE_MODULE_PATH "${PROJECT_ROOT_DIR}/cmake")
22+
list(APPEND CMAKE_MODULE_PATH "${PROJECT_ROOT_DIR}/cmake/chapel")
23+
24+
project(MyProject LANGUAGES CXX C CHPL)
25+
26+
27+
# For ExternalProject_Add
28+
include(FetchContent)
29+
include(ExternalProject)
30+
31+
# ------------------------------------------------------------------------------
32+
# 1) External project: build PyTorch (libtorch) from source as STATIC
33+
# ------------------------------------------------------------------------------
34+
35+
# Where to place PyTorch after installation
36+
set(PYTORCH_INSTALL_DIR "${CMAKE_BINARY_DIR}/pytorch-install")
37+
38+
# ExternalProject_Add can fetch from Git, a local path, or a release tarball.
39+
# Here, for simplicity, we'll fetch from Git. In practice, you might want
40+
# a fixed commit or a release tarball for reproducible builds.
41+
42+
ExternalProject_Add(
43+
pytorch
44+
GIT_REPOSITORY https://github.com/pytorch/pytorch.git
45+
GIT_TAG v2.6.0 # Example: specify a particular release
46+
UPDATE_COMMAND "" # Don’t auto-run 'git pull'
47+
PATCH_COMMAND "" # No custom patch step
48+
49+
# We need all PyTorch submodules. By default, ExternalProject won't do submodule init.
50+
# So we can do that in a separate step if we want a full build. For a minimal CPU build,
51+
# you might not need them all, but let's be safe:
52+
STEP_TARGETS clone
53+
# After 'clone', run "git submodule update --init --recursive"
54+
# to fetch all submodules.
55+
# We can use a little trick with COMMAND.
56+
# PATCH_COMMAND "git submodule update --init --recursive"
57+
58+
CMAKE_ARGS
59+
-DBUILD_SHARED_LIBS=OFF # Build static libraries
60+
-DBUILD_PYTHON=OFF # Don’t build Python bindings
61+
-DBUILD_TEST=OFF # Don’t build tests
62+
-DUSE_CUDA=OFF # Disable CUDA
63+
-DUSE_CUDNN=OFF # Disable cuDNN
64+
-DUSE_MKLDNN=OFF # Disable MKLDNN for simplicity
65+
-DCMAKE_BUILD_TYPE=Release
66+
-DCMAKE_INSTALL_PREFIX=${PYTORCH_INSTALL_DIR}
67+
68+
INSTALL_DIR ${PYTORCH_INSTALL_DIR} # Where to install
69+
)
70+
71+
# ------------------------------------------------------------------------------
72+
# 2) Create an INTERFACE library to wrap the installed static libs
73+
# ------------------------------------------------------------------------------
74+
75+
# We'll create a dummy target that depends on 'pytorch' so that
76+
# building your own code will first build/install PyTorch.
77+
78+
add_library(torch_interface INTERFACE)
79+
80+
# Ensure that our 'torch_interface' target isn't used until PyTorch is built
81+
add_dependencies(torch_interface pytorch)
82+
83+
# Include directories for PyTorch
84+
target_include_directories(torch_interface INTERFACE
85+
"${PYTORCH_INSTALL_DIR}/include"
86+
"${PYTORCH_INSTALL_DIR}/include/torch/csrc/api/include"
87+
)
88+
89+
# Link the relevant static libraries. For a minimal CPU-only build,
90+
# you'll likely need at least these (names can vary by version).
91+
# The exact set can differ depending on which components got built.
92+
93+
target_link_libraries(torch_interface INTERFACE
94+
"${PYTORCH_INSTALL_DIR}/lib/libtorch.a"
95+
"${PYTORCH_INSTALL_DIR}/lib/libtorch_cpu.a"
96+
"${PYTORCH_INSTALL_DIR}/lib/libc10.a"
97+
98+
# System libraries often needed:
99+
pthread
100+
dl
101+
rt
102+
)
103+
104+
105+
106+
add_executable(CHPLTest lib/CHPLTest.chpl)
107+
108+
109+
add_library(torchbridge STATIC "${PROJECT_ROOT_DIR}/lib/bridge.cpp" "${PROJECT_ROOT_DIR}/include/bridge.h")
110+
add_dependencies(torchbridge torch_interface)
111+
112+
target_include_directories(torchbridge PUBLIC
113+
"${PYTORCH_INSTALL_DIR}/include"
114+
"${PYTORCH_INSTALL_DIR}/include/torch/csrc/api/include"
115+
"${PROJECT_ROOT_DIR}/include"
116+
)
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+
# )
146+
147+
148+
149+
# ------------------------------------------------------------------------------
150+
# 3) Build your own executable that uses torch_interface
151+
# ------------------------------------------------------------------------------
152+
153+
# add_executable(my_app src/main.cpp)
154+
155+
# # Link your app against our interface library
156+
# target_link_libraries(my_app PRIVATE torch_interface)
157+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright 2023-2025 Hewlett Packard Enterprise Development LP
2+
# Other additional copyright holders may be indicated within.
3+
#
4+
# The entirety of this work is licensed under the Apache License,
5+
# Version 2.0 (the "License"); you may not use this file except
6+
# in compliance with the License.
7+
#
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
18+
set(CMAKE_CHPL_COMPILER "@CMAKE_CHPL_COMPILER@")
19+
set(CMAKE_CHPL_COMPILER_LOADED 1)
20+
set(CMAKE_CHPL_SOURCE_FILE_EXTENSIONS "@CMAKE_CHPL_SOURCE_FILE_EXTENSIONS@")
21+
set(CMAKE_CHPL_OUTPUT_EXTENSION "@CMAKE_CHPL_OUTPUT_EXTENSION@")
22+
set(CMAKE_CHPL_COMPILER_ENV_VAR "@CMAKE_CHPL_COMPILER_ENV_VAR@")
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright 2023-2025 Hewlett Packard Enterprise Development LP
2+
# Other additional copyright holders may be indicated within.
3+
#
4+
# The entirety of this work is licensed under the Apache License,
5+
# Version 2.0 (the "License"); you may not use this file except
6+
# in compliance with the License.
7+
#
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
18+
19+
# TODO: this is a hack, how do we tell cmake that we can't compile to object files?
20+
set(CMAKE_CHPL_COMPILE_OBJECT "test -f <OBJECT> || ln -s <SOURCE> <OBJECT>")
21+
set(CMAKE_CHPL_LINK_EXECUTABLE "<CMAKE_CHPL_COMPILER> -o <TARGET> <OBJECTS> <FLAGS> <LINK_FLAGS>")
22+
# <COMPILE_FLAGS> doesn't work on link step
23+
24+
# TODO: support --library directly to build Chapel shared libs for interoperability
25+
26+
set(CMAKE_CHPL_SOURCE_FILE_EXTENSIONS chpl)
27+
28+
set(CMAKE_CHPL_OUTPUT_EXTENSION ".chpl")
29+
30+
31+
set(CMAKE_CHPL_FLAGS_RELEASE "--fast")
32+
set(CMAKE_CHPL_FLAGS_DEBUG "-g")
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Copyright 2023-2025 Hewlett Packard Enterprise Development LP
2+
# Other additional copyright holders may be indicated within.
3+
#
4+
# The entirety of this work is licensed under the Apache License,
5+
# Version 2.0 (the "License"); you may not use this file except
6+
# in compliance with the License.
7+
#
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
18+
19+
# if there is an env CHPL_HOME, use it to find printchplenv
20+
if (DEFINED ENV{CHPL_HOME})
21+
set(CHPL_HOME $ENV{CHPL_HOME})
22+
23+
execute_process(
24+
COMMAND ${CHPL_HOME}/util/printchplenv --value --only CHPL_HOST_BIN_SUBDIR
25+
OUTPUT_VARIABLE CHPL_HOST_BIN_SUBDIR
26+
OUTPUT_STRIP_TRAILING_WHITESPACE
27+
)
28+
set(CHPL_HOME_BIN_PATH ${CHPL_HOME}/bin/${CHPL_HOST_BIN_SUBDIR})
29+
# TODO: what if its a prefix install?
30+
else()
31+
set(CHPL_HOME "")
32+
set(CHPL_HOME_BIN_PATH "")
33+
endif()
34+
35+
find_program(
36+
CMAKE_CHPL_COMPILER
37+
NAMES "chpl"
38+
HINTS ${CHPL_HOME} ${CHPL_HOME_BIN_PATH}
39+
DOC "chpl compiler"
40+
)
41+
# TODO: how to find chpl with just CHPL_HOME?
42+
43+
set(CMAKE_CHPL_COMPILER_ENV_VAR "")
44+
45+
configure_file(${CMAKE_CURRENT_LIST_DIR}/CMakeCHPLCompiler.cmake.in
46+
${CMAKE_PLATFORM_INFO_DIR}/CMakeCHPLCompiler.cmake)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright 2023-2025 Hewlett Packard Enterprise Development LP
2+
# Other additional copyright holders may be indicated within.
3+
#
4+
# The entirety of this work is licensed under the Apache License,
5+
# Version 2.0 (the "License"); you may not use this file except
6+
# in compliance with the License.
7+
#
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
18+
# For now do nothing
19+
set(CMAKE_CHPL_COMPILER_WORKS 1 CACHE INTERNAL "")

bridge/cmake/chapel/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# chpl CMake Support
2+
3+
This directory contains the necessary files to support building Chapel programs using CMake.
4+
5+
Take the following steps to use Chapel with CMake:
6+
7+
1. Copy the files in this directory to your project's source directory. You may want to copy the files to a subdirectory like `cmake`, or you can just copy them to the root of your project.
8+
2. Add `find_package(chpl REQUIRED HINTS .)` to your `CMakeLists.txt` file.
9+
10+
Here is an example `CMakeLists.txt` file for a minimal Hello World program:
11+
12+
```cmake
13+
find_package(chpl REQUIRED HINTS .)
14+
project(hello LANGUAGES CHPL)
15+
16+
add_executable(hello hello.chpl)
17+
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright 2023-2025 Hewlett Packard Enterprise Development LP
2+
# Other additional copyright holders may be indicated within.
3+
#
4+
# The entirety of this work is licensed under the Apache License,
5+
# Version 2.0 (the "License"); you may not use this file except
6+
# in compliance with the License.
7+
#
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
18+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")

bridge/include/bridge.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#ifndef BRIDGE_H
2+
#define BRIDGE_H
3+
4+
#include <stdio.h>
5+
6+
#ifdef __cplusplus
7+
#include <torch/torch.h>
8+
#include <iostream>
9+
#include <vector>
10+
extern "C" {
11+
#endif
12+
13+
int baz(void);
14+
15+
void wrHello(void);
16+
17+
void wrHelloTorch(void);
18+
19+
float sumArray(float* arr, int* sizes, int dim);
20+
21+
#ifdef __cplusplus
22+
}
23+
#endif
24+
25+
#endif // BRIDGE_H
26+
//hello

0 commit comments

Comments
 (0)