Skip to content

Commit 689344c

Browse files
committed
Add calling chapel from C example.
1 parent 9474863 commit 689344c

8 files changed

Lines changed: 197 additions & 0 deletions

File tree

demos/video/chapel-webcam/Makefile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
include lib/Makefile.smol
3+
4+
all: libsmol.a main
5+
6+
libsmol.a: smol.chpl
7+
chpl --library --library-makefile --library-cmakelists smol.chpl
8+
9+
main: main.c libsmol.a
10+
$(CHPL_COMPILER) $(CHPL_CFLAGS) -o main main.c $(CHPL_LDFLAGS)
11+
12+
clean:
13+
rm -f main lib/libsmol.a
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.a
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
CHPL_RUNTIME_LIB = /opt/homebrew/Cellar/chapel/2.4.0_1/libexec/lib
2+
3+
CHPL_RUNTIME_INCL = /opt/homebrew/Cellar/chapel/2.4.0_1/libexec/runtime/include
4+
5+
CHPL_THIRD_PARTY = /opt/homebrew/Cellar/chapel/2.4.0_1/libexec/third-party
6+
7+
CHPL_HOME = /opt/homebrew/Cellar/chapel/2.4.0_1/libexec
8+
9+
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$(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
10+
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
12+
13+
CHPL_COMPILER = /opt/homebrew/Cellar/llvm@19/19.1.7/bin/clang
14+
15+
CHPL_LINKER = /opt/homebrew/Cellar/llvm@19/19.1.7/bin/clang++
16+
17+
CHPL_LINKERSHARED = /opt/homebrew/Cellar/llvm@19/19.1.7/bin/clang++ -shared
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
set(CHPL_RUNTIME_LIB /opt/homebrew/Cellar/chapel/2.4.0_1/libexec/lib)
2+
3+
set(CHPL_RUNTIME_INCL /opt/homebrew/Cellar/chapel/2.4.0_1/libexec/runtime/include)
4+
5+
set(CHPL_THIRD_PARTY /opt/homebrew/Cellar/chapel/2.4.0_1/libexec/third-party)
6+
7+
set(CHPL_HOME /opt/homebrew/Cellar/chapel/2.4.0_1/libexec)
8+
9+
set(smol_INCLUDE_DIRS ${CMAKE_CURRENT_LIST_DIR} /opt/homebrew/Cellar/chapel/2.4.0_1/libexec/modules/internal ${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)
10+
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)
12+
13+
set(CHPL_COMPILER /opt/homebrew/Cellar/llvm@19/19.1.7/bin/clang)
14+
set(CHPL_LINKER /opt/homebrew/Cellar/llvm@19/19.1.7/bin/clang++)
15+
set(CHPL_LINKERSHARED /opt/homebrew/Cellar/llvm@19/19.1.7/bin/clang++ -shared)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include "stdchpl.h"
2+
#include "wctype.h"
3+
#include "ctype.h"
4+
void chpl__init_smol(int64_t _ln,
5+
int32_t _fn);
6+
int64_t square(int64_t x);

demos/video/chapel-webcam/main.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include "lib/smol.h"
2+
3+
int main(int argc, char* argv[]) {
4+
chpl_library_init(argc, argv);
5+
6+
square(3);
7+
8+
chpl_library_finalize();
9+
10+
return 0;
11+
}
12+
13+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
export proc square(x: int): int {
3+
writeln(x, " * ", x, " = ", x * x);
4+
return x * x;
5+
}
6+
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"id": "16a699c3",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"import torch"
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": 21,
16+
"id": "6916b074",
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"A = torch.arange(0,1000000).reshape(1000, 1000).to(torch.float32).to('mps') * 0.00001\n",
21+
"B = torch.arange(0,1000000).reshape(1000, 1000).to(torch.float32).to('mps') * 0.00001"
22+
]
23+
},
24+
{
25+
"cell_type": "markdown",
26+
"id": "faaecb7f",
27+
"metadata": {},
28+
"source": []
29+
},
30+
{
31+
"cell_type": "code",
32+
"execution_count": 22,
33+
"id": "2779e698",
34+
"metadata": {},
35+
"outputs": [
36+
{
37+
"data": {
38+
"text/plain": [
39+
"tensor([[3.3283e+01, 3.3283e+01, 3.3283e+01, ..., 3.3333e+01, 3.3333e+01,\n",
40+
" 3.3333e+01],\n",
41+
" [8.3233e+01, 8.3234e+01, 8.3234e+01, ..., 8.3383e+01, 8.3383e+01,\n",
42+
" 8.3383e+01],\n",
43+
" [1.3318e+02, 1.3318e+02, 1.3318e+02, ..., 1.3343e+02, 1.3343e+02,\n",
44+
" 1.3343e+02],\n",
45+
" ...,\n",
46+
" [4.9833e+04, 4.9834e+04, 4.9834e+04, ..., 4.9933e+04, 4.9933e+04,\n",
47+
" 4.9933e+04],\n",
48+
" [4.9883e+04, 4.9883e+04, 4.9884e+04, ..., 4.9983e+04, 4.9983e+04,\n",
49+
" 4.9983e+04],\n",
50+
" [4.9933e+04, 4.9933e+04, 4.9934e+04, ..., 5.0033e+04, 5.0033e+04,\n",
51+
" 5.0033e+04]], device='mps:0')"
52+
]
53+
},
54+
"execution_count": 22,
55+
"metadata": {},
56+
"output_type": "execute_result"
57+
}
58+
],
59+
"source": [
60+
"torch.matmul(A,B)"
61+
]
62+
},
63+
{
64+
"cell_type": "code",
65+
"execution_count": 23,
66+
"id": "6cf685f6",
67+
"metadata": {},
68+
"outputs": [
69+
{
70+
"data": {
71+
"text/plain": [
72+
"tensor([[3.3283e+01, 8.3233e+01, 1.3318e+02, ..., 4.9833e+04, 4.9883e+04,\n",
73+
" 4.9933e+04],\n",
74+
" [3.3283e+01, 8.3234e+01, 1.3318e+02, ..., 4.9834e+04, 4.9883e+04,\n",
75+
" 4.9933e+04],\n",
76+
" [3.3283e+01, 8.3234e+01, 1.3318e+02, ..., 4.9834e+04, 4.9884e+04,\n",
77+
" 4.9934e+04],\n",
78+
" ...,\n",
79+
" [3.3333e+01, 8.3383e+01, 1.3343e+02, ..., 4.9933e+04, 4.9983e+04,\n",
80+
" 5.0033e+04],\n",
81+
" [3.3333e+01, 8.3383e+01, 1.3343e+02, ..., 4.9933e+04, 4.9983e+04,\n",
82+
" 5.0033e+04],\n",
83+
" [3.3333e+01, 8.3383e+01, 1.3343e+02, ..., 4.9933e+04, 4.9983e+04,\n",
84+
" 5.0033e+04]], device='mps:0')"
85+
]
86+
},
87+
"execution_count": 23,
88+
"metadata": {},
89+
"output_type": "execute_result"
90+
}
91+
],
92+
"source": [
93+
"torch.einsum('ij,jk->ki',A, B)"
94+
]
95+
},
96+
{
97+
"cell_type": "code",
98+
"execution_count": null,
99+
"id": "f6c1ec9b",
100+
"metadata": {},
101+
"outputs": [],
102+
"source": []
103+
}
104+
],
105+
"metadata": {
106+
"kernelspec": {
107+
"display_name": ".venv",
108+
"language": "python",
109+
"name": "python3"
110+
},
111+
"language_info": {
112+
"codemirror_mode": {
113+
"name": "ipython",
114+
"version": 3
115+
},
116+
"file_extension": ".py",
117+
"mimetype": "text/x-python",
118+
"name": "python",
119+
"nbconvert_exporter": "python",
120+
"pygments_lexer": "ipython3",
121+
"version": "3.12.9"
122+
}
123+
},
124+
"nbformat": 4,
125+
"nbformat_minor": 5
126+
}

0 commit comments

Comments
 (0)