From 45d9648a496ff7b6d5fefbaafc3bad95ae21e643 Mon Sep 17 00:00:00 2001 From: Slaven Peles Date: Thu, 9 Jan 2025 17:02:47 -0500 Subject: [PATCH] Coding style suggestions. --- resolve/hykkt/CMakeLists.txt | 8 +- resolve/hykkt/PermClass.cpp | 132 -------- resolve/hykkt/PermClass.hpp | 211 ------------- resolve/hykkt/Permutation.cpp | 282 +++++++++++++++++ resolve/hykkt/Permutation.hpp | 78 +++++ resolve/hykkt/cpuHykktPermutationKernels.cpp | 185 ----------- resolve/hykkt/cpuHykktPermutationKernels.hpp | 116 ------- resolve/hykkt/cpuPermutationKernels.cpp | 316 +++++++++++++++++++ resolve/hykkt/cpuPermutationKernels.hpp | 64 ++++ tests/unit/hykkt/HykktPerm.hpp | 6 +- 10 files changed, 747 insertions(+), 651 deletions(-) delete mode 100644 resolve/hykkt/PermClass.cpp delete mode 100644 resolve/hykkt/PermClass.hpp create mode 100644 resolve/hykkt/Permutation.cpp create mode 100644 resolve/hykkt/Permutation.hpp delete mode 100644 resolve/hykkt/cpuHykktPermutationKernels.cpp delete mode 100644 resolve/hykkt/cpuHykktPermutationKernels.hpp create mode 100644 resolve/hykkt/cpuPermutationKernels.cpp create mode 100644 resolve/hykkt/cpuPermutationKernels.hpp diff --git a/resolve/hykkt/CMakeLists.txt b/resolve/hykkt/CMakeLists.txt index fae6ca11..abe1412f 100644 --- a/resolve/hykkt/CMakeLists.txt +++ b/resolve/hykkt/CMakeLists.txt @@ -8,8 +8,8 @@ # C++ code set(HyKKT_SRC - PermClass.cpp - cpuHykktPermutationKernels.cpp + Permutation.cpp + cpuPermutationKernels.cpp ) # C++ code that depends on CUDA SDK libraries @@ -22,8 +22,8 @@ set(HyKKT_ROCM_SRC # Header files to be installed set(HyKKT_HEADER_INSTALL - PermClass.hpp - cpuHykktPermutationKernels.hpp + Permutation.hpp + cpuPermutationKernels.hpp ) # Build shared library ReSolve::matrix diff --git a/resolve/hykkt/PermClass.cpp b/resolve/hykkt/PermClass.cpp deleted file mode 100644 index 6bf98f58..00000000 --- a/resolve/hykkt/PermClass.cpp +++ /dev/null @@ -1,132 +0,0 @@ -#include -#include -#include -#include "amd.h" -namespace ReSolve { namespace hykkt { - // Creates a class for the permutation of $H_\gamma$ in (6) - PermClass::PermClass(int n_hes, int nnz_hes, int nnz_jac) - : n_hes_(n_hes), - nnz_hes_(nnz_hes), - nnz_jac_(nnz_jac) - { - allocateWorkspace(); - } - - PermClass::~PermClass() - { - if(perm_is_default_){ - delete [] perm_; - } - delete [] rev_perm_; - delete [] perm_map_hes_; - delete [] perm_map_jac_; - delete [] perm_map_jac_tr_; - } - - void PermClass::addHInfo(int* hes_i, int* hes_j) - { - hes_i_ = hes_i; - hes_j_ = hes_j; - } - - void PermClass::addJInfo(int* jac_i, int* jac_j, int n_jac, int m_jac) - { - jac_i_ = jac_i; - jac_j_ = jac_j; - n_jac_ = n_jac; - m_jac_ = m_jac; - } - - void PermClass::addJtInfo(int* jac_tr_i, int* jac_tr_j) - { - jac_tr_i_ = jac_tr_i; - jac_tr_j_ = jac_tr_j; - } - - void PermClass::addPerm(int* custom_perm) - { - perm_is_default_ = false; - perm_ = custom_perm; - } - -// symAmd permutation of $H_\gamma$ in (6) - void PermClass::symAmd() - { - double Control[AMD_CONTROL], Info[AMD_INFO]; - - amd_defaults(Control); - amd_control(Control); - - int result = amd_order(n_hes_, hes_i_, hes_j_, perm_, Control, Info); - - if (result != AMD_OK) - { - printf("AMD failed\n"); - exit(1); - } - } - - void PermClass::invertPerm() - { - reversePerm(n_hes_, perm_, rev_perm_); - } - - void PermClass::vecMapRC(int* rhs_i, int* rhs_j) - { - makeVecMapRC(n_hes_, hes_i_, hes_j_, perm_, rev_perm_, rhs_i, rhs_j, perm_map_hes_); - } - - void PermClass::vecMapC(int* rhs_j) - { - makeVecMapC(n_jac_, jac_i_, jac_j_, rev_perm_, rhs_j, perm_map_jac_); - } - - void PermClass::vecMapR(int* rhs_i, int* rhs_j) - { - makeVecMapR(m_jac_, jac_tr_i_, jac_tr_j_, perm_, rhs_i, rhs_j, perm_map_jac_tr_); - } - - void PermClass::map_index(PermutationType permutation, - double* old_val, - double* new_val) - { - switch(permutation) - { - case PERM_V: - cpuMapIdx(n_hes_, perm_, old_val, new_val); - break; - case REV_PERM_V: - cpuMapIdx(n_hes_, rev_perm_, old_val, new_val); - break; - case PERM_HES_V: - cpuMapIdx(nnz_hes_, perm_map_hes_, old_val, new_val); - break; - case PERM_JAC_V: - cpuMapIdx(nnz_jac_, perm_map_jac_, old_val, new_val); - break; - case PERM_JAC_TR_V: - cpuMapIdx(nnz_jac_, perm_map_jac_tr_, old_val, new_val); - break; - default: - printf("Valid arguments are PERM_V, REV_PERM_V, PERM_H_V, PERM_J_V, PERM_JT_V\n"); - } - } - - void PermClass::deleteWorkspace() - { - delete [] perm_; - delete [] rev_perm_; - delete [] perm_map_hes_; - delete [] perm_map_jac_; - delete [] perm_map_jac_tr_; - } - - void PermClass::allocateWorkspace() - { - perm_ = new int[n_hes_]; - rev_perm_ = new int[n_hes_]; - perm_map_hes_ = new int[nnz_hes_]; - perm_map_jac_ = new int[nnz_jac_]; - perm_map_jac_tr_ = new int[nnz_jac_]; - } -}}// namespace hykkt // namespace ReSolve diff --git a/resolve/hykkt/PermClass.hpp b/resolve/hykkt/PermClass.hpp deleted file mode 100644 index e5c48372..00000000 --- a/resolve/hykkt/PermClass.hpp +++ /dev/null @@ -1,211 +0,0 @@ -#pragma once -namespace ReSolve { namespace hykkt -{ - enum PermutationType { PERM_V, REV_PERM_V, PERM_HES_V, PERM_JAC_V, PERM_JAC_TR_V }; - - class PermClass - { - public: - - // constructor - PermClass(int n_hes, int nnz_hes, int nnz_jac); - - // destructor - ~PermClass(); - - /* - * @brief loads CSR structure for matrix H - * - * @param hes_i - Row offsets for H - * hes_j - Column indices for H - * - * @post hes_i_ set to hes_i, hes_j_ set to hes_j - */ - void addHInfo(int* hes_i, int* hes_j); - - /* - * @brief loads CSR structure for matrix J - * - * @param jac_i - Row offsets for J - * jac_j - Column indices for j - * n_jac, m_jac - dimensions of J - * - * @post jac_i_ set to jac_i, jac_j_ set to jac_j, n_jac_ set to n_jac, m_jac_ set to m_jac - */ - void addJInfo(int* jac_i, int* jac_j, int n_jac, int m_jac); - - /* - * @brief loads CSR structure for matrix Jt - * - * @param jac_tr_i - Row offsets for Jt - * jac_tr_j - Column indices for Jt - * - * @pre - * @post jac_tr_i_ set to jac_tr_i, jac_tr_j_ set to jac_tr_j - */ - void addJtInfo(int* jac_tr_i, int* jac_tr_j); - - /* - * @brief sets custom permutation of matrix - * - * @param custom_perm - custom permutation vector - * - * @pre Member variable n_hes_ initialized to dimension of matrix - * - * @post perm points to custom_perm out of scope so perm_is_default - * set to false so that custom_perm not deleted twice in destructors, - * permutation vector copied onto device d_perm - */ - void addPerm(int* custom_perm); - - /* - * @brief Uses Symmetric Approximate Minimum Degree - * to reduce zero-fill in Cholesky Factorization - * - * @pre Member variables n_hes_, nnz_hes_, hes_i_, hes_j_ have been - * initialized to the dimensions of matrix H, the number - * of nonzeros it has, its row offsets, and column arrays - * - * @post perm is the perumation vector that implements symAmd - * on the 2x2 system - */ - void symAmd(); - - /* - * @brief Creates reverse permutation of perm and copies onto device - * - * @pre Member variables n_hes_, perm intialized to dimension of matrix - * and to a permutation vector - * - * @post rev_perm is now the reverse permuation of perm and copied onto - * the device d_perm - */ - void invertPerm(); - - /* - * @brief Creates permutation of rows and columns of matrix - * and copies onto device - * - * @param rhs_i - row offsets of permutation - * rhs_j - column indices of permutation - * - * @pre Member variables n_hes_, nnz_hes_, hes_i_, hes_j_, perm, rev_perm - * initialized to the dimension of matrix H, number of nonzeros - * in H, row offsets for H, column indices for H, permutation - * and reverse permutation of H - * - * @post perm_map_h is now permuted rows/columns of H and copied onto - * the device d_perm_map_h - */ - void vecMapRC(int* rhs_i, int* rhs_j); - - /* - * @brief Creates the permutation of the columns of matrix J - * and copies onto device - * - * @param rhs_j - column indices of permutation - * - * @pre Member variables n_jac_, nnz_jac_, jac_i_, jac_j_, rev_perm initialized - * to the dimension of matrix J, number of nonzeros in J, row - * offsets for J, column indices for J, and reverse permutation - * - * @post perm_map_jac is now the column permutation and is copied onto - * the device d_perm_map_jac - */ - void vecMapC(int* rhs_j); - - /* - * @brief Creates the permutation of the rows of matrix Jt - * and copies onto device - * - * @param rhs_i - row offsets of permutation - * rhs_j - column indices of permutation - * - * @pre Member variables m_jac_, nnz_jac_, jac_tr_i_, jac_tr_j_, initialized to - * the dimension of matrix J, the number of nonzeros in J, the - * row offsets for J transpose, the column indices for J transpose - * - * @post perm_map_jac_tr is now the permuations of the rows of J transpose - * and is copied onto the device d_perm_map_jac_tr - */ - void vecMapR(int* rhs_i, int* rhs_j); - - /* - * @brief maps the permutated values of old_val to new_val - * - * @param permutation - the type of permutation of the 2x2 system - * old_val - the old values in the matrix - * new_val - the permuted values - * - * @pre Member variables n_hes_, nnz_hes_, nnz_jac_, d_perm, d_rev_perm, - * d_perm_map_h, d_perm_map_jac, d_perm_map_jac_tr initialized to - * the dimension of matrix H, number of nonzeros in H, number - * of nonzeros in matrix J, the device permutation and reverse - * permutation vectors, the device permutation mappings for - * H, J, and J transpose - * - * @post new_val contains the permuted old_val - */ - void map_index(PermutationType permutation, - double* old_val, - double* new_val); - - void display_perm() const; - - private: - - /* - * @brief deletes memory allocated for permutation vectors - * - * @pre perm_, rev_perm_, perm_map_h, perm_map_jac, perm_map_jac_tr - * are allocated memory - * - * @post memory allocated for perm_, rev_perm_, perm_map_h, perm_map_jac, - * perm_map_jac_tr is deleted - */ - void deleteWorkspace(); - - /* - * @brief allocates memory on host for permutation vectors - * - * @pre Member variables n_hes_, nnz_hes_, nnz_jac_ are initialized to the - * dimension of matrix H, number of nonzeros in H, and number of - * nonzeros in matrix J - * - * @post perm_ and rev_perm_ are now vectors with size n_hes_, perm_map_h - * is now a vector with size nnz_hes_, perm_map_jac and perm_map_jac_tr - * are now vectors with size nnz_jac_ - */ - void allocateWorkspace(); - - // member variables - bool perm_is_default_ = true; // boolean if perm set custom - - int n_hes_; // dimension of H - int nnz_hes_; // nonzeros of H - - int n_jac_; // dimensions of J - int m_jac_; - int nnz_jac_; // nonzeros of J - - int* perm_; // permutation of 2x2 system - int* rev_perm_; // reverse of permutation - int* perm_map_hes_; // mapping of permuted H - int* perm_map_jac_; // mapping of permuted J - int* perm_map_jac_tr_; // mapping of permuted Jt - - int* hes_i_; // row offsets of csr storage of H - int* hes_j_; // column pointers of csr storage of H - - int* jac_i_; // row offsets of csr storage of J - int* jac_j_; // column pointers of csr storage of J - - int* jac_tr_i_; // row offsets of csr storage of J transpose - int* jac_tr_j_; // column pointers of csr storage of J transpose - - // right hand side of 2x2 system - double* rhs1_; // first block in vector - double* rhs2_; // second block in vector - }; -}}// namespace hykkt // namespace ReSolve - diff --git a/resolve/hykkt/Permutation.cpp b/resolve/hykkt/Permutation.cpp new file mode 100644 index 00000000..801f761f --- /dev/null +++ b/resolve/hykkt/Permutation.cpp @@ -0,0 +1,282 @@ +/** + * @file Permutation.cpp + * @author your name (you@domain.com) + * @brief + * + */ +#include + +#include "amd.h" + +#include +#include + +namespace ReSolve +{ + namespace hykkt + { + /// Creates a class for the permutation of $H_\gamma$ in (6) + Permutation::Permutation(int n_hes, int nnz_hes, int nnz_jac) + : n_hes_(n_hes), + nnz_hes_(nnz_hes), + nnz_jac_(nnz_jac) + { + allocateWorkspace(); + } + + /// Permutation destructor + Permutation::~Permutation() + { + if(perm_is_default_){ + delete [] perm_; + } + delete [] rev_perm_; + delete [] perm_map_hes_; + delete [] perm_map_jac_; + delete [] perm_map_jac_tr_; + } + + /** + * @brief loads CSR structure for matrix H + * + * @param[] hes_i - Row offsets for H + * @param[] hes_j - Column indices for H + * + * @post hes_i_ set to hes_i, hes_j_ set to hes_j + */ + void Permutation::addHInfo(int* hes_i, int* hes_j) + { + hes_i_ = hes_i; + hes_j_ = hes_j; + } + + /** + * @brief loads CSR structure for matrix J + * + * @param[] jac_i - Row offsets for J + * @param[] jac_j - Column indices for j + * @param[] n_jac - number of rows of J + * @param[] m_jac - number of columns of J + * + * @post jac_i_ set to jac_i, jac_j_ set to jac_j, n_jac_ set to n_jac, + * m_jac_ set to m_jac + */ + void Permutation::addJInfo(int* jac_i, int* jac_j, int n_jac, int m_jac) + { + jac_i_ = jac_i; + jac_j_ = jac_j; + n_jac_ = n_jac; + m_jac_ = m_jac; + } + + /** + * @brief loads CSR structure for matrix Jt + * + * @param[] jac_tr_i - Row offsets for Jt + * @param[] jac_tr_j - Column indices for Jt + * + * @pre + * @post jac_tr_i_ set to jac_tr_i, jac_tr_j_ set to jac_tr_j + */ + void Permutation::addJtInfo(int* jac_tr_i, int* jac_tr_j) + { + jac_tr_i_ = jac_tr_i; + jac_tr_j_ = jac_tr_j; + } + + /** + * @brief sets custom permutation of matrix + * + * @param[] custom_perm - custom permutation vector + * + * @pre Member variable n_hes_ initialized to dimension of matrix + * + * @post perm points to custom_perm out of scope so perm_is_default + * set to false so that custom_perm not deleted twice in destructors, + * permutation vector copied onto device d_perm + */ + void Permutation::addPerm(int* custom_perm) + { + perm_is_default_ = false; + perm_ = custom_perm; + } + + /** + * @brief Uses Symmetric Approximate Minimum Degree + * to reduce zero-fill in Cholesky Factorization + * + * @pre Member variables n_hes_, nnz_hes_, hes_i_, hes_j_ have been + * initialized to the dimensions of matrix H, the number + * of nonzeros it has, its row offsets, and column arrays + * + * @post perm is the perumation vector that implements symAmd + * on the 2x2 system + */ + void Permutation::symAmd() + { + double Control[AMD_CONTROL], Info[AMD_INFO]; + + amd_defaults(Control); + amd_control(Control); + + int result = amd_order(n_hes_, hes_i_, hes_j_, perm_, Control, Info); + + if (result != AMD_OK) + { + printf("AMD failed\n"); + exit(1); + } + } + + /** + * @brief Creates reverse permutation of perm and copies onto device + * + * @pre Member variables n_hes_, perm intialized to dimension of matrix + * and to a permutation vector + * + * @post rev_perm is now the reverse permuation of perm and copied onto + * the device d_perm + */ + void Permutation::invertPerm() + { + reversePerm(n_hes_, perm_, rev_perm_); + } + + /** + * @brief Creates permutation of rows and columns of matrix + * and copies onto device + * + * @param[] rhs_i - row offsets of permutation + * @param[] rhs_j - column indices of permutation + * + * @pre Member variables n_hes_, nnz_hes_, hes_i_, hes_j_, perm, rev_perm + * initialized to the dimension of matrix H, number of nonzeros + * in H, row offsets for H, column indices for H, permutation + * and reverse permutation of H + * + * @post perm_map_h is now permuted rows/columns of H and copied onto + * the device d_perm_map_h + */ + void Permutation::vecMapRC(int* rhs_i, int* rhs_j) + { + makeVecMapRC(n_hes_, hes_i_, hes_j_, perm_, rev_perm_, rhs_i, rhs_j, perm_map_hes_); + } + + /** + * @brief Creates the permutation of the columns of matrix J + * and copies onto device + * + * @param[] rhs_j - column indices of permutation + * + * @pre Member variables n_jac_, nnz_jac_, jac_i_, jac_j_, rev_perm initialized + * to the dimension of matrix J, number of nonzeros in J, row + * offsets for J, column indices for J, and reverse permutation + * + * @post perm_map_jac is now the column permutation and is copied onto + * the device d_perm_map_jac + */ + void Permutation::vecMapC(int* rhs_j) + { + makeVecMapC(n_jac_, jac_i_, jac_j_, rev_perm_, rhs_j, perm_map_jac_); + } + + /** + * @brief Creates the permutation of the rows of matrix Jt + * and copies onto device + * + * @param[] rhs_i - row offsets of permutation + * @param[] rhs_j - column indices of permutation + * + * @pre Member variables m_jac_, nnz_jac_, jac_tr_i_, jac_tr_j_, initialized to + * the dimension of matrix J, the number of nonzeros in J, the + * row offsets for J transpose, the column indices for J transpose + * + * @post perm_map_jac_tr is now the permuations of the rows of J transpose + * and is copied onto the device d_perm_map_jac_tr + */ + void Permutation::vecMapR(int* rhs_i, int* rhs_j) + { + makeVecMapR(m_jac_, jac_tr_i_, jac_tr_j_, perm_, rhs_i, rhs_j, perm_map_jac_tr_); + } + + /** + * @brief maps the permutated values of old_val to new_val + * + * @param[] permutation - the type of permutation of the 2x2 system + * @param[] old_val - the old values in the matrix + * @param[] new_val - the permuted values + * + * @pre Member variables n_hes_, nnz_hes_, nnz_jac_, d_perm, d_rev_perm, + * d_perm_map_h, d_perm_map_jac, d_perm_map_jac_tr initialized to + * the dimension of matrix H, number of nonzeros in H, number + * of nonzeros in matrix J, the device permutation and reverse + * permutation vectors, the device permutation mappings for + * H, J, and J transpose + * + * @post new_val contains the permuted old_val + */ + void Permutation::map_index(PermutationType permutation, + double* old_val, + double* new_val) + { + switch(permutation) + { + case PERM_V: + cpuMapIdx(n_hes_, perm_, old_val, new_val); + break; + case REV_PERM_V: + cpuMapIdx(n_hes_, rev_perm_, old_val, new_val); + break; + case PERM_HES_V: + cpuMapIdx(nnz_hes_, perm_map_hes_, old_val, new_val); + break; + case PERM_JAC_V: + cpuMapIdx(nnz_jac_, perm_map_jac_, old_val, new_val); + break; + case PERM_JAC_TR_V: + cpuMapIdx(nnz_jac_, perm_map_jac_tr_, old_val, new_val); + break; + default: + printf("Valid arguments are PERM_V, REV_PERM_V, PERM_H_V, PERM_J_V, PERM_JT_V\n"); + } + } + + /** + * @brief deletes memory allocated for permutation vectors + * + * @pre perm_, rev_perm_, perm_map_h, perm_map_jac, perm_map_jac_tr + * are allocated memory + * + * @post memory allocated for perm_, rev_perm_, perm_map_h, perm_map_jac, + * perm_map_jac_tr is deleted + */ + void Permutation::deleteWorkspace() + { + delete [] perm_; + delete [] rev_perm_; + delete [] perm_map_hes_; + delete [] perm_map_jac_; + delete [] perm_map_jac_tr_; + } + + /** + * @brief allocates memory on host for permutation vectors + * + * @pre Member variables n_hes_, nnz_hes_, nnz_jac_ are initialized to the + * dimension of matrix H, number of nonzeros in H, and number of + * nonzeros in matrix J + * + * @post perm_ and rev_perm_ are now vectors with size n_hes_, perm_map_h + * is now a vector with size nnz_hes_, perm_map_jac and perm_map_jac_tr + * are now vectors with size nnz_jac_ + */ + void Permutation::allocateWorkspace() + { + perm_ = new int[n_hes_]; + rev_perm_ = new int[n_hes_]; + perm_map_hes_ = new int[nnz_hes_]; + perm_map_jac_ = new int[nnz_jac_]; + perm_map_jac_tr_ = new int[nnz_jac_]; + } + } // namespace hykkt +} // namespace ReSolve diff --git a/resolve/hykkt/Permutation.hpp b/resolve/hykkt/Permutation.hpp new file mode 100644 index 00000000..d8e3e0e1 --- /dev/null +++ b/resolve/hykkt/Permutation.hpp @@ -0,0 +1,78 @@ +/** + * @file Permutation.hpp + * @author your name (you@domain.com) + * @brief + * + */ +#pragma once + +namespace ReSolve +{ + namespace hykkt + { + enum PermutationType { PERM_V, REV_PERM_V, PERM_HES_V, PERM_JAC_V, PERM_JAC_TR_V }; + + class Permutation + { + public: + + // constructor + Permutation(int n_hes, int nnz_hes, int nnz_jac); + + // destructor + ~Permutation(); + + void addHInfo(int* hes_i, int* hes_j); + void addJInfo(int* jac_i, int* jac_j, int n_jac, int m_jac); + void addJtInfo(int* jac_tr_i, int* jac_tr_j); + void addPerm(int* custom_perm); + void symAmd(); + void invertPerm(); + void vecMapRC(int* rhs_i, int* rhs_j); + void vecMapC(int* rhs_j); + void vecMapR(int* rhs_i, int* rhs_j); + void map_index(PermutationType permutation, + double* old_val, + double* new_val); + void display_perm() const; + + private: + + void deleteWorkspace(); + void allocateWorkspace(); + + // + // member variables + // + + bool perm_is_default_ = true; ///< boolean if perm set custom + + int n_hes_; ///< dimension of H + int nnz_hes_; ///< nonzeros of H + + int n_jac_; ///< dimensions of J + int m_jac_; + int nnz_jac_; ///< nonzeros of J + + int* perm_; ///< permutation of 2x2 system + int* rev_perm_; ///< reverse of permutation + int* perm_map_hes_; ///< mapping of permuted H + int* perm_map_jac_; ///< mapping of permuted J + int* perm_map_jac_tr_; ///< mapping of permuted Jt + + int* hes_i_; ///< row offsets of csr storage of H + int* hes_j_; ///< column pointers of csr storage of H + + int* jac_i_; ///< row offsets of csr storage of J + int* jac_j_; ///< column pointers of csr storage of J + + int* jac_tr_i_; ///< row offsets of csr storage of J transpose + int* jac_tr_j_; ///< column pointers of csr storage of J transpose + + ///< right hand side of 2x2 system + double* rhs1_; ///< first block in vector + double* rhs2_; ///< second block in vector + }; + } // namespace hykkt +} // namespace ReSolve + diff --git a/resolve/hykkt/cpuHykktPermutationKernels.cpp b/resolve/hykkt/cpuHykktPermutationKernels.cpp deleted file mode 100644 index 2a822098..00000000 --- a/resolve/hykkt/cpuHykktPermutationKernels.cpp +++ /dev/null @@ -1,185 +0,0 @@ -#include "cpuHykktPermutationKernels.hpp" -namespace ReSolve { namespace hykkt { - void cpuMapIdx(int n, const int* perm, const double* old_val, double* new_val) - { - for (int i = 0; i < n; i++) { - new_val[i] = old_val[perm[i]]; - } - } - - void selectionSort(int len, int* arr1, int* arr2) - { - int min_ind; - int temp; - for(int i = 0; i < len - 1; i++) - { - min_ind = i; - for(int j = i + 1; j < len; j++) - { - if(arr1[j] < arr1[min_ind]) - { - min_ind = j; - } - } - if(i != min_ind) - { - temp = arr1[i]; - arr1[i] = arr1[min_ind]; - arr1[min_ind] = temp; - temp = arr2[i]; - arr2[i] = arr2[min_ind]; - arr2[min_ind] = temp; - } - } - } - - inline void swap(int* arr1, int* arr2, int i, int j) { - int temp = arr1[i]; - arr1[i] = arr1[j]; - arr1[j] = temp; - - temp = arr2[i]; - arr2[i] = arr2[j]; - arr2[j] = temp; - } - - inline int partition(int* arr1, int* arr2, int low, int high) { - int pivot = arr1[high]; - int i = (low - 1); - for (int j = low; j <= high - 1; j++) { - if (arr1[j] < pivot) { - i++; - swap(arr1, arr2, i, j); - } - } - swap(arr1, arr2, i + 1, high); - return (i + 1); - } - - void quickSort(int* arr1, int* arr2, int low, int high) { - if (low < high) { - int pi = partition(arr1, arr2, low, high); - quickSort(arr1, arr2, low, pi - 1); - quickSort(arr1, arr2, pi + 1, high); - } - } - - void insertionSort(int n, int* arr1, int* arr2) - { - int i, key1, key2, j; - for (i = 1; i < n; i++) - { - key1 = arr1[i]; - key2 = arr2[i]; - - j = i - 1; - - while (j >= 0 && arr1[j] > key1) - { - arr1[j + 1] = arr1[j]; - arr2[j + 1] = arr2[j]; - j = j - 1; - } - arr1[j + 1] = key1; - arr2[j + 1] = key2; - } - } - - void makeVecMapC(int n, - const int* rows, - const int* cols, - const int* rev_perm, - int* perm_cols, - int* perm_map) - { - int row_s; - int rowlen; - for(int i = 0; i < n; i++) - { - row_s = rows[i]; - rowlen = rows[i + 1] - row_s; - for(int j = 0; j < rowlen; j++) - { - perm_map[row_s + j] = row_s + j; - perm_cols[row_s + j] = rev_perm[cols[row_s + j]]; - } - #if 0 - selectionSort(rowlen, &perm_cols[row_s], &perm_map[row_s]); - #else - //quickSort(&perm_cols[row_s], &perm_map[row_s], 0, rowlen-1); - insertionSort(rowlen, &perm_cols[row_s], &perm_map[row_s]); - #endif - } - } - - void reversePerm(int n, const int* perm, int* rev_perm) - { - for(int i = 0; i < n; i++) - { - rev_perm[perm[i]] = i; - } - } - - void makeVecMapR(int n, - const int* rows, - const int* cols, - const int* perm, - int* perm_rows, - int* perm_cols, - int* perm_map) - { - perm_rows[0] = 0; - int count = 0; - int idx; - int row_s; - int rowlen; - for(int i = 0; i < n; i++) - { - idx = perm[i]; - row_s = rows[idx]; - rowlen = rows[idx + 1] - row_s; - perm_rows[i + 1] = perm_rows[i] + rowlen; - for(int j = 0; j < rowlen; j++) - { - perm_map[count + j] = row_s + j; - perm_cols[count + j] = cols[row_s + j]; - } - count += rowlen; - } - } - - void makeVecMapRC(int n, - const int* rows, - const int* cols, - const int* perm, - const int* rev_perm, - int* perm_rows, - int* perm_cols, - int* perm_map) - { - perm_rows[0] = 0; - int count = 0; - int idx; - int row_s; - int rowlen; - for(int i = 0; i < n; i++) - { - idx = perm[i]; - row_s = rows[idx]; - rowlen = rows[idx + 1] - row_s; - perm_rows[i + 1] = perm_rows[i] + rowlen; - for(int j = 0; j < rowlen; j++) - { - perm_map[count + j] = row_s + j; - perm_cols[count + j] = rev_perm[cols[row_s + j]]; - } - #if 0 - selectionSort(rowlen, &perm_cols[count], &perm_map[count]); - #else - //quickSort(&perm_cols[count], &perm_map[count], 0, rowlen-1); - insertionSort(rowlen, &perm_cols[count], &perm_map[count]); - #endif - count += rowlen; - } - } -}} // namespace hykkt // namespace ReSolve \ No newline at end of file diff --git a/resolve/hykkt/cpuHykktPermutationKernels.hpp b/resolve/hykkt/cpuHykktPermutationKernels.hpp deleted file mode 100644 index 96171680..00000000 --- a/resolve/hykkt/cpuHykktPermutationKernels.hpp +++ /dev/null @@ -1,116 +0,0 @@ -#pragma once -namespace ReSolve { namespace hykkt { - /* - * @brief: maps the values in old_val to new_val based on perm - * - * @params: Size n of the matrix, perm - desired permutation, - * and old_val - the array to be permuted - * - * @pre: n is a positive integer, perm is an array of 0 to n-1 - * (in some order), old_val is initialized - * - * @post: new_val contains the permuted old_val - */ - void cpuMapIdx(int n, const int* perm, const double* old_val, double* new_val); - /* - * @brief: Selection sorts arr1 and arr2 w/indices - * based on increasing value in arr1 - * - * @params: Size n of the matrix, - * arr1 - the array that determines the sorting order, - * arr2- sorted based on arr1 - * - * @pre: arr1 and arr2 are arrays of length n - * - * @post: arr1 and arr2 are sorted based on increasing values in arr1 - */ - void selectionSort(int len, int* arr1, int* arr2); - - inline void swap(int* arr1, int* arr2, int i, int j); - inline int partition(int* arr1, int* arr2, int low, int high); - void quickSort(int* arr1, int* arr2, int low, int high); - - /* - * @brief: Insertion sorts arr1 and arr2 w/indices - * based on increasing value in arr1 - * - * @params: Size n of the matrix, - * arr1 - the array that determines the sorting order, - * arr2- sorted based on arr1 - * - * @pre: arr1 and arr2 are arrays of length n - * - * @post: arr1 and arr2 are sorted based on increasing values in arr1 - */ - void insertionSort(int len, int* arr1, int* arr2); - - /* - * @brief: Permutes the columns in a matrix represented by rows and cols - * - * @params: Size n of the matrix, rows and cols - representing the matrix, - * rev_perm - the permutation to be applied - * - * @pre: rev_perm has integers 0 to n-1 (permuted), - * rows and cols present valid csr storage array - * - * @post: perm_cols is now the permuted column array and perm_map stores - * the corresponding indices to facilitate permuting the values - */ - void makeVecMapC(int n, - const int* rows, - const int* cols, - const int* rev_perm, - int* perm_cols, - int* perm_map); - /* - * - * @brief: Creates a reverse permutation based on a given permutation - * - * @params: Size n of the vector, perm - original permutation - * - * @pre: perm has integers 0 to n-1 (permuted), - * - * @post: rev_perm now contains the reverse permutation - */ - void reversePerm(int n, const int* perm, int* rev_perm); - /* - * @brief: Permutes the rows in a matrix represented by rows and cols - * - * @params: Size n of the matrix, rows and cols - representing the matrix, - * perm - the permutation to be applied - * - * @pre: perm has integers 0 to n-1 (permuted), - * rows and cols present valid csr storage array - * - * @post: perm_rows and perm_cols are now the permuted rows and column arrays, - * perm_map stores the corresponding indices to facilitate permuting the values - */ - void makeVecMapR(int n, - const int* rows, - const int* cols, - const int* perm, - int* perm_rows, - int* perm_cols, - int* perm_map); - /* - * @brief: Permutes the rows and columns in a matrix represented by rows and cols - * - * @params: Size n of the matrix, rows and cols - representing the matrix, - * perm - the permutation to be applied on the rows, - * rev_perm - the permutation to be applied on the columns - * - * @pre: perm and rev_perm have corresponding integers 0 to n-1 (permuted), - * rows and cols present valid csr storage array - * - * @post: perm_rows and perm_cols are now the permuted rows and column arrays, - * perm_map stores the corresponding indices to facilitate permuting the values - */ - void makeVecMapRC(int n, - const int* rows, - const int* cols, - const int* perm, - const int* rev_perm, - int* perm_rows, - int* perm_cols, - int* perm_map); -}}// namespace hykkt // namespace ReSolve diff --git a/resolve/hykkt/cpuPermutationKernels.cpp b/resolve/hykkt/cpuPermutationKernels.cpp new file mode 100644 index 00000000..f45bd469 --- /dev/null +++ b/resolve/hykkt/cpuPermutationKernels.cpp @@ -0,0 +1,316 @@ +/** + * @file cpuPermutationKernels.cpp + * @author your name (you@domain.com) + * @brief + * + */ +#include "cpuPermutationKernels.hpp" + +namespace ReSolve +{ + namespace hykkt + { + /** + * @brief maps the values in old_val to new_val based on perm + * + * @param[in] n - matrix size + * @param[] perm - desired permutation + * @param[] old_val - the array to be permuted + * @param[] new_val - the permuted array + * + * @pre n is a positive integer, perm is an array of 0 to n-1 + * (in some order), old_val is initialized + * + * @post new_val contains the permuted old_val + */ + void cpuMapIdx(int n, const int* perm, const double* old_val, double* new_val) + { + for (int i = 0; i < n; i++) { + new_val[i] = old_val[perm[i]]; + } + } + + /** + * @brief Selection sorts arr1 and arr2 w/indices + * + * @param[in] len - Size n of the matrix, + * @param[] arr1 - the array that determines the sorting order, + * @param[] arr2 - sorted based on arr1 + * + * @pre arr1 and arr2 are arrays of length n + * + * @post arr1 and arr2 are sorted based on increasing values in arr1 + */ + void selectionSort(int len, int* arr1, int* arr2) + { + int min_ind; + int temp; + for(int i = 0; i < len - 1; i++) { + min_ind = i; + for(int j = i + 1; j < len; j++) { + if(arr1[j] < arr1[min_ind]) { + min_ind = j; + } + } + if(i != min_ind) { + temp = arr1[i]; + arr1[i] = arr1[min_ind]; + arr1[min_ind] = temp; + temp = arr2[i]; + arr2[i] = arr2[min_ind]; + arr2[min_ind] = temp; + } + } + } + + /** + * @brief + * + * @param[] arr1 + * @param[] arr2 + * @param[] i + * @param[] j + */ + inline void swap(int* arr1, int* arr2, int i, int j) + { + int temp = arr1[i]; + arr1[i] = arr1[j]; + arr1[j] = temp; + + temp = arr2[i]; + arr2[i] = arr2[j]; + arr2[j] = temp; + } + + /** + * @brief + * + * @param[] arr1 + * @param[] arr2 + * @param[] low + * @param[] high + * @return int + */ + inline int partition(int* arr1, int* arr2, int low, int high) + { + int pivot = arr1[high]; + int i = (low - 1); + for (int j = low; j <= high - 1; j++) { + if (arr1[j] < pivot) { + i++; + swap(arr1, arr2, i, j); + } + } + swap(arr1, arr2, i + 1, high); + return (i + 1); + } + + /** + * @brief + * + * @param[] arr1 + * @param[] arr2 + * @param[] low + * @param[] high + */ + void quickSort(int* arr1, int* arr2, int low, int high) + { + if (low < high) { + int pi = partition(arr1, arr2, low, high); + quickSort(arr1, arr2, low, pi - 1); + quickSort(arr1, arr2, pi + 1, high); + } + } + + /** + * @brief Insertion sorts arr1 and arr2 w/indices + * based on increasing value in arr1 + * + * @param[] n - Size of the matrix, + * @param[] arr1 - the array that determines the sorting order, + * @param[] arr2 - sorted based on arr1 + * + * @pre arr1 and arr2 are arrays of length n + * + * @post arr1 and arr2 are sorted based on increasing values in arr1 + */ + void insertionSort(int n, int* arr1, int* arr2) + { + int i, key1, key2, j; + for (i = 1; i < n; i++) { + key1 = arr1[i]; + key2 = arr2[i]; + + j = i - 1; + + while (j >= 0 && arr1[j] > key1) { + arr1[j + 1] = arr1[j]; + arr2[j + 1] = arr2[j]; + j = j - 1; + } + arr1[j + 1] = key1; + arr2[j + 1] = key2; + } + } + + /** + * @brief Permutes the columns in a matrix represented by rows and cols + * + * @param[] n + * @param[] rows + * @param[] cols + * @param[] rev_perm + * @param[] perm_cols + * @param[] perm_map + * + * @pre rev_perm has integers 0 to n-1 (permuted), + * rows and cols present valid csr storage array + * + * @post perm_cols is now the permuted column array and perm_map stores + * the corresponding indices to facilitate permuting the values + */ + void makeVecMapC(int n, + const int* rows, + const int* cols, + const int* rev_perm, + int* perm_cols, + int* perm_map) + { + int row_s; + int rowlen; + for(int i = 0; i < n; i++) { + row_s = rows[i]; + rowlen = rows[i + 1] - row_s; + for(int j = 0; j < rowlen; j++) { + perm_map[row_s + j] = row_s + j; + perm_cols[row_s + j] = rev_perm[cols[row_s + j]]; + } + // TODO: Find a way to select sorting mechanism at runtime +#if 0 + selectionSort(rowlen, &perm_cols[row_s], &perm_map[row_s]); +#else + //quickSort(&perm_cols[row_s], &perm_map[row_s], 0, rowlen-1); + insertionSort(rowlen, &perm_cols[row_s], &perm_map[row_s]); +#endif + } + } + + /** + * @brief Creates a reverse permutation based on a given permutation + * + * @param[] n + * @param[] perm + * @param[] rev_perm + * + * @pre perm has integers 0 to n-1 (permuted), + * + * @post rev_perm now contains the reverse permutation + */ + void reversePerm(int n, const int* perm, int* rev_perm) + { + for(int i = 0; i < n; i++) { + rev_perm[perm[i]] = i; + } + } + + /** + * @brief Permutes the rows in a matrix represented by rows and cols + * + * @param[] n + * @param[] rows + * @param[] cols + * @param[] perm + * @param[] perm_rows + * @param[] perm_cols + * @param[] perm_map + * + * @pre perm has integers 0 to n-1 (permuted), + * rows and cols present valid csr storage array + * + * @post perm_rows and perm_cols are now the permuted rows and column arrays, + * perm_map stores the corresponding indices to facilitate permuting the values + */ + void makeVecMapR(int n, + const int* rows, + const int* cols, + const int* perm, + int* perm_rows, + int* perm_cols, + int* perm_map) + { + perm_rows[0] = 0; + int count = 0; + int idx; + int row_s; + int rowlen; + for(int i = 0; i < n; i++) { + idx = perm[i]; + row_s = rows[idx]; + rowlen = rows[idx + 1] - row_s; + perm_rows[i + 1] = perm_rows[i] + rowlen; + for(int j = 0; j < rowlen; j++) + { + perm_map[count + j] = row_s + j; + perm_cols[count + j] = cols[row_s + j]; + } + count += rowlen; + } + } + + /** + * @brief Permutes the rows and columns in a matrix represented by rows + * and cols + * + * @param[] n + * @param[] rows + * @param[] cols + * @param[] perm + * @param[] rev_perm + * @param[] perm_rows + * @param[] perm_cols + * @param[] perm_map + * + * @pre perm and rev_perm have corresponding integers 0 to n-1 (permuted), + * rows and cols present valid csr storage array + * + * @post perm_rows and perm_cols are now the permuted rows and column + * arrays, perm_map stores the corresponding indices to facilitate + * permuting the values + */ + void makeVecMapRC(int n, + const int* rows, + const int* cols, + const int* perm, + const int* rev_perm, + int* perm_rows, + int* perm_cols, + int* perm_map) + { + perm_rows[0] = 0; + int count = 0; + int idx; + int row_s; + int rowlen; + + for(int i = 0; i < n; i++) { + idx = perm[i]; + row_s = rows[idx]; + rowlen = rows[idx + 1] - row_s; + perm_rows[i + 1] = perm_rows[i] + rowlen; + for(int j = 0; j < rowlen; j++) + { + perm_map[count + j] = row_s + j; + perm_cols[count + j] = rev_perm[cols[row_s + j]]; + } + // TODO: Find a way to select sorting mechanism at runtime +#if 0 + selectionSort(rowlen, &perm_cols[count], &perm_map[count]); +#else + //quickSort(&perm_cols[count], &perm_map[count], 0, rowlen-1); + insertionSort(rowlen, &perm_cols[count], &perm_map[count]); +#endif + count += rowlen; + } + } + } // namespace hykkt +} // namespace ReSolve \ No newline at end of file diff --git a/resolve/hykkt/cpuPermutationKernels.hpp b/resolve/hykkt/cpuPermutationKernels.hpp new file mode 100644 index 00000000..69aaab27 --- /dev/null +++ b/resolve/hykkt/cpuPermutationKernels.hpp @@ -0,0 +1,64 @@ +/** + * @file cpuPermutationKernels.hpp + * @author Shaked Regev (you@domain.com) + * @brief Prototypes of kernels for matrix and vector permutations + * + */ + +#pragma once + +namespace ReSolve +{ + namespace hykkt + { + void cpuMapIdx(int n, const int* perm, const double* old_val, double* new_val); + void selectionSort(int len, int* arr1, int* arr2); + inline void swap(int* arr1, int* arr2, int i, int j); + inline int partition(int* arr1, int* arr2, int low, int high); + void quickSort(int* arr1, int* arr2, int low, int high); + void insertionSort(int len, int* arr1, int* arr2); + void makeVecMapC(int n, + const int* rows, + const int* cols, + const int* rev_perm, + int* perm_cols, + int* perm_map); + /* + * + * @brief: + * + * @params: Size n of the vector, perm - original permutation + */ + void reversePerm(int n, const int* perm, int* rev_perm); + /* + * @brief: + * + * @params: Size n of the matrix, rows and cols - representing the matrix, + * perm - the permutation to be applied + * + */ + void makeVecMapR(int n, + const int* rows, + const int* cols, + const int* perm, + int* perm_rows, + int* perm_cols, + int* perm_map); + /* + * @brief: + * + * @params: Size n of the matrix, rows and cols - representing the matrix, + * perm - the permutation to be applied on the rows, + * rev_perm - the permutation to be applied on the columns + * + */ + void makeVecMapRC(int n, + const int* rows, + const int* cols, + const int* perm, + const int* rev_perm, + int* perm_rows, + int* perm_cols, + int* perm_map); + } // namespace hykkt +} // namespace ReSolve diff --git a/tests/unit/hykkt/HykktPerm.hpp b/tests/unit/hykkt/HykktPerm.hpp index 8d009cc5..ee7a8db4 100644 --- a/tests/unit/hykkt/HykktPerm.hpp +++ b/tests/unit/hykkt/HykktPerm.hpp @@ -5,8 +5,8 @@ #include #include #include -#include -#include +#include +#include namespace ReSolve { namespace tests { class HykktPerm : public TestBase @@ -36,7 +36,7 @@ class HykktPerm : public TestBase bool flagr = false; bool flagc = false; - ReSolve::hykkt::PermClass pc(n, nnz, nnz); + ReSolve::hykkt::Permutation pc(n, nnz, nnz); pc.addHInfo(a_i, a_j); pc.addJInfo(a_i, a_j, n, m); pc.addJtInfo(a_i, a_j);