Skip to content

Commit b57712c

Browse files
committed
Matrix multiplication with cblas, block works now, reformat lapack cheat a bit
1 parent e4c287a commit b57712c

File tree

6 files changed

+180
-156
lines changed

6 files changed

+180
-156
lines changed

lapack/README.md

+16-12
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ BLAS and LAPACK are:
88

99
- de-facto standards
1010

11-
- non-parallel
11+
- non-parallel
1212

1313
- originally written in Fortran
1414

@@ -30,8 +30,6 @@ BLAS contains low level functions such as:
3030
- vector matrix multiplication
3131
- matrix matrix multiplication
3232

33-
The BLAS project provides `cblas.h`, which contains a C interface for BLAS.
34-
3533
### LAPACK
3634

3735
LAPACK contains higher level functions such as:
@@ -61,21 +59,19 @@ Implements full BLAS, but only part of LAPACK.
6159

6260
Has C interface.
6361

64-
## Installation on Ubuntu
65-
66-
### Fortran
67-
68-
sudo aptitude install liblapack-dev liblapack-doc libblas-doc
62+
## C interface
6963

70-
### C interface
64+
The BLAS project provides `cblas.h`, which contains a C interface for BLAS (TODO but also an implementation?)
7165

72-
via atlas:
66+
Via atlas:
7367

74-
sudo aptitude install
68+
sudo aptitude install libatlas-dev
69+
gcc -lcblas
7570

76-
via LAPACKE (`libblas-dev` already contains `cblas.h`):
71+
Via LAPACKE (`libblas-dev` already contains `cblas.h`):
7772

7873
sudo aptitude install liblapacke-dev
74+
gcc -lblas
7975

8076
## Levels
8177

@@ -85,6 +81,8 @@ via LAPACKE (`libblas-dev` already contains `cblas.h`):
8581

8682
## Function naming conventions
8783

84+
<http://www.netlib.org/lapack/lug/node24.html>
85+
8886
The functions are named according to the pattern:
8987

9088
XYYZZZ
@@ -108,6 +106,8 @@ Where:
108106
- `ZZ`: computation to be done:
109107

110108
- `SV`: SolVe linear system
109+
- `MM`: Matrix Multiply
110+
- `LS`: Least Squares (overdetermined system)
111111

112112
## Sources
113113

@@ -118,3 +118,7 @@ Where:
118118
- user's guide. algorithm info <http://www.netlib.org/lapack/lug/>
119119

120120
- <http://www.tat.physik.uni-tuebingen.de/~kley/lehre/ftn77/tutorial/blas.html>
121+
122+
## LAPACKE
123+
124+
<http://stackoverflow.com/questions/26875415/difference-between-lapacke-and-lapack>

lapack/c.c

+62-68
Original file line numberDiff line numberDiff line change
@@ -14,103 +14,97 @@ interfaces provided by their respective projects, repectively through
1414
#include <lapacke.h>
1515

1616
/**
17-
* assert two integers are equal
18-
* if not, print them to stderr and assert false
19-
*/
20-
void assert_eqi( int i1, int i2 )
21-
{
22-
if( i1 != i2 )
23-
{
24-
fprintf( stderr, "%d\n%d\n\n\n", i1, i2 );
25-
assert( false );
17+
* assert two integers are equal
18+
* if not, print them to stderr and assert false
19+
*/
20+
void assert_eqi(int i1, int i2) {
21+
if (i1 != i2) {
22+
fprintf(stderr, "%d\n%d\n\n\n", i1, i2);
23+
assert(false);
2624
}
2725
}
2826

29-
/*
30-
* assert two doubles are equal within err precision
31-
* if not, print them to stderr
32-
*/
33-
void assert_eqd( double d1, double d2, double err )
34-
{
35-
if( fabs( d1 - d2 ) > err )
36-
{
37-
fprintf( stderr, "%f\n%f\n\n\n", d1, d2 );
38-
assert( false );
27+
/**
28+
* assert two doubles are equal within err precision
29+
* if not, print them to stderr
30+
*/
31+
void assert_eqd(double d1, double d2, double err) {
32+
if (fabs(d1 - d2) > err) {
33+
fprintf(stderr, "%f\n%f\n\n\n", d1, d2);
34+
assert(false);
3935
}
4036
}
4137

42-
/*
43-
* print an array of doubles to stderr
44-
*/
45-
void print_vecd( int n, double * v )
46-
{
38+
/** print an array of doubles to stderr */
39+
void print_vecd(int n, double * v) {
4740
int i;
48-
for(i=0; i<n; i++)
49-
{
50-
fprintf( stderr, "%.2f ", v[i] );
41+
for (i=0; i<n; i++) {
42+
fprintf(stderr, "%.2f ", v[i]);
5143
}
5244
printf("\n");
5345
}
5446

5547
/*
56-
bool eq_vecd2( int n, double * v1, double * v2, double err)
48+
bool eq_vecd2(int n, double * v1, double * v2, double err)
5749
{
58-
if( fabs( d1 - d2 ) > err )
50+
if(fabs(d1 - d2) > err)
5951
return false
6052
return true;
6153
}
6254
63-
void assert_eqvd( int n, double * v1, double * v2, double err){
64-
if( eq_vecd( n, v1, v2, err ) != true ){
65-
print_vecd( n, v1 );
66-
print_vecd( n, v2 );
55+
void assert_eqvd(int n, double * v1, double * v2, double err){
56+
if(eq_vecd(n, v1, v2, err) != true){
57+
print_vecd(n, v1);
58+
print_vecd(n, v2);
6759
}
6860
}
6961
*/
7062

71-
int main(void)
72-
{
63+
int main(void) {
7364
int info, ipiv2[2];
7465
float err = 1e-6;
7566
float x2[2], b2[2], c2[2];
7667
float a2x2[2][2];
7768

78-
//#cblas
79-
69+
/* cblas */
70+
{
8071
x2[0] = 1.0;
8172
x2[1] = -2.0;
82-
assert_eqd( cblas_snrm2(2, x2, 1), sqrt(5.0), err );
83-
84-
//#lapacke
85-
86-
//1 2 x = 5
87-
//3 4 y 11
88-
89-
//x = 1
90-
//y = 1
91-
92-
a2x2[0][0] = 1.0;
93-
a2x2[1][0] = 2.0;
94-
a2x2[0][1] = 3.0;
95-
a2x2[1][1] = 4.0;
96-
b2[0] = 5.;
97-
b2[1] = 11.;
73+
assert_eqd(cblas_snrm2(2, x2, 1), sqrt(5.0), err);
74+
}
9875

99-
info = LAPACKE_sgesv(
100-
LAPACK_COL_MAJOR, //COL or ROW
101-
2, //n
102-
1, //nrhs
103-
&a2x2[0][0],
104-
2, //lda
105-
&ipiv2[0],
106-
&b2[0],
107-
2 //ldb
108-
);
109-
c2[0] = 1.0;
110-
c2[1] = 2.0;
111-
assert_eqi( info, 0 );
112-
assert_eqd( b2[0], c2[0], err );
113-
assert_eqd( b2[1], c2[1], err );
76+
/* lapacke */
77+
{
78+
/*
79+
sgesv
80+
81+
Matrix vector multiply.
82+
*/
83+
{
84+
a2x2[0][0] = 1.0;
85+
a2x2[1][0] = 2.0;
86+
a2x2[0][1] = 3.0;
87+
a2x2[1][1] = 4.0;
88+
b2[0] = 5.;
89+
b2[1] = 11.;
90+
91+
info = LAPACKE_sgesv(
92+
LAPACK_COL_MAJOR,
93+
2,
94+
1,
95+
&a2x2[0][0],
96+
2,
97+
&ipiv2[0],
98+
&b2[0],
99+
2
100+
);
101+
c2[0] = 1.0;
102+
c2[1] = 2.0;
103+
assert_eqi(info, 0);
104+
assert_eqd(b2[0], c2[0], err);
105+
assert_eqd(b2[1], c2[1], err);
106+
}
107+
}
114108

115109
return EXIT_SUCCESS;
116110
}

lapack/configure

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env bash
22
sudo aptitude update
33
# BLAS C/Fotran and LAPACK Fortran:
4-
sudo aptitude install -y liblapack-dev
4+
sudo aptitude install -y liblapack-dev liblapacke-dev
55
# Lapack C via LAPACKE:
66
# TODO: how to install on Ubuntu 12.04?
77
# The following works only on later Ubuntu

lapack/main.f

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ program main
171171
!return status returned on `info`:
172172
!pivots returned on `pivots`:
173173
call sgesv( n, nrhs, a2x2, lda, pivots, b2, ldb, info )
174-
174+
175175
c2(1) = 1.0
176176
c2(2) = 2.0
177177
call assert_eqi( info, 0 )

opencl/Makefile_params

+3-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
LIBS := -lm -pthread -lOpenCL
1+
LIBS := -lm -pthread -lOpenCL -lblas
2+
# blas from libatlas-base-dev
3+
# cblas from libblas-dev

0 commit comments

Comments
 (0)