Skip to content

Commit a7d5ad3

Browse files
committed
Bridge module is working.
1 parent a585c5c commit a7d5ad3

6 files changed

Lines changed: 152 additions & 12 deletions

File tree

bridge/Bridge

22 KB
Binary file not shown.

bridge/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ bridge.o: lib/bridge.cpp include/bridge.h
44
g++ -c lib/bridge.cpp -I include -o bridge.o -I /Users/iainmoncrief/Documents/Github/ChAI/bridge/libtorch/include/torch/csrc/api/include -I /Users/iainmoncrief/Documents/Github/ChAI/bridge/libtorch/include --std=c++17
55

66
Bridge: bridge.o lib/Bridge.chpl
7-
chpl lib/Bridge.chpl include/bridge.h bridge.o -L /Users/iainmoncrief/Documents/Github/ChAI/bridge/libtorch/lib -ltorch -ltorch_cpu -lc10 --ldflags "-Wl,-rpath,/Users/iainmoncrief/Documents/Github/ChAI/bridge/libtorch/lib" --print-commands
7+
chpl lib/Bridge.chpl include/bridge.h bridge.o -L /Users/iainmoncrief/Documents/Github/ChAI/bridge/libtorch/lib -ltorch -ltorch_cpu -lc10 --ldflags "-Wl,-rpath,/Users/iainmoncrief/Documents/Github/ChAI/bridge/libtorch/lib"
88

99
clean:
1010
rm -f bridge.o

bridge/bridge.o

8.52 KB
Binary file not shown.

bridge/include/bridge.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
extern "C" {
88
#endif
99

10+
typedef struct tensor_result_t {
11+
float* data;
12+
int* sizes;
13+
int dim;
14+
} tensor_result_t;
15+
1016
int baz(void);
1117

1218
void wrHello(void);
@@ -15,6 +21,21 @@ void wrHelloTorch(void);
1521

1622
float sumArray(float* arr, int* sizes, int dim);
1723

24+
void increment(float* arr, int* sizes, int dim, float* output);
25+
tensor_result_t increment2(float* arr, int* sizes, int dim);
26+
27+
// void convolve(
28+
// float* input,
29+
// int* input_sizes,
30+
// int input_dim,
31+
// float* kernel,
32+
// int* kernel_sizes,
33+
// int kernel_dim,
34+
// float* output,
35+
// int* output_sizes,
36+
// int output_dim,
37+
// )
38+
1839
#ifdef __cplusplus
1940
}
2041
#endif

bridge/lib/Bridge.chpl

Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,37 @@ extern proc baz(): int;
55
extern proc wrHello(): void;
66
extern proc wrHelloTorch(): void;
77
extern proc sumArray(arr: [] real(32), sizes: [] int(32), dim: int(32)): real(32);
8+
extern proc increment(arr: [] real(32), sizes: [] int(32), dim: int(32), ref output: [] real(32)): void;
9+
10+
extern record tensor_result_t {
11+
var data: c_ptr(real(32));
12+
var sizes: c_ptr(int(32));
13+
var dim: int(32);
14+
}
15+
16+
extern proc increment2(arr: [] real(32), sizes: [] int(32), dim: int(32)): tensor_result_t;
817

918

10-
baz();
19+
// baz();
1120

12-
wrHello();
21+
// wrHello();
1322

1423

15-
wrHelloTorch();
24+
// wrHelloTorch();
1625

17-
writeln("baz: ", baz());
26+
// writeln("baz: ", baz());
1827

1928

2029
var dom = {0..<10, 0..<10};
2130
var a: [dom] real(32);
2231
for (idx,i) in zip(dom,0..<dom.size) do
2332
a[idx] = i:real(32);
2433

25-
var sizes: [0..1] int(32);
26-
sizes[0] = dom.dim(0).size : int(32);
27-
sizes[1] = dom.dim(1).size : int(32);
34+
// var sizes: [0..1] int(32);
35+
// sizes[0] = dom.dim(0).size : int(32);
36+
// sizes[1] = dom.dim(1).size : int(32);
2837

29-
writeln("Sum of array: ", sumArray(a,sizes,a.rank));
38+
// writeln("Sum of array: ", sumArray(a,sizes,a.rank));
3039

3140

3241
record arrayShape_c {
@@ -48,8 +57,47 @@ proc getArrayShapeC(const ref arr: [] ?eltType): arrayShape_c(arr.rank) {
4857
return shape;
4958
}
5059

51-
writeln("Sum of array: ", sumArray(a,getSizeArray(a),a.rank));
60+
// writeln("Sum of array: ", sumArray(a,getSizeArray(a),a.rank));
61+
62+
// var shape = getArrayShapeC(a);
63+
// writeln("Shape of array: ", shape.sizes);
64+
// writeln("Sum of array: ", sumArray(a,shape.sizes,shape.rank));
5265

5366
var shape = getArrayShapeC(a);
54-
writeln("Shape of array: ", shape.sizes);
55-
writeln("Sum of array: ", sumArray(a,shape.sizes,shape.rank));
67+
writeln("A: ", a);
68+
69+
var b: [a.domain] real(32);
70+
increment(a,shape.sizes,shape.rank,b);
71+
writeln("B: ", b);
72+
73+
74+
proc getResultTensorShape(param dim: int, result: tensor_result_t): dim*int {
75+
var shape: dim*int;
76+
for i in 0..<dim do
77+
shape[i] = result.sizes[i] : int;
78+
return shape;
79+
}
80+
81+
proc domainFromShape(shape: int ...?rank): domain(rank,int) {
82+
const _shape = shape;
83+
var ranges: rank*range;
84+
for param i in 0..<rank do
85+
ranges(i) = 0..<_shape(i);
86+
return {(...ranges)};
87+
}
88+
89+
var c = increment2(a,shape.sizes,shape.rank);
90+
91+
var cShape = getResultTensorShape(shape.rank, c);
92+
93+
var cDom = domainFromShape((...cShape));
94+
95+
var C: [cDom] real(32);
96+
forall i in 0..<cDom.size {
97+
var idx = cDom.orderToIndex(i);
98+
C[idx] = c.data[i];
99+
}
100+
101+
102+
writeln("C: ", C);
103+

bridge/lib/bridge.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,41 @@
66
#include <iostream>
77
#include <vector>
88

9+
#include <cstdint>
10+
11+
using float32_t = float;
12+
13+
14+
int tensor_result_elements(tensor_result_t &result) {
15+
int size = 1;
16+
for (int i = 0; i < result.dim; ++i) {
17+
size *= result.sizes[i];
18+
}
19+
return size;
20+
}
21+
22+
size_t tensor_result_size(tensor_result_t &result) {
23+
return sizeof(float32_t) * tensor_result_elements(result);
24+
}
25+
26+
void store_tensor(torch::Tensor &input, float32_t* dest) {
27+
float32_t * data = input.data_ptr<float32_t>();
28+
size_t bytes_size = sizeof(float32_t) * input.numel();
29+
std::memmove(dest,data,bytes_size);
30+
}
31+
32+
tensor_result_t tensor_result_convert(torch::Tensor &tensor) {
33+
tensor_result_t result;
34+
result.dim = tensor.dim();
35+
result.sizes = new int[result.dim];
36+
for (int i = 0; i < result.dim; ++i) {
37+
result.sizes[i] = tensor.size(i);
38+
}
39+
result.data = new float32_t[tensor_result_elements(result)];
40+
store_tensor(tensor, result.data);
41+
return result;
42+
}
43+
944
void secret() {
1045
std::cout << "Secret function called!" << std::endl;
1146
auto x = torch::randn({5, 3});
@@ -34,6 +69,42 @@ extern "C" void wrHelloTorch(void) {
3469
// std::cout << t << std::endl;
3570
}
3671

72+
73+
74+
75+
extern "C" void increment(float* arr, int* sizes, int dim, float* output) {
76+
// Convert sizes to std::vector<int64_t>
77+
std::vector<int64_t> sizes_vec(sizes, sizes + dim);
78+
auto shape = at::IntArrayRef(sizes_vec);
79+
auto t = torch::from_blob(arr, shape, torch::kFloat);
80+
81+
// // Increment the tensor
82+
// auto incremented_tensor = t + 1;
83+
84+
// // Store the incremented tensor in the output array
85+
// storeTensor(incremented_tensor, output);
86+
87+
auto incremented_tensor = torch::from_blob(output, shape, torch::kFloat);
88+
incremented_tensor.copy_(t + 1);
89+
}
90+
91+
extern "C" tensor_result_t increment2(float* arr, int* sizes, int dim) {
92+
// Convert sizes to std::vector<int64_t>
93+
std::vector<int64_t> sizes_vec(sizes, sizes + dim);
94+
auto shape = at::IntArrayRef(sizes_vec);
95+
auto t = torch::from_blob(arr, shape, torch::kFloat);
96+
97+
// // Increment the tensor
98+
// auto incremented_tensor = t + 1;
99+
100+
// // Store the incremented tensor in the output array
101+
// storeTensor(incremented_tensor, output);
102+
103+
auto incremented_tensor = t + 1;
104+
105+
return tensor_result_convert(incremented_tensor);
106+
}
107+
37108
extern "C" float sumArray(float* arr, int* sizes, int dim) {
38109
// Convert sizes to std::vector<int64_t>
39110

0 commit comments

Comments
 (0)