Skylark (Sketching Library)  0.1
/var/lib/jenkins/jobs/Skylark/workspace/tests/unit/SerializationTest.cpp
Go to the documentation of this file.
00001 #include <vector>
00002 
00003 #include <boost/mpi.hpp>
00004 #include <boost/test/minimal.hpp>
00005 #include <boost/property_tree/ptree.hpp>
00006 #include <boost/property_tree/json_parser.hpp>
00007 
00008 //#include "../../sketch/CT.hpp"
00009 //#include "../../sketch/CWT.hpp"
00010 #include "../../sketch/sketch.hpp"
00011 
00012 // TODO: Are these includes really needed?
00013 #include <elemental.hpp>
00014 #include <skylark.hpp>
00015 #include "../../sketch/CT.hpp"
00016 #include "../../sketch/CWT.hpp"
00017 
00018 #include "../../base/context.hpp"
00019 
00020 int test_main(int argc, char *argv[]) {
00021 
00023     //[> Parameters <]
00024     const size_t n   = 10;
00025     const size_t m   = 5;
00026     const size_t n_s = 6;
00027     const size_t m_s = 3;
00028 
00029     const int seed = static_cast<int>(rand() * 100);
00030 
00031     typedef FullyDistVec<size_t, double> mpi_vector_t;
00032     typedef SpDCCols<size_t, double> col_t;
00033     typedef SpParMat<size_t, double, col_t> DistMatrixType;
00034 
00035     namespace mpi = boost::mpi;
00036     mpi::environment env(argc, argv);
00037     mpi::communicator world;
00038     const size_t rank = world.rank();
00039     skylark::base::context_t context (seed);
00040 
00041     double count = 1.0;
00042 
00043     const size_t matrix_full = n * m;
00044     mpi_vector_t colsf(matrix_full);
00045     mpi_vector_t rowsf(matrix_full);
00046     mpi_vector_t valsf(matrix_full);
00047 
00048     for(size_t i = 0; i < matrix_full; ++i) {
00049         colsf.SetElement(i, i % m);
00050         rowsf.SetElement(i, i / m);
00051         valsf.SetElement(i, count);
00052         count++;
00053     }
00054 
00055     DistMatrixType A(n, m, rowsf, colsf, valsf);
00056 
00058     //[> Setup test <]
00059 
00060     //[> 1. Create the sketching matrix and dump JSON <]
00061     skylark::sketch::CWT_t<DistMatrixType, DistMatrixType>
00062         Sparse(n, n_s, context);
00063 
00064     // dump to property tree
00065     boost::property_tree::ptree pt = Sparse.get_data()->to_ptree();
00066 
00067     //[> 2. Dump the JSON string to file <]
00068     std::ofstream out("sketch.json");
00069     write_json(out, pt);
00070     out.close();
00071 
00072     //[> 3. Create a sketch from the JSON file. <]
00073     std::ifstream file;
00074     std::stringstream json;
00075     file.open("sketch.json", std::ios::in);
00076 
00077     boost::property_tree::ptree json_tree;
00078     boost::property_tree::read_json(file, json_tree);
00079 
00080     skylark::sketch::CWT_t<DistMatrixType, DistMatrixType> tmp(json_tree);
00081 
00082     //[> 4. Both sketches should compute the same result. <]
00083     mpi_vector_t zero;
00084     DistMatrixType sketch_A(n_s, m, zero, zero, zero);
00085     DistMatrixType sketch_Atmp(n_s, m, zero, zero, zero);
00086 
00087     Sparse.apply(A, sketch_A, skylark::sketch::columnwise_tag());
00088     tmp.apply(A, sketch_Atmp, skylark::sketch::columnwise_tag());
00089 
00090     if (!static_cast<bool>(sketch_A == sketch_Atmp))
00091         BOOST_FAIL("Applied sketch did not result in same result");
00092 
00093     return 0;
00094 }