-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.cu
115 lines (106 loc) · 2.18 KB
/
utils.cu
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
109
110
111
112
113
114
115
/**
* @file utils.c
* author Yujia Zhai (yzhai015@ucr.edu)
* @brief
* @version 0.1
* @date 2020-02-06
*
* @copyright Copyright (c) 2020
*
*/
#include <stdio.h>
#include "utils.h"
#include <stdlib.h>
#include <math.h>
#include <time.h>
void print_vector(float *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_lda(float *A, int lda, int m, int n)
{
int i,j;
float *a_curr_pos=A;
printf("[");
for (i = 0; i < n; i++){
for (j=0;j<m;j++){
printf("%5.2f, ",*a_curr_pos);
a_curr_pos++;
}
a_curr_pos+=(lda-m);
}
printf("]\n");
}
void print_matrix(const float *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(float *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] = (float)(rand() % 100);
if (rand() % 2 == 0)
{
A[i * n + j] *= -1.0;
}
}
}
}
void copy_matrix(float *src, float *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(float *mat1, float *mat2, int n)
{
float diff = 0.0;
int i;
for (i = 0; mat1 + i && mat2 + i && i < n; i++)
{
//diff = fabs(mat1[i] - mat2[i]) / fabs(mat2[i]);
diff = fabs(mat1[i] - mat2[i]);
if (diff > 1e-3) {
printf("error. %5.2f,%5.2f,%d\n", mat1[i],mat2[i],i);
return false;
}
}
return true;
}