Added object API example code.

Details:
- Added an 'examples' directory at the top level.
- Added an 'oapi' subdirectory in 'examples' that contains a tutorial-like
  sequence of example code demostrating the core functionality of BLIS's
  object-based API, along with a Makefile and README. Thanks to Victor
  Eijkhout for being the first to suggest including such code in BLIS.
This commit is contained in:
Field G. Van Zee
2018-04-24 18:48:09 -05:00
parent d6ab25a323
commit 4d97574e47
11 changed files with 2331 additions and 0 deletions

235
examples/oapi/0obj_basic.c Normal file
View File

@@ -0,0 +1,235 @@
/*
BLIS
An object-based framework for developing high-performance BLAS-like
libraries.
Copyright (C) 2014, The University of Texas
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 a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11;
obj_t v1, v2;
num_t dt;
dim_t m, n;
inc_t rs, cs;
//
// This file demonstrates the basics of creating objects in BLIS,
// inspecting their basic properties, and printing matrix objects.
//
//
// Example 1: Create an object containing a 4x3 matrix of double-
// precision real elements stored in column-major order.
//
// The matrix dimensions are m = 4 and n = 3. We choose to use column
// storage (often called column-major storage) and thus we specify
// that the row stride ("rs" for short) argument is 1 and the column
// stride ("cs" for short) argument is equal to m = 4. In column
// storage, cs is known as the leading dimension.
dt = BLIS_DOUBLE; m = 4; n = 3;
rs = 1; cs = 4;
bli_obj_create( dt, m, n, rs, cs, &a1 );
// If cs is greater than m, then extra rows (in this case, two) will
// be allocated beyond the lower edge of the matrix. Sometimes this
// is desireable for alignment purposes.
dt = BLIS_DOUBLE; m = 4; n = 3;
rs = 1; cs = 6;
bli_obj_create( dt, m, n, rs, cs, &a2 );
//
// Example 2: Create an object containing a 4x3 matrix of double-
// precision real elements stored in row-major order.
//
// Here, we choose to use row storage (often called row-major storage)
// and thus we specify that the cs is 1 and rs is equal to n = 3. In
// row storage, the leading dimension corresponds to rs.
dt = BLIS_DOUBLE; m = 4; n = 3;
rs = 3; cs = 1;
bli_obj_create( dt, m, n, rs, cs, &a3 );
// As with the second example, we can cause extra columns (in this
// case, five) to be allocated beyond the right edge of the matrix.
dt = BLIS_DOUBLE; m = 4; n = 3;
rs = 8; cs = 1;
bli_obj_create( dt, m, n, rs, cs, &a4 );
//
// Example 3: Create objects using other floating-point datatypes.
//
// Examples of using the other floating-point datatypes.
m = 4; n = 3;
rs = 1; cs = 4;
bli_obj_create( BLIS_FLOAT, m, n, rs, cs, &a5 );
bli_obj_create( BLIS_SCOMPLEX, m, n, rs, cs, &a6 );
bli_obj_create( BLIS_DCOMPLEX, m, n, rs, cs, &a7 );
//
// Example 4: Create objects using default (column) storage so that
// we avoid having to specify rs and cs manually.
//
// Specifying the row and column strides as zero, as is done here, is
// a shorthand request for the default storage scheme, which is
// currently (and always has been) column storage. When requesting the
// default storage scheme with rs = cs = 0, BLIS may insert additional
// padding for alignment purposes. So, the 3x8 matrix object created
// below may end up having a row stride that is greater than 3. When
// in doubt, query the value!
bli_obj_create( BLIS_FLOAT, 3, 5, 0, 0, &a8 );
//
// Example 5: Inspect object fields after creation to expose
// possible alignment/padding.
//
printf( "\n#\n# -- Example 5 --\n#\n\n" );
// Let's inspect the amount of padding inserted for alignment. Note
// the difference between the m dimension and the column stride.
printf( "datatype %s\n", bli_datatype_string( bli_obj_datatype( a8 ) ) );
printf( "datatype size %d bytes\n", bli_datatype_size( bli_obj_datatype( a8 ) ) );
printf( "m dim (# of rows): %d\n", ( int )bli_obj_length( a8 ) );
printf( "n dim (# of cols): %d\n", ( int )bli_obj_width( a8 ) );
printf( "row stride: %d\n", ( int )bli_obj_row_stride( a8 ) );
printf( "col stride: %d\n", ( int )bli_obj_col_stride( a8 ) );
//
// Example 6: Inspect object fields after creation of other floating-
// point datatypes.
//
printf( "\n#\n# -- Example 6 --\n#\n\n" );
bli_obj_create( BLIS_DOUBLE, 3, 5, 0, 0, &a9 );
bli_obj_create( BLIS_SCOMPLEX, 3, 5, 0, 0, &a10);
bli_obj_create( BLIS_DCOMPLEX, 3, 5, 0, 0, &a11 );
printf( "datatype %s\n", bli_datatype_string( bli_obj_datatype( a9 ) ) );
printf( "datatype size %d bytes\n", bli_datatype_size( bli_obj_datatype( a9 ) ) );
printf( "m dim (# of rows): %d\n", ( int )bli_obj_length( a9 ) );
printf( "n dim (# of cols): %d\n", ( int )bli_obj_width( a9 ) );
printf( "row stride: %d\n", ( int )bli_obj_row_stride( a9 ) );
printf( "col stride: %d\n", ( int )bli_obj_col_stride( a9 ) );
printf( "\n" );
printf( "datatype %s\n", bli_datatype_string( bli_obj_datatype( a10 ) ) );
printf( "datatype size %d bytes\n", bli_datatype_size( bli_obj_datatype( a10 ) ) );
printf( "m dim (# of rows): %d\n", ( int )bli_obj_length( a10 ) );
printf( "n dim (# of cols): %d\n", ( int )bli_obj_width( a10 ) );
printf( "row stride: %d\n", ( int )bli_obj_row_stride( a10 ) );
printf( "col stride: %d\n", ( int )bli_obj_col_stride( a10 ) );
printf( "\n" );
printf( "datatype %s\n", bli_datatype_string( bli_obj_datatype( a11 ) ) );
printf( "datatype size %d bytes\n", bli_datatype_size( bli_obj_datatype( a11 ) ) );
printf( "m dim (# of rows): %d\n", ( int )bli_obj_length( a11 ) );
printf( "n dim (# of cols): %d\n", ( int )bli_obj_width( a11 ) );
printf( "row stride: %d\n", ( int )bli_obj_row_stride( a11 ) );
printf( "col stride: %d\n", ( int )bli_obj_col_stride( a11 ) );
//
// Example 7: Initialize an object's elements to random values and then
// print the matrix.
//
printf( "\n#\n# -- Example 7 --\n#\n\n" );
// We can set matrices to random values. The default behavior of
// bli_randm() is to use random values on the internval [-1,1].
bli_randm( &a9 );
// And we can also print the matrices associated with matrix objects.
// Notice that the third argument is a printf()-style format specifier.
// Any valid printf() format specifier can be passed in here, but you
// still need to make sure that the specifier makes sense for the data
// being printed. For example, you shouldn't use "%d" when printing
// elements of type 'float'.
bli_printm( "matrix 'a9' contents:", &a9, "%4.1f", "" );
//
// Example 8: Randomize and then print from an object containing a complex
// matrix.
//
printf( "\n#\n# -- Example 8 --\n#\n\n" );
// When printing complex matrices, the same format specifier gets used
// for both the real and imaginary parts.
bli_randm( &a11 );
bli_printm( "matrix 'a11' contents (complex):", &a11, "%4.1f", "" );
//
// Example 9: Create, randomize, and print vector objects.
//
printf( "\n#\n# -- Example 9 --\n#\n\n" );
// Now let's create two vector object--a row vector and a column vector.
// (A vector object is like a matrix object, except that it has at least
// one unit dimension (equal to one).
bli_obj_create( BLIS_DOUBLE, 4, 1, 0, 0, &v1 );
bli_obj_create( BLIS_DOUBLE, 1, 6, 0, 0, &v2 );
// If we know the object is a vector, we can use bli_randv(), though
// bli_randm() would work just as well, since any vector is also a matrix.
bli_randv( &v1 );
bli_randv( &v2 );
// We can print vectors, too.
bli_printm( "vector 'v1' contents:", &v1, "%5.1f", "" );
bli_printm( "vector 'v2' contents:", &v2, "%5.1f", "" );
// Free all of the objects we created.
bli_obj_free( &a1 );
bli_obj_free( &a2 );
bli_obj_free( &a3 );
bli_obj_free( &a4 );
bli_obj_free( &a5 );
bli_obj_free( &a6 );
bli_obj_free( &a7 );
bli_obj_free( &a8 );
bli_obj_free( &a9 );
bli_obj_free( &a10 );
bli_obj_free( &a11 );
bli_obj_free( &v1 );
bli_obj_free( &v2 );
return 0;
}

173
examples/oapi/1obj_attach.c Normal file
View File

@@ -0,0 +1,173 @@
/*
BLIS
An object-based framework for developing high-performance BLAS-like
libraries.
Copyright (C) 2014, The University of Texas
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 <stdlib.h>
#include "blis.h"
void init_dmatrix_by_rows( dim_t m, dim_t n, double* a, inc_t rs, inc_t cs );
void init_dmatrix_by_cols( dim_t m, dim_t n, double* a, inc_t rs, inc_t cs );
int main( int argc, char** argv )
{
obj_t a1, a2;
num_t dt;
dim_t m, n;
inc_t rs, cs;
//
// This file demonstrates interfacing external or existing buffers
// with BLIS objects.
//
//
// Example 1: Create a bufferless object and then attach an external
// buffer to it, specifying column storage.
//
printf( "\n#\n# -- Example 1 --\n#\n\n" );
// We'll use these parameters for the following examples.
dt = BLIS_DOUBLE;
m = 4; n = 5; rs = 1; cs = m;
// First we allocate and initialize a matrix by columns.
double* p1 = malloc( m * n * sizeof( double ) );
init_dmatrix_by_cols( m, n, p1, rs, cs );
// bli_obj_create() automatically allocates an array large enough to hold
// of the elements. We can also create a "bufferless" object and then
// "attach" our own buffer to that object. This is useful when interfacing
// BLIS objects to an existing application that produces its own matrix
// arrays/buffers.
bli_obj_create_without_buffer( dt, m, n, &a1 );
// Note that the fourth argument of bli_obj_attach_buffer() is the so-called
// "imaginary stride". First of all, this stride only has meaning in the
// complex domain. Secondly, it is a somewhat experimental property of the
// obj_t, and one that is not fully recognized/utilized throughout BLIS.
// Thus, the safe thing to do is to always pass in a 0, which is a request
// for the default (which is actually 1). Please don't use any other value
// unless you really know what you are doing.
bli_obj_attach_buffer( p1, rs, cs, 0, &a1 );
// Now let's print the matrix so we can see how the element values were
// assigned.
bli_printm( "matrix 'a1', initialized by columns:", &a1, "%5.1f", "" );
//
// Example 2: Create a bufferless object and then attach an external
// buffer to it, specifying row storage.
//
printf( "\n#\n# -- Example 2 --\n#\n\n" );
// Now let's allocate another buffer, but this time we'll initialize it by
// rows instead of by columns. We'll use the same values for m, n, rs, cs.
double* p2 = malloc( m * n * sizeof( double ) );
init_dmatrix_by_rows( m, n, p2, rs, cs );
// Create a new bufferless object and attach the new buffer. This time,
// instead of calling bli_obj_create_without_buffer() followed by
// bli_obj_attach_buffer(), we call bli_obj_create_with_attached_buffer(),
// which is just a convenience wrapper around the former two functions.
// (Note that the wrapper function omits the imaginary stride argument.)
#if 1
bli_obj_create_with_attached_buffer( dt, m, n, p2, rs, cs, &a2 );
#else
bli_obj_create_without_buffer( dt, m, n, &a2 );
bli_obj_attach_buffer( p2, rs, cs, 0, &a2 );
#endif
// Print the matrix so we can compare it to the first matrix output.
bli_printm( "matrix 'a2', initialized by rows:", &a2, "%5.1f", "" );
// Please note that after creating an object via either of:
// - bli_obj_create_without_buffer(), or
// - bli_obj_create_with_attached_buffer()
// we do NOT free it! That's because these functions merely initialize the
// object and do not actually allocate any memory.
// Free the memory arrays we allocated.
free( p1 );
free( p2 );
return 0;
}
// -----------------------------------------------------------------------------
void init_dmatrix_by_rows( dim_t m, dim_t n, double* a, inc_t rs, inc_t cs )
{
dim_t i, j;
double alpha = 0.0;
// Step through a matrix by rows, assigning each element a unique
// value, starting at 0.
for ( i = 0; i < m; ++i )
{
for ( j = 0; j < n; ++j )
{
double* a_ij = a + i*rs + j*cs;
*a_ij = alpha;
alpha += 1.0;
}
}
}
void init_dmatrix_by_cols( dim_t m, dim_t n, double* a, inc_t rs, inc_t cs )
{
dim_t i, j;
double alpha = 0.0;
// Step through a matrix by columns, assigning each element a unique
// value, starting at 0.
for ( j = 0; j < n; ++j )
{
for ( i = 0; i < m; ++i )
{
double* a_ij = a + i*rs + j*cs;
*a_ij = alpha;
alpha += 1.0;
}
}
}

266
examples/oapi/2obj_ij.c Normal file
View File

@@ -0,0 +1,266 @@
/*
BLIS
An object-based framework for developing high-performance BLAS-like
libraries.
Copyright (C) 2014, The University of Texas
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 <stdlib.h>
#include "blis.h"
void init_dmatrix_by_rows( dim_t m, dim_t n, double* a, inc_t rs, inc_t cs );
void init_dmatrix_by_cols( dim_t m, dim_t n, double* a, inc_t rs, inc_t cs );
void init_dobj_by_cols( obj_t* a );
void init_zobj_by_cols( obj_t* a );
int main( int argc, char** argv )
{
obj_t a1, a2, a3;
num_t dt;
dim_t m, n;
inc_t rs, cs;
dim_t i, j;
//
// This file demonstrates accessing and updating individual matrix elements
// through the BLIS object API.
//
//
// Example 1: Create an object and then individually access/view some of
// its elements.
//
printf( "\n#\n# -- Example 1 --\n#\n\n" );
// We'll use these parameters for the following examples.
dt = BLIS_DOUBLE;
m = 4; n = 5; rs = 1; cs = m;
// Create a object with known elements using the same approach as the
// previous example file.
double* p1 = malloc( m * n * sizeof( double ) );
init_dmatrix_by_cols( m, n, p1, rs, cs );
bli_obj_create_with_attached_buffer( dt, m, n, p1, rs, cs, &a1 );
bli_printm( "matrix 'a1' (initial state)", &a1, "%5.1f", "" );
// Regardless of how we create our object--whether via bli_obj_create() or
// via attaching an existing buffer to a bufferless object--we can access
// individual elements by specifying their offsets. The output value is
// broken up by real and imaginary component. (When accessing real matrices,
// the imaginary component will always be zero.)
i = 1; j = 3;
double alpha_r, alpha_i;
bli_getijm( i, j, &a1, &alpha_r, &alpha_i );
// Here, we print out the element "returned" by bli_getijm().
printf( "element (%2d,%2d) of matrix 'a1' (real + imag): %5.1f + %5.1f\n", i, j, alpha_r, alpha_i );
// Let's query a few more elements.
i = 0; j = 2;
bli_getijm( i, j, &a1, &alpha_r, &alpha_i );
printf( "element (%2d,%2d) of matrix 'a1' (real + imag): %5.1f + %5.1f\n", i, j, alpha_r, alpha_i );
i = 3; j = 4;
bli_getijm( i, j, &a1, &alpha_r, &alpha_i );
printf( "element (%2d,%2d) of matrix 'a1' (real + imag): %5.1f + %5.1f\n", i, j, alpha_r, alpha_i );
printf( "\n" );
//
// Example 2: Modify individual elements of an existing matrix.
//
printf( "\n#\n# -- Example 2 --\n#\n\n" );
// Now let's change a few elements. Even if we set the imaginary
// argument to a non-zero value, argument is ignored since we're
// modifying a real matrix. If a1 were a complex object, those
// values would be stored verbatim into the appropriate matrix
// elements (see example for a3 below).
alpha_r = -3.0; alpha_i = 0.0; i = 1; j = 3;
bli_setijm( alpha_r, alpha_i, i, j, &a1 );
alpha_r = -9.0; alpha_i = -1.0; i = 0; j = 2;
bli_setijm( alpha_r, alpha_i, i, j, &a1 );
alpha_r = -7.0; alpha_i = 2.0; i = 3; j = 4;
bli_setijm( alpha_r, alpha_i, i, j, &a1 );
// Print the matrix again so we can see the update elements.
bli_printm( "matrix 'a1' (modified state)", &a1, "%5.1f", "" );
// Next, let's create a regular object (with a buffer) and then
// initialize its elements using bli_setijm().
bli_obj_create( dt, m, n, rs, cs, &a2 );
// See definition of init_dobj_by_cols() below.
init_dobj_by_cols( &a2 );
// Because we initialized a2 in the same manner as a1 (by columns),
// it should contain the same initial state as a1.
bli_printm( "matrix 'a2'", &a2, "%5.1f", "" );
//
// Example 3: Modify individual elements of an existing complex matrix.
//
printf( "\n#\n# -- Example 3 --\n#\n\n" );
// Let's create and initialize a complex object.
dt = BLIS_DCOMPLEX;
bli_obj_create( dt, m, n, rs, cs, &a3 );
// Initialize the matrix elements. (See definition of init_dobj_by_cols()
// below).
init_zobj_by_cols( &a3 );
// Print the complex matrix.
bli_printm( "matrix 'a3' (initial state)", &a3, "%5.1f", "" );
i = 3; j = 0;
bli_getijm( i, j, &a3, &alpha_r, &alpha_i );
alpha_r *= -1.0; alpha_i *= -1.0;
bli_setijm( alpha_r, alpha_i, i, j, &a3 );
i = 3; j = 4;
bli_getijm( i, j, &a3, &alpha_r, &alpha_i );
alpha_r *= -1.0; alpha_i *= -1.0;
bli_setijm( alpha_r, alpha_i, i, j, &a3 );
i = 0; j = 4;
bli_getijm( i, j, &a3, &alpha_r, &alpha_i );
alpha_r *= -1.0; alpha_i *= -1.0;
bli_setijm( alpha_r, alpha_i, i, j, &a3 );
// Print the matrix again so we can see the update elements.
bli_printm( "matrix 'a3' (modified state)", &a3, "%5.1f", "" );
// Free the memory arrays we allocated.
free( p1 );
// Free the objects we created.
bli_obj_free( &a2 );
bli_obj_free( &a3 );
return 0;
}
// -----------------------------------------------------------------------------
void init_dmatrix_by_rows( dim_t m, dim_t n, double* a, inc_t rs, inc_t cs )
{
dim_t i, j;
double alpha = 0.0;
// Step through a matrix by rows, assigning each element a unique
// value, starting at 0.
for ( i = 0; i < m; ++i )
{
for ( j = 0; j < n; ++j )
{
double* a_ij = a + i*rs + j*cs;
*a_ij = alpha;
alpha += 1.0;
}
}
}
void init_dmatrix_by_cols( dim_t m, dim_t n, double* a, inc_t rs, inc_t cs )
{
dim_t i, j;
double alpha = 0.0;
// Step through a matrix by columns, assigning each element a unique
// value, starting at 0.
for ( j = 0; j < n; ++j )
{
for ( i = 0; i < m; ++i )
{
double* a_ij = a + i*rs + j*cs;
*a_ij = alpha;
alpha += 1.0;
}
}
}
void init_dobj_by_cols( obj_t* a )
{
dim_t m = bli_obj_length( *a );
dim_t n = bli_obj_width( *a );
dim_t i, j;
double alpha = 0.0;
// Step through a matrix by columns, assigning each element a unique
// value, starting at 0.
for ( j = 0; j < n; ++j )
{
for ( i = 0; i < m; ++i )
{
bli_setijm( alpha, 0.0, i, j, a );
alpha += 1.0;
}
}
}
void init_zobj_by_cols( obj_t* a )
{
dim_t m = bli_obj_length( *a );
dim_t n = bli_obj_width( *a );
dim_t i, j;
double alpha = 0.0;
// Step through a matrix by columns, assigning each real and imaginary
// element a unique value, starting at 0.
for ( j = 0; j < n; ++j )
{
for ( i = 0; i < m; ++i )
{
bli_setijm( alpha, alpha + 1.0, i, j, a );
alpha += 2.0;
}
}
}

176
examples/oapi/3level0.c Normal file
View File

@@ -0,0 +1,176 @@
/*
BLIS
An object-based framework for developing high-performance BLAS-like
libraries.
Copyright (C) 2014, The University of Texas
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, kappa, zeta;
num_t dt;
double gamma_d;
//
// This file demonstrates working with scalar objects.
//
//
// Example 1: Create a scalar (1x1) object.
//
dt = BLIS_DOUBLE;
// The easiest way to create a scalar object is with the following
// convenience function.
bli_obj_create_1x1( dt, &alpha );
// We could, of course, create an object using our more general-purpose
// function, using m = n = 1.
bli_obj_create( dt, 1, 1, 0, 0, &beta );
// We can even attach an external scalar. This function, unlike
// bli_obj_create_1x1() and bli_obj_create(), does not result in any
// memory allocation.
bli_obj_create_1x1_with_attached_buffer( dt, &gamma_d, &gamma );
// There is one more way to create an object. Like the previous method,
// it also avoids memory allocation by referencing a special "internal"
// scalar that is invisibly part of every object.
bli_obj_scalar_init_detached( dt, &kappa );
// Digression: In the most common cases, there is no need to create scalar
// objects to begin with. That's because BLIS comes with three ready-to-use
// globally-scoped scalar objects:
//
// obj_t BLIS_MINUS_ONE;
// obj_t BLIS_ZERO;
// obj_t BLIS ONE;
//
// Each of these special objects is provided by blis.h. They can be used
// wherever a scalar object is expected as an input operand regardless of
// the datatype of your other operands. Note that you should never try to
// modify these global scalar objects directly, nor should you ever try to
// perform an operation *on* the objects (that is, you should never try to
// update their values, though you can always perform operations *with*
// them--that's the whole point!).
//
// Example 2: Set the value of an existing scalar object.
//
printf( "\n#\n# -- Example 2 --\n#\n\n" );
// Once you've created an object, you can set its value via setsc. As with
// setijm, setsc takes a real and imaginary value, but you can ignore the
// imaginary argument if your object is real. And even if you pass in a
// non-zero value, it is ignored for real objects.
bli_setsc( -4.0, 0.0, &alpha );
bli_setsc( 3.0, 1.0, &beta );
bli_setsc( 0.5, 0.0, &kappa );
bli_setsc( 10.0, 0.0, &gamma );
// BLIS does not have a special print function for scalars, but since a
// 1x1 is also a vector and a matrix, we can use printv or printm.
bli_printm( "alpha:", &alpha, "%4.1f", "" );
bli_printm( "beta:", &beta, "%4.1f", "" );
bli_printm( "kappa:", &kappa, "%4.1f", "" );
bli_printm( "gamma:", &gamma, "%4.1f", "" );
//
// Example 3: Create and set the value of a complex scalar object.
//
printf( "\n#\n# -- Example 3 --\n#\n\n" );
// Let's create one more scalar, this time a complex scalar, to show
// how it can be used.
bli_obj_create_1x1( BLIS_DCOMPLEX, &zeta );
bli_setsc( 3.3, -4.4, &zeta );
bli_printm( "zeta (complex):", &zeta, "%4.1f", "" );
//
// Example 4: Copy scalar objects.
//
printf( "\n#\n# -- Example 4 --\n#\n\n" );
// We can copy scalars amongst one another, and we can use the global
// scalar constants for input operands.
bli_copysc( &beta, &gamma );
bli_printm( "gamma (overwritten with beta):", &gamma, "%4.1f", "" );
bli_copysc( &BLIS_ONE, &gamma );
bli_printm( "gamma (overwritten with BLIS_ONE):", &gamma, "%4.1f", "" );
//
// Example 5: Perform other operations on scalar objects.
//
printf( "\n#\n# -- Example 5 --\n#\n\n" );
// BLIS defines a range of basic floating-point operations on scalars.
bli_addsc( &beta, &gamma );
bli_printm( "gamma := gamma + beta", &gamma, "%4.1f", "" );
bli_subsc( &alpha, &gamma );
bli_printm( "gamma := gamma - alpha", &gamma, "%4.1f", "" );
bli_divsc( &kappa, &gamma );
bli_printm( "gamma := gamma / kappa", &gamma, "%4.1f", "" );
bli_sqrtsc( &gamma, &gamma );
bli_printm( "gamma := sqrt( gamma )", &gamma, "%4.1f", "" );
bli_normfsc( &alpha, &alpha );
bli_printm( "alpha := normf( alpha ) # normf() = abs() in real domain.", &alpha, "%4.1f", "" );
// Note that normfsc() allows complex input objects, but requires that the
// output operand (the second operand) be a real object.
bli_normfsc( &zeta, &alpha );
bli_printm( "alpha := normf( zeta ) # normf() = complex modulus in complex domain.", &alpha, "%4.1f", "" );
bli_invertsc( &gamma );
bli_printm( "gamma := 1.0 / gamma", &gamma, "%4.2f", "" );
// Only free the objects that resulted in actual allocation.
bli_obj_free( &alpha );
bli_obj_free( &beta );
bli_obj_free( &zeta );
return 0;
}
// -----------------------------------------------------------------------------

179
examples/oapi/4level1v.c Normal file
View File

@@ -0,0 +1,179 @@
/*
BLIS
An object-based framework for developing high-performance BLAS-like
libraries.
Copyright (C) 2014, The University of Texas
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 x, y, z, w, a;
num_t dt;
dim_t m, n;
inc_t rs, cs;
//
// This file demonstrates working with vector objects and the level-1v
// operations.
//
//
// Example 1: Create vector objects and then broadcast (copy) scalar
// values to all elements.
//
printf( "\n#\n# -- Example 1 --\n#\n\n" );
// Let's create a few vectors to work with. We make them all of the
// same length so that we can perform operations between them.
// NOTE: We've chosen to use row vectors here (1x4) instead of column
// vectors (4x1) to allow for easier reading of standard output (less
// scrolling).
dt = BLIS_DOUBLE;
m = 1; n = 4; rs = 0; cs = 0;
bli_obj_create( dt, m, n, rs, cs, &x );
bli_obj_create( dt, m, n, rs, cs, &y );
bli_obj_create( dt, m, n, rs, cs, &z );
bli_obj_create( dt, m, n, rs, cs, &w );
bli_obj_create( dt, m, n, rs, cs, &a );
// 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", "" );
// Vectors can set by "broadcasting" a constant to every element.
bli_setv( &BLIS_ONE, &x );
bli_setv( &alpha, &y );
bli_setv( &BLIS_ZERO, &z );
// Note that we can use printv or printm to print vectors since vectors
// are also matrices. We choose to use printm because it honors the
// orientation of the vector (row or column) when printing, whereas
// printv always prints vectors as column vectors regardless of their
// they are 1 x n or n x 1.
bli_printm( "x := 1.0", &x, "%4.1f", "" );
bli_printm( "y := alpha", &y, "%4.1f", "" );
bli_printm( "z := 0.0", &z, "%4.1f", "" );
//
// Example 2: Randomize a vector object.
//
printf( "\n#\n# -- Example 2 --\n#\n\n" );
// Set a vector to random values.
bli_randv( &w );
bli_printm( "w := randv()", &w, "%4.1f", "" );
//
// Example 3: Perform various element-wise operations on vector objects.
//
printf( "\n#\n# -- Example 3 --\n#\n\n" );
// Copy a vector.
bli_copyv( &w, &a );
bli_printm( "a := w", &a, "%4.1f", "" );
// Add and subtract vectors.
bli_addv( &y, &a );
bli_printm( "a := a + y", &a, "%4.1f", "" );
bli_subv( &w, &a );
bli_printm( "a := a - w", &a, "%4.1f", "" );
// Scale a vector (destructive).
bli_scalv( &beta, &a );
bli_printm( "a := beta * a", &a, "%4.1f", "" );
// Scale a vector (non-destructive).
bli_scal2v( &gamma, &a, &z );
bli_printm( "z := gamma * a", &z, "%4.1f", "" );
// Scale and accumulate between vectors.
bli_axpyv( &alpha, &w, &x );
bli_printm( "x := x + alpha * w", &x, "%4.1f", "" );
bli_xpbyv( &w, &BLIS_MINUS_ONE, &x );
bli_printm( "x := -1.0 * x + w", &x, "%4.1f", "" );
// Invert a vector element-wise.
bli_invertv( &y );
bli_printm( "y := 1 / y", &y, "%4.1f", "" );
// Swap two vectors.
bli_swapv( &x, &y );
bli_printm( "x (after swapping with y)", &x, "%4.1f", "" );
bli_printm( "y (after swapping with x)", &y, "%4.1f", "" );
//
// Example 4: Perform contraction-like operations on vector objects.
//
printf( "\n#\n# -- Example 4 --\n#\n\n" );
// Perform a dot product.
bli_dotv( &a, &z, &gamma );
bli_printm( "gamma := a * z (dot product)", &gamma, "%5.2f", "" );
// Perform an extended dot product.
bli_dotxv( &alpha, &a, &z, &BLIS_ONE, &gamma );
bli_printm( "gamma := 1.0 * gamma + alpha * a * z (accumulate scaled dot product)", &gamma, "%5.2f", "" );
// Free the objects.
bli_obj_free( &alpha );
bli_obj_free( &beta );
bli_obj_free( &gamma );
bli_obj_free( &x );
bli_obj_free( &y );
bli_obj_free( &z );
bli_obj_free( &w );
bli_obj_free( &a );
return 0;
}
// -----------------------------------------------------------------------------

146
examples/oapi/5level1m.c Normal file
View File

@@ -0,0 +1,146 @@
/*
BLIS
An object-based framework for developing high-performance BLAS-like
libraries.
Copyright (C) 2014, The University of Texas
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;
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" );
// Let's 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 := randm()", &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", "" );
// 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 );
return 0;
}
// -----------------------------------------------------------------------------

View File

@@ -0,0 +1,341 @@
/*
BLIS
An object-based framework for developing high-performance BLAS-like
libraries.
Copyright (C) 2014, The University of Texas
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 )
{
num_t dt;
dim_t m, n;
inc_t rs, cs;
//
// This file demonstrates level-1m operations on structured matrices.
//
//
// Example 1: Initialize the upper triangle of a matrix to random values.
//
printf( "\n#\n# -- Example 1 --\n#\n\n" );
obj_t a;
// Let's create a matrix to work with.
dt = BLIS_DOUBLE;
m = 5; n = 5; rs = 0; cs = 0;
bli_obj_create( dt, m, n, rs, cs, &a );
// First, we mark the matrix structure as triangular.
bli_obj_set_struc( BLIS_TRIANGULAR, a )
// Next, we specify whether the lower part or the upper part is to be
// recognized as the "stored" region (which we call the uplo field). The
// strictly opposite part (in this case, the strictly lower region) will
// be *assumed* to be zero during computation. However, when printed out,
// the strictly lower part may contain junk values.
bli_obj_set_uplo( BLIS_UPPER, a );
// Now set the upper triangle to random values.
bli_randm( &a );
bli_printm( "a: randomize upper part (lower part may contain garbage)", &a, "%4.1f", "" );
//
// Example 2: Initialize the upper triangle of a matrix to random values
// but also explicitly set the strictly lower triangle to zero.
//
printf( "\n#\n# -- Example 2 --\n#\n\n" );
obj_t b, bl;
// Let's create a matrix to work with.
dt = BLIS_DOUBLE;
m = 5; n = 5; rs = 0; cs = 0;
bli_obj_create( dt, m, n, rs, cs, &b );
// Set structure and uplo.
bli_obj_set_struc( BLIS_TRIANGULAR, b )
bli_obj_set_uplo( BLIS_UPPER, b );
// Create an alias, 'bl', of the original object 'b'. Both objects will
// refer to the same underlying matrix elements, but now we will have two
// different "views" into the matrix. Aliases are simply "shallow copies"
// of the objects, meaning no additional memory allocation takes place.
// Therefore it is up to the API user (you) to make sure that you only
// free the original object (or exactly one of the aliases).
bli_obj_alias_to( b, bl );
// Digression: Each object contains a diagonal offset (even vectors),
// even if it is never needed. The diagonal offset for a newly-created
// object (ie: objects created via bli_obj_create*()) defaults to 0,
// meaning it intersects element (0,0), but it can be changed. When the
// diagonal offset delta is positive, the diagonal intersects element
// (0,delta). When the diagonal offset is negative, the diagonal
// intersects element (-delta,0). In other words, think of element (0,0)
// as the origin of a coordinate plane, with the diagonal being the
// x-axis value.
// Set the diagonal offset of 'bl' to -1.
bli_obj_set_diag_offset( -1, bl );
// Set the uplo field of 'bl' to "lower".
bli_obj_set_uplo( BLIS_LOWER, bl );
// Set the upper triangle of 'b' to random values.
bli_randm( &b );
// Set the strictly lower triangle of 'b' to zero (by setting the lower
// triangle of 'bl' to zero).
bli_setm( &BLIS_ZERO, &bl );
bli_printm( "b: randomize upper part; set strictly lower part to 0.0", &b, "%4.1f", "" );
// You may not see the effect of setting the strictly lower part to zero,
// since those values may already be zero (instead of random junk). So
// let's set it to something you'll notice, like -1.0.
bli_setm( &BLIS_MINUS_ONE, &bl );
bli_printm( "b: randomize upper part; set strictly lower part to -1.0", &b, "%4.1f", "" );
//
// Example 3: Copy the lower triangle of an existing object to a newly
// created (but otherwise uninitialized) object.
//
printf( "\n#\n# -- Example 3 --\n#\n\n" );
obj_t c;
// Let's create a matrix to work with.
dt = BLIS_DOUBLE;
m = 5; n = 5; rs = 0; cs = 0;
bli_obj_create( dt, m, n, rs, cs, &c );
// Reset the diagonal offset of 'bl' to 0.
bli_obj_set_diag_offset( 0, bl );
// Copy the lower triangle of matrix 'b' from Example 2 to object 'c'.
// This should give us -1.0 in the strictly lower part and some non-zero
// random values along the diagonal. Note that since 'c' is starting out
// uninitialized, the strictly upper part could contain junk.
bli_copym( &bl, &c );
bli_printm( "c: copy lower part of b (upper part may contain garbage)", &c, "%4.1f", "" );
// Notice that the structure and uplo properties of 'c' were set to their
// default values, BLIS_GENERAL and BLIS_DENSE, respectively. Thus, it is
// the structure and uplo of the *source* operand that controls what gets
// copied, regardless of the structure/uplo of the destination. To
// demonstrate this further, let's see what happens when we copy 'bl'
// (which is lower triangular) to 'a' (which is upper triangular).
bli_copym( &bl, &a );
// The result is that the lower part (diagonal and strictly lower part) is
// copied into 'a', but the elements in the strictly upper part of 'a' are
// unaffected. Note, however, that 'a' is still marked as upper triangular
// and so in future computations where 'a' is an input operand, the -1.0
// values that were copied from 'bl' into the lower triangle will be
// ignored. Generally speaking, level-1m operations on triangular matrices
// ignore the "unstored" regions of input operands because they are assumed
// to be zero).
bli_printm( "a: copy lower triangular bl to upper triangular a", &a, "%4.1f", "" );
//
// Example 4: Copy the lower triangle of an existing object into the
// upper triangle of an existing object.
//
printf( "\n#\n# -- Example 4 --\n#\n\n" );
obj_t d;
// Let's create a matrix to work with.
dt = BLIS_DOUBLE;
m = 5; n = 5; rs = 0; cs = 0;
bli_obj_create( dt, m, n, rs, cs, &d );
// Let's start by setting entire destination matrix to zero.
bli_setm( &BLIS_ZERO, &d );
bli_printm( "d: initial value (all zeros)", &d, "%4.1f", "" );
// Recall that 'bl' is marked as lower triangular with a diagonal offset
// of 0. Also recall that 'bl' is an alias of 'b', which is now fully
// initialized. But let's change a few values manually so we can later
// see the full effect of the transposition.
bli_setijm( 2.0, 0.0, 2, 0, &bl );
bli_setijm( 3.0, 0.0, 3, 0, &bl );
bli_setijm( 4.0, 0.0, 4, 0, &bl );
bli_setijm( 3.1, 0.0, 3, 1, &bl );
bli_setijm( 3.2, 0.0, 3, 2, &bl );
bli_printm( "bl: lower triangular bl is aliased to b", &bl, "%4.1f", "" );
// We want to pluck out the lower triangle and transpose it into the upper
// triangle of 'd'. 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( bl );
// Another way to mark and object for transposition is to set it directly.
//bli_obj_set_onlytrans( BLIS_TRANSPOSE, &bl );
// 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, &bl );
//bli_obj_apply_trans( BLIS_NO_TRANSPOSE, &bl );
//bli_obj_apply_trans( trans, &bl );
// Now we copy the transpose of the lower part of 'bl' into the upper
// part of 'd'. (Again, notice that we haven't modified any properties of
// 'd'. It's the source operand that matters, not the destination!)
bli_copym( &bl, &d );
bli_printm( "d: transpose of lower triangular of bl copied to d", &d, "%4.1f", "" );
//
// Example 5: Create a rectangular matrix (m > n) with a lower trapezoid
// containing random values, then set the strictly upper
// triangle to zeros.
//
printf( "\n#\n# -- Example 5 --\n#\n\n" );
obj_t e, el;
// Let's create a matrix to work with.
dt = BLIS_DOUBLE;
m = 6; n = 4; rs = 0; cs = 0;
bli_obj_create( dt, m, n, rs, cs, &e );
// Initialize the entire matrix to -1.0 to simulate junk values.
bli_setm( &BLIS_MINUS_ONE, &e );
bli_printm( "e: initial value (all -1.0)", &e, "%4.1f", "" );
// Create an alias to work with.
bli_obj_alias_to( e, el );
// Set structure and uplo of 'el'.
bli_obj_set_struc( BLIS_TRIANGULAR, el )
bli_obj_set_uplo( BLIS_LOWER, el );
// Digression: Notice that "triangular" structure does not require that
// the matrix be square. Rather, it simply means that either the part above
// or below the diagonal will be assumed to be zero.
// Randomize the lower trapezoid.
bli_randm( &el );
bli_printm( "e: after lower trapezoid randomized", &e, "%4.1f", "" );
// Move the diagonal offset of 'el' to 1 and flip the uplo field to
// "upper".
bli_obj_set_diag_offset( 1, el );
bli_obj_set_uplo( BLIS_UPPER, el );
// Set the upper triangle to zero.
bli_setm( &BLIS_ZERO, &el );
bli_printm( "e: after upper triangle set to zero", &e, "%4.1f", "" );
//
// Example 6: Create an upper Hessenberg matrix of random values and then
// set the "unstored" values to zero.
//
printf( "\n#\n# -- Example 6 --\n#\n\n" );
obj_t h, hl;
// Let's create a matrix to work with.
dt = BLIS_DOUBLE;
m = 5; n = 5; rs = 0; cs = 0;
bli_obj_create( dt, m, n, rs, cs, &h );
// Initialize the entire matrix to -1.0 to simulate junk values.
bli_setm( &BLIS_MINUS_ONE, &h );
bli_printm( "h: initial value (all -1.0)", &h, "%4.1f", "" );
// Set the diagonal offset of 'h' to -1.
bli_obj_set_diag_offset( -1, h );
// Set the structure and uplo of 'h'.
bli_obj_set_struc( BLIS_TRIANGULAR, h )
bli_obj_set_uplo( BLIS_UPPER, h );
// Randomize the elements on and above the first subdiagonal.
bli_randm( &h );
bli_printm( "h: after randomizing above first subdiagonal", &h, "%4.1f", "" );
// Create an alias to work with.
bli_obj_alias_to( h, hl );
// Flip the uplo of 'hl' and move the diagonal down by one.
bli_obj_set_uplo( BLIS_LOWER, hl );
bli_obj_set_diag_offset( -2, hl );
// Set the region strictly below the first subdiagonal (on or below
// the second subdiagonal) to zero.
bli_setm( &BLIS_ZERO, &hl );
bli_printm( "h: after setting elements below first subdiagonal to zero", &h, "%4.1f", "" );
// Free the objects.
bli_obj_free( &a );
bli_obj_free( &b );
bli_obj_free( &c );
bli_obj_free( &d );
bli_obj_free( &e );
return 0;
}
// -----------------------------------------------------------------------------

323
examples/oapi/7level2.c Normal file
View File

@@ -0,0 +1,323 @@
/*
BLIS
An object-based framework for developing high-performance BLAS-like
libraries.
Copyright (C) 2014, The University of Texas
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 )
{
num_t dt;
dim_t m, n;
inc_t rs, cs;
obj_t a, x, y, b, d;
obj_t* alpha;
obj_t* beta;
//
// This file demonstrates level-2 operations.
//
//
// Example 1: Perform a general rank-1 update (ger) operation.
//
printf( "\n#\n# -- Example 1 --\n#\n\n" );
// Create some matrix and vector operands to work with.
dt = BLIS_DOUBLE;
m = 4; n = 5; rs = 0; cs = 0;
bli_obj_create( dt, m, n, rs, cs, &a );
bli_obj_create( dt, m, 1, rs, cs, &x );
bli_obj_create( dt, 1, n, rs, cs, &y );
// Set alpha.
alpha = &BLIS_ONE;
// Initialize vectors 'x' and 'y'.
bli_randv( &x );
bli_setv( &BLIS_MINUS_ONE, &y );
// Initialize 'a' to 1.0.
bli_setm( &BLIS_ONE, &a );
bli_printm( "x: set to random values", &x, "%4.1f", "" );
bli_printm( "y: set to -1.0", &y, "%4.1f", "" );
bli_printm( "a: initial value", &a, "%4.1f", "" );
// a := a + alpha * x * y, where 'a' is general.
bli_ger( alpha, &x, &y, &a );
bli_printm( "a: after ger", &a, "%4.1f", "" );
// Free the objects.
bli_obj_free( &a );
bli_obj_free( &x );
bli_obj_free( &y );
//
// Example 2: Perform a general matrix-vector multiply (gemv) operation.
//
printf( "\n#\n# -- Example 2 --\n#\n\n" );
// Create some matrix and vector operands to work with.
dt = BLIS_DOUBLE;
m = 4; n = 5; rs = 0; cs = 0;
bli_obj_create( dt, m, n, rs, cs, &a );
bli_obj_create( dt, 1, n, rs, cs, &x );
bli_obj_create( dt, 1, m, rs, cs, &y );
// Notice that we created vectors 'x' and 'y' as row vectors, even though
// we often think of them as column vectors so that the overall problem
// dimensions remain conformal. Note that this flexibility only comes
// from the fact that the operation requires those operands to be vectors.
// If we were instead looking at an operation where the operands were of
// general shape (such as with the gemm operation), then typically the
// dimensions matter, and column vectors would not be interchangeable with
// row vectors and vice versa.
// Set the scalars to use.
alpha = &BLIS_ONE;
beta = &BLIS_ONE;
// Initialize vectors 'x' and 'y'.
bli_setv( &BLIS_ONE, &x );
bli_setv( &BLIS_ZERO, &y );
// Randomize 'a'.
bli_randm( &a );
bli_printm( "a: randomized", &a, "%4.1f", "" );
bli_printm( "x: set to 1.0", &x, "%4.1f", "" );
bli_printm( "y: initial value", &y, "%4.1f", "" );
// y := beta * y + alpha * a * x, where 'a' is general.
bli_gemv( alpha, &a, &x, beta, &y );
bli_printm( "y: after gemv", &y, "%4.1f", "" );
// Free the objects.
bli_obj_free( &a );
bli_obj_free( &x );
bli_obj_free( &y );
//
// Example 3: Perform a symmetric rank-1 update (syr) operation.
//
printf( "\n#\n# -- Example 3 --\n#\n\n" );
// Create some matrix and vector operands to work with.
dt = BLIS_DOUBLE;
m = 5; rs = 0; cs = 0;
bli_obj_create( dt, m, m, rs, cs, &a );
bli_obj_create( dt, 1, m, rs, cs, &x );
// Set alpha.
alpha = &BLIS_ONE;
// Initialize vector 'x'.
bli_randv( &x );
// Zero out all of matrix 'a'. This is optional, but will avoid possibly
// displaying junk values in the unstored triangle.
bli_setm( &BLIS_ZERO, &a );
// Mark matrix 'a' as symmetric and stored in the lower triangle, and
// then randomize that lower triangle.
bli_obj_set_struc( BLIS_SYMMETRIC, a )
bli_obj_set_uplo( BLIS_LOWER, a );
bli_randm( &a );
bli_printm( "x: set to random values", &x, "%4.1f", "" );
bli_printm( "a: initial value (zeros in upper triangle)", &a, "%4.1f", "" );
// a := a + alpha * x * x^T, where 'a' is symmetric and lower-stored.
bli_syr( alpha, &x, &a );
bli_printm( "a: after syr", &a, "%4.1f", "" );
// Free the objects.
bli_obj_free( &a );
bli_obj_free( &x );
//
// Example 4: Perform a symmetric matrix-vector multiply (symv) operation.
//
printf( "\n#\n# -- Example 4 --\n#\n\n" );
// Create some matrix and vector operands to work with.
dt = BLIS_DOUBLE;
m = 5; rs = 0; cs = 0;
bli_obj_create( dt, m, m, rs, cs, &a );
bli_obj_create( dt, 1, m, rs, cs, &x );
bli_obj_create( dt, 1, m, rs, cs, &y );
// Set the scalars to use.
alpha = &BLIS_ONE;
beta = &BLIS_ONE;
// Initialize vectors 'x' and 'y'.
bli_setv( &BLIS_ONE, &x );
bli_setv( &BLIS_ZERO, &y );
// Zero out all of matrix 'a'. This is optional, but will avoid possibly
// displaying junk values in the unstored triangle.
bli_setm( &BLIS_ZERO, &a );
// Mark matrix 'a' as symmetric and stored in the upper triangle, and
// then randomize that upper triangle.
bli_obj_set_struc( BLIS_SYMMETRIC, a )
bli_obj_set_uplo( BLIS_UPPER, a );
bli_randm( &a );
bli_printm( "a: randomized (zeros in lower triangle)", &a, "%4.1f", "" );
bli_printm( "x: set to 1.0", &x, "%4.1f", "" );
bli_printm( "y: initial value", &y, "%4.1f", "" );
// y := beta * y + alpha * a * x, where 'a' is symmetric and upper-stored.
bli_symv( alpha, &a, &x, beta, &y );
bli_printm( "y: after symv", &y, "%4.1f", "" );
// Free the objects.
bli_obj_free( &a );
bli_obj_free( &x );
bli_obj_free( &y );
//
// Example 5: Perform a triangular matrix-vector multiply (trmv) operation.
//
printf( "\n#\n# -- Example 5 --\n#\n\n" );
// Create some matrix and vector operands to work with.
dt = BLIS_DOUBLE;
m = 5; rs = 0; cs = 0;
bli_obj_create( dt, m, m, rs, cs, &a );
bli_obj_create( dt, 1, m, rs, cs, &x );
// Set the scalars to use.
alpha = &BLIS_ONE;
// Initialize vector 'x'.
bli_setv( &BLIS_ONE, &x );
// Zero out all of matrix 'a'. This is optional, but will avoid possibly
// displaying junk values in the unstored triangle.
bli_setm( &BLIS_ZERO, &a );
// Mark matrix 'a' as triangular and stored in the lower triangle, and
// then randomize that lower triangle.
bli_obj_set_struc( BLIS_TRIANGULAR, a )
bli_obj_set_uplo( BLIS_LOWER, a );
bli_randm( &a );
bli_printm( "a: randomized (zeros in upper triangle)", &a, "%4.1f", "" );
bli_printm( "x: initial value", &x, "%4.1f", "" );
// x := alpha * a * x, where 'a' is triangular and lower-stored.
bli_trmv( alpha, &a, &x );
bli_printm( "x: after trmv", &x, "%4.1f", "" );
// Free the objects.
bli_obj_free( &a );
bli_obj_free( &x );
//
// Example 6: Perform a triangular solve (trsv) operation.
//
printf( "\n#\n# -- Example 6 --\n#\n\n" );
// Create some matrix and vector operands to work with.
dt = BLIS_DOUBLE;
m = 5; rs = 0; cs = 0;
bli_obj_create( dt, m, m, rs, cs, &a );
bli_obj_create( dt, 1, m, rs, cs, &b );
bli_obj_create( dt, 1, m, rs, cs, &y );
// Set the scalars to use.
alpha = &BLIS_ONE;
// Initialize vector 'x'.
bli_setv( &BLIS_ONE, &b );
// Zero out all of matrix 'a'. This is optional, but will avoid possibly
// displaying junk values in the unstored triangle.
bli_setm( &BLIS_ZERO, &a );
// Mark matrix 'a' as triangular and stored in the lower triangle, and
// then randomize that lower triangle.
bli_obj_set_struc( BLIS_TRIANGULAR, a )
bli_obj_set_uplo( BLIS_LOWER, a );
bli_randm( &a );
// Load the diagonal. By setting the diagonal to something of greater
// absolute value than the off-diagonal elements, we increase the odds
// that the matrix is not singular (singular matrices have no inverse).
bli_obj_create( dt, m, m, 0, 0, &d );
bli_setd( &BLIS_TWO, &d );
bli_addd( &d, &a );
bli_printm( "a: randomized (zeros in upper triangle)", &a, "%4.1f", "" );
bli_printm( "b: initial value", &b, "%4.1f", "" );
// solve a * x = alpha * b, where 'a' is triangular and lower-stored, and
// overwrite b with the solution vector x.
bli_trsv( alpha, &a, &b );
bli_printm( "b: after trsv", &b, "%4.1f", "" );
// We can confirm the solution by comparing the product of a and x to the
// original value of b.
bli_copyv( &b, &y );
bli_trmv( alpha, &a, &y );
bli_printm( "y: should equal initial value of b", &y, "%4.1f", "" );
// Free the objects.
bli_obj_free( &a );
bli_obj_free( &b );
bli_obj_free( &d );
return 0;
}
// -----------------------------------------------------------------------------

286
examples/oapi/8level3.c Normal file
View File

@@ -0,0 +1,286 @@
/*
BLIS
An object-based framework for developing high-performance BLAS-like
libraries.
Copyright (C) 2014, The University of Texas
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 )
{
num_t dt;
dim_t m, n, k;
inc_t rs, cs;
side_t side;
obj_t a, b, c, d;
obj_t* alpha;
obj_t* beta;
//
// This file demonstrates level-3 operations.
//
//
// Example 1: Perform a general matrix-matrix multiply (gemm) operation.
//
printf( "\n#\n# -- Example 1 --\n#\n\n" );
// Create some matrix operands to work with.
dt = BLIS_DOUBLE;
m = 4; n = 5; k = 3; rs = 0; cs = 0;
bli_obj_create( dt, m, n, rs, cs, &c );
bli_obj_create( dt, m, k, rs, cs, &a );
bli_obj_create( dt, k, n, rs, cs, &b );
// Set the scalars to use.
alpha = &BLIS_ONE;
beta = &BLIS_ONE;
// Initialize the matrix operands.
bli_randm( &a );
bli_setm( &BLIS_ONE, &b );
bli_setm( &BLIS_ZERO, &c );
bli_printm( "a: randomized", &a, "%4.1f", "" );
bli_printm( "b: set to 1.0", &b, "%4.1f", "" );
bli_printm( "c: initial value", &c, "%4.1f", "" );
// c := beta * c + alpha * a * b, where 'a', 'b', and 'c' are general.
bli_gemm( alpha, &a, &b, beta, &c );
bli_printm( "c: after gemm", &c, "%4.1f", "" );
// Free the objects.
bli_obj_free( &a );
bli_obj_free( &b );
bli_obj_free( &c );
//
// Example 2: Perform a symmetric rank-k update (syrk) operation.
//
printf( "\n#\n# -- Example 2 --\n#\n\n" );
// Create some matrix and vector operands to work with.
dt = BLIS_DOUBLE;
m = 5; k = 3; rs = 0; cs = 0;
bli_obj_create( dt, m, m, rs, cs, &c );
bli_obj_create( dt, m, k, rs, cs, &a );
// Set alpha.
alpha = &BLIS_ONE;
// Initialize matrix operands.
bli_setm( &BLIS_ZERO, &c );
bli_randm( &a );
// Mark matrix 'c' as symmetric and stored in the lower triangle, and
// then randomize that lower triangle.
bli_obj_set_struc( BLIS_SYMMETRIC, c )
bli_obj_set_uplo( BLIS_LOWER, c );
bli_randm( &c );
bli_printm( "a: set to random values", &a, "%4.1f", "" );
bli_printm( "c: initial value (zeros in upper triangle)", &c, "%4.1f", "" );
// c := c + alpha * a * a^T, where 'c' is symmetric and lower-stored.
bli_syrk( alpha, &a, beta, &c );
bli_printm( "c: after syrk", &c, "%4.1f", "" );
// Free the objects.
bli_obj_free( &c );
bli_obj_free( &a );
//
// Example 3: Perform a symmetric matrix-matrix multiply (symm) operation.
//
printf( "\n#\n# -- Example 3 --\n#\n\n" );
// Create some matrix and vector operands to work with.
dt = BLIS_DOUBLE;
m = 5; n = 6; rs = 0; cs = 0;
bli_obj_create( dt, m, m, rs, cs, &a );
bli_obj_create( dt, m, n, rs, cs, &b );
bli_obj_create( dt, m, n, rs, cs, &c );
// Set the scalars to use.
alpha = &BLIS_ONE;
beta = &BLIS_ONE;
// Set the side operand.
side = BLIS_LEFT;
// Initialize matrices 'b' and 'c'.
bli_setm( &BLIS_ONE, &b );
bli_setm( &BLIS_ZERO, &c );
// Zero out all of matrix 'a'. This is optional, but will avoid possibly
// displaying junk values in the unstored triangle.
bli_setm( &BLIS_ZERO, &a );
// Mark matrix 'a' as symmetric and stored in the upper triangle, and
// then randomize that upper triangle.
bli_obj_set_struc( BLIS_SYMMETRIC, a )
bli_obj_set_uplo( BLIS_UPPER, a );
bli_randm( &a );
bli_printm( "a: randomized (zeros in lower triangle)", &a, "%4.1f", "" );
bli_printm( "b: set to 1.0", &b, "%4.1f", "" );
bli_printm( "c: initial value", &c, "%4.1f", "" );
// c := beta * c + alpha * a * b, where 'a' is symmetric and upper-stored.
// Note that the first 'side' operand indicates the side from which matrix
// 'a' is multiplied into 'b'.
bli_symm( side, alpha, &a, &b, beta, &c );
bli_printm( "c: after symm", &c, "%4.1f", "" );
// Free the objects.
bli_obj_free( &a );
bli_obj_free( &b );
bli_obj_free( &c );
//
// Example 4: Perform a triangular matrix-matrix multiply (trmm) operation.
//
printf( "\n#\n# -- Example 4 --\n#\n\n" );
// Create some matrix and vector operands to work with.
dt = BLIS_DOUBLE;
m = 5; n = 4; rs = 0; cs = 0;
bli_obj_create( dt, m, m, rs, cs, &a );
bli_obj_create( dt, m, n, rs, cs, &b );
// Set the scalars to use.
alpha = &BLIS_ONE;
// Set the side operand.
side = BLIS_LEFT;
// Initialize matrix 'b'.
bli_setm( &BLIS_ONE, &b );
// Zero out all of matrix 'a'. This is optional, but will avoid possibly
// displaying junk values in the unstored triangle.
bli_setm( &BLIS_ZERO, &a );
// Mark matrix 'a' as triangular and stored in the lower triangle, and
// then randomize that lower triangle.
bli_obj_set_struc( BLIS_TRIANGULAR, a )
bli_obj_set_uplo( BLIS_LOWER, a );
bli_randm( &a );
bli_printm( "a: randomized (zeros in upper triangle)", &a, "%4.1f", "" );
bli_printm( "b: initial value", &b, "%4.1f", "" );
// b := alpha * a * b, where 'a' is triangular and lower-stored.
bli_trmm( side, alpha, &a, &b );
bli_printm( "x: after trmv", &b, "%4.1f", "" );
// Free the objects.
bli_obj_free( &a );
bli_obj_free( &b );
//
// Example 5: Perform a triangular solve with multiple right-hand sides
// (trsm) operation.
//
printf( "\n#\n# -- Example 5 --\n#\n\n" );
// Create some matrix and vector operands to work with.
dt = BLIS_DOUBLE;
m = 5; n = 4; rs = 0; cs = 0;
bli_obj_create( dt, m, m, rs, cs, &a );
bli_obj_create( dt, m, n, rs, cs, &b );
bli_obj_create( dt, m, n, rs, cs, &c );
// Set the scalars to use.
alpha = &BLIS_ONE;
// Set the side operand.
side = BLIS_LEFT;
// Initialize matrix 'b'.
bli_setm( &BLIS_ONE, &b );
// Zero out all of matrix 'a'. This is optional, but will avoid possibly
// displaying junk values in the unstored triangle.
bli_setm( &BLIS_ZERO, &a );
// Mark matrix 'a' as triangular and stored in the lower triangle, and
// then randomize that lower triangle.
bli_obj_set_struc( BLIS_TRIANGULAR, a )
bli_obj_set_uplo( BLIS_LOWER, a );
bli_randm( &a );
// Load the diagonal. By setting the diagonal to something of greater
// absolute value than the off-diagonal elements, we increase the odds
// that the matrix is not singular (singular matrices have no inverse).
bli_obj_create( dt, m, m, 0, 0, &d );
bli_setd( &BLIS_TWO, &d );
bli_addd( &d, &a );
bli_printm( "a: randomized (zeros in upper triangle)", &a, "%4.1f", "" );
bli_printm( "b: initial value", &b, "%4.1f", "" );
// solve a * x = alpha * b, where 'a' is triangular and lower-stored, and
// overwrite b with the solution matrix x.
bli_trsm( side, alpha, &a, &b );
bli_printm( "b: after trsm", &b, "%4.1f", "" );
// We can confirm the solution by comparing the product of a and x to the
// original value of b.
bli_copym( &b, &c );
bli_trmm( side, alpha, &a, &c );
bli_printm( "c: should equal initial value of b", &c, "%4.1f", "" );
// Free the objects.
bli_obj_free( &a );
bli_obj_free( &b );
bli_obj_free( &c );
bli_obj_free( &d );
return 0;
}
// -----------------------------------------------------------------------------

181
examples/oapi/Makefile Normal file
View File

@@ -0,0 +1,181 @@
#
#
# 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 at Austin 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.
#
#
#
# Makefile
#
# Field G. Van Zee
#
# Makefile for BLIS testsuite.
#
#
# --- Makefile PHONY target definitions ----------------------------------------
#
.PHONY: all bin clean run
#
# --- Distribution path override -----------------------------------------------
#
# Override the default DIST_PATH and BUILD_PATH values so that make can find
# the source distribution and build location.
DIST_PATH := ../..
BUILD_PATH := ../..
#
# --- Include common makefile definitions --------------------------------------
#
# Define the name of the common makefile fragment.
COMMON_MK_FILE := common.mk
# Construct the path to the makefile configuration file that was generated by
# the configure script.
COMMON_MK_PATH := $(DIST_PATH)/$(COMMON_MK_FILE)
# Include the common makefile fragment.
-include $(COMMON_MK_PATH)
# Detect whether we actually got the common makefile fragment. If we didn't,
# then it is likely that the user has not yet generated it (via configure).
#ifeq ($(strip $(COMMON_MK_INCLUDED)),yes)
#COMMON_MK_PRESENT := yes
#else
#COMMON_MK_PRESENT := no
#endif
#
# --- General build definitions ------------------------------------------------
#
TEST_SRC_PATH := .
TEST_OBJ_PATH := .
# Gather all local object files.
TEST_OBJS := $(sort $(patsubst $(TEST_SRC_PATH)/%.c, \
$(TEST_OBJ_PATH)/%.o, \
$(wildcard $(TEST_SRC_PATH)/*.c)))
# Use the "framework" CFLAGS for the configuration family.
CFLAGS := $(call get-frame-cflags-for,$(CONFIG_NAME))
# Add local header paths to CFLAGS
CFLAGS += -I$(TEST_SRC_PATH)
# Locate the libblis library to which we will link.
LIBBLIS_LINK := $(BUILD_PATH)/$(LIBBLIS_LINK)
# Binary executable name.
TEST_BINS := 0obj_basic.x \
1obj_attach.x \
2obj_ij.x \
3level0.x \
4level1v.x \
5level1m.x \
6level1m_diag.x \
7level2.x \
8level3.x
#
# --- Targets/rules ------------------------------------------------------------
#
# --- Primary targets ---
all: bin
bin: $(TEST_BINS)
# --- Environment check rules ---
#
#check-env: check-env-make-defs check-env-fragments check-env-mk
#
#check-env-mk:
#ifeq ($(CONFIG_MK_PRESENT),no)
# $(error Cannot proceed: config.mk not detected! Run configure first)
#endif
#
#check-env-fragments: check-env-mk
#ifeq ($(MAKEFILE_FRAGMENTS_PRESENT),no)
# $(error Cannot proceed: makefile fragments not detected! Run configure first)
#endif
#
#check-env-make-defs: check-env-fragments
#ifeq ($(MAKE_DEFS_MK_PRESENT),no)
# $(error Cannot proceed: make_defs.mk not detected! Invalid configuration)
#endif
# --Object file rules --
$(TEST_OBJ_PATH)/%.o: $(TEST_SRC_PATH)/%.c $(LIBBLIS_LINK)
ifeq ($(BLIS_ENABLE_VERBOSE_MAKE_OUTPUT),yes)
$(CC) $(CFLAGS) -c $< -o $@
else
@echo "Compiling $@"
@$(CC) $(CFLAGS) -c $< -o $@
endif
# -- Executable file rules --
%.x: %.o $(LIBBLIS_LINK)
ifeq ($(BLIS_ENABLE_VERBOSE_MAKE_OUTPUT),yes)
$(LINKER) $< $(LIBBLIS_LINK) $(LDFLAGS) -o $@
else
@echo "Linking $@ against '$(LIBBLIS_LINK) $(LDFLAGS)'"
@$(LINKER) $< $(LIBBLIS_LINK) $(LDFLAGS) -o $@
endif
# -- Test run rules --
#run: $(TEST_BIN)
# ./$(TEST_BIN)
# -- Clean rules --
clean:
- $(RM_F) $(TEST_OBJS) $(TEST_BINS)

25
examples/oapi/README Normal file
View File

@@ -0,0 +1,25 @@
BLIS object API examples
------------------------
This directory contains several files, each containing various pieces of
example code that demonstrate core functionality of the object API in BLIS.
These example files should be thought of collectively like a tutorial, and
therefore it is recommended to start from the beginning (the file that
starts in '0').
You can build all of the examples by simply running 'make' from this
directory. (You can also run 'make clean'.) The makefile assumes that
you've already configured and built (but not necessarily installed) BLIS
two directories up, in "../..".
This tutorial is not exhaustive or complete; several object API functions
were omitted (mostly for brevity's sake) and thus more examples could be
written. If you've found object functionality in BLIS and are unsure how to
use it, or if you are unsure of what additional functionality is present in
BLIS, please contact the blis-devel mailing list [1] for guidance.
Thanks for your interest in BLIS!
[1] https://groups.google.com/d/forum/blis-devel