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

CSR binary loader/convertor #9

Open
wants to merge 2 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
3 changes: 2 additions & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# begin /* Add examples' subdirectories */
add_subdirectory(sssp)
# add_subdirectory(sssp)
# add_subdirectory(bfs)
# add_subdirectory(color)
add_subdirectory(mtx2bin)
# end /* Add examples' subdirectories */
23 changes: 23 additions & 0 deletions examples/mtx2bin/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# begin /* Set the application name. */
set(APPLICATION_NAME mtx2bin)
# end /* Set the application name. */

set (MTX_SOURCE_FILES ${MTX_SOURCE_DIR}/mmio.cpp)

# begin /* Add CUDA executables */
add_executable(${APPLICATION_NAME})

target_include_directories(${APPLICATION_NAME} PRIVATE
${MODERNGPU_INCLUDE_DIR}
${RAPIDJSON_INCLUDE_DIRS}
${MTX_INCLUDE_DIR}
)

set(SOURCE_LIST
${APPLICATION_NAME}.cu
${MTX_SOURCE_FILES}
)
target_sources(${APPLICATION_NAME} PRIVATE ${SOURCE_LIST})
target_link_libraries(${APPLICATION_NAME} PRIVATE essentials)
message("-- Example Added: ${APPLICATION_NAME}")
# end /* Add CUDA executables */
73 changes: 73 additions & 0 deletions examples/mtx2bin/mtx2bin.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include <cstdlib> // EXIT_SUCCESS

#include <gunrock/applications/application.hxx>

using namespace gunrock;
using namespace memory;

void test_read_binary(int num_arguments, char** argument_array) {
if (num_arguments != 3) {
std::cerr << "usage: ./bin/mtx2binary <inpath> <outpath>" << std::endl;
exit(1);
}

// --
// Define types

using vertex_t = int;
using edge_t = int;
using weight_t = float;

// --
// IO

std::string inpath = argument_array[1];
std::string outpath = argument_array[2];

io::matrix_market_t<vertex_t, edge_t, weight_t> mm;

using csr_t = format::csr_t<memory::memory_space_t::device, vertex_t, edge_t, weight_t>;
csr_t csr0;
csr0.from_coo(mm.load(inpath));

std::cout << "csr0.number_of_rows = " << csr0.number_of_rows << std::endl;
std::cout << "csr0.number_of_columns = " << csr0.number_of_columns << std::endl;
std::cout << "csr0.number_of_nonzeros = " << csr0.number_of_nonzeros << std::endl;

csr0.write_binary(outpath);

csr_t csr1;
csr1.read_binary(outpath);
std::cout << "csr1.number_of_rows = " << csr1.number_of_rows << std::endl;
std::cout << "csr1.number_of_columns = " << csr1.number_of_columns << std::endl;
std::cout << "csr1.number_of_nonzeros = " << csr1.number_of_nonzeros << std::endl;

// --
// Log

bool verbose = false;
if(verbose) {
std::cout << "----------------" << std::endl;

thrust::copy(csr0.row_offsets.begin(), csr0.row_offsets.end(), std::ostream_iterator<edge_t>(std::cout, " "));
std::cout << std::endl;
std::cout << "-" << std::endl;
thrust::copy(csr1.row_offsets.begin(), csr1.row_offsets.end(), std::ostream_iterator<edge_t>(std::cout, " "));
std::cout << std::endl;

std::cout << "----------------" << std::endl;

thrust::copy(csr0.column_indices.begin(), csr0.column_indices.end(), std::ostream_iterator<vertex_t>(std::cout, " "));
std::cout << std::endl;
std::cout << "-" << std::endl;
thrust::copy(csr1.column_indices.begin(), csr1.column_indices.end(), std::ostream_iterator<vertex_t>(std::cout, " "));
std::cout << std::endl;

std::cout << "----------------" << std::endl;
}
}

int main(int argc, char** argv) {
test_read_binary(argc, argv);
return EXIT_SUCCESS;
}
68 changes: 68 additions & 0 deletions include/gunrock/formats/csr.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ template <memory_space_t space,
typename offset_t,
typename value_t>
struct csr_t {

index_t number_of_rows;
index_t number_of_columns;
offset_t number_of_nonzeros;
Expand All @@ -49,6 +50,73 @@ struct csr_t {

~csr_t() {}

void write_binary(std::string filename) {
FILE* file = fopen(filename.c_str(), "wb");

// Write metadata
fwrite(&number_of_rows, sizeof(index_t), 1, file);
fwrite(&number_of_columns, sizeof(index_t), 1, file);
fwrite(&number_of_nonzeros, sizeof(offset_t), 1, file);

// Write data
if(space == memory_space_t::device) {
assert(space == memory_space_t::device);

thrust::host_vector<offset_t> h_row_offsets(row_offsets);
thrust::host_vector<index_t> h_column_indices(column_indices);
thrust::host_vector<value_t> h_nonzero_values(nonzero_values);

fwrite(memory::raw_pointer_cast(h_row_offsets.data()), sizeof(offset_t), number_of_rows + 1, file);
fwrite(memory::raw_pointer_cast(h_column_indices.data()), sizeof(index_t), number_of_nonzeros, file);
fwrite(memory::raw_pointer_cast(h_nonzero_values.data()), sizeof(value_t), number_of_nonzeros, file);
} else {
assert(space == memory_space_t::host);

fwrite(memory::raw_pointer_cast(row_offsets.data()), sizeof(offset_t), number_of_rows + 1, file);
fwrite(memory::raw_pointer_cast(column_indices.data()), sizeof(index_t), number_of_nonzeros, file);
fwrite(memory::raw_pointer_cast(nonzero_values.data()), sizeof(value_t), number_of_nonzeros, file);
}

fclose(file);
}

void read_binary(std::string filename) {
FILE* file = fopen(filename.c_str(), "rb");

// Read metadata
fread(&number_of_rows, sizeof(index_t), 1, file);
fread(&number_of_columns, sizeof(index_t), 1, file);
fread(&number_of_nonzeros, sizeof(offset_t), 1, file);

row_offsets.resize(number_of_rows + 1);
column_indices.resize(number_of_nonzeros);
nonzero_values.resize(number_of_nonzeros);

if(space == memory_space_t::device) {
assert(space == memory_space_t::device);

thrust::host_vector<offset_t> h_row_offsets(number_of_rows + 1);
thrust::host_vector<index_t> h_column_indices(number_of_nonzeros);
thrust::host_vector<value_t> h_nonzero_values(number_of_nonzeros);

fread(memory::raw_pointer_cast(h_row_offsets.data()), sizeof(offset_t), number_of_rows + 1, file);
fread(memory::raw_pointer_cast(h_column_indices.data()), sizeof(index_t), number_of_nonzeros, file);
fread(memory::raw_pointer_cast(h_nonzero_values.data()), sizeof(value_t), number_of_nonzeros, file);

// Copy data from host to device
row_offsets = h_row_offsets;
column_indices = h_column_indices;
nonzero_values = h_nonzero_values;

} else {
assert(space == memory_space_t::host);

fread(memory::raw_pointer_cast(row_offsets.data()), sizeof(offset_t), number_of_rows + 1, file);
fread(memory::raw_pointer_cast(column_indices.data()), sizeof(index_t), number_of_nonzeros, file);
fread(memory::raw_pointer_cast(nonzero_values.data()), sizeof(value_t), number_of_nonzeros, file);
}
}

/**
* @brief Convert a Coordinate Sparse Format into Compressed Sparse Row
* Format.
Expand Down