| 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 matmat.cvl -inputBOUND=4
|
|---|
| 6 | * will verify equivalent for all matrix dimensions and tile
|
|---|
| 7 | * sizes in the range 1..4.
|
|---|
| 8 | */
|
|---|
| 9 | #define MIN(lhs, A, B) if ((A)<(B)) lhs=(A); else lhs=(B);
|
|---|
| 10 |
|
|---|
| 11 | $input int BOUND = 3;
|
|---|
| 12 | $input int L;
|
|---|
| 13 | $assume(1<=L && L<=BOUND);
|
|---|
| 14 | $input int M;
|
|---|
| 15 | $assume(1<=M && M<=BOUND);
|
|---|
| 16 | $input int N;
|
|---|
| 17 | $assume(1<=N && N<=BOUND);
|
|---|
| 18 | $input int TILE_SIZE;
|
|---|
| 19 | $assume(1<=TILE_SIZE && TILE_SIZE<=BOUND);
|
|---|
| 20 | $input double A[L][M];
|
|---|
| 21 | $input double B[M][N];
|
|---|
| 22 | double C[L][N]; // A*B computed by standard algorithm
|
|---|
| 23 | double D[L][N]; // A*B computed by tiled algorithm
|
|---|
| 24 |
|
|---|
| 25 | void spec() {
|
|---|
| 26 | for (int i = 0; i < L; i++)
|
|---|
| 27 | for (int j = 0; j < N; j++) {
|
|---|
| 28 | C[i][j] = 0.0;
|
|---|
| 29 | for (int k = 0; k < M; k++)
|
|---|
| 30 | C[i][j] += A[i][k] * B[k][j];
|
|---|
| 31 | }
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | void rowdist() {
|
|---|
| 35 | int hi1, hi2, hi3;
|
|---|
| 36 |
|
|---|
| 37 | for (int i = 0; i < L; i++) {
|
|---|
| 38 | for (int j = 0; j < N; j++) {
|
|---|
| 39 | D[i][j] = 0.0;
|
|---|
| 40 | }
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | for (int ii = 0; ii < L; ii+=TILE_SIZE) {
|
|---|
| 44 | for (int jj = 0; jj < N; jj+=TILE_SIZE) {
|
|---|
| 45 | for (int kk = 0; kk < M; kk+=TILE_SIZE) {
|
|---|
| 46 | MIN(hi1, ii+TILE_SIZE, L);
|
|---|
| 47 | for (int i = ii; i < hi1; i++) {
|
|---|
| 48 | MIN(hi2, jj+TILE_SIZE, N);
|
|---|
| 49 | for (int j = jj; j < hi2; j++) {
|
|---|
| 50 | MIN(hi3, kk+TILE_SIZE, M);
|
|---|
| 51 | for (int k = kk; k < hi3; k++)
|
|---|
| 52 | D[i][j] = D[i][j] + A[i][k] * B[k][j];
|
|---|
| 53 | }
|
|---|
| 54 | }
|
|---|
| 55 | }
|
|---|
| 56 | }
|
|---|
| 57 | }
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | void main() {
|
|---|
| 61 | spec();
|
|---|
| 62 | rowdist();
|
|---|
| 63 | for (int i = 0; i < L; i++) {
|
|---|
| 64 | for (int j = 0; j < N; j++) {
|
|---|
| 65 | $assert(C[i][j] == D[i][j]);
|
|---|
| 66 | }
|
|---|
| 67 | }
|
|---|
| 68 | }
|
|---|