Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new permutation matrix format #352

Merged
merged 14 commits into from
Nov 13, 2019
Merged

Add a new permutation matrix format #352

merged 14 commits into from
Nov 13, 2019

Conversation

pratikvn
Copy link
Member

@pratikvn pratikvn commented Sep 17, 2019

This PR adds a new permutation matrix format, in preparation for the Reordering methods.

Additionally, it also adds a Permutable interface, which allows for LinOps to implement the row_permute, column_permute ,inverse_row_permute and inverse_column_permute functions. The apply method of the permutation matrix format, should automatically call these functions and there should be no need for the user to call these functions.

The Permutation format, holds two permutation arrays, the row_permutation and the col_permutation. It implements methods from EnableLinOp and hence will provide the apply methods. The apply methods will permute the given LinOp by calling the respective LinOp's Xrow_permute/Xcol_permute functions if the LinOp has a Permutable interface.

Along with the Permutable interface, permuting a matrix should look something like this:

using Perm = gko::matrix::Permutation<>;
using Csr = gko::matrix::Csr<>;
using Dense = gko::matrix::Dense<>;

// See the constructors of Permutation for available options
auto p = Perm::create(exec, perm_array);

auto csr_mat=Csr::create(exec, <row_ptrs,  vals ...>);
auto dense_mat=Dense::create(exec, <vals ...>);

auto perm_csr = Csr::create(exec, csr_mat->get_size(), csr_mat->get_num_stored_elements());
auto perm_dense = Dense::create(exec, dense->get_size());

// Permutes the CSR matrix.
p->apply(csr_mat, perm_csr);

// Permutes the dense matrix
p->apply(dense_mat, perm_dense);

TODO

@pratikvn pratikvn self-assigned this Sep 17, 2019
@pratikvn pratikvn added mod:core This is related to the core module. 1:ST:do-not-merge Please do not merge PR this yet. type:matrix-format This is related to the Matrix formats is:new-feature A request or implementation of a feature that does not exist yet. mod:openmp This is related to the OpenMP module. 1:ST:ready-for-review This PR is ready for review mod:reference This is related to the reference module. labels Sep 17, 2019
@codecov
Copy link

codecov bot commented Sep 17, 2019

Codecov Report

❗ No coverage uploaded for pull request base (develop@45a800a). Click here to learn what that means.
The diff coverage is 96.17%.

Impacted file tree graph

@@            Coverage Diff             @@
##             develop     #352   +/-   ##
==========================================
  Coverage           ?   89.45%           
==========================================
  Files              ?      289           
  Lines              ?    22588           
  Branches           ?        0           
==========================================
  Hits               ?    20205           
  Misses             ?     2383           
  Partials           ?        0
Impacted Files Coverage Δ
include/ginkgo/core/matrix/dense.hpp 100% <ø> (ø)
include/ginkgo/core/base/exception_helpers.hpp 100% <ø> (ø)
core/matrix/sparsity_csr.cpp 97.18% <ø> (ø)
include/ginkgo/core/matrix/csr.hpp 85.48% <ø> (ø)
reference/matrix/csr_kernels.cpp 100% <100%> (ø)
include/ginkgo/core/base/lin_op.hpp 91.48% <100%> (ø)
core/matrix/csr.cpp 99.43% <100%> (ø)
reference/test/matrix/dense_kernels.cpp 100% <100%> (ø)
core/test/matrix/permutation.cpp 100% <100%> (ø)
omp/matrix/csr_kernels.cpp 97.05% <100%> (ø)
... and 8 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 45a800a...85c98d8. Read the comment docs.

@pratikvn pratikvn force-pushed the permute-mat-format branch 7 times, most recently from 3920e0a to 4ffff0d Compare September 25, 2019 21:25
@pratikvn pratikvn force-pushed the permute-mat-format branch 2 times, most recently from 8e206bc to 244cd08 Compare September 27, 2019 09:34
@tcojean
Copy link
Member

tcojean commented Sep 27, 2019

@pratikvn @thoasm I updated the CUDA docker images to include libthrust-dev, which is required for HIP builds. Could that have messed up the ParILU tests? I don't think the test is complex related but who knows. That's the only thing I can think of right now from my part.

Another thing is, since this code will anyway go away with #353, we may as well not care about it until #353 is merged and everything is rebased on top of it. Debugging dead code is not very useful.

@thoasm
Copy link
Member

thoasm commented Sep 27, 2019

@tcojean I think it is possible that the problem is similar to what @yhmtsai had on Windows: In the old ILU preconditioner test, I did not set any initial value for the X vector or the cache vector, so it is possible that it reuses parts of previously allocated memory. That could result in wrong computations.
That part is fixed as part of the PR #353, so if that was the problem, rebasing should resolve it (after it is merged in master ofc).

@pratikvn
Copy link
Member Author

Yes, I will leave it alone until #353 is merged and then rebase.

@pratikvn pratikvn force-pushed the permute-mat-format branch 3 times, most recently from fd3c18f to a1d5ec1 Compare September 30, 2019 13:38
@pratikvn pratikvn mentioned this pull request Sep 30, 2019
1 task
@pratikvn pratikvn force-pushed the permute-mat-format branch 2 times, most recently from 2c7b757 to 7dc2d8c Compare October 1, 2019 08:57
@pratikvn pratikvn removed the 1:ST:do-not-merge Please do not merge PR this yet. label Oct 9, 2019
@tcojean
Copy link
Member

tcojean commented Nov 13, 2019

@pratikvn Nice. As suggestion improvement, you could consider whether it is worth making it similar to precision_reduction , i.e. to turn this into a class eventually with functionalities for easy testing such as is_row_permutation and similar.

class precision_reduction {
public:
/**
* The underlying datatype used to store the encoding.
*/
using storage_type = uint8;
private:
static constexpr auto nonpreserving_bits = 4u;
static constexpr auto preserving_bits =
byte_size * sizeof(storage_type) - nonpreserving_bits;
static constexpr auto nonpreserving_mask =
storage_type{(0x1 << nonpreserving_bits) - 1};
static constexpr auto preserving_mask =
storage_type{(0x1 << preserving_bits) - 1} << nonpreserving_bits;
public:
/**
* Creates a default precision_reduction encoding.
*
* This encoding represents the case where no conversions are performed.
*/
GKO_ATTRIBUTES constexpr precision_reduction() noexcept : data_{0x0} {}
/**
* Creates a precision_reduction encoding with the specified number of
* conversions.
*
* @param preserving the number of range preserving conversion
* @param nonpreserving the number of range non-preserving conversions
*/
GKO_ATTRIBUTES constexpr precision_reduction(
storage_type preserving, storage_type nonpreserving) noexcept
: data_((GKO_ASSERT(preserving < (0x1 << preserving_bits) - 1),
GKO_ASSERT(nonpreserving < (0x1 << nonpreserving_bits) - 1),
(preserving << nonpreserving_bits) | nonpreserving))
{}
/**
* Extracts the raw data of the encoding.
*
* @return the raw data of the encoding
*/
GKO_ATTRIBUTES constexpr operator storage_type() const noexcept
{
return data_;
}
/**
* Returns the number of preserving conversions in the encoding.
*
* @return the number of preserving conversions in the encoding.
*/
GKO_ATTRIBUTES constexpr storage_type get_preserving() const noexcept
{
return (data_ & preserving_mask) >> nonpreserving_bits;
}
/**
* Returns the number of non-preserving conversions in the encoding.
*
* @return the number of non-preserving conversions in the encoding.
*/
GKO_ATTRIBUTES constexpr storage_type get_nonpreserving() const noexcept
{
return data_ & nonpreserving_mask;
}
/**
* Returns a special encoding which instructs the algorithm to automatically
* detect the best precision.
*
* @return a special encoding instructing the algorithm to automatically
* detect the best precision.
*/
GKO_ATTRIBUTES constexpr static precision_reduction autodetect() noexcept
{
return precision_reduction{preserving_mask | nonpreserving_mask};
}
/**
* Returns the common encoding of input encodings.
*
* The common encoding is defined as the encoding that does not have more
* preserving, nor non-preserving conversions than the input encodings.
*
* @param x an encoding
* @param y an encoding
*
* @return the common encoding of `x` and `y`
*/
GKO_ATTRIBUTES constexpr static precision_reduction common(
precision_reduction x, precision_reduction y) noexcept
{
return precision_reduction(
min(x.data_ & preserving_mask, y.data_ & preserving_mask) |
min(x.data_ & nonpreserving_mask, y.data_ & nonpreserving_mask));
}
private:
GKO_ATTRIBUTES constexpr precision_reduction(storage_type data)
: data_{data}
{}
GKO_ATTRIBUTES constexpr static storage_type min(storage_type x,
storage_type y) noexcept
{
return x < y ? x : y;
}
storage_type data_;
};

+ Improve the linear scan as suggested by Tobias.
omp/matrix/csr_kernels.cpp Outdated Show resolved Hide resolved
omp/matrix/csr_kernels.cpp Outdated Show resolved Hide resolved
omp/matrix/csr_kernels.cpp Outdated Show resolved Hide resolved
omp/matrix/csr_kernels.cpp Outdated Show resolved Hide resolved
omp/matrix/csr_kernels.cpp Outdated Show resolved Hide resolved
@pratikvn
Copy link
Member Author

pratikvn commented Nov 13, 2019

The design looks mostly good to me, a few comments:

1. For column_permute and inverse_row_permute in CSR, the implementation currently uses a linear scan to effectively determine an entry of the inverse permutation for each row/non-zero. We could speed this up considerably by computing the inverse permutation explicitly for the calls or even storing it in the permutation class. This would not make a difference for Dense and only a slight difference for COO (i.e., sorting after permutation), but benefit CSR etc. This would also remove a bit of code duplication from the CSR permute code.

2. Concerning Terry's comment w.r.t the interface, if we look at it from the view of a matrix, we could limit the choices to two options: `inverse_permute` determines if we multiply with `P` or `P*` (transposed), while `row_permute` and `column_permute` describe a multiplication from the left or right. Can you clarify why we need two separate flags for these operations?

3. Just out of curiosity: Do we plan to be able to combine (i.e., multiply) permutation matrices?

@upsj ,

  1. Thanks, that is indeed better.

  2. row_permute can be seen as P A whereas the column_permute can be seen as A P, so I added two different options.

  3. I guess it would be nice to have this as well. But I think as the Permutation class here does not strictly represent a matrix, I think it might be a bit difficult to group two permutations into one.

omp/matrix/csr_kernels.cpp Outdated Show resolved Hide resolved
omp/matrix/csr_kernels.cpp Outdated Show resolved Hide resolved
Copy link
Member

@upsj upsj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! You could remove some duplicate code as the permute and inverse_permute now only differ in whether they use perm[...] or inv_pindx[...].

omp/matrix/csr_kernels.cpp Outdated Show resolved Hide resolved
omp/matrix/csr_kernels.cpp Outdated Show resolved Hide resolved
reference/matrix/csr_kernels.cpp Outdated Show resolved Hide resolved
reference/matrix/csr_kernels.cpp Outdated Show resolved Hide resolved
Co-Authored-By: Tobias Ribizel <ribizel@kit.edu>
@tcojean tcojean added 1:ST:ready-to-merge This PR is ready to merge. and removed 1:ST:ready-for-review This PR is ready for review labels Nov 13, 2019
@pratikvn pratikvn merged commit f9e591c into develop Nov 13, 2019
@pratikvn pratikvn deleted the permute-mat-format branch November 17, 2019 10:01
@tcojean tcojean mentioned this pull request Jun 23, 2020
tcojean added a commit that referenced this pull request Jul 7, 2020
The Ginkgo team is proud to announce the new minor release of Ginkgo version
1.2.0. This release brings full HIP support to Ginkgo, new preconditioners
(ParILUT, ISAI), conversion between double and float for all LinOps, and many
more features and fixes.

Supported systems and requirements:
+ For all platforms, cmake 3.9+
+ Linux and MacOS
  + gcc: 5.3+, 6.3+, 7.3+, all versions after 8.1+
  + clang: 3.9+
  + Intel compiler: 2017+
  + Apple LLVM: 8.0+
  + CUDA module: CUDA 9.0+
  + HIP module: ROCm 2.8+
+ Windows
  + MinGW and CygWin: gcc 5.3+, 6.3+, 7.3+, all versions after 8.1+
  + Microsoft Visual Studio: VS 2017 15.7+
  + CUDA module: CUDA 9.0+, Microsoft Visual Studio
  + OpenMP module: MinGW or CygWin.


The current known issues can be found in the [known issues page](https://github.com/ginkgo-project/ginkgo/wiki/Known-Issues).


# Additions
Here are the main additions to the Ginkgo library. Other thematic additions are listed below.
+ Add full HIP support to Ginkgo [#344](#344), [#357](#357), [#384](#384), [#373](#373), [#391](#391), [#396](#396), [#395](#395), [#393](#393), [#404](#404), [#439](#439), [#443](#443), [#567](#567)
+ Add a new ISAI preconditioner [#489](#489), [#502](#502), [#512](#512), [#508](#508), [#520](#520)
+ Add support for ParILUT and ParICT factorization with ILU preconditioners [#400](#400)
+ Add a new BiCG solver [#438](#438)
+ Add a new permutation matrix format [#352](#352), [#469](#469)
+ Add CSR SpGEMM support [#386](#386), [#398](#398), [#418](#418), [#457](#457)
+ Add CSR SpGEAM support [#556](#556)
+ Make all solvers and preconditioners transposable [#535](#535)
+ Add CsrBuilder and CooBuilder for intrusive access to matrix arrays [#437](#437)
+ Add a standard-compliant allocator based on the Executors [#504](#504)
+ Support conversions for all LinOp between double and float [#521](#521)
+ Add a new boolean to the CUDA and HIP executors to control DeviceReset (default off) [#557](#557)
+ Add a relaxation factor to IR to represent Richardson Relaxation [#574](#574)
+ Add two new stopping criteria, for relative (to `norm(b)`) and absolute residual norm [#577](#577)

### Example additions
+ Templatize all examples to simplify changing the precision [#513](#513)
+ Add a new adaptive precision block-Jacobi example [#507](#507)
+ Add a new IR example [#522](#522)
+ Add a new Mixed Precision Iterative Refinement example [#525](#525)
+ Add a new example on iterative trisolves in ILU preconditioning [#526](#526), [#536](#536), [#550](#550)

### Compilation and library changes
+ Auto-detect compilation settings based on environment [#435](#435), [#537](#537)
+ Add SONAME to shared libraries [#524](#524)
+ Add clang-cuda support [#543](#543)

### Other additions
+ Add sorting, searching and merging kernels for GPUs [#403](#403), [#428](#428), [#417](#417), [#455](#455)
+ Add `gko::as` support for smart pointers [#493](#493)
+ Add setters and getters for criterion factories [#527](#527)
+ Add a new method to check whether a solver uses `x` as an initial guess [#531](#531)
+ Add contribution guidelines [#549](#549)

# Fixes
### Algorithms
+ Improve the classical CSR strategy's performance [#401](#401)
+ Improve the CSR automatical strategy [#407](#407), [#559](#559)
+ Memory, speed improvements to the ELL kernel [#411](#411)
+ Multiple improvements and fixes to ParILU [#419](#419), [#427](#427), [#429](#429), [#456](#456), [#544](#544)
+ Fix multiple issues with GMRES [#481](#481), [#523](#523), [#575](#575)
+ Optimize OpenMP matrix conversions [#505](#505)
+ Ensure the linearity of the ILU preconditioner [#506](#506)
+ Fix IR's use of the advanced apply [#522](#522)
+ Fix empty matrices conversions and add tests [#560](#560)

### Other core functionalities
+ Fix complex number support in our math header [#410](#410)
+ Fix CUDA compatibility of the main ginkgo header [#450](#450)
+ Fix isfinite issues [#465](#465)
+ Fix the Array::view memory leak and the array/view copy/move [#485](#485)
+ Fix typos preventing use of some interface functions [#496](#496)
+ Fix the `gko::dim` to abide to the C++ standard [#498](#498)
+ Simplify the executor copy interface [#516](#516)
+ Optimize intermediate storage for Composition [#540](#540)
+ Provide an initial guess for relevant Compositions [#561](#561)
+ Better management of nullptr as criterion [#562](#562)
+ Fix the norm calculations for complex support [#564](#564)

### CUDA and HIP specific
+ Use the return value of the atomic operations in our wrappers [#405](#405)
+ Improve the portability of warp lane masks [#422](#422)
+ Extract thread ID computation into a separate function [#464](#464)
+ Reorder kernel parameters for consistency [#474](#474)
+ Fix the use of `pragma unroll` in HIP [#492](#492)

### Other
+ Fix the Ginkgo CMake installation files [#414](#414), [#553](#553)
+ Fix the Windows compilation [#415](#415)
+ Always use demangled types in error messages [#434](#434), [#486](#486)
+ Add CUDA header dependency to appropriate tests [#452](#452)
+ Fix several sonarqube or compilation warnings [#453](#453), [#463](#463), [#532](#532), [#569](#569)
+ Add shuffle tests [#460](#460)
+ Fix MSVC C2398 error [#490](#490)
+ Fix missing interface tests in test install [#558](#558)

# Tools and ecosystem
### Benchmarks
+ Add better norm support in the benchmarks [#377](#377)
+ Add CUDA 10.1 generic SpMV support in benchmarks [#468](#468), [#473](#473)
+ Add sparse library ILU in benchmarks [#487](#487)
+ Add overhead benchmarking capacities [#501](#501)
+ Allow benchmarking from a matrix list file [#503](#503)
+ Fix benchmarking issue with JSON and non-finite numbers [#514](#514)
+ Fix benchmark logger crashers with OpenMP [#565](#565)

### CI related
+ Improvements to the CI setup with HIP compilation [#421](#421), [#466](#466)
+ Add MacOSX CI support [#470](#470), [#488](#488)
+ Add Windows CI support [#471](#471), [#488](#488), [#510](#510), [#566](#566)
+ Use sanitizers instead of valgrind [#476](#476)
+ Add automatic container generation and update facilities [#499](#499)
+ Fix the CI parallelism settings [#517](#517), [#538](#538), [#539](#539)
+ Make the codecov patch check informational [#519](#519)
+ Add support for LLVM sanitizers with improved thread sanitizer support [#578](#578)

### Test suite
+ Add an assertion for sparsity pattern equality [#416](#416)
+ Add core and reference multiprecision tests support [#448](#448)
+ Speed up GPU tests by avoiding device reset [#467](#467)
+ Change test matrix location string [#494](#494)

### Other
+ Add Ginkgo badges from our tools [#413](#413)
+ Update the `create_new_algorithm.sh` script [#420](#420)
+ Bump copyright and improve license management [#436](#436), [#433](#433)
+ Set clang-format minimum requirement [#441](#441), [#484](#484)
+ Update git-cmake-format [#446](#446), [#484](#484)
+ Disable the development tools by default [#442](#442)
+ Add a script for automatic header formatting [#447](#447)
+ Add GDB pretty printer for `gko::Array` [#509](#509)
+ Improve compilation speed [#533](#533)
+ Add editorconfig support [#546](#546)
+ Add a compile-time check for header self-sufficiency [#552](#552)


# Related PR: #583
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1:ST:ready-to-merge This PR is ready to merge. is:new-feature A request or implementation of a feature that does not exist yet. mod:core This is related to the core module. mod:openmp This is related to the OpenMP module. mod:reference This is related to the reference module. type:matrix-format This is related to the Matrix formats
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants