summaryrefslogtreecommitdiff
path: root/debug.c
blob: 844e8e8bd94b7601d5ab5a880bdab9cb5c278ffb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

#include "tests.h"
#include "matrix.h"

#define STR_FMT(str, ...)                        \
    do {                                         \
        int tmp = asprintf(&(str), __VA_ARGS__); \
        if (tmp < 0) str = NULL;                 \
    } while (0)

#define TIMER(func, details, samples, ...)                              \
    do {                                                                \
        size_t _samples = (samples);                                    \
        clock_t _t;                                                     \
        double _xs[_samples];                                           \
        for (size_t _i = 0; _i < _samples; ++_i)                        \
        {                                                               \
            _t = clock();                                               \
            (__VA_ARGS__);                                              \
            _xs[_i] = ((double) (clock() - _t)) / CLOCKS_PER_SEC;       \
        }                                                               \
        double max = _xs[0], min = _xs[0], sum = 0;                     \
                                                                        \
        for (size_t i = 0; i < _samples; ++i)                           \
        {                                                               \
            if (_xs[i] > max) max = _xs[i];                             \
            else if (_xs[i] < min) min = _xs[i];                        \
            sum += _xs[i];                                              \
        }                                                               \
        printf("BENCHMARK (CPU time in s)\n");                          \
        printf("---------------------\n");                              \
        printf("Function            : %s\n", (func));                   \
        if ((details)[0] != '\0')                                       \
            printf("Details             : %s\n", (details));            \
        printf("Total (%ld samples) : %f\n", _samples, sum);            \
        printf("Avg.                : %f\n", sum / _samples);           \
        printf("Max                 : %f\n", max);                      \
        printf("Min                 : %f\n", min);                      \
    } while (0)

#define BENCHMARK_RAND1(m, n, bound_l, bound_u, func,...)                     \
    do {                                                                      \
        Matrix *A = matrix_rand((m), (n), (bound_l), (bound_u), MATRIX_NONE); \
        func(A, __VA_ARGS__);                                                 \
        free(A);                                                              \
    } while(0)

int main()
{
    srand(time(NULL));
    /* run_tests() */

    /* TODO: Add timing. For now the macro executes the given function once */
    /* TODO: Fix for functions that take just the matrix and no additional arguments */
    BENCHMARK_RAND1(
            100, 100,                /* Dimensions: m x n */
            0, 100,                  /* Value bounds */
            matrix_swap1,            /* Function to be tested */
            MATRIX_SWAP_ROWS, 0, 1   /* List of arguments to function */
            );

    Matrix *A, *B;
    int n = 100;
    size_t samples = 500;
    char *str;

    STR_FMT(str, "rows, %dx%d random matrices", n, n);

    TIMER("swap()", str, samples,
            A = matrix_rand(n, n, 0, 100, MATRIX_NONE),
            matrix_swap(A, MATRIX_SWAP_ROWS, 0, 1),
            matrix_free(A)
         );

    printf("\n");

    TIMER("swap1()", str, samples,
            A = matrix_rand(n, n, 0, 100, MATRIX_NONE),
            matrix_swap1(A, MATRIX_SWAP_ROWS, 0, 1),
            matrix_free(A)
         );

    printf("\n");

    TIMER("matrix_eq()", "", samples,
            A = matrix_rand(n, n, 0, 100, MATRIX_NONE),
            B = matrix_rand(n, n, 0, 100, MATRIX_NONE),
            matrix_eq(A, B, 0.01),
            matrix_free(A),
            matrix_free(B)
         );

    printf("\n");

    TIMER("matrix_add()", "", samples,
            A = matrix_rand(n, n, 0, 100, MATRIX_NONE),
            B = matrix_rand(n, n, 0, 100, MATRIX_NONE),
            matrix_add(A, B),
            matrix_free(A),
            matrix_free(B)
         );

    free(str);
}