From d4fb40fda645ff554ac807c635c92d7666eda48c Mon Sep 17 00:00:00 2001 From: Yi Zhang Date: Tue, 5 Aug 2025 22:17:49 +0800 Subject: [PATCH] tmp --- CMakeLists.txt | 1 + src/optimization/geoblas_lcg_solver_ex.cpp | 114 +++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 src/optimization/geoblas_lcg_solver_ex.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index e7f98fe..a89a7b6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -98,6 +98,7 @@ endif() if(GCTL_FOUND AND GCTL_OPTIMIZATION_FOUND) add_example(optimization lcg_solver_ex ON OFF ON OFF) + add_example(optimization geoblas_lcg_solver_ex ON OFF ON OFF) add_example(optimization lgd_solver_ex ON OFF ON OFF) add_example(optimization ex3 ON OFF ON OFF) add_example(optimization ex4 ON OFF ON OFF) diff --git a/src/optimization/geoblas_lcg_solver_ex.cpp b/src/optimization/geoblas_lcg_solver_ex.cpp new file mode 100644 index 0000000..978c53c --- /dev/null +++ b/src/optimization/geoblas_lcg_solver_ex.cpp @@ -0,0 +1,114 @@ +/******************************************************** + * ██████╗ ██████╗████████╗██╗ + * ██╔════╝ ██╔════╝╚══██╔══╝██║ + * ██║ ███╗██║ ██║ ██║ + * ██║ ██║██║ ██║ ██║ + * ╚██████╔╝╚██████╗ ██║ ███████╗ + * ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝ + * Geophysical Computational Tools & Library (GCTL) + * + * Copyright (c) 2022 Yi Zhang (yizhang-geo@zju.edu.cn) + * + * GCTL is distributed under a dual licensing scheme. You can redistribute + * it and/or modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation, either version 2 + * of the License, or (at your option) any later version. You should have + * received a copy of the GNU Lesser General Public License along with this + * program. If not, see . + * + * If the terms and conditions of the LGPL v.2. would prevent you from using + * the GCTL, please consider the option to obtain a commercial license for a + * fee. These licenses are offered by the GCTL's original author. As a rule, + * licenses are provided "as-is", unlimited in time for a one time fee. Please + * send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget + * to include some description of your company and the realm of its activities. + * Also add information on how to contact you by electronic and paper mail. + ******************************************************/ + +#include "gctl/core/array_algorithm.h" +#include "gctl/core/matrix_algorithm.h" +#include "gctl/optimization/lcg_geoblas.h" + +#define M 1000 +#define N 800 + +double max_diff(const gctl::array &a, const gctl::array &b) +{ + double max = -1.0; + for (size_t i = 0; i < a.size(); i++) + { + max = std::max(sqrt((a[i] - b[i])*(a[i] - b[i])), max); + } + return max; +} + +class ex1 : public gctl::geoblas_lcg_solver +{ +public: + ex1(); + virtual ~ex1(); + + // 计算共轭梯度的B项 + void cal_partb(const geoblas_vec_t dx, geoblas_vec_t dB); + + //定义共轭梯度中Ax的算法 + virtual void LCG_Ax(const geoblas_vec_t x, geoblas_vec_t ax); + +private: + geoblas_mat_t d_kernel; // 普通二维数组做核矩阵 + geoblas_vec_t d_tmp; // 中间结果数组 + + gctl::matrix h_kernel; +}; + +ex1::ex1() +{ + h_kernel.resize(M, N); + random_float(h_kernel, -1.0, 1.0, gctl::RdUniform); + + d_kernel = geoblas_mat_new(M, N); + d_tmp = geoblas_vec_new(M); + + geoblas_put_mat_data(d_kernel, h_kernel.get()); +} + +ex1::~ex1() +{ + geoblas_mat_free(d_kernel); + geoblas_vec_free(d_tmp); +} + +void ex1::cal_partb(const geoblas_vec_t dx, geoblas_vec_t dB) +{ + LCG_Ax(dx, dB); + return; +} + +void ex1::LCG_Ax(const geoblas_vec_t x, geoblas_vec_t ax) +{ + geoblas_gemv('N', M, N, 1.0, d_kernel, x, 0.0, d_tmp); + geoblas_gemv('T', M, N, 1.0, d_kernel, d_tmp, 0.0, ax); + return; +} + +int main(int argc, char const *argv[]) +{ + ex1 test; + + gctl::array m(N, 0.0); + gctl::array fm(N); + random_float(fm, 1.0, 2.0, gctl::RdUniform); + + geoblas_vec_t dfm = geoblas_vec_new(N); + geoblas_vec_t dm = geoblas_vec_new(N); + geoblas_vec_t dB = geoblas_vec_new(N); + geoblas_put_vec_data(dfm, fm.get()); + geoblas_put_vec_data(dm, m.get()); + + test.cal_partb(dfm, dB); + test.lcg(dm, dB); + + geoblas_get_vec_data(dm, m.get()); + std::cout << "max-diff = " << max_diff(fm , m) << "\n"; + return 0; +} \ No newline at end of file