Skylark (Sketching Library)  0.1
/var/lib/jenkins/jobs/Skylark/workspace/tests/unit/SparseSketchApplyElementalTest.cpp
Go to the documentation of this file.
00001 
00013 #include <vector>
00014 
00015 #include <elemental.hpp>
00016 
00017 #include "../../utility/distributions.hpp"
00018 #include "../../sketch/sketch.hpp"
00019 
00020 #include <boost/mpi.hpp>
00021 #include <boost/test/minimal.hpp>
00022 
00023 template < typename InputMatrixType,
00024            typename OutputMatrixType = InputMatrixType >
00025 struct Dummy_t : public skylark::sketch::hash_transform_t<
00026     InputMatrixType, OutputMatrixType,
00027     boost::random::uniform_int_distribution,
00028     skylark::utility::rademacher_distribution_t > {
00029 
00030     typedef skylark::sketch::hash_transform_t<
00031         InputMatrixType, OutputMatrixType,
00032         boost::random::uniform_int_distribution,
00033         skylark::utility::rademacher_distribution_t >
00034             hash_t;
00035 
00036     Dummy_t(int N, int S, skylark::base::context_t& context)
00037         : skylark::sketch::hash_transform_t<InputMatrixType, OutputMatrixType,
00038           boost::random::uniform_int_distribution,
00039           skylark::utility::rademacher_distribution_t>(N, S, context)
00040     {}
00041 
00042     std::vector<size_t> getRowIdx() { return hash_t::row_idx; }
00043     std::vector<double> getRowValues() { return hash_t::row_value; }
00044 };
00045 
00046 int test_main(int argc, char *argv[]) {
00047 
00049     //[> Parameters <]
00050 
00051     //FIXME: use random sizes?
00052     const size_t n   = 10;
00053     const size_t m   = 5;
00054     const size_t n_s = 6;
00055     const size_t m_s = 3;
00056 
00058     //[> Setup test <]
00059     namespace mpi = boost::mpi;
00060 
00061 #ifdef SKYLARK_HAVE_OPENMP
00062     int provided;
00063     MPI_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE, &provided);
00064 #endif
00065     typedef elem::Matrix<double> MatrixType;
00066     typedef elem::DistMatrix<double, elem::VR, elem::STAR> DistMatrixType;
00067 
00068     mpi::environment env(argc, argv);
00069     mpi::communicator world;
00070 
00071     elem::Initialize(argc, argv);
00072     MPI_Comm mpi_world(world);
00073     elem::Grid grid(mpi_world);
00074 
00075     skylark::base::context_t context (0);
00076 
00077     double count = 1.0;
00078     elem::DistMatrix<double, elem::VR, elem::STAR> A(grid);
00079     elem::Uniform (A, n, m);
00080     for( size_t j = 0; j < A.Height(); j++ )
00081         for( size_t i = 0; i < A.Width(); i++ )
00082             A.Set(j, i, count++);
00083 
00084 
00086     //[> Column wise application <]
00087 
00088     /* 1. Create the sketching matrix */
00089     Dummy_t<DistMatrixType, MatrixType> Sparse(n, n_s, context);
00090     std::vector<size_t> row_idx    = Sparse.getRowIdx();
00091     std::vector<double> row_val = Sparse.getRowValues();
00092 
00093     // PI generated by random number gen
00094     elem::DistMatrix<double, elem::VR, elem::STAR> pi_sketch(grid);
00095     elem::Uniform(pi_sketch, n_s, n);
00096     elem::Zero(pi_sketch);
00097     for(size_t i = 0; i < row_idx.size(); ++i)
00098         pi_sketch.Set(row_idx[i], i, row_val[i]);
00099 
00100     /* 2. Create space for the sketched matrix */
00101     MatrixType sketch_A(n_s, m);
00102 
00103     /* 3. Apply the transform */
00104     Sparse.apply(A, sketch_A, skylark::sketch::columnwise_tag());
00105 
00106     /* 4. Build structure to compare */
00107     elem::DistMatrix<double, elem::VR, elem::STAR> expected_A(grid);
00108     elem::Uniform (expected_A, n_s, m);
00109     elem::Gemm(elem::NORMAL, elem::NORMAL,
00110                1.0, pi_sketch.LockedMatrix(), A.LockedMatrix(),
00111                0.0, expected_A.Matrix());
00112 
00113     for(size_t j = 0; j < sketch_A.Height(); j++ )
00114         for(size_t i = 0; i < sketch_A.Width(); i++ )
00115             if(sketch_A.Get(j, i) != expected_A.Get(j,i)) {
00116                 std::cerr << sketch_A.Get(j, i)  << " != "
00117                           << expected_A.Get(j, i) << std::endl;
00118                 BOOST_FAIL("Result of colwise application not as expected");
00119             }
00120 
00121 
00122 
00124     //[> Row wise application <]
00125 
00126     //[> 1. Create the sketching matrix <]
00127     Dummy_t<DistMatrixType, MatrixType> Sparse_r (m, m_s, context);
00128     row_idx.clear(); row_val.clear();
00129     row_idx = Sparse_r.getRowIdx();
00130     row_val = Sparse_r.getRowValues();
00131 
00132     // PI^T generated by random number gen
00133     elem::DistMatrix<double, elem::VR, elem::STAR> pi_sketch_r(grid);
00134     elem::Uniform(pi_sketch_r, m, m_s);
00135     elem::Zero(pi_sketch_r);
00136     for(size_t i = 0; i < row_idx.size(); ++i)
00137         pi_sketch_r.Set(i, row_idx[i], row_val[i]);
00138 
00139     //[> 2. Create space for the sketched matrix <]
00140     MatrixType sketch_A_r(n, m_s);
00141 
00142     //[> 3. Apply the transform <]
00143     Sparse_r.apply (A, sketch_A_r, skylark::sketch::rowwise_tag());
00144 
00145     /* 4. Build structure to compare */
00146     elem::DistMatrix<double, elem::VR, elem::STAR> expected_AR(grid);
00147     elem::Uniform (expected_AR, n, m_s);
00148     elem::Gemm(elem::NORMAL, elem::NORMAL,
00149                1.0, A.LockedMatrix(), pi_sketch_r.Matrix(),
00150                0.0, expected_AR.Matrix());
00151 
00152     for(size_t j = 0; j < sketch_A_r.Height(); j++ )
00153         for(size_t i = 0; i < sketch_A_r.Width(); i++ )
00154             if(sketch_A_r.Get(j, i) != expected_AR.Get(j,i)) {
00155                 std::cerr << sketch_A_r.Get(j, i)  << " != "
00156                           << expected_AR.Get(j, i) << std::endl;
00157                 BOOST_FAIL("Result of rowwise application not as expected");
00158             }
00159 
00160 
00161     elem::Finalize();
00162     return 0;
00163 }