Skip to content

Commit f2a3656

Browse files
committed
Add multisource
1 parent bad95e8 commit f2a3656

File tree

5 files changed

+86
-2
lines changed

5 files changed

+86
-2
lines changed

6_multisource/Makefile

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
all: lib.exe
2+
3+
clean:
4+
rm -f *.o *.ll *.exe
5+
6+
%.o: %.c
7+
../dockerscript.sh clang-12 -c -fuse-ld=lld -flto /host/$^ -O2 -ffast-math -o /host/$@
8+
9+
lib.exe: myblas.o multisource.o
10+
../dockerscript.sh clang-12 -fuse-ld=lld -flto /host/myblas.o /host/multisource.o -O2 -ffast-math -o /host/$@ -Wl,-mllvm=-load=/Enzyme/enzyme/build/Enzyme/ClangEnzyme-12.so
11+
12+
run-%: %.exe
13+
../dockerscript.sh /host/$^ 3

6_multisource/multisource.c

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <math.h>
4+
#include <assert.h>
5+
#include "myblas.h"
6+
7+
double dotabs(struct complex* alpha, struct complex* beta, int n) {
8+
struct complex prod = myblas_cdot(alpha, beta, n);
9+
return myblas_cabs(prod);
10+
}
11+
12+
void __enzyme_autodiff(void*, ...);
13+
int enzyme_const, enzyme_dup, enzyme_out;
14+
15+
int main(int argc, char *argv[]) {
16+
int n = 3;
17+
if (argc > 1) {
18+
n = atoi(argv[1]);
19+
}
20+
21+
22+
struct complex *A = (struct complex*)malloc(sizeof(struct complex) * n);
23+
assert(A != 0);
24+
for(int i=0; i<n; i++)
25+
A[i] = (struct complex){(i+1), (i+2)};
26+
27+
struct complex *grad_A = (struct complex*)malloc(sizeof(struct complex) * n);
28+
assert(grad_A != 0);
29+
for(int i=0; i<n; i++)
30+
grad_A[i] = (struct complex){0,0};
31+
32+
33+
34+
struct complex *B = (struct complex*)malloc(sizeof(struct complex) * n);
35+
assert(B != 0);
36+
for(int i=0; i<n; i++)
37+
B[i] = (struct complex){-3-i, 2*i};
38+
39+
struct complex *grad_B = (struct complex*)malloc(sizeof(struct complex) * n);
40+
assert(grad_B != 0);
41+
for(int i=0; i<n; i++)
42+
grad_B[i] = (struct complex){0,0};
43+
44+
__enzyme_autodiff((void*)dotabs, A, grad_A, B, grad_B, n);
45+
printf("Gradient dotabs(A)[0] = %f\n", grad_A[0].r);
46+
47+
return 0;
48+
}

6_multisource/myblas.c

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include "myblas.h"
2+
3+
struct complex myblas_cdot(struct complex* x, struct complex *y, int n) {
4+
struct complex sum = { 0, 0 };
5+
for (int i=0; i<n; i++) {
6+
sum.r += x[i].r * y[i].r - x[i].i * y[i].i;
7+
sum.i += x[i].r * y[i].i + x[i].i * y[i].r;
8+
}
9+
return sum;
10+
}
11+
12+
double myblas_cabs(struct complex x) {
13+
return x.r * x.r + x.i * x.i;
14+
}

6_multisource/myblas.h

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
struct complex {
2+
double r;
3+
double i;
4+
};
5+
6+
struct complex myblas_cdot(struct complex* x, struct complex *y, int n);
7+
8+
double myblas_cabs(struct complex x);

docker/Dockerfile

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ ENV DEBIAN_FRONTEND=noninteractive
55
RUN apt-get -y update && apt-get install -y --no-install-recommends curl gnupg lsb-core software-properties-common \
66
&& curl -fsSL https://apt.llvm.org/llvm-snapshot.gpg.key|apt-key add - \
77
&& apt-add-repository "deb http://apt.llvm.org/`lsb_release -c | cut -f2`/ llvm-toolchain-`lsb_release -c | cut -f2`-12 main" \
8-
&& apt-get install -y --no-install-recommends autoconf cmake ninja-build gcc g++ libtool gfortran llvm-12-dev clang-12 libopenmpi-dev openmpi-bin git \
8+
&& apt-get install -y --no-install-recommends autoconf cmake ninja-build gcc g++ libtool gfortran llvm-12-dev lld-12 clang-12 libopenmpi-dev openmpi-bin git \
99
&& apt-get autoremove -y --purge \
1010
&& apt-get clean -y \
1111
&& rm -rf /var/lib/apt/lists/*
@@ -19,6 +19,7 @@ RUN git clone https://github.com/wsmoses/Enzyme.git \
1919

2020
RUN update-alternatives --install /usr/bin/clang clang /usr/bin/clang-12 10 --slave /usr/bin/clang++ clang++ /usr/bin/clang++-12 \
2121
&& update-alternatives --install /usr/bin/opt opt /usr/bin/opt-12 10 \
22+
&& update-alternatives --install /usr/bin/lld lld /usr/bin/lld-12 10 \
2223
&& update-alternatives --install /usr/bin/llvm-symbolizer llvm-symbolizer /usr/bin/llvm-symbolizer-12 10
2324

24-
ENV DEBIAN_FRONTEND=
25+
ENV DEBIAN_FRONTEND=

0 commit comments

Comments
 (0)