Skip to content

Commit 5d8ad25

Browse files
committed
Chapel-webcam demo works with ChAI and libtorch.
1 parent 81f9530 commit 5d8ad25

8 files changed

Lines changed: 70 additions & 33 deletions

File tree

CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,22 @@ target_include_directories(
131131
)
132132

133133

134+
add_library(bridge_objs STATIC $<TARGET_OBJECTS:bridge>)
135+
set_target_properties(bridge_objs
136+
PROPERTIES ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}"
137+
)
138+
139+
134140
set(BRIDGE_OBJECT_FILES $<TARGET_OBJECTS:bridge>)
135141

142+
# add_custom_command(
143+
# TARGET bridge
144+
# POST_BUILD
145+
# COMMAND ${CMAKE_COMMAND} -E copy_directory
146+
# "${CMAKE_CURRENT_SOURCE_DIR}/style-transfer/models"
147+
# "$<TARGET_FILE_DIR:StyleTransfer>/style-transfer/models"
148+
# COMMENT "NOT! Copying ${PROJECT_ROOT_DIR}/examples/vgg/images to $<TARGET_FILE_DIR:vgg>/images"
149+
# )
136150

137151

138152

bridge/include/bridge.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ proto_bridge_simple(tanhshrink);
107107
void split_loop(int64_t idx, int64_t n);
108108
void split_loop_filler(int64_t n,int64_t* ret);
109109

110-
void show_webcam(void);
111110

112111

113112
// bridge_tensor_t conv2d(

bridge/lib/bridge.cpp

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include <chrono>
1717
#include <thread>
1818

19-
#include <opencv2/opencv.hpp>
2019

2120

2221
#define def_bridge_simple(Name) \
@@ -404,37 +403,37 @@ extern "C" void split_loop_filler(int64_t n,int64_t* ret) {
404403

405404

406405

407-
cv::VideoCapture open_camera(int cam_index) {
408-
cv::VideoCapture cap(cam_index, cv::CAP_AVFOUNDATION);
409-
if (!cap.isOpened()) {
410-
std::cerr << "Could not open camera index " << cam_index << std::endl;
411-
return cv::VideoCapture();
412-
}
413-
cap.set(cv::CAP_PROP_BUFFERSIZE, 1); // minimal internal buffering
414-
cap.set(cv::CAP_PROP_FPS, 60); // request higher FPS if possible
415-
return cap;
416-
}
406+
// cv::VideoCapture open_camera(int cam_index) {
407+
// cv::VideoCapture cap(cam_index, cv::CAP_AVFOUNDATION);
408+
// if (!cap.isOpened()) {
409+
// std::cerr << "Could not open camera index " << cam_index << std::endl;
410+
// return cv::VideoCapture();
411+
// }
412+
// cap.set(cv::CAP_PROP_BUFFERSIZE, 1); // minimal internal buffering
413+
// cap.set(cv::CAP_PROP_FPS, 60); // request higher FPS if possible
414+
// return cap;
415+
// }
417416

418417

419-
extern "C" void show_webcam(void) {
420-
cv::VideoCapture cap;
421-
cap = open_camera(0);
418+
// extern "C" void show_webcam(void) {
419+
// cv::VideoCapture cap;
420+
// cap = open_camera(0);
422421

423-
cv::Mat frame_bgr;
422+
// cv::Mat frame_bgr;
424423

425-
while (true) {
426-
if (!cap.read(frame_bgr) || frame_bgr.empty()) {
427-
std::cerr << "[WARN] Empty frame, exiting" << std::endl;
428-
break;
429-
}
424+
// while (true) {
425+
// if (!cap.read(frame_bgr) || frame_bgr.empty()) {
426+
// std::cerr << "[WARN] Empty frame, exiting" << std::endl;
427+
// break;
428+
// }
430429

431-
cv::imshow("webcam", frame_bgr);
430+
// cv::imshow("webcam", frame_bgr);
432431

433-
if (cv::waitKey(1) == 27) { // ESC key
434-
break;
435-
}
436-
}
432+
// if (cv::waitKey(1) == 27) { // ESC key
433+
// break;
434+
// }
435+
// }
437436

438-
cap.release();
439-
cv::destroyAllWindows();
440-
}
437+
// cap.release();
438+
// cv::destroyAllWindows();
439+
// }

demos/video/chapel-webcam/Makefile

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ all: main
77
# chpl --library --library-makefile --library-cmakelists smol.chpl
88

99

10+
CHAI_DIR = `cd ../../.. && pwd`
11+
12+
BRIDGE_CFLAGS = \
13+
-I $(CHAI_DIR)/bridge/include \
14+
$(CHAI_DIR)/build/libbridge_objs.a
15+
16+
BRIDGE_LDFLAGS = \
17+
-L $(CHAI_DIR)/build \
18+
-L $(CHAI_DIR)/libtorch/lib \
19+
-Wl,-rpath,$(CHAI_DIR)/libtorch/lib
20+
1021
LIB_GEN_CMD = \
1122
chpl \
1223
--library \
@@ -16,7 +27,9 @@ LIB_GEN_CMD = \
1627
--library-cmakelists \
1728
--no-munge-with-ids \
1829
-M ../../../lib \
19-
smol.chpl
30+
smol.chpl \
31+
--ccflags "$(BRIDGE_CFLAGS)" \
32+
--ldflags "$(BRIDGE_LDFLAGS)"
2033
# --savec lib/savec \
2134
2235

@@ -30,6 +43,7 @@ pre-build:
3043

3144

3245
libsmol: smol.chpl
46+
echo "$(CHAI_DIR)"
3347
$(LIB_GEN_CMD)
3448

3549

@@ -38,7 +52,7 @@ OPENCV_LDFLAGS = `pkg-config --cflags --libs opencv4`
3852

3953
main: main.cpp smol_wrapper.h lib/libsmol.so
4054
@echo $(OPENCV_CFLAGS)
41-
$(CHPL_LINKER) $(CHPL_CFLAGS) $(OPENCV_CFLAGS) -std=c++20 -fPIC main.cpp -o main $(CHPL_LDFLAGS) $(OPENCV_LDFLAGS)
55+
$(CHPL_LINKER) $(CHPL_CFLAGS) $(OPENCV_CFLAGS) -std=c++20 -fPIC main.cpp -o main $(CHPL_LDFLAGS) $(OPENCV_LDFLAGS) $(BRIDGE_LDFLAGS)
4256

4357
clean:
4458
rm -f maincpp maincpp.o main.o main

demos/video/chapel-webcam/lib/Makefile.smol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ CHPL_HOME = /opt/homebrew/Cellar/chapel/2.4.0_1/libexec
88

99
CHPL_CFLAGS = -Ilib -Wno-unused -Wno-uninitialized -Wno-pointer-sign -Wno-incompatible-pointer-types -Wno-tautological-compare -I/opt/homebrew/Cellar/chapel/2.4.0_1/libexec/modules/internal -I/opt/homebrew/Cellar/chapel/2.4.0_1/libexec/modules/packages -I$(CHPL_RUNTIME_INCL)/localeModels/flat -I$(CHPL_RUNTIME_INCL)/localeModels -I$(CHPL_RUNTIME_INCL)/comm/none -I$(CHPL_RUNTIME_INCL)/comm -I$(CHPL_RUNTIME_INCL)/tasks/qthreads -I$(CHPL_RUNTIME_INCL)/. -I$(CHPL_RUNTIME_INCL)/./qio -I$(CHPL_RUNTIME_INCL)/./atomics/cstdlib -I$(CHPL_RUNTIME_INCL)/./mem/jemalloc -I$(CHPL_THIRD_PARTY)/utf8-decoder -I$(CHPL_THIRD_PARTY)/qthread/install/darwin-arm64-native-llvm-none-flat-jemalloc-system/include -Wno-error=unused-variable -I$(CHPL_THIRD_PARTY)/re2/install/darwin-arm64-native-llvm-none/include -I. -I/opt/homebrew/Cellar/gmp/6.3.0/include -I/opt/homebrew/Cellar/hwloc/2.12.0/include -I/opt/homebrew/Cellar/jemalloc/5.3.0/include -I/opt/homebrew/include
1010

11-
CHPL_LDFLAGS = -Llib -lsmol -L$(CHPL_RUNTIME_LIB)/darwin/llvm/arm64/cpu-native/loc-flat/comm-none/tasks-qthreads/tmr-generic/unwind-none/mem-jemalloc/atomics-cstdlib/hwloc-system/re2-bundled/fs-none/lib_pic-none/san-none -lchpl -L$(CHPL_THIRD_PARTY)/qthread/install/darwin-arm64-native-llvm-none-flat-jemalloc-system/lib -Wl,-rpath,$(CHPL_THIRD_PARTY)/qthread/install/darwin-arm64-native-llvm-none-flat-jemalloc-system/lib -lqthread -L/opt/homebrew/Cellar/hwloc/2.12.0/lib -L$(CHPL_THIRD_PARTY)/re2/install/darwin-arm64-native-llvm-none/lib -lre2 -Wl,-rpath,$(CHPL_THIRD_PARTY)/re2/install/darwin-arm64-native-llvm-none/lib -lm -lpthread -L/opt/homebrew/Cellar/gmp/6.3.0/lib -lgmp -L/opt/homebrew/Cellar/hwloc/2.12.0/lib -Wl,-rpath,/opt/homebrew/Cellar/hwloc/2.12.0/lib -lhwloc -L/opt/homebrew/Cellar/jemalloc/5.3.0/lib -Wl,-rpath,/opt/homebrew/Cellar/jemalloc/5.3.0/lib -ljemalloc -L/opt/homebrew/lib
11+
CHPL_LDFLAGS = -Llib -lsmol -ltorch -ltorch_cpu -lc10 -ltorch_global_deps -L$(CHPL_RUNTIME_LIB)/darwin/llvm/arm64/cpu-native/loc-flat/comm-none/tasks-qthreads/tmr-generic/unwind-none/mem-jemalloc/atomics-cstdlib/hwloc-system/re2-bundled/fs-none/lib_pic-none/san-none -lchpl -L$(CHPL_THIRD_PARTY)/qthread/install/darwin-arm64-native-llvm-none-flat-jemalloc-system/lib -Wl,-rpath,$(CHPL_THIRD_PARTY)/qthread/install/darwin-arm64-native-llvm-none-flat-jemalloc-system/lib -lqthread -L/opt/homebrew/Cellar/hwloc/2.12.0/lib -L$(CHPL_THIRD_PARTY)/re2/install/darwin-arm64-native-llvm-none/lib -lre2 -Wl,-rpath,$(CHPL_THIRD_PARTY)/re2/install/darwin-arm64-native-llvm-none/lib -lm -lpthread -L/opt/homebrew/Cellar/gmp/6.3.0/lib -lgmp -L/opt/homebrew/Cellar/hwloc/2.12.0/lib -Wl,-rpath,/opt/homebrew/Cellar/hwloc/2.12.0/lib -lhwloc -L/opt/homebrew/Cellar/jemalloc/5.3.0/lib -Wl,-rpath,/opt/homebrew/Cellar/jemalloc/5.3.0/lib -ljemalloc -L/opt/homebrew/lib
1212

1313
CHPL_COMPILER = /opt/homebrew/Cellar/llvm@19/19.1.7/bin/clang
1414

demos/video/chapel-webcam/lib/smol.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ set(CHPL_HOME /opt/homebrew/Cellar/chapel/2.4.0_1/libexec)
88

99
set(smol_INCLUDE_DIRS ${CMAKE_CURRENT_LIST_DIR} /opt/homebrew/Cellar/chapel/2.4.0_1/libexec/modules/internal /opt/homebrew/Cellar/chapel/2.4.0_1/libexec/modules/packages ${CHPL_RUNTIME_INCL}/localeModels/flat ${CHPL_RUNTIME_INCL}/localeModels ${CHPL_RUNTIME_INCL}/comm/none ${CHPL_RUNTIME_INCL}/comm ${CHPL_RUNTIME_INCL}/tasks/qthreads ${CHPL_RUNTIME_INCL}/. ${CHPL_RUNTIME_INCL}/./qio ${CHPL_RUNTIME_INCL}/./atomics/cstdlib ${CHPL_RUNTIME_INCL}/./mem/jemalloc ${CHPL_THIRD_PARTY}/utf8-decoder ${CHPL_THIRD_PARTY}/qthread/install/darwin-arm64-native-llvm-none-flat-jemalloc-system/include -Wno-error=unused-variable ${CHPL_THIRD_PARTY}/re2/install/darwin-arm64-native-llvm-none/include . /opt/homebrew/Cellar/gmp/6.3.0/include /opt/homebrew/Cellar/hwloc/2.12.0/include /opt/homebrew/Cellar/jemalloc/5.3.0/include /opt/homebrew/include)
1010

11-
set(smol_LINK_LIBS -L${CMAKE_CURRENT_LIST_DIR} -lsmol -L${CHPL_RUNTIME_LIB}/darwin/llvm/arm64/cpu-native/loc-flat/comm-none/tasks-qthreads/tmr-generic/unwind-none/mem-jemalloc/atomics-cstdlib/hwloc-system/re2-bundled/fs-none/lib_pic-none/san-none -lchpl -L${CHPL_THIRD_PARTY}/qthread/install/darwin-arm64-native-llvm-none-flat-jemalloc-system/lib -Wl,-rpath,${CHPL_THIRD_PARTY}/qthread/install/darwin-arm64-native-llvm-none-flat-jemalloc-system/lib -lqthread -L/opt/homebrew/Cellar/hwloc/2.12.0/lib -L${CHPL_THIRD_PARTY}/re2/install/darwin-arm64-native-llvm-none/lib -lre2 -Wl,-rpath,${CHPL_THIRD_PARTY}/re2/install/darwin-arm64-native-llvm-none/lib -lm -lpthread -L/opt/homebrew/Cellar/gmp/6.3.0/lib -lgmp -L/opt/homebrew/Cellar/hwloc/2.12.0/lib -Wl,-rpath,/opt/homebrew/Cellar/hwloc/2.12.0/lib -lhwloc -L/opt/homebrew/Cellar/jemalloc/5.3.0/lib -Wl,-rpath,/opt/homebrew/Cellar/jemalloc/5.3.0/lib -ljemalloc -L/opt/homebrew/lib -lsmol)
11+
set(smol_LINK_LIBS -L${CMAKE_CURRENT_LIST_DIR} -lsmol -ltorch -ltorch_cpu -lc10 -ltorch_global_deps -L${CHPL_RUNTIME_LIB}/darwin/llvm/arm64/cpu-native/loc-flat/comm-none/tasks-qthreads/tmr-generic/unwind-none/mem-jemalloc/atomics-cstdlib/hwloc-system/re2-bundled/fs-none/lib_pic-none/san-none -lchpl -L${CHPL_THIRD_PARTY}/qthread/install/darwin-arm64-native-llvm-none-flat-jemalloc-system/lib -Wl,-rpath,${CHPL_THIRD_PARTY}/qthread/install/darwin-arm64-native-llvm-none-flat-jemalloc-system/lib -lqthread -L/opt/homebrew/Cellar/hwloc/2.12.0/lib -L${CHPL_THIRD_PARTY}/re2/install/darwin-arm64-native-llvm-none/lib -lre2 -Wl,-rpath,${CHPL_THIRD_PARTY}/re2/install/darwin-arm64-native-llvm-none/lib -lm -lpthread -L/opt/homebrew/Cellar/gmp/6.3.0/lib -lgmp -L/opt/homebrew/Cellar/hwloc/2.12.0/lib -Wl,-rpath,/opt/homebrew/Cellar/hwloc/2.12.0/lib -lhwloc -L/opt/homebrew/Cellar/jemalloc/5.3.0/lib -Wl,-rpath,/opt/homebrew/Cellar/jemalloc/5.3.0/lib -ljemalloc -L/opt/homebrew/lib -lsmol)
1212

1313
set(CHPL_COMPILER /opt/homebrew/Cellar/llvm@19/19.1.7/bin/clang)
1414
set(CHPL_LINKER /opt/homebrew/Cellar/llvm@19/19.1.7/bin/clang++)

demos/video/chapel-webcam/lib/smol.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22
#include "wctype.h"
33
#include "ctype.h"
44
#include "ImageHelper/stb_image_helper.h"
5+
void chpl__init_NDArray(int64_t _ln,
6+
int32_t _fn);
7+
void chpl__init_Standard(int64_t _ln,
8+
int32_t _fn);
9+
void chpl__init_Types(int64_t _ln,
10+
int32_t _fn);
11+
void chpl__init_Utilities(int64_t _ln,
12+
int32_t _fn);
13+
void chpl__init_ndarrayRandom(int64_t _ln,
14+
int32_t _fn);
515
void chpl__init_smol(int64_t _ln,
616
int32_t _fn);
717
int64_t square(int64_t x);

demos/video/chapel-webcam/smol.chpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Utilities as utils;
22

3+
use NDArray;
34

45
export proc square(x: int): int {
56
writeln(x, " * ", x, " = ", x * x);

0 commit comments

Comments
 (0)