1
2 #include <sightglass.h>
3
4 #include <assert.h>
5 #include <stdlib.h>
6
7 #define LENGTH 300000
8 #define SIZE 10
9
10 static int **
mkmatrix2(int rows,int cols)11 mkmatrix2(int rows, int cols)
12 {
13 int **m = calloc(rows, sizeof(int *));
14 assert(m != NULL);
15
16 int i, j, count = 1;
17 for (i = 0; i < rows; i++) {
18 m[i] = calloc(cols, sizeof(int));
19 assert(m[i] != NULL);
20 for (j = 0; j < cols; j++) {
21 m[i][j] = count++;
22 }
23 }
24 return m;
25 }
26
27 static void
freematrix2(int rows,int ** m)28 freematrix2(int rows, int **m)
29 {
30 while (--rows > -1) {
31 free(m[rows]);
32 }
33 free(m);
34 }
35
36 static int **
mmult2(int rows,int cols,int ** m1,int ** m2,int ** m3)37 mmult2(int rows, int cols, int **m1, int **m2, int **m3)
38 {
39 int i, j, k, val;
40
41 for (i = 0; i < rows; i++) {
42 for (j = 0; j < cols; j++) {
43 val = 0;
44 for (k = 0; k < cols; k++) {
45 val += m1[i][k] * m2[k][j];
46 }
47 m3[i][j] = val;
48 }
49 }
50 return m3;
51 }
52
53 typedef struct matrix2Ctx_ {
54 int **m1, **m2, **mm;
55 int n;
56 int res;
57 } matrix2Ctx;
58
59 void
matrix2_setup(void * global_ctx,void ** ctx_p)60 matrix2_setup(void *global_ctx, void **ctx_p)
61 {
62 (void) global_ctx;
63
64 static matrix2Ctx ctx;
65 ctx.n = LENGTH;
66 ctx.m1 = mkmatrix2(SIZE, SIZE);
67 ctx.m2 = mkmatrix2(SIZE, SIZE);
68 ctx.mm = mkmatrix2(SIZE, SIZE);
69 assert(ctx.m1 != NULL && ctx.m2 != NULL && ctx.mm != NULL);
70
71 *ctx_p = (void *) &ctx;
72 }
73
74 void
matrix2_teardown(void * ctx_)75 matrix2_teardown(void *ctx_)
76 {
77 matrix2Ctx *ctx = (matrix2Ctx *) ctx_;
78
79 freematrix2(SIZE, ctx->m1);
80 freematrix2(SIZE, ctx->m2);
81 freematrix2(SIZE, ctx->mm);
82 }
83
84 void
matrix2_body(void * ctx_)85 matrix2_body(void *ctx_)
86 {
87
88 matrix2Ctx *ctx = (matrix2Ctx*) ctx_;
89
90 int i;
91 for (i = 0; i < ctx->n; i++) {
92 ctx->mm = mmult2(SIZE, SIZE, ctx->m1, ctx->m2, ctx->mm);
93 }
94 ctx->res = ctx->mm[0][0] + ctx->mm[2][3] + ctx->mm[3][2] + ctx->mm[4][4];
95 BLACK_BOX(ctx->res);
96
97 }
98