|
| 1 | +/*******************************<GINKGO LICENSE>****************************** |
| 2 | +Copyright (c) 2017-2021, the Ginkgo authors |
| 3 | +All rights reserved. |
| 4 | +
|
| 5 | +Redistribution and use in source and binary forms, with or without |
| 6 | +modification, are permitted provided that the following conditions |
| 7 | +are met: |
| 8 | +
|
| 9 | +1. Redistributions of source code must retain the above copyright |
| 10 | +notice, this list of conditions and the following disclaimer. |
| 11 | +
|
| 12 | +2. Redistributions in binary form must reproduce the above copyright |
| 13 | +notice, this list of conditions and the following disclaimer in the |
| 14 | +documentation and/or other materials provided with the distribution. |
| 15 | +
|
| 16 | +3. Neither the name of the copyright holder nor the names of its |
| 17 | +contributors may be used to endorse or promote products derived from |
| 18 | +this software without specific prior written permission. |
| 19 | +
|
| 20 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 21 | +IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 22 | +TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
| 23 | +PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | +HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | +******************************<GINKGO LICENSE>*******************************/ |
| 32 | + |
| 33 | +#include <ginkgo/ginkgo.hpp> |
| 34 | + |
| 35 | + |
| 36 | +#include <ginkgo/kernels/kernel_launch.hpp> |
| 37 | + |
| 38 | + |
| 39 | +namespace GKO_DEVICE_NAMESPACE { |
| 40 | + |
| 41 | + |
| 42 | +using namespace gko::kernels::GKO_DEVICE_NAMESPACE; |
| 43 | +using std::cos; |
| 44 | +using std::sin; |
| 45 | + |
| 46 | +template <typename T> |
| 47 | +struct err {}; |
| 48 | + |
| 49 | + |
| 50 | +void linear_step(std::shared_ptr<const DefaultExecutor> exec, int n, |
| 51 | + double phase_scale, |
| 52 | + gko::matrix::Dense<std::complex<double>>* freq) |
| 53 | +{ |
| 54 | + using device_complex = device_type<std::complex<double>>; |
| 55 | + run_kernel( |
| 56 | + exec, |
| 57 | + GKO_KERNEL(auto i, auto j, auto n, auto phase_scale, |
| 58 | + auto amplitude_scale, auto freq) { |
| 59 | + auto phase = -(i * i + j * j) * phase_scale; |
| 60 | + freq[i * n + j] *= |
| 61 | + device_complex{cos(phase), sin(phase)} * amplitude_scale; |
| 62 | + }, |
| 63 | + gko::dim<2>{n, n}, n, phase_scale, 1.0 / (n * n), freq); |
| 64 | +} |
| 65 | + |
| 66 | + |
| 67 | +void nonlinear_step(std::shared_ptr<const DefaultExecutor> exec, int n, |
| 68 | + double nonlinear_scale, double potential_scale, |
| 69 | + double time_scale, |
| 70 | + const gko::matrix::Dense<double>* potential, |
| 71 | + gko::matrix::Dense<std::complex<double>>* ampl) |
| 72 | +{ |
| 73 | + using device_complex = device_type<std::complex<double>>; |
| 74 | + run_kernel( |
| 75 | + exec, |
| 76 | + GKO_KERNEL(auto i, auto j, auto n, auto nonlinear_scale, |
| 77 | + auto potential_scale, auto time_scale, auto potential, |
| 78 | + auto ampl) { |
| 79 | + auto idx = i * n + j; |
| 80 | + auto phase = -(nonlinear_scale * gko::squared_norm(ampl[idx]) + |
| 81 | + potential_scale * potential[idx]) * |
| 82 | + time_scale; |
| 83 | + ampl[idx] *= device_complex{cos(phase), sin(phase)}; |
| 84 | + }, |
| 85 | + gko::dim<2>{n, n}, n, nonlinear_scale, potential_scale, time_scale, |
| 86 | + potential, ampl); |
| 87 | +} |
| 88 | + |
| 89 | + |
| 90 | +} // namespace GKO_DEVICE_NAMESPACE |
0 commit comments