From b5d326dc549f177297547ea7aa99b5adb9dfaef3 Mon Sep 17 00:00:00 2001 From: Shaked Regev Date: Fri, 15 Nov 2024 16:49:56 +0000 Subject: [PATCH 01/30] added new permutation kernels --- resolve/cpu/cpuHykktPermutationKernels.cpp | 184 +++++++++++++++++++++ resolve/cpu/cpuHykktPermutationKernels.hpp | 115 +++++++++++++ 2 files changed, 299 insertions(+) create mode 100644 resolve/cpu/cpuHykktPermutationKernels.cpp create mode 100644 resolve/cpu/cpuHykktPermutationKernels.hpp diff --git a/resolve/cpu/cpuHykktPermutationKernels.cpp b/resolve/cpu/cpuHykktPermutationKernels.cpp new file mode 100644 index 000000000..0c03cd349 --- /dev/null +++ b/resolve/cpu/cpuHykktPermutationKernels.cpp @@ -0,0 +1,184 @@ +#include "cpuHykktPermutationKernels.hpp" + +void cpu_map_idx(int n, int* perm, double* old_val, double* new_val) +{ + for (int i = 0; i < n; i++) { + new_val[i] = old_val[perm[i]]; + } +} + +void selection_sort2(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 insertion_sort(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 make_vec_map_c(int n, + int* rows, + int* cols, + 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 + selection_sort2(rowlen, &perm_cols[row_s], &perm_map[row_s]); +#else + //quickSort(&perm_cols[row_s], &perm_map[row_s], 0, rowlen-1); + insertion_sort(rowlen, &perm_cols[row_s], &perm_map[row_s]); +#endif + } +} + +void reverse_perm(int n, int* perm, int* rev_perm) +{ + for(int i = 0; i < n; i++) + { + rev_perm[perm[i]] = i; + } +} + +void make_vec_map_r(int n, + int* rows, + int* cols, + 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 make_vec_map_rc(int n, + int* rows, + int* cols, + int* perm, + 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 + selection_sort2(rowlen, &perm_cols[count], &perm_map[count]); +#else + //quickSort(&perm_cols[count], &perm_map[count], 0, rowlen-1); + insertion_sort(rowlen, &perm_cols[count], &perm_map[count]); +#endif + count += rowlen; + } +} diff --git a/resolve/cpu/cpuHykktPermutationKernels.hpp b/resolve/cpu/cpuHykktPermutationKernels.hpp new file mode 100644 index 000000000..bafc494cd --- /dev/null +++ b/resolve/cpu/cpuHykktPermutationKernels.hpp @@ -0,0 +1,115 @@ +#pragma once + +/* + * @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 cpu_map_idx(int n, int* perm, 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 selection_sort2(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 insertion_sort(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 make_vec_map_c(int n, + int* rows, + int* cols, + 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 reverse_perm(int n, 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 make_vec_map_r(int n, + int* rows, + int* cols, + 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 make_vec_map_rc(int n, + int* rows, + int* cols, + int* perm, + int* rev_perm, + int* perm_rows, + int* perm_cols, + int* perm_map); From df765dd4be4fd0ca3f48363475713552bf2d6a48 Mon Sep 17 00:00:00 2001 From: shakedregev Date: Thu, 21 Nov 2024 18:56:42 +0000 Subject: [PATCH 02/30] edited cmake --- CMakeLists.txt | 2 +- resolve/cpu/CMakeLists.txt | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9d4da16fa..bae37bb01 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,7 +24,7 @@ endif() option(RESOLVE_TEST_WITH_BSUB "Use `jsrun` instead of `mpirun` commands when running tests" OFF) option(RESOLVE_USE_KLU "Use KLU, AMD and COLAMD libraries from SuiteSparse" OFF) option(RESOLVE_USE_LUSOL "Build the LUSOL library" OFF) -option(RESOLVE_USE_CUDA "Use CUDA language and SDK" OFF) +option(RESOLVE_USE_CUDA "Use CUDA language and SDK" ON) option(RESOLVE_USE_HIP "Use HIP language and ROCm library" OFF) option(RESOLVE_USE_PROFILING "Set profiling tracers in the code" OFF) diff --git a/resolve/cpu/CMakeLists.txt b/resolve/cpu/CMakeLists.txt index cf323f8ff..2ea17261e 100644 --- a/resolve/cpu/CMakeLists.txt +++ b/resolve/cpu/CMakeLists.txt @@ -8,10 +8,12 @@ set(ReSolve_CPU_SRC MemoryUtils.cpp + cpuHykktPermutationKernels.cpp ) set(ReSolve_CPU_HEADER_INSTALL CpuMemory.hpp + cpuHykktPermutationKernels.hpp ) # First create dummy backend From 1dd20c47c9dd9634ed6e43ef859ad52dbc97c113 Mon Sep 17 00:00:00 2001 From: shakedregev Date: Thu, 21 Nov 2024 23:17:35 +0000 Subject: [PATCH 03/30] added Hello world tests --- tests/unit/matrix/CMakeLists.txt | 5 + tests/unit/matrix/MatrixHelloWorld.hpp | 74 ++ tests/unit/matrix/MatrixPermutationTests.hpp | 657 ++++++++++++++++++ tests/unit/matrix/runMatrixHelloWorld.cpp | 20 + .../unit/matrix/runMatrixPermutationTests.cpp | 24 + 5 files changed, 780 insertions(+) create mode 100644 tests/unit/matrix/MatrixHelloWorld.hpp create mode 100644 tests/unit/matrix/MatrixPermutationTests.hpp create mode 100644 tests/unit/matrix/runMatrixHelloWorld.cpp create mode 100644 tests/unit/matrix/runMatrixPermutationTests.cpp diff --git a/tests/unit/matrix/CMakeLists.txt b/tests/unit/matrix/CMakeLists.txt index 85d3a9af0..c0163c22f 100644 --- a/tests/unit/matrix/CMakeLists.txt +++ b/tests/unit/matrix/CMakeLists.txt @@ -18,6 +18,10 @@ target_link_libraries(runMatrixHandlerTests.exe PRIVATE ReSolve resolve_matrix) add_executable(runMatrixFactorizationTests.exe runMatrixFactorizationTests.cpp) target_link_libraries(runMatrixFactorizationTests.exe PRIVATE ReSolve resolve_matrix) +# Build matrix Hello World tests +add_executable(runMatrixHelloWorldTests.exe runMatrixHelloWorld.cpp) +target_link_libraries(runMatrixHelloWorldTests.exe PRIVATE ReSolve resolve_matrix) + # Build LUSOL-related tests if(RESOLVE_USE_LUSOL) add_executable(runLUSOLTests.exe runLUSOLTests.cpp) @@ -35,6 +39,7 @@ install(TARGETS ${installable_tests} add_test(NAME matrix_test COMMAND $) add_test(NAME matrix_handler_test COMMAND $) add_test(NAME matrix_factorization_test COMMAND $) +add_test(NAME matrix_hello_world_test COMMAND $) if(RESOLVE_USE_LUSOL) add_test(NAME lusol_factorization_test COMMAND $) endif() diff --git a/tests/unit/matrix/MatrixHelloWorld.hpp b/tests/unit/matrix/MatrixHelloWorld.hpp new file mode 100644 index 000000000..3ab7441d5 --- /dev/null +++ b/tests/unit/matrix/MatrixHelloWorld.hpp @@ -0,0 +1,74 @@ +#pragma once +#include +#include +#include +#include +#include +#include +#include + +namespace ReSolve { namespace tests { + +class MatrixHelloWorld : TestBase +{ +public: + MatrixHelloWorld(){} + virtual ~MatrixHelloWorld(){} + + + TestOutcome addition() + { + real_type a = 1.0; + real_type b = 2.0; + real_type c = a + b; + if (isEqual(c, 3.0)) + return PASS; + else + return FAIL; + } + + TestOutcome subtraction() + { + real_type a = 1.0; + real_type b = 2.0; + real_type c = a - b; + if (isEqual(c, -1.0)) + return PASS; + else + return FAIL; + } + + TestOutcome multiplication() + { + real_type a = 1.0; + real_type b = 2.0; + real_type c = a * b; + if (isEqual(c, 2.0)) + return PASS; + else + return FAIL; + } + + TestOutcome division() + { + real_type a = 1.0; + real_type b = 2.0; + real_type c = a / b; + if (isEqual(c, 0.5)) + return PASS; + else + return FAIL; + } + + TestOutcome helloWorld() + { + std::cout << "Hello World!" << std::endl; + return PASS; + } + + + + +}; // class MatrixHelloWorld + +}} // namespace ReSolve::tests diff --git a/tests/unit/matrix/MatrixPermutationTests.hpp b/tests/unit/matrix/MatrixPermutationTests.hpp new file mode 100644 index 000000000..4be9c3b83 --- /dev/null +++ b/tests/unit/matrix/MatrixPermutationTests.hpp @@ -0,0 +1,657 @@ +#pragma once +#include +#include +#include +#include +#include +#include +#include + +namespace ReSolve { namespace tests { + +class MatrixIoTests : TestBase +{ +public: + MatrixIoTests(){} + virtual ~MatrixIoTests(){} + + + TestOutcome cooMatrixImport() + { + TestStatus status; + + // Read string into istream and status it to `createCooFromFile` function. + std::istringstream file(general_coo_matrix_file_); + ReSolve::matrix::Coo* A = ReSolve::io::createCooFromFile(file); + + // Check if the matrix data was correctly loaded + status = true; + + index_type nnz_answer = static_cast(general_coo_matrix_vals_.size()); + if (A->getNnz() != nnz_answer) { + std::cout << "Incorrect NNZ read from the file ...\n"; + status = false; + } + + if (A->symmetric()) { + std::cout << "Incorrect matrix type, matrix not symmetric ...\n"; + status = false; + } + + if (!A->expanded()) { + std::cout << "Incorrect matrix type, matrix is general (expanded) ...\n"; + status = false; + } + + status *= verifyAnswer(*A, general_coo_matrix_rows_, general_coo_matrix_cols_, general_coo_matrix_vals_); + + // A->print(); + delete A; + A = nullptr; + + bool is_expand_symmetric = false; + std::istringstream file2(symmetric_duplicates_coo_matrix_file_); + A = ReSolve::io::createCooFromFile(file2, is_expand_symmetric); + + nnz_answer = static_cast(symmetric_coo_matrix_vals_.size()); + if (A->getNnz() != nnz_answer) { + std::cout << "Incorrect NNZ read from the file ...\n"; + status = false; + } + + if (!A->symmetric()) { + std::cout << "Incorrect matrix type, matrix is symmetric ...\n"; + status = false; + } + + if (A->expanded()) { + std::cout << "Incorrect matrix type, matrix not expanded ...\n"; + status = false; + } + + status *= verifyAnswer(*A, symmetric_coo_matrix_rows_, symmetric_coo_matrix_cols_, symmetric_coo_matrix_vals_); + + delete A; + A = nullptr; + + is_expand_symmetric = true; + std::istringstream file3(symmetric_duplicates_coo_matrix_file_); + A = ReSolve::io::createCooFromFile(file3, is_expand_symmetric); + + nnz_answer = static_cast(symmetric_expanded_coo_matrix_vals_.size()); + if (A->getNnz() != nnz_answer) { + std::cout << "Incorrect NNZ read from the file ...\n"; + status = false; + } + + if (!A->symmetric()) { + std::cout << "Incorrect matrix type, matrix is symmetric ...\n"; + status = false; + } + + if (!A->expanded()) { + std::cout << "Incorrect matrix type, matrix is expanded ...\n"; + status = false; + } + + status *= verifyAnswer(*A, symmetric_expanded_coo_matrix_rows_, symmetric_expanded_coo_matrix_cols_, symmetric_expanded_coo_matrix_vals_); + + delete A; + A = nullptr; + + return status.report(__func__); + } + + + TestOutcome csrMatrixImport() + { + TestStatus status; + status = true; + + bool is_expand_symmetric = true; + std::istringstream file(symmetric_duplicates_coo_matrix_file_); + ReSolve::matrix::Csr* B = ReSolve::io::createCsrFromFile(file, is_expand_symmetric); + + index_type nnz_answer = static_cast(symmetric_expanded_csr_matrix_vals_.size()); + if (B->getNnz() != nnz_answer) { + std::cout << "Incorrect NNZ read from the file ...\n"; + status = false; + } + + if (!B->symmetric()) { + std::cout << "Incorrect matrix type, matrix is symmetric ...\n"; + status = false; + } + + if (!B->expanded()) { + std::cout << "Incorrect matrix type, matrix is expanded ...\n"; + status = false; + } + + status *= verifyAnswer(*B, symmetric_expanded_csr_matrix_rows_, symmetric_expanded_csr_matrix_cols_, symmetric_expanded_csr_matrix_vals_); + + delete B; + B = nullptr; + + return status.report(__func__); + } + + + TestOutcome cooMatrixExport() + { + TestStatus status; + status = true; + + // Read string into istream and status it to `createCooFromFile` function. + std::ostringstream buffer; + + // Deep copy constant test vectors with matrix data to nonconstant ones + std::vector rows = general_coo_matrix_rows_; + std::vector cols = general_coo_matrix_cols_; + std::vector vals = general_coo_matrix_vals_; + + // Get number of matrix rows + const index_type N = 1 + *(std::max_element(rows.begin(), rows.end())); + + // Get number of matrix columns + const index_type M = 1 + *(std::max_element(cols.begin(), cols.end())); + + // Get number of nonzeros + const index_type NNZ = static_cast(general_coo_matrix_vals_.size()); + + // Create the test COO matrix + ReSolve::matrix::Coo A(N, M, NNZ, false, false); + A.setMatrixData(&rows[0], + &cols[0], + &vals[0], + memory::HOST); + + // Write the matrix to an ostream + ReSolve::io::writeMatrixToFile(&A, buffer); + status *= (buffer.str() == resolve_general_coo_matrix_file_); + + return status.report(__func__); + } + + TestOutcome csrMatrixExport() + { + TestStatus status; + status = true; + + // Read string into istream and status it to `createCooFromFile` function. + std::ostringstream buffer; + + // Deep copy constant test vectors with matrix data to nonconstant ones + std::vector rows = general_csr_matrix_rows_; + std::vector cols = general_csr_matrix_cols_; + std::vector vals = general_csr_matrix_vals_; + + // Get number of matrix rows + const index_type N = static_cast(rows.size()) - 1; + + // Get number of matrix columns + const index_type M = 1 + *(std::max_element(cols.begin(), cols.end())); + + // Get number of nonzeros + const index_type NNZ = static_cast(vals.size()); + + // Create the test CSR matrix + ReSolve::matrix::Csr A(N, M, NNZ, false, false); + A.setMatrixData(&rows[0], + &cols[0], + &vals[0], + memory::HOST); + + // Write the matrix to an ostream + ReSolve::io::writeMatrixToFile(&A, buffer); + status *= (buffer.str() == resolve_row_sorted_general_coo_matrix_file_); + + return status.report(__func__); + } + + TestOutcome cooMatrixReadAndUpdate() + { + TestStatus status; + + bool is_symmetric = true; + bool is_expanded = false; + + // Create a 5x5 COO matrix with 10 nonzeros + ReSolve::matrix::Coo A(5, 5, 10, is_symmetric, is_expanded); + A.allocateMatrixData(memory::HOST); + + // Read string into istream and status it to `createCooFromFile` function. + std::istringstream file2(symmetric_coo_matrix_file_); + + // Update matrix A with data from the matrix market file + ReSolve::io::updateMatrixFromFile(file2, &A); + + // Check if the matrix data was correctly loaded + status = true; + + index_type nnz_answer = static_cast(symmetric_coo_matrix_vals_.size()); + if (A.getNnz() != nnz_answer) { + std::cout << "Incorrect NNZ read from the file ...\n"; + status = false; + } + + status *= verifyAnswer(A, symmetric_coo_matrix_rows_, symmetric_coo_matrix_cols_, symmetric_coo_matrix_vals_); + + // Read next matrix market file into istream. This matrix has duplicates + // that need to be merged and number of nonzeros needs to be recalculated + // accordingly. + std::istringstream file(symmetric_duplicates_coo_matrix_file_); + + // Update matrix A with data from the matrix market file + ReSolve::io::updateMatrixFromFile(file, &A); + + if (A.getNnz() != nnz_answer) { + std::cout << "Incorrect NNZ read from the file ...\n"; + status = false; + } + + status *= verifyAnswer(A, symmetric_coo_matrix_rows_, symmetric_coo_matrix_cols_, symmetric_coo_matrix_vals_); + + // Create a 5x5 COO matrix with 13 nonzeros + is_expanded = true; + ReSolve::matrix::Coo B(5, 5, 13, is_symmetric, is_expanded); + B.allocateMatrixData(memory::HOST); + + // Read in symmetric matrix data + std::istringstream file3(symmetric_duplicates_coo_matrix_file_); + + // Update matrix B with data from the matrix market file + ReSolve::io::updateMatrixFromFile(file3, &B); + + nnz_answer = static_cast(symmetric_expanded_coo_matrix_vals_.size()); + if (B.getNnz() != nnz_answer) { + std::cout << "Incorrect NNZ read from the file ...\n"; + status = false; + } + + status *= verifyAnswer(B, symmetric_expanded_coo_matrix_rows_, symmetric_expanded_coo_matrix_cols_, symmetric_expanded_coo_matrix_vals_); + + return status.report(__func__); + } + + + TestOutcome csrMatrixReadAndUpdate() + { + TestStatus status; + status = true; + + bool is_symmetric = true; + bool is_expanded = true; + + ReSolve::matrix::Csr A(5, 5, 13, is_symmetric, is_expanded); + A.allocateMatrixData(memory::HOST); + + // Read in symmetric matrix data + std::istringstream file(symmetric_duplicates_coo_matrix_file_); + + // Update matrix B with data from the matrix market file + ReSolve::io::updateMatrixFromFile(file, &A); + + index_type nnz_answer = static_cast(symmetric_expanded_csr_matrix_vals_.size()); + if (A.getNnz() != nnz_answer) { + std::cout << "Incorrect NNZ read from the file ...\n"; + std::cout << A.getNnz() << " ?= " << nnz_answer << "\n"; + status = false; + } + + status *= verifyAnswer(A, symmetric_expanded_csr_matrix_rows_, symmetric_expanded_csr_matrix_cols_, symmetric_expanded_csr_matrix_vals_); + + return status.report(__func__); + } + + TestOutcome rhsVectorReadFromFile() + { + TestStatus status; + + // Read string into istream and status it to `createCooFromFile` function. + std::istringstream file(general_vector_file_); + + // Create rhs vector and load its data from the input file + real_type* rhs = ReSolve::io::createArrayFromFile(file); + + // Check if the matrix data was correctly loaded + status = true; + + for (size_t i = 0; i < general_vector_vals_.size(); ++i) { + if (!isEqual(rhs[i], general_vector_vals_[i])) + { + std::cout << "Incorrect vector value at storage element " << i << ".\n"; + status = false; + break; + } + // std::cout << i << ": " << rhs[i] << "\n"; + } + + return status.report(__func__); + } + + TestOutcome rhsVectorReadAndUpdate() + { + TestStatus status; + + // Read string into istream and status it to `createCooFromFile` function. + std::istringstream file(general_vector_file_); + + // For now let's test only the case when `updateArrayFromFile` does not allocate rhs + real_type* rhs = new real_type[5]; //nullptr; + + // Update matrix A with data from the matrix market file + ReSolve::io::updateArrayFromFile(file, &rhs); + + // Check if the matrix data was correctly loaded + status = true; + + for (size_t i = 0; i < general_vector_vals_.size(); ++i) { + if (!isEqual(rhs[i], general_vector_vals_[i])) + { + std::cout << "Incorrect vector value at storage element " << i << ".\n"; + status = false; + break; + } + // std::cout << i << ": " << rhs[i] << "\n"; + } + + return status.report(__func__); + } + +private: + bool verifyAnswer(/* const */ ReSolve::matrix::Coo& answer, + const std::vector& row_data, + const std::vector& col_data, + const std::vector& val_data) + { + for (size_t i = 0; i < val_data.size(); ++i) { + if ((answer.getRowData(memory::HOST)[i] != row_data[i]) || + (answer.getColData(memory::HOST)[i] != col_data[i]) || + (!isEqual(answer.getValues(memory::HOST)[i], val_data[i]))) + { + std::cout << "Incorrect matrix value at storage element " << i << ".\n"; + return false; + } + } + return true; + } + + bool verifyAnswer(/* const */ ReSolve::matrix::Csr& answer, + const std::vector& row_data, + const std::vector& col_data, + const std::vector& val_data) + { + for (size_t i = 0; i < val_data.size(); ++i) { + if ((answer.getColData(memory::HOST)[i] != col_data[i]) || + (!isEqual(answer.getValues(memory::HOST)[i], val_data[i]))) { + std::cout << "Incorrect matrix value at storage element " << i << ".\n"; + return false; + } + } + + for (size_t i = 0; i < row_data.size(); ++i) { + if(answer.getRowData(memory::HOST)[i] != row_data[i]) { + std::cout << "Incorrect row pointer value at storage element " << i << ".\n"; + return false; + } + } + + return true; + } + +private: + // + // Test examples + // + + /// String pretending to be matrix market file + /// Same stored in file `matrix_general_coo_ordered.mtx` + const std::string general_coo_matrix_file_ = +R"(%%MatrixMarket matrix coordinate real general +% This ASCII file represents a sparse MxN matrix with L +% nonzeros in the following Matrix Market format: +% +% +----------------------------------------------+ +% |%%MatrixMarket matrix coordinate real general | <--- header line +% |% | <--+ +% |% comments | |-- 0 or more comment lines +% |% | <--+ +% | M N L | <--- rows, columns, entries +% | I1 J1 A(I1, J1) | <--+ +% | I2 J2 A(I2, J2) | | +% | I3 J3 A(I3, J3) | |-- L lines +% | . . . | | +% | IL JL A(IL, JL) | <--+ +% +----------------------------------------------+ +% +% Indices are 1-based, i.e. A(1,1) is the first element. +% +%================================================================================= + 5 5 8 + 1 1 1.000e+00 + 2 2 1.050e+01 + 3 3 1.500e-02 + 1 4 6.000e+00 + 4 2 2.505e+02 + 4 4 -2.800e+02 + 4 5 3.332e+01 + 5 5 1.200e+01 +)"; + + /// String pretending to be matrix market file. + /// Generated by ReSolve's `writeMatrixToFile` function. + const std::string resolve_general_coo_matrix_file_ = +R"(%%MatrixMarket matrix coordinate real general +% Generated by Re::Solve +5 5 8 +1 1 1.000000000000000e+00 +1 4 6.000000000000000e+00 +2 2 1.050000000000000e+01 +3 3 1.500000000000000e-02 +4 2 2.505000000000000e+02 +4 4 -2.800000000000000e+02 +4 5 3.332000000000000e+01 +5 5 1.200000000000000e+01 +)"; + + /// String pretending to be matrix market file. + /// Generated by ReSolve's `writeMatrixToFile` function. + const std::string resolve_row_sorted_general_coo_matrix_file_ = +R"(%%MatrixMarket matrix coordinate real general +% Generated by Re::Solve +5 5 8 +1 1 1.000000000000000e+00 +1 4 6.000000000000000e+00 +2 2 1.050000000000000e+01 +3 3 1.500000000000000e-02 +4 2 2.505000000000000e+02 +4 4 -2.800000000000000e+02 +4 5 3.332000000000000e+01 +5 5 1.200000000000000e+01 +)"; + + /// Matching COO matrix data as it is supposed to be read from the file + const std::vector general_coo_matrix_rows_ = {0,0,1,2,3,3,3,4}; + const std::vector general_coo_matrix_cols_ = {0,3,1,2,1,3,4,4}; + const std::vector general_coo_matrix_vals_ = { 1.000e+00, + 6.000e+00, + 1.050e+01, + 1.500e-02, + 2.505e+02, + -2.800e+02, + 3.332e+01, + 1.200e+01 }; + + /// Matching CSR matrix data as it is supposed to be read from the file + const std::vector general_csr_matrix_rows_ = {0,2,3,4,7,8}; + const std::vector general_csr_matrix_cols_ = {0,3,1,2,1,3,4,4}; + const std::vector general_csr_matrix_vals_ = { 1.000e+00, + 6.000e+00, + 1.050e+01, + 1.500e-02, + 2.505e+02, + -2.800e+02, + 3.332e+01, + 1.200e+01 }; + + // + // [11 15] + // [ 22 23 24 ] + // A = [ 33 35] + // [ 44 ] + // [ 55] + // + const std::string symmetric_coo_matrix_file_ = +R"(%%MatrixMarket matrix coordinate real symmetric +% This ASCII file represents a sparse MxN matrix with L +% nonzeros in the following Matrix Market format: +% +% +------------------------------------------------+ +% |%%MatrixMarket matrix coordinate real symmetric | <--- header line +% |% | <--+ +% |% comments | |-- 0 or more comment lines +% |% | <--+ +% | M N L | <--- rows, columns, entries +% | I1 J1 A(I1, J1) | <--+ +% | I2 J2 A(I2, J2) | | +% | I3 J3 A(I3, J3) | |-- L lines +% | . . . | | +% | IL JL A(IL, JL) | <--+ +% +------------------------------------------------+ +% +% Indices are 1-based, i.e. A(1,1) is the first element. +% +%================================================================================= +% + 5 5 9 + 1 1 11.0 + 1 5 15.0 + 2 2 22.0 + 2 3 23.0 + 2 4 24.0 + 3 3 33.0 + 3 5 35.0 + 4 4 44.0 + 5 5 55.0 + )"; + + + // + // [11 15] + // [ 22 23 24 ] + // A = [ 33 35] + // [ 44 ] + // [ 55] + // + // A(1,1), A(5,5) and A(2,4) are stored in duplicate entries. + const std::string symmetric_duplicates_coo_matrix_file_ = +R"(%%MatrixMarket matrix coordinate real symmetric +% + 5 5 13 + 5 5 50.0 + 1 1 10.0 + 1 5 15.0 + 2 2 22.0 + 2 3 23.0 + 2 4 20.0 + 3 3 33.0 + 3 5 35.0 + 4 4 44.0 + 5 5 5.0 + 2 4 2.0 + 2 4 2.0 + 1 1 1.0 + )"; + + + /// Matching COO matrix data as it is supposed to be read from the file + const std::vector symmetric_coo_matrix_rows_ = {0,0,1,1,1,2,2,3,4}; + const std::vector symmetric_coo_matrix_cols_ = {0,4,1,2,3,2,4,3,4}; + const std::vector symmetric_coo_matrix_vals_ = {11.0, + 15.0, + 22.0, + 23.0, + 24.0, + 33.0, + 35.0, + 44.0, + 55.0}; + + // Matching expanded COO matrix data as it is supposed to be created + // + // [11 15] + // [ 22 23 24 ] + // A = [ 23 33 35] + // [ 24 44 ] + // [15 35 55] + // + // Symmetric matrix stored in COO general format + // + const std::vector symmetric_expanded_coo_matrix_rows_ = {0,0,1,1,1,2,2,2,3,3,4,4,4}; + const std::vector symmetric_expanded_coo_matrix_cols_ = {0,4,1,2,3,1,2,4,1,3,0,2,4}; + const std::vector symmetric_expanded_coo_matrix_vals_ = { 11.0, + 15.0, + 22.0, + 23.0, + 24.0, + 23.0, + 33.0, + 35.0, + 24.0, + 44.0, + 15.0, + 35.0, + 55.0 }; + + // Matching expanded CSR matrix data as it is supposed to be converted + // + // [11 15] + // [ 22 23 24 ] + // A = [ 23 33 35] + // [ 24 44 ] + // [15 35 55] + // + // Symmetric matrix in CSR general format + // + const std::vector symmetric_expanded_csr_matrix_rows_ = {0,2,5,8,10,13}; + const std::vector symmetric_expanded_csr_matrix_cols_ = {0,4,1,2,3,1,2,4,1,3,0,2,4}; + const std::vector symmetric_expanded_csr_matrix_vals_ = { 11.0, + 15.0, + 22.0, + 23.0, + 24.0, + 23.0, + 33.0, + 35.0, + 24.0, + 44.0, + 15.0, + 35.0, + 55.0 }; + + const std::string general_vector_file_ = +R"(% This ASCII file represents a sparse MxN matrix with L +% nonzeros in the following Matrix Market format: +% +% +%================================================================================= + 5 1 + 1.000e+00 + 2.000e+01 + 3.000e-02 + 4.000e+00 + 5.505e+02 +)"; + + const std::vector general_vector_vals_ = { 1.000e+00, + 2.000e+01, + 3.000e-02, + 4.000e+00, + 5.505e+02 }; + + /// Location of other test data + std::string datafiles_folder_; +}; // class MatrixIoTests + +}} // namespace ReSolve::tests diff --git a/tests/unit/matrix/runMatrixHelloWorld.cpp b/tests/unit/matrix/runMatrixHelloWorld.cpp new file mode 100644 index 000000000..5ad927ee9 --- /dev/null +++ b/tests/unit/matrix/runMatrixHelloWorld.cpp @@ -0,0 +1,20 @@ +#include +#include +#include +#include +#include +#include +#include "MatrixHelloWorld.hpp" + +int main(int, char**) +{ + ReSolve::tests::MatrixHelloWorld test; + + ReSolve::tests::TestingResults result; + result += test.addition(); + result += test.subtraction(); + result += test.multiplication(); + result += test.division(); + result += test.helloWorld(); + return result.summary(); +} \ No newline at end of file diff --git a/tests/unit/matrix/runMatrixPermutationTests.cpp b/tests/unit/matrix/runMatrixPermutationTests.cpp new file mode 100644 index 000000000..485deff04 --- /dev/null +++ b/tests/unit/matrix/runMatrixPermutationTests.cpp @@ -0,0 +1,24 @@ +#include +#include +#include +#include +#include +#include +#include "MatrixIoTests.hpp" + +int main(int, char**) +{ + ReSolve::tests::MatrixIoTests test; + + ReSolve::tests::TestingResults result; + result += test.cooMatrixImport(); + result += test.csrMatrixImport(); + result += test.cooMatrixExport(); + result += test.csrMatrixExport(); + result += test.cooMatrixReadAndUpdate(); + result += test.csrMatrixReadAndUpdate(); + result += test.rhsVectorReadFromFile(); + result += test.rhsVectorReadAndUpdate(); + + return result.summary(); +} \ No newline at end of file From 66c947f399f57f7f5c07f34aef303ef3d7e69922 Mon Sep 17 00:00:00 2001 From: shakedregev Date: Thu, 21 Nov 2024 23:18:11 +0000 Subject: [PATCH 04/30] added Hello world tests --- tests/unit/matrix/MatrixHelloWorld.hpp | 74 --- tests/unit/matrix/MatrixPermutationTests.hpp | 657 ------------------- 2 files changed, 731 deletions(-) delete mode 100644 tests/unit/matrix/MatrixHelloWorld.hpp delete mode 100644 tests/unit/matrix/MatrixPermutationTests.hpp diff --git a/tests/unit/matrix/MatrixHelloWorld.hpp b/tests/unit/matrix/MatrixHelloWorld.hpp deleted file mode 100644 index 3ab7441d5..000000000 --- a/tests/unit/matrix/MatrixHelloWorld.hpp +++ /dev/null @@ -1,74 +0,0 @@ -#pragma once -#include -#include -#include -#include -#include -#include -#include - -namespace ReSolve { namespace tests { - -class MatrixHelloWorld : TestBase -{ -public: - MatrixHelloWorld(){} - virtual ~MatrixHelloWorld(){} - - - TestOutcome addition() - { - real_type a = 1.0; - real_type b = 2.0; - real_type c = a + b; - if (isEqual(c, 3.0)) - return PASS; - else - return FAIL; - } - - TestOutcome subtraction() - { - real_type a = 1.0; - real_type b = 2.0; - real_type c = a - b; - if (isEqual(c, -1.0)) - return PASS; - else - return FAIL; - } - - TestOutcome multiplication() - { - real_type a = 1.0; - real_type b = 2.0; - real_type c = a * b; - if (isEqual(c, 2.0)) - return PASS; - else - return FAIL; - } - - TestOutcome division() - { - real_type a = 1.0; - real_type b = 2.0; - real_type c = a / b; - if (isEqual(c, 0.5)) - return PASS; - else - return FAIL; - } - - TestOutcome helloWorld() - { - std::cout << "Hello World!" << std::endl; - return PASS; - } - - - - -}; // class MatrixHelloWorld - -}} // namespace ReSolve::tests diff --git a/tests/unit/matrix/MatrixPermutationTests.hpp b/tests/unit/matrix/MatrixPermutationTests.hpp deleted file mode 100644 index 4be9c3b83..000000000 --- a/tests/unit/matrix/MatrixPermutationTests.hpp +++ /dev/null @@ -1,657 +0,0 @@ -#pragma once -#include -#include -#include -#include -#include -#include -#include - -namespace ReSolve { namespace tests { - -class MatrixIoTests : TestBase -{ -public: - MatrixIoTests(){} - virtual ~MatrixIoTests(){} - - - TestOutcome cooMatrixImport() - { - TestStatus status; - - // Read string into istream and status it to `createCooFromFile` function. - std::istringstream file(general_coo_matrix_file_); - ReSolve::matrix::Coo* A = ReSolve::io::createCooFromFile(file); - - // Check if the matrix data was correctly loaded - status = true; - - index_type nnz_answer = static_cast(general_coo_matrix_vals_.size()); - if (A->getNnz() != nnz_answer) { - std::cout << "Incorrect NNZ read from the file ...\n"; - status = false; - } - - if (A->symmetric()) { - std::cout << "Incorrect matrix type, matrix not symmetric ...\n"; - status = false; - } - - if (!A->expanded()) { - std::cout << "Incorrect matrix type, matrix is general (expanded) ...\n"; - status = false; - } - - status *= verifyAnswer(*A, general_coo_matrix_rows_, general_coo_matrix_cols_, general_coo_matrix_vals_); - - // A->print(); - delete A; - A = nullptr; - - bool is_expand_symmetric = false; - std::istringstream file2(symmetric_duplicates_coo_matrix_file_); - A = ReSolve::io::createCooFromFile(file2, is_expand_symmetric); - - nnz_answer = static_cast(symmetric_coo_matrix_vals_.size()); - if (A->getNnz() != nnz_answer) { - std::cout << "Incorrect NNZ read from the file ...\n"; - status = false; - } - - if (!A->symmetric()) { - std::cout << "Incorrect matrix type, matrix is symmetric ...\n"; - status = false; - } - - if (A->expanded()) { - std::cout << "Incorrect matrix type, matrix not expanded ...\n"; - status = false; - } - - status *= verifyAnswer(*A, symmetric_coo_matrix_rows_, symmetric_coo_matrix_cols_, symmetric_coo_matrix_vals_); - - delete A; - A = nullptr; - - is_expand_symmetric = true; - std::istringstream file3(symmetric_duplicates_coo_matrix_file_); - A = ReSolve::io::createCooFromFile(file3, is_expand_symmetric); - - nnz_answer = static_cast(symmetric_expanded_coo_matrix_vals_.size()); - if (A->getNnz() != nnz_answer) { - std::cout << "Incorrect NNZ read from the file ...\n"; - status = false; - } - - if (!A->symmetric()) { - std::cout << "Incorrect matrix type, matrix is symmetric ...\n"; - status = false; - } - - if (!A->expanded()) { - std::cout << "Incorrect matrix type, matrix is expanded ...\n"; - status = false; - } - - status *= verifyAnswer(*A, symmetric_expanded_coo_matrix_rows_, symmetric_expanded_coo_matrix_cols_, symmetric_expanded_coo_matrix_vals_); - - delete A; - A = nullptr; - - return status.report(__func__); - } - - - TestOutcome csrMatrixImport() - { - TestStatus status; - status = true; - - bool is_expand_symmetric = true; - std::istringstream file(symmetric_duplicates_coo_matrix_file_); - ReSolve::matrix::Csr* B = ReSolve::io::createCsrFromFile(file, is_expand_symmetric); - - index_type nnz_answer = static_cast(symmetric_expanded_csr_matrix_vals_.size()); - if (B->getNnz() != nnz_answer) { - std::cout << "Incorrect NNZ read from the file ...\n"; - status = false; - } - - if (!B->symmetric()) { - std::cout << "Incorrect matrix type, matrix is symmetric ...\n"; - status = false; - } - - if (!B->expanded()) { - std::cout << "Incorrect matrix type, matrix is expanded ...\n"; - status = false; - } - - status *= verifyAnswer(*B, symmetric_expanded_csr_matrix_rows_, symmetric_expanded_csr_matrix_cols_, symmetric_expanded_csr_matrix_vals_); - - delete B; - B = nullptr; - - return status.report(__func__); - } - - - TestOutcome cooMatrixExport() - { - TestStatus status; - status = true; - - // Read string into istream and status it to `createCooFromFile` function. - std::ostringstream buffer; - - // Deep copy constant test vectors with matrix data to nonconstant ones - std::vector rows = general_coo_matrix_rows_; - std::vector cols = general_coo_matrix_cols_; - std::vector vals = general_coo_matrix_vals_; - - // Get number of matrix rows - const index_type N = 1 + *(std::max_element(rows.begin(), rows.end())); - - // Get number of matrix columns - const index_type M = 1 + *(std::max_element(cols.begin(), cols.end())); - - // Get number of nonzeros - const index_type NNZ = static_cast(general_coo_matrix_vals_.size()); - - // Create the test COO matrix - ReSolve::matrix::Coo A(N, M, NNZ, false, false); - A.setMatrixData(&rows[0], - &cols[0], - &vals[0], - memory::HOST); - - // Write the matrix to an ostream - ReSolve::io::writeMatrixToFile(&A, buffer); - status *= (buffer.str() == resolve_general_coo_matrix_file_); - - return status.report(__func__); - } - - TestOutcome csrMatrixExport() - { - TestStatus status; - status = true; - - // Read string into istream and status it to `createCooFromFile` function. - std::ostringstream buffer; - - // Deep copy constant test vectors with matrix data to nonconstant ones - std::vector rows = general_csr_matrix_rows_; - std::vector cols = general_csr_matrix_cols_; - std::vector vals = general_csr_matrix_vals_; - - // Get number of matrix rows - const index_type N = static_cast(rows.size()) - 1; - - // Get number of matrix columns - const index_type M = 1 + *(std::max_element(cols.begin(), cols.end())); - - // Get number of nonzeros - const index_type NNZ = static_cast(vals.size()); - - // Create the test CSR matrix - ReSolve::matrix::Csr A(N, M, NNZ, false, false); - A.setMatrixData(&rows[0], - &cols[0], - &vals[0], - memory::HOST); - - // Write the matrix to an ostream - ReSolve::io::writeMatrixToFile(&A, buffer); - status *= (buffer.str() == resolve_row_sorted_general_coo_matrix_file_); - - return status.report(__func__); - } - - TestOutcome cooMatrixReadAndUpdate() - { - TestStatus status; - - bool is_symmetric = true; - bool is_expanded = false; - - // Create a 5x5 COO matrix with 10 nonzeros - ReSolve::matrix::Coo A(5, 5, 10, is_symmetric, is_expanded); - A.allocateMatrixData(memory::HOST); - - // Read string into istream and status it to `createCooFromFile` function. - std::istringstream file2(symmetric_coo_matrix_file_); - - // Update matrix A with data from the matrix market file - ReSolve::io::updateMatrixFromFile(file2, &A); - - // Check if the matrix data was correctly loaded - status = true; - - index_type nnz_answer = static_cast(symmetric_coo_matrix_vals_.size()); - if (A.getNnz() != nnz_answer) { - std::cout << "Incorrect NNZ read from the file ...\n"; - status = false; - } - - status *= verifyAnswer(A, symmetric_coo_matrix_rows_, symmetric_coo_matrix_cols_, symmetric_coo_matrix_vals_); - - // Read next matrix market file into istream. This matrix has duplicates - // that need to be merged and number of nonzeros needs to be recalculated - // accordingly. - std::istringstream file(symmetric_duplicates_coo_matrix_file_); - - // Update matrix A with data from the matrix market file - ReSolve::io::updateMatrixFromFile(file, &A); - - if (A.getNnz() != nnz_answer) { - std::cout << "Incorrect NNZ read from the file ...\n"; - status = false; - } - - status *= verifyAnswer(A, symmetric_coo_matrix_rows_, symmetric_coo_matrix_cols_, symmetric_coo_matrix_vals_); - - // Create a 5x5 COO matrix with 13 nonzeros - is_expanded = true; - ReSolve::matrix::Coo B(5, 5, 13, is_symmetric, is_expanded); - B.allocateMatrixData(memory::HOST); - - // Read in symmetric matrix data - std::istringstream file3(symmetric_duplicates_coo_matrix_file_); - - // Update matrix B with data from the matrix market file - ReSolve::io::updateMatrixFromFile(file3, &B); - - nnz_answer = static_cast(symmetric_expanded_coo_matrix_vals_.size()); - if (B.getNnz() != nnz_answer) { - std::cout << "Incorrect NNZ read from the file ...\n"; - status = false; - } - - status *= verifyAnswer(B, symmetric_expanded_coo_matrix_rows_, symmetric_expanded_coo_matrix_cols_, symmetric_expanded_coo_matrix_vals_); - - return status.report(__func__); - } - - - TestOutcome csrMatrixReadAndUpdate() - { - TestStatus status; - status = true; - - bool is_symmetric = true; - bool is_expanded = true; - - ReSolve::matrix::Csr A(5, 5, 13, is_symmetric, is_expanded); - A.allocateMatrixData(memory::HOST); - - // Read in symmetric matrix data - std::istringstream file(symmetric_duplicates_coo_matrix_file_); - - // Update matrix B with data from the matrix market file - ReSolve::io::updateMatrixFromFile(file, &A); - - index_type nnz_answer = static_cast(symmetric_expanded_csr_matrix_vals_.size()); - if (A.getNnz() != nnz_answer) { - std::cout << "Incorrect NNZ read from the file ...\n"; - std::cout << A.getNnz() << " ?= " << nnz_answer << "\n"; - status = false; - } - - status *= verifyAnswer(A, symmetric_expanded_csr_matrix_rows_, symmetric_expanded_csr_matrix_cols_, symmetric_expanded_csr_matrix_vals_); - - return status.report(__func__); - } - - TestOutcome rhsVectorReadFromFile() - { - TestStatus status; - - // Read string into istream and status it to `createCooFromFile` function. - std::istringstream file(general_vector_file_); - - // Create rhs vector and load its data from the input file - real_type* rhs = ReSolve::io::createArrayFromFile(file); - - // Check if the matrix data was correctly loaded - status = true; - - for (size_t i = 0; i < general_vector_vals_.size(); ++i) { - if (!isEqual(rhs[i], general_vector_vals_[i])) - { - std::cout << "Incorrect vector value at storage element " << i << ".\n"; - status = false; - break; - } - // std::cout << i << ": " << rhs[i] << "\n"; - } - - return status.report(__func__); - } - - TestOutcome rhsVectorReadAndUpdate() - { - TestStatus status; - - // Read string into istream and status it to `createCooFromFile` function. - std::istringstream file(general_vector_file_); - - // For now let's test only the case when `updateArrayFromFile` does not allocate rhs - real_type* rhs = new real_type[5]; //nullptr; - - // Update matrix A with data from the matrix market file - ReSolve::io::updateArrayFromFile(file, &rhs); - - // Check if the matrix data was correctly loaded - status = true; - - for (size_t i = 0; i < general_vector_vals_.size(); ++i) { - if (!isEqual(rhs[i], general_vector_vals_[i])) - { - std::cout << "Incorrect vector value at storage element " << i << ".\n"; - status = false; - break; - } - // std::cout << i << ": " << rhs[i] << "\n"; - } - - return status.report(__func__); - } - -private: - bool verifyAnswer(/* const */ ReSolve::matrix::Coo& answer, - const std::vector& row_data, - const std::vector& col_data, - const std::vector& val_data) - { - for (size_t i = 0; i < val_data.size(); ++i) { - if ((answer.getRowData(memory::HOST)[i] != row_data[i]) || - (answer.getColData(memory::HOST)[i] != col_data[i]) || - (!isEqual(answer.getValues(memory::HOST)[i], val_data[i]))) - { - std::cout << "Incorrect matrix value at storage element " << i << ".\n"; - return false; - } - } - return true; - } - - bool verifyAnswer(/* const */ ReSolve::matrix::Csr& answer, - const std::vector& row_data, - const std::vector& col_data, - const std::vector& val_data) - { - for (size_t i = 0; i < val_data.size(); ++i) { - if ((answer.getColData(memory::HOST)[i] != col_data[i]) || - (!isEqual(answer.getValues(memory::HOST)[i], val_data[i]))) { - std::cout << "Incorrect matrix value at storage element " << i << ".\n"; - return false; - } - } - - for (size_t i = 0; i < row_data.size(); ++i) { - if(answer.getRowData(memory::HOST)[i] != row_data[i]) { - std::cout << "Incorrect row pointer value at storage element " << i << ".\n"; - return false; - } - } - - return true; - } - -private: - // - // Test examples - // - - /// String pretending to be matrix market file - /// Same stored in file `matrix_general_coo_ordered.mtx` - const std::string general_coo_matrix_file_ = -R"(%%MatrixMarket matrix coordinate real general -% This ASCII file represents a sparse MxN matrix with L -% nonzeros in the following Matrix Market format: -% -% +----------------------------------------------+ -% |%%MatrixMarket matrix coordinate real general | <--- header line -% |% | <--+ -% |% comments | |-- 0 or more comment lines -% |% | <--+ -% | M N L | <--- rows, columns, entries -% | I1 J1 A(I1, J1) | <--+ -% | I2 J2 A(I2, J2) | | -% | I3 J3 A(I3, J3) | |-- L lines -% | . . . | | -% | IL JL A(IL, JL) | <--+ -% +----------------------------------------------+ -% -% Indices are 1-based, i.e. A(1,1) is the first element. -% -%================================================================================= - 5 5 8 - 1 1 1.000e+00 - 2 2 1.050e+01 - 3 3 1.500e-02 - 1 4 6.000e+00 - 4 2 2.505e+02 - 4 4 -2.800e+02 - 4 5 3.332e+01 - 5 5 1.200e+01 -)"; - - /// String pretending to be matrix market file. - /// Generated by ReSolve's `writeMatrixToFile` function. - const std::string resolve_general_coo_matrix_file_ = -R"(%%MatrixMarket matrix coordinate real general -% Generated by Re::Solve -5 5 8 -1 1 1.000000000000000e+00 -1 4 6.000000000000000e+00 -2 2 1.050000000000000e+01 -3 3 1.500000000000000e-02 -4 2 2.505000000000000e+02 -4 4 -2.800000000000000e+02 -4 5 3.332000000000000e+01 -5 5 1.200000000000000e+01 -)"; - - /// String pretending to be matrix market file. - /// Generated by ReSolve's `writeMatrixToFile` function. - const std::string resolve_row_sorted_general_coo_matrix_file_ = -R"(%%MatrixMarket matrix coordinate real general -% Generated by Re::Solve -5 5 8 -1 1 1.000000000000000e+00 -1 4 6.000000000000000e+00 -2 2 1.050000000000000e+01 -3 3 1.500000000000000e-02 -4 2 2.505000000000000e+02 -4 4 -2.800000000000000e+02 -4 5 3.332000000000000e+01 -5 5 1.200000000000000e+01 -)"; - - /// Matching COO matrix data as it is supposed to be read from the file - const std::vector general_coo_matrix_rows_ = {0,0,1,2,3,3,3,4}; - const std::vector general_coo_matrix_cols_ = {0,3,1,2,1,3,4,4}; - const std::vector general_coo_matrix_vals_ = { 1.000e+00, - 6.000e+00, - 1.050e+01, - 1.500e-02, - 2.505e+02, - -2.800e+02, - 3.332e+01, - 1.200e+01 }; - - /// Matching CSR matrix data as it is supposed to be read from the file - const std::vector general_csr_matrix_rows_ = {0,2,3,4,7,8}; - const std::vector general_csr_matrix_cols_ = {0,3,1,2,1,3,4,4}; - const std::vector general_csr_matrix_vals_ = { 1.000e+00, - 6.000e+00, - 1.050e+01, - 1.500e-02, - 2.505e+02, - -2.800e+02, - 3.332e+01, - 1.200e+01 }; - - // - // [11 15] - // [ 22 23 24 ] - // A = [ 33 35] - // [ 44 ] - // [ 55] - // - const std::string symmetric_coo_matrix_file_ = -R"(%%MatrixMarket matrix coordinate real symmetric -% This ASCII file represents a sparse MxN matrix with L -% nonzeros in the following Matrix Market format: -% -% +------------------------------------------------+ -% |%%MatrixMarket matrix coordinate real symmetric | <--- header line -% |% | <--+ -% |% comments | |-- 0 or more comment lines -% |% | <--+ -% | M N L | <--- rows, columns, entries -% | I1 J1 A(I1, J1) | <--+ -% | I2 J2 A(I2, J2) | | -% | I3 J3 A(I3, J3) | |-- L lines -% | . . . | | -% | IL JL A(IL, JL) | <--+ -% +------------------------------------------------+ -% -% Indices are 1-based, i.e. A(1,1) is the first element. -% -%================================================================================= -% - 5 5 9 - 1 1 11.0 - 1 5 15.0 - 2 2 22.0 - 2 3 23.0 - 2 4 24.0 - 3 3 33.0 - 3 5 35.0 - 4 4 44.0 - 5 5 55.0 - )"; - - - // - // [11 15] - // [ 22 23 24 ] - // A = [ 33 35] - // [ 44 ] - // [ 55] - // - // A(1,1), A(5,5) and A(2,4) are stored in duplicate entries. - const std::string symmetric_duplicates_coo_matrix_file_ = -R"(%%MatrixMarket matrix coordinate real symmetric -% - 5 5 13 - 5 5 50.0 - 1 1 10.0 - 1 5 15.0 - 2 2 22.0 - 2 3 23.0 - 2 4 20.0 - 3 3 33.0 - 3 5 35.0 - 4 4 44.0 - 5 5 5.0 - 2 4 2.0 - 2 4 2.0 - 1 1 1.0 - )"; - - - /// Matching COO matrix data as it is supposed to be read from the file - const std::vector symmetric_coo_matrix_rows_ = {0,0,1,1,1,2,2,3,4}; - const std::vector symmetric_coo_matrix_cols_ = {0,4,1,2,3,2,4,3,4}; - const std::vector symmetric_coo_matrix_vals_ = {11.0, - 15.0, - 22.0, - 23.0, - 24.0, - 33.0, - 35.0, - 44.0, - 55.0}; - - // Matching expanded COO matrix data as it is supposed to be created - // - // [11 15] - // [ 22 23 24 ] - // A = [ 23 33 35] - // [ 24 44 ] - // [15 35 55] - // - // Symmetric matrix stored in COO general format - // - const std::vector symmetric_expanded_coo_matrix_rows_ = {0,0,1,1,1,2,2,2,3,3,4,4,4}; - const std::vector symmetric_expanded_coo_matrix_cols_ = {0,4,1,2,3,1,2,4,1,3,0,2,4}; - const std::vector symmetric_expanded_coo_matrix_vals_ = { 11.0, - 15.0, - 22.0, - 23.0, - 24.0, - 23.0, - 33.0, - 35.0, - 24.0, - 44.0, - 15.0, - 35.0, - 55.0 }; - - // Matching expanded CSR matrix data as it is supposed to be converted - // - // [11 15] - // [ 22 23 24 ] - // A = [ 23 33 35] - // [ 24 44 ] - // [15 35 55] - // - // Symmetric matrix in CSR general format - // - const std::vector symmetric_expanded_csr_matrix_rows_ = {0,2,5,8,10,13}; - const std::vector symmetric_expanded_csr_matrix_cols_ = {0,4,1,2,3,1,2,4,1,3,0,2,4}; - const std::vector symmetric_expanded_csr_matrix_vals_ = { 11.0, - 15.0, - 22.0, - 23.0, - 24.0, - 23.0, - 33.0, - 35.0, - 24.0, - 44.0, - 15.0, - 35.0, - 55.0 }; - - const std::string general_vector_file_ = -R"(% This ASCII file represents a sparse MxN matrix with L -% nonzeros in the following Matrix Market format: -% -% -%================================================================================= - 5 1 - 1.000e+00 - 2.000e+01 - 3.000e-02 - 4.000e+00 - 5.505e+02 -)"; - - const std::vector general_vector_vals_ = { 1.000e+00, - 2.000e+01, - 3.000e-02, - 4.000e+00, - 5.505e+02 }; - - /// Location of other test data - std::string datafiles_folder_; -}; // class MatrixIoTests - -}} // namespace ReSolve::tests From 7e5393215f706a20be857c6deccb9842560f30c6 Mon Sep 17 00:00:00 2001 From: shakedregev Date: Fri, 3 Jan 2025 23:41:52 +0000 Subject: [PATCH 05/30] resolved conflict --- resolve/CMakeLists.txt | 1 + resolve/hykkt/cpu/CMakeLists.txt | 24 +++ resolve/hykkt/cpu/PermClass.cpp | 133 ++++++++++++ resolve/hykkt/cpu/PermClass.hpp | 199 ++++++++++++++++++ .../cpu/cpuHykktPermutationKernels.cpp | 0 .../cpu/cpuHykktPermutationKernels.hpp | 0 tests/unit/CMakeLists.txt | 1 + tests/unit/hykkt/CMakeLists.txt | 13 ++ tests/unit/hykkt/HykktHelloWorld.hpp | 74 +++++++ tests/unit/hykkt/runHykktHelloWorld.cpp | 20 ++ tests/unit/matrix/MatrixHelloWorld.hpp | 74 +++++++ 11 files changed, 539 insertions(+) create mode 100644 resolve/hykkt/cpu/CMakeLists.txt create mode 100644 resolve/hykkt/cpu/PermClass.cpp create mode 100644 resolve/hykkt/cpu/PermClass.hpp rename resolve/{ => hykkt}/cpu/cpuHykktPermutationKernels.cpp (100%) rename resolve/{ => hykkt}/cpu/cpuHykktPermutationKernels.hpp (100%) create mode 100644 tests/unit/hykkt/CMakeLists.txt create mode 100644 tests/unit/hykkt/HykktHelloWorld.hpp create mode 100644 tests/unit/hykkt/runHykktHelloWorld.cpp create mode 100644 tests/unit/matrix/MatrixHelloWorld.hpp diff --git a/resolve/CMakeLists.txt b/resolve/CMakeLists.txt index a71b9e6de..317931f86 100644 --- a/resolve/CMakeLists.txt +++ b/resolve/CMakeLists.txt @@ -81,6 +81,7 @@ add_subdirectory(workspace) add_subdirectory(vector) add_subdirectory(matrix) add_subdirectory(random) +add_subdirectory(hykkt/cpu) # Build shared library ReSolve add_library(resolve_tpl INTERFACE) diff --git a/resolve/hykkt/cpu/CMakeLists.txt b/resolve/hykkt/cpu/CMakeLists.txt new file mode 100644 index 000000000..2943dddc7 --- /dev/null +++ b/resolve/hykkt/cpu/CMakeLists.txt @@ -0,0 +1,24 @@ +#[[ + +@brief Build HyKKT + +@author Shaked Regev + +]] + +set (HyKKT_SRC + PermClass.cpp + cpuHykktPermutationKernels.cpp +) + +set (HyKKT_HEADER_INSTALL + PermClass.hpp + cpuHykktPermutationKernels.hpp +) + +add_library(hyktt SHARED ${HyKKT_SRC}) +target_link_libraries(hyktt PRIVATE hykkt_logger) +# install include headers +install(FILES ${HyKKT_HEADER_INSTALL} DESTINATION include/hykkt) + + diff --git a/resolve/hykkt/cpu/PermClass.cpp b/resolve/hykkt/cpu/PermClass.cpp new file mode 100644 index 000000000..81ab46e6d --- /dev/null +++ b/resolve/hykkt/cpu/PermClass.cpp @@ -0,0 +1,133 @@ +#include "PermClass.hpp" +#include "cpuHykktPermutationKernels.hpp" +#include +#include +#include "amd.h" + +// Creates a class for the permutation of $H_\gamma$ in (6) +PermClass::PermClass(int n_h, int nnz_h, int nnz_j) + : n_h_(n_h), + nnz_h_(nnz_h), + nnz_j_(nnz_j) + { + allocate_workspace(); + } + + PermClass::~PermClass() + { + if(perm_is_default_){ + delete [] perm_; + } + delete [] rev_perm_; + delete [] perm_map_h_; + delete [] perm_map_j_; + delete [] perm_map_jt_; + } + + void PermClass::add_h_info(int* h_i, int* h_j) + { + h_i_ = h_i; + h_j_ = h_j; + } + + void PermClass::add_j_info(int* j_i, int* j_j, int n_j, int m_j) + { + j_i_ = j_i; + j_j_ = j_j; + n_j_ = n_j; + m_j_ = m_j; + } + + void PermClass::add_jt_info(int* jt_i, int* jt_j) + { + jt_i_ = jt_i; + jt_j_ = jt_j; + } + + void PermClass::add_perm(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_h_, h_i_, h_j_, perm_, Control, Info); + + if (result != AMD_OK) + { + printf("AMD failed\n"); + exit(1); + } + } + + void PermClass::invert_perm() + { + reverse_perm(n_h_, perm_, rev_perm_); + } + + void PermClass::vec_map_rc(int* b_i, int* b_j) + { + make_vec_map_rc(n_h_, h_i_, h_j_, perm_, rev_perm_, b_i, b_j, perm_map_h_); + } + + void PermClass::vec_map_c(int* b_j) + { + make_vec_map_c(n_j_, j_i_, j_j_, rev_perm_, b_j, perm_map_j_); + } + + void PermClass::vec_map_r(int* b_i, int* b_j) + { + make_vec_map_r(m_j_, jt_i_, jt_j_, perm_, b_i, b_j, perm_map_jt_); + } + + void PermClass::map_index(Permutation_Type permutation, + double* old_val, + double* new_val) + { + switch(permutation) + { + case perm_v: + fun_map_idx(n_h_, d_perm_, old_val, new_val); + break; + case rev_perm_v: + fun_map_idx(n_h_, d_rev_perm_, old_val, new_val); + break; + case perm_h_v: + fun_map_idx(nnz_h_, d_perm_map_h_, old_val, new_val); + break; + case perm_j_v: + fun_map_idx(nnz_j_, d_perm_map_j_, old_val, new_val); + break; + case perm_jt_v: + fun_map_idx(nnz_j_, d_perm_map_jt_, 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::display_perm() const + { + displayDeviceVector(d_perm_, + n_h_, + 0, + 10, + "PERMUTATION"); + + } + + void PermClass::allocate_workspace() + { + perm_ = new int[n_h_]; + rev_perm_ = new int[n_h_]; + perm_map_h_ = new int[nnz_h_]; + perm_map_j_ = new int[nnz_j_]; + perm_map_jt_ = new int[nnz_j_]; + } diff --git a/resolve/hykkt/cpu/PermClass.hpp b/resolve/hykkt/cpu/PermClass.hpp new file mode 100644 index 000000000..3a8b10c29 --- /dev/null +++ b/resolve/hykkt/cpu/PermClass.hpp @@ -0,0 +1,199 @@ +#pragma once + + +enum Permutation_Type { perm_v, rev_perm_v, perm_h_v, perm_j_v, perm_jt_v }; + +class PermClass +{ +public: + + // constructor + PermClass(int n_h, int nnz_h, int nnz_j); + + // destructor + ~PermClass(); + + /* + * @brief loads CSR structure for matrix H + * + * @param h_i - Row offsets for H + * h_j - Column indices for H + * + * @post h_i_ set to h_i, h_j_ set to h_j + */ + void add_h_info(int* h_i, int* h_j); + + /* + * @brief loads CSR structure for matrix J + * + * @param j_i - Row offsets for J + * j_j - Column indices for j + * n_j, m_j - dimensions of J + * + * @post j_i_ set to j_i, j_j_ set to j_j, n_j_ set to n_j, m_j_ set to m_j + */ + void add_j_info(int* j_i, int* j_j, int n_j, int m_j); + + /* + * @brief loads CSR structure for matrix Jt + * + * @param jt_i - Row offsets for Jt + * jt_j - Column indices for Jt + * + * @pre + * @post jt_i_ set to jt_i, jt_j_ set to jt_j + */ + void add_jt_info(int* jt_i, int* jt_j); + + /* + * @brief sets custom permutation of matrix + * + * @param custom_perm - custom permutation vector + * + * @pre Member variable n_h_ 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 add_perm(int* custom_perm); + + /* + * @brief Uses Symmetric Approximate Minimum Degree + * to reduce zero-fill in Cholesky Factorization + * + * @pre Member variables n_h_, nnz_h_, h_i_, h_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_h_, 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 invert_perm(); + + /* + * @brief Creates permutation of rows and columns of matrix + * and copies onto device + * + * @param b_i - row offsets of permutation + * b_j - column indices of permutation + * + * @pre Member variables n_h_, nnz_h_, h_i_, h_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 vec_map_rc(int* b_i, int* b_j); + + /* + * @brief Creates the permutation of the columns of matrix J + * and copies onto device + * + * @param b_j - column indices of permutation + * + * @pre Member variables n_j_, nnz_j_, j_i_, j_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_j is now the column permutation and is copied onto + * the device d_perm_map_j + */ + void vec_map_c(int* b_j); + + /* + * @brief Creates the permutation of the rows of matrix Jt + * and copies onto device + * + * @param b_i - row offsets of permutation + * b_j - column indices of permutation + * + * @pre Member variables m_j_, nnz_j_, jt_i_, jt_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_jt is now the permuations of the rows of J transpose + * and is copied onto the device d_perm_map_jt + */ + void vec_map_r(int* b_i, int* b_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_h_, nnz_h_, nnz_j_, d_perm, d_rev_perm, + * d_perm_map_h, d_perm_map_j, d_perm_map_jt 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(Permutation_Type permutation, + double* old_val, + double* new_val); + + void display_perm() const; + +private: + +/* + * @brief allocates memory on host for permutation vectors + * + * @pre Member variables n_h_, nnz_h_, nnz_j_ 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_h_, perm_map_h + * is now a vector with size nnz_h_, perm_map_j and perm_map_jt + * are now vectors with size nnz_j_ +*/ + void allocate_workspace(); + + // member variables + bool perm_is_default_ = true; // boolean if perm set custom + + int n_h_; // dimension of H + int nnz_h_; // nonzeros of H + + int n_j_; // dimensions of J + int m_j_; + int nnz_j_; // nonzeros of J + + int* perm_; // permutation of 2x2 system + int* rev_perm_; // reverse of permutation + int* perm_map_h_; // mapping of permuted H + int* perm_map_j_; // mapping of permuted J + int* perm_map_jt_; // mapping of permuted Jt + + int* h_i_; // row offsets of csr storage of H + int* h_j_; // column pointers of csr storage of H + + int* j_i_; // row offsets of csr storage of J + int* j_j_; // column pointers of csr storage of J + + int* jt_i_; // row offsets of csr storage of J transpose + int* jt_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 +}; + diff --git a/resolve/cpu/cpuHykktPermutationKernels.cpp b/resolve/hykkt/cpu/cpuHykktPermutationKernels.cpp similarity index 100% rename from resolve/cpu/cpuHykktPermutationKernels.cpp rename to resolve/hykkt/cpu/cpuHykktPermutationKernels.cpp diff --git a/resolve/cpu/cpuHykktPermutationKernels.hpp b/resolve/hykkt/cpu/cpuHykktPermutationKernels.hpp similarity index 100% rename from resolve/cpu/cpuHykktPermutationKernels.hpp rename to resolve/hykkt/cpu/cpuHykktPermutationKernels.hpp diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt index b58a9419a..d631ccc64 100644 --- a/tests/unit/CMakeLists.txt +++ b/tests/unit/CMakeLists.txt @@ -11,3 +11,4 @@ add_subdirectory(vector) add_subdirectory(utilities) add_subdirectory(memory) add_subdirectory(params) +add_subdirectory(hykkt) diff --git a/tests/unit/hykkt/CMakeLists.txt b/tests/unit/hykkt/CMakeLists.txt new file mode 100644 index 000000000..bafaea726 --- /dev/null +++ b/tests/unit/hykkt/CMakeLists.txt @@ -0,0 +1,13 @@ +#[[ + +@brief Build ReSolve HyKKT unit tests + +@author Shaked Regev + +]] + +# Build matrix Hello World tests +add_executable(runHykktHelloWorldTests.exe runHykktHelloWorld.cpp) +target_link_libraries(runHykktHelloWorldTests.exe PRIVATE ReSolve) + +add_test(NAME hykkt_hello_world_test COMMAND $) diff --git a/tests/unit/hykkt/HykktHelloWorld.hpp b/tests/unit/hykkt/HykktHelloWorld.hpp new file mode 100644 index 000000000..dcfe9e214 --- /dev/null +++ b/tests/unit/hykkt/HykktHelloWorld.hpp @@ -0,0 +1,74 @@ +#pragma once +#include +#include +#include +#include +#include +#include +#include + +namespace ReSolve { namespace tests { + +class HykktHelloWorld : TestBase +{ +public: + HykktHelloWorld(){} + virtual ~HykktHelloWorld(){} + + + TestOutcome addition() + { + real_type a = 1.0; + real_type b = 2.0; + real_type c = a + b; + if (isEqual(c, 3.0)) + return PASS; + else + return FAIL; + } + + TestOutcome subtraction() + { + real_type a = 1.0; + real_type b = 2.0; + real_type c = a - b; + if (isEqual(c, -1.0)) + return PASS; + else + return FAIL; + } + + TestOutcome multiplication() + { + real_type a = 1.0; + real_type b = 2.0; + real_type c = a * b; + if (isEqual(c, 2.0)) + return PASS; + else + return FAIL; + } + + TestOutcome division() + { + real_type a = 1.0; + real_type b = 2.0; + real_type c = a / b; + if (isEqual(c, 0.5)) + return PASS; + else + return FAIL; + } + + TestOutcome helloWorld() + { + std::cout << "Hello World!" << std::endl; + return PASS; + } + + + + +}; // class HykktHelloWorld + +}} // namespace ReSolve::tests diff --git a/tests/unit/hykkt/runHykktHelloWorld.cpp b/tests/unit/hykkt/runHykktHelloWorld.cpp new file mode 100644 index 000000000..c0de933bf --- /dev/null +++ b/tests/unit/hykkt/runHykktHelloWorld.cpp @@ -0,0 +1,20 @@ +#include +#include +#include +#include +#include +#include +#include "HykktHelloWorld.hpp" + +int main(int, char**) +{ + ReSolve::tests::HykktHelloWorld test; + + ReSolve::tests::TestingResults result; + result += test.addition(); + result += test.subtraction(); + result += test.multiplication(); + result += test.division(); + result += test.helloWorld(); + return result.summary(); +} \ No newline at end of file diff --git a/tests/unit/matrix/MatrixHelloWorld.hpp b/tests/unit/matrix/MatrixHelloWorld.hpp new file mode 100644 index 000000000..3ab7441d5 --- /dev/null +++ b/tests/unit/matrix/MatrixHelloWorld.hpp @@ -0,0 +1,74 @@ +#pragma once +#include +#include +#include +#include +#include +#include +#include + +namespace ReSolve { namespace tests { + +class MatrixHelloWorld : TestBase +{ +public: + MatrixHelloWorld(){} + virtual ~MatrixHelloWorld(){} + + + TestOutcome addition() + { + real_type a = 1.0; + real_type b = 2.0; + real_type c = a + b; + if (isEqual(c, 3.0)) + return PASS; + else + return FAIL; + } + + TestOutcome subtraction() + { + real_type a = 1.0; + real_type b = 2.0; + real_type c = a - b; + if (isEqual(c, -1.0)) + return PASS; + else + return FAIL; + } + + TestOutcome multiplication() + { + real_type a = 1.0; + real_type b = 2.0; + real_type c = a * b; + if (isEqual(c, 2.0)) + return PASS; + else + return FAIL; + } + + TestOutcome division() + { + real_type a = 1.0; + real_type b = 2.0; + real_type c = a / b; + if (isEqual(c, 0.5)) + return PASS; + else + return FAIL; + } + + TestOutcome helloWorld() + { + std::cout << "Hello World!" << std::endl; + return PASS; + } + + + + +}; // class MatrixHelloWorld + +}} // namespace ReSolve::tests From 8d9631bff15ea2c83ae8ea650c6dd571d26a2fdd Mon Sep 17 00:00:00 2001 From: shakedregev Date: Tue, 17 Dec 2024 23:05:15 +0000 Subject: [PATCH 06/30] hykkt hello world working, working on linking for permutation --- resolve/CMakeLists.txt | 4 ++ resolve/hykkt/cpu/CMakeLists.txt | 2 +- resolve/hykkt/cpu/PermClass.cpp | 24 ++----- tests/unit/hykkt/CMakeLists.txt | 6 +- tests/unit/hykkt/HykktPerm.hpp | 115 ++++++++++++++++++++++++++++++ tests/unit/hykkt/runHykktPerm.cpp | 16 +++++ 6 files changed, 148 insertions(+), 19 deletions(-) create mode 100644 tests/unit/hykkt/HykktPerm.hpp create mode 100644 tests/unit/hykkt/runHykktPerm.cpp diff --git a/resolve/CMakeLists.txt b/resolve/CMakeLists.txt index 317931f86..ea3b1fd2f 100644 --- a/resolve/CMakeLists.txt +++ b/resolve/CMakeLists.txt @@ -29,6 +29,7 @@ set(ReSolve_LUSOL_SRC LinSolverDirectLUSOL.cpp ) + # C++ code that links to CUDA SDK libraries set(ReSolve_CUDASDK_SRC LinSolverDirectCuSolverGLU.cpp @@ -114,6 +115,9 @@ if(RESOLVE_USE_LUSOL) list(APPEND ReSolve_Targets_List lusol_lib) endif() +add_subdirectory(hykkt/cpu) + + # If no GPU support is enabled, link to dummy device backend if(NOT RESOLVE_USE_GPU) add_subdirectory(cpu) diff --git a/resolve/hykkt/cpu/CMakeLists.txt b/resolve/hykkt/cpu/CMakeLists.txt index 2943dddc7..96ce22a8f 100644 --- a/resolve/hykkt/cpu/CMakeLists.txt +++ b/resolve/hykkt/cpu/CMakeLists.txt @@ -17,7 +17,7 @@ set (HyKKT_HEADER_INSTALL ) add_library(hyktt SHARED ${HyKKT_SRC}) -target_link_libraries(hyktt PRIVATE hykkt_logger) +target_link_libraries(hyktt PUBLIC ReSolve) # install include headers install(FILES ${HyKKT_HEADER_INSTALL} DESTINATION include/hykkt) diff --git a/resolve/hykkt/cpu/PermClass.cpp b/resolve/hykkt/cpu/PermClass.cpp index 81ab46e6d..8db2e3d12 100644 --- a/resolve/hykkt/cpu/PermClass.cpp +++ b/resolve/hykkt/cpu/PermClass.cpp @@ -1,5 +1,5 @@ -#include "PermClass.hpp" -#include "cpuHykktPermutationKernels.hpp" +#include "resolve/hykkt/cpu/PermClass.hpp" +#include "resolve/hykkt/cpu/cpuHykktPermutationKernels.hpp" #include #include #include "amd.h" @@ -94,34 +94,24 @@ PermClass::PermClass(int n_h, int nnz_h, int nnz_j) switch(permutation) { case perm_v: - fun_map_idx(n_h_, d_perm_, old_val, new_val); + cpu_map_idx(n_h_, perm_, old_val, new_val); break; case rev_perm_v: - fun_map_idx(n_h_, d_rev_perm_, old_val, new_val); + cpu_map_idx(n_h_, rev_perm_, old_val, new_val); break; case perm_h_v: - fun_map_idx(nnz_h_, d_perm_map_h_, old_val, new_val); + cpu_map_idx(nnz_h_, perm_map_h_, old_val, new_val); break; case perm_j_v: - fun_map_idx(nnz_j_, d_perm_map_j_, old_val, new_val); + cpu_map_idx(nnz_j_, perm_map_j_, old_val, new_val); break; case perm_jt_v: - fun_map_idx(nnz_j_, d_perm_map_jt_, old_val, new_val); + cpu_map_idx(nnz_j_, perm_map_jt_, 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::display_perm() const - { - displayDeviceVector(d_perm_, - n_h_, - 0, - 10, - "PERMUTATION"); - - } void PermClass::allocate_workspace() { diff --git a/tests/unit/hykkt/CMakeLists.txt b/tests/unit/hykkt/CMakeLists.txt index bafaea726..15fdcc895 100644 --- a/tests/unit/hykkt/CMakeLists.txt +++ b/tests/unit/hykkt/CMakeLists.txt @@ -6,8 +6,12 @@ ]] -# Build matrix Hello World tests add_executable(runHykktHelloWorldTests.exe runHykktHelloWorld.cpp) target_link_libraries(runHykktHelloWorldTests.exe PRIVATE ReSolve) add_test(NAME hykkt_hello_world_test COMMAND $) + +add_executable(runHykktPermTests.exe runHykktPerm.cpp) +target_link_libraries(runHykktPermTests.exe PRIVATE ReSolve) + +add_test(NAME hykkt_perm_test COMMAND $) diff --git a/tests/unit/hykkt/HykktPerm.hpp b/tests/unit/hykkt/HykktPerm.hpp new file mode 100644 index 000000000..0c54cf57b --- /dev/null +++ b/tests/unit/hykkt/HykktPerm.hpp @@ -0,0 +1,115 @@ +#pragma once +#include +#include +#include +#include +#include +#include +#include +#include +#include +namespace ReSolve { namespace tests { + +class HykktPerm : public TestBase +{ +public: + HykktPerm() {} + virtual ~HykktPerm() {} + + TestOutcome permutation() + { + int n = 4; + int m = 4; + int nnz = 9; + int a_i[5] = {0, 2, 5, 7, 9}; + int a_j[9] = {0, 2, 0, 1, 2, 1, 2, 1, 3}; + int a_prc_i[5] = {0, 2, 4, 6, 9}; + int a_prc_j[9] = {0, 3, 0, 1, 2, 3, 0, 1, 3}; + int a_pr_i[5] = {0, 2, 4, 6, 9}; + int a_pr_j[9] = {1, 2, 0, 2, 1, 3, 0, 1, 2}; + int a_pc_i[5] = {0, 2, 5, 7, 9}; + int a_pc_j[9] = {0, 1, 0, 1, 3, 0, 3, 2, 3}; + int b_i[5] = {0}; // Initialize row pointer + int b_j[9] = {0}; // Initialize column indices + int perm[4] = {2, 0, 3, 1}; + + bool flagrc = false; + bool flagr = false; + bool flagc = false; + + PermClass pc(n, nnz, nnz); + pc.add_h_info(a_i, a_j); + pc.add_j_info(a_i, a_j, n, m); + pc.add_jt_info(a_i, a_j); + pc.add_perm(perm); + pc.invert_perm(); + + // Test RC permutation + pc.vec_map_rc(b_i, b_j); + printf("Comparing RC permutation\n"); + for (int i = 0; i < n + 1; i++) // Loop over row pointers (n+1) + { + if (a_prc_i[i] != b_i[i]) + { + printf("Mismatch in row pointer %d\n", i); + flagrc = true; + } + } + for (int j = 0; j < nnz; j++) // Compare column indices + { + if (a_prc_j[j] != b_j[j]) + { + printf("Mismatch in column index %d\n", j); + flagrc = true; + } + } + printf(flagrc ? "RC permutation failed\n" : "RC permutation passed\n"); + + // Test R permutation + pc.vec_map_r(b_i, b_j); + printf("Comparing R permutation\n"); + for (int i = 0; i < n + 1; i++) + { + if (a_pr_i[i] != b_i[i]) + { + printf("Mismatch in row pointer %d\n", i); + flagr = true; + } + } + for (int j = 0; j < nnz; j++) + { + if (a_pr_j[j] != b_j[j]) + { + printf("Mismatch in column index %d\n", j); + flagr = true; + } + } + printf(flagr ? "R permutation failed\n" : "R permutation passed\n"); + + // Test C permutation + pc.vec_map_c(b_j); + printf("Comparing C permutation\n"); + for (int i = 0; i < n + 1; i++) + { + if (a_pc_i[i] != a_i[i]) // Row pointers should match + { + printf("Mismatch in row pointer %d\n", i); + flagc = true; + } + } + for (int j = 0; j < nnz; j++) + { + if (a_pc_j[j] != b_j[j]) + { + printf("Mismatch in column index %d\n", j); + flagc = true; + } + } + printf(flagc ? "C permutation failed\n" : "C permutation passed\n"); + + // Final Test Outcome + return (!flagrc && !flagr && !flagc) ? PASS : FAIL; + } +}; // class HykktPerm + +}} // namespace ReSolve::tests \ No newline at end of file diff --git a/tests/unit/hykkt/runHykktPerm.cpp b/tests/unit/hykkt/runHykktPerm.cpp new file mode 100644 index 000000000..0c9c14173 --- /dev/null +++ b/tests/unit/hykkt/runHykktPerm.cpp @@ -0,0 +1,16 @@ +#include +#include +#include +#include +#include +#include +#include "tests/unit/hykkt/HykktPerm.hpp" + +int main(int, char**) +{ + ReSolve::tests::HykktPerm test; + + ReSolve::tests::TestingResults result; + result += test.permutation(); + return result.summary(); +} \ No newline at end of file From d06e86fcfc9c3bbd56db3873cd0cc7e379da98dc Mon Sep 17 00:00:00 2001 From: shakedregev Date: Fri, 3 Jan 2025 23:42:32 +0000 Subject: [PATCH 07/30] resolved conflict --- resolve/CMakeLists.txt | 4 +--- tests/unit/CMakeLists.txt | 4 ++++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/resolve/CMakeLists.txt b/resolve/CMakeLists.txt index ea3b1fd2f..01856de07 100644 --- a/resolve/CMakeLists.txt +++ b/resolve/CMakeLists.txt @@ -82,7 +82,7 @@ add_subdirectory(workspace) add_subdirectory(vector) add_subdirectory(matrix) add_subdirectory(random) -add_subdirectory(hykkt/cpu) +#add_subdirectory(hykkt/cpu) # Build shared library ReSolve add_library(resolve_tpl INTERFACE) @@ -115,8 +115,6 @@ if(RESOLVE_USE_LUSOL) list(APPEND ReSolve_Targets_List lusol_lib) endif() -add_subdirectory(hykkt/cpu) - # If no GPU support is enabled, link to dummy device backend if(NOT RESOLVE_USE_GPU) diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt index d631ccc64..58377e5ef 100644 --- a/tests/unit/CMakeLists.txt +++ b/tests/unit/CMakeLists.txt @@ -10,5 +10,9 @@ add_subdirectory(matrix) add_subdirectory(vector) add_subdirectory(utilities) add_subdirectory(memory) +<<<<<<< HEAD add_subdirectory(params) add_subdirectory(hykkt) +======= +#add_subdirectory(hykkt) +>>>>>>> 857f284 (Disable building HyKKT.) From dc937d50e8274d2c1cebb5a6ec9e037bdf3a398d Mon Sep 17 00:00:00 2001 From: Slaven Peles Date: Thu, 26 Dec 2024 14:49:32 -0500 Subject: [PATCH 08/30] Ignore files in resolve/hybrid dir. --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index bb4134bee..fb76b5c08 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,5 @@ spack-configure-* docs/dox.warnings docs/@dox_output_dir@axom.tag docs/html +resolve/hybrid + From a0e2d5a95cad23008911a0b92cef9e15dae117e7 Mon Sep 17 00:00:00 2001 From: Slaven Peles Date: Thu, 26 Dec 2024 14:57:05 -0500 Subject: [PATCH 09/30] Remove cpu subdirectory in hykkt dir. --- resolve/hykkt/{cpu => }/CMakeLists.txt | 0 resolve/hykkt/{cpu => }/PermClass.cpp | 0 resolve/hykkt/{cpu => }/PermClass.hpp | 0 resolve/hykkt/{cpu => }/cpuHykktPermutationKernels.cpp | 0 resolve/hykkt/{cpu => }/cpuHykktPermutationKernels.hpp | 0 5 files changed, 0 insertions(+), 0 deletions(-) rename resolve/hykkt/{cpu => }/CMakeLists.txt (100%) rename resolve/hykkt/{cpu => }/PermClass.cpp (100%) rename resolve/hykkt/{cpu => }/PermClass.hpp (100%) rename resolve/hykkt/{cpu => }/cpuHykktPermutationKernels.cpp (100%) rename resolve/hykkt/{cpu => }/cpuHykktPermutationKernels.hpp (100%) diff --git a/resolve/hykkt/cpu/CMakeLists.txt b/resolve/hykkt/CMakeLists.txt similarity index 100% rename from resolve/hykkt/cpu/CMakeLists.txt rename to resolve/hykkt/CMakeLists.txt diff --git a/resolve/hykkt/cpu/PermClass.cpp b/resolve/hykkt/PermClass.cpp similarity index 100% rename from resolve/hykkt/cpu/PermClass.cpp rename to resolve/hykkt/PermClass.cpp diff --git a/resolve/hykkt/cpu/PermClass.hpp b/resolve/hykkt/PermClass.hpp similarity index 100% rename from resolve/hykkt/cpu/PermClass.hpp rename to resolve/hykkt/PermClass.hpp diff --git a/resolve/hykkt/cpu/cpuHykktPermutationKernels.cpp b/resolve/hykkt/cpuHykktPermutationKernels.cpp similarity index 100% rename from resolve/hykkt/cpu/cpuHykktPermutationKernels.cpp rename to resolve/hykkt/cpuHykktPermutationKernels.cpp diff --git a/resolve/hykkt/cpu/cpuHykktPermutationKernels.hpp b/resolve/hykkt/cpuHykktPermutationKernels.hpp similarity index 100% rename from resolve/hykkt/cpu/cpuHykktPermutationKernels.hpp rename to resolve/hykkt/cpuHykktPermutationKernels.hpp From 9e9149504308a7d87a409d67075e215eaf19cc44 Mon Sep 17 00:00:00 2001 From: Slaven Peles Date: Thu, 26 Dec 2024 15:18:42 -0500 Subject: [PATCH 10/30] hykkt lib builds. --- resolve/CMakeLists.txt | 7 ++++++- resolve/hykkt/CMakeLists.txt | 6 ++++-- resolve/hykkt/PermClass.cpp | 10 ++++++---- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/resolve/CMakeLists.txt b/resolve/CMakeLists.txt index 01856de07..7906d4d9e 100644 --- a/resolve/CMakeLists.txt +++ b/resolve/CMakeLists.txt @@ -82,7 +82,6 @@ add_subdirectory(workspace) add_subdirectory(vector) add_subdirectory(matrix) add_subdirectory(random) -#add_subdirectory(hykkt/cpu) # Build shared library ReSolve add_library(resolve_tpl INTERFACE) @@ -140,6 +139,12 @@ if(RESOLVE_USE_HIP) list(APPEND ReSolve_HEADER_INSTALL ${ReSolve_ROCM_HEADER_INSTALL}) endif() +# Add HyKKT solver +add_subdirectory(hykkt) +#list(APPEND ReSolve_Targets_List resolve_hykkt) + + + # Set installable targets install(TARGETS ${ReSolve_Targets_List} EXPORT ReSolveTargets) diff --git a/resolve/hykkt/CMakeLists.txt b/resolve/hykkt/CMakeLists.txt index 96ce22a8f..a639f5169 100644 --- a/resolve/hykkt/CMakeLists.txt +++ b/resolve/hykkt/CMakeLists.txt @@ -16,8 +16,10 @@ set (HyKKT_HEADER_INSTALL cpuHykktPermutationKernels.hpp ) -add_library(hyktt SHARED ${HyKKT_SRC}) -target_link_libraries(hyktt PUBLIC ReSolve) +add_library(resolve_hyktt SHARED ${HyKKT_SRC}) +target_link_libraries(resolve_hyktt PRIVATE resolve_tpl) +#target_include_directories(resolve_hykkt PRIVATE ${CMAKE_BINARY_DIR}) + # install include headers install(FILES ${HyKKT_HEADER_INSTALL} DESTINATION include/hykkt) diff --git a/resolve/hykkt/PermClass.cpp b/resolve/hykkt/PermClass.cpp index 8db2e3d12..703544ecf 100644 --- a/resolve/hykkt/PermClass.cpp +++ b/resolve/hykkt/PermClass.cpp @@ -1,7 +1,9 @@ -#include "resolve/hykkt/cpu/PermClass.hpp" -#include "resolve/hykkt/cpu/cpuHykktPermutationKernels.hpp" -#include -#include +#include "PermClass.hpp" +#include "cpuHykktPermutationKernels.hpp" +// #include +// #include +// #include +// #include #include "amd.h" // Creates a class for the permutation of $H_\gamma$ in (6) From 93f719838b9ec802802f2590e31a2df972342b75 Mon Sep 17 00:00:00 2001 From: Slaven Peles Date: Thu, 26 Dec 2024 15:26:57 -0500 Subject: [PATCH 11/30] Fix issues in CMake related to HyKKT build. --- CMakeLists.txt | 2 +- resolve/cpu/CMakeLists.txt | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index bae37bb01..9d4da16fa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,7 +24,7 @@ endif() option(RESOLVE_TEST_WITH_BSUB "Use `jsrun` instead of `mpirun` commands when running tests" OFF) option(RESOLVE_USE_KLU "Use KLU, AMD and COLAMD libraries from SuiteSparse" OFF) option(RESOLVE_USE_LUSOL "Build the LUSOL library" OFF) -option(RESOLVE_USE_CUDA "Use CUDA language and SDK" ON) +option(RESOLVE_USE_CUDA "Use CUDA language and SDK" OFF) option(RESOLVE_USE_HIP "Use HIP language and ROCm library" OFF) option(RESOLVE_USE_PROFILING "Set profiling tracers in the code" OFF) diff --git a/resolve/cpu/CMakeLists.txt b/resolve/cpu/CMakeLists.txt index 2ea17261e..cf323f8ff 100644 --- a/resolve/cpu/CMakeLists.txt +++ b/resolve/cpu/CMakeLists.txt @@ -8,12 +8,10 @@ set(ReSolve_CPU_SRC MemoryUtils.cpp - cpuHykktPermutationKernels.cpp ) set(ReSolve_CPU_HEADER_INSTALL CpuMemory.hpp - cpuHykktPermutationKernels.hpp ) # First create dummy backend From 59c1c6d43ab04c4891baaa535e5a862136fda4b1 Mon Sep 17 00:00:00 2001 From: shakedregev Date: Fri, 3 Jan 2025 23:45:13 +0000 Subject: [PATCH 12/30] resolved conflict --- resolve/hykkt/CMakeLists.txt | 50 +++++++++++++++++++------ resolve/hykkt/PermClass.cpp | 7 ++-- tests/unit/CMakeLists.txt | 4 ++ tests/unit/hykkt/CMakeLists.txt | 24 ++++++++---- tests/unit/hykkt/HykktHelloWorld.hpp | 2 +- tests/unit/hykkt/runHykktHelloWorld.cpp | 6 +-- 6 files changed, 67 insertions(+), 26 deletions(-) diff --git a/resolve/hykkt/CMakeLists.txt b/resolve/hykkt/CMakeLists.txt index a639f5169..fe4c8f61e 100644 --- a/resolve/hykkt/CMakeLists.txt +++ b/resolve/hykkt/CMakeLists.txt @@ -1,26 +1,54 @@ #[[ -@brief Build HyKKT +@brief Build ReSolve matrix module -@author Shaked Regev +@author Slaven Peles ]] -set (HyKKT_SRC +# C++ code +set(HyKKT_SRC PermClass.cpp cpuHykktPermutationKernels.cpp ) -set (HyKKT_HEADER_INSTALL - PermClass.hpp - cpuHykktPermutationKernels.hpp +# C++ code that depends on CUDA SDK libraries +set(HyKKT_CUDASDK_SRC ) -add_library(resolve_hyktt SHARED ${HyKKT_SRC}) -target_link_libraries(resolve_hyktt PRIVATE resolve_tpl) -#target_include_directories(resolve_hykkt PRIVATE ${CMAKE_BINARY_DIR}) +# and on HIP +set(HyKKT_ROCM_SRC +) -# install include headers -install(FILES ${HyKKT_HEADER_INSTALL} DESTINATION include/hykkt) +# Header files to be installed +set(HyKKT_HEADER_INSTALL + PermClass.hpp + cpuHykktPermutationKernels.hpp +) +# Build shared library ReSolve::matrix +add_library(resolve_hykkt SHARED ${HyKKT_SRC}) +target_link_libraries(resolve_hykkt PRIVATE resolve_logger resolve_vector resolve_tpl) + +# Link to CUDA ReSolve backend if CUDA is support enabled +if (RESOLVE_USE_CUDA) +# target_sources(resolve_hykkt PRIVATE ${HyKKT_CUDASDK_SRC}) +# target_link_libraries(resolve_hykkt PUBLIC resolve_backend_cuda) +endif() + +if (RESOLVE_USE_HIP) +# target_sources(resolve_hykkt PRIVATE ${HyKKT_ROCM_SRC}) +# target_link_libraries(resolve_hykkt PUBLIC resolve_backend_hip) +endif() + +# Link to dummy device backend if GPU support is not enabled +if (NOT RESOLVE_USE_GPU) + target_link_libraries(resolve_hykkt PUBLIC resolve_backend_cpu) +endif() + +target_include_directories(resolve_hykkt INTERFACE + $ + $ +) +install(FILES ${HyKKT_HEADER_INSTALL} DESTINATION include/resolve/hykkt) diff --git a/resolve/hykkt/PermClass.cpp b/resolve/hykkt/PermClass.cpp index 703544ecf..6d6996751 100644 --- a/resolve/hykkt/PermClass.cpp +++ b/resolve/hykkt/PermClass.cpp @@ -1,9 +1,8 @@ -#include "PermClass.hpp" -#include "cpuHykktPermutationKernels.hpp" -// #include -// #include +#include +#include // #include // #include + #include "amd.h" // Creates a class for the permutation of $H_\gamma$ in (6) diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt index 58377e5ef..121a0b196 100644 --- a/tests/unit/CMakeLists.txt +++ b/tests/unit/CMakeLists.txt @@ -11,8 +11,12 @@ add_subdirectory(vector) add_subdirectory(utilities) add_subdirectory(memory) <<<<<<< HEAD +<<<<<<< HEAD add_subdirectory(params) add_subdirectory(hykkt) ======= #add_subdirectory(hykkt) >>>>>>> 857f284 (Disable building HyKKT.) +======= +add_subdirectory(hykkt) +>>>>>>> 48c55ce (Rework hykkt cmake altogether, hello world test works.) diff --git a/tests/unit/hykkt/CMakeLists.txt b/tests/unit/hykkt/CMakeLists.txt index 15fdcc895..05c6095af 100644 --- a/tests/unit/hykkt/CMakeLists.txt +++ b/tests/unit/hykkt/CMakeLists.txt @@ -1,17 +1,27 @@ #[[ -@brief Build ReSolve HyKKT unit tests +@brief Build ReSolve matrix unit tests -@author Shaked Regev +@author Slaven Peles ]] +# Build matrix I/O tests add_executable(runHykktHelloWorldTests.exe runHykktHelloWorld.cpp) -target_link_libraries(runHykktHelloWorldTests.exe PRIVATE ReSolve) +target_link_libraries(runHykktHelloWorldTests.exe PRIVATE resolve_hykkt) -add_test(NAME hykkt_hello_world_test COMMAND $) +# Install tests +set(installable_tests runHykktHelloWorldTests.exe) -add_executable(runHykktPermTests.exe runHykktPerm.cpp) -target_link_libraries(runHykktPermTests.exe PRIVATE ReSolve) +install(TARGETS ${installable_tests} + RUNTIME DESTINATION bin/resolve/tests/unit) -add_test(NAME hykkt_perm_test COMMAND $) +add_test(NAME hello_world_test COMMAND $) + + + + +# add_executable(runHykktPermTests.exe runHykktPerm.cpp) +# target_link_libraries(runHykktPermTests.exe PRIVATE ReSolve) + +# add_test(NAME hykkt_perm_test COMMAND $) diff --git a/tests/unit/hykkt/HykktHelloWorld.hpp b/tests/unit/hykkt/HykktHelloWorld.hpp index dcfe9e214..e2bb5f261 100644 --- a/tests/unit/hykkt/HykktHelloWorld.hpp +++ b/tests/unit/hykkt/HykktHelloWorld.hpp @@ -5,7 +5,7 @@ #include #include #include -#include +// #include namespace ReSolve { namespace tests { diff --git a/tests/unit/hykkt/runHykktHelloWorld.cpp b/tests/unit/hykkt/runHykktHelloWorld.cpp index c0de933bf..f132c7c5d 100644 --- a/tests/unit/hykkt/runHykktHelloWorld.cpp +++ b/tests/unit/hykkt/runHykktHelloWorld.cpp @@ -1,9 +1,9 @@ #include #include #include -#include -#include -#include +// #include +// #include +// #include #include "HykktHelloWorld.hpp" int main(int, char**) From f6aa92c978d4d9d038033bc4fbcd72ef6aef1dc9 Mon Sep 17 00:00:00 2001 From: Slaven Peles Date: Thu, 26 Dec 2024 15:55:34 -0500 Subject: [PATCH 13/30] HyKKT permutation test works. --- .gitignore | 1 - tests/unit/hykkt/CMakeLists.txt | 19 ++++++++----------- tests/unit/hykkt/HykktPerm.hpp | 5 ++--- tests/unit/hykkt/runHykktPerm.cpp | 3 --- 4 files changed, 10 insertions(+), 18 deletions(-) diff --git a/.gitignore b/.gitignore index fb76b5c08..21323e946 100644 --- a/.gitignore +++ b/.gitignore @@ -15,5 +15,4 @@ spack-configure-* docs/dox.warnings docs/@dox_output_dir@axom.tag docs/html -resolve/hybrid diff --git a/tests/unit/hykkt/CMakeLists.txt b/tests/unit/hykkt/CMakeLists.txt index 05c6095af..5808156d7 100644 --- a/tests/unit/hykkt/CMakeLists.txt +++ b/tests/unit/hykkt/CMakeLists.txt @@ -1,27 +1,24 @@ #[[ -@brief Build ReSolve matrix unit tests +@brief Build HyKKT module unit tests @author Slaven Peles ]] -# Build matrix I/O tests +# Build HyKKT tests add_executable(runHykktHelloWorldTests.exe runHykktHelloWorld.cpp) target_link_libraries(runHykktHelloWorldTests.exe PRIVATE resolve_hykkt) +add_executable(runHykktPermTests.exe runHykktPerm.cpp) +target_link_libraries(runHykktPermTests.exe PRIVATE resolve_hykkt) + # Install tests -set(installable_tests runHykktHelloWorldTests.exe) +set(installable_tests runHykktHelloWorldTests.exe runHykktPermTests.exe) install(TARGETS ${installable_tests} RUNTIME DESTINATION bin/resolve/tests/unit) -add_test(NAME hello_world_test COMMAND $) - - - - -# add_executable(runHykktPermTests.exe runHykktPerm.cpp) -# target_link_libraries(runHykktPermTests.exe PRIVATE ReSolve) +add_test(NAME hello_world_test COMMAND $) +add_test(NAME hykkt_perm_test COMMAND $) -# add_test(NAME hykkt_perm_test COMMAND $) diff --git a/tests/unit/hykkt/HykktPerm.hpp b/tests/unit/hykkt/HykktPerm.hpp index 0c54cf57b..27199014c 100644 --- a/tests/unit/hykkt/HykktPerm.hpp +++ b/tests/unit/hykkt/HykktPerm.hpp @@ -5,9 +5,8 @@ #include #include #include -#include -#include -#include +#include +#include namespace ReSolve { namespace tests { class HykktPerm : public TestBase diff --git a/tests/unit/hykkt/runHykktPerm.cpp b/tests/unit/hykkt/runHykktPerm.cpp index 0c9c14173..b2c3a3270 100644 --- a/tests/unit/hykkt/runHykktPerm.cpp +++ b/tests/unit/hykkt/runHykktPerm.cpp @@ -1,9 +1,6 @@ #include #include #include -#include -#include -#include #include "tests/unit/hykkt/HykktPerm.hpp" int main(int, char**) From f335cfbc0ff134ae57ee87e93976c849be03fa66 Mon Sep 17 00:00:00 2001 From: shakedregev Date: Sat, 28 Dec 2024 00:40:37 +0000 Subject: [PATCH 14/30] modifications for CUDA --- cmake/FindSuiteSparse.cmake | 10 +++++----- resolve/hykkt/CMakeLists.txt | 3 ++- resolve/hykkt/PermClass.cpp | 2 +- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/cmake/FindSuiteSparse.cmake b/cmake/FindSuiteSparse.cmake index d7fce9efd..ec7f7394a 100644 --- a/cmake/FindSuiteSparse.cmake +++ b/cmake/FindSuiteSparse.cmake @@ -22,10 +22,10 @@ find_library(SUITESPARSE_LIBRARY ${SUITESPARSE_DIR} $ENV{SUITESPARSE_DIR} ${SUITESPARSE_ROOT_DIR} ENV LD_LIBRARY_PATH ENV DYLD_LIBRARY_PATH PATH_SUFFIXES - lib64 lib) + lib/x86_64-linux-gnu lib64 lib) if(SUITESPARSE_LIBRARY) - set(SUITESPARSE_LIBRARY CACHE FILEPATH "Path to Suitesparse library") + set(SUITESPARSE_LIBRARY CACHE FILEPATH "File path to Suitesparse library") get_filename_component(SUITESPARSE_LIBRARY_DIR ${SUITESPARSE_LIBRARY} DIRECTORY CACHE "Suitesparse library directory") message(STATUS "Found Suitesparse libraries in: " ${SUITESPARSE_LIBRARY_DIR}) mark_as_advanced(SUITESPARSE_LIBRARY SUITESPARSE_LIBRARY_DIR) @@ -44,7 +44,7 @@ find_path(SUITESPARSE_INCLUDE_DIR PATHS ${SUITESPARSE_DIR} $ENV{SUITESPARSE_DIR} ${SUITESPARSE_ROOT_DIR} ${SUITESPARSE_LIBRARY_DIR}/.. PATH_SUFFIXES - include) + include include/suitesparse) if(SUITESPARSE_LIBRARY) message(STATUS "Found Suitesparse include: ${SUITESPARSE_INCLUDE_DIR}") @@ -75,6 +75,6 @@ else() message(STATUS "Suitesparse library not found! Please provide correct filepath.") endif() if(SUITESPARSE_ROOT_DIR AND NOT SUITESPARSE_INCLUDE_DIR) - message(STATUS "Suitesparse include dir not found! Please provide correct filepath.") + message(STATUS "Suitesparse include dir not found! Please provide correct path.") endif() -endif() +endif() \ No newline at end of file diff --git a/resolve/hykkt/CMakeLists.txt b/resolve/hykkt/CMakeLists.txt index fe4c8f61e..a8853c55d 100644 --- a/resolve/hykkt/CMakeLists.txt +++ b/resolve/hykkt/CMakeLists.txt @@ -28,7 +28,8 @@ set(HyKKT_HEADER_INSTALL # Build shared library ReSolve::matrix add_library(resolve_hykkt SHARED ${HyKKT_SRC}) -target_link_libraries(resolve_hykkt PRIVATE resolve_logger resolve_vector resolve_tpl) +target_link_libraries(resolve_hykkt PUBLIC resolve_logger resolve_vector ${suitesparse_amd}) +target_include_directories(resolve_hykkt PUBLIC ${SUITESPARSE_INCLUDE_DIR}) # Link to CUDA ReSolve backend if CUDA is support enabled if (RESOLVE_USE_CUDA) diff --git a/resolve/hykkt/PermClass.cpp b/resolve/hykkt/PermClass.cpp index 6d6996751..40305f078 100644 --- a/resolve/hykkt/PermClass.cpp +++ b/resolve/hykkt/PermClass.cpp @@ -2,7 +2,7 @@ #include // #include // #include - +#include #include "amd.h" // Creates a class for the permutation of $H_\gamma$ in (6) From b044767efe86596494de119655ce89a76a3139f2 Mon Sep 17 00:00:00 2001 From: shakedregev Date: Fri, 3 Jan 2025 21:48:10 +0000 Subject: [PATCH 15/30] removed extraneous tests --- tests/unit/hykkt/CMakeLists.txt | 5 +- tests/unit/hykkt/HykktHelloWorld.hpp | 74 ----------------------- tests/unit/hykkt/runHykktHelloWorld.cpp | 20 ------ tests/unit/matrix/CMakeLists.txt | 5 -- tests/unit/matrix/MatrixHelloWorld.hpp | 74 ----------------------- tests/unit/matrix/runMatrixHelloWorld.cpp | 20 ------ 6 files changed, 1 insertion(+), 197 deletions(-) delete mode 100644 tests/unit/hykkt/HykktHelloWorld.hpp delete mode 100644 tests/unit/hykkt/runHykktHelloWorld.cpp delete mode 100644 tests/unit/matrix/MatrixHelloWorld.hpp delete mode 100644 tests/unit/matrix/runMatrixHelloWorld.cpp diff --git a/tests/unit/hykkt/CMakeLists.txt b/tests/unit/hykkt/CMakeLists.txt index 5808156d7..90577c905 100644 --- a/tests/unit/hykkt/CMakeLists.txt +++ b/tests/unit/hykkt/CMakeLists.txt @@ -7,18 +7,15 @@ ]] # Build HyKKT tests -add_executable(runHykktHelloWorldTests.exe runHykktHelloWorld.cpp) -target_link_libraries(runHykktHelloWorldTests.exe PRIVATE resolve_hykkt) add_executable(runHykktPermTests.exe runHykktPerm.cpp) target_link_libraries(runHykktPermTests.exe PRIVATE resolve_hykkt) # Install tests -set(installable_tests runHykktHelloWorldTests.exe runHykktPermTests.exe) +set(installable_tests runHykktPermTests.exe) install(TARGETS ${installable_tests} RUNTIME DESTINATION bin/resolve/tests/unit) -add_test(NAME hello_world_test COMMAND $) add_test(NAME hykkt_perm_test COMMAND $) diff --git a/tests/unit/hykkt/HykktHelloWorld.hpp b/tests/unit/hykkt/HykktHelloWorld.hpp deleted file mode 100644 index e2bb5f261..000000000 --- a/tests/unit/hykkt/HykktHelloWorld.hpp +++ /dev/null @@ -1,74 +0,0 @@ -#pragma once -#include -#include -#include -#include -#include -#include -// #include - -namespace ReSolve { namespace tests { - -class HykktHelloWorld : TestBase -{ -public: - HykktHelloWorld(){} - virtual ~HykktHelloWorld(){} - - - TestOutcome addition() - { - real_type a = 1.0; - real_type b = 2.0; - real_type c = a + b; - if (isEqual(c, 3.0)) - return PASS; - else - return FAIL; - } - - TestOutcome subtraction() - { - real_type a = 1.0; - real_type b = 2.0; - real_type c = a - b; - if (isEqual(c, -1.0)) - return PASS; - else - return FAIL; - } - - TestOutcome multiplication() - { - real_type a = 1.0; - real_type b = 2.0; - real_type c = a * b; - if (isEqual(c, 2.0)) - return PASS; - else - return FAIL; - } - - TestOutcome division() - { - real_type a = 1.0; - real_type b = 2.0; - real_type c = a / b; - if (isEqual(c, 0.5)) - return PASS; - else - return FAIL; - } - - TestOutcome helloWorld() - { - std::cout << "Hello World!" << std::endl; - return PASS; - } - - - - -}; // class HykktHelloWorld - -}} // namespace ReSolve::tests diff --git a/tests/unit/hykkt/runHykktHelloWorld.cpp b/tests/unit/hykkt/runHykktHelloWorld.cpp deleted file mode 100644 index f132c7c5d..000000000 --- a/tests/unit/hykkt/runHykktHelloWorld.cpp +++ /dev/null @@ -1,20 +0,0 @@ -#include -#include -#include -// #include -// #include -// #include -#include "HykktHelloWorld.hpp" - -int main(int, char**) -{ - ReSolve::tests::HykktHelloWorld test; - - ReSolve::tests::TestingResults result; - result += test.addition(); - result += test.subtraction(); - result += test.multiplication(); - result += test.division(); - result += test.helloWorld(); - return result.summary(); -} \ No newline at end of file diff --git a/tests/unit/matrix/CMakeLists.txt b/tests/unit/matrix/CMakeLists.txt index c0163c22f..85d3a9af0 100644 --- a/tests/unit/matrix/CMakeLists.txt +++ b/tests/unit/matrix/CMakeLists.txt @@ -18,10 +18,6 @@ target_link_libraries(runMatrixHandlerTests.exe PRIVATE ReSolve resolve_matrix) add_executable(runMatrixFactorizationTests.exe runMatrixFactorizationTests.cpp) target_link_libraries(runMatrixFactorizationTests.exe PRIVATE ReSolve resolve_matrix) -# Build matrix Hello World tests -add_executable(runMatrixHelloWorldTests.exe runMatrixHelloWorld.cpp) -target_link_libraries(runMatrixHelloWorldTests.exe PRIVATE ReSolve resolve_matrix) - # Build LUSOL-related tests if(RESOLVE_USE_LUSOL) add_executable(runLUSOLTests.exe runLUSOLTests.cpp) @@ -39,7 +35,6 @@ install(TARGETS ${installable_tests} add_test(NAME matrix_test COMMAND $) add_test(NAME matrix_handler_test COMMAND $) add_test(NAME matrix_factorization_test COMMAND $) -add_test(NAME matrix_hello_world_test COMMAND $) if(RESOLVE_USE_LUSOL) add_test(NAME lusol_factorization_test COMMAND $) endif() diff --git a/tests/unit/matrix/MatrixHelloWorld.hpp b/tests/unit/matrix/MatrixHelloWorld.hpp deleted file mode 100644 index 3ab7441d5..000000000 --- a/tests/unit/matrix/MatrixHelloWorld.hpp +++ /dev/null @@ -1,74 +0,0 @@ -#pragma once -#include -#include -#include -#include -#include -#include -#include - -namespace ReSolve { namespace tests { - -class MatrixHelloWorld : TestBase -{ -public: - MatrixHelloWorld(){} - virtual ~MatrixHelloWorld(){} - - - TestOutcome addition() - { - real_type a = 1.0; - real_type b = 2.0; - real_type c = a + b; - if (isEqual(c, 3.0)) - return PASS; - else - return FAIL; - } - - TestOutcome subtraction() - { - real_type a = 1.0; - real_type b = 2.0; - real_type c = a - b; - if (isEqual(c, -1.0)) - return PASS; - else - return FAIL; - } - - TestOutcome multiplication() - { - real_type a = 1.0; - real_type b = 2.0; - real_type c = a * b; - if (isEqual(c, 2.0)) - return PASS; - else - return FAIL; - } - - TestOutcome division() - { - real_type a = 1.0; - real_type b = 2.0; - real_type c = a / b; - if (isEqual(c, 0.5)) - return PASS; - else - return FAIL; - } - - TestOutcome helloWorld() - { - std::cout << "Hello World!" << std::endl; - return PASS; - } - - - - -}; // class MatrixHelloWorld - -}} // namespace ReSolve::tests diff --git a/tests/unit/matrix/runMatrixHelloWorld.cpp b/tests/unit/matrix/runMatrixHelloWorld.cpp deleted file mode 100644 index 5ad927ee9..000000000 --- a/tests/unit/matrix/runMatrixHelloWorld.cpp +++ /dev/null @@ -1,20 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include "MatrixHelloWorld.hpp" - -int main(int, char**) -{ - ReSolve::tests::MatrixHelloWorld test; - - ReSolve::tests::TestingResults result; - result += test.addition(); - result += test.subtraction(); - result += test.multiplication(); - result += test.division(); - result += test.helloWorld(); - return result.summary(); -} \ No newline at end of file From 1fe399f52bf69bf1cf37fcbe58a304b06860c65e Mon Sep 17 00:00:00 2001 From: shakedregev Date: Fri, 3 Jan 2025 22:46:32 +0000 Subject: [PATCH 16/30] reformatted according to contributing guidelines --- resolve/hykkt/PermClass.cpp | 34 ++++++++++++++++------------------ resolve/hykkt/PermClass.hpp | 4 ++-- tests/unit/hykkt/HykktPerm.hpp | 16 ++++++++-------- 3 files changed, 26 insertions(+), 28 deletions(-) diff --git a/resolve/hykkt/PermClass.cpp b/resolve/hykkt/PermClass.cpp index 40305f078..67ac8623e 100644 --- a/resolve/hykkt/PermClass.cpp +++ b/resolve/hykkt/PermClass.cpp @@ -1,7 +1,5 @@ #include #include -// #include -// #include #include #include "amd.h" @@ -11,7 +9,7 @@ PermClass::PermClass(int n_h, int nnz_h, int nnz_j) nnz_h_(nnz_h), nnz_j_(nnz_j) { - allocate_workspace(); + allocateWorkspace(); } PermClass::~PermClass() @@ -25,13 +23,13 @@ PermClass::PermClass(int n_h, int nnz_h, int nnz_j) delete [] perm_map_jt_; } - void PermClass::add_h_info(int* h_i, int* h_j) + void PermClass::addHInfo(int* h_i, int* h_j) { h_i_ = h_i; h_j_ = h_j; } - void PermClass::add_j_info(int* j_i, int* j_j, int n_j, int m_j) + void PermClass::addJInfo(int* j_i, int* j_j, int n_j, int m_j) { j_i_ = j_i; j_j_ = j_j; @@ -39,20 +37,20 @@ PermClass::PermClass(int n_h, int nnz_h, int nnz_j) m_j_ = m_j; } - void PermClass::add_jt_info(int* jt_i, int* jt_j) + void PermClass::addJtInfo(int* jt_i, int* jt_j) { jt_i_ = jt_i; jt_j_ = jt_j; } - void PermClass::add_perm(int* custom_perm) + void PermClass::addPerm(int* custom_perm) { perm_is_default_ = false; perm_ = custom_perm; } -// Symamd permutation of $H_\gamma$ in (6) - void PermClass::symamd() +// symAmd permutation of $H_\gamma$ in (6) + void PermClass::symAmd() { double Control[AMD_CONTROL], Info[AMD_INFO]; @@ -68,27 +66,27 @@ PermClass::PermClass(int n_h, int nnz_h, int nnz_j) } } - void PermClass::invert_perm() + void PermClass::invertPerm() { reverse_perm(n_h_, perm_, rev_perm_); } - void PermClass::vec_map_rc(int* b_i, int* b_j) + void PermClass::vecMapRC(int* b_i, int* b_j) { - make_vec_map_rc(n_h_, h_i_, h_j_, perm_, rev_perm_, b_i, b_j, perm_map_h_); + make_vecMapRC(n_h_, h_i_, h_j_, perm_, rev_perm_, b_i, b_j, perm_map_h_); } - void PermClass::vec_map_c(int* b_j) + void PermClass::vecMapC(int* b_j) { - make_vec_map_c(n_j_, j_i_, j_j_, rev_perm_, b_j, perm_map_j_); + make_vecMapC(n_j_, j_i_, j_j_, rev_perm_, b_j, perm_map_j_); } - void PermClass::vec_map_r(int* b_i, int* b_j) + void PermClass::vecMapR(int* b_i, int* b_j) { - make_vec_map_r(m_j_, jt_i_, jt_j_, perm_, b_i, b_j, perm_map_jt_); + make_vecMapR(m_j_, jt_i_, jt_j_, perm_, b_i, b_j, perm_map_jt_); } - void PermClass::map_index(Permutation_Type permutation, + void PermClass::map_index(PermutationType permutation, double* old_val, double* new_val) { @@ -114,7 +112,7 @@ PermClass::PermClass(int n_h, int nnz_h, int nnz_j) } } - void PermClass::allocate_workspace() + void PermClass::allocateWorkspace() { perm_ = new int[n_h_]; rev_perm_ = new int[n_h_]; diff --git a/resolve/hykkt/PermClass.hpp b/resolve/hykkt/PermClass.hpp index 3a8b10c29..5d7ab4019 100644 --- a/resolve/hykkt/PermClass.hpp +++ b/resolve/hykkt/PermClass.hpp @@ -1,7 +1,7 @@ #pragma once -enum Permutation_Type { perm_v, rev_perm_v, perm_h_v, perm_j_v, perm_jt_v }; +enum PermutationType { perm_v, rev_perm_v, perm_h_v, perm_j_v, perm_jt_v }; class PermClass { @@ -146,7 +146,7 @@ class PermClass * * @post new_val contains the permuted old_val */ - void map_index(Permutation_Type permutation, + void map_index(PermutationType permutation, double* old_val, double* new_val); diff --git a/tests/unit/hykkt/HykktPerm.hpp b/tests/unit/hykkt/HykktPerm.hpp index 27199014c..3f728d713 100644 --- a/tests/unit/hykkt/HykktPerm.hpp +++ b/tests/unit/hykkt/HykktPerm.hpp @@ -37,14 +37,14 @@ class HykktPerm : public TestBase bool flagc = false; PermClass pc(n, nnz, nnz); - pc.add_h_info(a_i, a_j); - pc.add_j_info(a_i, a_j, n, m); - pc.add_jt_info(a_i, a_j); - pc.add_perm(perm); - pc.invert_perm(); + pc.addHInfo(a_i, a_j); + pc.addJInfo(a_i, a_j, n, m); + pc.addJtInfo(a_i, a_j); + pc.addPerm(perm); + pc.invertPerm(); // Test RC permutation - pc.vec_map_rc(b_i, b_j); + pc.vecMapRC(b_i, b_j); printf("Comparing RC permutation\n"); for (int i = 0; i < n + 1; i++) // Loop over row pointers (n+1) { @@ -65,7 +65,7 @@ class HykktPerm : public TestBase printf(flagrc ? "RC permutation failed\n" : "RC permutation passed\n"); // Test R permutation - pc.vec_map_r(b_i, b_j); + pc.vecMapR(b_i, b_j); printf("Comparing R permutation\n"); for (int i = 0; i < n + 1; i++) { @@ -86,7 +86,7 @@ class HykktPerm : public TestBase printf(flagr ? "R permutation failed\n" : "R permutation passed\n"); // Test C permutation - pc.vec_map_c(b_j); + pc.vecMapC(b_j); printf("Comparing C permutation\n"); for (int i = 0; i < n + 1; i++) { From b6b1617c15c00a56d673ef1158d91ac6807d6659 Mon Sep 17 00:00:00 2001 From: shakedregev Date: Fri, 3 Jan 2025 22:59:22 +0000 Subject: [PATCH 17/30] fixed straggling old names --- resolve/hykkt/PermClass.cpp | 18 ++++++++-------- resolve/hykkt/PermClass.hpp | 22 ++++++++++---------- resolve/hykkt/cpuHykktPermutationKernels.cpp | 22 ++++++++++---------- resolve/hykkt/cpuHykktPermutationKernels.hpp | 14 ++++++------- 4 files changed, 38 insertions(+), 38 deletions(-) diff --git a/resolve/hykkt/PermClass.cpp b/resolve/hykkt/PermClass.cpp index 67ac8623e..9ff2f9e07 100644 --- a/resolve/hykkt/PermClass.cpp +++ b/resolve/hykkt/PermClass.cpp @@ -68,22 +68,22 @@ PermClass::PermClass(int n_h, int nnz_h, int nnz_j) void PermClass::invertPerm() { - reverse_perm(n_h_, perm_, rev_perm_); + reversePerm(n_h_, perm_, rev_perm_); } void PermClass::vecMapRC(int* b_i, int* b_j) { - make_vecMapRC(n_h_, h_i_, h_j_, perm_, rev_perm_, b_i, b_j, perm_map_h_); + makeVecMapRC(n_h_, h_i_, h_j_, perm_, rev_perm_, b_i, b_j, perm_map_h_); } void PermClass::vecMapC(int* b_j) { - make_vecMapC(n_j_, j_i_, j_j_, rev_perm_, b_j, perm_map_j_); + makeVecMapC(n_j_, j_i_, j_j_, rev_perm_, b_j, perm_map_j_); } void PermClass::vecMapR(int* b_i, int* b_j) { - make_vecMapR(m_j_, jt_i_, jt_j_, perm_, b_i, b_j, perm_map_jt_); + makeVecMapR(m_j_, jt_i_, jt_j_, perm_, b_i, b_j, perm_map_jt_); } void PermClass::map_index(PermutationType permutation, @@ -93,19 +93,19 @@ PermClass::PermClass(int n_h, int nnz_h, int nnz_j) switch(permutation) { case perm_v: - cpu_map_idx(n_h_, perm_, old_val, new_val); + cpuMapIdx(n_h_, perm_, old_val, new_val); break; case rev_perm_v: - cpu_map_idx(n_h_, rev_perm_, old_val, new_val); + cpuMapIdx(n_h_, rev_perm_, old_val, new_val); break; case perm_h_v: - cpu_map_idx(nnz_h_, perm_map_h_, old_val, new_val); + cpuMapIdx(nnz_h_, perm_map_h_, old_val, new_val); break; case perm_j_v: - cpu_map_idx(nnz_j_, perm_map_j_, old_val, new_val); + cpuMapIdx(nnz_j_, perm_map_j_, old_val, new_val); break; case perm_jt_v: - cpu_map_idx(nnz_j_, perm_map_jt_, old_val, new_val); + cpuMapIdx(nnz_j_, perm_map_jt_, 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"); diff --git a/resolve/hykkt/PermClass.hpp b/resolve/hykkt/PermClass.hpp index 5d7ab4019..6a387b569 100644 --- a/resolve/hykkt/PermClass.hpp +++ b/resolve/hykkt/PermClass.hpp @@ -21,7 +21,7 @@ class PermClass * * @post h_i_ set to h_i, h_j_ set to h_j */ - void add_h_info(int* h_i, int* h_j); + void addHInfo(int* h_i, int* h_j); /* * @brief loads CSR structure for matrix J @@ -32,7 +32,7 @@ class PermClass * * @post j_i_ set to j_i, j_j_ set to j_j, n_j_ set to n_j, m_j_ set to m_j */ - void add_j_info(int* j_i, int* j_j, int n_j, int m_j); + void addJInfo(int* j_i, int* j_j, int n_j, int m_j); /* * @brief loads CSR structure for matrix Jt @@ -43,7 +43,7 @@ class PermClass * @pre * @post jt_i_ set to jt_i, jt_j_ set to jt_j */ - void add_jt_info(int* jt_i, int* jt_j); + void addJtInfo(int* jt_i, int* jt_j); /* * @brief sets custom permutation of matrix @@ -56,7 +56,7 @@ class PermClass * set to false so that custom_perm not deleted twice in destructors, * permutation vector copied onto device d_perm */ - void add_perm(int* custom_perm); + void addPerm(int* custom_perm); /* * @brief Uses Symmetric Approximate Minimum Degree @@ -66,10 +66,10 @@ class PermClass * 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 + * @post perm is the perumation vector that implements symAmd * on the 2x2 system */ - void symamd(); + void symAmd(); /* * @brief Creates reverse permutation of perm and copies onto device @@ -80,7 +80,7 @@ class PermClass * @post rev_perm is now the reverse permuation of perm and copied onto * the device d_perm */ - void invert_perm(); + void invertPerm(); /* * @brief Creates permutation of rows and columns of matrix @@ -97,7 +97,7 @@ class PermClass * @post perm_map_h is now permuted rows/columns of H and copied onto * the device d_perm_map_h */ - void vec_map_rc(int* b_i, int* b_j); + void vecMapRC(int* b_i, int* b_j); /* * @brief Creates the permutation of the columns of matrix J @@ -112,7 +112,7 @@ class PermClass * @post perm_map_j is now the column permutation and is copied onto * the device d_perm_map_j */ - void vec_map_c(int* b_j); + void vecMapC(int* b_j); /* * @brief Creates the permutation of the rows of matrix Jt @@ -128,7 +128,7 @@ class PermClass * @post perm_map_jt is now the permuations of the rows of J transpose * and is copied onto the device d_perm_map_jt */ - void vec_map_r(int* b_i, int* b_j); + void vecMapR(int* b_i, int* b_j); /* * @brief maps the permutated values of old_val to new_val @@ -165,7 +165,7 @@ class PermClass * is now a vector with size nnz_h_, perm_map_j and perm_map_jt * are now vectors with size nnz_j_ */ - void allocate_workspace(); + void allocateWorkspace(); // member variables bool perm_is_default_ = true; // boolean if perm set custom diff --git a/resolve/hykkt/cpuHykktPermutationKernels.cpp b/resolve/hykkt/cpuHykktPermutationKernels.cpp index 0c03cd349..e537d588c 100644 --- a/resolve/hykkt/cpuHykktPermutationKernels.cpp +++ b/resolve/hykkt/cpuHykktPermutationKernels.cpp @@ -1,13 +1,13 @@ #include "cpuHykktPermutationKernels.hpp" -void cpu_map_idx(int n, int* perm, double* old_val, double* new_val) +void cpuMapIdx(int n, int* perm, double* old_val, double* new_val) { for (int i = 0; i < n; i++) { new_val[i] = old_val[perm[i]]; } } -void selection_sort2(int len, int* arr1, int* arr2) +void SelectionSort2(int len, int* arr1, int* arr2) { int min_ind; int temp; @@ -64,7 +64,7 @@ void quickSort(int* arr1, int* arr2, int low, int high) { } } -void insertion_sort(int n, int* arr1, int* arr2) +void InsertionSort(int n, int* arr1, int* arr2) { int i, key1, key2, j; for (i = 1; i < n; i++) @@ -85,7 +85,7 @@ void insertion_sort(int n, int* arr1, int* arr2) } } -void make_vec_map_c(int n, +void makeVecMapC(int n, int* rows, int* cols, int* rev_perm, @@ -104,15 +104,15 @@ void make_vec_map_c(int n, perm_cols[row_s + j] = rev_perm[cols[row_s + j]]; } #if 0 - selection_sort2(rowlen, &perm_cols[row_s], &perm_map[row_s]); + SelectionSort2(rowlen, &perm_cols[row_s], &perm_map[row_s]); #else //quickSort(&perm_cols[row_s], &perm_map[row_s], 0, rowlen-1); - insertion_sort(rowlen, &perm_cols[row_s], &perm_map[row_s]); + InsertionSort(rowlen, &perm_cols[row_s], &perm_map[row_s]); #endif } } -void reverse_perm(int n, int* perm, int* rev_perm) +void reversePerm(int n, int* perm, int* rev_perm) { for(int i = 0; i < n; i++) { @@ -120,7 +120,7 @@ void reverse_perm(int n, int* perm, int* rev_perm) } } -void make_vec_map_r(int n, +void makeVecMapR(int n, int* rows, int* cols, int* perm, @@ -148,7 +148,7 @@ void make_vec_map_r(int n, } } -void make_vec_map_rc(int n, +void makeVecMapRC(int n, int* rows, int* cols, int* perm, @@ -174,10 +174,10 @@ void make_vec_map_rc(int n, perm_cols[count + j] = rev_perm[cols[row_s + j]]; } #if 0 - selection_sort2(rowlen, &perm_cols[count], &perm_map[count]); + SelectionSort2(rowlen, &perm_cols[count], &perm_map[count]); #else //quickSort(&perm_cols[count], &perm_map[count], 0, rowlen-1); - insertion_sort(rowlen, &perm_cols[count], &perm_map[count]); + InsertionSort(rowlen, &perm_cols[count], &perm_map[count]); #endif count += rowlen; } diff --git a/resolve/hykkt/cpuHykktPermutationKernels.hpp b/resolve/hykkt/cpuHykktPermutationKernels.hpp index bafc494cd..ff5e556f9 100644 --- a/resolve/hykkt/cpuHykktPermutationKernels.hpp +++ b/resolve/hykkt/cpuHykktPermutationKernels.hpp @@ -11,7 +11,7 @@ * * @post: new_val contains the permuted old_val */ -void cpu_map_idx(int n, int* perm, double* old_val, double* new_val); +void cpuMapIdx(int n, int* perm, double* old_val, double* new_val); /* * @brief: Selection sorts arr1 and arr2 w/indices * based on increasing value in arr1 @@ -24,7 +24,7 @@ void cpu_map_idx(int n, int* perm, double* old_val, double* new_val); * * @post: arr1 and arr2 are sorted based on increasing values in arr1 */ -void selection_sort2(int len, int* arr1, int* arr2); +void SelectionSort2(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); @@ -42,7 +42,7 @@ void quickSort(int* arr1, int* arr2, int low, int high); * * @post: arr1 and arr2 are sorted based on increasing values in arr1 */ -void insertion_sort(int len, int* arr1, int* arr2); +void InsertionSort(int len, int* arr1, int* arr2); /* * @brief: Permutes the columns in a matrix represented by rows and cols @@ -56,7 +56,7 @@ void insertion_sort(int len, int* arr1, int* arr2); * @post: perm_cols is now the permuted column array and perm_map stores * the corresponding indices to facilitate permuting the values */ -void make_vec_map_c(int n, +void makeVecMapC(int n, int* rows, int* cols, int* rev_perm, @@ -72,7 +72,7 @@ void make_vec_map_c(int n, * * @post: rev_perm now contains the reverse permutation */ -void reverse_perm(int n, int* perm, int* rev_perm); +void reversePerm(int n, int* perm, int* rev_perm); /* * @brief: Permutes the rows in a matrix represented by rows and cols * @@ -85,7 +85,7 @@ void reverse_perm(int n, int* perm, int* rev_perm); * @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 make_vec_map_r(int n, +void makeVecMapR(int n, int* rows, int* cols, int* perm, @@ -105,7 +105,7 @@ void make_vec_map_r(int n, * @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 make_vec_map_rc(int n, +void makeVecMapRC(int n, int* rows, int* cols, int* perm, From 40e671cf03a8d4aeffb1feae7d041ab86ffdfc32 Mon Sep 17 00:00:00 2001 From: shakedregev Date: Mon, 6 Jan 2025 17:10:12 +0000 Subject: [PATCH 18/30] stash --- tests/unit/CMakeLists.txt | 8 -------- 1 file changed, 8 deletions(-) diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt index 121a0b196..d631ccc64 100644 --- a/tests/unit/CMakeLists.txt +++ b/tests/unit/CMakeLists.txt @@ -10,13 +10,5 @@ add_subdirectory(matrix) add_subdirectory(vector) add_subdirectory(utilities) add_subdirectory(memory) -<<<<<<< HEAD -<<<<<<< HEAD add_subdirectory(params) add_subdirectory(hykkt) -======= -#add_subdirectory(hykkt) ->>>>>>> 857f284 (Disable building HyKKT.) -======= -add_subdirectory(hykkt) ->>>>>>> 48c55ce (Rework hykkt cmake altogether, hello world test works.) From f663e3686b852dee3a3a0cf0b442d516d62cfa6e Mon Sep 17 00:00:00 2001 From: shakedregev Date: Mon, 6 Jan 2025 17:51:18 +0000 Subject: [PATCH 19/30] fixed capitalization for selectionSort2 --- resolve/hykkt/cpuHykktPermutationKernels.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resolve/hykkt/cpuHykktPermutationKernels.hpp b/resolve/hykkt/cpuHykktPermutationKernels.hpp index ff5e556f9..3a10a9d9e 100644 --- a/resolve/hykkt/cpuHykktPermutationKernels.hpp +++ b/resolve/hykkt/cpuHykktPermutationKernels.hpp @@ -24,7 +24,7 @@ void cpuMapIdx(int n, int* perm, double* old_val, double* new_val); * * @post: arr1 and arr2 are sorted based on increasing values in arr1 */ -void SelectionSort2(int len, int* arr1, int* arr2); +void selectionSort2(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); From d439018a770545103910e3e45205a7e78774af0e Mon Sep 17 00:00:00 2001 From: shakedregev Date: Mon, 6 Jan 2025 17:51:32 +0000 Subject: [PATCH 20/30] fixed capitalization for selectionSort2 --- resolve/hykkt/cpuHykktPermutationKernels.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/resolve/hykkt/cpuHykktPermutationKernels.cpp b/resolve/hykkt/cpuHykktPermutationKernels.cpp index e537d588c..9f566fb76 100644 --- a/resolve/hykkt/cpuHykktPermutationKernels.cpp +++ b/resolve/hykkt/cpuHykktPermutationKernels.cpp @@ -7,7 +7,7 @@ void cpuMapIdx(int n, int* perm, double* old_val, double* new_val) } } -void SelectionSort2(int len, int* arr1, int* arr2) +void selectionSort2(int len, int* arr1, int* arr2) { int min_ind; int temp; @@ -104,7 +104,7 @@ void makeVecMapC(int n, perm_cols[row_s + j] = rev_perm[cols[row_s + j]]; } #if 0 - SelectionSort2(rowlen, &perm_cols[row_s], &perm_map[row_s]); + selectionSort2(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]); @@ -174,7 +174,7 @@ void makeVecMapRC(int n, perm_cols[count + j] = rev_perm[cols[row_s + j]]; } #if 0 - SelectionSort2(rowlen, &perm_cols[count], &perm_map[count]); + selectionSort2(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]); From e87366777abf0cd7b757f05599d45d670a45de7f Mon Sep 17 00:00:00 2001 From: shakedregev Date: Tue, 7 Jan 2025 19:29:50 +0000 Subject: [PATCH 21/30] doesn't add hykkt if not using suitesparse --- resolve/CMakeLists.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/resolve/CMakeLists.txt b/resolve/CMakeLists.txt index 7906d4d9e..3e063dab1 100644 --- a/resolve/CMakeLists.txt +++ b/resolve/CMakeLists.txt @@ -140,7 +140,9 @@ if(RESOLVE_USE_HIP) endif() # Add HyKKT solver -add_subdirectory(hykkt) +if(RESOLVE_USE_KLU) + add_subdirectory(hykkt) +endif() #list(APPEND ReSolve_Targets_List resolve_hykkt) From bed24afe2730230e155ef2435595a7f82d1aeeb7 Mon Sep 17 00:00:00 2001 From: shakedregev Date: Tue, 7 Jan 2025 19:56:36 +0000 Subject: [PATCH 22/30] doesn't add hykkt if not using suitesparse --- tests/unit/CMakeLists.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt index d631ccc64..125d2cbae 100644 --- a/tests/unit/CMakeLists.txt +++ b/tests/unit/CMakeLists.txt @@ -11,4 +11,6 @@ add_subdirectory(vector) add_subdirectory(utilities) add_subdirectory(memory) add_subdirectory(params) -add_subdirectory(hykkt) +if(RESOLVE_USE_KLU) + add_subdirectory(hykkt) +endif() \ No newline at end of file From 0592edc8783491a35aae4a3484ed11e51641ecc4 Mon Sep 17 00:00:00 2001 From: shakedregev Date: Tue, 7 Jan 2025 20:50:39 +0000 Subject: [PATCH 23/30] added namespaces and capitalized enums --- resolve/hykkt/PermClass.cpp | 20 +- resolve/hykkt/PermClass.hpp | 383 +++++++++++++++++---------------- tests/unit/hykkt/HykktPerm.hpp | 2 +- 3 files changed, 204 insertions(+), 201 deletions(-) diff --git a/resolve/hykkt/PermClass.cpp b/resolve/hykkt/PermClass.cpp index 9ff2f9e07..2c91dab13 100644 --- a/resolve/hykkt/PermClass.cpp +++ b/resolve/hykkt/PermClass.cpp @@ -2,9 +2,10 @@ #include #include #include "amd.h" - -// Creates a class for the permutation of $H_\gamma$ in (6) -PermClass::PermClass(int n_h, int nnz_h, int nnz_j) +namespace ReSolve::Hykkt +{ + // Creates a class for the permutation of $H_\gamma$ in (6) + PermClass::PermClass(int n_h, int nnz_h, int nnz_j) : n_h_(n_h), nnz_h_(nnz_h), nnz_j_(nnz_j) @@ -92,23 +93,23 @@ PermClass::PermClass(int n_h, int nnz_h, int nnz_j) { switch(permutation) { - case perm_v: + case PERM_V: cpuMapIdx(n_h_, perm_, old_val, new_val); break; - case rev_perm_v: + case REV_PERM_V: cpuMapIdx(n_h_, rev_perm_, old_val, new_val); break; - case perm_h_v: + case PERM_H_V: cpuMapIdx(nnz_h_, perm_map_h_, old_val, new_val); break; - case perm_j_v: + case PERM_J_V: cpuMapIdx(nnz_j_, perm_map_j_, old_val, new_val); break; - case perm_jt_v: + case PERM_JT_V: cpuMapIdx(nnz_j_, perm_map_jt_, 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"); + printf("Valid arguments are PERM_V, REV_PERM_V, PERM_H_V, PERM_J_V, PERM_JT_V\n"); } } @@ -120,3 +121,4 @@ PermClass::PermClass(int n_h, int nnz_h, int nnz_j) perm_map_j_ = new int[nnz_j_]; perm_map_jt_ = new int[nnz_j_]; } +} diff --git a/resolve/hykkt/PermClass.hpp b/resolve/hykkt/PermClass.hpp index 6a387b569..509c9ea8d 100644 --- a/resolve/hykkt/PermClass.hpp +++ b/resolve/hykkt/PermClass.hpp @@ -1,199 +1,200 @@ #pragma once - - -enum PermutationType { perm_v, rev_perm_v, perm_h_v, perm_j_v, perm_jt_v }; - -class PermClass +namespace ReSolve::Hykkt { -public: - - // constructor - PermClass(int n_h, int nnz_h, int nnz_j); - - // destructor - ~PermClass(); + enum PermutationType { PERM_V, REV_PERM_V, PERM_H_V, PERM_J_V, PERM_JT_V }; - /* - * @brief loads CSR structure for matrix H - * - * @param h_i - Row offsets for H - * h_j - Column indices for H - * - * @post h_i_ set to h_i, h_j_ set to h_j - */ - void addHInfo(int* h_i, int* h_j); - - /* - * @brief loads CSR structure for matrix J - * - * @param j_i - Row offsets for J - * j_j - Column indices for j - * n_j, m_j - dimensions of J - * - * @post j_i_ set to j_i, j_j_ set to j_j, n_j_ set to n_j, m_j_ set to m_j - */ - void addJInfo(int* j_i, int* j_j, int n_j, int m_j); - - /* - * @brief loads CSR structure for matrix Jt - * - * @param jt_i - Row offsets for Jt - * jt_j - Column indices for Jt - * - * @pre - * @post jt_i_ set to jt_i, jt_j_ set to jt_j - */ - void addJtInfo(int* jt_i, int* jt_j); - - /* - * @brief sets custom permutation of matrix - * - * @param custom_perm - custom permutation vector - * - * @pre Member variable n_h_ 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); + class PermClass + { + public: - /* - * @brief Uses Symmetric Approximate Minimum Degree - * to reduce zero-fill in Cholesky Factorization - * - * @pre Member variables n_h_, nnz_h_, h_i_, h_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(); + // constructor + PermClass(int n_h, int nnz_h, int nnz_j); + + // destructor + ~PermClass(); + + /* + * @brief loads CSR structure for matrix H + * + * @param h_i - Row offsets for H + * h_j - Column indices for H + * + * @post h_i_ set to h_i, h_j_ set to h_j + */ + void addHInfo(int* h_i, int* h_j); + + /* + * @brief loads CSR structure for matrix J + * + * @param j_i - Row offsets for J + * j_j - Column indices for j + * n_j, m_j - dimensions of J + * + * @post j_i_ set to j_i, j_j_ set to j_j, n_j_ set to n_j, m_j_ set to m_j + */ + void addJInfo(int* j_i, int* j_j, int n_j, int m_j); + + /* + * @brief loads CSR structure for matrix Jt + * + * @param jt_i - Row offsets for Jt + * jt_j - Column indices for Jt + * + * @pre + * @post jt_i_ set to jt_i, jt_j_ set to jt_j + */ + void addJtInfo(int* jt_i, int* jt_j); + + /* + * @brief sets custom permutation of matrix + * + * @param custom_perm - custom permutation vector + * + * @pre Member variable n_h_ 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_h_, nnz_h_, h_i_, h_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_h_, 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 b_i - row offsets of permutation + * b_j - column indices of permutation + * + * @pre Member variables n_h_, nnz_h_, h_i_, h_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* b_i, int* b_j); + + /* + * @brief Creates the permutation of the columns of matrix J + * and copies onto device + * + * @param b_j - column indices of permutation + * + * @pre Member variables n_j_, nnz_j_, j_i_, j_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_j is now the column permutation and is copied onto + * the device d_perm_map_j + */ + void vecMapC(int* b_j); + + /* + * @brief Creates the permutation of the rows of matrix Jt + * and copies onto device + * + * @param b_i - row offsets of permutation + * b_j - column indices of permutation + * + * @pre Member variables m_j_, nnz_j_, jt_i_, jt_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_jt is now the permuations of the rows of J transpose + * and is copied onto the device d_perm_map_jt + */ + void vecMapR(int* b_i, int* b_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_h_, nnz_h_, nnz_j_, d_perm, d_rev_perm, + * d_perm_map_h, d_perm_map_j, d_perm_map_jt 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 Creates reverse permutation of perm and copies onto device - * - * @pre Member variables n_h_, 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 + * @brief allocates memory on host for permutation vectors + * + * @pre Member variables n_h_, nnz_h_, nnz_j_ 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_h_, perm_map_h + * is now a vector with size nnz_h_, perm_map_j and perm_map_jt + * are now vectors with size nnz_j_ */ - void invertPerm(); - - /* - * @brief Creates permutation of rows and columns of matrix - * and copies onto device - * - * @param b_i - row offsets of permutation - * b_j - column indices of permutation - * - * @pre Member variables n_h_, nnz_h_, h_i_, h_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* b_i, int* b_j); - - /* - * @brief Creates the permutation of the columns of matrix J - * and copies onto device - * - * @param b_j - column indices of permutation - * - * @pre Member variables n_j_, nnz_j_, j_i_, j_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_j is now the column permutation and is copied onto - * the device d_perm_map_j - */ - void vecMapC(int* b_j); - - /* - * @brief Creates the permutation of the rows of matrix Jt - * and copies onto device - * - * @param b_i - row offsets of permutation - * b_j - column indices of permutation - * - * @pre Member variables m_j_, nnz_j_, jt_i_, jt_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_jt is now the permuations of the rows of J transpose - * and is copied onto the device d_perm_map_jt - */ - void vecMapR(int* b_i, int* b_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_h_, nnz_h_, nnz_j_, d_perm, d_rev_perm, - * d_perm_map_h, d_perm_map_j, d_perm_map_jt 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 allocates memory on host for permutation vectors - * - * @pre Member variables n_h_, nnz_h_, nnz_j_ 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_h_, perm_map_h - * is now a vector with size nnz_h_, perm_map_j and perm_map_jt - * are now vectors with size nnz_j_ -*/ - void allocateWorkspace(); - - // member variables - bool perm_is_default_ = true; // boolean if perm set custom - - int n_h_; // dimension of H - int nnz_h_; // nonzeros of H - - int n_j_; // dimensions of J - int m_j_; - int nnz_j_; // nonzeros of J - - int* perm_; // permutation of 2x2 system - int* rev_perm_; // reverse of permutation - int* perm_map_h_; // mapping of permuted H - int* perm_map_j_; // mapping of permuted J - int* perm_map_jt_; // mapping of permuted Jt - - int* h_i_; // row offsets of csr storage of H - int* h_j_; // column pointers of csr storage of H - - int* j_i_; // row offsets of csr storage of J - int* j_j_; // column pointers of csr storage of J - - int* jt_i_; // row offsets of csr storage of J transpose - int* jt_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 -}; + void allocateWorkspace(); + + // member variables + bool perm_is_default_ = true; // boolean if perm set custom + + int n_h_; // dimension of H + int nnz_h_; // nonzeros of H + + int n_j_; // dimensions of J + int m_j_; + int nnz_j_; // nonzeros of J + + int* perm_; // permutation of 2x2 system + int* rev_perm_; // reverse of permutation + int* perm_map_h_; // mapping of permuted H + int* perm_map_j_; // mapping of permuted J + int* perm_map_jt_; // mapping of permuted Jt + + int* h_i_; // row offsets of csr storage of H + int* h_j_; // column pointers of csr storage of H + + int* j_i_; // row offsets of csr storage of J + int* j_j_; // column pointers of csr storage of J + + int* jt_i_; // row offsets of csr storage of J transpose + int* jt_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 + }; +} diff --git a/tests/unit/hykkt/HykktPerm.hpp b/tests/unit/hykkt/HykktPerm.hpp index 3f728d713..09c72765e 100644 --- a/tests/unit/hykkt/HykktPerm.hpp +++ b/tests/unit/hykkt/HykktPerm.hpp @@ -36,7 +36,7 @@ class HykktPerm : public TestBase bool flagr = false; bool flagc = false; - PermClass pc(n, nnz, nnz); + ReSolve::Hykkt::PermClass pc(n, nnz, nnz); pc.addHInfo(a_i, a_j); pc.addJInfo(a_i, a_j, n, m); pc.addJtInfo(a_i, a_j); From 91c0f04ee53dea575cadd236bb7fa42caf28a8fa Mon Sep 17 00:00:00 2001 From: shakedregev Date: Wed, 8 Jan 2025 20:28:30 +0000 Subject: [PATCH 24/30] fixed naming --- resolve/hykkt/PermClass.cpp | 78 +++++++------- resolve/hykkt/PermClass.hpp | 106 +++++++++---------- resolve/hykkt/cpuHykktPermutationKernels.cpp | 12 +-- resolve/hykkt/cpuHykktPermutationKernels.hpp | 4 +- 4 files changed, 100 insertions(+), 100 deletions(-) diff --git a/resolve/hykkt/PermClass.cpp b/resolve/hykkt/PermClass.cpp index 2c91dab13..1f83dd63c 100644 --- a/resolve/hykkt/PermClass.cpp +++ b/resolve/hykkt/PermClass.cpp @@ -5,10 +5,10 @@ namespace ReSolve::Hykkt { // Creates a class for the permutation of $H_\gamma$ in (6) - PermClass::PermClass(int n_h, int nnz_h, int nnz_j) - : n_h_(n_h), - nnz_h_(nnz_h), - nnz_j_(nnz_j) + PermClass::PermClass(int n_hes, int nnz_hes, int nnz_jac) + : n_hes_(n_hes), + nnz_hes_(nnz_hes), + nnz_jac_(nnz_jac) { allocateWorkspace(); } @@ -19,29 +19,29 @@ namespace ReSolve::Hykkt delete [] perm_; } delete [] rev_perm_; - delete [] perm_map_h_; - delete [] perm_map_j_; - delete [] perm_map_jt_; + delete [] perm_map_hes_; + delete [] perm_map_jac_; + delete [] perm_map_jac_tr_; } - void PermClass::addHInfo(int* h_i, int* h_j) + void PermClass::addHInfo(int* hes_i, int* hes_j) { - h_i_ = h_i; - h_j_ = h_j; + hes_i_ = hes_i; + hes_j_ = hes_j; } - void PermClass::addJInfo(int* j_i, int* j_j, int n_j, int m_j) + void PermClass::addJInfo(int* jac_i, int* jac_j, int n_jac, int m_jac) { - j_i_ = j_i; - j_j_ = j_j; - n_j_ = n_j; - m_j_ = m_j; + jac_i_ = jac_i; + jac_j_ = jac_j; + n_jac_ = n_jac; + m_jac_ = m_jac; } - void PermClass::addJtInfo(int* jt_i, int* jt_j) + void PermClass::addJtInfo(int* jac_tr_i, int* jac_tr_j) { - jt_i_ = jt_i; - jt_j_ = jt_j; + jac_tr_i_ = jac_tr_i; + jac_tr_j_ = jac_tr_j; } void PermClass::addPerm(int* custom_perm) @@ -58,7 +58,7 @@ namespace ReSolve::Hykkt amd_defaults(Control); amd_control(Control); - int result = amd_order(n_h_, h_i_, h_j_, perm_, Control, Info); + int result = amd_order(n_hes_, hes_i_, hes_j_, perm_, Control, Info); if (result != AMD_OK) { @@ -69,22 +69,22 @@ namespace ReSolve::Hykkt void PermClass::invertPerm() { - reversePerm(n_h_, perm_, rev_perm_); + reversePerm(n_hes_, perm_, rev_perm_); } - void PermClass::vecMapRC(int* b_i, int* b_j) + void PermClass::vecMapRC(int* rhs_i, int* rhs_j) { - makeVecMapRC(n_h_, h_i_, h_j_, perm_, rev_perm_, b_i, b_j, perm_map_h_); + makeVecMapRC(n_hes_, hes_i_, hes_j_, perm_, rev_perm_, rhs_i, rhs_j, perm_map_hes_); } - void PermClass::vecMapC(int* b_j) + void PermClass::vecMapC(int* rhs_j) { - makeVecMapC(n_j_, j_i_, j_j_, rev_perm_, b_j, perm_map_j_); + makeVecMapC(n_jac_, jac_i_, jac_j_, rev_perm_, rhs_j, perm_map_jac_); } - void PermClass::vecMapR(int* b_i, int* b_j) + void PermClass::vecMapR(int* rhs_i, int* rhs_j) { - makeVecMapR(m_j_, jt_i_, jt_j_, perm_, b_i, b_j, perm_map_jt_); + makeVecMapR(m_jac_, jac_tr_i_, jac_tr_j_, perm_, rhs_i, rhs_j, perm_map_jac_tr_); } void PermClass::map_index(PermutationType permutation, @@ -94,19 +94,19 @@ namespace ReSolve::Hykkt switch(permutation) { case PERM_V: - cpuMapIdx(n_h_, perm_, old_val, new_val); + cpuMapIdx(n_hes_, perm_, old_val, new_val); break; case REV_PERM_V: - cpuMapIdx(n_h_, rev_perm_, old_val, new_val); + cpuMapIdx(n_hes_, rev_perm_, old_val, new_val); break; - case PERM_H_V: - cpuMapIdx(nnz_h_, perm_map_h_, old_val, new_val); + case PERM_HES_V: + cpuMapIdx(nnz_hes_, perm_map_hes_, old_val, new_val); break; - case PERM_J_V: - cpuMapIdx(nnz_j_, perm_map_j_, old_val, new_val); + case PERM_JAC_V: + cpuMapIdx(nnz_jac_, perm_map_jac_, old_val, new_val); break; - case PERM_JT_V: - cpuMapIdx(nnz_j_, perm_map_jt_, old_val, new_val); + 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"); @@ -115,10 +115,10 @@ namespace ReSolve::Hykkt void PermClass::allocateWorkspace() { - perm_ = new int[n_h_]; - rev_perm_ = new int[n_h_]; - perm_map_h_ = new int[nnz_h_]; - perm_map_j_ = new int[nnz_j_]; - perm_map_jt_ = new int[nnz_j_]; + 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_]; } } diff --git a/resolve/hykkt/PermClass.hpp b/resolve/hykkt/PermClass.hpp index 509c9ea8d..b0f225f9c 100644 --- a/resolve/hykkt/PermClass.hpp +++ b/resolve/hykkt/PermClass.hpp @@ -1,14 +1,14 @@ #pragma once namespace ReSolve::Hykkt { - enum PermutationType { PERM_V, REV_PERM_V, PERM_H_V, PERM_J_V, PERM_JT_V }; + enum PermutationType { PERM_V, REV_PERM_V, PERM_HES_V, PERM_JAC_V, PERM_JAC_TR_V }; class PermClass { public: // constructor - PermClass(int n_h, int nnz_h, int nnz_j); + PermClass(int n_hes, int nnz_hes, int nnz_jac); // destructor ~PermClass(); @@ -16,41 +16,41 @@ namespace ReSolve::Hykkt /* * @brief loads CSR structure for matrix H * - * @param h_i - Row offsets for H - * h_j - Column indices for H + * @param hes_i - Row offsets for H + * hes_j - Column indices for H * - * @post h_i_ set to h_i, h_j_ set to h_j + * @post hes_i_ set to hes_i, hes_j_ set to hes_j */ - void addHInfo(int* h_i, int* h_j); + void addHInfo(int* hes_i, int* hes_j); /* * @brief loads CSR structure for matrix J * - * @param j_i - Row offsets for J - * j_j - Column indices for j - * n_j, m_j - dimensions of J + * @param jac_i - Row offsets for J + * jac_j - Column indices for j + * n_jac, m_jac - dimensions of J * - * @post j_i_ set to j_i, j_j_ set to j_j, n_j_ set to n_j, m_j_ set to m_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* j_i, int* j_j, int n_j, int m_j); + void addJInfo(int* jac_i, int* jac_j, int n_jac, int m_jac); /* * @brief loads CSR structure for matrix Jt * - * @param jt_i - Row offsets for Jt - * jt_j - Column indices for Jt + * @param jac_tr_i - Row offsets for Jt + * jac_tr_j - Column indices for Jt * * @pre - * @post jt_i_ set to jt_i, jt_j_ set to jt_j + * @post jac_tr_i_ set to jac_tr_i, jac_tr_j_ set to jac_tr_j */ - void addJtInfo(int* jt_i, int* jt_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_h_ initialized to dimension of matrix + * @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, @@ -62,7 +62,7 @@ namespace ReSolve::Hykkt * @brief Uses Symmetric Approximate Minimum Degree * to reduce zero-fill in Cholesky Factorization * - * @pre Member variables n_h_, nnz_h_, h_i_, h_j_ have been + * @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 * @@ -74,7 +74,7 @@ namespace ReSolve::Hykkt /* * @brief Creates reverse permutation of perm and copies onto device * - * @pre Member variables n_h_, perm intialized to dimension of matrix + * @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 @@ -86,10 +86,10 @@ namespace ReSolve::Hykkt * @brief Creates permutation of rows and columns of matrix * and copies onto device * - * @param b_i - row offsets of permutation - * b_j - column indices of permutation + * @param rhs_i - row offsets of permutation + * rhs_j - column indices of permutation * - * @pre Member variables n_h_, nnz_h_, h_i_, h_j_, perm, rev_perm + * @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 @@ -97,38 +97,38 @@ namespace ReSolve::Hykkt * @post perm_map_h is now permuted rows/columns of H and copied onto * the device d_perm_map_h */ - void vecMapRC(int* b_i, int* b_j); + void vecMapRC(int* rhs_i, int* rhs_j); /* * @brief Creates the permutation of the columns of matrix J * and copies onto device * - * @param b_j - column indices of permutation + * @param rhs_j - column indices of permutation * - * @pre Member variables n_j_, nnz_j_, j_i_, j_j_, rev_perm initialized + * @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_j is now the column permutation and is copied onto - * the device d_perm_map_j + * @post perm_map_jac is now the column permutation and is copied onto + * the device d_perm_map_jac */ - void vecMapC(int* b_j); + void vecMapC(int* rhs_j); /* * @brief Creates the permutation of the rows of matrix Jt * and copies onto device * - * @param b_i - row offsets of permutation - * b_j - column indices of permutation + * @param rhs_i - row offsets of permutation + * rhs_j - column indices of permutation * - * @pre Member variables m_j_, nnz_j_, jt_i_, jt_j_, initialized to + * @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_jt is now the permuations of the rows of J transpose - * and is copied onto the device d_perm_map_jt + * @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* b_i, int* b_j); + void vecMapR(int* rhs_i, int* rhs_j); /* * @brief maps the permutated values of old_val to new_val @@ -137,8 +137,8 @@ namespace ReSolve::Hykkt * old_val - the old values in the matrix * new_val - the permuted values * - * @pre Member variables n_h_, nnz_h_, nnz_j_, d_perm, d_rev_perm, - * d_perm_map_h, d_perm_map_j, d_perm_map_jt initialized to + * @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 @@ -157,40 +157,40 @@ namespace ReSolve::Hykkt /* * @brief allocates memory on host for permutation vectors * - * @pre Member variables n_h_, nnz_h_, nnz_j_ are initialized to the + * @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_h_, perm_map_h - * is now a vector with size nnz_h_, perm_map_j and perm_map_jt - * are now vectors with size nnz_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_h_; // dimension of H - int nnz_h_; // nonzeros of H + int n_hes_; // dimension of H + int nnz_hes_; // nonzeros of H - int n_j_; // dimensions of J - int m_j_; - int nnz_j_; // nonzeros of J + 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_h_; // mapping of permuted H - int* perm_map_j_; // mapping of permuted J - int* perm_map_jt_; // mapping of permuted Jt + 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* h_i_; // row offsets of csr storage of H - int* h_j_; // column pointers of csr storage of H + int* hes_i_; // row offsets of csr storage of H + int* hes_j_; // column pointers of csr storage of H - int* j_i_; // row offsets of csr storage of J - int* j_j_; // column pointers of csr storage of J + int* jac_i_; // row offsets of csr storage of J + int* jac_j_; // column pointers of csr storage of J - int* jt_i_; // row offsets of csr storage of J transpose - int* jt_j_; // column pointers of csr storage of J transpose + 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 diff --git a/resolve/hykkt/cpuHykktPermutationKernels.cpp b/resolve/hykkt/cpuHykktPermutationKernels.cpp index 9f566fb76..ffe9e30e0 100644 --- a/resolve/hykkt/cpuHykktPermutationKernels.cpp +++ b/resolve/hykkt/cpuHykktPermutationKernels.cpp @@ -7,7 +7,7 @@ void cpuMapIdx(int n, int* perm, double* old_val, double* new_val) } } -void selectionSort2(int len, int* arr1, int* arr2) +void selectionSort(int len, int* arr1, int* arr2) { int min_ind; int temp; @@ -64,7 +64,7 @@ void quickSort(int* arr1, int* arr2, int low, int high) { } } -void InsertionSort(int n, int* arr1, int* arr2) +void insertionSort(int n, int* arr1, int* arr2) { int i, key1, key2, j; for (i = 1; i < n; i++) @@ -104,10 +104,10 @@ void makeVecMapC(int n, perm_cols[row_s + j] = rev_perm[cols[row_s + j]]; } #if 0 - selectionSort2(rowlen, &perm_cols[row_s], &perm_map[row_s]); + 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]); + insertionSort(rowlen, &perm_cols[row_s], &perm_map[row_s]); #endif } } @@ -174,10 +174,10 @@ void makeVecMapRC(int n, perm_cols[count + j] = rev_perm[cols[row_s + j]]; } #if 0 - selectionSort2(rowlen, &perm_cols[count], &perm_map[count]); + 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]); + insertionSort(rowlen, &perm_cols[count], &perm_map[count]); #endif count += rowlen; } diff --git a/resolve/hykkt/cpuHykktPermutationKernels.hpp b/resolve/hykkt/cpuHykktPermutationKernels.hpp index 3a10a9d9e..321c07593 100644 --- a/resolve/hykkt/cpuHykktPermutationKernels.hpp +++ b/resolve/hykkt/cpuHykktPermutationKernels.hpp @@ -24,7 +24,7 @@ void cpuMapIdx(int n, int* perm, double* old_val, double* new_val); * * @post: arr1 and arr2 are sorted based on increasing values in arr1 */ -void selectionSort2(int len, int* arr1, int* arr2); +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); @@ -42,7 +42,7 @@ void quickSort(int* arr1, int* arr2, int low, int high); * * @post: arr1 and arr2 are sorted based on increasing values in arr1 */ -void InsertionSort(int len, int* arr1, int* arr2); +void insertionSort(int len, int* arr1, int* arr2); /* * @brief: Permutes the columns in a matrix represented by rows and cols From 4eb17c2a0146a95795e524649d9727cbf6ecb5be Mon Sep 17 00:00:00 2001 From: shakedregev Date: Wed, 8 Jan 2025 20:41:12 +0000 Subject: [PATCH 25/30] function alignment --- resolve/hykkt/PermClass.cpp | 9 +++++ resolve/hykkt/PermClass.hpp | 11 ++++++ resolve/hykkt/cpuHykktPermutationKernels.cpp | 36 ++++++++++---------- resolve/hykkt/cpuHykktPermutationKernels.hpp | 36 ++++++++++---------- 4 files changed, 56 insertions(+), 36 deletions(-) diff --git a/resolve/hykkt/PermClass.cpp b/resolve/hykkt/PermClass.cpp index 1f83dd63c..90d497b29 100644 --- a/resolve/hykkt/PermClass.cpp +++ b/resolve/hykkt/PermClass.cpp @@ -113,6 +113,15 @@ namespace ReSolve::Hykkt } } + 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_]; diff --git a/resolve/hykkt/PermClass.hpp b/resolve/hykkt/PermClass.hpp index b0f225f9c..49515587d 100644 --- a/resolve/hykkt/PermClass.hpp +++ b/resolve/hykkt/PermClass.hpp @@ -153,6 +153,17 @@ namespace ReSolve::Hykkt 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 diff --git a/resolve/hykkt/cpuHykktPermutationKernels.cpp b/resolve/hykkt/cpuHykktPermutationKernels.cpp index ffe9e30e0..0a91ec0fc 100644 --- a/resolve/hykkt/cpuHykktPermutationKernels.cpp +++ b/resolve/hykkt/cpuHykktPermutationKernels.cpp @@ -86,11 +86,11 @@ void insertionSort(int n, int* arr1, int* arr2) } void makeVecMapC(int n, - int* rows, - int* cols, - int* rev_perm, - int* perm_cols, - int* perm_map) + int* rows, + int* cols, + int* rev_perm, + int* perm_cols, + int* perm_map) { int row_s; int rowlen; @@ -121,12 +121,12 @@ void reversePerm(int n, int* perm, int* rev_perm) } void makeVecMapR(int n, - int* rows, - int* cols, - int* perm, - int* perm_rows, - int* perm_cols, - int* perm_map) + int* rows, + int* cols, + int* perm, + int* perm_rows, + int* perm_cols, + int* perm_map) { perm_rows[0] = 0; int count = 0; @@ -149,13 +149,13 @@ void makeVecMapR(int n, } void makeVecMapRC(int n, - int* rows, - int* cols, - int* perm, - int* rev_perm, - int* perm_rows, - int* perm_cols, - int* perm_map) + int* rows, + int* cols, + int* perm, + int* rev_perm, + int* perm_rows, + int* perm_cols, + int* perm_map) { perm_rows[0] = 0; int count = 0; diff --git a/resolve/hykkt/cpuHykktPermutationKernels.hpp b/resolve/hykkt/cpuHykktPermutationKernels.hpp index 321c07593..3f216c60c 100644 --- a/resolve/hykkt/cpuHykktPermutationKernels.hpp +++ b/resolve/hykkt/cpuHykktPermutationKernels.hpp @@ -57,11 +57,11 @@ void insertionSort(int len, int* arr1, int* arr2); * the corresponding indices to facilitate permuting the values */ void makeVecMapC(int n, - int* rows, - int* cols, - int* rev_perm, - int* perm_cols, - int* perm_map); + int* rows, + int* cols, + int* rev_perm, + int* perm_cols, + int* perm_map); /* * * @brief: Creates a reverse permutation based on a given permutation @@ -86,12 +86,12 @@ void reversePerm(int n, int* perm, int* rev_perm); * perm_map stores the corresponding indices to facilitate permuting the values */ void makeVecMapR(int n, - int* rows, - int* cols, - int* perm, - int* perm_rows, - int* perm_cols, - int* perm_map); + int* rows, + int* cols, + 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 * @@ -106,10 +106,10 @@ void makeVecMapR(int n, * perm_map stores the corresponding indices to facilitate permuting the values */ void makeVecMapRC(int n, - int* rows, - int* cols, - int* perm, - int* rev_perm, - int* perm_rows, - int* perm_cols, - int* perm_map); + int* rows, + int* cols, + int* perm, + int* rev_perm, + int* perm_rows, + int* perm_cols, + int* perm_map); From fa242c10f55c666a983ea4c1b899145caa075169 Mon Sep 17 00:00:00 2001 From: shakedregev Date: Wed, 8 Jan 2025 21:34:37 +0000 Subject: [PATCH 26/30] namespace fixes, const where possible --- resolve/hykkt/PermClass.cpp | 5 +- resolve/hykkt/PermClass.hpp | 4 +- resolve/hykkt/cpuHykktPermutationKernels.cpp | 293 ++++++++++--------- resolve/hykkt/cpuHykktPermutationKernels.hpp | 223 +++++++------- tests/unit/hykkt/HykktPerm.hpp | 2 +- 5 files changed, 264 insertions(+), 263 deletions(-) diff --git a/resolve/hykkt/PermClass.cpp b/resolve/hykkt/PermClass.cpp index 90d497b29..6bf98f586 100644 --- a/resolve/hykkt/PermClass.cpp +++ b/resolve/hykkt/PermClass.cpp @@ -2,8 +2,7 @@ #include #include #include "amd.h" -namespace ReSolve::Hykkt -{ +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), @@ -130,4 +129,4 @@ namespace ReSolve::Hykkt 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 index 49515587d..e5c48372c 100644 --- a/resolve/hykkt/PermClass.hpp +++ b/resolve/hykkt/PermClass.hpp @@ -1,5 +1,5 @@ #pragma once -namespace ReSolve::Hykkt +namespace ReSolve { namespace hykkt { enum PermutationType { PERM_V, REV_PERM_V, PERM_HES_V, PERM_JAC_V, PERM_JAC_TR_V }; @@ -207,5 +207,5 @@ namespace ReSolve::Hykkt 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 index 0a91ec0fc..2a8220985 100644 --- a/resolve/hykkt/cpuHykktPermutationKernels.cpp +++ b/resolve/hykkt/cpuHykktPermutationKernels.cpp @@ -1,184 +1,185 @@ #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 cpuMapIdx(int n, int* perm, 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++) + void selectionSort(int len, int* arr1, int* arr2) { - min_ind = i; - for(int j = i + 1; j < len; j++) + int min_ind; + int temp; + for(int i = 0; i < len - 1; i++) { - if(arr1[j] < arr1[min_ind]) + min_ind = i; + for(int j = i + 1; j < len; j++) { - min_ind = 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; } - } - 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; + 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; -} + 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); + 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); } - 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 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++) + void insertionSort(int n, int* arr1, int* arr2) { - key1 = arr1[i]; - key2 = arr2[i]; + int i, key1, key2, j; + for (i = 1; i < n; i++) + { + key1 = arr1[i]; + key2 = arr2[i]; - j = i - 1; + 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; + 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, - int* rows, - int* cols, - int* rev_perm, - int* perm_cols, - int* perm_map) -{ - int row_s; - int rowlen; - for(int i = 0; i < n; i++) + void makeVecMapC(int n, + const int* rows, + const int* cols, + const int* rev_perm, + int* perm_cols, + int* perm_map) { - row_s = rows[i]; - rowlen = rows[i + 1] - row_s; - for(int j = 0; j < rowlen; j++) + int row_s; + int rowlen; + for(int i = 0; i < n; i++) { - perm_map[row_s + j] = row_s + j; - perm_cols[row_s + j] = rev_perm[cols[row_s + j]]; + 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 } -#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, int* perm, int* rev_perm) -{ - for(int i = 0; i < n; i++) + void reversePerm(int n, const int* perm, int* rev_perm) { - rev_perm[perm[i]] = i; + for(int i = 0; i < n; i++) + { + rev_perm[perm[i]] = i; + } } -} -void makeVecMapR(int n, - int* rows, - int* cols, - 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++) + void makeVecMapR(int n, + const int* rows, + const int* cols, + const int* perm, + int* perm_rows, + int* perm_cols, + int* perm_map) { - 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_rows[0] = 0; + int count = 0; + int idx; + int row_s; + int rowlen; + for(int i = 0; i < n; i++) { - perm_map[count + j] = row_s + j; - perm_cols[count + j] = cols[row_s + j]; + 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; } - count += rowlen; } -} -void makeVecMapRC(int n, - int* rows, - int* cols, - int* perm, - 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++) + 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) { - 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_rows[0] = 0; + int count = 0; + int idx; + int row_s; + int rowlen; + for(int i = 0; i < n; i++) { - perm_map[count + j] = row_s + j; - perm_cols[count + j] = rev_perm[cols[row_s + j]]; + 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; } -#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 index 3f216c60c..961716805 100644 --- a/resolve/hykkt/cpuHykktPermutationKernels.hpp +++ b/resolve/hykkt/cpuHykktPermutationKernels.hpp @@ -1,115 +1,116 @@ #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); -/* - * @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, int* perm, 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); -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: 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, - int* rows, - int* cols, - 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, 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, - int* rows, - int* cols, - 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, - int* rows, - int* cols, - int* perm, - int* rev_perm, - int* perm_rows, - int* perm_cols, - int* perm_map); + /* + * @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/tests/unit/hykkt/HykktPerm.hpp b/tests/unit/hykkt/HykktPerm.hpp index 09c72765e..8d009cc52 100644 --- a/tests/unit/hykkt/HykktPerm.hpp +++ b/tests/unit/hykkt/HykktPerm.hpp @@ -36,7 +36,7 @@ class HykktPerm : public TestBase bool flagr = false; bool flagc = false; - ReSolve::Hykkt::PermClass pc(n, nnz, nnz); + ReSolve::hykkt::PermClass pc(n, nnz, nnz); pc.addHInfo(a_i, a_j); pc.addJInfo(a_i, a_j, n, m); pc.addJtInfo(a_i, a_j); From 7c013d33b6c255b041b59f91a274d8e077d287b8 Mon Sep 17 00:00:00 2001 From: pelesh Date: Wed, 8 Jan 2025 13:43:15 -0800 Subject: [PATCH 27/30] Fix issue with linking workspace to HyKKT. (#211) --- resolve/hykkt/CMakeLists.txt | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/resolve/hykkt/CMakeLists.txt b/resolve/hykkt/CMakeLists.txt index a8853c55d..fae6ca11a 100644 --- a/resolve/hykkt/CMakeLists.txt +++ b/resolve/hykkt/CMakeLists.txt @@ -33,13 +33,13 @@ target_include_directories(resolve_hykkt PUBLIC ${SUITESPARSE_INCLUDE_DIR}) # Link to CUDA ReSolve backend if CUDA is support enabled if (RESOLVE_USE_CUDA) -# target_sources(resolve_hykkt PRIVATE ${HyKKT_CUDASDK_SRC}) -# target_link_libraries(resolve_hykkt PUBLIC resolve_backend_cuda) + target_sources(resolve_hykkt PRIVATE ${HyKKT_CUDASDK_SRC}) + target_link_libraries(resolve_hykkt PUBLIC resolve_backend_cuda) endif() if (RESOLVE_USE_HIP) -# target_sources(resolve_hykkt PRIVATE ${HyKKT_ROCM_SRC}) -# target_link_libraries(resolve_hykkt PUBLIC resolve_backend_hip) + target_sources(resolve_hykkt PRIVATE ${HyKKT_ROCM_SRC}) + target_link_libraries(resolve_hykkt PUBLIC resolve_backend_hip) endif() # Link to dummy device backend if GPU support is not enabled @@ -47,6 +47,8 @@ if (NOT RESOLVE_USE_GPU) target_link_libraries(resolve_hykkt PUBLIC resolve_backend_cpu) endif() +target_link_libraries(resolve_hykkt PUBLIC resolve_workspace) + target_include_directories(resolve_hykkt INTERFACE $ $ From 08d08b153fa1913a1a705bba9faf599113fa9272 Mon Sep 17 00:00:00 2001 From: pelesh Date: Mon, 13 Jan 2025 08:59:25 -0500 Subject: [PATCH 28/30] Coding style suggestions (#213) * Coding style suggestions. * added param in/out * Style suggestions for permutation tests. * Some more doxygen formatting. --------- Co-authored-by: shakedregev --- resolve/hykkt/CMakeLists.txt | 8 +- resolve/hykkt/PermClass.cpp | 132 -------- resolve/hykkt/PermClass.hpp | 211 ------------ resolve/hykkt/Permutation.cpp | 283 ++++++++++++++++ resolve/hykkt/Permutation.hpp | 89 +++++ resolve/hykkt/cpuHykktPermutationKernels.cpp | 185 ---------- resolve/hykkt/cpuHykktPermutationKernels.hpp | 116 ------- resolve/hykkt/cpuPermutationKernels.cpp | 316 ++++++++++++++++++ resolve/hykkt/cpuPermutationKernels.hpp | 64 ++++ tests/unit/hykkt/CMakeLists.txt | 8 +- tests/unit/hykkt/HykktPerm.hpp | 114 ------- tests/unit/hykkt/HykktPermutationTests.hpp | 130 +++++++ tests/unit/hykkt/runHykktPerm.cpp | 13 - tests/unit/hykkt/runHykktPermutationTests.cpp | 20 ++ 14 files changed, 910 insertions(+), 779 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 delete mode 100644 tests/unit/hykkt/HykktPerm.hpp create mode 100644 tests/unit/hykkt/HykktPermutationTests.hpp delete mode 100644 tests/unit/hykkt/runHykktPerm.cpp create mode 100644 tests/unit/hykkt/runHykktPermutationTests.cpp diff --git a/resolve/hykkt/CMakeLists.txt b/resolve/hykkt/CMakeLists.txt index fae6ca11a..abe1412f1 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 6bf98f586..000000000 --- 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 e5c48372c..000000000 --- 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 000000000..96f076bf9 --- /dev/null +++ b/resolve/hykkt/Permutation.cpp @@ -0,0 +1,283 @@ +/** + * @file Permutation.cpp + * @author Shaked Regev (regevs@ornl.gov) + * @brief Implementation of the Permutation class. + * + * + */ +#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[in] hes_i - Row offsets for H + * @param[in] 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[in] jac_i - Row offsets for J + * @param[in] jac_j - Column indices for j + * @param[in] n_jac - number of rows of J + * @param[in] 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[in] jac_tr_i - Row offsets for Jt + * @param[in] 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[in] 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[out] perm_i - row offsets of permutation + * @param[out] perm_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* perm_i, int* perm_j) + { + makeVecMapRC(n_hes_, hes_i_, hes_j_, perm_, rev_perm_, perm_i, perm_j, perm_map_hes_); + } + + /** + * @brief Creates the permutation of the columns of matrix J + * and copies onto device + * + * @param[out] perm_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* perm_j) + { + makeVecMapC(n_jac_, jac_i_, jac_j_, rev_perm_, perm_j, perm_map_jac_); + } + + /** + * @brief Creates the permutation of the rows of matrix Jt + * and copies onto device + * + * @param[out] perm_i - row offsets of permutation + * @param[out] perm_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* perm_i, int* perm_j) + { + makeVecMapR(m_jac_, jac_tr_i_, jac_tr_j_, perm_, perm_i, perm_j, perm_map_jac_tr_); + } + + /** + * @brief maps the permutated values of old_val to new_val + * + * @param[in] permutation - the type of permutation of the 2x2 system + * @param[in] old_val - the old values in the matrix + * @param[out] 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 000000000..bc565a3c2 --- /dev/null +++ b/resolve/hykkt/Permutation.hpp @@ -0,0 +1,89 @@ +#pragma once +/** + * @file Permutation.hpp + * @author Shaked Regev (regevs@ornl.gov) + * @brief Declaration of hykkt::Permutation class + * + */ +namespace ReSolve +{ + namespace hykkt + { + enum PermutationType { PERM_V, REV_PERM_V, PERM_HES_V, PERM_JAC_V, PERM_JAC_TR_V }; + + /** + * @class Permutation + * + * @brief Creates a permutation of the 2x2 system. + * + * This class creates a permutation of the 2x2 system, which is obtained + * by block-Gauss elimination in the 4x4 system. The permutation is + * based on the Symmetric Approximate Minimum Degree algorithm to minimize + * fill in for H and maps the permutation to the matrices H, J, and J + * transpose. + * + */ + 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* perm_i, int* perm_j); + void vecMapC(int* perm_j); + void vecMapR(int* perm_i, int* perm_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 2a8220985..000000000 --- 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 961716805..000000000 --- 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 000000000..d3ca087b2 --- /dev/null +++ b/resolve/hykkt/cpuPermutationKernels.cpp @@ -0,0 +1,316 @@ +/** + * @file cpuPermutationKernels.cpp + * @author Shaked Regev (regevs@ornl.gov) + * @brief Kernels for matrix and vector permutations + * + */ +#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[in] perm - desired permutation + * @param[in] old_val - the array to be permuted + * @param[out] 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[in,out] arr1 - the array that determines the sorting order, + * @param[in,out] 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[in,out] arr1 + * @param[in,out] arr2 + * @param[in] i + * @param[in] 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[in,out] arr1 + * @param[in,out] arr2 + * @param[in] low + * @param[in] 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[in,out] arr1 + * @param[in,out] arr2 + * @param[in] low + * @param[in] 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[in] n - Size of the matrix, + * @param[in,out] arr1 - the array that determines the sorting order, + * @param[in,out] 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[in] n + * @param[in] rows + * @param[in] cols + * @param[in] rev_perm + * @param[out] perm_cols + * @param[out] 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[in] n + * @param[in] perm + * @param[out] 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[in] n + * @param[in] rows + * @param[in] cols + * @param[in] perm + * @param[out] perm_rows + * @param[out] perm_cols + * @param[out] 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[in] n + * @param[in] rows + * @param[in] cols + * @param[in] perm + * @param[in] rev_perm + * @param[out] perm_rows + * @param[out] perm_cols + * @param[out] 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 diff --git a/resolve/hykkt/cpuPermutationKernels.hpp b/resolve/hykkt/cpuPermutationKernels.hpp new file mode 100644 index 000000000..49fff212b --- /dev/null +++ b/resolve/hykkt/cpuPermutationKernels.hpp @@ -0,0 +1,64 @@ +/** + * @file cpuPermutationKernels.hpp + * @author Shaked Regev (regevs@ornl.gov) + * @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/CMakeLists.txt b/tests/unit/hykkt/CMakeLists.txt index 90577c905..c80e89918 100644 --- a/tests/unit/hykkt/CMakeLists.txt +++ b/tests/unit/hykkt/CMakeLists.txt @@ -8,14 +8,14 @@ # Build HyKKT tests -add_executable(runHykktPermTests.exe runHykktPerm.cpp) -target_link_libraries(runHykktPermTests.exe PRIVATE resolve_hykkt) +add_executable(runHykktPermutationTests.exe runHykktPermutationTests.cpp) +target_link_libraries(runHykktPermutationTests.exe PRIVATE resolve_hykkt) # Install tests -set(installable_tests runHykktPermTests.exe) +set(installable_tests runHykktPermutationTests.exe) install(TARGETS ${installable_tests} RUNTIME DESTINATION bin/resolve/tests/unit) -add_test(NAME hykkt_perm_test COMMAND $) +add_test(NAME hykkt_perm_test COMMAND $) diff --git a/tests/unit/hykkt/HykktPerm.hpp b/tests/unit/hykkt/HykktPerm.hpp deleted file mode 100644 index 8d009cc52..000000000 --- a/tests/unit/hykkt/HykktPerm.hpp +++ /dev/null @@ -1,114 +0,0 @@ -#pragma once -#include -#include -#include -#include -#include -#include -#include -#include -namespace ReSolve { namespace tests { - -class HykktPerm : public TestBase -{ -public: - HykktPerm() {} - virtual ~HykktPerm() {} - - TestOutcome permutation() - { - int n = 4; - int m = 4; - int nnz = 9; - int a_i[5] = {0, 2, 5, 7, 9}; - int a_j[9] = {0, 2, 0, 1, 2, 1, 2, 1, 3}; - int a_prc_i[5] = {0, 2, 4, 6, 9}; - int a_prc_j[9] = {0, 3, 0, 1, 2, 3, 0, 1, 3}; - int a_pr_i[5] = {0, 2, 4, 6, 9}; - int a_pr_j[9] = {1, 2, 0, 2, 1, 3, 0, 1, 2}; - int a_pc_i[5] = {0, 2, 5, 7, 9}; - int a_pc_j[9] = {0, 1, 0, 1, 3, 0, 3, 2, 3}; - int b_i[5] = {0}; // Initialize row pointer - int b_j[9] = {0}; // Initialize column indices - int perm[4] = {2, 0, 3, 1}; - - bool flagrc = false; - bool flagr = false; - bool flagc = false; - - ReSolve::hykkt::PermClass pc(n, nnz, nnz); - pc.addHInfo(a_i, a_j); - pc.addJInfo(a_i, a_j, n, m); - pc.addJtInfo(a_i, a_j); - pc.addPerm(perm); - pc.invertPerm(); - - // Test RC permutation - pc.vecMapRC(b_i, b_j); - printf("Comparing RC permutation\n"); - for (int i = 0; i < n + 1; i++) // Loop over row pointers (n+1) - { - if (a_prc_i[i] != b_i[i]) - { - printf("Mismatch in row pointer %d\n", i); - flagrc = true; - } - } - for (int j = 0; j < nnz; j++) // Compare column indices - { - if (a_prc_j[j] != b_j[j]) - { - printf("Mismatch in column index %d\n", j); - flagrc = true; - } - } - printf(flagrc ? "RC permutation failed\n" : "RC permutation passed\n"); - - // Test R permutation - pc.vecMapR(b_i, b_j); - printf("Comparing R permutation\n"); - for (int i = 0; i < n + 1; i++) - { - if (a_pr_i[i] != b_i[i]) - { - printf("Mismatch in row pointer %d\n", i); - flagr = true; - } - } - for (int j = 0; j < nnz; j++) - { - if (a_pr_j[j] != b_j[j]) - { - printf("Mismatch in column index %d\n", j); - flagr = true; - } - } - printf(flagr ? "R permutation failed\n" : "R permutation passed\n"); - - // Test C permutation - pc.vecMapC(b_j); - printf("Comparing C permutation\n"); - for (int i = 0; i < n + 1; i++) - { - if (a_pc_i[i] != a_i[i]) // Row pointers should match - { - printf("Mismatch in row pointer %d\n", i); - flagc = true; - } - } - for (int j = 0; j < nnz; j++) - { - if (a_pc_j[j] != b_j[j]) - { - printf("Mismatch in column index %d\n", j); - flagc = true; - } - } - printf(flagc ? "C permutation failed\n" : "C permutation passed\n"); - - // Final Test Outcome - return (!flagrc && !flagr && !flagc) ? PASS : FAIL; - } -}; // class HykktPerm - -}} // namespace ReSolve::tests \ No newline at end of file diff --git a/tests/unit/hykkt/HykktPermutationTests.hpp b/tests/unit/hykkt/HykktPermutationTests.hpp new file mode 100644 index 000000000..00452a18e --- /dev/null +++ b/tests/unit/hykkt/HykktPermutationTests.hpp @@ -0,0 +1,130 @@ +/** + * @file HykktPermutationTests.hpp + * @author your name (you@domain.com) + * @brief + * + */ +#pragma once + +#include +#include +#include +#include +#include + +#include +#include +#include + +namespace ReSolve +{ + namespace tests + { + /** + * @brief Tests for class hykkt::Permutation + * + */ + class HykktPermutationTests : public TestBase + { + public: + HykktPermutationTests() {} + virtual ~HykktPermutationTests() {} + + TestOutcome permutation() + { + int n = 4; + int m = 4; + int nnz = 9; + int a_i[5] = {0, 2, 5, 7, 9}; + int a_j[9] = {0, 2, 0, 1, 2, 1, 2, 1, 3}; + int a_prc_i[5] = {0, 2, 4, 6, 9}; + int a_prc_j[9] = {0, 3, 0, 1, 2, 3, 0, 1, 3}; + int a_pr_i[5] = {0, 2, 4, 6, 9}; + int a_pr_j[9] = {1, 2, 0, 2, 1, 3, 0, 1, 2}; + int a_pc_i[5] = {0, 2, 5, 7, 9}; + int a_pc_j[9] = {0, 1, 0, 1, 3, 0, 3, 2, 3}; + int b_i[5] = {0}; // Initialize row pointer + int b_j[9] = {0}; // Initialize column indices + int perm[4] = {2, 0, 3, 1}; + + bool flagrc = false; + bool flagr = false; + bool flagc = false; + + 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); + pc.addPerm(perm); + pc.invertPerm(); + + // Test RC permutation + pc.vecMapRC(b_i, b_j); + printf("Comparing RC permutation\n"); + for (int i = 0; i < n + 1; i++) // Loop over row pointers (n+1) + { + if (a_prc_i[i] != b_i[i]) + { + printf("Mismatch in row pointer %d\n", i); + flagrc = true; + } + } + for (int j = 0; j < nnz; j++) // Compare column indices + { + if (a_prc_j[j] != b_j[j]) + { + printf("Mismatch in column index %d\n", j); + flagrc = true; + } + } + printf(flagrc ? "RC permutation failed\n" : "RC permutation passed\n"); + + // Test R permutation + pc.vecMapR(b_i, b_j); + printf("Comparing R permutation\n"); + for (int i = 0; i < n + 1; i++) + { + if (a_pr_i[i] != b_i[i]) + { + printf("Mismatch in row pointer %d\n", i); + flagr = true; + } + } + for (int j = 0; j < nnz; j++) + { + if (a_pr_j[j] != b_j[j]) + { + printf("Mismatch in column index %d\n", j); + flagr = true; + } + } + printf(flagr ? "R permutation failed\n" : "R permutation passed\n"); + + // Test C permutation + pc.vecMapC(b_j); + printf("Comparing C permutation\n"); + for (int i = 0; i < n + 1; i++) + { + if (a_pc_i[i] != a_i[i]) // Row pointers should match + { + printf("Mismatch in row pointer %d\n", i); + flagc = true; + } + } + for (int j = 0; j < nnz; j++) + { + if (a_pc_j[j] != b_j[j]) + { + printf("Mismatch in column index %d\n", j); + flagc = true; + } + } + printf(flagc ? "C permutation failed\n" : "C permutation passed\n"); + + // Final Test Outcome + return (!flagrc && !flagr && !flagc) ? PASS : FAIL; + } + }; // class HykktPermutationTests + + } // namespace tests +} // namespace ReSolve diff --git a/tests/unit/hykkt/runHykktPerm.cpp b/tests/unit/hykkt/runHykktPerm.cpp deleted file mode 100644 index b2c3a3270..000000000 --- a/tests/unit/hykkt/runHykktPerm.cpp +++ /dev/null @@ -1,13 +0,0 @@ -#include -#include -#include -#include "tests/unit/hykkt/HykktPerm.hpp" - -int main(int, char**) -{ - ReSolve::tests::HykktPerm test; - - ReSolve::tests::TestingResults result; - result += test.permutation(); - return result.summary(); -} \ No newline at end of file diff --git a/tests/unit/hykkt/runHykktPermutationTests.cpp b/tests/unit/hykkt/runHykktPermutationTests.cpp new file mode 100644 index 000000000..c392f9f5b --- /dev/null +++ b/tests/unit/hykkt/runHykktPermutationTests.cpp @@ -0,0 +1,20 @@ +/** + * @file runHykktPermutationTests.cpp + * @author your name (you@domain.com) + * @brief + * + */ +#include +#include +#include + +#include "tests/unit/hykkt/HykktPermutationTests.hpp" + +int main(int, char**) +{ + ReSolve::tests::HykktPermutationTests test; + + ReSolve::tests::TestingResults result; + result += test.permutation(); + return result.summary(); +} From 2522866852b9ec2e54646d2692d9d1ab95caefe3 Mon Sep 17 00:00:00 2001 From: shakedregev Date: Tue, 14 Jan 2025 22:14:15 +0000 Subject: [PATCH 29/30] added requested changes --- resolve/hykkt/Permutation.cpp | 12 +-- resolve/hykkt/cpuPermutationKernels.cpp | 94 ++++++++++--------- resolve/hykkt/cpuPermutationKernels.hpp | 21 ----- tests/unit/hykkt/HykktPermutationTests.hpp | 4 +- tests/unit/hykkt/runHykktPermutationTests.cpp | 6 +- .../unit/matrix/runMatrixPermutationTests.cpp | 24 ----- 6 files changed, 58 insertions(+), 103 deletions(-) delete mode 100644 tests/unit/matrix/runMatrixPermutationTests.cpp diff --git a/resolve/hykkt/Permutation.cpp b/resolve/hykkt/Permutation.cpp index 96f076bf9..aaf4576a8 100644 --- a/resolve/hykkt/Permutation.cpp +++ b/resolve/hykkt/Permutation.cpp @@ -28,13 +28,7 @@ namespace ReSolve /// 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_; + deleteWorkspace(); } /** @@ -253,7 +247,9 @@ namespace ReSolve */ void Permutation::deleteWorkspace() { - delete [] perm_; + if(perm_is_default_){ + delete [] perm_; + } delete [] rev_perm_; delete [] perm_map_hes_; delete [] perm_map_jac_; diff --git a/resolve/hykkt/cpuPermutationKernels.cpp b/resolve/hykkt/cpuPermutationKernels.cpp index d3ca087b2..c8f99ade7 100644 --- a/resolve/hykkt/cpuPermutationKernels.cpp +++ b/resolve/hykkt/cpuPermutationKernels.cpp @@ -33,6 +33,10 @@ namespace ReSolve /** * @brief Selection sorts arr1 and arr2 w/indices * + * The complexity of selection sort is O(n^2) in all cases. + * In the future the user will be given the option to choose between + * selection sort, insertion sort, and quicksort. + * * @param[in] len - Size n of the matrix, * @param[in,out] arr1 - the array that determines the sorting order, * @param[in,out] arr2 - sorted based on arr1 @@ -64,7 +68,7 @@ namespace ReSolve } /** - * @brief + * @brief swaps arr1[i] with arr1[j] and arr2[i] with arr2[j] * * @param[in,out] arr1 * @param[in,out] arr2 @@ -83,7 +87,7 @@ namespace ReSolve } /** - * @brief + * @brief helper function for quicksort * * @param[in,out] arr1 * @param[in,out] arr2 @@ -106,7 +110,14 @@ namespace ReSolve } /** - * @brief + * @brief quicksorts arr1 and arr2 between indices low and high + * + * The complexity of quicksort is O(n log n) in the average case, + * but O(n^2) in the worst case. For our test cases, n is small, + * so quicksort is not a good choice, therefore we use insertion sort. + * In the future the user will be given the option to choose between + * selection sort, insertion sort, and quicksort. + * * * @param[in,out] arr1 * @param[in,out] arr2 @@ -126,6 +137,11 @@ namespace ReSolve * @brief Insertion sorts arr1 and arr2 w/indices * based on increasing value in arr1 * + * The complexity of insertion sort is O(n^2) in the worst case. + * It is chosen here because it is simple and efficient for small n. + * In the future the user will be given the option to choose between + * selection sort, insertion sort, and quicksort. + * * @param[in] n - Size of the matrix, * @param[in,out] arr1 - the array that determines the sorting order, * @param[in,out] arr2 - sorted based on arr1 @@ -156,12 +172,12 @@ namespace ReSolve /** * @brief Permutes the columns in a matrix represented by rows and cols * - * @param[in] n - * @param[in] rows - * @param[in] cols - * @param[in] rev_perm - * @param[out] perm_cols - * @param[out] perm_map + * @param[in] n - size of the matrix + * @param[in] rows - row offsets of matrix + * @param[in] cols - column indices of matrix + * @param[in] rev_perm - reverse permutation + * @param[out] perm_cols - permuted column array + * @param[out] perm_map - corresponding indices to facilitate permuting the values * * @pre rev_perm has integers 0 to n-1 (permuted), * rows and cols present valid csr storage array @@ -185,22 +201,16 @@ namespace ReSolve 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[in] n - * @param[in] perm - * @param[out] rev_perm + * @param[in] n - size of the permutation + * @param[in] perm - permutation array + * @param[out] rev_perm - reversed permutation array * * @pre perm has integers 0 to n-1 (permuted), * @@ -216,13 +226,13 @@ namespace ReSolve /** * @brief Permutes the rows in a matrix represented by rows and cols * - * @param[in] n - * @param[in] rows - * @param[in] cols - * @param[in] perm - * @param[out] perm_rows - * @param[out] perm_cols - * @param[out] perm_map + * @param[in] n - size of the matrix + * @param[in] rows - row offsets of matrix + * @param[in] cols - column indices of matrix + * @param[in] perm - permutation array + * @param[out] perm_rows - row offsets of permuted matrix + * @param[out] perm_cols - column indices of permuted matrix + * @param[out] perm_map - corresponding indices to facilitate permuting the values * * @pre perm has integers 0 to n-1 (permuted), * rows and cols present valid csr storage array @@ -231,12 +241,12 @@ namespace ReSolve * 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) + 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; @@ -261,14 +271,14 @@ namespace ReSolve * @brief Permutes the rows and columns in a matrix represented by rows * and cols * - * @param[in] n - * @param[in] rows - * @param[in] cols - * @param[in] perm - * @param[in] rev_perm - * @param[out] perm_rows - * @param[out] perm_cols - * @param[out] perm_map + * @param[in] n - size of the matrix + * @param[in] rows - row offsets of matrix + * @param[in] cols - column indices of matrix + * @param[in] perm - permutation array + * @param[in] rev_perm - reverse permutation array + * @param[out] perm_rows - row offsets of permuted matrix + * @param[out] perm_cols - column indices of permuted matrix + * @param[out] perm_map - corresponding indices to facilitate permuting the values * * @pre perm and rev_perm have corresponding integers 0 to n-1 (permuted), * rows and cols present valid csr storage array @@ -302,13 +312,7 @@ namespace ReSolve 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; } } diff --git a/resolve/hykkt/cpuPermutationKernels.hpp b/resolve/hykkt/cpuPermutationKernels.hpp index 49fff212b..bd347aa23 100644 --- a/resolve/hykkt/cpuPermutationKernels.hpp +++ b/resolve/hykkt/cpuPermutationKernels.hpp @@ -23,20 +23,7 @@ namespace ReSolve 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, @@ -44,14 +31,6 @@ namespace ReSolve 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, diff --git a/tests/unit/hykkt/HykktPermutationTests.hpp b/tests/unit/hykkt/HykktPermutationTests.hpp index 00452a18e..2f7e01c81 100644 --- a/tests/unit/hykkt/HykktPermutationTests.hpp +++ b/tests/unit/hykkt/HykktPermutationTests.hpp @@ -1,7 +1,7 @@ /** * @file HykktPermutationTests.hpp - * @author your name (you@domain.com) - * @brief + * @author Shaked Regev (regevs@ornl.gov) + * @brief Implementation of tests for class hykkt::Permutation * */ #pragma once diff --git a/tests/unit/hykkt/runHykktPermutationTests.cpp b/tests/unit/hykkt/runHykktPermutationTests.cpp index c392f9f5b..20cccbe64 100644 --- a/tests/unit/hykkt/runHykktPermutationTests.cpp +++ b/tests/unit/hykkt/runHykktPermutationTests.cpp @@ -1,7 +1,7 @@ /** - * @file runHykktPermutationTests.cpp - * @author your name (you@domain.com) - * @brief + * @file HykktPermutationTests.hpp + * @author Shaked Regev (regevs@ornl.gov) + * @brief Tests for class hykkt::Permutation * */ #include diff --git a/tests/unit/matrix/runMatrixPermutationTests.cpp b/tests/unit/matrix/runMatrixPermutationTests.cpp deleted file mode 100644 index 485deff04..000000000 --- a/tests/unit/matrix/runMatrixPermutationTests.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include "MatrixIoTests.hpp" - -int main(int, char**) -{ - ReSolve::tests::MatrixIoTests test; - - ReSolve::tests::TestingResults result; - result += test.cooMatrixImport(); - result += test.csrMatrixImport(); - result += test.cooMatrixExport(); - result += test.csrMatrixExport(); - result += test.cooMatrixReadAndUpdate(); - result += test.csrMatrixReadAndUpdate(); - result += test.rhsVectorReadFromFile(); - result += test.rhsVectorReadAndUpdate(); - - return result.summary(); -} \ No newline at end of file From ea52fbcabb982b2e3c1fab94bc13532891109fab Mon Sep 17 00:00:00 2001 From: shakedregev Date: Wed, 15 Jan 2025 15:59:09 +0000 Subject: [PATCH 30/30] addressed Doxygen comment issues --- resolve/hykkt/cpuPermutationKernels.cpp | 26 ++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/resolve/hykkt/cpuPermutationKernels.cpp b/resolve/hykkt/cpuPermutationKernels.cpp index c8f99ade7..33a024d3c 100644 --- a/resolve/hykkt/cpuPermutationKernels.cpp +++ b/resolve/hykkt/cpuPermutationKernels.cpp @@ -70,10 +70,10 @@ namespace ReSolve /** * @brief swaps arr1[i] with arr1[j] and arr2[i] with arr2[j] * - * @param[in,out] arr1 - * @param[in,out] arr2 - * @param[in] i - * @param[in] j + * @param[in,out] arr1 - first array to have values swapped + * @param[in,out] arr2 - second array to have values swapped + * @param[in] i - index of first value to be swapped + * @param[in] j - index of second value to be swapped */ inline void swap(int* arr1, int* arr2, int i, int j) { @@ -89,11 +89,11 @@ namespace ReSolve /** * @brief helper function for quicksort * - * @param[in,out] arr1 - * @param[in,out] arr2 - * @param[in] low - * @param[in] high - * @return int + * @param[in,out] arr1 - array to be sorted based on itself + * @param[in,out] arr2 - array to be sorted based on other array + * @param[in] low - lower index bound of array slice + * @param[in] high - higher index bound of array slice + * @return int - index of the pivot */ inline int partition(int* arr1, int* arr2, int low, int high) { @@ -119,10 +119,10 @@ namespace ReSolve * selection sort, insertion sort, and quicksort. * * - * @param[in,out] arr1 - * @param[in,out] arr2 - * @param[in] low - * @param[in] high + * @param[in,out] arr1 - input array to be sorted + * @param[in,out] arr2 - array to be sorted based on other array + * @param[in] low - lower index bound of array slice + * @param[in] high - higher index bound of array slice */ void quickSort(int* arr1, int* arr2, int low, int high) {