Beispiel #1
0
template <typename T, typename MemoryBlock> void save(
    archive& ar
    , std::string const& path
    , alps::numeric::matrix<T, MemoryBlock> const& m
    , std::vector<std::size_t> size   = std::vector<std::size_t>()
                                        , std::vector<std::size_t> chunk  = std::vector<std::size_t>()
                                                , std::vector<std::size_t> offset = std::vector<std::size_t>()
) {
    using std::copy;
    using std::fill_n;
    using alps::cast;
    typedef typename alps::numeric::matrix<T,MemoryBlock>::const_col_element_iterator col_iterator;
    if (is_continuous<T>::value && m.empty())
        ar.write(path, static_cast<typename scalar_type<alps::numeric::matrix<T, MemoryBlock> >::type const *>(NULL), std::vector<std::size_t>());
    else if (is_continuous<T>::value) {
        std::vector<std::size_t> extent(get_extent(m));
        copy(extent.begin(),extent.end(), std::back_inserter(size));
        // We want to write one column:
        chunk.push_back(1);
        // How much memory does the column and the elements it contains need?
        copy(extent.begin()+1,extent.end(), std::back_inserter(chunk));
        std::size_t const offset_col_index = offset.size();
        fill_n(std::back_inserter(offset), extent.size(), 0);
        // Write column by column
        for(std::size_t j=0; j < num_cols(m); ++j) {
            offset[offset_col_index] = j;
            ar.write(path, get_pointer(*(col(m, j).first)), size, chunk, offset);
        }
    } else if (m.empty())
        ar.write(path, static_cast<int const *>(NULL), std::vector<std::size_t>());
    else if (is_vectorizable(m)) {
        size.push_back(num_cols(m));
        size.push_back(num_rows(m));
        // We want to write element by element:
        chunk.push_back(1);
        chunk.push_back(1);
        std::size_t const offset_col_index = offset.size();
        std::size_t const offset_row_index = offset.size()+1;
        offset.push_back(0);
        offset.push_back(0);
        for(std::size_t j=0; j < num_cols(m); ++j) {
            offset[offset_col_index] = j;
            for(std::size_t i=0; i< num_rows(m); ++i) {
                offset[offset_row_index] = i;
                save(ar, path, m(i,j), size, chunk, offset);
            }
        }
    } else {
        if( ar.is_data(path) )
            ar.delete_data(path);
        for(std::size_t j=0; j < num_cols(m); ++j)
            for(std::size_t i=0; i < num_rows(m); ++i)
                save(ar, ar.complete_path(path) + "/" + cast<std::string>(j) + "/" + cast<std::string>(i), m(i,j) );
    }
}
Beispiel #2
0
template <typename T, typename MemoryBlock> void load(
    archive & ar
    , std::string const & path
    , alps::numeric::matrix<T,MemoryBlock> & m
    , std::vector<std::size_t> chunk  = std::vector<std::size_t>()
                                        , std::vector<std::size_t> offset = std::vector<std::size_t>()
) {
    using std::copy;
    if(ar.is_data(path + "/size1") && ar.is_scalar(path + "/size1")) {
        // Old matrix hdf5 format
        std::size_t size1(0), size2(0), reserved_size1(0);
        ar[path + "/size1"] >> size1;
        ar[path + "/size2"] >> size2;
        ar[path + "/reserved_size1"] >> reserved_size1;
        std::vector<T> data;
        ar[path + "/values"] >> data;
        alps::numeric::matrix<T,MemoryBlock> m2(reserved_size1,size2);
        assert(m2.capacity().first  == reserved_size1);
        copy(data.begin(), data.end(), col(m2,0).first);
        m2.resize(size1,size2);
        swap(m, m2);
        return;
    }