|
| 1 | + |
| 2 | +#include <cstddef> |
| 3 | +#include <cstring> |
| 4 | + |
| 5 | +#include <graphblas.hpp> |
| 6 | +#include <graphblas/utils/parser.hpp> |
| 7 | +#include <graphblas/algorithms/matrix_factory.hpp> |
| 8 | + |
| 9 | +#include <assert.h> |
| 10 | + |
| 11 | + |
| 12 | +constexpr size_t max_fn_size = 255; |
| 13 | +typedef char Filename[ max_fn_size ]; |
| 14 | + |
| 15 | +void hello_world( const Filename &in, int &out ) { |
| 16 | + grb::Vector< bool > x( 497 ), y( 497, 1 ); |
| 17 | + grb::Matrix< double > A( 497, 497, 1727 ); |
| 18 | + |
| 19 | + grb::RC rc = grb::set( x, false ); |
| 20 | + rc = rc ? rc : grb::setElement( y, true, 200 ); |
| 21 | + if( rc != grb::SUCCESS ) { |
| 22 | + out = 10; |
| 23 | + return; |
| 24 | + } |
| 25 | + |
| 26 | + std::cout << "elements in x: " << grb::nnz( x ) << "\n"; |
| 27 | + std::cout << "elements in y: " << grb::nnz( y ) << "\n"; |
| 28 | + std::cout << "cacacity of y: " << grb::capacity( y ) << "\n"; |
| 29 | + |
| 30 | + for( const auto &pair : y ) { |
| 31 | + std::cout << "y[ " << pair.first << " ] = " << pair.second << "\n"; |
| 32 | + } |
| 33 | + |
| 34 | + size_t x_nnz = 0; |
| 35 | + for( const auto &pair : x ) { |
| 36 | + (void) ++x_nnz; |
| 37 | + if( pair.second ) { |
| 38 | + std::cerr << "x[ " << pair.first << " ] reads true " |
| 39 | + << "but false was expected!\n"; |
| 40 | + out = 20; |
| 41 | + return; |
| 42 | + } |
| 43 | + } |
| 44 | + if( x_nnz != 497 ) { |
| 45 | + std::cerr << "Output iterator of x retrieved " << x_nnz << " elements, " |
| 46 | + << "expected 497!\n"; |
| 47 | + out = 30; |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + grb::utils::MatrixFileReader< double > parser( in, true ); |
| 52 | + if( parser.m() != 497 || parser.n() != 497 ) { |
| 53 | + std::cerr << in << " corresponds to a matrix of unexpected size\n"; |
| 54 | + out = 40; |
| 55 | + return; |
| 56 | + } |
| 57 | + { |
| 58 | + const auto iterator = parser.begin(); |
| 59 | + std::cout << "First parsed entry: ( " << iterator.i() << ", " << iterator.j() << " ) = " << iterator.v() << "\n"; |
| 60 | + } |
| 61 | + |
| 62 | + rc = grb::buildMatrixUnique( |
| 63 | + A, |
| 64 | + parser.begin( grb::SEQUENTIAL ), parser.end( grb::SEQUENTIAL ), |
| 65 | + grb::SEQUENTIAL |
| 66 | + ); |
| 67 | + if( rc != grb::SUCCESS ) { |
| 68 | + std::cerr << "Error encountered during reading " << in << "\n"; |
| 69 | + out = 50; |
| 70 | + return; |
| 71 | + } |
| 72 | + std::cout << "nonzeroes in A: " << grb::nnz( A ) << "\n"; |
| 73 | + |
| 74 | + // exercise 6 |
| 75 | + grb::Matrix< double > B( 497, 497 ), C( 497, 497 ); |
| 76 | + rc = grb::set( B, A, grb::RESIZE ); |
| 77 | + rc = rc ? rc : grb::set( B, A, grb::EXECUTE ); |
| 78 | + rc = rc ? rc : grb::set( C, B, A, grb::RESIZE ); |
| 79 | + rc = rc ? rc : grb::set( C, B, A, grb::EXECUTE ); |
| 80 | + if( rc != grb::SUCCESS ) { |
| 81 | + std::cerr << "Error during computing solution to ex. 6\n"; |
| 82 | + out = 60; |
| 83 | + return; |
| 84 | + } |
| 85 | + std::cout << "capacity of C: " << grb::capacity( C ) << "\n"; |
| 86 | + std::cout << "number of elements in C (==number of nonzeroes in A): " |
| 87 | + << grb::nnz( C ) << "\n"; |
| 88 | + assert( grb::nnz(A) >= grb::nnz(C) ); |
| 89 | + std::cout << "number of explicit (numerical) zeroes in A: " << |
| 90 | + (grb::nnz(A) - grb::nnz(C)) << "\n"; |
| 91 | + |
| 92 | + // exercise 7 |
| 93 | + size_t nzs = 0; |
| 94 | + grb::Matrix< bool > mask = |
| 95 | + grb::algorithms::matrices< double >::eye( 497, 497, true, 1 ); |
| 96 | + rc = grb::set( B, mask1, A ); |
| 97 | + rc = rc ? rc : grb::set( C, B, A ); |
| 98 | + // note that capacities of B and C are guaranteed sufficient in the above |
| 99 | + if( rc == grb::SUCCESS ) { nzs += grb::nnz( B ) - grb::nnz( C ); } |
| 100 | + mask = grb::algorithms::matrices< double >::eye( 497, 497, true, 0 ); |
| 101 | + rc = rc ? rc : grb::set( B, mask2, A ); |
| 102 | + rc = rc ? rc : grb::set( C, B, A ); |
| 103 | + if( rc == grb::SUCCESS ) { nzs += grb::nnz( B ) - grb::nnz( C ); } |
| 104 | + mask = grb::algorithms::matrices< double >::eye( 497, 497, true, -1 ); |
| 105 | + rc = rc ? rc : grb::set( B, mask3, A ); |
| 106 | + rc = rc ? rc : grb::set( C, B, A ); |
| 107 | + if( rc == grb::SUCCESS ) { nzs += grb::nnz( B ) - grb::nnz( C ); } else { |
| 108 | + std::cerr << "Error during computing solution to ex. 7\n"; |
| 109 | + out = 70; |
| 110 | + return; |
| 111 | + } |
| 112 | + std::cout << "Number of explicit (numerical) zeroes in the subdiagonal, " |
| 113 | + << "diagonal, and superdiagonal of A combined: " << nzs << "\n"; |
| 114 | + |
| 115 | + // all OK |
| 116 | + out = 0; |
| 117 | +} |
| 118 | + |
| 119 | +int main( int argc, char ** argv ) { |
| 120 | + if( argc < 2 || argc > 2 ) { |
| 121 | + std::cout << "Usage: " << argv[ 0 ] << " </path/to/west0497.mtx>\n"; |
| 122 | + return 0; |
| 123 | + } |
| 124 | + |
| 125 | + // get input |
| 126 | + Filename fn; |
| 127 | + (void) std::strncpy( fn, argv[ 1 ], max_fn_size ); |
| 128 | + |
| 129 | + // set up output field |
| 130 | + int error_code = 100; |
| 131 | + |
| 132 | + // launch hello world program |
| 133 | + grb::Launcher< grb::AUTOMATIC > launcher; |
| 134 | + assert( launcher.exec( &hello_world, fn, error_code, true ) |
| 135 | + == grb::SUCCESS ); |
| 136 | + |
| 137 | + // return with the hello_world error code |
| 138 | + return error_code; |
| 139 | +} |
| 140 | + |
0 commit comments