forked from cirosantilli/cpp-cheat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc.c
107 lines (95 loc) · 2.19 KB
/
c.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
/*
* How to use blas and lapack with the standard
* interfaces provided by their respective projects, repectively through
* `cblas.h` and `lapacke.h`
*/
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <cblas.h>
#include <lapacke.h>
/* Assert two integers are equal.
* if not, print them to stderr and assert false
*/
void assert_eqi(int i1, int i2) {
if (i1 != i2) {
fprintf(stderr, "%d\n%d\n\n\n", i1, i2);
assert(false);
}
}
/* Assert two doubles are equal within err precision
* If not, print them to stderr
*/
void assert_eqd(double d1, double d2, double err) {
if (fabs(d1 - d2) > err) {
fprintf(stderr, "%f\n%f\n\n\n", d1, d2);
assert(false);
}
}
/* print an array of doubles to stderr */
void print_vecd(int n, double * v) {
int i;
for (i=0; i<n; i++) {
fprintf(stderr, "%.2f ", v[i]);
}
printf("\n");
}
/*
bool eq_vecd2(int n, double * v1, double * v2, double err)
{
if(fabs(d1 - d2) > err)
return false
return true;
}
void assert_eqvd(int n, double * v1, double * v2, double err){
if(eq_vecd(n, v1, v2, err) != true){
print_vecd(n, v1);
print_vecd(n, v2);
}
}
*/
int main(void) {
int info, ipiv2[2];
float err = 1e-6;
float x2[2], b2[2], c2[2];
float a2x2[2][2];
/* cblas */
{
x2[0] = 1.0;
x2[1] = -2.0;
assert_eqd(cblas_snrm2(2, x2, 1), sqrt(5.0), err);
}
/* lapacke */
{
/* sgesv
*
* Matrix vector multiply.
*/
{
a2x2[0][0] = 1.0;
a2x2[1][0] = 2.0;
a2x2[0][1] = 3.0;
a2x2[1][1] = 4.0;
b2[0] = 5.;
b2[1] = 11.;
info = LAPACKE_sgesv(
LAPACK_COL_MAJOR,
2,
1,
&a2x2[0][0],
2,
&ipiv2[0],
&b2[0],
2
);
c2[0] = 1.0;
c2[1] = 2.0;
assert_eqi(info, 0);
assert_eqd(b2[0], c2[0], err);
assert_eqd(b2[1], c2[1], err);
}
}
return EXIT_SUCCESS;
}