Examples of usage of librsb
in C (<rsb.h> and <blas_sparse.h>), C++ (optional <rsb.hpp>), Fortran (<rsb.F90> and <rsb_blas_sparse.F90>).
More...
|
file | assemble.cpp |
| C++ example based on <rsb.hpp> assembling RsbMatrix by pieces.
|
|
file | autotune.cpp |
| C++ example based on <rsb.hpp> for performance-benchmarking a matrix from file using RsbMatrix.tune_spmm(), for various right-hand side counts.
|
|
file | bench.cpp |
| C++ example based on <rsb.hpp> for performance-benchmarking a matrix from file using RsbMatrix.spmm(), for various right-hand side counts.
|
|
file | build.cpp |
| C++ example based on <rsb.hpp> using timings for common matrix operations on RsbMatrix: RsbMatrix.get_coo(), rsb_coo_sort(), rsb_time().
|
|
file | example.cpp |
| C++ example based on <rsb.hpp> using RsbMatrix.spmm().
|
|
file | misc.cpp |
| C++ example based on <rsb.hpp> showing various RsbMatrix operations.
|
|
file | mtx2bin.cpp |
| C++ example based on <rsb.hpp> converting a Matrix Market file into a custom format using RsbMatrix.get_coo().
|
|
file | render.cpp |
| C++ example based on <rsb.hpp> of invoking the autotuner on an RsbMatrix matrix with RsbMatrix.tune_spmm() and rendering it with RsbMatrix.rndr().
|
|
file | span.cpp |
| C++ example based on <rsb.hpp> illustrating use of RsbMatrix.file_save(), and std::span-based versions of RsbMatrix.tune_spmm(), RsbMatrix.spmv().
|
|
file | twonnz.cpp |
| C++ example based on <rsb.hpp> measuring RsbMatrix.spmm() performance of a matrix with only two elements; this is is effectively measuring performance of result vector scaling.
|
|
file | autotune.c |
| C "RSB autotune" example program based on <rsb.h>. uses rsb_file_mtx_load(), rsb_spmm(), rsb_tune_spmm().
|
|
file | backsolve.c |
| C triangular solve example program. Uses rsb_spsv(),rsb_tune_spsm(). Based on <rsb.h>.
|
|
file | cplusplus.cpp |
| C++ program using the C <rsb.h> interface. Uses rsb_tune_spmm(), rsb_spmv().
|
|
file | hello-spblas.c |
| A first C "hello RSB" example program using a Sparse BLAS interface and <rsb.h>. Uses BLAS_duscr_begin(), BLAS_ussp(), BLAS_usgp(), BLAS_duscr_insert_entries(), BLAS_duscr_end(), BLAS_dusget_element(),BLAS_dusmv(),BLAS_usds().
|
|
file | hello.c |
| A first "hello RSB" example C program based on <rsb.h>. Uses rsb_lib_set_opt(), rsb_mtx_get_info_str().
|
|
file | io-spblas.c |
| Example C program using the Sparse BLAS interface and reading from file using rsb_blas_file_mtx_load(), BLAS_usgp(), BLAS_dusmv(), BLAS_usds().
|
|
file | power.c |
| A toy <rsb.h>-based C program implementing the power method for computing matrix eigenvalues. Uses rsb_spmv().
|
|
file | snippets.c |
| Collection of C snippets of other examples. Used piecewise the documentation. Not intended to be read as example.
|
|
file | transpose.c |
| A toy <rsb.h>-based C program showing instantiation, transposition and other operations on a single matrix. Uses rsb_mtx_clone(), rsb_file_mtx_save(), rsb_file_mtx_get_dims(), rsb_file_mtx_load().
|
|
Examples of usage of librsb
in C (<rsb.h> and <blas_sparse.h>), C++ (optional <rsb.hpp>), Fortran (<rsb.F90> and <rsb_blas_sparse.F90>).
The following fully working example programs illustrate correct ways of using the library.
- First example in C, using <rsb.h>: examples_hello_c.
- First example in C, using <blas_sparse.h>: examples_hello_spblas_c.
- Autotuning example in C, using <rsb.h>: examples_autotune_c.
- I/O example in C, using <blas_sparse.h>: examples_io_spblas_c.
- Example transposing a matrix in C, using <rsb.h> in C: examples_transpose_c.
- Example showing the power method in C, using <rsb.h> in C: examples_power_c.
- Example in Fortran, using <rsb_blas_sparse.F90>: examples_fortran_F90.
- Example in Fortran, using <rsb.F90>: examples_fortran_rsb_fi_F90.
- Example in C, using <rsb.h>: examples_backsolve_c.
- Misc example snippets in C, using <rsb.h>: examples_snippets_c.
- Benchmark invocation from shell script: examples_bench_sh.
Once installed librsb
, the script displayed here (examples/make.sh) should be sufficient to build these examples:
#!/bin/bash
# Example script to build the librsb example programs.
set -e
set -x
srcdir=${srcdir:-`pwd`}
builddir=${builddir:-`pwd`}
LIBRSB_CONFIG=${LIBRSB_CONFIG:-librsb-config}
PKG_CONFIG=pkg-config
WANT_PKGCONFIG="no"
if test x"rsblib" != x"" ; then
for s in `ls ${srcdir}/../rsblib/examples/*.cpp`
do
p=${builddir}/../rsblib/examples/`basename ${s/.cpp/}`
rm -f $p
CXXFLAGS=`${LIBRSB_CONFIG} --cppflags --I_opts`
LDFLAGS=`${LIBRSB_CONFIG} --ldflags --extra_libs`
CXX=`${LIBRSB_CONFIG} --cxx`
LINK=`${LIBRSB_CONFIG} --link`
o="${p}.o"
ccmd="$CXX $CXXFLAGS -c $s -o $o"
lcmd="$LINK $o $LDFLAGS -o $p"
echo "$ccmd && $lcmd"
( $ccmd && $lcmd )
# one may use a single command, but that's error-prone (may miss libraries):
#cmd="$CXX $CXXFLAGS $s $LDFLAGS -o $p"
#echo $cmd
#$cmd
done
fi
for s in ${srcdir}/*.c
do
p=`basename ${s/.c/}`
if test $p == hello-spblas -a x"yes" != x"yes" ; then continue; fi
if test $p == io-spblas -a x"yes" != x"yes" ; then continue; fi
rm -f $p
CFLAGS=`${LIBRSB_CONFIG} --I_opts --cppflags`
LDFLAGS=`${LIBRSB_CONFIG} --ldflags --extra_libs`
CC=`${LIBRSB_CONFIG} --cc`
LINK=`${LIBRSB_CONFIG} --link`
o="${p}.o"
ccmd="$CC $CFLAGS -c $s -o $o"
lcmd="$LINK $o $LDFLAGS -o $p"
echo "$ccmd && $lcmd"
( $ccmd && $lcmd )
# one may use a single command, but that's error-prone (may miss libraries):
#cmd="$CC $CFLAGS $s $LDFLAGS -o $p"
#echo $cmd
#$cmd
if test x"${WANT_PKGCONFIG}" != x"no" ; then
CFLAGS=`${PKG_CONFIG} --cflags librsb`
LIBS=`${PKG_CONFIG} --libs --static librsb`
ccmd="$CC $CFLAGS -c $s -o $o"
lcmd="$LINK $o $LIBS -o $p"
echo "$ccmd && $lcmd"
( $ccmd && $lcmd )
fi
done
if test x"yes" = x"yes" ; then
if test x"yes" = x"yes" ; then
FP=${srcdir}/fortran_rsb_fi.F90
if test x"yes" = x"yes" ; then
FP+=\ ${srcdir}/fortran.F90
fi
# activated if you have built the Fortran modules and installed them in the right path.
for s in $FP
do
p=`basename ${s/.F90/}`
rm -f $p
FCFLAGS=`${LIBRSB_CONFIG} --I_opts --fcflags`
LDFLAGS=`${LIBRSB_CONFIG} --ldflags --extra_libs`
FC=`${LIBRSB_CONFIG} --fc`
LINK=`${LIBRSB_CONFIG} --link`
o="${p}.o"
FCLIBS=`${LIBRSB_CONFIG} --fclibs`
ccmd="$FC $FCFLAGS -c $s -o $o"
lcmd="$LINK $o $LDFLAGS $FCLIBS -o $p"
echo "$ccmd && $lcmd"
( $ccmd && $lcmd )
done
fi
fi
echo " [*] done building examples!"
examples/hello.c:
#include <stdio.h>
int main(
const int argc,
char *
const argv[])
{
char ib[200];
struct rsb_mtx_t *mtxAp = NULL;
printf("Hello, RSB!\n");
printf("Initializing the library...\n");
{
printf("Error initializing the library!\n");
goto err;
}
printf("Correctly initialized the library.\n");
printf("Attempting to set the"
" RSB_IO_WANT_EXTRA_VERBOSE_INTERFACE library option.\n");
{
{
char errbuf[256];
printf("Failed setting the"
" RSB_IO_WANT_EXTRA_VERBOSE_INTERFACE"
" library option (reason string:\n%s).\n",errbuf);
{
printf("This error may be safely ignored.\n");
}
else
{
printf("Some unexpected error occurred!\n");
goto err;
}
}
else
{
printf("Setting back the "
"RSB_IO_WANT_EXTRA_VERBOSE_INTERFACE"
" library option.\n");
evi = 0;
&evi);
}
}
VA,IA,JA,nnzA,typecode,nrA,ncA,brA,bcA,
,&errval);
{
printf("Error while allocating the matrix!\n");
goto err;
}
printf("Correctly allocated a matrix.\n");
printf("Summary information of the matrix:\n");
ib,sizeof(ib));
printf("%s",ib);
printf("\n");
if((errval =
{
printf("Error performing a multiplication!\n");
goto err;
}
printf("Correctly performed a SPMV.\n");
printf("Correctly freed the matrix.\n");
{
printf("Error finalizing the library!\n");
goto err;
}
printf("Correctly finalized the library.\n");
printf("Program terminating with no error.\n");
return EXIT_SUCCESS;
err:
printf("Program terminating with error.\n");
return EXIT_FAILURE;
}
examples/hello-spblas.c:
#include <stdio.h>
int main(
const int argc,
char *
const argv[])
{
#ifndef RSB_NUMERICAL_TYPE_DOUBLE
printf("'double' type configured out."
" Please reconfigure the library with it and recompile.\n");
return EXIT_SUCCESS;
#else
const int nnz = 4;
const int nr = 3;
const int nc = 3;
#ifdef RSB_WANT_LONG_IDX_TYPE
const int64_t IA[] = { 0, 1, 2, 2 };
#else
const int IA[] = { 0, 1, 2, 2 };
#endif
#ifdef RSB_WANT_LONG_IDX_TYPE
const int64_t JA[] = { 0, 1, 0, 2 };
#else
const int JA[] = { 0, 1, 0, 2 };
#endif
double VA[] = { 11.0, 22.0, 13.0, 33.0 };
double X[] = { 0.0, 0.0, 0.0 };
const double B[] = { -1.0, -2.0, -2.0 };
const double AB[] = { 11.0+26.0, 44.0, 66.0+13.0 };
int i;
printf("Hello, RSB!\n");
{
goto err;
}
printf("Correctly initialized the library.\n");
{
goto err;
}
{
goto err;
}
{
printf("Symmetry property non set ?!\n");
goto err;
}
{
goto err;
}
{
goto err;
}
printf("Correctly allocated a matrix.\n");
VA[0] = 0.0;
{
goto err;
}
if( VA[0] != 11.0 )
{
goto err;
}
{
goto err;
}
for( i = 0 ; i < nc; ++i )
if( X[i] != AB[i] )
{
printf("Computed SPMV result seems wrong. Terminating.\n");
goto err;
}
printf("Correctly performed a SPMV.\n");
{
goto err;
}
printf("Correctly freed the matrix.\n");
{
goto err;
}
printf("Correctly finalized the library.\n");
printf("Program terminating with no error.\n");
return EXIT_SUCCESS;
err:
printf("Program terminating with error.\n");
return EXIT_FAILURE;
#endif
}
examples/autotune.c:
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
static int tune_from_file(
char *
const filename,
rsb_int_t wvat)
{
struct rsb_mtx_t *mtxMp = NULL;
const void * alphap = NULL;
const void * betap = NULL;
char ib[200];
const char*is = "RSB_MIF_MATRIX_INFO__TO__CHAR_P";
rsb_type_t typecodea [] = RSB_MATRIX_TYPE_CODES_ARRAY;
int typecodei;
goto err;
goto err;
printf("Loading matrix from file \"%s\".\n",filename);
goto err;
for( typecodei = 0 ; typecodei < RSB_IMPLEMENTED_TYPES; ++typecodei )
{
struct rsb_mtx_t *mtxAp = NULL;
struct rsb_mtx_t *mtxOp = NULL;
sf = 0.0;
tn = 0;
printf("Considering %c clone.\n",typecode);
flagsA);
goto err;
printf("Base matrix:\n");
printf("%s\n\n",ib);
alphap, mtxAp, nrhs, order, NULL, ldB, betap, NULL, ldC);
if(tn == 0)
printf("After %lfs, autotuning routine did not find a better"
" threads count configuration.\n",dt);
else
printf("After %lfs, thread autotuning declared speedup of %lg x,"
" when using threads count of %d.\n",dt,sf,tn);
printf("\n");
mtxOp = mtxAp;
alphap, NULL, nrhs, order, NULL, ldB, betap, NULL, ldC);
goto err;
if( mtxOp == mtxAp )
{
printf("After %lfs, global autotuning found old matrix optimal,"
" with declared speedup %lg x when using %d threads\n",dt,sf,tn);
}
else
{
printf("After %lfs, global autotuning declared speedup of %lg x,"
" when using threads count of %d and a new matrix:\n",dt,sf,tn);
printf("%s\n",ib);
}
printf("\n");
mtxAp = NULL;
}
mtxMp = NULL;
err:
printf("Program terminating with error.\n");
return errval;
}
int main(
const int argc,
char *
const argv[])
{
struct rsb_mtx_t *mtxAp = NULL;
char ib[200];
const char*is = "RSB_MIF_MATRIX_INFO__TO__CHAR_P";
if(argc > 1 && !isdigit(argv[1][0]) )
{
errval = tune_from_file(argv[1],wvat);
goto ret;
}
if(argc > 1)
{
nrA = ncA = atoi(argv[1]);
goto err;
nnzA = (nrA/rd)*(ncA/cd);
ldB = nrA;
ldC = ncA;
}
printf("Creating %d x %d matrix with %d nonzeroes.\n",(int)nrA,
(int)ncA, (int)nnzA);
IA = calloc(nnzA, si);
JA = calloc(nnzA, si);
VA = calloc(nnzA, so);
Bp = calloc(nrhs*ncA ,so);
Cp = calloc(nrhs*nrA ,so);
if( ! ( VA && IA && JA && Bp && Cp ) )
goto err;
for(nrhsi=0;nrhsi<nrhs;++nrhsi)
for(ci=0;ci<ncA/cd;++ci)
Bp[nrhsi*ldC+ci] = 1.0;
for(nrhsi=0;nrhsi<nrhs;++nrhsi)
for(ri=0;ri<nrA/rd;++ri)
Cp[nrhsi*ldC+ri] = 1.0;
ni = 0;
for(ci=0;ci<ncA/cd;++ci)
for(ri=0;ri<nrA/rd;++ri)
{
VA[ni] = nrA * ri + ci,
IA[ni] = ri;
JA[ni] = ci;
ni++;
}
{
printf("Error setting option!\n");
goto err;
}
VA,IA,JA,nnzA,typecode,nrA,ncA,bs,bs,
free(VA);
free(IA);
free(JA);
VA = NULL;
IA = NULL;
JA = NULL;
goto err;
printf("Allocated matrix of %zd nonzeroes:\n",(size_t)nnzA);
printf("%s\n\n",ib);
for(t=0;t<tt;++t)
rsb_spmm(transA,&alpha,mtxAp,nrhs,order,Bp,ldB,&beta,Cp,ldC);
odt = dt;
printf("Before auto-tuning, %d multiplications took %lfs.\n",tt,dt);
printf("Threads autotuning (may take more than %lfs)...\n",
oitmax*tmax);
&alpha, mtxAp, nrhs, order, Bp, ldB, &beta, Cp, ldC);
goto err;
if(tn == 0)
printf("After %lfs, autotuning routine did not find a better"
" threads count configuration.\n",dt);
else
printf("After %lfs, autotuning routine declared speedup of %lg x,"
" when using threads count of %d.\n",dt,sf,tn);
goto err;
printf("%s\n",ib);
for(t=0;t<tt;++t)
rsb_spmm(transA,&alpha,mtxAp,nrhs,order,Bp,ldB,&beta,Cp,ldC);
printf("After threads auto-tuning, %d multiplications took %lfs"
" -- effective speedup of %lg x\n",tt,dt,odt/dt);
odt = dt;
tn = 0;
goto err;
goto err;
printf("Matrix autotuning (may take more than %lfs; using %d"
" threads )...\n", oitmax*tmax, tn);
&alpha, NULL, nrhs, order, Bp, ldB, &beta, Cp, ldC);
goto err;
if(tn == 0)
printf("After %lfs, autotuning routine did not find a better"
" threads count configuration.\n",dt);
else
printf("After %lfs, autotuning routine declared speedup of %lg x,"
" when using threads count of %d.\n",dt,sf,tn);
printf("%s\n",ib);
for(t=0;t<tt;++t)
rsb_spmm(transA,&alpha,mtxAp,nrhs,order,Bp,ldB,&beta,Cp,ldC);
printf("After threads auto-tuning, %d multiplications took %lfs"
" -- further speedup of %lg x\n",tt,dt,odt/dt);
free(Cp);
free(Bp);
{
printf("librsb timer-based profiling is not supported in "
"this build. If you wish to have it, re-configure librsb "
"with its support. So you can safely ignore the error you"
" might just have seen printed out on screen.\n");
}
else
if(etime)
printf("Elapsed program time is %5.2lfs\n",etime);
ret:
goto err;
return EXIT_SUCCESS;
err:
printf("Program terminating with error.\n");
return EXIT_FAILURE;
}
examples/io-spblas.c:
#include <stdio.h>
int main(
const int argc,
char *
const argv[])
{
#ifndef RSB_NUMERICAL_TYPE_DOUBLE
printf("Skipping a test because of 'double' type opted out.\n");
return EXIT_SUCCESS;
#else
const rsb_char_t * filename = argc > 1 ? argv[1] :
"pd.mtx";
printf("Hello, RSB!\n");
{
printf("Error while initializing the library.\n");
goto err;
}
printf("Correctly initialized the library.\n");
{
printf("Error while loading matrix %s from file.\n",
filename);
goto err;
}
printf("Correctly loaded and allocated a matrix"
" from file %s.\n",filename);
printf("Matrix is symmetric\n");
printf("Matrix is hermitian\n");
printf("Now SPMV with NULL vectors will be attempted,"
" resulting in an error (so don't worry).\n");
{
printf("Correctly detected an error condition.\n");
goto okerr;
}
printf("No error detected ?\nIf you see this line printed out,"
" please report as a bug, because the above NULL pointers"
" should have been detected\n");
return EXIT_FAILURE;
okerr:
printf("Program correctly recovered from intentional"
" error condition.\n");
{
printf("Error while freeing the matrix!\n");
goto err;
}
printf("Correctly freed the matrix.\n");
err:
{
printf("Failed finalizing the library.\n");
goto ferr;
}
printf("Correctly finalized the library.\n");
return EXIT_SUCCESS;
ferr:
return EXIT_FAILURE;
#endif
}
examples/transpose.c:
#include <stdio.h>
int main(
const int argc,
char *
const argv[])
{
struct rsb_mtx_t *mtxAp = NULL;
{
return EXIT_FAILURE;
}
VA,IA,JA,nnzA,typecode,nrA,ncA,
if(!mtxAp)
{
return EXIT_FAILURE;
}
{
goto err;
}
{
goto err;
}
{
goto err;
}
if(!mtxAp)
{
return EXIT_FAILURE;
}
{
goto err;
}
{
goto err;
}
{
goto err;
}
goto err;
if( vl != 6 )
{
goto err;
}
goto err;
{
unsigned char pixmap[3*2*2];
goto err;
}
{
goto err;
}
return EXIT_SUCCESS;
err:
return EXIT_FAILURE;
}
examples/power.c:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main(
const int argc,
char *
const argv[])
{
const int WANT_VERBOSE = 0;
struct rsb_mtx_t *mtxAp = NULL;
const int br = bs, bc = bs;
int i;
oldnorm = 1.0,
*b1 = NULL, *b2 = NULL,
*bnow = NULL, *bnext = NULL;
size_t ds = 0;
return EXIT_FAILURE;
if(!mtxAp)
return EXIT_FAILURE;
b1 = calloc(1,ds);
b2 = calloc(1,ds);
if(! (b1 && b2))
{
goto err;
}
for( i = 0; i < nrA; ++i )
b1[i] = 1;
bnow = b1, bnext = b2;
while( fabs(norm-oldnorm) > tol && it<maxit )
{
++ it;
oldnorm = norm;
goto err;
norm = 0;
for(i=0;i<nrA;++i)
norm += bnext[i]*bnext[i];
norm = sqrt(norm);
norm = 1.0/norm;
for(i=0;i<nrA;++i)
bnext[i] *= norm;
norm = 1.0/norm;
printf("it:%d norm:%lg norm diff:%lg\n",it,norm,norm-oldnorm);
{void *tmp=bnow;bnow=bnext;bnext=tmp;}
if(WANT_VERBOSE)
{
printf("norm:%lg\n",norm);
if(isinf(norm))
goto err;
for(i=0;i<2;++i)
printf("x[%d]=%lg\n",i,((double*)bnext)[i]);
}
}
free(b1);
free(b2);
goto err;
if( it == maxit )
{
printf("ERROR: hit iterations limit without convergence!");
}
return EXIT_SUCCESS;
err:
return EXIT_FAILURE;
}
examples/fortran.F90:
IMPLICIT NONE
INTEGER :: i, j
INTEGER(KIND=RSB_BLAS_IDX_KIND) :: istat = 0, res
TYPE(c_ptr),TARGET :: mtxap = c_null_ptr
INTEGER :: a
INTEGER(KIND=RSB_IDX_KIND),PARAMETER :: incx = 1
INTEGER(KIND=RSB_IDX_KIND),PARAMETER :: incy = 1
REAL(KIND=8),PARAMETER :: alpha = 3
INTEGER(KIND=RSB_IDX_KIND),PARAMETER :: nr = 20
INTEGER(KIND=RSB_IDX_KIND),PARAMETER :: nc = nr
INTEGER(KIND=RSB_IDX_KIND),PARAMETER :: nnz = (nr*(nr+1))/2
INTEGER(KIND=RSB_IDX_KIND),PARAMETER :: nrhs = 1
INTEGER(KIND=RSB_IDX_KIND) :: nt = 0
INTEGER :: ic, ir
INTEGER(KIND=RSB_IDX_KIND),PARAMETER :: ia(nnz) = (/ (((ir), ic=1,ir), ir=1,nr ) /)
INTEGER(KIND=RSB_IDX_KIND),PARAMETER :: ja(nnz) = (/ (((ic), ic=1,ir), ir=1,nr ) /)
REAL(KIND=8),PARAMETER :: va(nnz) = (/ ((1, ic=1,ir), ir=1,nr ) /)
REAL(KIND=8) :: x(nc,nrhs) = reshape((/((1), ic=1,nc*nrhs)/),[nc,nrhs])
REAL(KIND=8),PARAMETER :: cy(nr,nrhs) = reshape((/((alpha+alpha*nr), ir=1,nr*nrhs)/),[nr,nrhs])
REAL(KIND=8) :: y(nr,nrhs) = reshape((/((alpha), ir=1,nr*nrhs)/),[nr,nrhs])
res = 0
IF (res.NE.0) GOTO 9999
IF (istat.NE.0) GOTO 9997
IF (istat.NE.0) print *,"autotuning returned nonzero:", istat &
&," ...did you enable autotuning ?"
IF (istat.NE.0) GOTO 9997
IF (istat.NE.0) GOTO 9997
IF (nt.NE.0) print*,"autotuner chose ",nt," threads"
IF (istat.NE.0) GOTO 9997
DO j = 1, nrhs
CALL usmv(transn,alpha,a,x(:,j),incx,y(:,j),incy,istat)
END DO
IF (istat.NE.0) GOTO 9997
DO j = 1, nrhs
DO i = 1, nr
IF (y(i,j).NE.cy(i,j)) print *, "first check results are not ok"
IF (y(i,j).NE.cy(i,j)) GOTO 9997
END DO
END DO
y(:,:) = alpha
IF (istat.NE.0) GOTO 9997
DO j = 1, nrhs
CALL usmv(transn,alpha,a,x(:,j),incx,y(:,j),incy,istat)
END DO
IF (istat.NE.0) GOTO 9997
DO j = 1, nrhs
DO i = 1, nr
IF (y(i,j).NE.cy(i,j)) print *,"second check results are not ok"
IF (y(i,j).NE.cy(i,j)) GOTO 9997
END DO
END DO
print *, "check results are ok"
GOTO 9998
9997 res = -1
9998 CONTINUE
IF (istat.NE.0) res = -1
9999 CONTINUE
USE iso_c_binding
IMPLICIT NONE
INTEGER(KIND=RSB_IDX_KIND) :: res
INTEGER :: j
INTEGER(KIND=RSB_BLAS_IDX_KIND) :: istat = 0
INTEGER :: a
INTEGER(KIND=RSB_IDX_KIND),PARAMETER :: incx = 1
INTEGER(KIND=RSB_IDX_KIND),PARAMETER :: incy = 1
COMPLEX(KIND=8),PARAMETER :: alpha = 3
INTEGER(KIND=RSB_IDX_KIND) :: nr
INTEGER(KIND=RSB_IDX_KIND) :: nc
INTEGER(KIND=RSB_IDX_KIND) :: nz
INTEGER(KIND=RSB_IDX_KIND) :: st
INTEGER(KIND=RSB_IDX_KIND),PARAMETER :: nrhs = 4
COMPLEX(KIND=8), ALLOCATABLE, TARGET, DIMENSION(:,:) :: x
COMPLEX(KIND=8), ALLOCATABLE, TARGET, DIMENSION(:,:) :: y
CHARACTER(KIND=C_SIGNED_CHAR,LEN=7),TARGET :: filename = "pd.mtx"//c_null_char
REAL(KIND=C_DOUBLE) :: mvt,mmt,omt
INTEGER(KIND=C_INT),TARGET::izero=0
res = 0
print*,"Read matrix ",filename(1:6)," ",nr,"x",nc,":",nz
IF (st .EQ. 1) print*,"Matrix has no symmetry"
IF (st .EQ. 1) print*,"Matrix is upper symmetric"
IF (st .EQ. 1) print*,"Matrix is upper hermitian"
IF (istat.NE.0) GOTO 9997
WRITE(*,'(a,i0)') "Using NRHS=",nrhs
ALLOCATE( x(nc,nrhs))
ALLOCATE( y(nr,nrhs))
x = 1.0
y = 0.0
DO j = 1, nrhs
CALL usmv(transn,alpha,a,x(:,j),incx,y(:,j),incy,istat)
END DO
IF (istat.NE.0) GOTO 9997
WRITE(*,'(a,e12.4,a)') "Repeated USMV took ",mvt," s"
y = 0.0
IF (istat.NE.0) GOTO 9997
WRITE(*,'(a,e12.4,a)') "A single USMM took ",mmt," s"
WRITE(*,'(a,g11.4,a)')"USMM-to-USMV speed ratio is is ", mvt/mmt, "x"
print*,"Call auto-tuning routine.."
IF (res.NE.0) GOTO 9997
IF (istat.NE.0) GOTO 9997
IF (istat.NE.0) GOTO 9997
print*,"Repeat measurement."
y = 0.0
IF (istat.NE.0) GOTO 9997
WRITE(*,'(a,e12.4,a)') "Tuned USMM took ",omt," s"
WRITE(*,'(a,g11.4,a)')"Tuned-to-untuned speed ratio is is ",mmt/omt,"x"
GOTO 9998
9997 res = -1
9998 CONTINUE
IF (istat.NE.0) res = -1
USE iso_c_binding
IMPLICIT NONE
INTEGER :: passed = 0, failed = 0
INTEGER(KIND=RSB_IDX_KIND) :: res
TYPE(c_ptr),PARAMETER :: eo = c_null_ptr
TYPE(c_ptr),PARAMETER :: io = c_null_ptr
INTEGER(KIND=C_INT),TARGET::ione=1
IF (res.LT.0) failed = failed + 1
IF (res.EQ.0) passed = passed + 1
IF (res.LT.0) failed = failed + 1
IF (res.EQ.0) passed = passed + 1
print *, "FAILED:", failed
print *, "PASSED:", passed
IF (failed .GT. 0) THEN
stop 1
END IF
END PROGRAM
examples/fortran_rsb_fi.F90:
USE iso_c_binding
IMPLICIT NONE
INTEGER ::res
INTEGER,TARGET :: istat = 0, i
INTEGER(KIND=RSB_IDX_KIND) :: incx = 1, incy = 1
REAL(KIND=8),TARGET :: alpha = 3, beta = 1
INTEGER(KIND=RSB_IDX_KIND) :: nnz = 4
INTEGER(KIND=RSB_IDX_KIND) :: nr = 2
INTEGER(KIND=RSB_IDX_KIND) :: nc = 2
INTEGER(KIND=RSB_IDX_KIND) :: nrhs = 1
INTEGER(KIND=RSB_IDX_KIND),TARGET :: ia(4) = (/0, 1, 1,0/)
INTEGER(KIND=RSB_IDX_KIND),TARGET :: ja(4) = (/0, 0, 1,1/)
REAL(KIND=8),TARGET :: va(4) = (/1,1,1,1/)
REAL(KIND=8),TARGET :: x(2) = (/1, 1/)
REAL(KIND=8),TARGET :: cy(2) = (/9, 9/)
REAL(KIND=8),TARGET :: y(2) = (/3, 3/)
TYPE(c_ptr),TARGET :: mtxap = c_null_ptr
REAL(KIND=8) :: tmax = 2.0
INTEGER :: titmax = 2
INTEGER,TARGET :: ont = 0
TYPE(c_ptr),PARAMETER :: eo = c_null_ptr
TYPE(c_ptr),PARAMETER :: io = c_null_ptr
INTEGER,TARGET :: errval
res = 0
&,nnz,&
istat =
rsb_tune_spmm(c_loc(mtxap),c_null_ptr,c_null_ptr,titmax,&
& tmax,&
& transt,c_loc(alpha),c_null_ptr,nrhs,order,c_loc(x),nr,&
& c_loc(beta),c_loc(y),nc)
& tmax,&
& transt,c_loc(alpha),mtxap,nrhs,order,c_loc(x),nr,c_loc(beta),&
& c_loc(y),nc)
print *, "Optimal number of threads:", ont
y(:) = (/3, 3/)
istat =
rsb_spmv(transt,c_loc(alpha),mtxap,c_loc(x),incx,&
& c_loc(beta),c_loc(y),incy)
DO i = 1, 2
IF (y(i).NE.cy(i)) print *, "type=d dims=2x2 sym=g diag=g &
&blocks=1x1 usmv alpha= 3 beta= 1 incx=1 incy=1 trans=n is not ok"
IF (y(i).NE.cy(i)) GOTO 9997
END DO
print*,"type=d dims=2x2 sym=g diag=g blocks=1x1 usmv alpha= 3&
& beta= 1 incx=1 incy=1 trans=n is ok"
GOTO 9998
9997 res = -1
9998 CONTINUE
& stop "error calling rsb_lib_exit"
print *, "rsb module fortran test is ok"
USE iso_c_binding
IMPLICIT NONE
INTEGER,TARGET :: errval
INTEGER :: res
INTEGER(KIND=RSB_IDX_KIND) :: incx = 1, incb = 1
REAL(KIND=8),TARGET :: alpha = 3,beta = 1
INTEGER(KIND=RSB_IDX_KIND) :: nnza = 4, nra = 3, nca = 3
INTEGER(KIND=RSB_IDX_KIND),TARGET :: ia(4) = (/1, 2, 3, 3/)
INTEGER(KIND=RSB_IDX_KIND),TARGET :: ja(4) = (/1, 2, 1, 3/)
REAL(KIND=8),TARGET :: va(4) = (/11.0, 22.0, 13.0, 33.0/)
REAL(KIND=8),TARGET :: x(3) = (/ 0, 0, 0/)
REAL(KIND=8),TARGET :: b(3) = (/-1.0, -2.0, -2.0/)
TYPE(c_ptr),TARGET :: mtxap = c_null_ptr
TYPE(c_ptr) :: mtxapp = c_null_ptr
REAL(KIND=8),TARGET :: etime = 0.0
TYPE(c_ptr),PARAMETER :: eo = c_null_ptr
TYPE(c_ptr),PARAMETER :: io = c_null_ptr
& stop "error calling rsb_lib_init"
#if defined(__GNUC__) && (__GNUC__ == 4) && (__GNUC_MINOR__ < 5)
#define RSB_SKIP_BECAUSE_OLD_COMPILER 1
#endif
#ifndef RSB_SKIP_BECAUSE_OLD_COMPILER
& c_loc(errval))
& c_loc(va),c_loc(ia),c_loc(ja),nnza,flags)
mtxapp = c_loc(mtxap)
& stop "error calling rsb_mtx_set_vals"
& stop "error calling rsb_mtx_alloc_from_coo_end"
errval =
rsb_spmv(transt,c_loc(alpha),mtxap,c_loc(x),&
& incx,c_loc(beta),c_loc(b),incb)
& stop "error calling rsb_spmv"
& print*,"Time spent in librsb is:",etime
& stop "error calling rsb_mtx_free"
#else
print*,"You have an old Fortran compiler not supporting C_LOC."
print*,"Skipping a part of the test"
#endif
& stop "error calling rsb_lib_exit"
print *, "rsb module fortran test is ok"
res = errval
USE iso_c_binding
IMPLICIT NONE
INTEGER ::res
INTEGER,TARGET :: istat = 0, i
INTEGER(KIND=RSB_IDX_KIND) :: incx = 1, incy = 1
REAL(KIND=8),TARGET :: alpha = 4, beta = 1
INTEGER(KIND=RSB_IDX_KIND), PARAMETER :: nnz = 3
INTEGER(KIND=RSB_IDX_KIND), PARAMETER :: nr = 2
INTEGER(KIND=RSB_IDX_KIND), PARAMETER :: nc = 2
INTEGER(KIND=RSB_IDX_KIND), PARAMETER :: nrhs = 1
INTEGER(KIND=RSB_IDX_KIND),TARGET :: ip(3) = (/1, 2, 4/)
INTEGER(KIND=RSB_IDX_KIND),TARGET :: ja(3) = (/1, 1, 2/)
REAL(KIND=8),TARGET :: va(3) = (/11,21,22/)
REAL(KIND=8),TARGET :: x(2) = (/1, 2/)
REAL(KIND=8),TARGET :: cy(2) = (/215.0, 264.0/)
REAL(KIND=8),TARGET :: y(2) = (/3, 4/)
TYPE(c_ptr),TARGET :: mtxap = c_null_ptr
REAL(KIND=8) :: tmax = 2.0
INTEGER :: titmax = 2
INTEGER,TARGET :: ont = 0
TYPE(c_ptr),PARAMETER :: eo = c_null_ptr
TYPE(c_ptr),PARAMETER :: io = c_null_ptr
INTEGER,TARGET :: errval
& stop "error calling rsb_lib_init"
res = 0
istat =
rsb_tune_spmm(c_loc(mtxap),c_null_ptr,c_null_ptr,titmax,&
& tmax,&
& transt,c_loc(alpha),c_null_ptr,nrhs,order,c_loc(x),nr,&
& c_loc(beta),c_loc(y),nc)
& tmax,&
& transt,c_loc(alpha),mtxap,nrhs,order,c_loc(x),nr,c_loc(beta),&
& c_loc(y),nc)
print *, "Optimal number of threads:", ont
y(:) = (/3, 4/)
istat =
rsb_spmv(transt,c_loc(alpha),mtxap,c_loc(x),incx,&
& c_loc(beta),c_loc(y),incy)
print *, y
DO i = 1, 2
IF (y(i).NE.cy(i)) print *, "type=d dims=2x2 sym=s diag=g &
&blocks=1x1 usmv alpha= 4 beta= 1 incx=1 incy=1 trans=n is not ok"
IF (y(i).NE.cy(i)) GOTO 9997
END DO
print*,"type=d dims=2x2 sym=s diag=g blocks=1x1 usmv alpha= 4&
& beta= 1 incx=1 incy=1 trans=n is ok"
GOTO 9998
& stop "error calling rsb_lib_exit"
print *, "rsb module fortran test is ok"
9997 res = -1
9998 CONTINUE
IMPLICIT NONE
IF (res.LT.0) failed = failed + 1
IF (res.EQ.0) passed = passed + 1
IF (res.LT.0) failed = failed + 1
IF (res.EQ.0) passed = passed + 1
IF (res.LT.0) failed = failed + 1
IF (res.EQ.0) passed = passed + 1
print *, "FAILED:", failed
print *, "PASSED:", passed
IF (failed.GT.0) THEN
stop 1
END IF
END PROGRAM
examples/backsolve.c:
#include <stdio.h>
int main(
const int argc,
char *
const argv[])
{
const int brA = bs, bcA = bs;
struct rsb_mtx_t *mtxAp = NULL;
char ib[200];
int i;
printf("Hello, RSB!\n");
printf("Initializing the library...\n");
{
printf("Error initializing the library!\n");
goto err;
}
printf("Correctly initialized the library.\n");
{
printf("Error setting option!\n");
goto err;
}
VA,IA,JA,nnzA,typecode,nrA,ncA,brA,bcA,
, &errval);
{
printf("Error while allocating the matrix!\n");
goto err;
}
printf("Correctly allocated a matrix with %ld nonzeroes.\n",
(long int)nnzA);
printf("Summary information of the matrix:\n");
ib,sizeof(ib));
printf("%s",ib);
printf("\nMatrix printout:\n");
if((errval =
{
printf("Error performing a multiplication!\n");
goto err;
}
printf("\nWe have a unitary vector:\n");
printf("\nMultiplying matrix by unitary vector we get:\n");
NULL, NULL, nrA);
{
printf("Error performing autotuning!\n");
goto err;
}
{
printf("Error performing triangular solve!\n");
goto err;
}
printf("\nBacksolving we should get a unitary vector:\n");
for(i=0;i<nrA;++i)
if(X[i]!=one)
{
printf("Warning! Result vector not unitary!:\n");
goto err;
}
printf("All done.\n");
printf("Correctly freed the matrix.\n");
{
printf("Error finalizing the library!\n");
goto err;
}
printf("Correctly finalized the library.\n");
printf("Program terminating with no error.\n");
return EXIT_SUCCESS;
err:
printf("Program terminating with error.\n");
return EXIT_FAILURE;
}
examples/bench.sh:
#!/bin/bash
#
# Copyright (C) 2008-2021 Michele Martone
#
# This file is part of librsb.
#
# librsb is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# librsb is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
# License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with librsb; see the file COPYING.
# If not, see <http://www.gnu.org/licenses/>.
# Benchmark rsbench and postprocess results.
# \ingroup rsb_doc_examples
# @file
# @author Michele Martone
# @brief Benchmark invocation from shell script.
#
set -e
set -x
which rsbench
BRF=test.rpr
# invoke rsbench and produce a performance record, using all types and one thread
rsbench -oa -Ob --bench --lower 100 --as-symmetric \
--types ':' -n 1 \
--notranspose --compare-competitors \
--verbose --verbose \
--write-performance-record=${BRF}
# examine tuning renderings (produced by --verbose --verbose)
ls -ltr ${BRF/.rpr/}-tuning*
# convert the performance record to text form
rsbench --read-performance-record ${BRF} > ${BRF/rpr/txt}
ls -ltr ${BRF/rpr/txt}
# convert the performance record to LaTeX table document form
RSB_PR_WLTC=2 RSB_PR_SR=0 \
rsbench --read-performance-record ${BRF} > ${BRF/rpr/tex}
which latex || exit 0 # to compile LaTeX document
which kpsepath || exit 0 # to check LaTeX packages
find `kpsepath tex | sed 's/!!//g;s/:/\n/g;'` -name sciposter.cls || exit 0 # need sciposter class, usually in texlive-science
# convert the LaTeX table into a DVI (may as well use pdflatex for PDF)
latex -interaction=batchmode -file-line-error ${BRF/rpr/tex}
# convert the performance record to GNUPLOT plots
which gnuplot || exit 0
RSB_PRD_STYLE_PLT_PFN=${BRF/rpr/} RSB_PRD_STYLE_PLT_FMT=1 RSB_PR_SR=2 \
rsbench --read-performance-record ${BRF} > ${BRF/rpr/gnu}
# convert the GNUPLOT plots into PDF
ls -ltr ${BRF/rpr/gnu}
gnuplot ${BRF/rpr/gnu}
# convert the performance record to GNUPLOT plots, different way
RSB_PRD_STYLE_PLT_PFN=${BRF/rpr/} RSB_PRD_STYLE_PLT_FMT= RSB_PR_SR=2 \
rsbench --read-performance-record ${BRF} > ${BRF/rpr/gnu}
gnuplot ${BRF/rpr/gnu}
ls -ltr ${BRF/.rpr/}*.png
ls -ltr ${BRF/.rpr/}*.eps
ls -ltr ${BRF} ${BRF/rpr/tex} ${BRF/rpr/dvi} ${BRF/rpr/txt}
# clean up
rm ${BRF/rpr/}{aux,log,out,gnu}
rm ${BRF/.rpr/}*{.png,.eps}
rm ${BRF} ${BRF/rpr/tex} ${BRF/rpr/dvi} ${BRF/rpr/txt}
exit
Most of the snippets in the documentation come from examples/snippets.c.