diff --git a/include/gauge_field.h b/include/gauge_field.h index e00b79aca8..fe6cd7b8ed 100644 --- a/include/gauge_field.h +++ b/include/gauge_field.h @@ -393,6 +393,21 @@ namespace quda { */ void removeStaggeredPhase(); + /** + @brief Put the antiperiodic t boundary sign into the links of a field without staggered phases: negate the t + links of the last global time slice, or of the last three for long links, halo included, and record an + antiperiodic t boundary, which for such fields means that the links hold the sign. The field must record a + periodic t boundary, be in native order with reconstruct NO, 13, 12, 9 or 8 (NO, 13 or 9 for long links), and + for reconstruct 12 and 8 have no imaginary chemical potential. + */ + void applyTBoundary(); + + /** + @brief Take the antiperiodic t boundary sign out of the links of a field without staggered phases, and record + a periodic t boundary; the reverse of applyTBoundary + */ + void removeTBoundary(); + /** Return the imaginary chemical potential applied to this field */ @@ -807,6 +822,14 @@ namespace quda { */ void applyGaugePhase(GaugeField &u); + /** + @brief Negate the t links that cross the t boundary: those of the last global time slice, or of the last three + for long links, halo included. The field's t boundary label is left to the caller. + @param[in,out] u The gauge field, in native order with reconstruct NO, 13, 12, 9 or 8 (NO, 13 or 9 for long + links), and for reconstruct 12 and 8 no imaginary chemical potential + */ + void applyGaugeTBoundary(GaugeField &u); + /** Compute XOR-based checksum of this gauge field: each gauge field entry is converted to type uint64_t, and compute the cummulative XOR of these values. diff --git a/include/kernels/extract_gauge_ghost_extended.cuh b/include/kernels/extract_gauge_ghost_extended.cuh index 21979a0d5c..8723589643 100644 --- a/include/kernels/extract_gauge_ghost_extended.cuh +++ b/include/kernels/extract_gauge_ghost_extended.cuh @@ -163,7 +163,8 @@ namespace quda { int d = D0 + gd - g *arg.R[dim]; // we only do the extraction for parity we are currently working on - int oddness = (a+b+c+d) & 1; + // sites are labelled by the parity of their regular coordinates, as in the rest of the field + int oddness = (a + b + c + d + arg.R[0] + arg.R[1] + arg.R[2] + arg.R[3]) & 1; if (oddness == parity) { if (Arg::extract) extractor(arg, dir, a, b, c, d, g, parity); else injector(arg, dir, a, b, c, d, g, parity); diff --git a/include/kernels/gauge_phase.cuh b/include/kernels/gauge_phase.cuh index 8bbc2be978..d166a361e8 100644 --- a/include/kernels/gauge_phase.cuh +++ b/include/kernels/gauge_phase.cuh @@ -5,87 +5,148 @@ #include #include -namespace quda { +namespace quda +{ + + /** + @brief Where the sites of a gauge field are on the global lattice, halo included, and which t links cross the + t boundary + */ + struct GaugePhaseGeometry { + int_fastdiv E[4]; // extended grid dims + int border[4]; + int offset[4]; // global coordinates of the rank's first regular site + int_fastdiv G[4]; // global grid dims + int parity_shift; // sites are labelled by their regular parity: this changes it to that of their coordinates + int boundary_slices; // the last global time slices whose t links cross the boundary: three for long links + + GaugePhaseGeometry(const GaugeField &u) + { + parity_shift = 0; + for (int d = 0; d < 4; d++) { + E[d] = u.X()[d]; + border[d] = u.R()[d]; + const int X = u.X()[d] - 2 * border[d]; + offset[d] = comm_coord(d) * X; + G[d] = comm_dim(d) * X; + parity_shift += border[d]; + } + parity_shift &= 1; + boundary_slices = u.LinkType() == QUDA_THREE_LINKS ? 3 : 1; + } + + /** + @brief Whether the t links of a global time slice cross the t boundary + @param[in] t The global t coordinate + */ + __device__ __host__ inline bool crossesTBoundary(int t) const { return t >= G[3] - boundary_slices; } + + /** + @brief The global coordinate of a coordinate of the field, halo included, which wraps around the global lattice + @param[in] d The direction + @param[in] x The coordinate in the field + */ + __device__ __host__ inline int globalCoord(int d, int x) const { return (x - border[d] + offset[d] + G[d]) % G[d]; } + + /** + @brief The global coordinates of a site of the field, halo included + @param[out] x The coordinates + @param[in] x_cb The site's checkerboard index in the field + @param[in] parity The site's parity label + */ + __device__ __host__ inline void getGlobalCoords(int x[4], int x_cb, int parity) const + { + getCoords(x, x_cb, E, (parity + parity_shift) & 1); +#pragma unroll + for (int d = 0; d < 4; d++) x[d] = globalCoord(d, x[d]); + } + + /** + @brief The number of time slices of the field, halo included, whose t links cross the t boundary + */ + int boundaryTimeSlices() const + { + int n = 0; + for (int t = 0; t < E[3]; t++) n += crossesTBoundary(globalCoord(3, t)); + return n; + } + }; template - struct GaugePhaseArg : kernel_param<> { + struct GaugePhaseArg : kernel_param<>, GaugePhaseGeometry { using Float = Float_; using real = typename mapper::type; static constexpr int nColor = nColor_; static_assert(nColor == 3, "Only nColor=3 enabled at this time"); static constexpr QudaReconstructType recon = recon_; static constexpr QudaStaggeredPhase phase = phase_; - typedef typename gauge_mapper::type Gauge; + typedef typename gauge_mapper::type Gauge; Gauge u; - int X[4]; real tBoundary; real i_mu; complex i_mu_phase; - GaugePhaseArg(GaugeField &u) : kernel_param(dim3(u.VolumeCB(), 2, 1)), u(u), i_mu(static_cast(u.iMu())) + GaugePhaseArg(GaugeField &u) : + kernel_param(dim3(u.VolumeCB(), 2, 1)), GaugePhaseGeometry(u), u(u), i_mu(static_cast(u.iMu())) { // if staggered phases are applied, then we are removing them // else we are applying them real dir = u.StaggeredPhaseApplied() ? real(-1.0) : real(1.0); - i_mu_phase = complex(static_cast(cos(M_PI * double(u.iMu()) / (u.X()[3] * comm_dim(3)))), - dir * static_cast(sin(M_PI * double(u.iMu()) / (u.X()[3] * comm_dim(3))))); + const int T = G[3]; + i_mu_phase = complex(static_cast(cos(M_PI * double(u.iMu()) / T)), + dir * static_cast(sin(M_PI * double(u.iMu()) / T))); - for (int d=0; d<4; d++) X[d] = u.X()[d]; - - // only set the boundary condition on the last time slice of nodes - bool last_node_in_t = (commCoords(3) == commDim(3)-1); - tBoundary = static_cast(static_cast(last_node_in_t ? u.TBoundary() : QUDA_PERIODIC_T)); + tBoundary = static_cast(static_cast(u.TBoundary())); } }; - // FIXME need to check this with odd local volumes - template constexpr auto getPhase(int x, int y, int z, int t, const Arg &arg) { + // x, y, z and t are a site's global coordinates + template constexpr auto getPhase(int x, int y, int z, int t, const Arg &arg) + { typename Arg::real phase = typename Arg::real(1.0); if (Arg::phase == QUDA_STAGGERED_PHASE_MILC) { - if (dim==0) { + if (dim == 0) { phase = (1 - 2 * (t % 2)); } else if (dim == 1) { phase = (1 - 2 * ((t + x) % 2)); } else if (dim == 2) { phase = (1 - 2 * ((t + x + y) % 2)); } else if (dim == 3) { // also apply boundary condition - phase = (t == arg.X[3] - 1) ? arg.tBoundary : typename Arg::real(1.0); + phase = arg.crossesTBoundary(t) ? arg.tBoundary : typename Arg::real(1.0); } } else if (Arg::phase == QUDA_STAGGERED_PHASE_TIFR) { - if (dim==0) { + if (dim == 0) { phase = (1 - 2 * ((3 + t + z + y) % 2)); } else if (dim == 1) { phase = (1 - 2 * ((2 + t + z) % 2)); } else if (dim == 2) { phase = (1 - 2 * ((1 + t) % 2)); } else if (dim == 3) { // also apply boundary condition - phase = (t == arg.X[3] - 1) ? arg.tBoundary : typename Arg::real(1.0); + phase = arg.crossesTBoundary(t) ? arg.tBoundary : typename Arg::real(1.0); } } else if (Arg::phase == QUDA_STAGGERED_PHASE_CHROMA) { // Chroma follows CPS convention, but uses -Dslash instead of Dslash compared to QUDA - if (dim==0) { - phase = -1.0; + if (dim == 0) { + phase = -1.0; } else if (dim == 1) { phase = (1 - 2 * ((1 + x) % 2)); } else if (dim == 2) { phase = (1 - 2 * ((1 + x + y) % 2)); } else if (dim == 3) { // also apply boundary condition - phase = ((t == arg.X[3] - 1) ? arg.tBoundary : typename Arg::real(1.0)) * (1 - 2 * ((1 + x + y + z) % 2)); + phase = (arg.crossesTBoundary(t) ? arg.tBoundary : typename Arg::real(1.0)) * (1 - 2 * ((1 + x + y + z) % 2)); } } return phase; } template - __device__ __host__ void gaugePhase(int indexCB, int parity, const Arg &arg) { + __device__ __host__ void gaugePhase(const int x[4], int indexCB, int parity, const Arg &arg) + { using real = typename Arg::real; - int x[4]; - getCoords(x, indexCB, arg.X, parity); - real phase = getPhase(x[0], x[1], x[2], x[3], arg); - Matrix,Arg::nColor> u = arg.u(dim, indexCB, parity); + Matrix, Arg::nColor> u = arg.u(dim, indexCB, parity); u *= phase; // apply imaginary chemical potential if needed @@ -94,19 +155,53 @@ namespace quda { arg.u(dim, indexCB, parity) = u; } - template struct GaugePhase - { + template struct GaugePhase { + const Arg &arg; + constexpr GaugePhase(const Arg &arg) : arg(arg) { } + static constexpr const char *filename() { return KERNEL_FILE; } + + __device__ __host__ void operator()(int x_cb, int parity) + { + int x[4]; + arg.getGlobalCoords(x, x_cb, parity); + gaugePhase<0>(x, x_cb, parity, arg); + gaugePhase<1>(x, x_cb, parity, arg); + gaugePhase<2>(x, x_cb, parity, arg); + gaugePhase<3>(x, x_cb, parity, arg); + } + }; + + template + struct GaugeTBoundaryArg : kernel_param<>, GaugePhaseGeometry { + using Float = Float_; + using real = typename mapper::type; + static constexpr int nColor = nColor_; + static_assert(nColor == 3, "Only nColor=3 enabled at this time"); + static constexpr QudaReconstructType recon = recon_; + typedef typename gauge_mapper::type Gauge; + + Gauge u; + GaugeTBoundaryArg(GaugeField &u) : kernel_param(dim3(u.VolumeCB(), 2, 1)), GaugePhaseGeometry(u), u(u) { } + }; + + /** + @brief Negate the t links that cross the t boundary. They are loaded under the field's t boundary label and + stored without it: the reconstructions pack a link without the label, which only the unpacking uses. + */ + template struct GaugeTBoundary { const Arg &arg; - constexpr GaugePhase(const Arg &arg) : arg(arg) {} + constexpr GaugeTBoundary(const Arg &arg) : arg(arg) { } static constexpr const char *filename() { return KERNEL_FILE; } __device__ __host__ void operator()(int x_cb, int parity) { - gaugePhase<0>(x_cb, parity, arg); - gaugePhase<1>(x_cb, parity, arg); - gaugePhase<2>(x_cb, parity, arg); - gaugePhase<3>(x_cb, parity, arg); + int x[4]; + arg.getGlobalCoords(x, x_cb, parity); + if (!arg.crossesTBoundary(x[3])) return; + Matrix, Arg::nColor> u = arg.u(3, x_cb, parity); + u *= static_cast(-1.0); + arg.u(3, x_cb, parity) = u; } }; -} +} // namespace quda diff --git a/include/quda.h b/include/quda.h index 02ec102620..3aa9ad9ede 100644 --- a/include/quda.h +++ b/include/quda.h @@ -1481,8 +1481,8 @@ extern "C" { /** * Apply the staggered phase factors to the gauge field. If the * imaginary chemical potential is non-zero then the phase factor - * exp(imu/T) will be applied to the links in the temporal - * direction. + * exp(i pi i_mu / T), T being the global time extent, will be + * applied to every link in the temporal direction. * * @param gauge_h The gauge field * @param param The parameters of the gauge field diff --git a/lib/gauge_field.cpp b/lib/gauge_field.cpp index f260e7e22c..4ea1c32844 100644 --- a/lib/gauge_field.cpp +++ b/lib/gauge_field.cpp @@ -406,6 +406,43 @@ namespace quda { staggeredPhaseApplied = false; } + /** + @brief Check that a field's t boundary sign can be put into or taken out of its links + @param[in] u The field + @param[in] t_boundary The t boundary the field must record + @param[in] name The operation, for messages + */ + static void checkTBoundary(const GaugeField &u, QudaTboundary t_boundary, const char *name) + { + switch (u.StaggeredPhase()) { + case QUDA_STAGGERED_PHASE_MILC: + case QUDA_STAGGERED_PHASE_TIFR: + case QUDA_STAGGERED_PHASE_CHROMA: + errorQuda("%s: the t boundary sign of a field with staggered phase type %d goes with its phases", name, + u.StaggeredPhase()); + default: break; + } + if (u.TBoundary() != t_boundary) + errorQuda("%s: the field records t boundary %d, not %d", name, u.TBoundary(), t_boundary); + if (!u.isNative()) errorQuda("%s: the field is in order %d, not native order", name, u.Order()); + } + + void GaugeField::applyTBoundary() + { + checkTBoundary(*this, QUDA_PERIODIC_T, __func__); + applyGaugeTBoundary(*this); + t_boundary = QUDA_ANTI_PERIODIC_T; + if (ghostExchange == QUDA_GHOST_EXCHANGE_PAD) exchangeGhost(); + } + + void GaugeField::removeTBoundary() + { + checkTBoundary(*this, QUDA_ANTI_PERIODIC_T, __func__); + applyGaugeTBoundary(*this); + t_boundary = QUDA_PERIODIC_T; + if (ghostExchange == QUDA_GHOST_EXCHANGE_PAD) exchangeGhost(); + } + void GaugeField::createComms(const lat_dim_t &R, bool no_comms_fill, bool bidir) { allocateGhostBuffer(R, no_comms_fill, bidir); // allocate the ghost buffer if not yet allocated diff --git a/lib/gauge_phase.cu b/lib/gauge_phase.cu index 52e47dad71..1e253d5304 100644 --- a/lib/gauge_phase.cu +++ b/lib/gauge_phase.cu @@ -45,6 +45,9 @@ namespace quda { void applyGaugePhase(GaugeField &u) { + if (u.LinkType() == QUDA_THREE_LINKS && u.iMu() != 0.0) + errorQuda("The imaginary chemical potential (i_mu = %g) is not supported on long links", + static_cast(u.iMu())); getProfile().TPSTART(QUDA_PROFILE_COMPUTE); instantiate(u); // ensure that ghosts are updated if needed @@ -52,4 +55,49 @@ namespace quda { getProfile().TPSTOP(QUDA_PROFILE_COMPUTE); } + template class GaugeTBoundary_ : TunableKernel2D + { + GaugeField &u; // used for meta data only + long long boundary_bytes; + unsigned int minThreads() const { return u.VolumeCB(); } + + public: + GaugeTBoundary_(GaugeField &u) : + TunableKernel2D(u, 2), + u(u), + boundary_bytes(2 * (u.Bytes() / 4) * GaugePhaseGeometry(u).boundaryTimeSlices() / u.X()[3]) + { + strcat(aux, "t_boundary"); + apply(device::get_default_stream()); + } + + void apply(const qudaStream_t &stream) + { + TuneParam tp = tuneLaunch(*this, getTuning(), getVerbosity()); + launch(tp, stream, GaugeTBoundaryArg(u)); + } + + void preTune() { u.backup(); } + void postTune() { u.restore(); } + + long long flops() const { return 0; } + long long bytes() const { return boundary_bytes; } + }; + + void applyGaugeTBoundary(GaugeField &u) + { + const bool recon_12_8 = u.Reconstruct() == QUDA_RECONSTRUCT_12 || u.Reconstruct() == QUDA_RECONSTRUCT_8; + if (u.LinkType() == QUDA_THREE_LINKS && recon_12_8) + errorQuda("Reconstruct %d rebuilds the t boundary sign on one time slice, not the three of long links", + u.Reconstruct()); + // a field without phases holds i_mu only if its links were given the potential's U(1) phase, which reconstruct 12 + // and 8 cannot represent + if (recon_12_8 && u.iMu() != 0.0) + errorQuda("Reconstruct %d cannot hold the imaginary chemical potential (i_mu = %g)", u.Reconstruct(), + static_cast(u.iMu())); + getProfile().TPSTART(QUDA_PROFILE_COMPUTE); + instantiate(u); + getProfile().TPSTOP(QUDA_PROFILE_COMPUTE); + } + } // namespace quda diff --git a/lib/milc_interface.cpp b/lib/milc_interface.cpp index c0ce693c1b..2df4643257 100644 --- a/lib/milc_interface.cpp +++ b/lib/milc_interface.cpp @@ -744,11 +744,8 @@ QudaGaugeParam createGaugeParamForObservables(int precision, QudaMILCSiteArg_t * qudaGaugeParam.gauge_order = arg->site ? QUDA_MILC_SITE_GAUGE_ORDER : QUDA_MILC_GAUGE_ORDER; qudaGaugeParam.staggered_phase_applied = phase_in; qudaGaugeParam.staggered_phase_type = QUDA_STAGGERED_PHASE_MILC; - // FIXME: phases and boundary conditions are "munged" together inside QUDA, so the unphase function - // doesn't change the boundary condition flag. This setting guarantees that phases and boundary conditions - // are consistently set under the hood --- but we still need an extra minus sign on the output. - qudaGaugeParam.t_boundary = QUDA_PERIODIC_T; - // if (phase_in) qudaGaugeParam.t_boundary = QUDA_ANTI_PERIODIC_T; + // MILC's phases include the antiperiodic boundary sign, which QUDA removes with them, as for the other phased links + if (phase_in) qudaGaugeParam.t_boundary = QUDA_ANTI_PERIODIC_T; if (phase_in) qudaGaugeParam.reconstruct_sloppy = qudaGaugeParam.reconstruct = QUDA_RECONSTRUCT_NO; if (!have_resident_gauge) { @@ -827,9 +824,8 @@ void qudaPolyakovLoopPhased(int precision, double ploop[2], int dir, QudaMILCSit obsParam.remove_staggered_phase = phase_in ? QUDA_BOOLEAN_TRUE : QUDA_BOOLEAN_FALSE; gaugeObservablesQuda(&obsParam); - // FIXME: see comment in createGaugeParamForObservables - ploop[0] = -obsParam.ploop[0]; - ploop[1] = -obsParam.ploop[1]; + ploop[0] = obsParam.ploop[0]; + ploop[1] = obsParam.ploop[1]; qudamilc_called(__func__); return; @@ -867,9 +863,8 @@ void qudaGaugeMeasurementsPhased(int precision, double plaq[3], double ploop[2], plaq[1] = obsParam.plaquette[1]; plaq[2] = obsParam.plaquette[2]; - // FIXME: see comment in createGaugeParamForObservables - ploop[0] = -obsParam.ploop[0]; - ploop[1] = -obsParam.ploop[1]; + ploop[0] = obsParam.ploop[0]; + ploop[1] = obsParam.ploop[1]; qudamilc_called(__func__); return; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index f560d040cb..a5aa8384f8 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -211,6 +211,11 @@ target_link_libraries(su3_test ${TEST_LIBS}) quda_checkbuildtest(su3_test QUDA_BUILD_ALL_TESTS) install(TARGETS su3_test ${QUDA_EXCLUDE_FROM_INSTALL} DESTINATION ${CMAKE_INSTALL_BINDIR}) +add_executable(gauge_phase_test gauge_phase_test.cpp) +target_link_libraries(gauge_phase_test ${TEST_LIBS}) +quda_checkbuildtest(gauge_phase_test QUDA_BUILD_ALL_TESTS) +install(TARGETS gauge_phase_test ${QUDA_EXCLUDE_FROM_INSTALL} DESTINATION ${CMAKE_INSTALL_BINDIR}) + if(QUDA_DIRAC_LAPLACE) add_executable(su3_fermion_test su3_fermion_test.cpp) target_link_libraries(su3_fermion_test ${TEST_LIBS}) @@ -1688,6 +1693,18 @@ add_test(NAME su3 --dim 2 4 6 8 --enable-testing true --niter 1 --gtest_output=xml:su3_test.xml) +add_test(NAME gauge_phase + COMMAND ${QUDA_CTEST_LAUNCH} $ ${MPIEXEC_POSTFLAGS} + --dim 4 4 4 8 + --gtest_output=xml:gauge_phase_test.xml) + +# x, z and t partitioned: an odd number of partitioned directions alone or on a grid partitioned in z and t, so that +# extended fields of halo width 1 label their sites with the opposite parity to their coordinates +add_test(NAME gauge_phase_partition + COMMAND ${QUDA_CTEST_LAUNCH} $ ${MPIEXEC_POSTFLAGS} + --dim 4 4 4 8 --partition 13 + --gtest_output=xml:gauge_phase_partition_test.xml) + foreach(prec IN LISTS TEST_PRECS) if(QUDA_DIRAC_STAGGERED) diff --git a/tests/gauge_phase_test.cpp b/tests/gauge_phase_test.cpp new file mode 100644 index 0000000000..a120710615 --- /dev/null +++ b/tests/gauge_phase_test.cpp @@ -0,0 +1,507 @@ +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "host_utils.h" +#include "gauge_utils.h" +#include "momentum_utils.h" +#include "misc.h" +#include "test.h" + +// The staggered phases that GaugeField::applyStaggeredPhase and removeStaggeredPhase apply to device fields, +// against the test utilities' host phases (applyGaugeStaggeredPhase) and t boundary sign (applyGaugeTemporalBCs): +// the MILC, TIFR or CHROMA convention, times the antiperiodic sign on the t links of the last global t slice, or of +// the last three for long links. The links are Haar-random, and the fields must agree exactly, except under an +// imaginary chemical potential, whose phase on the t links rounds, so they agree within tolerance. Extended fields are +// checked as the gauge force and gauge path computations use them: removing the phases from an extended copy of a +// phased field must give the extended copy of the field without phases, halo included. Through the interface, +// phased links with the antiperiodic sign are loaded with the phases removed: labelled periodic, as MILC's +// observables load them, the sign stays and the Polyakov loop is the negative of that of the links without phases; +// labelled antiperiodic, the sign is removed with the phases and the Polyakov loop is theirs. The gauge force of +// phased links labelled antiperiodic, as MILC's gauge force loads them, is that of the links without phases. +// GaugeField::applyTBoundary and removeTBoundary put the antiperiodic sign into the links of a field without phases +// and take it out, recording it in the field's t boundary, for every reconstruct of gauge fields. + +namespace +{ + + /** + @brief Parameters for a gauge field of the test's lattice in double or single precision, without ghost zone: + QDP order on the host, native order on the device + @param[in] location Host or device + @param[in] prec Precision of the field + @param[in] phase Staggered phase type the field records + @param[in] t_boundary t boundary the field records + @param[in] link_type SU(3) links, or long links + @param[in] recon The reconstruct of a device field; host fields have none + @param[in] i_mu The imaginary chemical potential the field records + @return The parameters + */ + quda::GaugeFieldParam field_param(QudaFieldLocation location, QudaPrecision prec, QudaStaggeredPhase phase, + QudaTboundary t_boundary, QudaLinkType link_type = QUDA_SU3_LINKS, + QudaReconstructType recon = QUDA_RECONSTRUCT_NO, double i_mu = 0.0) + { + QudaGaugeParam gauge_param = newQudaGaugeParam(); + setWilsonGaugeParam(gauge_param); + gauge_param.type = link_type; + gauge_param.cpu_prec = prec; + gauge_param.t_boundary = t_boundary; + gauge_param.staggered_phase_type = phase; + gauge_param.staggered_phase_applied = false; + gauge_param.i_mu = i_mu; + quda::GaugeFieldParam param(gauge_param); + param.location = location; + param.create = QUDA_NULL_FIELD_CREATE; + param.ghostExchange = QUDA_GHOST_EXCHANGE_NO; + param.reconstruct = location == QUDA_CUDA_FIELD_LOCATION ? recon : QUDA_RECONSTRUCT_NO; + param.setPrecision(prec, location == QUDA_CUDA_FIELD_LOCATION); + return param; + } + + /** + @brief A Haar-random host gauge field of the test's lattice, without phases or boundary sign + @param[in] prec Precision of the field + @param[in] t_boundary The t boundary the field records, which GaugeField::copy requires of the fields it is + copied to and from; without reconstruct, the label doesn't change the links + @param[in] link_type SU(3) links, or long links, which GaugeField::copy also requires to match + @return The field + */ + quda::GaugeField random_field(QudaPrecision prec, QudaTboundary t_boundary, QudaLinkType link_type = QUDA_SU3_LINKS) + { + QudaGaugeParam gauge_param = newQudaGaugeParam(); + setWilsonGaugeParam(gauge_param); + gauge_param.cpu_prec = prec; + gauge_param.t_boundary = QUDA_PERIODIC_T; + gauge_param.anisotropy = 1.0; + quda::GaugeField u(field_param(QUDA_CPU_FIELD_LOCATION, prec, QUDA_STAGGERED_PHASE_NO, t_boundary, link_type)); + constructHostGaugeInputField(u, gauge_param, 0, nullptr, GaugeInputMode::HAAR); + return u; + } + + /** + @brief A host field's links times the phases of a convention and the t boundary sign, applied with the test + utilities + @param[in] u The links, in QDP order + @param[in] type MILC, TIFR or CHROMA + @param[in] t_boundary Periodic or antiperiodic: whether the t links of the last global t slice, or of the last + three for long links, change sign + @return The phased links, as a host field like u + */ + quda::GaugeField host_phased(const quda::GaugeField &u, QudaStaggeredPhase type, QudaTboundary t_boundary) + { + quda::GaugeField phased(u); + applyGaugeStaggeredPhase(phased, type); + applyGaugeTemporalBCs(phased, t_boundary); + return phased; + } + + /** + @brief Expect two host fields in QDP order to hold the same links, with every rank passing or failing alike. + Collective. + @param[in] result The field to check + @param[in] msg_result Its label in messages + @param[in] expected The reference + @param[in] msg_expected Its label in messages + @param[in] tol The largest difference allowed of any real + */ + void expect_links(const quda::GaugeField &result, const std::string &msg_result, const quda::GaugeField &expected, + const std::string &msg_expected, double tol = 0.0) + { + int fail = compare_link_fields(result, msg_result, expected, msg_expected, tol).first ? 0 : 1; + quda::comm_allreduce_max(fail); + EXPECT_EQ(fail, 0) << msg_result << " links differ from the " << msg_expected << " links"; + } + + /** + @brief Expect two fields to hold the same links on every rank, halo included, compared on the host in QDP order. + Collective. + @param[in] a A field + @param[in] msg_a Its label in messages + @param[in] b Another, with the same parameters + @param[in] msg_b Its label in messages + */ + void expect_same_links(const quda::GaugeField &a, const std::string &msg_a, const quda::GaugeField &b, + const std::string &msg_b) + { + quda::GaugeFieldParam host_param(a); + host_param.location = QUDA_CPU_FIELD_LOCATION; + host_param.create = QUDA_NULL_FIELD_CREATE; + host_param.order = QUDA_QDP_GAUGE_ORDER; + quda::GaugeField ha(host_param), hb(host_param); + ha.copy(a); + hb.copy(b); + expect_links(ha, msg_a, hb, msg_b); + } + + /** + @brief Gauge parameters for loading host links in QDP order through the interface, without reconstruct, in + one precision throughout + @param[in] prec The precision of the links, on the host and the device + @param[in] type The staggered phase convention the links record + @param[in] t_boundary The t boundary the links record + @param[in] applied Whether the links record their phases as applied + @return The parameters + */ + QudaGaugeParam load_param(QudaPrecision prec, QudaStaggeredPhase type, QudaTboundary t_boundary, bool applied) + { + QudaGaugeParam gauge_param = newQudaGaugeParam(); + setWilsonGaugeParam(gauge_param); + gauge_param.cpu_prec = gauge_param.cuda_prec = gauge_param.cuda_prec_sloppy = prec; + gauge_param.cuda_prec_precondition = gauge_param.cuda_prec_eigensolver = prec; + gauge_param.cuda_prec_refinement_sloppy = prec; + gauge_param.reconstruct = gauge_param.reconstruct_sloppy = QUDA_RECONSTRUCT_NO; + gauge_param.reconstruct_precondition = gauge_param.reconstruct_eigensolver = QUDA_RECONSTRUCT_NO; + gauge_param.reconstruct_refinement_sloppy = QUDA_RECONSTRUCT_NO; + gauge_param.anisotropy = 1.0; + gauge_param.t_boundary = t_boundary; + gauge_param.staggered_phase_type = type; + gauge_param.staggered_phase_applied = applied; + return gauge_param; + } + + /** + @brief The temporal Polyakov loop of host links loaded through the interface, with their phases removed + if they are applied, as gaugeObservablesQuda computes it. Collective. + @param[in] u The links, in QDP order + @param[in] type The staggered phase convention the links record + @param[in] t_boundary The t boundary the links record + @param[in] applied Whether the links hold their phases, which are then removed + @return The real and imaginary parts + */ + std::array polyakov_loop(const quda::GaugeField &u, QudaStaggeredPhase type, QudaTboundary t_boundary, + bool applied) + { + auto gauge_param = load_param(u.Precision(), type, t_boundary, applied); + auto arrays = u.data_array(); + loadGaugeQuda(arrays.data, &gauge_param); + QudaGaugeObservableParam obs_param = newQudaGaugeObservableParam(); + obs_param.compute_polyakov_loop = QUDA_BOOLEAN_TRUE; + obs_param.remove_staggered_phase = applied ? QUDA_BOOLEAN_TRUE : QUDA_BOOLEAN_FALSE; + gaugeObservablesQuda(&obs_param); + freeGaugeQuda(); + return {obs_param.ploop[0], obs_param.ploop[1]}; + } + + /** + @brief The gauge force of host links, as computeGaugeForceQuda computes it from the plaquette staples and two + rectangles of each direction, with the links' phases removed if they are applied. Collective. + @param[in] u The links, in QDP order + @param[in] type The staggered phase convention the links record + @param[in] t_boundary The t boundary the links record + @param[in] applied Whether the links hold their phases, which are then removed + @return The momentum, as a host field in QDP order + */ + quda::GaugeField gauge_force(const quda::GaugeField &u, QudaStaggeredPhase type, QudaTboundary t_boundary, bool applied) + { + // as gauge_path_test's: per direction, the six plaquette staples and two rectangles, 7 - d being backwards in d + const int paths[4][8][5] + = {{{1, 7, 6}, {6, 7, 1}, {2, 7, 5}, {5, 7, 2}, {3, 7, 4}, {4, 7, 3}, {0, 1, 7, 7, 6}, {1, 7, 7, 6, 0}}, + {{2, 6, 5}, {5, 6, 2}, {3, 6, 4}, {4, 6, 3}, {0, 6, 7}, {7, 6, 0}, {1, 2, 6, 6, 5}, {2, 6, 6, 5, 1}}, + {{3, 5, 4}, {4, 5, 3}, {0, 5, 7}, {7, 5, 0}, {1, 5, 6}, {6, 5, 1}, {2, 3, 5, 5, 4}, {3, 5, 5, 4, 2}}, + {{0, 4, 7}, {7, 4, 0}, {1, 4, 6}, {6, 4, 1}, {2, 4, 5}, {5, 4, 2}, {3, 0, 4, 4, 7}, {0, 4, 4, 7, 3}}}; + constexpr int num_paths = 8; + int length[num_paths] = {3, 3, 3, 3, 3, 3, 5, 5}; + double coeff[num_paths] = {1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.5, 2.6}; + std::array, num_paths>, 4> path_data; + std::array, 4> path_ptrs; + std::array input_path_buf; + for (int dir = 0; dir < 4; dir++) { + for (int i = 0; i < num_paths; i++) { + path_data[dir][i].assign(paths[dir][i], paths[dir][i] + length[i]); + path_ptrs[dir][i] = path_data[dir][i].data(); + } + input_path_buf[dir] = path_ptrs[dir].data(); + } + + auto gauge_param = load_param(u.Precision(), type, t_boundary, applied); + quda::GaugeFieldParam mom_param(gauge_param, nullptr, QUDA_ASQTAD_MOM_LINKS); + mom_param.location = QUDA_CPU_FIELD_LOCATION; + mom_param.create = QUDA_ZERO_FIELD_CREATE; + quda::GaugeField mom(mom_param); + auto mom_arrays = mom.data_array(); + auto arrays = u.data_array(); + computeGaugeForceQuda(mom_arrays.data, arrays.data, input_path_buf.data(), length, coeff, num_paths, 6, 0.3, + &gauge_param); + return mom; + } + + /** + @param[in] mom A host momentum field + @return Its copy in MILC order, the layout of the test utilities' momentum checks + */ + quda::GaugeField milc_mom(const quda::GaugeField &mom) + { + quda::GaugeFieldParam milc_param(mom); + milc_param.order = QUDA_MILC_GAUGE_ORDER; + milc_param.create = QUDA_NULL_FIELD_CREATE; + quda::GaugeField m(milc_param); + m.copy(mom); + return m; + } + + /** + @brief Expect two host momentum fields to agree within tol on every rank, with the test utilities' momentum + checks. Collective. + @param[in] a A host momentum field + @param[in] b Another, with the same parameters + @param[in] tol The largest difference allowed of any real + */ + void expect_same_mom(const quda::GaugeField &a, const quda::GaugeField &b, double tol = 0.0) + { + const auto ma = milc_mom(a); + const auto mb = milc_mom(b); + const int links = static_cast(4 * ma.Volume()); + strong_check_mom(ma.data(), mb.data(), links, ma.Precision()); + int fail = compare_floats(ma.data(), mb.data(), links * mom_site_size, tol, ma.Precision()) ? 0 : 1; + quda::comm_allreduce_max(fail); + EXPECT_EQ(fail, 0) << "the momenta differ"; + } + + /** + @brief How much a host momentum field's action, summed over all ranks by momentumActionCPU, exceeds that of a + zero field, -4 per link: zero only for a zero field. Collective. + @param[in] mom A host momentum field + @return The excess + */ + double mom_action_excess(const quda::GaugeField &mom) + { + const auto m = milc_mom(mom); + const int links = static_cast(4 * m.Volume()); + double all_links = links; + quda::comm_allreduce_sum(all_links); + return momentumActionCPU(m.data(), links, m.Precision()) + 4.0 * all_links; + } + +} // namespace + +using test_t = ::testing::tuple; + +class GaugePhaseTest : public ::testing::TestWithParam +{ +}; + +// the device phases and t boundary sign are those of the test utilities, and removing them restores the links +TEST_P(GaugePhaseTest, apply_remove) +{ + const auto [prec, type, t_boundary, link_type] = GetParam(); + if (!quda::is_enabled(prec)) GTEST_SKIP(); + const auto u = random_field(prec, t_boundary, link_type); + quda::GaugeField device(field_param(QUDA_CUDA_FIELD_LOCATION, prec, type, t_boundary, link_type)); + device.copy(u); + + // the copy takes u's phase type, so the device field is given its own + device.applyStaggeredPhase(type); + EXPECT_TRUE(device.StaggeredPhaseApplied()); + quda::GaugeField host(field_param(QUDA_CPU_FIELD_LOCATION, prec, type, t_boundary, link_type)); + host.copy(device); + expect_links(host, "device phased", host_phased(u, type, t_boundary), "host phased"); + + device.removeStaggeredPhase(); + EXPECT_FALSE(device.StaggeredPhaseApplied()); + host.copy(device); + expect_links(host, "device unphased", u, "original"); +} + +// an imaginary chemical potential multiplies every t link by exp(i pi i_mu / T) along with the phases, T being the +// global time extent, and removing the phases takes it out again; long links don't support it +TEST_P(GaugePhaseTest, i_mu) +{ + const auto [prec, type, t_boundary, link_type] = GetParam(); + if (!quda::is_enabled(prec) || link_type == QUDA_THREE_LINKS) GTEST_SKIP(); + constexpr double i_mu = 0.3; + const double tol = getTolerance(prec); + const auto u = random_field(prec, t_boundary); + quda::GaugeField device( + field_param(QUDA_CUDA_FIELD_LOCATION, prec, type, t_boundary, link_type, QUDA_RECONSTRUCT_NO, i_mu)); + device.copy(u); + + device.applyStaggeredPhase(type); + quda::GaugeField host(field_param(QUDA_CPU_FIELD_LOCATION, prec, type, t_boundary, link_type)); + host.copy(device); + auto expected = host_phased(u, type, t_boundary); + auto arrays = expected.data_array(); + applyGaugeImaginaryChemicalPotential(arrays.data, static_cast(expected.VolumeCB()), i_mu, prec); + expect_links(host, "device phased", expected, "host phased", tol); + + device.removeStaggeredPhase(); + host.copy(device); + expect_links(host, "device unphased", u, "original", tol); +} + +// removing the phases from an extended copy of a phased field gives the extended copy of the field without phases +TEST_P(GaugePhaseTest, extended_remove) +{ + const auto [prec, type, t_boundary, link_type] = GetParam(); + if (!quda::is_enabled(prec)) GTEST_SKIP(); + const auto u = random_field(prec, t_boundary, link_type); + quda::GaugeField plain(field_param(QUDA_CUDA_FIELD_LOCATION, prec, type, t_boundary, link_type)); + plain.copy(u); + quda::GaugeField phased(field_param(QUDA_CUDA_FIELD_LOCATION, prec, type, t_boundary, link_type)); + phased.copy(u); + phased.applyStaggeredPhase(type); + + // the halo widths of the interface's extended fields, and of width 1, whose sum is odd when an odd number of + // directions is partitioned, so that a site's parity label differs from the parity of its coordinates + for (int width : {2, 1}) { + quda::lat_dim_t R; + for (int d = 0; d < 4; d++) R[d] = width * quda::comm_dim_partitioned(d); + std::unique_ptr expected(quda::createExtendedGauge(plain, R)); + std::unique_ptr extended(quda::createExtendedGauge(phased, R)); + extended->removeStaggeredPhase(); + expect_same_links(*extended, "extended unphased, halo width " + std::to_string(width), *expected, + "extended original"); + } +} + +using interface_test_t = ::testing::tuple; + +class GaugePhaseInterfaceTest : public ::testing::TestWithParam +{ +}; + +// phased links with the antiperiodic sign, labelled periodic as MILC's observables load them: removing the phases +// leaves the sign, so the Polyakov loop is the negative of that of the links without phases +TEST_P(GaugePhaseInterfaceTest, polyakov_periodic_label) +{ + const auto [prec, type] = GetParam(); + if (!quda::is_enabled(prec)) GTEST_SKIP(); + const auto u = random_field(prec, QUDA_ANTI_PERIODIC_T); + const auto plain = polyakov_loop(u, QUDA_STAGGERED_PHASE_NO, QUDA_PERIODIC_T, false); + const auto phased = polyakov_loop(host_phased(u, type, QUDA_ANTI_PERIODIC_T), type, QUDA_PERIODIC_T, true); + for (int i = 0; i < 2; i++) EXPECT_DOUBLE_EQ(phased[i], -plain[i]) << (i == 0 ? "real part" : "imaginary part"); +} + +// the same links labelled antiperiodic: the sign is removed with the phases, so the Polyakov loop is that of the +// links without phases +TEST_P(GaugePhaseInterfaceTest, polyakov_antiperiodic_label) +{ + const auto [prec, type] = GetParam(); + if (!quda::is_enabled(prec)) GTEST_SKIP(); + const auto u = random_field(prec, QUDA_ANTI_PERIODIC_T); + const auto plain = polyakov_loop(u, QUDA_STAGGERED_PHASE_NO, QUDA_PERIODIC_T, false); + const auto phased = polyakov_loop(host_phased(u, type, QUDA_ANTI_PERIODIC_T), type, QUDA_ANTI_PERIODIC_T, true); + for (int i = 0; i < 2; i++) EXPECT_DOUBLE_EQ(phased[i], plain[i]) << (i == 0 ? "real part" : "imaginary part"); +} + +// the gauge force of phased links labelled antiperiodic, as MILC's gauge force loads them, is that of the links +// without phases: a sign on every t link of a time slice cancels in loops that don't wind around the lattice +TEST_P(GaugePhaseInterfaceTest, gauge_force) +{ + const auto [prec, type] = GetParam(); + if (!quda::is_enabled(prec)) GTEST_SKIP(); + const auto u = random_field(prec, QUDA_ANTI_PERIODIC_T); + const auto plain = gauge_force(u, QUDA_STAGGERED_PHASE_NO, QUDA_PERIODIC_T, false); + const auto phased = gauge_force(host_phased(u, type, QUDA_ANTI_PERIODIC_T), type, QUDA_ANTI_PERIODIC_T, true); + expect_same_mom(phased, plain); + EXPECT_GT(mom_action_excess(plain), 0.0) << "the force is zero, so the comparison shows nothing"; +} + +using t_boundary_test_t = ::testing::tuple; + +class GaugeTBoundaryTest : public ::testing::TestWithParam +{ +}; + +// applyTBoundary puts the antiperiodic sign into the links of a field without phases and records it, and +// removeTBoundary takes it out again, as the test utilities apply the sign. Reconstruct NO stores the links exactly, +// so the fields must agree exactly; the others rebuild part of each link, so the fields agree within the tests' +// usual tolerance, getTolerance. Long links take reconstruct NO, 13 or 9: 12 and 8 rebuild the sign on one time +// slice, not three. +TEST_P(GaugeTBoundaryTest, apply_remove) +{ + const auto [prec, recon, link_type] = GetParam(); + if (!quda::is_enabled(prec) || (QUDA_RECONSTRUCT & getReconstructNibble(recon)) == 0) GTEST_SKIP(); + if (link_type == QUDA_THREE_LINKS && (recon == QUDA_RECONSTRUCT_12 || recon == QUDA_RECONSTRUCT_8)) GTEST_SKIP(); + const double tol = recon == QUDA_RECONSTRUCT_NO ? 0.0 : getTolerance(prec); + const auto u = random_field(prec, QUDA_PERIODIC_T, link_type); + quda::GaugeField host_signed(u); + applyGaugeTemporalBCs(host_signed, QUDA_ANTI_PERIODIC_T); + quda::GaugeField device( + field_param(QUDA_CUDA_FIELD_LOCATION, prec, QUDA_STAGGERED_PHASE_NO, QUDA_PERIODIC_T, link_type, recon)); + device.copy(u); + + device.applyTBoundary(); + EXPECT_EQ(device.TBoundary(), QUDA_ANTI_PERIODIC_T); + quda::GaugeField signed_links( + field_param(QUDA_CPU_FIELD_LOCATION, prec, QUDA_STAGGERED_PHASE_NO, QUDA_ANTI_PERIODIC_T, link_type)); + signed_links.copy(device); + expect_links(signed_links, "device signed", host_signed, "host signed", tol); + + device.removeTBoundary(); + EXPECT_EQ(device.TBoundary(), QUDA_PERIODIC_T); + quda::GaugeField plain(field_param(QUDA_CPU_FIELD_LOCATION, prec, QUDA_STAGGERED_PHASE_NO, QUDA_PERIODIC_T, link_type)); + plain.copy(device); + expect_links(plain, "device unsigned", u, "host unsigned", tol); +} + +// applying the sign to an extended copy of a field gives the extended copy of the field with the sign, halo +// included, for halo widths of 2 and 1 +TEST_P(GaugeTBoundaryTest, extended) +{ + const auto [prec, recon, link_type] = GetParam(); + if (!quda::is_enabled(prec) || recon != QUDA_RECONSTRUCT_NO) GTEST_SKIP(); + const auto u = random_field(prec, QUDA_PERIODIC_T, link_type); + quda::GaugeField plain(field_param(QUDA_CUDA_FIELD_LOCATION, prec, QUDA_STAGGERED_PHASE_NO, QUDA_PERIODIC_T, link_type)); + plain.copy(u); + quda::GaugeField applied( + field_param(QUDA_CUDA_FIELD_LOCATION, prec, QUDA_STAGGERED_PHASE_NO, QUDA_PERIODIC_T, link_type)); + applied.copy(u); + applied.applyTBoundary(); + + for (int width : {2, 1}) { + quda::lat_dim_t R; + for (int d = 0; d < 4; d++) R[d] = width * quda::comm_dim_partitioned(d); + std::unique_ptr expected(quda::createExtendedGauge(applied, R)); + std::unique_ptr extended(quda::createExtendedGauge(plain, R)); + extended->applyTBoundary(); + expect_same_links(*extended, "extended signed, halo width " + std::to_string(width), *expected, + "extended copy of signed"); + } +} + +int main(int argc, char **argv) +{ + quda_test test("Gauge phase test", argc, argv); + test.init(); + return test.execute(); +} + +using ::testing::Combine; +using ::testing::Values; + +INSTANTIATE_TEST_SUITE_P(Phase, GaugePhaseTest, + Combine(Values(QUDA_DOUBLE_PRECISION, QUDA_SINGLE_PRECISION), + Values(QUDA_STAGGERED_PHASE_MILC, QUDA_STAGGERED_PHASE_TIFR, QUDA_STAGGERED_PHASE_CHROMA), + Values(QUDA_PERIODIC_T, QUDA_ANTI_PERIODIC_T), Values(QUDA_SU3_LINKS, QUDA_THREE_LINKS)), + [](testing::TestParamInfo param) { + std::string name = get_prec_str(::testing::get<0>(param.param)); + name += std::string("_") + get_staggered_phase_str(::testing::get<1>(param.param)); + name += ::testing::get<2>(param.param) == QUDA_PERIODIC_T ? "_periodic" : "_antiperiodic"; + return ::testing::get<3>(param.param) == QUDA_THREE_LINKS ? name + "_long" : name; + }); + +INSTANTIATE_TEST_SUITE_P(Interface, GaugePhaseInterfaceTest, + Combine(Values(QUDA_DOUBLE_PRECISION, QUDA_SINGLE_PRECISION), + Values(QUDA_STAGGERED_PHASE_MILC, QUDA_STAGGERED_PHASE_TIFR, + QUDA_STAGGERED_PHASE_CHROMA)), + [](testing::TestParamInfo param) { + return std::string(get_prec_str(::testing::get<0>(param.param))) + "_" + + get_staggered_phase_str(::testing::get<1>(param.param)); + }); + +INSTANTIATE_TEST_SUITE_P(TBoundary, GaugeTBoundaryTest, + Combine(Values(QUDA_DOUBLE_PRECISION, QUDA_SINGLE_PRECISION), + Values(QUDA_RECONSTRUCT_NO, QUDA_RECONSTRUCT_13, QUDA_RECONSTRUCT_12, + QUDA_RECONSTRUCT_9, QUDA_RECONSTRUCT_8), + Values(QUDA_SU3_LINKS, QUDA_THREE_LINKS)), + [](testing::TestParamInfo param) { + std::string name = get_prec_str(::testing::get<0>(param.param)); + name += std::string("_") + get_recon_str(::testing::get<1>(param.param)); + return ::testing::get<2>(param.param) == QUDA_THREE_LINKS ? name + "_long" : name; + }); diff --git a/tests/utils/gauge_utils.cpp b/tests/utils/gauge_utils.cpp index ca83a9cdc4..fce09e3731 100644 --- a/tests/utils/gauge_utils.cpp +++ b/tests/utils/gauge_utils.cpp @@ -1,5 +1,7 @@ #include +#include #include +#include #include #include @@ -81,12 +83,58 @@ template struct ApplyGaugeTemporalBCs { * @param[in] precision Floating-point precision of the field * @param[in] three_link Whether or not this is for the asqtad long links, default false */ -void applyGaugeTemporalBCs(void *const *gauge, int Vh, QudaTboundary t_boundary, QudaPrecision precision, - bool three_link = false) +void applyGaugeTemporalBCs(void *const *gauge, int Vh, QudaTboundary t_boundary, QudaPrecision precision, bool three_link) { instantiate_host(precision, gauge, Vh, t_boundary, three_link); } +void applyGaugeTemporalBCs(quda::GaugeField &gauge, QudaTboundary t_boundary) +{ + if (gauge.Location() != QUDA_CPU_FIELD_LOCATION) errorQuda("applyGaugeTemporalBCs needs a host gauge field"); + if (gauge.GhostExchange() == QUDA_GHOST_EXCHANGE_EXTENDED) + errorQuda("applyGaugeTemporalBCs does not support extended gauge fields"); + const bool three_link = gauge.LinkType() == QUDA_THREE_LINKS; + // not raw_pointer(): it returns a static buffer that every later call overwrites + if (gauge.Order() == QUDA_QDP_GAUGE_ORDER) { + auto arrays = gauge.data_array(); + applyGaugeTemporalBCs(arrays.data, static_cast(gauge.VolumeCB()), t_boundary, gauge.Precision(), three_link); + } else { + GaugeFieldParam param(gauge); + param.order = QUDA_QDP_GAUGE_ORDER; + param.create = QUDA_NULL_FIELD_CREATE; + GaugeField u(param); + u.copy(gauge); + auto arrays = u.data_array(); + applyGaugeTemporalBCs(arrays.data, static_cast(u.VolumeCB()), t_boundary, u.Precision(), three_link); + gauge.copy(u); + } +} + +/** + * @brief Apply an imaginary chemical potential, as QUDA's staggered phases do + * + * @tparam real Floating point type of the gauge field + * @param[in,out] gauge QDP-ordered gauge field + * @param[in] Vh One-half of the local volume + * @param[in] i_mu The imaginary chemical potential + */ +template struct ApplyGaugeImaginaryChemicalPotential { + void operator()(void *const *gauge_, int Vh, double i_mu) + { + const auto gauge = typed_ptr_array(gauge_); + const double angle = M_PI * i_mu / (quda::comm_dim(3) * Z[3]); + const std::complex phase(std::cos(angle), std::sin(angle)); + const auto links = reinterpret_cast *>(gauge[3]); +#pragma omp parallel for + for (auto i = 0lu; i < 2 * Vh * (gauge_site_size / 2); i++) links[i] *= phase; + } +}; + +void applyGaugeImaginaryChemicalPotential(void *const *gauge, int Vh, double i_mu, QudaPrecision precision) +{ + instantiate_host(precision, gauge, Vh, i_mu); +} + /** * @brief Emulate imposing the temporal gauge. * @@ -231,37 +279,36 @@ real getStaggeredPhase(int x, int y, int z, int t, const int X[], int dim, } /** - * @brief Apply staggered phases to a gauge field + * @brief Apply staggered phases to a gauge field, in all four directions, without the t boundary sign * * @tparam real Floating point type of the gauge field * @param[out] gauge Generated QDP-ordered gauge field * @param[in] Vh One-half of the local volume * @param[in] X Full-parity local dimensions - * @param[in] t_boundary Temporal boundary conditions * @param[in] phase_type Staggered phase type */ template struct ApplyGaugeStaggeredPhase { - void operator()(void *const *gauge_, int Vh, const int X[], QudaTboundary t_boundary = QUDA_ANTI_PERIODIC_T, - QudaStaggeredPhase phase_type = QUDA_STAGGERED_PHASE_MILC) + void operator()(void *const *gauge_, int Vh, const int X[], QudaStaggeredPhase phase_type) { const auto gauge = typed_ptr_array(gauge_); + int dims[4] = {X[0], X[1], X[2], X[3]}; int X1 = X[0]; int X2 = X[1]; int X3 = X[2]; - // apply the staggered phases - for (int d = 0; d < 3; d++) { + // the t boundary sign is applyGaugeTemporalBCs's, so the phases are taken with a periodic t + for (int d = 0; d < 4; d++) { #pragma omp parallel for for (int i = 0; i < Vh; i++) { for (int parity = 0; parity < 2; parity++) { - int index = fullLatticeIndex(i, parity); + int index = fullLatticeIndex(dims, i, parity); int i4 = index / (X3 * X2 * X1); int i3 = (index - i4 * (X3 * X2 * X1)) / (X2 * X1); int i2 = (index - i4 * (X3 * X2 * X1) - i3 * (X2 * X1)) / X1; int i1 = index - i4 * (X3 * X2 * X1) - i3 * (X2 * X1) - i2 * X1; - real sign = getStaggeredPhase(i1, i2, i3, i4, X, d, t_boundary, phase_type); + real sign = getStaggeredPhase(i1, i2, i3, i4, X, d, QUDA_PERIODIC_T, phase_type); for (int j = 0; j < 18; j++) { gauge[d][(parity * Vh + i) * gauge_site_size + j] *= sign; } } @@ -271,9 +318,31 @@ template struct ApplyGaugeStaggeredPhase { }; void applyGaugeStaggeredPhase(void *const *gauge, int Vh, const int X[], QudaPrecision precision, - QudaTboundary t_boundary, QudaStaggeredPhase phase_type) + QudaStaggeredPhase phase_type) +{ + instantiate_host(precision, gauge, Vh, X, phase_type); +} + +void applyGaugeStaggeredPhase(quda::GaugeField &gauge, QudaStaggeredPhase phase_type) { - instantiate_host(precision, gauge, Vh, X, t_boundary, phase_type); + if (gauge.Location() != QUDA_CPU_FIELD_LOCATION) errorQuda("applyGaugeStaggeredPhase needs a host gauge field"); + if (gauge.GhostExchange() == QUDA_GHOST_EXCHANGE_EXTENDED) + errorQuda("applyGaugeStaggeredPhase does not support extended gauge fields"); + const int X[4] = {gauge.X()[0], gauge.X()[1], gauge.X()[2], gauge.X()[3]}; + // not raw_pointer(): it returns a static buffer that every later call overwrites + if (gauge.Order() == QUDA_QDP_GAUGE_ORDER) { + auto arrays = gauge.data_array(); + applyGaugeStaggeredPhase(arrays.data, static_cast(gauge.VolumeCB()), X, gauge.Precision(), phase_type); + } else { + GaugeFieldParam param(gauge); + param.order = QUDA_QDP_GAUGE_ORDER; + param.create = QUDA_NULL_FIELD_CREATE; + GaugeField u(param); + u.copy(gauge); + auto arrays = u.data_array(); + applyGaugeStaggeredPhase(arrays.data, static_cast(u.VolumeCB()), X, u.Precision(), phase_type); + gauge.copy(u); + } } void applyGaugeFieldScaling(void *const *gauge, int Vh, const QudaGaugeParam ¶m, QudaPrecision precision) @@ -526,7 +595,7 @@ GaugeInputMode resolveGaugeInputMode(GaugeInputMode default_mode) void constructHostGaugeInputField(void *const *gauge, const QudaGaugeParam &gauge_param, int argc, char **argv, GaugeInputMode default_mode, QudaStaggeredPhase phase) { - // applyGaugeStaggeredPhase only sets the spatial phases, which is the complete MILC convention + // the tests' gauge input takes MILC's phases or none if (phase != QUDA_STAGGERED_PHASE_NO && phase != QUDA_STAGGERED_PHASE_MILC) errorQuda("Unsupported staggered phase type %d for gauge input", phase); @@ -543,8 +612,7 @@ void constructHostGaugeInputField(void *const *gauge, const QudaGaugeParam &gaug } applyGaugeFieldScaling(gauge, Vh, gauge_param, gauge_param.cpu_prec); - if (phase != QUDA_STAGGERED_PHASE_NO) - applyGaugeStaggeredPhase(gauge, Vh, gauge_param.X, gauge_param.cpu_prec, QUDA_PERIODIC_T, phase); + if (phase != QUDA_STAGGERED_PHASE_NO) applyGaugeStaggeredPhase(gauge, Vh, gauge_param.X, gauge_param.cpu_prec, phase); } void constructHostGaugeInputField(quda::GaugeField &gauge, const QudaGaugeParam &gauge_param, int argc, char **argv, @@ -766,7 +834,7 @@ void applyGaugeFieldScaling_long(void *const *gauge, int Vh, const QudaGaugePara // apply staggered phases WITHOUT the temporal boundary conditions // The temporal boundary conditions are (carefully) applied in // applyGaugeTemporalBCs - applyGaugeStaggeredPhase(gauge, Vh, param.X, precision, QUDA_PERIODIC_T, QUDA_STAGGERED_PHASE_MILC); + applyGaugeStaggeredPhase(gauge, Vh, param.X, precision, QUDA_STAGGERED_PHASE_MILC); applyGaugeTemporalBCs(gauge, Vh, param.t_boundary, precision, dslash_type == QUDA_ASQTAD_DSLASH); } diff --git a/tests/utils/gauge_utils.h b/tests/utils/gauge_utils.h index 99d95c08a4..a1868e3e17 100644 --- a/tests/utils/gauge_utils.h +++ b/tests/utils/gauge_utils.h @@ -81,20 +81,61 @@ void constructQudaGaugeField(void *const *gauge, GaugeFieldConstructionType type QudaPrecision precision); /** - * @brief Apply staggered phases to a QDP-ordered gauge field + * @brief Apply the antiperiodic t boundary sign to the t links of the last global time slice, or of the last three + * for long links, of a QDP-ordered gauge field of the test's lattice + * + * @param[in,out] gauge QDP-ordered gauge field + * @param[in] Vh One-half of the local volume + * @param[in] t_boundary Temporal boundary conditions: nothing is done for periodic ones + * @param[in] precision Floating-point precision of the field + * @param[in] three_link Whether or not this is for the asqtad long links + */ +void applyGaugeTemporalBCs(void *const *gauge, int Vh, QudaTboundary t_boundary, QudaPrecision precision, + bool three_link = false); + +/** + * @brief Apply the antiperiodic t boundary sign to the t links of the last global time slice, or of the last three + * for long links, of a host gauge field of the test's lattice of any order; a field in another order than QDP goes + * through a QDP copy + * + * @param[in,out] gauge The host gauge field, not extended; its link type decides whether it holds long links + * @param[in] t_boundary Temporal boundary conditions: nothing is done for periodic ones + */ +void applyGaugeTemporalBCs(quda::GaugeField &gauge, QudaTboundary t_boundary); + +/** + * @brief Multiply every t link of a QDP-ordered gauge field of the test's lattice by the phase that an imaginary + * chemical potential gives it in QUDA's staggered phases, exp(i pi i_mu / T), T being the global time extent + * + * @param[in,out] gauge QDP-ordered gauge field + * @param[in] Vh One-half of the local volume + * @param[in] i_mu The imaginary chemical potential + * @param[in] precision Floating-point precision of the field + */ +void applyGaugeImaginaryChemicalPotential(void *const *gauge, int Vh, double i_mu, QudaPrecision precision); + +/** + * @brief Apply staggered phases to a QDP-ordered gauge field, in all four directions, without the t boundary sign, + * which applyGaugeTemporalBCs applies * - * @tparam real Floating point type of the gauge field * @param[out] gauge Generated QDP-ordered gauge field * @param[in] Vh One-half of the local volume * @param[in] X Full-parity local dimensions * @param[in] precision Gauge field floating point precision - * @param[in] t_boundary Temporal boundary conditions - * @param[in] phase_type Staggered phase type + * @param[in] phase_type Staggered phase type: MILC, TIFR or CHROMA */ void applyGaugeStaggeredPhase(void *const *gauge, int Vh, const int X[], QudaPrecision precision, - QudaTboundary t_boundary = QUDA_ANTI_PERIODIC_T, QudaStaggeredPhase phase_type = QUDA_STAGGERED_PHASE_MILC); +/** + * @brief Apply staggered phases to a host gauge field of any order, in all four directions, without the t boundary + * sign, which applyGaugeTemporalBCs applies; a field in another order than QDP goes through a QDP copy + * + * @param[in,out] gauge The host gauge field, not extended + * @param[in] phase_type Staggered phase type: MILC, TIFR or CHROMA + */ +void applyGaugeStaggeredPhase(quda::GaugeField &gauge, QudaStaggeredPhase phase_type = QUDA_STAGGERED_PHASE_MILC); + /** * @brief Apply spatial scaling, anti-periodic boundary conditions, or temporal gauge fixing as requested diff --git a/tests/utils/host_utils.cpp b/tests/utils/host_utils.cpp index 3756377172..16fbd1603d 100644 --- a/tests/utils/host_utils.cpp +++ b/tests/utils/host_utils.cpp @@ -1032,7 +1032,7 @@ void createSiteLinkCPU(void *const *gauge, QudaPrecision precision, SiteLinkType constructRandomSU3GaugeField(gauge, precision); } else if (phase == SiteLinkType::SITELINK_PHASE_MILC) { constructRandomSU3GaugeField(gauge, precision); - applyGaugeStaggeredPhase(gauge, Vh, Z, precision, QUDA_ANTI_PERIODIC_T, QUDA_STAGGERED_PHASE_MILC); + applyGaugeStaggeredPhase(gauge, Vh, Z, precision, QUDA_STAGGERED_PHASE_MILC); } else if (phase == SiteLinkType::SITELINK_PHASE_U1) { constructRandomSU3GaugeField(gauge, precision); applyRandomU1Phase(gauge, precision); diff --git a/tests/utils/host_utils.h b/tests/utils/host_utils.h index 78b4934b00..22b12c0ef2 100644 --- a/tests/utils/host_utils.h +++ b/tests/utils/host_utils.h @@ -278,7 +278,8 @@ int strong_check_link(const quda::GaugeField &linkA, const std::string &msgA, co const std::string &msgB); /** - @brief Compare two non-extended QDP-ordered host gauge fields element-wise and by accuracy level + @brief Compare two QDP-ordered host gauge fields of the same layout, extended or not, element-wise and by + accuracy level @param[in] result Field under test @param[in] msg_result Label printed for result in verbose output @param[in] expected Reference field diff --git a/tests/utils/misc.cpp b/tests/utils/misc.cpp index 02b7471a77..1299f18147 100644 --- a/tests/utils/misc.cpp +++ b/tests/utils/misc.cpp @@ -66,6 +66,21 @@ const char *get_gauge_order_str(QudaGaugeFieldOrder order) return ret; } +const char *get_staggered_phase_str(QudaStaggeredPhase phase) +{ + const char *ret; + + switch (phase) { + case QUDA_STAGGERED_PHASE_NO: ret = "no"; break; + case QUDA_STAGGERED_PHASE_MILC: ret = "milc"; break; + case QUDA_STAGGERED_PHASE_CHROMA: ret = "chroma"; break; + case QUDA_STAGGERED_PHASE_TIFR: ret = "tifr"; break; + default: ret = "unknown"; break; + } + + return ret; +} + const char *get_recon_str(QudaReconstructType recon) { const char *ret; diff --git a/tests/utils/misc.h b/tests/utils/misc.h index 2b16c826dc..0be7052944 100644 --- a/tests/utils/misc.h +++ b/tests/utils/misc.h @@ -7,6 +7,7 @@ const char *get_quda_ver_str(); const char *get_recon_str(QudaReconstructType recon); const char *get_prec_str(QudaPrecision prec); const char *get_gauge_order_str(QudaGaugeFieldOrder order); +const char *get_staggered_phase_str(QudaStaggeredPhase phase); const char *get_test_type(int t); const char *get_unitarization_str(bool svd_only); const char *get_mass_normalization_str(QudaMassNormalization);