forked from flame/blis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
06level1m.c
229 lines (171 loc) · 6.89 KB
/
06level1m.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
BLIS
An object-based framework for developing high-performance BLAS-like
libraries.
Copyright (C) 2014, The University of Texas at Austin
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
- Neither the name of The University of Texas nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include "blis.h"
int main( int argc, char** argv )
{
obj_t alpha, beta, gamma;
obj_t a, b, c, d, e, f, g, h;
num_t dt;
dim_t m, n;
inc_t rs, cs;
//
// This file demonstrates working with matrix objects and the level-1m
// operations.
//
//
// Example 1: Create matrix objects and then broadcast (copy) scalar
// values to all elements.
//
printf( "\n#\n# -- Example 1 --\n#\n\n" );
// Create a few matrices to work with. We make them all of the same
// dimensions so that we can perform operations between them.
dt = BLIS_DOUBLE;
m = 2; n = 3; rs = 0; cs = 0;
bli_obj_create( dt, m, n, rs, cs, &a );
bli_obj_create( dt, m, n, rs, cs, &b );
bli_obj_create( dt, m, n, rs, cs, &c );
bli_obj_create( dt, m, n, rs, cs, &d );
bli_obj_create( dt, m, n, rs, cs, &e );
// Let's also create and initialize some scalar objects.
bli_obj_create_1x1( dt, &alpha );
bli_obj_create_1x1( dt, &beta );
bli_obj_create_1x1( dt, &gamma );
bli_setsc( 2.0, 0.0, &alpha );
bli_setsc( 0.2, 0.0, &beta );
bli_setsc( 3.0, 0.0, &gamma );
bli_printm( "alpha:", &alpha, "%4.1f", "" );
bli_printm( "beta:", &beta, "%4.1f", "" );
bli_printm( "gamma:", &gamma, "%4.1f", "" );
// Matrices, like vectors, can set by "broadcasting" a constant to every
// element.
bli_setm( &BLIS_ONE, &a );
bli_setm( &alpha, &b );
bli_setm( &BLIS_ZERO, &c );
bli_printm( "a := 1.0", &a, "%4.1f", "" );
bli_printm( "b := alpha", &b, "%4.1f", "" );
bli_printm( "c := 0.0", &c, "%4.1f", "" );
//
// Example 2: Randomize a matrix object.
//
printf( "\n#\n# -- Example 2 --\n#\n\n" );
// Set a matrix to random values.
bli_randm( &e );
bli_printm( "e (randomized):", &e, "%4.1f", "" );
//
// Example 3: Perform element-wise operations on matrices.
//
printf( "\n#\n# -- Example 3 --\n#\n\n" );
// Copy a matrix.
bli_copym( &e, &d );
bli_printm( "d := e", &d, "%4.1f", "" );
// Add and subtract vectors.
bli_addm( &a, &d );
bli_printm( "d := d + a", &d, "%4.1f", "" );
bli_subm( &a, &e );
bli_printm( "e := e - a", &e, "%4.1f", "" );
// Scale a matrix (destructive).
bli_scalm( &alpha, &e );
bli_printm( "e := alpha * e", &e, "%4.1f", "" );
// Scale a matrix (non-destructive).
bli_scal2m( &beta, &e, &c );
bli_printm( "c := beta * e", &c, "%4.1f", "" );
// Scale and accumulate between matrices.
bli_axpym( &alpha, &a, &c );
bli_printm( "c := c + alpha * a", &c, "%4.1f", "" );
//
// Example 4: Copy and transpose a matrix.
//
printf( "\n#\n# -- Example 4 --\n#\n\n" );
// Create an n-by-m matrix into which we can copy-transpose an m-by-n
// matrix.
bli_obj_create( dt, n, m, rs, cs, &f );
// Initialize all of 'f' to -1.0 to simulate junk values.
bli_setm( &BLIS_MINUS_ONE, &f );
bli_printm( "e:", &e, "%4.1f", "" );
bli_printm( "f (initial value):", &f, "%4.1f", "" );
// Since we are going to copy 'e' to 'f', we need to indicate a transpose
// on 'e', the input operand. Transposition can be indicated by setting a
// bit in the object. Since it always starts out as "no transpose", we can
// simply toggle the bit.
bli_obj_toggle_trans( &e );
// Another way to mark and object for transposition is to set it directly.
//bli_obj_set_onlytrans( BLIS_TRANSPOSE, &e );
// A third way is to "apply" a transposition. This is equivalent to toggling
// the transposition when the value being applied is BLIS_TRANSPOSE. If
// the value applied is BLIS_NO_TRANSPOSE, the transposition bit in the
// targeted object is unaffected. (Applying transposes is more useful in
// practice when the 'trans' argument is a variable and not a constant
// literal.)
//bli_obj_apply_trans( BLIS_TRANSPOSE, &e );
//bli_obj_apply_trans( BLIS_NO_TRANSPOSE, &e );
//bli_obj_apply_trans( trans, &e );
// Copy 'e' to 'f', transposing 'e' in the process. Notice that we haven't
// modified any properties of 'd'. It's the source operand that matters
// when marking an operand for transposition, not the destination.
bli_copym( &e, &f );
bli_printm( "f (copied value):", &f, "%4.1f", "" );
//
// Example 5: Copy and Hermitian-transpose a matrix.
//
printf( "\n#\n# -- Example 5 --\n#\n\n" );
// Create an n-by-m complex matrix into which we can Hermitian-transpose
// (or, conjugate-transpose) another complex (m-by-n) matrix.
dt = BLIS_DCOMPLEX;
bli_obj_create( dt, m, n, rs, cs, &g );
bli_obj_create( dt, n, m, rs, cs, &h );
// Randomize 'g', the input operand.
bli_randm( &g );
// Initialize all of 'h' to -1.0 to simulate junk values.
bli_setm( &BLIS_MINUS_ONE, &h );
bli_printm( "g:", &g, "%4.1f", "" );
bli_printm( "h (initial value):", &h, "%4.1f", "" );
// Set both the transpose and conjugation bits.
bli_obj_toggle_trans( &g );
bli_obj_toggle_conj( &g );
// Copy 'g' to 'h', conjugating and transposing 'g' in the process.
// Once again, notice that it's the source operand that we've marked for
// conjugation.
bli_copym( &g, &h );
bli_printm( "h (copied value):", &h, "%4.1f", "" );
// Free the objects.
bli_obj_free( &alpha );
bli_obj_free( &beta );
bli_obj_free( &gamma );
bli_obj_free( &a );
bli_obj_free( &b );
bli_obj_free( &c );
bli_obj_free( &d );
bli_obj_free( &e );
bli_obj_free( &f );
bli_obj_free( &g );
bli_obj_free( &h );
return 0;
}
// -----------------------------------------------------------------------------