简体   繁体   中英

Using CUDA Thrust to evaluate recurrence relations for nonlinear partial differential equations

I need to compute double precision recurrence relations of the form

  1. X[n] = A[n]*X[n-1] + B[n]
  2. X[n] = A[n]*X[n-1] + B[n]*X[n-2] + C[n]

I'm using these in conjunction with nonlinear PDE's. Is Thrust's inclusive scan operator flexible enough to compute these?

As mentioned by talonmies, recurrence relations involved in the solution of nonlinear partial differential equations are something different from prefix sums.

The above relations are typically in the form of update relations like

在此输入图像描述

see also the book "Partial Difference Equations" by Sui Sun Cheng.

To evaluate this update equation in parallel, a possibility is using zip iterators in conjunction with transforms, as in the below example

#include <thrust\device_vector.h>

struct Recurrence{
template <typename Tuple>
    __host__ __device__ double operator()(Tuple a) {

        // --- X[n] = A[n]*X[n-1] + B[n]*X[n-2] + C[n]
        return (thrust::get<0>(a) * thrust::get<3>(a) + thrust::get<1>(a) * thrust::get<4>(a) + thrust::get<2>(a));

    }
};


int main() {

    const int N = 10;

    thrust::device_vector<double> d_x(N, 1.f);
    thrust::device_vector<double> d_a(N, 2.f);
    thrust::device_vector<double> d_b(N, 3.f);
    thrust::device_vector<double> d_c(N, 4.f);

    thrust::transform(thrust::make_zip_iterator(thrust::make_tuple(d_a.begin() + 2, d_b.begin() + 2, d_c.begin() + 2, d_x.begin() + 1,     d_x.begin())), 
                      thrust::make_zip_iterator(thrust::make_tuple(d_a.begin() + N, d_b.begin() + N, d_c.begin() + N, d_x.begin() + N - 1, d_x.begin() + N - 2)), 
                      d_x.begin() + 2, Recurrence());

    for (int i=2; i<N; i++) {
        double temp = d_x[i];
        printf("%i %f\n", i, temp);
    }

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM