| [056aaa8] | 1 | /* matmat_spec.c: sequential matrix multiplication serves as
|
|---|
| 2 | * specification when compared with the MPI implementation.
|
|---|
| 3 | */
|
|---|
| 4 | #include <stdlib.h>
|
|---|
| 5 | #include <stdio.h>
|
|---|
| 6 | #include <string.h>
|
|---|
| 7 |
|
|---|
| 8 | #ifdef _CIVL
|
|---|
| 9 |
|
|---|
| 10 | #include <civlc.cvh>
|
|---|
| 11 | /* Dimensions of 2 matrices: a[N][L] * b[L][M] */
|
|---|
| 12 | $input int NB = 5; // upper bound of N
|
|---|
| 13 | $input int N;
|
|---|
| 14 | $assume(0 < N && N <= NB);
|
|---|
| 15 | $input int LB = 5; // upper bound of L
|
|---|
| 16 | $input int L;
|
|---|
| 17 | $assume(0 < L && L <= LB);
|
|---|
| 18 | $input int MB = 5; // upper bound of M
|
|---|
| 19 | $input int M;
|
|---|
| 20 | $assume(0 < M && M <= MB);
|
|---|
| 21 | $input double a[N][L]; // input data for matrix a
|
|---|
| 22 | $input double b[L][M]; // input data for matrix b
|
|---|
| 23 | $output double output[N][M];
|
|---|
| 24 |
|
|---|
| 25 | #else
|
|---|
| 26 |
|
|---|
| 27 | int N = 3, L = 3, M = 3;
|
|---|
| 28 |
|
|---|
| 29 | #endif
|
|---|
| 30 |
|
|---|
| 31 | /* prints a matrix. */
|
|---|
| 32 | void printMatrix(int numRows, int numCols, double *m) {
|
|---|
| 33 | int i, j;
|
|---|
| 34 |
|
|---|
| 35 | for (i = 0; i < numRows; i++) {
|
|---|
| 36 | for (j = 0; j < numCols; j++)
|
|---|
| 37 | printf("%f ", m[i*numCols + j]);
|
|---|
| 38 | printf("\n");
|
|---|
| 39 | }
|
|---|
| 40 | printf("\n");
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | /* Computes a vetor with length L times a matrix with dimensions [L][M] */
|
|---|
| 44 | void vecmat(double vector[L], double matrix[L][M], double result[M]) {
|
|---|
| 45 | int j, k;
|
|---|
| 46 |
|
|---|
| 47 | for (j = 0; j < M; j++)
|
|---|
| 48 | for (k = 0, result[j] = 0.0; k < L; k++)
|
|---|
| 49 | result[j] += vector[k]*matrix[k][j];
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | int main(int argc, char *argv[]) {
|
|---|
| 53 | int i, j;
|
|---|
| 54 | double c[N][M];
|
|---|
| 55 |
|
|---|
| 56 | #ifndef _CIVL
|
|---|
| 57 | double a[N][L], b[L][M];
|
|---|
| 58 |
|
|---|
| 59 | // random initialization:
|
|---|
| 60 | for (i = 0; i < N; i++)
|
|---|
| 61 | for (j = 0; j < L; j++)
|
|---|
| 62 | a[i][j] = i * N + j;
|
|---|
| 63 | for (i = 0; i < L; i++)
|
|---|
| 64 | for (j = 0; j < M; j++)
|
|---|
| 65 | b[i][j] = i * L + j;
|
|---|
| 66 | #endif
|
|---|
| 67 | for(int i=0; i < N; i++)
|
|---|
| 68 | vecmat(a[i], b, c[i]);
|
|---|
| 69 | #ifdef _CIVL
|
|---|
| 70 | // copy to out put
|
|---|
| 71 | for(int i=0; i < N; i++)
|
|---|
| 72 | memcpy(output[i], c[i], M * sizeof(double));
|
|---|
| 73 | #endif
|
|---|
| 74 | printMatrix(N, M, &c[0][0]);
|
|---|
| 75 | return 0;
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|