Skylark (Sketching Library)  0.1
/var/lib/jenkins/jobs/Skylark/workspace/utility/external/uniform_matrix.hpp
Go to the documentation of this file.
00001 #ifndef SKYLARK_UNIFORM_MATRIX_HPP
00002 #define SKYLARK_UNIFORM_MATRIX_HPP
00003 
00004 #include "../../config.h"
00005 #include "../../base/context.hpp"
00006 
00007 #if SKYLARK_HAVE_COMBBLAS
00008 #include <CombBLAS.h>
00009 #endif
00010 
00011 #if SKYLARK_HAVE_ELEMENTAL
00012 #include <elemental.hpp>
00013 #endif
00014 
00015 namespace skylark { namespace utility {
00016 
00017 
00022 template <typename MatrixOrVectorType>
00023 struct uniform_matrix_t {};
00024 
00025 #if SKYLARK_HAVE_COMBBLAS && SKYLARK_HAVE_BOOST
00026 
00030 template <typename IndexType,
00031           typename ValueType>
00032 struct uniform_matrix_t <FullyDistVec<IndexType, ValueType> > {
00033     typedef ValueType value_t;
00034     typedef IndexType index_t;
00035     typedef FullyDistVec<IndexType,ValueType> mpi_vector_t;
00036     typedef uniform_distribution_t<ValueType> distribution_t;
00037 
00038     static mpi_vector_t generate (index_t& M,
00039         skylark::base::context_t& context) {
00040 
00041         /* Create a dummy vector */
00042         mpi_vector_t x(M, 0);
00043 
00044         distribution_t distribution;
00045         random_samples_array_t <distribution_t> samples =
00046             context.allocate_random_samples_array(x.TotalLength(), distribution);
00047 
00048         /* Iterate and fill up the local entries */
00049         for (index_t i=0; i<x.TotalLength(); ++i) {
00050             value_t sample;
00051                 sample = samples[i];
00052                 x.SetElement(i, sample);
00053         }
00054         return x;
00055     }
00056 };
00057 
00058 
00059 template <typename IndexType,
00060           typename ValueType>
00061 struct uniform_matrix_t <FullyDistMultiVec<IndexType, ValueType> > {
00062     typedef ValueType value_t;
00063     typedef IndexType index_t;
00064     typedef FullyDistVec<IndexType,ValueType> mpi_vector_t;
00065     typedef FullyDistMultiVec<IndexType,ValueType> mpi_multi_vector_t;
00066 
00067     static mpi_multi_vector_t generate (index_t M,
00068         index_t N,
00069         skylark::base::context_t& context) {
00070         /* Create an empty multi-vector */
00071         mpi_multi_vector_t X(M/*dimension*/, N/*number of vectors*/);
00072 
00073         /* Just pass each individual vector to the uniform_matrix_t above */
00074         for (index_t i=0; i<X.size; ++i)
00075             X[i] = uniform_matrix_t<mpi_vector_t>::generate(M, context);
00076         return X;
00077     }
00078 };
00079 
00080 
00081 template <typename IndexType,
00082           typename ValueType>
00083 struct uniform_matrix_t <SpParMat<IndexType,
00084                                   ValueType,
00085                                   SpDCCols<IndexType, ValueType> > > {
00086     typedef IndexType index_t;
00087     typedef ValueType value_t;
00088     typedef SpDCCols<index_t,value_t> seq_matrix_t;
00089     typedef SpParMat<index_t,value_t, seq_matrix_t> mpi_matrix_t;
00090     typedef FullyDistVec<IndexType,ValueType> mpi_value_vector_t;
00091     typedef FullyDistVec<IndexType,IndexType> mpi_index_vector_t;
00092     typedef uniform_distribution_t<bool> distribution_t;
00093 
00094     static mpi_matrix_t generate(index_t M,
00095         index_t N,
00096         index_t NNZ,
00097         skylark::base::context_t& context) {
00098         /* Create three FullyDistVec for colid, rowid, and values */
00099         mpi_value_vector_t values =
00100             uniform_matrix_t<mpi_value_vector_t>::generate(NNZ, context);
00101         mpi_index_vector_t col_id(NNZ, 0);
00102         mpi_index_vector_t row_id(NNZ, 0);
00103 
00104         /* Add edges carefully */
00105         index_t total_num_edges_added = 0;
00106         distribution_t distribution;
00107         random_samples_array_t <distribution_t> samples =
00108             context.allocate_random_samples_array(M * N, distribution);
00109         for (index_t j=0; j<N; ++j) {
00110             for (index_t i=0; i<M; ++i) {
00111                 bool sample;
00112                 sample = samples[j * M + i];
00113                 if (sample) {
00114                     col_id.SetElement(total_num_edges_added, j);
00115                     row_id.SetElement(total_num_edges_added, i);
00116                     ++total_num_edges_added;
00117                     if (NNZ==total_num_edges_added) break;
00118                 }
00119             }
00120             if (NNZ==total_num_edges_added) break;
00121         }
00122         return mpi_matrix_t (M, N, row_id, col_id, values);
00123     }
00124 };
00125 
00126 #endif // SKYLARK_HAVE_COMBBLAS and SKYLARK_HAVE_BOOST
00127 
00128 #if SKYLARK_HAVE_ELEMENTAL && SKYLARK_HAVE_BOOST
00129 
00130 template <typename ValueType>
00131 struct uniform_matrix_t <elem::Matrix<ValueType> > {
00132     typedef int index_t;
00133     typedef ValueType value_t;
00134     typedef elem::Matrix<ValueType> matrix_t;
00135     typedef uniform_distribution_t<value_t> distribution_t;
00136 
00137     static matrix_t generate (index_t M,
00138         index_t N,
00139         skylark::base::context_t& context) {
00140 
00141         matrix_t A(M, N);
00142         distribution_t distribution;
00143         random_samples_array_t <distribution_t> samples =
00144             context.allocate_random_samples_array(M * N, distribution);
00145         for (index_t j = 0; j < N; j++) {
00146             for (index_t i = 0; i < M; i++) {
00147                 value_t sample;
00148                 sample = samples[j * M + i];
00149                 A.Set(i, j, sample);
00150             }
00151         }
00152         return A;
00153     }
00154 };
00155 
00156 
00157 template <typename ValueType,
00158           elem::Distribution CD,
00159           elem::Distribution RD>
00160 struct uniform_matrix_t <elem::DistMatrix<ValueType, CD, RD> > {
00161     typedef int index_t;
00162     typedef ValueType value_t;
00163     typedef elem::DistMatrix<ValueType, CD, RD> mpi_matrix_t;
00164     typedef uniform_distribution_t<value_t> distribution_t;
00165 
00166     static mpi_matrix_t generate (index_t M,
00167         index_t N,
00168         const elem::Grid& grid,
00169         skylark::base::context_t& context) {
00170 
00171         mpi_matrix_t A(M, N, grid);
00172         distribution_t distribution;
00173         random_samples_array_t <distribution_t> samples =
00174             context.allocate_random_samples_array(M * N, distribution);
00175         for (index_t j = 0; j < N; j++) {
00176             for (index_t i = 0; i < M; i++) {
00177                 value_t sample = samples[j * M + i];
00178                 A.Set(i, j, sample);
00179             }
00180         }
00181         return A;
00182     }
00183 };
00184 
00185 #endif 
00187 } } 
00189 #endif // SKYLARK_UNIFORM_MATRIX_HPP