Skip to content

Commit b5ad3bf

Browse files
authored
Merge pull request #30 from charguer/dot-rename
Rename vectvectmul to dot (product), improve a few things
2 parents 5bdb80e + 8ad4fd6 commit b5ad3bf

9 files changed

Lines changed: 59 additions & 19 deletions

File tree

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ __DEF(matmul, "fun (A B: int * int -> float) (p: int) -> fun (i j: int) -> reduc
88
/* Multiplies the vect A (dim n) by the vector B (dim n),
99
* and returns the result of the scalar product.
1010
*/
11-
float vect_vect_mul(float* a, float* b, int n) { // todo: requires divides
11+
float dot(float* a, float* b, int n) { // todo: requires divides
1212
__requires("A: int -> float, B: int -> float");
1313
__reads("a ~> Matrix1(n, A), b ~> Matrix1(n, B)");
1414
__ensures("_Res =. reduce_sum(n, fun j -> A(j) *. B(j))");
@@ -21,8 +21,8 @@ float vect_vect_mul(float* a, float* b, int n) { // todo: requires divides
2121
s += a[MINDEX1(n,i)] * b[MINDEX1(n,i)];
2222
__GHOST_END(focusA);
2323
__GHOST_END(focusB);
24-
__ghost(in_range_bounds, "i", "i_gt_0 <- lower_bound");
25-
__ghost(rewrite_float_linear, "inside := fun v -> &s ~~> v, by := reduce_sum_add_right(i, fun j -> A(j) *. B(j), i_gt_0)");
24+
__ghost(in_range_bounds, "i", "i_ge_0 <- lower_bound");
25+
__ghost(rewrite_float_linear, "inside := fun v -> &s ~~> v, by := reduce_sum_add_right(i, fun j -> A(j) *. B(j), i_ge_0)");
2626
}
2727
__ghost(eq_refl_float, "reduce_sum(n, fun j -> A(j) *. B(j))");
2828
return s;
Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,36 @@ let _ = if part = 1 then Run.script_cpp (fun () ->
2020
!! Loop.tile (int 32) ~index:"bi" ~bound:TileDivides [cFor "i"];
2121

2222
(* LATER: !! Variable.local_name ~var:"s" ~local_var:"t" [tSpanSeq [cForBody "bi"]]; *)
23-
!! Sequence.intro ~mark:"t_scope" ~start:[tFirst; cForBody "bi"] ~stop:[tLast; cForBody "bi"] ();
24-
!! Variable.local_name ~var:"s" ~local_var:"t" [cMark "t_scope"];
25-
!! Sequence.elim [cMark "t_scope"];
23+
!! (
24+
Sequence.intro ~mark:"t_scope" ~start:[tFirst; cForBody "bi"] ~stop:[tLast; cForBody "bi"] ();
25+
Variable.local_name ~var:"s" ~local_var:"t" [cMark "t_scope"];
26+
Sequence.elim [cMark "t_scope"];
27+
);
2628

2729
(* DEPRECATED? !! Sequence_basic.insert (trm_let (new_var "d", typ_f32) (trm_get (trm_find_var "s" []))) [tFirst; cForBody "bi"]; *)
28-
!! Variable.insert ~name:"d" ~typ:typ_f32 ~value:(trm_get (trm_find_var "s" [])) [cForBody "bi"; tFirst];
30+
!! (
31+
Variable.insert ~name:"d" ~typ:typ_f32 ~value:(trm_get (trm_find_var "s" [])) [cForBody "bi"; tFirst];
2932

30-
(* at this line, the output is the equivalent of vectvectmul0_gen.cpp,
33+
(* at this line, the output is the equivalent of dot0_gen.cpp,
3134
beware that "==" needs to be replaced with "=." and all the "__is_true" must be removed;
3235
some +. and + need to be fixed
33-
----> LATER: tweak display so that the output of _after.cpp is exactly vectvectmul0.cpp *)
36+
----> LATER: tweak display so that the output of _after.cpp is exactly dot0.cpp *)
3437
(* )
3538
3639
(* Part 2: *)
3740
let _ = if part = 2 then Run.script_cpp ~filename:"vv1.cpp" (fun () ->
3841
*)
3942
(* Why nbMulti? !! Accesses.shift_var ~inv:true ~factor:(trm_find_var "d" []) [nbMulti; cVarDef "t"]; *)
40-
!! Accesses.shift_var ~inv:true ~factor:(trm_find_var "d" []) [cFor "bi"; cVarDef "t"];
41-
!! Variable.inline [cVarDef "d"];
42-
!! Arith.simpl_surrounding_expr Arith.gather_rec [nbMulti; cVar "s"];
43+
Accesses.shift_var ~inv:true ~factor:(trm_find_var "d" []) [cFor "bi"; cVarDef "t"];
44+
Variable.inline [cVarDef "d"];
45+
Arith.simpl_surrounding_expr Arith.gather_rec [nbMulti; cVar "s"];
46+
);
4347
(* )
4448
4549
(* Part 3: *)
4650
let _ = if part = 3 then Run.script_cpp ~filename:"vv2.cpp" (fun () ->
4751
*)
48-
!! Resources.loop_minimize [cFor "i"];
52+
(* !! Resources.loop_minimize [cFor "i"]; *)
4953

5054
!! Loop.hoist [cVarDef "t"];
5155
!! Loop.fission [tBefore; cFor "bi"; cWriteVar "s"];
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ __ghost(define,
1919
"fun (i: int) (j: int) -> reduce_sum(p, fun k -> A(i, k) *. B(k, j))",
2020
"matmul <- x");
2121

22-
float vect_vect_mul(float* a, float* b, int n) {
22+
float dot(float* a, float* b, int n) {
2323
__requires("A: int -> float");
2424
__requires("B: int -> float");
2525
__ensures("(_Res =. reduce_sum(n, fun j -> A(j) *. B(j)))");
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ __ghost(define,
1919
"fun (i: int) (j: int) -> reduce_sum(p, fun k -> A(i, k) *. B(k, j))",
2020
"matmul <- x");
2121

22-
float vect_vect_mul(float* a, float* b, int n) {
22+
float dot(float* a, float* b, int n) {
2323
__requires("A: int -> float");
2424
__requires("B: int -> float");
2525
__ensures("(_Res =. reduce_sum(n, fun j -> A(j) *. B(j)))");
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ __ghost(define,
1919
"fun (i: int) (j: int) -> reduce_sum(p, fun k -> A(i, k) *. B(k, j))",
2020
"matmul <- x");
2121

22-
float vect_vect_mul(float* a, float* b, int n) {
22+
float dot(float* a, float* b, int n) {
2323
__requires("A: int -> float");
2424
__requires("B: int -> float");
2525
__ensures("(_Res =. reduce_sum(n, fun j -> A(j) *. B(j)))");
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <optitrust_models.h>
2+
3+
#include "omp.h"
4+
5+
float dot(float* a, float* b, int n) {
6+
float s = 0.f;
7+
float* const t = (float*)malloc(exact_div(n, 32) * sizeof(float));
8+
#pragma omp parallel for
9+
for (int bi = 0; bi < exact_div(n, 32); bi++) {
10+
t[bi] = ({
11+
float arith_res = 0;
12+
const float arith_res1 = arith_res;
13+
arith_res1;
14+
});
15+
for (int i = 0; i < 32; i++) {
16+
t[bi] = ({
17+
float arith_res = a[32 * bi + i] * b[32 * bi + i] + t[bi];
18+
const float arith_res2 = arith_res;
19+
arith_res2;
20+
});
21+
}
22+
}
23+
for (int bi = 0; bi < exact_div(n, 32); bi++) {
24+
s = ({
25+
float get = ({
26+
float arith_res = t[bi] + s;
27+
const float arith_res3 = arith_res;
28+
arith_res3;
29+
});
30+
const float getc = get;
31+
getc;
32+
});
33+
}
34+
free(t);
35+
return s;
36+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ __ghost(define,
1818
"fun (i: int) (j: int) -> reduce_sum(p, fun k -> A(i, k) *. B(k, j))",
1919
"matmul <- x");
2020

21-
float vect_vect_mul(float* a, float* b, int n) {
21+
float dot(float* a, float* b, int n) {
2222
__requires("A: int -> float");
2323
__requires("B: int -> float");
2424
__ensures("_Res =. reduce_sum(n, fun j -> A(j) *. B(j))");
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ __ghost(define,
1818
"fun (i: int) (j: int) -> reduce_sum(p, fun k -> A(i, k) *. B(k, j))",
1919
"matmul <- x");
2020

21-
float vect_vect_mul(float* a, float* b, int n) {
21+
float dot(float* a, float* b, int n) {
2222
__requires("A: int -> float");
2323
__requires("B: int -> float");
2424
__ensures("_Res =. reduce_sum(n, fun j -> A(j) *. B(j))");

include/optitrust_common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ __GHOST(in_range_shift_extend) {
307307
__GHOST(in_range_bounds) {
308308
__requires("x: int, a: int, b: int, s: int");
309309
__requires("in_range(x, range(a, b, s)), s >= 0");
310-
__ensures("lower_bound: x >= a, upper_bound: x <= b");
310+
__ensures("lower_bound: x >= a, upper_bound: x < b");
311311
__admitted();
312312
}
313313

0 commit comments

Comments
 (0)