| 1 | /* matmat.cvl: two matrix-matrix multiplication algorithms.
|
|---|
| 2 | * The first is the standard one, the second uses a complex
|
|---|
| 3 | * tiling optimization. This model is used to determine
|
|---|
| 4 | * whether the two are equivalent. Example:
|
|---|
| 5 | * civl verify matmatBad.cvl -inputBOUND=4
|
|---|
| 6 | * or (if you want to find the minimal counterexample)
|
|---|
| 7 | * civl verify matmatBad.cvl -inputBOUND=4 -min
|
|---|
| 8 | * will verify equivalent for all matrix dimensions and tile
|
|---|
| 9 | * sizes in the range 1..4.
|
|---|
| 10 | */
|
|---|
| 11 | #include<civlc.cvh>
|
|---|
| 12 | #define MIN(lhs, A, B) if ((A)<(B)) lhs=(A); else lhs=(B);
|
|---|
| 13 |
|
|---|
| 14 | $input int BOUND = 3;
|
|---|
| 15 | $input int L;
|
|---|
| 16 | $assume(1<=L && L<=BOUND);
|
|---|
| 17 | $input int M;
|
|---|
| 18 | $assume(1<=M && M<=BOUND);
|
|---|
| 19 | $input int N;
|
|---|
| 20 | $assume(1<=N && N<=BOUND);
|
|---|
| 21 | $input int TILE_SIZE;
|
|---|
| 22 | $assume(1<=TILE_SIZE && TILE_SIZE<=BOUND);
|
|---|
| 23 | $input double A[L][M];
|
|---|
| 24 | $input double B[M][N];
|
|---|
| 25 | double C[L][N]; // A*B computed by standard algorithm
|
|---|
| 26 | double D[L][N]; // A*B computed by tiled algorithm
|
|---|
| 27 |
|
|---|
| 28 | void spec() {
|
|---|
| 29 | for (int i = 0; i < L; i++)
|
|---|
| 30 | for (int j = 0; j < N; j++) {
|
|---|
| 31 | C[i][j] = 0.0;
|
|---|
| 32 | for (int k = 0; k < M; k++)
|
|---|
| 33 | C[i][j] += A[i][k] * B[k][j];
|
|---|
| 34 | }
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | void rowdist() {
|
|---|
| 38 | int hi1, hi2, hi3;
|
|---|
| 39 |
|
|---|
| 40 | for (int i = 0; i < L; i++) {
|
|---|
| 41 | for (int j = 0; j < N; j++) {
|
|---|
| 42 | D[i][j] = 0.0;
|
|---|
| 43 | }
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | for (int ii = 0; ii < L; ii+=TILE_SIZE) {
|
|---|
| 47 | for (int jj = 0; jj < N; jj+=TILE_SIZE) {
|
|---|
| 48 | for (int kk = 0; kk < M; kk+=TILE_SIZE) {
|
|---|
| 49 | MIN(hi1, ii+TILE_SIZE, L);
|
|---|
| 50 | for (int i = ii; i < hi1; i++) {
|
|---|
| 51 | MIN(hi2, jj+TILE_SIZE, N);
|
|---|
| 52 | for (int j = jj; j < hi2; j++) {
|
|---|
| 53 | MIN(hi3, kk+TILE_SIZE, M);
|
|---|
| 54 | for (int k = kk; k < hi1; k++) // oops hi1->hi3
|
|---|
| 55 | D[i][j] = D[i][j] + A[i][k] * B[k][j];
|
|---|
| 56 | }
|
|---|
| 57 | }
|
|---|
| 58 | }
|
|---|
| 59 | }
|
|---|
| 60 | }
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | void main() {
|
|---|
| 64 | spec();
|
|---|
| 65 | rowdist();
|
|---|
| 66 | for (int i = 0; i < L; i++) {
|
|---|
| 67 | for (int j = 0; j < N; j++) {
|
|---|
| 68 | $assert(C[i][j] == D[i][j]);
|
|---|
| 69 | }
|
|---|
| 70 | }
|
|---|
| 71 | }
|
|---|