Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

csc hdf5 blaze #544

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions phylanx/plugins/fileio/csc_read_hdf5.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (c) 2017 Alireza Kheirkhahan
// Copyright (c) 2018 Chris Taylor
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#pragma once
#ifndef __CSCREADHDF5__
#define __CSCREADHDF5__

#include <phylanx/config.hpp>

#if defined(PHYLANX_HAVE_HIGHFIVE)
#include <phylanx/execution_tree/primitives/base_primitive.hpp>
#include <phylanx/execution_tree/primitives/primitive_component_base.hpp>

#include <hpx/lcos/future.hpp>

#include <highfive/H5DataSet.hpp>
#include <highfive/H5DataSpace.hpp>
#include <highfive/H5File.hpp>

#include <blaze/Blaze.h>

#include <memory>
#include <string>
#include <utility>
#include <vector>

namespace phylanx { namespace execution_tree { namespace primitives
{
class csc_read_hdf5 : public primitive_component_base
{
public:
static match_pattern_type const match_data;

csc_read_hdf5() = default;

csc_read_hdf5(std::vector<primitive_argument_type>&& operands,
std::string const& name, std::string const& codename);

hpx::future<primitive_argument_type> eval(
std::vector<primitive_argument_type> const& args) const override;
};

inline primitive create_csc_read_hdf5(hpx::id_type const& locality,
std::vector<primitive_argument_type>&& operands,
std::string const& name = "", std::string const& codename = "")
{
return create_primitive_component(
locality, "csc_read_hdf5", std::move(operands), name, codename);
}
}}}

#endif
#endif
55 changes: 55 additions & 0 deletions phylanx/plugins/fileio/csc_write_hdf5.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (c) 2017 Alireza Kheirkhahan
// Copyright (c) 2018 Chris Taylor
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#pragma once
#ifndef __CSCWRITEHDF5__
#define __CSCWRITEHDF5__

#include <phylanx/config.hpp>

#if defined(PHYLANX_HAVE_HIGHFIVE)
#include <phylanx/execution_tree/primitives/base_primitive.hpp>
#include <phylanx/execution_tree/primitives/primitive_component_base.hpp>

#include <hpx/lcos/future.hpp>

#include <highfive/H5DataSet.hpp>
#include <highfive/H5DataSpace.hpp>
#include <highfive/H5File.hpp>

#include <blaze/Blaze.h>

#include <memory>
#include <string>
#include <utility>
#include <vector>

namespace phylanx { namespace execution_tree { namespace primitives
{
class csc_write_hdf5 : public primitive_component_base
{
public:
static match_pattern_type const match_data;

csc_write_hdf5() = default;

csc_write_hdf5(std::vector<primitive_argument_type>&& operands,
std::string const& name, std::string const& codename);

hpx::future<primitive_argument_type> eval(
std::vector<primitive_argument_type> const& args) const override;
};

inline primitive create_csc_write_hdf5(hpx::id_type const& locality,
std::vector<primitive_argument_type>&& operands,
std::string const& name = "", std::string const& codename = "")
{
return create_primitive_component(
locality, "csc_write_hdf5", std::move(operands), name, codename);
}
}}}

#endif
#endif
2 changes: 2 additions & 0 deletions phylanx/plugins/fileio/fileio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include <phylanx/plugins/fileio/file_write.hpp>
#include <phylanx/plugins/fileio/file_write_csv.hpp>
#include <phylanx/plugins/fileio/file_write_hdf5.hpp>
#include <phylanx/plugins/fileio/csc_read_hdf5.hpp>
#include <phylanx/plugins/fileio/csc_write_hdf5.hpp>

#endif

Expand Down
4 changes: 3 additions & 1 deletion src/plugins/fileio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ if(PHYLANX_WITH_HIGHFIVE)
set(headers ${headers}
"${PROJECT_SOURCE_DIR}/phylanx/plugins/fileio/file_read_hdf5.hpp"
"${PROJECT_SOURCE_DIR}/phylanx/plugins/fileio/file_write_hdf5.hpp"
"${PROJECT_SOURCE_DIR}/phylanx/plugins/fileio/csc_read_hdf5.hpp"
"${PROJECT_SOURCE_DIR}/phylanx/plugins/fileio/csc_write_hdf5.hpp"
)
set(sources ${sources} "file_read_hdf5.cpp" "file_write_hdf5.cpp")
set(sources ${sources} "file_read_hdf5.cpp" "file_write_hdf5.cpp" "csc_read_hdf5.cpp" "csc_write_hdf5.cpp")
endif()

add_phylanx_primitive_plugin(fileio
Expand Down
143 changes: 143 additions & 0 deletions src/plugins/fileio/csc_read_hdf5.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
// Copyright (c) 2018 Alireza Kheirkhahan
// Copyright (c) 2018 Chris Taylor
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#include <phylanx/config.hpp>

#if defined(PHYLANX_HAVE_HIGHFIVE)
#include <phylanx/ir/node_data.hpp>
#include <phylanx/plugins/fileio/csc_read_hdf5.hpp>

#include <hpx/include/lcos.hpp>
#include <hpx/include/naming.hpp>
#include <hpx/include/util.hpp>
#include <hpx/throw_exception.hpp>

#include <phylanx/util/detail/blaze-highfive.hpp>
#include <blaze/Blaze.h>
#include <highfive/H5File.hpp>
#include <highfive/H5DataSet.hpp>
#include <highfive/H5DataSpace.hpp>

#include <cstddef>
#include <fstream>
#include <memory>
#include <string>
#include <vector>
#include <utility>

///////////////////////////////////////////////////////////////////////////////
namespace phylanx { namespace execution_tree { namespace primitives
{

template<typename ValueType, bool SO>
void marshal(HighFive::File & file
, std::string const& hdf5_group_name
, blaze::CompressedMatrix<ValueType, SO> & data);

template<typename ValueType>
void marshal(HighFive::File & file
, std::string const& hdf5_group_name
, blaze::CompressedMatrix<ValueType, blaze::columnMajor> & data) {

using namespace HighFive;

Group grp = file.getGroup(hdf5_group_name);
std::string const desc("csc");

std::string const fmtkey("h5sparse_format");
Attribute fmt = grp.getAttribute(fmtkey);
std::string smat_fmt;
fmt.read(smat_fmt);

if(smat_fmt.compare(desc) == 0) {
//TODO: how does error handling work?
}

std::string const shapekey("h5sparse_shape");
std::vector< std::int64_t > dims(2);
Attribute shape = grp.getAttribute(shapekey);
shape.read(dims);

data.resize(dims[0], dims[1], false);

std::string const indiceskey("indices");
DataSet dset_indices = grp.getDataSet(indiceskey);
std::string const datakey("data");
DataSet dset_data = grp.getDataSet(datakey);
std::string const indptrkey("indptr");
DataSet dset_indptr = grp.getDataSet(indptrkey);

std::vector<ValueType> values;
std::vector<std::int64_t> indices;
std::vector<std::int64_t> indptr;

dset_data.read(values);
dset_indices.read(indices);
dset_indptr.read(indptr);

for(std::int64_t i = 0; i < dims[1]; ++i) {
for(std::int64_t j = indptr[i]; j < indptr[i+1]; ++j) {
data(i, indices[j]) = values[j];
}
}
}

///////////////////////////////////////////////////////////////////////////
match_pattern_type const csc_read_hdf5::match_data =
{
hpx::util::make_tuple("csc_read_hdf5",
std::vector<std::string>{"csc_read_hdf5(_1, _2, _3)"},
&create_csc_read_hdf5, &create_primitive<csc_read_hdf5>)
};

///////////////////////////////////////////////////////////////////////////
csc_read_hdf5::csc_read_hdf5(
std::vector<primitive_argument_type> && operands,
std::string const& name, std::string const& codename)
: primitive_component_base(std::move(operands), name, codename)
{
}

hpx::future<primitive_argument_type> csc_read_hdf5::eval(
std::vector<primitive_argument_type> const& args) const
{
if (operands_.size() != 2)
{
HPX_THROW_EXCEPTION(hpx::bad_parameter,
"phylanx::execution_tree::primitives::file_read_hdf5::eval",
util::generate_error_message(
"the file_read_hdf5 primitive requires exactly two "
"literal arguments",
name_, codename_));
}

if (!valid(operands_[0]) || !valid(operands_[1]))
{
HPX_THROW_EXCEPTION(hpx::bad_parameter,
"phylanx::execution_tree::primitives::file_read_hdf5::eval",
util::generate_error_message(
"the file_read_hdf5 primitive requires that the given "
"operand is valid",
name_, codename_));
}

std::string filename =
string_operand_sync(operands_[0], args, name_, codename_);
std::string datasetName =
string_operand_sync(operands_[1], args, name_, codename_);

HighFive::File infile(filename, HighFive::File::ReadOnly);
// matrix
blaze::CompressedMatrix<double, blaze::columnMajor> matrix;
marshal(infile, datasetName, matrix);

return hpx::make_ready_future(primitive_argument_type{
ir::node_data< blaze::CompressedMatrix<double, blaze::columnMajor> >{std::move(matrix)}});
};
}
}}}

#endif
Loading