Skip to content

Commit

Permalink
Merge pull request #22 from edin-dal/feature/skew-symmetry-support
Browse files Browse the repository at this point in the history
Add skew-symmetry and fancier structure support
  • Loading branch information
mtghorbani authored Jul 17, 2024
2 parents 690f24a + 7f30f48 commit 1a07598
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 2 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,19 @@ for (int i = 0; i < N; ++i) {
}
```

#### More Complex Structures

More complex structures can be represented through STUR as well. For example, assigning a matrix with skew-symmetric structure to another matrix can be represented as follows:

```
symbols: N
A(i, j) := B(i, j)
A:D(i, j) := (0 <= i < N) * (0 <= j < N)
B:D(i, j) := (0 <= i < N) * (0 <= j < N)
B:U(i, j) := (0 <= i <= j < N)
B:R(i, j, ip, jp) := -1 * (0 <= j < i < N) * (ip = j) * (jp = i)
```

### Advanced Syntax

#### Preprocessing
Expand Down
6 changes: 6 additions & 0 deletions examples/fancy-symmetry.stur
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
symbols: N
A(i, j) := B(i, j)
A:D(i, j) := (0 <= i < N) * (0 <= j < N)
B:D(i, j) := (0 <= i < N) * (0 <= j < N)
B:U(i, j) := (0 <= i <= j < N)
B:R(i, j, ip, jp) := -21.5 * (0 <= j < i < N) * (ip = j) * (jp = i)
6 changes: 6 additions & 0 deletions examples/skew-symmetry.stur
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
symbols: N
A(i, j) := B(i, j)
A:D(i, j) := (0 <= i < N) * (0 <= j < N)
B:D(i, j) := (0 <= i < N) * (0 <= j < N)
B:U(i, j) := (0 <= i <= j < N)
B:R(i, j, ip, jp) := -1 * (0 <= j < i < N) * (ip = j) * (jp = i)
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ object Optimizer {
(
expsNotInMap.filter(exp =>
exp match {
case acc @ Access(_, v, _) if v.isEmpty => false
case _ => true
case acc @ Access(_, v, k) if v.isEmpty && k != Tensor =>
false
case _ => true
}
),
false
Expand Down
41 changes: 41 additions & 0 deletions src/test/resources/correct_test_outputs/fancy-symmetry_wo_body.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

#include <iostream>
#include <random>
#include <algorithm>
#include <chrono>

using namespace std;
using namespace std::chrono;

extern "C"
void fn(double ** A, double ** B, int N) {


long time_computation = 0, start_computation, end_computation;
start_computation = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
for (int i = 0; i < N; ++i) {

for (int j = max({0, i}); j < N; ++j) {

A[i][j] += B[i][j];
}
}
end_computation = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
time_computation = end_computation - start_computation;
cout << time_computation << endl;
long time_reconstruction = 0, start_reconstruction, end_reconstruction;
start_reconstruction = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();


for (int ip = 0; ip < N; ++ip) {
int j = ip;
for (int jp = max({0, (ip) + 1}); jp < N; ++jp) {
int i = jp;
A[i][j] = (A[ip][jp] * -21.5);
}
}
end_reconstruction = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
time_reconstruction = end_reconstruction - start_reconstruction;
cout << time_reconstruction << endl;

}
41 changes: 41 additions & 0 deletions src/test/resources/correct_test_outputs/skew-symmetry_wo_body.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

#include <iostream>
#include <random>
#include <algorithm>
#include <chrono>

using namespace std;
using namespace std::chrono;

extern "C"
void fn(double ** A, double ** B, int N) {


long time_computation = 0, start_computation, end_computation;
start_computation = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
for (int i = 0; i < N; ++i) {

for (int j = max({0, i}); j < N; ++j) {

A[i][j] += B[i][j];
}
}
end_computation = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
time_computation = end_computation - start_computation;
cout << time_computation << endl;
long time_reconstruction = 0, start_reconstruction, end_reconstruction;
start_reconstruction = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();


for (int ip = 0; ip < N; ++ip) {
int j = ip;
for (int jp = max({0, (ip) + 1}); jp < N; ++jp) {
int i = jp;
A[i][j] = (A[ip][jp] * -1);
}
}
end_reconstruction = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
time_reconstruction = end_reconstruction - start_reconstruction;
cout << time_reconstruction << endl;

}
44 changes: 44 additions & 0 deletions src/test/scala/uk/ac/ed/dal/structtensor/codegen/CodegenTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2042,4 +2042,48 @@ class CodegenTest extends AnyFlatSpec with Matchers {
val lines2 = file2.getLines().toList
lines2 should be(lines1)
}

it should "generate code for skew-symmetry structure without the body" in {
Utils.cnt = 0
Main.main(
Array(
"-i",
"examples/skew-symmetry.stur",
"-o",
"src/test/resources/test_outputs/skew-symmetry_wo_body_test.cpp"
)
)

val file1 = scala.io.Source.fromFile(
"src/test/resources/correct_test_outputs/skew-symmetry_wo_body.cpp"
)
val file2 = scala.io.Source.fromFile(
"src/test/resources/test_outputs/skew-symmetry_wo_body_test.cpp"
)
val lines1 = file1.getLines().toList
val lines2 = file2.getLines().toList
lines2 should be(lines1)
}

it should "generate code for fancy-symmetry structure without the body" in {
Utils.cnt = 0
Main.main(
Array(
"-i",
"examples/fancy-symmetry.stur",
"-o",
"src/test/resources/test_outputs/fancy-symmetry_wo_body_test.cpp"
)
)

val file1 = scala.io.Source.fromFile(
"src/test/resources/correct_test_outputs/fancy-symmetry_wo_body.cpp"
)
val file2 = scala.io.Source.fromFile(
"src/test/resources/test_outputs/fancy-symmetry_wo_body_test.cpp"
)
val lines1 = file1.getLines().toList
val lines2 = file2.getLines().toList
lines2 should be(lines1)
}
}

0 comments on commit 1a07598

Please sign in to comment.