Skip to content

Commit

Permalink
add the infinite_norm
Browse files Browse the repository at this point in the history
  • Loading branch information
yhmtsai committed Jul 10, 2024
1 parent e57d680 commit fef8ff3
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 1 deletion.
3 changes: 3 additions & 0 deletions common/unified/matrix/dense_kernels.instantiate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ GKO_INSTANTIATE_FOR_EACH_VALUE_TYPE(GKO_DECLARE_DENSE_COMPUTE_NORM2_KERNEL);
// split
GKO_INSTANTIATE_FOR_EACH_VALUE_TYPE(GKO_DECLARE_DENSE_COMPUTE_NORM1_KERNEL);
// split
GKO_INSTANTIATE_FOR_EACH_VALUE_TYPE(
GKO_DECLARE_DENSE_COMPUTE_INFINITE_NORM_KERNEL);
// split
GKO_INSTANTIATE_FOR_EACH_VALUE_TYPE(
GKO_DECLARE_DENSE_COMPUTE_MAX_NNZ_PER_ROW_KERNEL);
// split
Expand Down
13 changes: 13 additions & 0 deletions common/unified/matrix/dense_kernels.template.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,19 @@ void compute_norm1(std::shared_ptr<const DefaultExecutor> exec,
}


template <typename ValueType>
void compute_infinite_norm(std::shared_ptr<const DefaultExecutor> exec,
const matrix::Dense<ValueType>* x,
matrix::Dense<remove_complex<ValueType>>* result,
array<char>& tmp)
{
run_kernel_col_reduction_cached(
exec, [] GKO_KERNEL(auto i, auto j, auto x) { return abs(x(i, j)); },
GKO_KERNEL_REDUCE_MAX(remove_complex<ValueType>), result->get_values(),
x->get_size(), tmp, x);
}


template <typename ValueType>
void compute_mean(std::shared_ptr<const DefaultExecutor> exec,
const matrix::Dense<ValueType>* x,
Expand Down
1 change: 1 addition & 0 deletions core/device_hooks/common_kernels.inc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ GKO_STUB_VALUE_TYPE(GKO_DECLARE_DENSE_COMPUTE_CONJ_DOT_KERNEL);
GKO_STUB_VALUE_TYPE(GKO_DECLARE_DENSE_COMPUTE_CONJ_DOT_DISPATCH_KERNEL);
GKO_STUB_VALUE_TYPE(GKO_DECLARE_DENSE_COMPUTE_NORM2_KERNEL);
GKO_STUB_VALUE_TYPE(GKO_DECLARE_DENSE_COMPUTE_NORM2_DISPATCH_KERNEL);
GKO_STUB_VALUE_TYPE(GKO_DECLARE_DENSE_COMPUTE_INFINITE_NORM_KERNEL);
GKO_STUB_VALUE_TYPE(GKO_DECLARE_DENSE_COMPUTE_NORM1_KERNEL);
GKO_STUB_VALUE_TYPE(GKO_DECLARE_DENSE_COMPUTE_MEAN_KERNEL);
GKO_STUB_VALUE_TYPE(GKO_DECLARE_DENSE_COMPUTE_SQUARED_NORM2_KERNEL);
Expand Down
30 changes: 30 additions & 0 deletions core/matrix/dense.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ GKO_REGISTER_OPERATION(sub_scaled_diag, dense::sub_scaled_diag);
GKO_REGISTER_OPERATION(compute_dot, dense::compute_dot_dispatch);
GKO_REGISTER_OPERATION(compute_conj_dot, dense::compute_conj_dot_dispatch);
GKO_REGISTER_OPERATION(compute_norm2, dense::compute_norm2_dispatch);
GKO_REGISTER_OPERATION(compute_infinite_norm, dense::compute_infinite_norm);
GKO_REGISTER_OPERATION(compute_norm1, dense::compute_norm1);
GKO_REGISTER_OPERATION(compute_mean, dense::compute_mean);
GKO_REGISTER_OPERATION(compute_squared_norm2, dense::compute_squared_norm2);
Expand Down Expand Up @@ -204,6 +205,18 @@ void Dense<ValueType>::compute_norm2(ptr_param<LinOp> result) const
}


template <typename ValueType>
void Dense<ValueType>::compute_infinite_norm(ptr_param<LinOp> result) const
{
GKO_ASSERT_EQUAL_DIMENSIONS(result, dim<2>(1, this->get_size()[1]));
auto exec = this->get_executor();
auto dense_res =
make_temporary_conversion<remove_complex<ValueType>>(result);
array<char> tmp{exec};
exec->run(dense::make_compute_infinite_norm(this, dense_res.get(), tmp));
}


template <typename ValueType>
void Dense<ValueType>::compute_norm1(ptr_param<LinOp> result) const
{
Expand Down Expand Up @@ -424,6 +437,23 @@ void Dense<ValueType>::compute_norm2(ptr_param<LinOp> result,
}


template <typename ValueType>
void Dense<ValueType>::compute_infinite_norm(ptr_param<LinOp> result,
array<char>& tmp) const
{
GKO_ASSERT_EQUAL_DIMENSIONS(result, dim<2>(1, this->get_size()[1]));
auto exec = this->get_executor();
if (tmp.get_executor() != exec) {
tmp.clear();
tmp.set_executor(exec);
}
auto local_result = make_temporary_clone(exec, result);
auto dense_res = make_temporary_conversion<remove_complex<ValueType>>(
local_result.get());
exec->run(dense::make_compute_infinite_norm(this, dense_res.get(), tmp));
}


template <typename ValueType>
void Dense<ValueType>::compute_norm2_impl(LinOp* result) const
{
Expand Down
8 changes: 8 additions & 0 deletions core/matrix/dense_kernels.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ namespace kernels {
matrix::Dense<remove_complex<_type>>* result, \
array<char>& tmp)

#define GKO_DECLARE_DENSE_COMPUTE_INFINITE_NORM_KERNEL(_type) \
void compute_infinite_norm(std::shared_ptr<const DefaultExecutor> exec, \
const matrix::Dense<_type>* x, \
matrix::Dense<remove_complex<_type>>* result, \
array<char>& tmp)

#define GKO_DECLARE_DENSE_COMPUTE_NORM1_KERNEL(_type) \
void compute_norm1(std::shared_ptr<const DefaultExecutor> exec, \
const matrix::Dense<_type>* x, \
Expand Down Expand Up @@ -384,6 +390,8 @@ namespace kernels {
template <typename ValueType> \
GKO_DECLARE_DENSE_COMPUTE_NORM2_DISPATCH_KERNEL(ValueType); \
template <typename ValueType> \
GKO_DECLARE_DENSE_COMPUTE_INFINITE_NORM_KERNEL(ValueType); \
template <typename ValueType> \
GKO_DECLARE_DENSE_COMPUTE_NORM1_KERNEL(ValueType); \
template <typename ValueType> \
GKO_DECLARE_DENSE_COMPUTE_MEAN_KERNEL(ValueType); \
Expand Down
2 changes: 1 addition & 1 deletion core/solver/ir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ void Ir<ValueType>::apply_dense_impl(const VectorType* dense_b,
// solver_->apply(relaxation_factor_, residual_ptr, one_op,
// dense_x);
// WARNING: ignore the relaxation factor now
residual_ptr->compute_norm2(norm_op, reduction_tmp);
residual_ptr->compute_infinite_norm(norm_op, reduction_tmp);
residual_ptr->inv_scale(norm_op);
solver_->apply(residual_ptr, inner_solution);
dense_x->add_scaled(norm_op, inner_solution);
Expand Down
21 changes: 21 additions & 0 deletions include/ginkgo/core/matrix/dense.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1037,6 +1037,27 @@ class Dense
*/
void compute_norm2(ptr_param<LinOp> result, array<char>& tmp) const;

/**
* Computes the column-wise infinite norm of this matrix.
*
* @param result a Dense row vector, used to store the norm
* (the number of columns in the vector must match the number
* of columns of this)
*/
void compute_infinite_norm(ptr_param<LinOp> result) const;

/**
* Computes the column-wise infinite norm of this matrix.
*
* @param result a Dense row vector, used to store the norm
* (the number of columns in the vector must match the
* number of columns of this)
* @param tmp the temporary storage to use for partial sums during the
* reduction computation. It may be resized and/or reset to the
* correct executor.
*/
void compute_infinite_norm(ptr_param<LinOp> result, array<char>& tmp) const;

/**
* Computes the column-wise (L^1) norm of this matrix.
*
Expand Down
20 changes: 20 additions & 0 deletions reference/matrix/dense_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,26 @@ void compute_norm1(std::shared_ptr<const ReferenceExecutor> exec,
GKO_INSTANTIATE_FOR_EACH_VALUE_TYPE(GKO_DECLARE_DENSE_COMPUTE_NORM1_KERNEL);


template <typename ValueType>
void compute_infinite_norm(std::shared_ptr<const ReferenceExecutor> exec,
const matrix::Dense<ValueType>* x,
matrix::Dense<remove_complex<ValueType>>* result,
array<char>&)
{
for (size_type j = 0; j < x->get_size()[1]; ++j) {
result->at(0, j) = zero<remove_complex<ValueType>>();
}
for (size_type i = 0; i < x->get_size()[0]; ++i) {
for (size_type j = 0; j < x->get_size()[1]; ++j) {
result->at(0, j) = max(result->at(0, j), abs(x->at(i, j)));
}
}
}

GKO_INSTANTIATE_FOR_EACH_VALUE_TYPE(
GKO_DECLARE_DENSE_COMPUTE_INFINITE_NORM_KERNEL);


template <typename ValueType>
void compute_mean(std::shared_ptr<const ReferenceExecutor> exec,
const matrix::Dense<ValueType>* x,
Expand Down

0 comments on commit fef8ff3

Please sign in to comment.