9#include "Propagator_u.h"
11#include <pscf/field/cuda/WaveList.h>
12#include <pscf/field/cuda/FFT.h>
13#include <pscf/crystal/UnitCell.h>
15#include <pscf/backend/cuda/Reduce.h>
16#include <pscf/backend/cuda/VecOp.h>
17#include <pscf/backend/cuda/ThreadArray.h>
18#include <pscf/backend/cuda/cudaTypes.h>
19#include <pscf/mesh/Mesh.h>
20#include <pscf/mesh/MeshIterator.h>
22#include <pscf/solvers/BlockTmpl.tpp>
35 __global__
void _realMulVConjVV(
cudaReal* a,
40 int nThreads = blockDim.x * gridDim.x;
41 int startID = blockIdx.x * blockDim.x + threadIdx.x;
43 for (
int i = startID; i < n; i +=
nThreads) {
52 a[i] = ((bt.x * ct.x) + (bt.y * ct.y)) * d[i];
59 __global__
void _richardsonEx(
cudaReal* qNew,
64 int nThreads = blockDim.x * gridDim.x;
65 int startID = blockIdx.x * blockDim.x + threadIdx.x;
67 for (
int i = startID; i < n; i +=
nThreads) {
68 q2 = qr2[i] * expW2[i];
69 qNew[i] = (4.0 * q2 - qr[i]) / 3.0;
76 __global__
void _addEqMulVVc(
cudaReal* a,
82 int nThreads = blockDim.x * gridDim.x;
83 int startID = blockIdx.x * blockDim.x + threadIdx.x;
84 for (
int i = startID; i < n; i +=
nThreads) {
85 a[i] += b[i] * c[i] * d;
92 __global__
void _addEqMulVVV(
cudaReal* a,
98 int nThreads = blockDim.x * gridDim.x;
99 int startID = blockIdx.x * blockDim.x + threadIdx.x;
100 for (
int i = startID; i < n; i +=
nThreads) {
101 a[i] += b[i]*c[i]*d[i];
117 void realMulVConjVV(BackendArray<cudaReal,CUT>& a,
118 BackendArray<cudaComplex,CUT>
const & b,
119 BackendArray<cudaComplex,CUT>
const & c,
120 BackendArray<cudaReal,CUT>
const & d)
122 int n = a.capacity();
132 _realMulVConjVV<<<nBlocks, nThreads>>>(a.cArray(), b.cArray(),
133 c.cArray(), d.cArray(), n);
144 void richardsonEx(BackendArray<cudaReal,CUT>& qNew,
145 BackendArray<cudaReal,CUT>
const & qr,
146 BackendArray<cudaReal,CUT>
const & qr2,
147 BackendArray<cudaReal,CUT>
const & expW2)
149 int n = qNew.capacity();
159 _richardsonEx<<<nBlocks, nThreads>>>(qNew.cArray(), qr.cArray(),
160 qr2.cArray(), expW2.cArray(),
172 void addEqMulVVc(BackendArray<cudaReal,CUT>& a,
173 BackendArray<cudaReal,CUT>
const & b,
174 BackendArray<cudaReal,CUT>
const & c,
177 int n = a.capacity();
186 _addEqMulVVc<<<nBlocks, nThreads>>>(a.cArray(), b.cArray(),
193 void addEqMulVVV(BackendArray<cudaReal,CUT>& a,
194 BackendArray<cudaReal,CUT>
const & b,
195 BackendArray<cudaReal,CUT>
const & c,
196 BackendArray<cudaReal,CUT>
const & d)
198 int n = a.capacity();
208 _addEqMulVVV<<<nBlocks, nThreads>>>(a.cArray(), b.cArray(),
209 c.cArray(), d.cArray(), n);
221 unitCellPtr_(nullptr),
222 waveListPtr_(nullptr),
253 UTIL_CHECK(mesh.dimensions() == fft.meshDimensions());
261 unitCellPtr_ = &cell;
262 waveListPtr_ = &waveList;
276 useBatchedFFT_ = useBatchedFFT;
282 expW_.allocate(mesh().dimensions());
283 expKsq_.allocate(kMeshDimensions_);
284 expKsq2_.allocate(kMeshDimensions_);
286 expW2_.allocate(mesh().dimensions());
287 qrPair_.allocate(2 * mesh().size());
288 qkPair_.allocate(2 * kSize_);
291 expWInv_.allocate(mesh().dimensions());
292 qk_.allocate(mesh().dimensions());
296 cField().allocate(mesh().dimensions());
306 tempNs = floor(
length / (2.0 *
ds) + 0.5);
311 ds_ =
length/double(ns_ - 1);
327 fftBatchedPair_.setup(mesh().dimensions(), 2);
330 if (useBatchedFFT_) {
332 fftBatchedAll_.setup(mesh().dimensions(), ns_);
333 q0kBatched_.allocate(ns_ * kSize_);
334 q1kBatched_.allocate(ns_ * kSize_);
358 tempNs = floor(
length / (2.0 * dsTarget_) + 0.5);
363 ds_ =
length/double(ns_-1);
372 if (useBatchedFFT_) {
374 q0kBatched_.deallocate();
375 q1kBatched_.deallocate();
376 q0kBatched_.allocate(ns_ * kSize_);
377 q1kBatched_.allocate(ns_ * kSize_);
378 fftBatchedAll_.resetBatchSize(ns_);
394 if (isAllocated_ && ns_ !=
nBead + 2) {
401 if (useBatchedFFT_) {
403 q0kBatched_.deallocate();
404 q1kBatched_.deallocate();
405 q0kBatched_.allocate(ns_ * kSize_);
406 q1kBatched_.allocate(ns_ * kSize_);
407 fftBatchedAll_.resetBatchSize(ns_);
429 UTIL_CHECK(nParams_ == unitCell().nParameter());
444 if (!waveList().hasKSq()) {
445 waveList().computeKSq();
449 const double b = BlockTmplT::kuhn();
450 double bSqFactor = -1.0 * b * b / 6.0;
457 VecOp::expVc(expKsq2_, waveList().kSq(), bSqFactor / 2.0);
470 int nx = mesh().size();
500 int nx = mesh().size();
503 UTIL_CHECK(fft().meshDimensions() == mesh().dimensions());
513 qr.associate(qrPair_, 0, mesh().dimensions());
514 qr2.associate(qrPair_, nx, mesh().dimensions());
515 qk.associate(qkPair_, 0, mesh().dimensions());
516 qk2.associate(qkPair_, kSize_, mesh().dimensions());
521 fftBatchedPair_.forwardTransform(qrPair_, qkPair_);
524 fftBatchedPair_.inverseTransformUnsafe(qkPair_, qrPair_);
526 fft().forwardTransform(qr2, qk2);
528 fft().inverseTransformUnsafe(qk2, qr2);
529 richardsonEx(qout, qr, qr2, expW2_);
555 int nx = mesh().size();
572 int nx = mesh().size();
574 UTIL_CHECK(fft().meshDimensions() == mesh().dimensions());
583 fft().forwardTransform(qin, qk_);
585 fft().inverseTransformUnsafe(qk_, qout);
597 int nx = mesh().size();
606 fft().forwardTransform(qin, qk_);
608 fft().inverseTransformUnsafe(qk_, qout);
618 int nx = mesh().size();
632 addEqMulVVc(
cField(), p0.q(0), p1.q(ns_ - 1), 1.0);
633 addEqMulVVc(
cField(), p0.q(ns_ - 1), p1.q(0), 1.0);
635 for (
int j = 1; j < ns_ - 1; j += 2) {
637 addEqMulVVc(
cField(), p0.q(j), p1.q(ns_ - 1 - j), 4.0);
639 for (
int j = 2; j < ns_ - 2; j += 2) {
641 addEqMulVVc(
cField(), p0.q(j), p1.q(ns_ - 1 - j), 2.0);
654 int nx = mesh().size();
669 for (
int j = 1; j < ns_ - 1; ++j) {
670 addEqMulVVV(
cField(), p0.q(j), p1.q(ns_ - 1 - j), expWInv_);
685 int nx = mesh().size();
694 UTIL_CHECK(mesh().dimensions() == fft().meshDimensions());
703 if (!waveList().hasdKSq()) {
704 waveList().computedKSq();
708 double dels, normal, increment;
716 for (i = 0; i < nParams_; ++i) {
721 if (useBatchedFFT_) {
724 UTIL_CHECK(mesh().dimensions() == fftBatchedAll_.meshDimensions());
731 fftBatchedAll_.forwardTransform(p0.qAll(), q0kBatched_);
732 fftBatchedAll_.forwardTransform(p1.qAll(), q1kBatched_);
736 if (!q0k_.isAllocated()) {
737 q0k_.allocate(mesh().dimensions());
738 q1k_.allocate(mesh().dimensions());
744 for (j = 0; j < ns_ ; ++j) {
746 if (useBatchedFFT_) {
749 q0k_.associate(q0kBatched_, j * kSize_, mesh().dimensions());
750 q1k_.associate(q1kBatched_, (ns_-1-j) * kSize_,
751 mesh().dimensions());
755 fft().forwardTransform(p0.q(j), q0k_);
756 fft().forwardTransform(p1.q(ns_-1-j), q1k_);
761 if (j != 0 && j != ns_ - 1) {
771 for (n = 0; n < nParams_ ; ++n) {
774 realMulVConjVV(rTmp, q0k_, q1k_, waveList().dKSq(n));
778 increment *= b * b * dels / normal;
782 if (useBatchedFFT_) {
790 for (i = 0; i < nParams_; ++i) {
791 stress_[i] -= (dQ[i] * prefactor);
805 int nx = mesh().size();
806 IntVec<D> const & meshDim = mesh().dimensions();
810 UTIL_CHECK(meshDim == fft().meshDimensions());
820 if (useBatchedFFT_) {
822 UTIL_CHECK(meshDim == fftBatchedAll_.meshDimensions());
829 fftBatchedAll_.forwardTransform(p0.qAll(), q0kBatched_);
830 fftBatchedAll_.forwardTransform(p1.qAll(), q1kBatched_);
832 if (!q0k_.isAllocated()) {
833 q0k_.allocate(meshDim);
834 q1k_.allocate(meshDim);
839 if (!waveList().hasdKSq()) {
840 waveList().computedKSq();
846 for (
int i = 0; i < nParams_; ++i) {
852 const double bSq = b * b / 6.0;
856 if (!p0.isHeadEnd()) {
857 if (useBatchedFFT_) {
860 q0k_.associate(q0kBatched_, 0, meshDim);
861 q1k_.associate(q1kBatched_, (ns_- 2) * kSize_, meshDim);
865 fft().forwardTransform(p0.q(0), q0k_);
866 fft().forwardTransform(p1.q(ns_ - 2), q1k_);
868 for (
int n = 0; n < nParams_ ; ++n) {
869 realMulVConjVV(rTmp, q0k_, q1k_, waveList().dKSq(n));
872 increment *= 0.5*bSq;
875 if (useBatchedFFT_) {
882 for (
int j = 1; j < ns_ - 2; ++j) {
885 if (useBatchedFFT_) {
888 q0k_.associate(q0kBatched_, j * kSize_, meshDim);
889 q1k_.associate(q1kBatched_, (ns_- 2 -j) * kSize_, meshDim);
893 fft().forwardTransform(p0.q(j), q0k_);
894 fft().forwardTransform(p1.q(ns_ - 2 - j), q1k_);
898 for (
int n = 0; n < nParams_ ; ++n) {
901 realMulVConjVV(rTmp, q0k_, q1k_, waveList().dKSq(n));
911 if (useBatchedFFT_) {
920 if (!p0.isTailEnd()) {
921 if (useBatchedFFT_) {
922 q0k_.associate(q0kBatched_, (ns_-2)*kSize_, meshDim);
923 q1k_.associate(q1kBatched_, 0, meshDim);
926 fft().forwardTransform(p0.q(ns_-2), q0k_);
927 fft().forwardTransform(p1.q(0), q1k_);
929 for (
int n = 0; n < nParams_ ; ++n) {
930 realMulVConjVV(rTmp, q0k_, q1k_, waveList().dKSq(n));
933 increment *= 0.5*bSq;
936 if (useBatchedFFT_) {
944 for (
int i = 0; i < nParams_; ++i) {
945 stress_.append(-1.0*prefactor*dQ[i]);
956 BlockTmpl< Rp::Propagator<1,CUT>, RField<1,CUT> >;
958 BlockTmpl< Rp::Propagator<2,CUT>, RField<2,CUT> >;
960 BlockTmpl< Rp::Propagator<3,CUT>, RField<3,CUT> >;
963 template class Block<1,CUT>;
964 template class Block<2,CUT>;
965 template class Block<3,CUT>;
virtual void setKuhn(double kuhn)
virtual void setNBead(int nBead)
Set the number of beads in this block (only valid for bead model).
int nBead() const
Get the number of beads in this block, in the bead model.
virtual void setLength(double length)
Set the length of this block (only valid for thread model).
double length() const
Get the length of this block, in the thread model.
An IntVec<D, T> is a D-component vector of elements of integer type T.
Description of a regular grid of points in a periodic domain.
void stepBondBead(RField< D, CUT > const &qin, RField< D, CUT > &qout) const
Compute a bond operator for the bead model.
Rp::Propagator< D, CUT > & propagator(int directionId)
Get a Propagator for a specified direction (non-const reference).
double ds() const
Contour length step size.
void stepFieldBead(RField< D, CUT > &q) const
Apply the exponential field operator for the bead model.
RField< D, CUT > const & cField() const
Get the associated monomer concentration field (const reference).
int nParameter() const
Get the number of parameters in the unit cell.
Base template for UnitCell<D> classes, D=1, 2 or 3.
A fixed capacity (static) contiguous array with a variable logical size.
void clear()
Set logical size to zero.
void append(Data const &data)
Append data to the end of the array.
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
void setThreadsLogical(int nThreadsLogical)
Given total number of threads, set 1D execution configuration.
int nThreads()
Get the number of threads per block for execution.
int nBlocks()
Get the current number of blocks for execution.
double sum(Array< double > const &in)
Compute sum of array elements (real).
void mulVVPair(Array< double > &a1, Array< double > &a2, Array< double > const &b1, Array< double > const &b2, Array< double > const &c)
Vector multiplication in pairs, ax[i] = bx[i] * s[i], x=1,2.
void mulEqV(Array< double > &a, Array< double > const &b)
Vector-vector in-place multiplication, a[i] *= b[i] (real).
void mulEqS(Array< double > &a, double b)
Vector-scalar in-place multiplication, a[i] *= b (real).
void expVc(Array< double > &a, Array< double > const &b, const double c)
Exponentiation a scaled vector, a[i] = exp(b[i]*c) (real).
void eqS(Array< double > &a, double b)
Vector assignment, a[i] = b (real).
void mulEqVPair(Array< double > &a1, Array< double > &a2, Array< double > const &b)
In-place vector multiplication in pairs, ax[i] *= b[i], x=1,2.
void divSV(Array< double > &a, double b, Array< double > const &c)
Vector division, a[i] = b / c[i].
bool isThread()
Is the thread model in use ?
bool isBead()
Is the bead model in use ?
Code for programs that simulate real-valued periodic fields.
PSCF package top-level namespace.
cufftDoubleComplex cudaComplex
Complex number type used in CPU code that uses FFTW.
cufftDoubleReal cudaReal
Real number type used in CPU code that uses FFTW.