-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.c
88 lines (81 loc) · 1.72 KB
/
utils.c
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
#include <stdio.h>
#include "utils.h"
#include <stdlib.h>
#include <math.h>
#include <sys/time.h>
#include <stdbool.h>
void print_vector(double *vec, int n)
{
int i;
for (i = 0; i < n; i++)
{
//if (i % 5 == 0) printf("\n");
printf(" %5.2f, ", vec[i]);
}
printf("\n");
}
void print_matrix(const double *A, int m, int n)
{
int i;
printf("[");
for (i = 0; i < m * n; i++)
{
if ((i + 1) % n == 0)
printf("%5.2f ", A[i]);
else
printf("%5.2f, ", A[i]);
if ((i + 1) % n == 0)
{
if (i + 1 < m * n)
printf(";\n");
}
}
printf("]\n");
}
double get_sec()
{
struct timeval time;
gettimeofday(&time, NULL);
return (time.tv_sec + 1e-6 * time.tv_usec);
}
void randomize_matrix(double *A, int m, int n)
{
srand(time(NULL));
int i, j;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
A[i * n + j] = (double)(rand() % 100) + 0.01 * (rand() % 100);
A[i * n + j] = i * n + j;
if (rand() % 2 == 1)
{
//A[i * n + j] *= -1.0;
}
}
}
}
void copy_matrix(double *src, double *dest, int n)
{
int i;
for (i = 0; src + i && dest + i && i < n; i++)
{
*(dest + i) = *(src + i);
}
if (i != n)
{
printf("copy failed at %d while there are %d elements in total.\n", i, n);
}
}
bool verify_matrix(double *mat1, double *mat2, int n)
{
double diff = 0.0;
int i;
for (i = 0; mat1 + i && mat2 + i && i < n; i++)
{
diff = fabs(mat1[i] - mat2[i]);
if (diff > 1e-6) break;
}
if (i != n) return false;
else return true;
}