diff --git a/apps/choleskey/CMakeLists.txt b/apps/choleskey/CMakeLists.txt index 7c9d04d..fef796e 100644 --- a/apps/choleskey/CMakeLists.txt +++ b/apps/choleskey/CMakeLists.txt @@ -1,11 +1,11 @@ -project(choleskey_stdpar LANGUAGES CXX) +project(cholesky_stdpar LANGUAGES CXX) -add_executable(choleskey_serial choleskey_serial.cpp) +add_executable(cholesky_serial cholesky_serial.cpp) target_include_directories( - choleskey_serial + cholesky_serial PRIVATE ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_LIST_DIR}/../../include ${ARGPARSE_INCLUDE_DIR} ${MDSPAN_INCLUDE_DIR}) -target_link_libraries(choleskey_serial hpcpp-core) +target_link_libraries(cholesky_serial hpcpp-core) add_executable(choleskey_stdpar choleskey_stdpar.cpp) target_link_libraries(choleskey_stdpar stdexec hpcpp-core) diff --git a/apps/choleskey/choleskey_serial.cpp b/apps/choleskey/choleskey_serial.cpp deleted file mode 100644 index c0ef896..0000000 --- a/apps/choleskey/choleskey_serial.cpp +++ /dev/null @@ -1,127 +0,0 @@ -/* - * MIT License - * - * Copyright (c) 2023 Chuanqiu He - * Copyright (c) 2023 Weile Wei - * Copyright (c) 2023 The Regents of the University of California, - * through Lawrence Berkeley National Laboratory (subject to receipt of any - * required approvals from the U.S. Dept. of Energy).All rights reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -// -// This example provides a serial(mdspan) implementation for choleskey decomposition code. - -#include -#include -#include "argparse/argparse.hpp" -#include "commons.hpp" -#include "matrixutil.hpp" - -using namespace std; - -struct solver { - - using view_2d = std::extents; - - typedef std::mdspan matrix_ms_t; - - template - matrix_ms_t Cholesky_Decomposition(std::vector& vec, int n) { - std::vector lower(n * n, 0); - - auto matrix_ms = std::mdspan(vec.data(), n, n); - auto lower_ms = std::mdspan(lower.data(), n, n); - - // Decomposing a matrix into Lower Triangular - for (int i = 0; i < matrix_ms.extent(0); i++) { - for (int j = 0; j <= i; j++) { - T sum = 0; - - if (j == i) { - // summation for diagonals - for (int k = 0; k < j; k++) - sum += pow(lower_ms(j, k), 2); - lower_ms(j, j) = sqrt(matrix_ms(i, j) - sum); - } else { - // Evaluating L(i, j) using L(j, j) - for (int k = 0; k < j; k++) - sum += (lower_ms(i, k) * lower_ms(j, k)); - lower_ms(i, j) = (matrix_ms(i, j) - sum) / lower_ms(j, j); - } - } - } - return lower_ms; - } -}; - -/////////////////////////////////////////////////////////////////////////////// -int benchmark(args_params_t const& args) { - - std::uint64_t nd = args.nd; // Number of matrix dimension. - - std::vector inputMatrix = generate_pascal_matrix(nd); - - // Create the solverobject - solver solve; - // Measure execution time. - Timer timer; - // start decomposation - auto res_matrix = solve.Cholesky_Decomposition(inputMatrix, nd); - auto time = timer.stop(); - - // Print the final results - if (args.results) { - // Displaying Lower Triangular and its Transpose - fmt::print("{:>6} {:>30}\n", "Lower Triangular", "Transpose"); - for (int i = 0; i < nd; i++) { - // Lower Triangular - for (int j = 0; j < nd; j++) - fmt::print("{:>6}\t", res_matrix(i, j)); - fmt::print("\t"); - - // Transpose of Lower Triangular - for (int j = 0; j < nd; j++) - fmt::print("{:>6}\t", res_matrix(j, i)); - fmt::print("\n"); - } - } - - if (args.time) { - fmt::print("Duration: {:f} ms\n", time); - } - - return 0; -} - -// Driver Code for testing -int main(int argc, char* argv[]) { - - // parse params - args_params_t args = argparse::parse(argc, argv); - // see if help wanted - if (args.help) { - args.print(); // prints all variables - return 0; - } - - benchmark(args); - - return 0; -} diff --git a/apps/choleskey/matrixutil.hpp b/apps/choleskey/matrixutil.hpp index 8b08fb1..85a00c4 100644 --- a/apps/choleskey/matrixutil.hpp +++ b/apps/choleskey/matrixutil.hpp @@ -1,38 +1,42 @@ #pragma once -#include -#include - -// generate positive definition matrix -template -using Matrix = std::vector>; - -template -std::vector generate_pascal_matrix(const int n) { - Matrix matrix(n, std::vector(n, static_cast(0))); - - for (int i = 0; i < n; ++i) { - for (int j = 0; j < n; ++j) { - if (i == 0 || j == 0) { - matrix[i][j] = static_cast(1); - } else { - matrix[i][j] = matrix[i][j - 1] + matrix[i - 1][j]; - } - } - } - - std::vector flattenedVector; - for (const auto& row : matrix) { - flattenedVector.insert(flattenedVector.end(), row.begin(), row.end()); - } - return std::move(flattenedVector); -} +#include "argparse/argparse.hpp" +#include "commons.hpp" // parameters define struct args_params_t : public argparse::Args { bool& results = kwarg("results", "print generated results (default: false)").set_default(true); - std::uint64_t& nd = kwarg("nd", "Number of input(positive definition) matrix dimension(<=18)").set_default(10); - std::uint64_t& np = kwarg("np", "Number of partitions").set_default(4); bool& help = flag("h, help", "print help"); bool& time = kwarg("t, time", "print time").set_default(true); + std::string& input_file = kwarg("input_file", "path to input file").set_default(""); }; + +// Function to read data from a text file and store it in a vector +template +std::vector readDataFromFile(const std::string& filename) { + std::vector data; + + // Open the file + std::ifstream file(filename); + + // Check if the file is open successfully + if (!file.is_open()) { + fmt::print("Failed to open the file: {}\n", filename); + return data; // Return an empty vector in case of failure + } + + std::string line; + while (std::getline(file, line)) { + // to parse each line into doubles and store them in the vector + T value; + std::istringstream iss(line); + while (iss >> value) { + data.push_back(value); + } + } + + // Close the file + file.close(); + + return data; +}