-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatfunc.c
180 lines (145 loc) · 3.46 KB
/
matfunc.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "matfunc.h"
#define ALLOW_ERROR 0.00001
//#define GET_TIME_METHOD 1
#ifdef GET_TIME_METHOD
#include <time.h>
double getTime()
{
clock_t ct;
ct = clock();
return((double)ct / CLOCKS_PER_SEC);
}
#else
#include <sys/time.h>
double getTime()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return(tv.tv_sec + (double)tv.tv_usec*1e-6);
}
#endif
double diffMat(const int matsize, const double *mata, const double *matb)
{
int i,j;
double calcerror=0.0;
for(i=0; i<matsize; i++){
for(j=0; j<matsize; j++){
calcerror += (mata[i*matsize+j] - matb[i*matsize+j]) * (mata[i*matsize+j] - matb[i*matsize+j]);
}
}
return(calcerror);
}
double checkMulMatFunc(void (*func)(const int matsize, const double *mata, const double *matb, double *matc),
const int matsize,
const double *mata,
const double *matb,
const double *matc)
{
double start, end;
double *tmp_matc;
double calcerror;
double time;
tmp_matc = allocMat(matsize);
clearMat(matsize, tmp_matc);
start = getTime();
func(matsize, mata, matb, tmp_matc);
end = getTime();
time = end-start;
printf("Time=%f sec , ", time);
calcerror = diffMat(matsize, tmp_matc, matc);
if(calcerror > ALLOW_ERROR){
printf("\tWrong Calculation! (error=%.10f)\n", calcerror);
}else{
printf("\tOK (error =%.10f)\n", calcerror);
}
return(time);
}
double* allocMat(int matsize)
{
double *mat;
mat = (double *)malloc(matsize * matsize * sizeof(double));
if(mat == NULL){ printf("Error : allocation fault.\n"); exit(-1); }
return(mat);
}
void freeMat(double *mat)
{
free(mat);
}
void genMat(const int matsize, double *mat)
{
int i,j;
for(i=0; i<matsize; i++){
for(j=0; j<matsize; j++){
mat[i*matsize + j] = ((double)rand())/((double)RAND_MAX+1);
}
}
}
void printMat(const int matsize, const double *mat)
{
int i,j;
printf("N=%d\n", matsize);
for(i=0; i<matsize; i++){
for(j=0; j<matsize; j++){
printf("%f\n", mat[i*matsize + j]);
}
}
}
void writeMat(const int matsize, const double *mat, const char *filename)
{
int i,j;
FILE *fp;
fp = fopen(filename, "w");
fprintf(fp, "%d\n", matsize);
for(i=0; i<matsize; i++){
for(j=0; j<matsize; j++){
fprintf(fp, "%.10f\n", mat[i*matsize + j]);
}
}
fclose(fp);
}
/* return mat size */
int readMat(double *mat, const char *filename)
{
int i;
int matsize;
char buf[1024];
FILE *fp;
char *result;
fp = fopen(filename, "r");
if(fp==NULL){ printf("ERROR : file open failure\n"); exit(-1); }
result = fgets(buf, sizeof(buf), fp);
if(result==NULL){ printf("ERROR : file read failure\n"); exit(-1); }
sscanf(buf, "%d", &matsize);
if(matsize <= 0){ printf("ERROR : file format failure\n"); exit(-1); }
for(i=0; i<matsize*matsize; i++){
result = fgets(buf, sizeof(buf), fp);
if(result==NULL){ printf("ERROR : file read failure\n"); exit(-1); }
sscanf(buf, "%lf", &(mat[i]));
}
fclose(fp);
return(matsize);
}
void clearMat(const int matsize, double *mat)
{
int i,j;
for(i=0; i<matsize; i++){
for(j=0; j<matsize; j++){
mat[i*matsize+j] = 0.0;
}
}
}
void mulMatSample(const int matsize, const double *mata, const double *matb, double *matc)
{
int i,j,k;
for(k=0; k<matsize; k++){
for(j=0; j<matsize; j++){
for(i=0; i<matsize; i++){
matc[i*matsize+j] += mata[i*matsize+k] * matb[k*matsize+j];
}
}
}
}