From 144be999801f4a5d76a385c56a538f1f6f465540 Mon Sep 17 00:00:00 2001 From: lxrzlyr <1289539524@qq.com> Date: Thu, 20 Jun 2024 11:12:00 +0800 Subject: [PATCH] Update: IO Function Fixbug: Function DAWN::Matrix::transpose_Weighted modified: algorithm/cpu/apsp_cpu/apsp_cpu.cpp modified: algorithm/cpu/bc_cpu/bc_cpu.cpp modified: algorithm/cpu/bfs_cpu/bfs_cpu.cpp modified: algorithm/cpu/cc_cpu/cc_cpu.cpp modified: algorithm/cpu/mssp_cpu/mssp_cpu.cpp modified: algorithm/cpu/sssp_cpu/sssp_cpu.cpp modified: algorithm/gpu/apsp_gpu/apsp_gpu.cu modified: algorithm/gpu/bfs_gpu/bfs_gpu.cu modified: algorithm/gpu/cc_gpu/cc_gpu.cu modified: algorithm/gpu/mssp_gpu/mssp_gpu.cu modified: algorithm/gpu/sssp_gpu/sssp_gpu.cu modified: include/dawn/dawn.hxx modified: include/dawn/graph.hxx new file: include/dawn/io.hxx new file: include/dawn/matrix.hxx new file: include/dawn/parameters.hxx modified: include/dawn/tool.hxx modified: src/algorithm/cpu/bc.cpp modified: src/algorithm/cpu/bfs.cpp modified: src/algorithm/cpu/sssp.cpp modified: src/algorithm/gpu/bfs.cu modified: src/algorithm/gpu/sssp.cu modified: src/graph.cpp new file: src/io.cpp new file: src/matrix.cpp new file: src/parameters.cpp modified: src/tool.cpp --- algorithm/cpu/apsp_cpu/apsp_cpu.cpp | 29 +-- algorithm/cpu/bc_cpu/bc_cpu.cpp | 10 +- algorithm/cpu/bfs_cpu/bfs_cpu.cpp | 22 +- algorithm/cpu/cc_cpu/cc_cpu.cpp | 16 +- algorithm/cpu/mssp_cpu/mssp_cpu.cpp | 35 ++-- algorithm/cpu/sssp_cpu/sssp_cpu.cpp | 16 +- algorithm/gpu/apsp_gpu/apsp_gpu.cu | 42 ++-- algorithm/gpu/bfs_gpu/bfs_gpu.cu | 22 +- algorithm/gpu/cc_gpu/cc_gpu.cu | 21 +- algorithm/gpu/mssp_gpu/mssp_gpu.cu | 41 ++-- algorithm/gpu/sssp_gpu/sssp_gpu.cu | 26 +-- include/dawn/dawn.hxx | 4 + include/dawn/graph.hxx | 54 +---- include/dawn/io.hxx | 26 +++ include/dawn/matrix.hxx | 33 +++ include/dawn/parameters.hxx | 19 ++ include/dawn/tool.hxx | 8 - src/algorithm/cpu/bc.cpp | 2 +- src/algorithm/cpu/bfs.cpp | 4 +- src/algorithm/cpu/sssp.cpp | 4 +- src/algorithm/gpu/bfs.cu | 2 +- src/algorithm/gpu/sssp.cu | 2 +- src/graph.cpp | 310 +--------------------------- src/io.cpp | 281 +++++++++++++++++++++++++ src/matrix.cpp | 82 ++++++++ src/parameters.cpp | 98 +++++++++ src/tool.cpp | 68 ------ 27 files changed, 649 insertions(+), 628 deletions(-) create mode 100644 include/dawn/io.hxx create mode 100644 include/dawn/matrix.hxx create mode 100644 include/dawn/parameters.hxx create mode 100644 src/io.cpp create mode 100644 src/matrix.cpp create mode 100644 src/parameters.cpp diff --git a/algorithm/cpu/apsp_cpu/apsp_cpu.cpp b/algorithm/cpu/apsp_cpu/apsp_cpu.cpp index 1911625..770f793 100755 --- a/algorithm/cpu/apsp_cpu/apsp_cpu.cpp +++ b/algorithm/cpu/apsp_cpu/apsp_cpu.cpp @@ -7,36 +7,19 @@ #include int main(int argc, char* argv[]) { + DAWN::IO::parameters_t params = DAWN::IO::parameters(argc, argv); DAWN::Graph::Graph_t graph; - std::string input_path = argv[1]; - std::string output_path = argv[2]; - std::string print = argv[3]; - graph.source = atoi(argv[4]); - std::string weighted = argv[5]; + graph.source = params.source; + graph.print = params.print; + graph.weighted = params.weighted; graph.interval = 100; - - if (print == "true") { - graph.print = true; - std::cout << "Print source " << graph.source << std::endl; - } else - graph.print = false; - - if (weighted == "weighted") { - graph.weighted = true; - } else { - graph.weighted = false; - } - - graph.source = atoi(argv[6]); graph.thread = omp_get_num_threads(); - DAWN::Graph::createGraph(input_path, graph); - float elapsed_time = DAWN::APSP_CPU::run(graph, output_path); + DAWN::Graph::createGraph(params.input_path, graph); + float elapsed_time = DAWN::APSP_CPU::run(graph, params.output_path); printf("%-21s%3.5d\n", "Nodes:", graph.rows); printf("%-21s%3.5ld\n", "Edges:", graph.nnz); printf("%-21s%3.5lf\n", "Time:", elapsed_time); return 0; } - -// ./apsp_cpu $GRAPH_DIR/XX.mtx ../output.txt false 0 unweighted// \ No newline at end of file diff --git a/algorithm/cpu/bc_cpu/bc_cpu.cpp b/algorithm/cpu/bc_cpu/bc_cpu.cpp index f420642..084f176 100755 --- a/algorithm/cpu/bc_cpu/bc_cpu.cpp +++ b/algorithm/cpu/bc_cpu/bc_cpu.cpp @@ -7,10 +7,8 @@ #include int main(int argc, char* argv[]) { + DAWN::IO::parameters_t params = DAWN::IO::parameters(argc, argv); DAWN::Graph::Graph_t graph; - std::string input_path = argv[1]; - std::string output_path = argv[2]; - // std::string weighted = argv[3]; graph.thread = omp_get_num_threads(); graph.print = true; @@ -27,13 +25,13 @@ int main(int argc, char* argv[]) { // printf("%-21s%3.5lf\n", "Time:", elapsed_time); // } else { graph.weighted = false; - DAWN::Graph::createGraph(input_path, graph); + DAWN::Graph::createGraph(params.input_path, graph); printf("%-21s%3.5d\n", "Nodes:", graph.rows); printf("%-21s%3.5ld\n", "Edges:", graph.nnz); - float elapsed_time = DAWN::BC_CPU::Betweenness_Centrality(graph, output_path); + float elapsed_time = + DAWN::BC_CPU::Betweenness_Centrality(graph, params.output_path); printf("%-21s%3.5lf\n", "Time:", elapsed_time); // } return 0; } -// ./bc_cpu $GRAPH_DIR/XX.mtx ./output.txt weighted diff --git a/algorithm/cpu/bfs_cpu/bfs_cpu.cpp b/algorithm/cpu/bfs_cpu/bfs_cpu.cpp index f496acf..4593f78 100755 --- a/algorithm/cpu/bfs_cpu/bfs_cpu.cpp +++ b/algorithm/cpu/bfs_cpu/bfs_cpu.cpp @@ -7,24 +7,15 @@ #include int main(int argc, char* argv[]) { + DAWN::IO::parameters_t params = DAWN::IO::parameters(argc, argv); DAWN::Graph::Graph_t graph; - std::string input_path = argv[1]; - std::string output_path = argv[2]; - std::string print = argv[3]; - graph.source = atoi(argv[4]); - - if (print == "true") { - graph.print = true; - std::cout << "Print source " << graph.source << std::endl; - } else { - graph.print = false; - } - graph.thread = omp_get_num_threads(); + graph.source = params.source; + graph.print = params.print; graph.weighted = false; + graph.thread = omp_get_num_threads(); - DAWN::Graph::createGraph(input_path, graph); - - float elapsed_time = DAWN::BFS_CPU::run(graph, output_path); + DAWN::Graph::createGraph(params.input_path, graph); + float elapsed_time = DAWN::BFS_CPU::run(graph, params.output_path); printf("%-21s%3.5d\n", "Nodes:", graph.rows); printf("%-21s%3.5ld\n", "Edges:", graph.nnz); @@ -32,4 +23,3 @@ int main(int argc, char* argv[]) { return 0; } -// ./bfs_cpu $GRAPH_DIR/XX.mtx ../output.txt false 0 diff --git a/algorithm/cpu/cc_cpu/cc_cpu.cpp b/algorithm/cpu/cc_cpu/cc_cpu.cpp index b34499b..7b5760f 100755 --- a/algorithm/cpu/cc_cpu/cc_cpu.cpp +++ b/algorithm/cpu/cc_cpu/cc_cpu.cpp @@ -7,18 +7,17 @@ #include int main(int argc, char* argv[]) { + DAWN::IO::parameters_t params = DAWN::IO::parameters(argc, argv); DAWN::Graph::Graph_t graph; - std::string input_path = argv[1]; - graph.source = atoi(argv[2]); - std::string weighted = argv[3]; + graph.source = params.source; + graph.print = params.print; + graph.weighted = params.weighted; graph.thread = omp_get_num_threads(); - DAWN::Graph::createGraph(input_path, graph); + DAWN::Graph::createGraph(params.input_path, graph); float elapsed_time = 0.0f; - if (weighted == "weighted") { - graph.weighted = true; - + if (params.weighted) { float closeness_centrality = DAWN::CC_CPU::run_Weighted(graph, graph.source, elapsed_time); @@ -28,8 +27,6 @@ int main(int argc, char* argv[]) { printf("%-21s%3.5ld\n", "Edges:", graph.nnz); printf("%-21s%3.5lf\n", "Time:", elapsed_time); } else { - graph.weighted = false; - float closeness_centrality = DAWN::CC_CPU::run(graph, graph.source, elapsed_time); @@ -42,4 +39,3 @@ int main(int argc, char* argv[]) { return 0; } -// ./cc_cpu $GRAPH_DIR/XX.mtx 0 weighted diff --git a/algorithm/cpu/mssp_cpu/mssp_cpu.cpp b/algorithm/cpu/mssp_cpu/mssp_cpu.cpp index 84294eb..72b2a1c 100755 --- a/algorithm/cpu/mssp_cpu/mssp_cpu.cpp +++ b/algorithm/cpu/mssp_cpu/mssp_cpu.cpp @@ -7,35 +7,24 @@ #include int main(int argc, char* argv[]) { + DAWN::IO::parameters_t params = DAWN::IO::parameters(argc, argv); DAWN::Graph::Graph_t graph; - std::string input_path = argv[1]; - std::string output_path = argv[2]; - std::string print = argv[3]; - std::string sourceList = argv[4]; - std::string weighted = argv[5]; + graph.source = params.source; + graph.print = params.print; + graph.weighted = params.weighted; graph.interval = 100; - if (print == "true") { - graph.print = true; - std::cout << "Print source " << graph.source << std::endl; - } else { - graph.print = false; - } - if (weighted == "weighted") { - graph.weighted = true; - } else { - graph.weighted = false; - } graph.thread = omp_get_num_threads(); - DAWN::Graph::createGraph(input_path, graph); - DAWN::Graph::readList(sourceList, graph); - float elapsed_time = DAWN::MSSP_CPU::run(graph, output_path); + if (params.sourceList_path.empty()) { + std::cerr << "Invalid value for sourceList_path option.\n"; + exit(EXIT_FAILURE); + } + + DAWN::Graph::createGraph(params.input_path, graph); + DAWN::IO::readList(params.sourceList_path, graph); + float elapsed_time = DAWN::MSSP_CPU::run(graph, params.output_path); printf("%-21s%3.5d\n", "Nodes:", graph.rows); printf("%-21s%3.5ld\n", "Edges:", graph.nnz); printf("%-21s%3.5lf\n", "Time:", elapsed_time); return 0; } -// ./mssp_cpu $GRAPH_DIR/XX.mtx ../output.txt false ./sourceList.txt -// unweighted - -// ./mssp_cpu $GRAPH_DIR/XX.mtx ../output.txt false ./sourceList.txt weighted diff --git a/algorithm/cpu/sssp_cpu/sssp_cpu.cpp b/algorithm/cpu/sssp_cpu/sssp_cpu.cpp index c2b60e4..9248854 100755 --- a/algorithm/cpu/sssp_cpu/sssp_cpu.cpp +++ b/algorithm/cpu/sssp_cpu/sssp_cpu.cpp @@ -7,24 +7,18 @@ #include int main(int argc, char* argv[]) { + DAWN::IO::parameters_t params = DAWN::IO::parameters(argc, argv); DAWN::Graph::Graph_t graph; - std::string input_path = argv[1]; - std::string output_path = argv[2]; - std::string print = argv[3]; + graph.source = params.source; + graph.print = params.print; graph.source = atoi(argv[4]); - if (print == "true") { - graph.print = true; - std::cout << "Print source " << graph.source << std::endl; - } else { - graph.print = false; - } graph.thread = omp_get_num_threads(); graph.weighted = true; - DAWN::Graph::createGraph(input_path, graph); + DAWN::Graph::createGraph(params.input_path, graph); - float elapsed_time = DAWN::SSSP_CPU::run(graph, output_path); + float elapsed_time = DAWN::SSSP_CPU::run(graph, params.output_path); printf("%-21s%3.5d\n", "Nodes:", graph.rows); printf("%-21s%3.5ld\n", "Edges:", graph.nnz); diff --git a/algorithm/gpu/apsp_gpu/apsp_gpu.cu b/algorithm/gpu/apsp_gpu/apsp_gpu.cu index 0596274..a6dd56b 100755 --- a/algorithm/gpu/apsp_gpu/apsp_gpu.cu +++ b/algorithm/gpu/apsp_gpu/apsp_gpu.cu @@ -6,46 +6,30 @@ */ #include int main(int argc, char* argv[]) { + DAWN::IO::parameters_t params = DAWN::IO::parameters(argc, argv); DAWN::Graph::Graph_t graph; - - std::string input_path = argv[1]; - std::string output_path = argv[2]; - graph.stream = atoi(argv[3]); - graph.block_size = atoi(argv[4]); - std::string print = argv[5]; - graph.source = atoi(argv[6]); - std::string weighted = argv[7]; - + graph.source = params.source; + graph.print = params.print; + graph.weighted = params.weighted; + graph.thread = 1; + graph.stream = 4; + graph.block_size = 1024; graph.interval = 100; - if (print == "true") { - graph.print = true; - std::cout << "Print source " << graph.source << std::endl; - } else { - graph.print = false; - } - - DAWN::Graph::createGraph(input_path, graph); - - if (weighted == "weighted") { - graph.weighted = true; - - float elapsed_time = DAWN::APSP_GPU::run_Weighted(graph, output_path); + DAWN::Graph::createGraph(params.input_path, graph); + if (graph.weighted) { + float elapsed_time = + DAWN::APSP_GPU::run_Weighted(graph, params.output_path); printf("%-21s%3.5d\n", "Nodes:", graph.rows); printf("%-21s%3.5ld\n", "Edges:", graph.nnz); printf("%-21s%3.5lf\n", "Time:", elapsed_time); } else { - graph.weighted = false; - - float elapsed_time = DAWN::APSP_GPU::run(graph, output_path); - + float elapsed_time = DAWN::APSP_GPU::run(graph, params.output_path); printf("%-21s%3.5d\n", "Nodes:", graph.rows); printf("%-21s%3.5ld\n", "Edges:", graph.nnz); printf("%-21s%3.5lf\n", "Time:", elapsed_time); } return 0; -} -//./apsp_gpu $GRAPH_DIR/XXX.mtx ../output.txt 8 1024 false 0 unweighted -//./apsp_gpu $GRAPH_DIR/XXX.mtx ../output.txt 8 1024 false 0 weighted \ No newline at end of file +} \ No newline at end of file diff --git a/algorithm/gpu/bfs_gpu/bfs_gpu.cu b/algorithm/gpu/bfs_gpu/bfs_gpu.cu index 70a6e00..5ccd312 100755 --- a/algorithm/gpu/bfs_gpu/bfs_gpu.cu +++ b/algorithm/gpu/bfs_gpu/bfs_gpu.cu @@ -7,28 +7,18 @@ #include int main(int argc, char* argv[]) { + DAWN::IO::parameters_t params = DAWN::IO::parameters(argc, argv); DAWN::Graph::Graph_t graph; - - std::string input_path = argv[1]; - std::string output_path = argv[2]; - graph.block_size = atoi(argv[3]); - std::string print = argv[4]; - graph.source = atoi(argv[5]); - + graph.source = params.source; + graph.print = params.print; graph.thread = 1; graph.stream = 1; + graph.block_size = 1024; graph.weighted = false; - if (print == "true") { - graph.print = true; - std::cout << "Print source " << graph.source << std::endl; - } else { - graph.print = false; - } - - DAWN::Graph::createGraph(input_path, graph); + DAWN::Graph::createGraph(params.input_path, graph); - float elapsed_time = DAWN::BFS_GPU::run(graph, output_path); + float elapsed_time = DAWN::BFS_GPU::run(graph, params.output_path); printf("%-21s%3.5d\n", "Nodes:", graph.rows); printf("%-21s%3.5ld\n", "Edges:", graph.nnz); diff --git a/algorithm/gpu/cc_gpu/cc_gpu.cu b/algorithm/gpu/cc_gpu/cc_gpu.cu index ad037ed..9a9d35f 100755 --- a/algorithm/gpu/cc_gpu/cc_gpu.cu +++ b/algorithm/gpu/cc_gpu/cc_gpu.cu @@ -7,31 +7,24 @@ #include int main(int argc, char* argv[]) { + DAWN::IO::parameters_t params = DAWN::IO::parameters(argc, argv); DAWN::Graph::Graph_t graph; - - std::string input_path = argv[1]; - graph.block_size = atoi(argv[2]); - graph.source = atoi(argv[3]); - std::string weighted = argv[4]; - + graph.source = params.source; + graph.print = params.print; + graph.weighted = params.weighted; graph.thread = 1; graph.stream = 1; + graph.block_size = 1024; - DAWN::Graph::createGraph(input_path, graph); - - if (weighted == "true") { - graph.weighted = true; + DAWN::Graph::createGraph(params.input_path, graph); + if (graph.weighted) { float closeness_centrality = DAWN::CC_GPU::run_Weighted(graph, graph.source); - printf("%-21s%3.5d\n", "Source:", graph.source); printf("%-21s%3.5lf\n", "Closeness Centrality:", closeness_centrality); } else { - graph.weighted = false; - float closeness_centrality = DAWN::CC_GPU::run(graph, graph.source); - printf("%-21s%3.5d\n", "Source:", graph.source); printf("%-21s%3.5lf\n", "Closeness Centrality:", closeness_centrality); } diff --git a/algorithm/gpu/mssp_gpu/mssp_gpu.cu b/algorithm/gpu/mssp_gpu/mssp_gpu.cu index 30e153c..dea83ab 100755 --- a/algorithm/gpu/mssp_gpu/mssp_gpu.cu +++ b/algorithm/gpu/mssp_gpu/mssp_gpu.cu @@ -7,42 +7,27 @@ #include int main(int argc, char* argv[]) { + DAWN::IO::parameters_t params = DAWN::IO::parameters(argc, argv); DAWN::Graph::Graph_t graph; - - std::string input_path = argv[1]; - std::string output_path = argv[2]; - graph.stream = atoi(argv[3]); - graph.block_size = atoi(argv[4]); - std::string print = argv[5]; - std::string sourceList = argv[6]; - std::string weighted = argv[7]; - + graph.print = params.print; + graph.weighted = params.weighted; + graph.source = params.source; + graph.thread = 1; + graph.stream = 4; + graph.block_size = 1024; graph.interval = 100; - if (print == "true") { - graph.print = true; - std::cout << "Print source " << graph.source << std::endl; - } else { - graph.print = false; - } - - DAWN::Graph::createGraph(input_path, graph); - DAWN::Graph::readList(sourceList, graph); - - if (weighted == "weighted") { - graph.weighted = true; - - float elapsed_time = DAWN::MSSP_GPU::run_Weighted(graph, output_path); + DAWN::Graph::createGraph(params.input_path, graph); + DAWN::IO::readList(params.sourceList_path, graph); + if (graph.weighted) { + float elapsed_time = + DAWN::MSSP_GPU::run_Weighted(graph, params.output_path); printf("%-21s%3.5d\n", "Nodes:", graph.rows); printf("%-21s%3.5ld\n", "Edges:", graph.nnz); printf("%-21s%3.5lf\n", "Time:", elapsed_time); - } else { - graph.weighted = false; - - float elapsed_time = DAWN::MSSP_GPU::run(graph, output_path); - + float elapsed_time = DAWN::MSSP_GPU::run(graph, params.output_path); printf("%-21s%3.5d\n", "Nodes:", graph.rows); printf("%-21s%3.5ld\n", "Edges:", graph.nnz); printf("%-21s%3.5lf\n", "Time:", elapsed_time); diff --git a/algorithm/gpu/sssp_gpu/sssp_gpu.cu b/algorithm/gpu/sssp_gpu/sssp_gpu.cu index 0097514..d60c190 100755 --- a/algorithm/gpu/sssp_gpu/sssp_gpu.cu +++ b/algorithm/gpu/sssp_gpu/sssp_gpu.cu @@ -7,29 +7,17 @@ #include int main(int argc, char* argv[]) { + DAWN::IO::parameters_t params = DAWN::IO::parameters(argc, argv); DAWN::Graph::Graph_t graph; - - std::string input_path = argv[1]; - std::string output_path = argv[2]; - graph.block_size = atoi(argv[3]); - std::string print = argv[4]; - graph.source = atoi(argv[5]); - + graph.source = params.source; + graph.print = params.print; + graph.weighted = true; graph.thread = 1; graph.stream = 1; - graph.weighted = true; - - if (print == "true") { - graph.print = true; - std::cout << "Print source " << graph.source << std::endl; - } else { - graph.print = false; - } - - DAWN::Graph::createGraph(input_path, graph); - - float elapsed_time = DAWN::SSSP_GPU::run(graph, output_path); + graph.block_size = 1024; + DAWN::Graph::createGraph(params.input_path, graph); + float elapsed_time = DAWN::SSSP_GPU::run(graph, params.output_path); printf("%-21s%3.5d\n", "Nodes:", graph.rows); printf("%-21s%3.5ld\n", "Edges:", graph.nnz); printf("%-21s%3.5lf\n", "Time:", elapsed_time); diff --git a/include/dawn/dawn.hxx b/include/dawn/dawn.hxx index a5adda8..6a93389 100755 --- a/include/dawn/dawn.hxx +++ b/include/dawn/dawn.hxx @@ -4,5 +4,9 @@ * * @copyright Copyright (c) 2024 */ +#pragma once #include #include +#include +#include +#include diff --git a/include/dawn/graph.hxx b/include/dawn/graph.hxx index cda4f08..e0c3ea5 100755 --- a/include/dawn/graph.hxx +++ b/include/dawn/graph.hxx @@ -6,73 +6,31 @@ */ #pragma once #include +#include namespace DAWN { namespace Graph { -struct Csr_t { - public: - int* row_ptr; - int* col; - float* val; -}; - -struct Coo_t { - public: - int* row; - int* col; - float* val; -}; - class Graph_t { public: int rows; int cols; uint64_t nnz; - Csr_t csr; - Coo_t coo; + DAWN::Matrix::Csr_t csr; + DAWN::Matrix::Coo_t coo; int thread; int interval; int stream; int block_size; - bool print; // print the result int source; + bool print; bool weighted; bool directed; std::vector msource; // Source list for Multi-Source algorithms }; -void coo2Csr(int n, - int nnz, - Csr_t& csr, - Coo_t& coo); // COO matrix to CSR matrix - -void coo2Csr_Weighted(int n, int nnz, Csr_t& csr, Coo_t& coo); - -void transpose(int nnz, Coo_t& coo); - -void transpose_Weighted(int nnz, Coo_t& coo); - -void createGraph(std::string& input_path, Graph_t& graph); // create graph - -void readGraph( - std::string& input_path, - Graph_t& graph); // read undirected and unweighted graph from file - -void readGraph_Weighted( - std::string& input_path, - Graph_t& graph); // read undirected and weighted graph from file - -void readGraph_Directed( - std::string& input_path, - Graph_t& graph); // read directed and unweighted graph from file - -void readGraph_Directed_Weighted( - std::string& input_path, - Graph_t& graph); // read directed and weighted graph from file - -void readList(std::string& input_path, - Graph_t& graph); // read list from file +void createGraph(std::string& input_path, + DAWN::Graph::Graph_t& graph); // create graph } // namespace Graph } // namespace DAWN \ No newline at end of file diff --git a/include/dawn/io.hxx b/include/dawn/io.hxx new file mode 100644 index 0000000..d05ea3d --- /dev/null +++ b/include/dawn/io.hxx @@ -0,0 +1,26 @@ +#pragma once +#include + +namespace DAWN { +namespace IO { + +void readGraph(std::string& input_path, DAWN::Graph::Graph_t& graph); + +void readGraph_Weighted(std::string& input_path, DAWN::Graph::Graph_t& graph); + +void readGraph_Directed(std::string& input_path, DAWN::Graph::Graph_t& graph); + +void readGraph_Directed_Weighted(std::string& input_path, + DAWN::Graph::Graph_t& graph); + +void readList(std::string& input_path, DAWN::Graph::Graph_t& graph); + +void outfile(int n, int* result, int source, std::string& output_path); + +void outfile(int n, float* result, int source, std::string& output_path); + +void outfile(int n, int* result, std::string& output_path); + +void outfile(int n, float* result, std::string& output_path); +} // namespace IO +} // namespace DAWN \ No newline at end of file diff --git a/include/dawn/matrix.hxx b/include/dawn/matrix.hxx new file mode 100644 index 0000000..d363e0c --- /dev/null +++ b/include/dawn/matrix.hxx @@ -0,0 +1,33 @@ +#pragma once +#include + +namespace DAWN { +namespace Matrix { + +struct Csr_t { + public: + int* row_ptr; + int* col; + float* val; +}; + +struct Coo_t { + public: + int* row; + int* col; + float* val; +}; + +void coo2Csr(int n, + int nnz, + Csr_t& csr, + Coo_t& coo); // COO matrix to CSR matrix + +void coo2Csr_Weighted(int n, int nnz, Csr_t& csr, Coo_t& coo); + +void transpose(int nnz, Coo_t& coo); + +void transpose_Weighted(int nnz, Coo_t& coo); + +} // namespace Matrix +} // namespace DAWN \ No newline at end of file diff --git a/include/dawn/parameters.hxx b/include/dawn/parameters.hxx new file mode 100644 index 0000000..5b48ce0 --- /dev/null +++ b/include/dawn/parameters.hxx @@ -0,0 +1,19 @@ +// dawn/parameters.hxx +#pragma once +#include +#include + +namespace DAWN { +namespace IO { +struct parameters_t { + std::string input_path; + std::string output_path; + std::string sourceList_path; + bool print = false; + bool weighted = false; + int source = -1; +}; +parameters_t parameters(int argc, char* argv[]); +void printHelp(); +} // namespace IO +} // namespace DAWN \ No newline at end of file diff --git a/include/dawn/tool.hxx b/include/dawn/tool.hxx index 9fc9f01..56a59a4 100644 --- a/include/dawn/tool.hxx +++ b/include/dawn/tool.hxx @@ -18,13 +18,5 @@ void infoprint(int entry, int thread, float elapsed_time); // Print current task progress -void outfile(int n, int* result, int source, std::string& output_path); - -void outfile(int n, float* result, int source, std::string& output_path); - -void outfile(int n, int* result, std::string& output_path); - -void outfile(int n, float* result, std::string& output_path); - } // namespace Tool } // namespace DAWN \ No newline at end of file diff --git a/src/algorithm/cpu/bc.cpp b/src/algorithm/cpu/bc.cpp index 58a8f54..81a014a 100755 --- a/src/algorithm/cpu/bc.cpp +++ b/src/algorithm/cpu/bc.cpp @@ -30,7 +30,7 @@ float DAWN::BC_CPU::Betweenness_Centrality(DAWN::Graph::Graph_t& graph, bc_temp = nullptr; } - DAWN::Tool::outfile(row, bc_values, output_path); + DAWN::IO::outfile(row, bc_values, output_path); delete[] bc_values; bc_values = nullptr; diff --git a/src/algorithm/cpu/bfs.cpp b/src/algorithm/cpu/bfs.cpp index 979bab1..de06ac6 100755 --- a/src/algorithm/cpu/bfs.cpp +++ b/src/algorithm/cpu/bfs.cpp @@ -64,7 +64,7 @@ float DAWN::BFS_CPU::BFS(int* row_ptr, // Output if (print) { printf("Start print\n"); - DAWN::Tool::outfile(row, distance, source, output_path); + DAWN::IO::outfile(row, distance, source, output_path); } delete[] beta; @@ -119,7 +119,7 @@ float DAWN::BFS_CPU::BFS_kernel(int* row_ptr, // Output if (print) { printf("Start print\n"); - DAWN::Tool::outfile(row, distance, source, output_path); + DAWN::IO::outfile(row, distance, source, output_path); } delete[] alpha; diff --git a/src/algorithm/cpu/sssp.cpp b/src/algorithm/cpu/sssp.cpp index a3c43a8..25c053a 100755 --- a/src/algorithm/cpu/sssp.cpp +++ b/src/algorithm/cpu/sssp.cpp @@ -66,7 +66,7 @@ float DAWN::SSSP_CPU::SSSP(int* row_ptr, // Output if (print) { printf("Start print\n"); - DAWN::Tool::outfile(row, distance, source, output_path); + DAWN::IO::outfile(row, distance, source, output_path); } delete[] beta; @@ -125,7 +125,7 @@ float DAWN::SSSP_CPU::SSSP_kernel(int* row_ptr, // Output if (print) { printf("Start print\n"); - DAWN::Tool::outfile(row, distance, source, output_path); + DAWN::IO::outfile(row, distance, source, output_path); } delete[] alpha; diff --git a/src/algorithm/gpu/bfs.cu b/src/algorithm/gpu/bfs.cu index 17dc787..2e5f1c4 100644 --- a/src/algorithm/gpu/bfs.cu +++ b/src/algorithm/gpu/bfs.cu @@ -98,7 +98,7 @@ float DAWN::BFS_GPU::kernel(DAWN::Graph::Graph_t& graph, thrust::copy(d_distance.begin(), d_distance.end(), h_distance.begin()); printf("Start print\n"); - DAWN::Tool::outfile(graph.rows, h_distance.data(), source, output_path); + DAWN::IO::outfile(graph.rows, h_distance.data(), source, output_path); } return elapsed_time; diff --git a/src/algorithm/gpu/sssp.cu b/src/algorithm/gpu/sssp.cu index f993b9d..46c991d 100644 --- a/src/algorithm/gpu/sssp.cu +++ b/src/algorithm/gpu/sssp.cu @@ -123,7 +123,7 @@ float DAWN::SSSP_GPU::kernel(DAWN::Graph::Graph_t& graph, thrust::copy(d_distance.begin(), d_distance.end(), h_distance.begin()); printf("Start print\n"); - DAWN::Tool::outfile(graph.rows, h_distance.data(), source, output_path); + DAWN::IO::outfile(graph.rows, h_distance.data(), source, output_path); } return elapsed_time; } diff --git a/src/graph.cpp b/src/graph.cpp index 8a4a953..b4b935e 100755 --- a/src/graph.cpp +++ b/src/graph.cpp @@ -4,91 +4,9 @@ * * @copyright Copyright (c) 2024 */ -#include -namespace DAWN { - -void Graph::transpose_Weighted(int nnz, DAWN::Graph::Coo_t& coo) { - std::vector>> tmp; - for (int i = 0; i < nnz; i++) { - tmp.push_back({coo.row[i], {coo.col[i], coo.val[i]}}); - } - std::sort(tmp.begin(), tmp.end()); - for (int i = 0; i < nnz; i++) { - coo.row[i] = tmp[i].first; - coo.col[i] = tmp[i].second.first; - coo.val[i] = tmp[i].second.second; - } -} - -void Graph::transpose(int nnz, DAWN::Graph::Coo_t& coo) { - std::vector> tmp; - for (int i = 0; i < nnz; i++) { - tmp.push_back({coo.row[i], coo.col[i]}); - } - std::sort(tmp.begin(), tmp.end()); - for (int i = 0; i < nnz; i++) { - coo.row[i] = tmp[i].first; - coo.col[i] = tmp[i].second; - } -} - -void Graph::coo2Csr_Weighted(int n, - int nnz, - DAWN::Graph::Csr_t& csr, - DAWN::Graph::Coo_t& coo) { - csr.val = new float[nnz]; - csr.row_ptr = new int[n + 1]; - csr.col = new int[nnz]; - - // Count the number of non-zero elements in each column - int* row_count = new int[n](); - for (int i = 0; i < nnz; i++) { - row_count[coo.row[i]]++; - } - csr.row_ptr[0] = 0; - for (int i = 1; i <= n; i++) { - csr.row_ptr[i] = csr.row_ptr[i - 1] + row_count[i - 1]; - } - -// Fill each non-zero element into val and col -#pragma omp parallel for - for (int i = 0; i < n; i++) { - for (int j = csr.row_ptr[i]; j < csr.row_ptr[i + 1]; j++) { - csr.col[j] = coo.col[j]; - csr.val[j] = coo.val[j]; - } - } - delete[] row_count; -} - -void Graph::coo2Csr(int n, - int nnz, - DAWN::Graph::Csr_t& csr, - DAWN::Graph::Coo_t& coo) { - csr.row_ptr = new int[n + 1]; - csr.col = new int[nnz]; - - // Count the number of non-zero elements in each column - int* row_count = new int[n](); - for (int i = 0; i < nnz; i++) { - row_count[coo.row[i]]++; - } - csr.row_ptr[0] = 0; - for (int i = 1; i <= n; i++) { - csr.row_ptr[i] = csr.row_ptr[i - 1] + row_count[i - 1]; - } - -// Fill each non-zero element into val and col -#pragma omp parallel for - for (int i = 0; i < n; i++) { - for (int j = csr.row_ptr[i]; j < csr.row_ptr[i + 1]; j++) { - csr.col[j] = coo.col[j]; - } - } - delete[] row_count; -} - -void Graph::createGraph(std::string& input_path, Graph::Graph_t& graph) { +#include +#include +void DAWN::Graph::createGraph(std::string& input_path, Graph::Graph_t& graph) { std::ifstream file(input_path); if (!file.is_open()) { std::cerr << "Error opening file " << input_path << std::endl; @@ -141,231 +59,19 @@ void Graph::createGraph(std::string& input_path, Graph::Graph_t& graph) { graph.rows = rows; graph.cols = cols; graph.nnz = nnz; + graph.source = graph.source % rows; std::cout << "Read Input Graph" << std::endl; if (graph.directed) { if (graph.weighted) - readGraph_Directed_Weighted(input_path, graph); + DAWN::IO::readGraph_Directed_Weighted(input_path, graph); else - readGraph_Directed(input_path, graph); + DAWN::IO::readGraph_Directed(input_path, graph); } else { if (graph.weighted) - readGraph_Weighted(input_path, graph); + DAWN::IO::readGraph_Weighted(input_path, graph); else - readGraph(input_path, graph); - } -} - -void Graph::readGraph(std::string& input_path, Graph::Graph_t& graph) { - std::ifstream file(input_path); - if (!file.is_open()) { - std::cerr << "Error opening file " << input_path << std::endl; - return; - } - std::string line; - std::priority_queue> s; - int rows, cols; - - while (std::getline(file, line)) { - if (line[0] == '%') - continue; - std::stringstream ss(line); - ss >> rows >> cols; - rows--; - cols--; - if (rows != cols) { - s.push({rows, cols}); - s.push({cols, rows}); - } - } - file.close(); - - graph.nnz = s.size(); - graph.coo.col = new int[graph.nnz]; - graph.coo.row = new int[graph.nnz]; - std::fill_n(graph.coo.col, graph.nnz, 0); - std::fill_n(graph.coo.row, graph.nnz, 0); - int i = graph.nnz - 1; - - while (!s.empty()) { - graph.coo.row[i] = s.top().first; - graph.coo.col[i] = s.top().second; - --i; - s.pop(); - } - coo2Csr(graph.rows, graph.nnz, graph.csr, graph.coo); - - delete[] graph.coo.col; - graph.coo.col = NULL; - delete[] graph.coo.row; - graph.coo.row = NULL; -} - -void Graph::readGraph_Directed(std::string& input_path, Graph::Graph_t& graph) { - std::ifstream file(input_path); - if (!file.is_open()) { - std::cerr << "Error opening file " << input_path << std::endl; - return; - } - std::string line; - - graph.coo.col = new int[graph.nnz]; - graph.coo.row = new int[graph.nnz]; - std::fill_n(graph.coo.col, graph.nnz, 0); - std::fill_n(graph.coo.row, graph.nnz, 0); - - int rows, cols; - int i = 0; - - while (std::getline(file, line)) { - if (line[0] == '%') - continue; - std::stringstream ss(line); - ss >> rows >> cols; - rows--; - cols--; - if (rows != cols) { - graph.coo.row[i] = rows; - graph.coo.col[i] = cols; - ++i; - } + DAWN::IO::readGraph(input_path, graph); } - file.close(); - - graph.nnz = i; - // std::cout << "nnz: " << graph.nnz << std::endl; - - transpose(graph.nnz, graph.coo); - coo2Csr(graph.rows, graph.nnz, graph.csr, graph.coo); - - delete[] graph.coo.col; - graph.coo.col = NULL; - delete[] graph.coo.row; - graph.coo.row = NULL; -} - -void Graph::readGraph_Weighted(std::string& input_path, Graph::Graph_t& graph) { - std::ifstream file(input_path); - if (!file.is_open()) { - std::cerr << "Error opening file " << input_path << std::endl; - return; - } - std::string line; - - std::priority_queue> s; - - int rows, cols; - float vals; - while (std::getline(file, line)) { - if (line[0] == '%') - continue; - std::stringstream ss(line); - ss >> rows >> cols >> vals; - rows--; - cols--; - if (rows != cols) { - s.push({rows, cols, vals}); - s.push({cols, rows, vals}); - } - } - file.close(); - - graph.nnz = s.size(); - graph.coo.col = new int[graph.nnz]; - graph.coo.row = new int[graph.nnz]; - graph.coo.val = new float[graph.nnz]; - std::fill_n(graph.coo.col, graph.nnz, 0); - std::fill_n(graph.coo.row, graph.nnz, 0); - std::fill_n(graph.coo.val, graph.nnz, 0.0f); - int i = graph.nnz - 1; - - while (!s.empty()) { - graph.coo.row[i] = std::get<0>(s.top()); - graph.coo.col[i] = std::get<1>(s.top()); - graph.coo.val[i] = std::get<2>(s.top()); - --i; - s.pop(); - } - - coo2Csr_Weighted(graph.rows, graph.nnz, graph.csr, graph.coo); - - delete[] graph.coo.col; - graph.coo.col = NULL; - delete[] graph.coo.row; - graph.coo.row = NULL; - delete[] graph.coo.val; - graph.coo.val = NULL; -} - -void Graph::readGraph_Directed_Weighted(std::string& input_path, - Graph::Graph_t& graph) { - std::ifstream file(input_path); - if (!file.is_open()) { - std::cerr << "Error opening file " << input_path << std::endl; - return; - } - std::string line; - - graph.coo.col = new int[graph.nnz]; - graph.coo.row = new int[graph.nnz]; - graph.coo.val = new float[graph.nnz]; - std::fill_n(graph.coo.col, graph.nnz, 0); - std::fill_n(graph.coo.row, graph.nnz, 0); - std::fill_n(graph.coo.val, graph.nnz, 0.0f); - - int rows, cols; - float vals; - int i = 0; - // std::cout << "nnz: " << graph.nnz << std::endl; - while (std::getline(file, line)) { - if (line[0] == '%') - continue; - std::stringstream ss(line); - ss >> rows >> cols >> vals; - rows--; - cols--; - if (rows != cols) { - graph.coo.row[i] = rows; - graph.coo.col[i] = cols; - graph.coo.val[i] = vals; - ++i; - } - } - file.close(); - - graph.nnz = i; - - transpose_Weighted(graph.nnz, graph.coo); - coo2Csr_Weighted(graph.rows, graph.nnz, graph.csr, graph.coo); - - delete[] graph.coo.col; - graph.coo.col = NULL; - delete[] graph.coo.row; - graph.coo.row = NULL; - delete[] graph.coo.val; - graph.coo.val = NULL; } - -void Graph::readList(std::string& input_path, Graph::Graph_t& graph) { - std::ifstream file(input_path); - if (!file.is_open()) { - std::cerr << "Error opening file " << input_path << std::endl; - return; - } - std::string line; - - int source; - int i = 0; - while (std::getline(file, line)) { - if (line[0] == '%') - continue; - std::stringstream ss(line); - ss >> source; - graph.msource.push_back(source); - i++; - } - file.close(); -} - -} // namespace DAWN \ No newline at end of file diff --git a/src/io.cpp b/src/io.cpp new file mode 100644 index 0000000..7e375ae --- /dev/null +++ b/src/io.cpp @@ -0,0 +1,281 @@ +#include +void DAWN::IO::readGraph(std::string& input_path, DAWN::Graph::Graph_t& graph) { + std::ifstream file(input_path); + if (!file.is_open()) { + std::cerr << "Error opening file " << input_path << std::endl; + return; + } + std::string line; + std::priority_queue> s; + int rows, cols; + + while (std::getline(file, line)) { + if (line[0] == '%') + continue; + std::stringstream ss(line); + ss >> rows >> cols; + rows--; + cols--; + if (rows != cols) { + s.push({rows, cols}); + s.push({cols, rows}); + } + } + file.close(); + + graph.nnz = s.size(); + graph.coo.col = new int[graph.nnz]; + graph.coo.row = new int[graph.nnz]; + std::fill_n(graph.coo.col, graph.nnz, 0); + std::fill_n(graph.coo.row, graph.nnz, 0); + int i = graph.nnz - 1; + + while (!s.empty()) { + graph.coo.row[i] = s.top().first; + graph.coo.col[i] = s.top().second; + --i; + s.pop(); + } + coo2Csr(graph.rows, graph.nnz, graph.csr, graph.coo); + + delete[] graph.coo.col; + graph.coo.col = NULL; + delete[] graph.coo.row; + graph.coo.row = NULL; +} + +void DAWN::IO::readGraph_Directed(std::string& input_path, + DAWN::Graph::Graph_t& graph) { + std::ifstream file(input_path); + if (!file.is_open()) { + std::cerr << "Error opening file " << input_path << std::endl; + return; + } + std::string line; + + graph.coo.col = new int[graph.nnz]; + graph.coo.row = new int[graph.nnz]; + std::fill_n(graph.coo.col, graph.nnz, 0); + std::fill_n(graph.coo.row, graph.nnz, 0); + + int rows, cols; + int i = 0; + + while (std::getline(file, line)) { + if (line[0] == '%') + continue; + std::stringstream ss(line); + ss >> rows >> cols; + rows--; + cols--; + if (rows != cols) { + graph.coo.row[i] = rows; + graph.coo.col[i] = cols; + ++i; + } + } + file.close(); + + graph.nnz = i; + // std::cout << "nnz: " << graph.nnz << std::endl; + + transpose(graph.nnz, graph.coo); + coo2Csr(graph.rows, graph.nnz, graph.csr, graph.coo); + + delete[] graph.coo.col; + graph.coo.col = NULL; + delete[] graph.coo.row; + graph.coo.row = NULL; +} + +void DAWN::IO::readGraph_Weighted(std::string& input_path, + DAWN::Graph::Graph_t& graph) { + std::ifstream file(input_path); + if (!file.is_open()) { + std::cerr << "Error opening file " << input_path << std::endl; + return; + } + std::string line; + + std::priority_queue> s; + + int rows, cols; + float vals; + while (std::getline(file, line)) { + if (line[0] == '%') + continue; + std::stringstream ss(line); + ss >> rows >> cols >> vals; + rows--; + cols--; + if (rows != cols) { + s.push({rows, cols, vals}); + s.push({cols, rows, vals}); + } + } + file.close(); + + graph.nnz = s.size(); + graph.coo.col = new int[graph.nnz]; + graph.coo.row = new int[graph.nnz]; + graph.coo.val = new float[graph.nnz]; + std::fill_n(graph.coo.col, graph.nnz, 0); + std::fill_n(graph.coo.row, graph.nnz, 0); + std::fill_n(graph.coo.val, graph.nnz, 0.0f); + int i = graph.nnz - 1; + + while (!s.empty()) { + graph.coo.row[i] = std::get<0>(s.top()); + graph.coo.col[i] = std::get<1>(s.top()); + graph.coo.val[i] = std::get<2>(s.top()); + --i; + s.pop(); + } + + coo2Csr_Weighted(graph.rows, graph.nnz, graph.csr, graph.coo); + + delete[] graph.coo.col; + graph.coo.col = NULL; + delete[] graph.coo.row; + graph.coo.row = NULL; + delete[] graph.coo.val; + graph.coo.val = NULL; +} + +void DAWN::IO::readGraph_Directed_Weighted(std::string& input_path, + DAWN::Graph::Graph_t& graph) { + std::ifstream file(input_path); + if (!file.is_open()) { + std::cerr << "Error opening file " << input_path << std::endl; + return; + } + std::string line; + + graph.coo.col = new int[graph.nnz]; + graph.coo.row = new int[graph.nnz]; + graph.coo.val = new float[graph.nnz]; + std::fill_n(graph.coo.col, graph.nnz, 0); + std::fill_n(graph.coo.row, graph.nnz, 0); + std::fill_n(graph.coo.val, graph.nnz, 0.0f); + + int rows, cols; + float vals; + int i = 0; + // std::cout << "nnz: " << graph.nnz << std::endl; + while (std::getline(file, line)) { + if (line[0] == '%') + continue; + std::stringstream ss(line); + ss >> rows >> cols >> vals; + rows--; + cols--; + if (rows != cols) { + graph.coo.row[i] = rows; + graph.coo.col[i] = cols; + graph.coo.val[i] = vals; + ++i; + } + } + file.close(); + + graph.nnz = i; + + transpose_Weighted(graph.nnz, graph.coo); + coo2Csr_Weighted(graph.rows, graph.nnz, graph.csr, graph.coo); + + delete[] graph.coo.col; + graph.coo.col = NULL; + delete[] graph.coo.row; + graph.coo.row = NULL; + delete[] graph.coo.val; + graph.coo.val = NULL; +} + +void DAWN::IO::readList(std::string& input_path, DAWN::Graph::Graph_t& graph) { + std::ifstream file(input_path); + if (!file.is_open()) { + std::cerr << "Error opening file " << input_path << std::endl; + return; + } + std::string line; + + int source; + int i = 0; + while (std::getline(file, line)) { + if (line[0] == '%') + continue; + std::stringstream ss(line); + ss >> source; + graph.msource.push_back(source); + i++; + } + file.close(); +} + +void DAWN::IO::outfile(int n, + int* result, + int source, + std::string& output_path) { + std::ofstream outfile(output_path); + if (!outfile.is_open()) { + std::cerr << "Error opening file " << output_path << std::endl; + return; + } + std::cout << "Start outfile" << std::endl; + for (int j = 0; j < n; j++) { + if ((source != j) && (result[j] > 0)) + outfile << source << " " << j << " " << result[j] << std::endl; + } + std::cout << "End outfile" << std::endl; + outfile.close(); +} + +void DAWN::IO::outfile(int n, + float* result, + int source, + std::string& output_path) { + std::ofstream outfile(output_path); + if (!outfile.is_open()) { + std::cerr << "Error opening file " << output_path << std::endl; + return; + } + int INF = 0xfffffff; + std::cout << "Start outfile" << std::endl; + for (int j = 0; j < n; j++) { + if ((source != j) && (result[j] != 0)) + outfile << source << " " << j << " " << result[j] << std::endl; + } + std::cout << "End outfile" << std::endl; + outfile.close(); +} + +void DAWN::IO::outfile(int n, int* result, std::string& output_path) { + std::ofstream outfile(output_path); + if (!outfile.is_open()) { + std::cerr << "Error opening file " << output_path << std::endl; + return; + } + std::cout << "Start outfile" << std::endl; + for (int j = 0; j < n; j++) { + if (result[j] > 0) + outfile << j << " " << result[j] << std::endl; + } + std::cout << "End outfile" << std::endl; + outfile.close(); +} + +void DAWN::IO::outfile(int n, float* result, std::string& output_path) { + std::ofstream outfile(output_path); + if (!outfile.is_open()) { + std::cerr << "Error opening file " << output_path << std::endl; + return; + } + std::cout << "Start outfile" << std::endl; + for (int j = 0; j < n; j++) { + if (result[j] != 0) + outfile << j << " " << std::fixed << std::setprecision(6) << result[j] + << std::endl; + } + std::cout << "End outfile" << std::endl; + outfile.close(); +} diff --git a/src/matrix.cpp b/src/matrix.cpp new file mode 100644 index 0000000..4ef3686 --- /dev/null +++ b/src/matrix.cpp @@ -0,0 +1,82 @@ +#include + +void DAWN::Matrix::transpose_Weighted(int nnz, DAWN::Matrix::Coo_t& coo) { + std::vector>> tmp; + for (int i = 0; i < nnz; i++) { + tmp.push_back({coo.row[i], {coo.col[i], coo.val[i]}}); + } + std::sort(tmp.begin(), tmp.end()); + for (int i = 0; i < nnz; i++) { + coo.row[i] = tmp[i].first; + coo.col[i] = tmp[i].second.first; + coo.val[i] = tmp[i].second.second; + } +} + +void DAWN::Matrix::transpose(int nnz, DAWN::Matrix::Coo_t& coo) { + std::vector> tmp; + for (int i = 0; i < nnz; i++) { + tmp.push_back({coo.row[i], coo.col[i]}); + } + std::sort(tmp.begin(), tmp.end()); + for (int i = 0; i < nnz; i++) { + coo.row[i] = tmp[i].first; + coo.col[i] = tmp[i].second; + } +} + +void DAWN::Matrix::coo2Csr_Weighted(int n, + int nnz, + DAWN::Matrix::Csr_t& csr, + DAWN::Matrix::Coo_t& coo) { + csr.val = new float[nnz]; + csr.row_ptr = new int[n + 1]; + csr.col = new int[nnz]; + + // Count the number of non-zero elements in each column + int* row_count = new int[n](); + for (int i = 0; i < nnz; i++) { + row_count[coo.row[i]]++; + } + csr.row_ptr[0] = 0; + for (int i = 1; i <= n; i++) { + csr.row_ptr[i] = csr.row_ptr[i - 1] + row_count[i - 1]; + } + +// Fill each non-zero element into val and col +#pragma omp parallel for + for (int i = 0; i < n; i++) { + for (int j = csr.row_ptr[i]; j < csr.row_ptr[i + 1]; j++) { + csr.col[j] = coo.col[j]; + csr.val[j] = coo.val[j]; + } + } + delete[] row_count; +} + +void DAWN::Matrix::coo2Csr(int n, + int nnz, + DAWN::Matrix::Csr_t& csr, + DAWN::Matrix::Coo_t& coo) { + csr.row_ptr = new int[n + 1]; + csr.col = new int[nnz]; + + // Count the number of non-zero elements in each column + int* row_count = new int[n](); + for (int i = 0; i < nnz; i++) { + row_count[coo.row[i]]++; + } + csr.row_ptr[0] = 0; + for (int i = 1; i <= n; i++) { + csr.row_ptr[i] = csr.row_ptr[i - 1] + row_count[i - 1]; + } + +// Fill each non-zero element into val and col +#pragma omp parallel for + for (int i = 0; i < n; i++) { + for (int j = csr.row_ptr[i]; j < csr.row_ptr[i + 1]; j++) { + csr.col[j] = coo.col[j]; + } + } + delete[] row_count; +} diff --git a/src/parameters.cpp b/src/parameters.cpp new file mode 100644 index 0000000..4f68ac7 --- /dev/null +++ b/src/parameters.cpp @@ -0,0 +1,98 @@ +#include + +void DAWN::IO::printHelp() { + const int optionWidth = 30; + std::cout << "Usage: program [options]\n" + << "Options:\n" + << std::left // 左对齐 + << " " << std::setw(optionWidth) << "-h, --help" + << "Show this help message and exit\n" + << " " << std::setw(optionWidth) << "-i, --input PATH" + << "Input file path\n" + << " " << std::setw(optionWidth) << "-o, --output PATH" + << "Output file path\n" + << " " << std::setw(optionWidth) << "-l, --sourceList PATH" + << "List of Source nodes file path\n" + << " " << std::setw(optionWidth) << "-p, --print [true|false]" + << "Print source information\n" + << " " << std::setw(optionWidth) << "-w, --weighted [true|false]" + << "Set graph as weighted\n" + << " " << std::setw(optionWidth) << "-s, --source NUM" + << "Source node (optional, defaults to a random value)\n"; +} + +DAWN::IO::parameters_t DAWN::IO::parameters(int argc, char* argv[]) { + DAWN::IO::parameters_t params; + int opt; + int option_index = 0; + + struct option long_options[] = { + {"help", no_argument, nullptr, 'h'}, + {"input", required_argument, nullptr, 'i'}, + {"output", required_argument, nullptr, 'o'}, + {"sourceList", required_argument, nullptr, 'l'}, + {"print", required_argument, nullptr, 'p'}, + {"weighted", required_argument, nullptr, 'w'}, + {"source", required_argument, nullptr, 's'}, + {0, 0, 0, 0}}; + + while ((opt = getopt_long(argc, argv, "hi:o:l:p:w:s:", long_options, + &option_index)) != -1) { + switch (opt) { + case 'h': + DAWN::IO::printHelp(); + exit(EXIT_SUCCESS); + case 'i': + params.input_path = optarg; + break; + case 'o': + params.output_path = optarg; + break; + case 'l': + params.sourceList_path = optarg; + break; + case 'p': + if (std::string(optarg) == "true") { + params.print = true; + } else if (std::string(optarg) == "false") { + params.print = false; + } else { + std::cerr + << "Invalid value for print option. Use 'true' or 'false'.\n"; + exit(EXIT_FAILURE); + } + break; + case 'w': + if (std::string(optarg) == "true") { + params.weighted = true; + } else if (std::string(optarg) == "false") { + params.weighted = false; + } else { + std::cerr + << "Invalid value for weighted option. Use 'true' or 'false'.\n"; + exit(EXIT_FAILURE); + } + break; + case 's': + params.source = std::atoi(optarg); + break; + default: + DAWN::IO::printHelp(); + exit(EXIT_FAILURE); + } + } + + // Check for required arguments + if (params.input_path.empty()) { + std::cerr << "Error: Missing the input file path.\n"; + DAWN::IO::printHelp(); + exit(EXIT_FAILURE); + } + + // If source is not provided, generate a random value + if (params.source == -1) { + params.source = std::rand(); + } + + return params; +} \ No newline at end of file diff --git a/src/tool.cpp b/src/tool.cpp index 3dd6942..a9683b1 100755 --- a/src/tool.cpp +++ b/src/tool.cpp @@ -24,74 +24,6 @@ float DAWN::Tool::average(float* result, int n) { return sum / i; } -void DAWN::Tool::outfile(int n, - int* result, - int source, - std::string& output_path) { - std::ofstream outfile(output_path); - if (!outfile.is_open()) { - std::cerr << "Error opening file " << output_path << std::endl; - return; - } - std::cout << "Start outfile" << std::endl; - for (int j = 0; j < n; j++) { - if ((source != j) && (result[j] > 0)) - outfile << source << " " << j << " " << result[j] << std::endl; - } - std::cout << "End outfile" << std::endl; - outfile.close(); -} - -void DAWN::Tool::outfile(int n, - float* result, - int source, - std::string& output_path) { - std::ofstream outfile(output_path); - if (!outfile.is_open()) { - std::cerr << "Error opening file " << output_path << std::endl; - return; - } - int INF = 0xfffffff; - std::cout << "Start outfile" << std::endl; - for (int j = 0; j < n; j++) { - if ((source != j) && (result[j] != 0)) - outfile << source << " " << j << " " << result[j] << std::endl; - } - std::cout << "End outfile" << std::endl; - outfile.close(); -} - -void DAWN::Tool::outfile(int n, int* result, std::string& output_path) { - std::ofstream outfile(output_path); - if (!outfile.is_open()) { - std::cerr << "Error opening file " << output_path << std::endl; - return; - } - std::cout << "Start outfile" << std::endl; - for (int j = 0; j < n; j++) { - if (result[j] > 0) - outfile << j << " " << result[j] << std::endl; - } - std::cout << "End outfile" << std::endl; - outfile.close(); -} - -void DAWN::Tool::outfile(int n, float* result, std::string& output_path) { - std::ofstream outfile(output_path); - if (!outfile.is_open()) { - std::cerr << "Error opening file " << output_path << std::endl; - return; - } - std::cout << "Start outfile" << std::endl; - for (int j = 0; j < n; j++) { - if (result[j] != 0) - outfile << j << " " << std::fixed << std::setprecision(6) << result[j] - << std::endl; - } - std::cout << "End outfile" << std::endl; - outfile.close(); -} - void DAWN::Tool::infoprint(int entry, int total, int interval,