From ee86d150a54ce61f6c9197ddcb03a0451ac3776a Mon Sep 17 00:00:00 2001 From: Evan Weinberg Date: Sun, 22 Nov 2026 23:03:24 -0800 Subject: [PATCH 01/19] Apply the test utilities' staggered phases in all four directions without the t boundary sign, and on any host gauge field --- tests/utils/gauge_utils.cpp | 48 ++++++++++++++++++++++++++----------- tests/utils/gauge_utils.h | 17 +++++++++---- tests/utils/host_utils.cpp | 2 +- 3 files changed, 47 insertions(+), 20 deletions(-) diff --git a/tests/utils/gauge_utils.cpp b/tests/utils/gauge_utils.cpp index ca83a9cdc4..a438e3175b 100644 --- a/tests/utils/gauge_utils.cpp +++ b/tests/utils/gauge_utils.cpp @@ -231,37 +231,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 +270,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, t_boundary, phase_type); + instantiate_host(precision, gauge, Vh, X, phase_type); +} + +void applyGaugeStaggeredPhase(quda::GaugeField &gauge, QudaStaggeredPhase 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 +547,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 +564,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 +786,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..c086efd5da 100644 --- a/tests/utils/gauge_utils.h +++ b/tests/utils/gauge_utils.h @@ -81,20 +81,27 @@ void constructQudaGaugeField(void *const *gauge, GaugeFieldConstructionType type QudaPrecision precision); /** - * @brief Apply staggered phases to a QDP-ordered gauge field + * @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); From 87938305a6a289780acfa3a06334f2a8c0c15926 Mon Sep 17 00:00:00 2001 From: Evan Weinberg Date: Sun, 22 Nov 2026 23:09:56 -0800 Subject: [PATCH 02/19] Add gauge_phase_test for the staggered phases of device, extended and interface-loaded gauge fields --- tests/CMakeLists.txt | 10 + tests/gauge_phase_test.cpp | 418 +++++++++++++++++++++++++++++++++++++ 2 files changed, 428 insertions(+) create mode 100644 tests/gauge_phase_test.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index f560d040cb..9253291677 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,11 @@ 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) + 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..5968e04e1b --- /dev/null +++ b/tests/gauge_phase_test.cpp @@ -0,0 +1,418 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "host_utils.h" +#include "gauge_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, +// through applyGaugeFieldScaling): the MILC, TIFR or CHROMA convention, times the antiperiodic sign on the t links +// of the last global t slice. The links are Haar-random, and the fields must agree exactly. 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. + +namespace +{ + + /** + @brief Parameters for a gauge field of the test's lattice in double or single precision, without + reconstruct or 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 + @return The parameters + */ + quda::GaugeFieldParam field_param(QudaFieldLocation location, QudaPrecision prec, QudaStaggeredPhase phase, + QudaTboundary t_boundary) + { + QudaGaugeParam gauge_param = newQudaGaugeParam(); + setWilsonGaugeParam(gauge_param); + gauge_param.cpu_prec = prec; + gauge_param.t_boundary = t_boundary; + gauge_param.staggered_phase_type = phase; + gauge_param.staggered_phase_applied = false; + quda::GaugeFieldParam param(gauge_param); + param.location = location; + param.create = QUDA_NULL_FIELD_CREATE; + param.ghostExchange = QUDA_GHOST_EXCHANGE_NO; + param.reconstruct = 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 + @return The field + */ + quda::GaugeField random_field(QudaPrecision prec, QudaTboundary t_boundary) + { + 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)); + 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 change sign + @return The phased links, as a host field in QDP order recording t_boundary + */ + quda::GaugeField host_phased(const quda::GaugeField &u, QudaStaggeredPhase type, QudaTboundary t_boundary) + { + quda::GaugeField phased(field_param(QUDA_CPU_FIELD_LOCATION, u.Precision(), QUDA_STAGGERED_PHASE_NO, t_boundary)); + for (int dir = 0; dir < 4; dir++) memcpy(phased.data(dir), u.data(dir), u.Volume() * 18 * u.Precision()); + applyGaugeStaggeredPhase(phased, type); + + // with no anisotropy and no gauge fixing, applyGaugeFieldScaling only applies the t boundary sign + QudaGaugeParam gauge_param = newQudaGaugeParam(); + setWilsonGaugeParam(gauge_param); + gauge_param.anisotropy = 1.0; + gauge_param.gauge_fix = QUDA_GAUGE_FIXED_NO; + gauge_param.t_boundary = t_boundary; + auto arrays = phased.data_array(); + applyGaugeFieldScaling(arrays.data, static_cast(phased.VolumeCB()), gauge_param, phased.Precision()); + 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 + */ + void expect_links(const quda::GaugeField &result, const std::string &msg_result, const quda::GaugeField &expected, + const std::string &msg_expected) + { + int fail = compare_link_fields(result, msg_result, expected, msg_expected, 0.0).first ? 0 : 1; + quda::comm_allreduce_max(fail); + EXPECT_EQ(fail, 0) << msg_result << " links differ from the " << msg_expected << " links"; + } + + /** + @brief Whether two fields hold the same links on every rank, compared on the host in QDP order, printing the + first few that differ. Collective. + @param[in] a A field + @param[in] b Another, with the same parameters + @return Whether they are equal + */ + bool same_links(const quda::GaugeField &a, const quda::GaugeField &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); + + // sites by their coordinates in the field, halo included; R is the halo width of an extended field + const auto &E = ha.X(); + const auto &R = ha.R(); + const size_t volume = ha.Volume(); + const size_t site_bytes = 18 * ha.Precision(); + size_t wrong = 0; + for (int t = 0; t < E[3]; t++) + for (int z = 0; z < E[2]; z++) + for (int y = 0; y < E[1]; y++) + for (int x = 0; x < E[0]; x++) { + const size_t lex = x + E[0] * (y + static_cast(E[1]) * (z + static_cast(E[2]) * t)); + const size_t index = (x + y + z + t) % 2 == 0 ? lex / 2 : (lex + volume) / 2; + for (int dir = 0; dir < 4; dir++) { + const auto pa = static_cast(ha.data(dir)) + index * site_bytes; + const auto pb = static_cast(hb.data(dir)) + index * site_bytes; + if (memcmp(pa, pb, site_bytes) != 0 && wrong++ < 4) + printf("rank %d: link dir %d at (%d %d %d %d) in the field, (%d %d %d %d) in the interior, differs\n", + quda::comm_rank(), dir, x, y, z, t, x - R[0], y - R[1], z - R[2], t - R[3]); + } + } + quda::comm_allreduce_sum(wrong); + return wrong == 0; + } + + /** + @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; + } + + /** + @brief The largest difference between two momentum fields over all ranks. Collective. + @tparam real double or float + @param[in] a A host momentum field in QDP order + @param[in] b Another, with the same parameters + @return The largest absolute difference of any real + */ + template double max_mom_deviation(const quda::GaugeField &a, const quda::GaugeField &b) + { + const size_t reals = a.Volume() * a.Reconstruct(); + double dev = 0.0; + for (int dir = 0; dir < 4; dir++) { + const auto pa = a.data(dir); + const auto pb = b.data(dir); + for (size_t i = 0; i < reals; i++) dev = std::max(dev, std::abs(static_cast(pa[i]) - pb[i])); + } + quda::comm_allreduce_max(dev); + return dev; + } + + /** + @param[in] type A staggered phase convention + @return Its name in test names + */ + const char *get_phase_str(QudaStaggeredPhase type) + { + switch (type) { + case QUDA_STAGGERED_PHASE_MILC: return "milc"; + case QUDA_STAGGERED_PHASE_TIFR: return "tifr"; + case QUDA_STAGGERED_PHASE_CHROMA: return "chroma"; + default: return "invalid"; + } + } + +} // 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] = GetParam(); + if (!quda::is_enabled(prec)) GTEST_SKIP(); + const auto u = random_field(prec, t_boundary); + quda::GaugeField device(field_param(QUDA_CUDA_FIELD_LOCATION, prec, type, t_boundary)); + 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)); + 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"); +} + +// 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] = GetParam(); + if (!quda::is_enabled(prec)) GTEST_SKIP(); + const auto u = random_field(prec, t_boundary); + quda::GaugeField plain(field_param(QUDA_CUDA_FIELD_LOCATION, prec, type, t_boundary)); + plain.copy(u); + quda::GaugeField phased(field_param(QUDA_CUDA_FIELD_LOCATION, prec, type, t_boundary)); + phased.copy(u); + phased.applyStaggeredPhase(type); + + // the halo widths of the interface's extended fields + quda::lat_dim_t R; + for (int d = 0; d < 4; d++) R[d] = 2 * quda::comm_dim_partitioned(d); + std::unique_ptr expected(quda::createExtendedGauge(plain, R)); + std::unique_ptr extended(quda::createExtendedGauge(phased, R)); + extended->removeStaggeredPhase(); + EXPECT_TRUE(same_links(*extended, *expected)); +} + +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); + const double dev = prec == QUDA_DOUBLE_PRECISION ? max_mom_deviation(phased, plain) : + max_mom_deviation(phased, plain); + EXPECT_EQ(dev, 0.0); + + quda::GaugeFieldParam zero_param(plain); + zero_param.create = QUDA_ZERO_FIELD_CREATE; + const quda::GaugeField zero(zero_param); + const double size + = prec == QUDA_DOUBLE_PRECISION ? max_mom_deviation(plain, zero) : max_mom_deviation(plain, zero); + EXPECT_GT(size, 0.0) << "the force is zero, so the comparison shows nothing"; +} + +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)), + [](testing::TestParamInfo param) { + std::string name = get_prec_str(::testing::get<0>(param.param)); + name += std::string("_") + get_phase_str(::testing::get<1>(param.param)); + return name + + (::testing::get<2>(param.param) == QUDA_PERIODIC_T ? "_periodic" : "_antiperiodic"); + }); + +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_phase_str(::testing::get<1>(param.param)); + }); From af3f616b1bf1a9f21edfa685105fe123aa5e632e Mon Sep 17 00:00:00 2001 From: Evan Weinberg Date: Sun, 22 Nov 2026 23:30:30 -0800 Subject: [PATCH 03/19] Take the staggered phases and t boundary sign of every site of a gauge field, halo included, from its global coordinates --- include/kernels/gauge_phase.cuh | 40 +++++++++++++++++++++++---------- tests/gauge_phase_test.cpp | 17 ++++++++------ 2 files changed, 38 insertions(+), 19 deletions(-) diff --git a/include/kernels/gauge_phase.cuh b/include/kernels/gauge_phase.cuh index 8bbc2be978..990a6d32ca 100644 --- a/include/kernels/gauge_phase.cuh +++ b/include/kernels/gauge_phase.cuh @@ -18,7 +18,12 @@ namespace quda { typedef typename gauge_mapper::type Gauge; Gauge u; - int X[4]; + int_fastdiv E[4]; // extended grid dims + int_fastdiv X[4]; // regular 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 real tBoundary; real i_mu; complex i_mu_phase; @@ -28,18 +33,26 @@ namespace quda { // 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))))); + parity_shift = 0; + for (int d = 0; d < 4; d++) { + E[d] = u.X()[d]; + border[d] = u.R()[d]; + X[d] = E[d] - 2 * border[d]; + offset[d] = comm_coord(d) * X[d]; + G[d] = comm_dim(d) * X[d]; + parity_shift += border[d]; + } + parity_shift &= 1; - for (int d=0; d<4; d++) X[d] = u.X()[d]; + 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))); - // 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 + // 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) { @@ -50,7 +63,7 @@ namespace quda { } 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 = (t == arg.G[3] - 1) ? arg.tBoundary : typename Arg::real(1.0); } } else if (Arg::phase == QUDA_STAGGERED_PHASE_TIFR) { if (dim==0) { @@ -60,7 +73,7 @@ namespace quda { } 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 = (t == arg.G[3] - 1) ? 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 @@ -71,7 +84,7 @@ namespace quda { } 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 = ((t == arg.G[3] - 1) ? arg.tBoundary : typename Arg::real(1.0)) * (1 - 2 * ((1 + x + y + z) % 2)); } } return phase; @@ -81,8 +94,11 @@ namespace quda { __device__ __host__ void gaugePhase(int indexCB, int parity, const Arg &arg) { using real = typename Arg::real; + // every site of the field, halo included, which wraps around the global lattice int x[4]; - getCoords(x, indexCB, arg.X, parity); + getCoords(x, indexCB, arg.E, (parity + arg.parity_shift) & 1); +#pragma unroll + for (int d = 0; d < 4; d++) x[d] = (x[d] - arg.border[d] + arg.offset[d] + arg.G[d]) % arg.G[d]; real phase = getPhase(x[0], x[1], x[2], x[3], arg); Matrix,Arg::nColor> u = arg.u(dim, indexCB, parity); diff --git a/tests/gauge_phase_test.cpp b/tests/gauge_phase_test.cpp index 5968e04e1b..4df97177ff 100644 --- a/tests/gauge_phase_test.cpp +++ b/tests/gauge_phase_test.cpp @@ -327,13 +327,16 @@ TEST_P(GaugePhaseTest, extended_remove) phased.copy(u); phased.applyStaggeredPhase(type); - // the halo widths of the interface's extended fields - quda::lat_dim_t R; - for (int d = 0; d < 4; d++) R[d] = 2 * quda::comm_dim_partitioned(d); - std::unique_ptr expected(quda::createExtendedGauge(plain, R)); - std::unique_ptr extended(quda::createExtendedGauge(phased, R)); - extended->removeStaggeredPhase(); - EXPECT_TRUE(same_links(*extended, *expected)); + // 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_TRUE(same_links(*extended, *expected)) << "halo width " << width; + } } using interface_test_t = ::testing::tuple; From b35f916e03d4763b7e92f238cef16749a3e5a089 Mon Sep 17 00:00:00 2001 From: Evan Weinberg Date: Sun, 22 Nov 2026 23:37:28 -0800 Subject: [PATCH 04/19] Label MILC's phased links antiperiodic for the gauge observables, so that their Polyakov loop needs no negation --- lib/milc_interface.cpp | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) 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; From b853dd38748f0b2fdc8db5614ffc537c06f84144 Mon Sep 17 00:00:00 2001 From: Evan Weinberg Date: Sun, 22 Nov 2026 23:44:21 -0800 Subject: [PATCH 05/19] Apply the antiperiodic t sign to the last three time slices of long links, and reject an imaginary chemical potential on them --- include/kernels/gauge_phase.cuh | 44 +++++++++++++------------ lib/gauge_phase.cu | 3 ++ tests/gauge_phase_test.cpp | 57 ++++++++++++++++----------------- tests/utils/gauge_utils.cpp | 3 +- tests/utils/gauge_utils.h | 13 ++++++++ 5 files changed, 69 insertions(+), 51 deletions(-) diff --git a/include/kernels/gauge_phase.cuh b/include/kernels/gauge_phase.cuh index 990a6d32ca..7966a585ba 100644 --- a/include/kernels/gauge_phase.cuh +++ b/include/kernels/gauge_phase.cuh @@ -5,7 +5,8 @@ #include #include -namespace quda { +namespace quda +{ template struct GaugePhaseArg : kernel_param<> { @@ -15,15 +16,16 @@ namespace quda { 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_fastdiv E[4]; // extended grid dims int_fastdiv X[4]; // regular 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 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 real tBoundary; real i_mu; complex i_mu_phase; @@ -48,50 +50,53 @@ namespace quda { i_mu_phase = complex(static_cast(cos(M_PI * double(u.iMu()) / T)), dir * static_cast(sin(M_PI * double(u.iMu()) / T))); + boundary_slices = u.LinkType() == QUDA_THREE_LINKS ? 3 : 1; tBoundary = static_cast(static_cast(u.TBoundary())); } }; // 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) { + 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.G[3] - 1) ? arg.tBoundary : typename Arg::real(1.0); + phase = (t >= arg.G[3] - arg.boundary_slices) ? 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.G[3] - 1) ? arg.tBoundary : typename Arg::real(1.0); + phase = (t >= arg.G[3] - arg.boundary_slices) ? 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.G[3] - 1) ? arg.tBoundary : typename Arg::real(1.0)) * (1 - 2 * ((1 + x + y + z) % 2)); + phase = ((t >= arg.G[3] - arg.boundary_slices) ? 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) { + template __device__ __host__ void gaugePhase(int indexCB, int parity, const Arg &arg) + { using real = typename Arg::real; // every site of the field, halo included, which wraps around the global lattice @@ -101,7 +106,7 @@ namespace quda { for (int d = 0; d < 4; d++) x[d] = (x[d] - arg.border[d] + arg.offset[d] + arg.G[d]) % arg.G[d]; 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 @@ -110,10 +115,9 @@ namespace quda { arg.u(dim, indexCB, parity) = u; } - template struct GaugePhase - { + template struct GaugePhase { const Arg &arg; - constexpr GaugePhase(const Arg &arg) : 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) @@ -125,4 +129,4 @@ namespace quda { } }; -} +} // namespace quda diff --git a/lib/gauge_phase.cu b/lib/gauge_phase.cu index 52e47dad71..c0f84a3174 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 diff --git a/tests/gauge_phase_test.cpp b/tests/gauge_phase_test.cpp index 4df97177ff..adedf53a3c 100644 --- a/tests/gauge_phase_test.cpp +++ b/tests/gauge_phase_test.cpp @@ -18,9 +18,9 @@ #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, -// through applyGaugeFieldScaling): the MILC, TIFR or CHROMA convention, times the antiperiodic sign on the t links -// of the last global t slice. The links are Haar-random, and the fields must agree exactly. Extended fields are +// 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. 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 @@ -38,13 +38,15 @@ namespace @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 @return The parameters */ quda::GaugeFieldParam field_param(QudaFieldLocation location, QudaPrecision prec, QudaStaggeredPhase phase, - QudaTboundary t_boundary) + QudaTboundary t_boundary, QudaLinkType link_type = QUDA_SU3_LINKS) { 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; @@ -63,16 +65,17 @@ namespace @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) + 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)); + 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; } @@ -82,23 +85,19 @@ namespace 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 change sign - @return The phased links, as a host field in QDP order recording t_boundary + @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 in QDP order of u's link type recording t_boundary */ quda::GaugeField host_phased(const quda::GaugeField &u, QudaStaggeredPhase type, QudaTboundary t_boundary) { - quda::GaugeField phased(field_param(QUDA_CPU_FIELD_LOCATION, u.Precision(), QUDA_STAGGERED_PHASE_NO, t_boundary)); + quda::GaugeField phased( + field_param(QUDA_CPU_FIELD_LOCATION, u.Precision(), QUDA_STAGGERED_PHASE_NO, t_boundary, u.LinkType())); for (int dir = 0; dir < 4; dir++) memcpy(phased.data(dir), u.data(dir), u.Volume() * 18 * u.Precision()); applyGaugeStaggeredPhase(phased, type); - - // with no anisotropy and no gauge fixing, applyGaugeFieldScaling only applies the t boundary sign - QudaGaugeParam gauge_param = newQudaGaugeParam(); - setWilsonGaugeParam(gauge_param); - gauge_param.anisotropy = 1.0; - gauge_param.gauge_fix = QUDA_GAUGE_FIXED_NO; - gauge_param.t_boundary = t_boundary; auto arrays = phased.data_array(); - applyGaugeFieldScaling(arrays.data, static_cast(phased.VolumeCB()), gauge_param, phased.Precision()); + applyGaugeTemporalBCs(arrays.data, static_cast(phased.VolumeCB()), t_boundary, phased.Precision(), + u.LinkType() == QUDA_THREE_LINKS); return phased; } @@ -287,7 +286,7 @@ namespace } // namespace -using test_t = ::testing::tuple; +using test_t = ::testing::tuple; class GaugePhaseTest : public ::testing::TestWithParam { @@ -296,16 +295,16 @@ 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] = GetParam(); + const auto [prec, type, t_boundary, link_type] = GetParam(); if (!quda::is_enabled(prec)) GTEST_SKIP(); - const auto u = random_field(prec, t_boundary); - quda::GaugeField device(field_param(QUDA_CUDA_FIELD_LOCATION, prec, type, t_boundary)); + 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)); + 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"); @@ -318,12 +317,12 @@ TEST_P(GaugePhaseTest, apply_remove) // 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] = GetParam(); + const auto [prec, type, t_boundary, link_type] = GetParam(); if (!quda::is_enabled(prec)) GTEST_SKIP(); - const auto u = random_field(prec, t_boundary); - quda::GaugeField plain(field_param(QUDA_CUDA_FIELD_LOCATION, prec, type, t_boundary)); + 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)); + quda::GaugeField phased(field_param(QUDA_CUDA_FIELD_LOCATION, prec, type, t_boundary, link_type)); phased.copy(u); phased.applyStaggeredPhase(type); @@ -403,12 +402,12 @@ 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_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_phase_str(::testing::get<1>(param.param)); - return name - + (::testing::get<2>(param.param) == QUDA_PERIODIC_T ? "_periodic" : "_antiperiodic"); + 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, diff --git a/tests/utils/gauge_utils.cpp b/tests/utils/gauge_utils.cpp index a438e3175b..06b83667ce 100644 --- a/tests/utils/gauge_utils.cpp +++ b/tests/utils/gauge_utils.cpp @@ -81,8 +81,7 @@ 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); } diff --git a/tests/utils/gauge_utils.h b/tests/utils/gauge_utils.h index c086efd5da..13ed64661c 100644 --- a/tests/utils/gauge_utils.h +++ b/tests/utils/gauge_utils.h @@ -80,6 +80,19 @@ void constructHostGaugeField(quda::GaugeField &gauge, const QudaGaugeParam &gaug void constructQudaGaugeField(void *const *gauge, GaugeFieldConstructionType type, const QudaGaugeParam ¶m, QudaPrecision precision); +/** + * @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 staggered phases to a QDP-ordered gauge field, in all four directions, without the t boundary sign, * which applyGaugeTemporalBCs applies From 9935de43dd5a0be25dd7aff49c0f6275af678191 Mon Sep 17 00:00:00 2001 From: Evan Weinberg Date: Mon, 23 Nov 2026 00:10:28 -0800 Subject: [PATCH 06/19] Add GaugeField::applyTBoundary and removeTBoundary, which put the antiperiodic t sign into the links of fields without staggered phases and take it out --- include/gauge_field.h | 22 +++++ include/kernels/gauge_phase.cuh | 118 +++++++++++++++++++------- lib/gauge_field.cpp | 40 +++++++++ lib/gauge_phase.cu | 32 ++++++++ tests/gauge_phase_test.cpp | 141 ++++++++++++++++++++++++++++++-- 5 files changed, 316 insertions(+), 37 deletions(-) diff --git a/include/gauge_field.h b/include/gauge_field.h index e00b79aca8..3e55193cb5 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, 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,13 @@ 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 + */ + 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/gauge_phase.cuh b/include/kernels/gauge_phase.cuh index 7966a585ba..96aed4ad3d 100644 --- a/include/kernels/gauge_phase.cuh +++ b/include/kernels/gauge_phase.cuh @@ -8,17 +8,11 @@ namespace quda { - template - struct GaugePhaseArg : kernel_param<> { - 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; - - Gauge u; + /** + @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_fastdiv X[4]; // regular grid dims int border[4]; @@ -26,15 +20,9 @@ namespace quda 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 - 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())) - { - // 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); + GaugePhaseGeometry(const GaugeField &u) + { parity_shift = 0; for (int d = 0; d < 4; d++) { E[d] = u.X()[d]; @@ -45,12 +33,50 @@ namespace quda parity_shift += border[d]; } parity_shift &= 1; + boundary_slices = u.LinkType() == QUDA_THREE_LINKS ? 3 : 1; + } + }; + + /** + @brief The global coordinates of a site of a gauge field, halo included, which wraps around the global lattice + @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 + @param[in] arg The field's geometry + */ + template + __device__ __host__ inline void getGlobalCoords(int x[4], int x_cb, int parity, const Arg &arg) + { + getCoords(x, x_cb, arg.E, (parity + arg.parity_shift) & 1); +#pragma unroll + for (int d = 0; d < 4; d++) x[d] = (x[d] - arg.border[d] + arg.offset[d] + arg.G[d]) % arg.G[d]; + } + + template + 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; + + Gauge u; + real tBoundary; + real i_mu; + complex i_mu_phase; + 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); 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))); - boundary_slices = u.LinkType() == QUDA_THREE_LINKS ? 3 : 1; tBoundary = static_cast(static_cast(u.TBoundary())); } }; @@ -95,16 +121,11 @@ namespace quda return phase; } - template __device__ __host__ void gaugePhase(int indexCB, int parity, const Arg &arg) + template + __device__ __host__ void gaugePhase(const int x[4], int indexCB, int parity, const Arg &arg) { using real = typename Arg::real; - // every site of the field, halo included, which wraps around the global lattice - int x[4]; - getCoords(x, indexCB, arg.E, (parity + arg.parity_shift) & 1); -#pragma unroll - for (int d = 0; d < 4; d++) x[d] = (x[d] - arg.border[d] + arg.offset[d] + arg.G[d]) % arg.G[d]; - real phase = getPhase(x[0], x[1], x[2], x[3], arg); Matrix, Arg::nColor> u = arg.u(dim, indexCB, parity); u *= phase; @@ -122,10 +143,45 @@ namespace quda __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]; + getGlobalCoords(x, x_cb, parity, arg); + 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 GaugeTBoundary(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]; + getGlobalCoords(x, x_cb, parity, arg); + if (x[3] < arg.G[3] - arg.boundary_slices) return; + Matrix, Arg::nColor> u = arg.u(3, x_cb, parity); + u *= static_cast(-1.0); + arg.u(3, x_cb, parity) = u; } }; diff --git a/lib/gauge_field.cpp b/lib/gauge_field.cpp index f260e7e22c..b82c781a71 100644 --- a/lib/gauge_field.cpp +++ b/lib/gauge_field.cpp @@ -406,6 +406,46 @@ 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()); + if ((u.Reconstruct() == QUDA_RECONSTRUCT_12 || u.Reconstruct() == QUDA_RECONSTRUCT_8) && u.iMu() != 0.0) + errorQuda("%s: reconstruct %d cannot hold the imaginary chemical potential (i_mu = %g)", name, u.Reconstruct(), + static_cast(u.iMu())); + } + + 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 c0f84a3174..b08f8eea46 100644 --- a/lib/gauge_phase.cu +++ b/lib/gauge_phase.cu @@ -55,4 +55,36 @@ namespace quda { getProfile().TPSTOP(QUDA_PROFILE_COMPUTE); } + template class GaugeTBoundary_ : TunableKernel2D + { + GaugeField &u; // used for meta data only + unsigned int minThreads() const { return u.VolumeCB(); } + + public: + GaugeTBoundary_(GaugeField &u) : TunableKernel2D(u, 2), u(u) + { + 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 2 * u.Bytes() / 4; } + }; + + void applyGaugeTBoundary(GaugeField &u) + { + getProfile().TPSTART(QUDA_PROFILE_COMPUTE); + instantiate(u); + getProfile().TPSTOP(QUDA_PROFILE_COMPUTE); + } + } // namespace quda diff --git a/tests/gauge_phase_test.cpp b/tests/gauge_phase_test.cpp index adedf53a3c..c3f4796413 100644 --- a/tests/gauge_phase_test.cpp +++ b/tests/gauge_phase_test.cpp @@ -27,22 +27,26 @@ // 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 - reconstruct or ghost zone: QDP order on the host, native order on the device + @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 @return The parameters */ quda::GaugeFieldParam field_param(QudaFieldLocation location, QudaPrecision prec, QudaStaggeredPhase phase, - QudaTboundary t_boundary, QudaLinkType link_type = QUDA_SU3_LINKS) + QudaTboundary t_boundary, QudaLinkType link_type = QUDA_SU3_LINKS, + QudaReconstructType recon = QUDA_RECONSTRUCT_NO) { QudaGaugeParam gauge_param = newQudaGaugeParam(); setWilsonGaugeParam(gauge_param); @@ -55,7 +59,7 @@ namespace param.location = location; param.create = QUDA_NULL_FIELD_CREATE; param.ghostExchange = QUDA_GHOST_EXCHANGE_NO; - param.reconstruct = QUDA_RECONSTRUCT_NO; + param.reconstruct = location == QUDA_CUDA_FIELD_LOCATION ? recon : QUDA_RECONSTRUCT_NO; param.setPrecision(prec, location == QUDA_CUDA_FIELD_LOCATION); return param; } @@ -108,11 +112,12 @@ namespace @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) + const std::string &msg_expected, double tol = 0.0) { - int fail = compare_link_fields(result, msg_result, expected, msg_expected, 0.0).first ? 0 : 1; + 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"; } @@ -274,6 +279,57 @@ namespace @param[in] type A staggered phase convention @return Its name in test names */ + /** + @param[in] recon A reconstruct + @return Whether this build instantiates it + */ + bool recon_enabled(QudaReconstructType recon) + { + switch (recon) { + case QUDA_RECONSTRUCT_NO: return quda::is_enabled(); + case QUDA_RECONSTRUCT_13: return quda::is_enabled(); + case QUDA_RECONSTRUCT_12: return quda::is_enabled(); + case QUDA_RECONSTRUCT_9: return quda::is_enabled(); + case QUDA_RECONSTRUCT_8: return quda::is_enabled(); + default: return false; + } + } + + /** + @brief A host field's links with the t links that cross the t boundary negated, by the test utilities + @param[in] u The links, in QDP order + @param[in] t_boundary The t boundary the returned field records + @return The links, as a host field in QDP order of u's link type + */ + quda::GaugeField negate_boundary(const quda::GaugeField &u, QudaTboundary t_boundary) + { + quda::GaugeField v( + field_param(QUDA_CPU_FIELD_LOCATION, u.Precision(), QUDA_STAGGERED_PHASE_NO, t_boundary, u.LinkType())); + for (int dir = 0; dir < 4; dir++) memcpy(v.data(dir), u.data(dir), u.Volume() * 18 * u.Precision()); + auto arrays = v.data_array(); + applyGaugeTemporalBCs(arrays.data, static_cast(v.VolumeCB()), QUDA_ANTI_PERIODIC_T, v.Precision(), + u.LinkType() == QUDA_THREE_LINKS); + return v; + } + + /** + @brief A host field's links as a device field with a reconstruct stores them: packed into the device field and + unpacked again, under the host field's t boundary + @param[in] u The links, in QDP order + @param[in] recon The reconstruct + @return The links, as a host field in QDP order like u + */ + quda::GaugeField stored(const quda::GaugeField &u, QudaReconstructType recon) + { + quda::GaugeField device(field_param(QUDA_CUDA_FIELD_LOCATION, u.Precision(), QUDA_STAGGERED_PHASE_NO, u.TBoundary(), + u.LinkType(), recon)); + device.copy(u); + quda::GaugeField v( + field_param(QUDA_CPU_FIELD_LOCATION, u.Precision(), QUDA_STAGGERED_PHASE_NO, u.TBoundary(), u.LinkType())); + v.copy(device); + return v; + } + const char *get_phase_str(QudaStaggeredPhase type) { switch (type) { @@ -389,6 +445,68 @@ TEST_P(GaugePhaseInterfaceTest, gauge_force) EXPECT_GT(size, 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 and 12 store the negated +// links exactly, so the fields must agree exactly with the links as the reconstruct stores them, u_r. Reconstruct +// 8, 13 and 9 store phase angles, which negating a link shifts by pi, so the fields agree with the links within the +// tests' usual tolerance, getTolerance. +TEST_P(GaugeTBoundaryTest, apply_remove) +{ + const auto [prec, recon, link_type] = GetParam(); + if (!quda::is_enabled(prec) || !recon_enabled(recon)) GTEST_SKIP(); + if (link_type == QUDA_THREE_LINKS && recon != QUDA_RECONSTRUCT_NO) GTEST_SKIP(); + const bool exact = recon == QUDA_RECONSTRUCT_NO || recon == QUDA_RECONSTRUCT_12; + const double tol = exact ? 0.0 : getTolerance(prec); + const auto u = random_field(prec, QUDA_PERIODIC_T, link_type); + const auto reference = exact ? stored(u, recon) : u; + 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", negate_boundary(reference, QUDA_ANTI_PERIODIC_T), "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", reference, "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_TRUE(same_links(*extended, *expected)) << "halo width " << width; + } +} + int main(int argc, char **argv) { quda_test test("Gauge phase test", argc, argv); @@ -418,3 +536,14 @@ INSTANTIATE_TEST_SUITE_P(Interface, GaugePhaseInterfaceTest, return std::string(get_prec_str(::testing::get<0>(param.param))) + "_" + get_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; + }); From c80d6b4b9e8c15205fb66aa3062327385daa2dcf Mon Sep 17 00:00:00 2001 From: Evan Weinberg Date: Mon, 23 Nov 2026 11:15:41 -0800 Subject: [PATCH 07/19] Keep the regular grid dims local to GaugePhaseGeometry's constructor, and take whether a time slice's t links cross the t boundary from one GaugePhaseGeometry::crossesTBoundary --- include/kernels/gauge_phase.cuh | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/include/kernels/gauge_phase.cuh b/include/kernels/gauge_phase.cuh index 96aed4ad3d..730ba2a4fd 100644 --- a/include/kernels/gauge_phase.cuh +++ b/include/kernels/gauge_phase.cuh @@ -14,7 +14,6 @@ namespace quda */ struct GaugePhaseGeometry { int_fastdiv E[4]; // extended grid dims - int_fastdiv X[4]; // regular grid dims int border[4]; int offset[4]; // global coordinates of the rank's first regular site int_fastdiv G[4]; // global grid dims @@ -27,14 +26,20 @@ namespace quda for (int d = 0; d < 4; d++) { E[d] = u.X()[d]; border[d] = u.R()[d]; - X[d] = E[d] - 2 * border[d]; - offset[d] = comm_coord(d) * X[d]; - G[d] = comm_dim(d) * X[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; } }; /** @@ -93,7 +98,7 @@ namespace quda } else if (dim == 2) { phase = (1 - 2 * ((t + x + y) % 2)); } else if (dim == 3) { // also apply boundary condition - phase = (t >= arg.G[3] - arg.boundary_slices) ? 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) { @@ -103,7 +108,7 @@ namespace quda } else if (dim == 2) { phase = (1 - 2 * ((1 + t) % 2)); } else if (dim == 3) { // also apply boundary condition - phase = (t >= arg.G[3] - arg.boundary_slices) ? 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 @@ -114,8 +119,7 @@ namespace quda } else if (dim == 2) { phase = (1 - 2 * ((1 + x + y) % 2)); } else if (dim == 3) { // also apply boundary condition - phase = ((t >= arg.G[3] - arg.boundary_slices) ? 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; @@ -178,7 +182,7 @@ namespace quda { int x[4]; getGlobalCoords(x, x_cb, parity, arg); - if (x[3] < arg.G[3] - arg.boundary_slices) return; + 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; From f21468659f1a1470a943016fc7a3b40938ae6e2e Mon Sep 17 00:00:00 2001 From: Evan Weinberg Date: Mon, 23 Nov 2026 11:15:57 -0800 Subject: [PATCH 08/19] Count only the t links of the time slices that cross the t boundary, halo included, in GaugeTBoundary_::bytes() --- include/kernels/gauge_phase.cuh | 10 ++++++++++ lib/gauge_phase.cu | 8 ++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/include/kernels/gauge_phase.cuh b/include/kernels/gauge_phase.cuh index 730ba2a4fd..fd08f6ad34 100644 --- a/include/kernels/gauge_phase.cuh +++ b/include/kernels/gauge_phase.cuh @@ -40,6 +40,16 @@ namespace quda @param[in] t The global t coordinate */ __device__ __host__ inline bool crossesTBoundary(int t) const { return t >= G[3] - boundary_slices; } + + /** + @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((t - border[3] + offset[3] + G[3]) % G[3]); + return n; + } }; /** diff --git a/lib/gauge_phase.cu b/lib/gauge_phase.cu index b08f8eea46..d7bbbac516 100644 --- a/lib/gauge_phase.cu +++ b/lib/gauge_phase.cu @@ -58,10 +58,14 @@ namespace quda { 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) + 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()); @@ -77,7 +81,7 @@ namespace quda { void postTune() { u.restore(); } long long flops() const { return 0; } - long long bytes() const { return 2 * u.Bytes() / 4; } + long long bytes() const { return boundary_bytes; } }; void applyGaugeTBoundary(GaugeField &u) From 107343f644980eca5f18f24d94709cd6bb31de7b Mon Sep 17 00:00:00 2001 From: Evan Weinberg Date: Mon, 23 Nov 2026 11:16:14 -0800 Subject: [PATCH 09/19] Test GaugeField::applyTBoundary and removeTBoundary on long links with reconstruct 13 and 9, which store the sign in their phase --- tests/gauge_phase_test.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/gauge_phase_test.cpp b/tests/gauge_phase_test.cpp index c3f4796413..64e2af8b04 100644 --- a/tests/gauge_phase_test.cpp +++ b/tests/gauge_phase_test.cpp @@ -455,12 +455,13 @@ class GaugeTBoundaryTest : public ::testing::TestWithParam // removeTBoundary takes it out again, as the test utilities apply the sign. Reconstruct NO and 12 store the negated // links exactly, so the fields must agree exactly with the links as the reconstruct stores them, u_r. Reconstruct // 8, 13 and 9 store phase angles, which negating a link shifts by pi, so the fields agree with the links within the -// tests' usual tolerance, getTolerance. +// 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) || !recon_enabled(recon)) GTEST_SKIP(); - if (link_type == QUDA_THREE_LINKS && recon != QUDA_RECONSTRUCT_NO) GTEST_SKIP(); + if (link_type == QUDA_THREE_LINKS && (recon == QUDA_RECONSTRUCT_12 || recon == QUDA_RECONSTRUCT_8)) GTEST_SKIP(); const bool exact = recon == QUDA_RECONSTRUCT_NO || recon == QUDA_RECONSTRUCT_12; const double tol = exact ? 0.0 : getTolerance(prec); const auto u = random_field(prec, QUDA_PERIODIC_T, link_type); From 454115cde430cc5b7a9d00894f8905f461cf9318 Mon Sep 17 00:00:00 2001 From: Evan Weinberg Date: Mon, 23 Nov 2026 11:16:31 -0800 Subject: [PATCH 10/19] Reject long links with reconstruct 12 or 8 in applyGaugeTBoundary, which rebuild the t boundary sign on one time slice rather than three, and move the reconstruct checks of GaugeField::applyTBoundary and removeTBoundary there, noting why reconstruct 12 and 8 reject an imaginary chemical potential --- include/gauge_field.h | 7 ++++--- lib/gauge_field.cpp | 3 --- lib/gauge_phase.cu | 9 +++++++++ 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/include/gauge_field.h b/include/gauge_field.h index 3e55193cb5..fe6cd7b8ed 100644 --- a/include/gauge_field.h +++ b/include/gauge_field.h @@ -397,8 +397,8 @@ namespace quda { @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, and for reconstruct 12 and 8 - have no imaginary chemical potential. + 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(); @@ -825,7 +825,8 @@ namespace quda { /** @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 + @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); diff --git a/lib/gauge_field.cpp b/lib/gauge_field.cpp index b82c781a71..4ea1c32844 100644 --- a/lib/gauge_field.cpp +++ b/lib/gauge_field.cpp @@ -425,9 +425,6 @@ namespace quda { 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()); - if ((u.Reconstruct() == QUDA_RECONSTRUCT_12 || u.Reconstruct() == QUDA_RECONSTRUCT_8) && u.iMu() != 0.0) - errorQuda("%s: reconstruct %d cannot hold the imaginary chemical potential (i_mu = %g)", name, u.Reconstruct(), - static_cast(u.iMu())); } void GaugeField::applyTBoundary() diff --git a/lib/gauge_phase.cu b/lib/gauge_phase.cu index d7bbbac516..1e253d5304 100644 --- a/lib/gauge_phase.cu +++ b/lib/gauge_phase.cu @@ -86,6 +86,15 @@ namespace quda { 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); From 5b4f7665146b32d778bed6ca10ff20e14f019a82 Mon Sep 17 00:00:00 2001 From: Evan Weinberg Date: Mon, 23 Nov 2026 11:16:46 -0800 Subject: [PATCH 11/19] Index the sites of extended fields by the parity of their interior coordinates in gauge_phase_test's link comparison, so that differing links are reported at their coordinates, and put get_phase_str's documentation on it --- tests/gauge_phase_test.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/tests/gauge_phase_test.cpp b/tests/gauge_phase_test.cpp index 64e2af8b04..be354439d1 100644 --- a/tests/gauge_phase_test.cpp +++ b/tests/gauge_phase_test.cpp @@ -139,7 +139,8 @@ namespace ha.copy(a); hb.copy(b); - // sites by their coordinates in the field, halo included; R is the halo width of an extended field + // sites by their coordinates in the field, halo included; R is the halo width of an extended field, whose sites + // are labelled by the parity of their interior coordinates const auto &E = ha.X(); const auto &R = ha.R(); const size_t volume = ha.Volume(); @@ -150,7 +151,8 @@ namespace for (int y = 0; y < E[1]; y++) for (int x = 0; x < E[0]; x++) { const size_t lex = x + E[0] * (y + static_cast(E[1]) * (z + static_cast(E[2]) * t)); - const size_t index = (x + y + z + t) % 2 == 0 ? lex / 2 : (lex + volume) / 2; + const int parity = (x + y + z + t + R[0] + R[1] + R[2] + R[3]) & 1; + const size_t index = parity * (volume / 2) + lex / 2; for (int dir = 0; dir < 4; dir++) { const auto pa = static_cast(ha.data(dir)) + index * site_bytes; const auto pb = static_cast(hb.data(dir)) + index * site_bytes; @@ -275,10 +277,6 @@ namespace return dev; } - /** - @param[in] type A staggered phase convention - @return Its name in test names - */ /** @param[in] recon A reconstruct @return Whether this build instantiates it @@ -330,6 +328,10 @@ namespace return v; } + /** + @param[in] type A staggered phase convention + @return Its name in test names + */ const char *get_phase_str(QudaStaggeredPhase type) { switch (type) { From 611c8121c8dc21325e3ad2f7297fdf9a0f293250 Mon Sep 17 00:00:00 2001 From: Evan Weinberg Date: Mon, 23 Nov 2026 11:17:59 -0800 Subject: [PATCH 12/19] Test that the staggered phases of thin links carry an imaginary chemical potential, exp(i pi i_mu / T) on every t link with T the global time extent, and that removing them takes it out, with the host phase from a new test utility, applyGaugeImaginaryChemicalPotential, and document that phase for staggeredPhaseQuda --- include/quda.h | 4 ++-- tests/gauge_phase_test.cpp | 33 +++++++++++++++++++++++++++++++-- tests/utils/gauge_utils.cpp | 27 +++++++++++++++++++++++++++ tests/utils/gauge_utils.h | 11 +++++++++++ 4 files changed, 71 insertions(+), 4 deletions(-) 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/tests/gauge_phase_test.cpp b/tests/gauge_phase_test.cpp index be354439d1..8a04c8bb62 100644 --- a/tests/gauge_phase_test.cpp +++ b/tests/gauge_phase_test.cpp @@ -20,7 +20,8 @@ // 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. Extended fields are +// 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 @@ -42,11 +43,12 @@ namespace @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) + QudaReconstructType recon = QUDA_RECONSTRUCT_NO, double i_mu = 0.0) { QudaGaugeParam gauge_param = newQudaGaugeParam(); setWilsonGaugeParam(gauge_param); @@ -55,6 +57,7 @@ namespace 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; @@ -372,6 +375,32 @@ TEST_P(GaugePhaseTest, apply_remove) 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) { diff --git a/tests/utils/gauge_utils.cpp b/tests/utils/gauge_utils.cpp index 06b83667ce..ec71c84c57 100644 --- a/tests/utils/gauge_utils.cpp +++ b/tests/utils/gauge_utils.cpp @@ -1,5 +1,7 @@ #include +#include #include +#include #include #include @@ -86,6 +88,31 @@ void applyGaugeTemporalBCs(void *const *gauge, int Vh, QudaTboundary t_boundary, instantiate_host(precision, gauge, Vh, t_boundary, three_link); } +/** + * @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. * diff --git a/tests/utils/gauge_utils.h b/tests/utils/gauge_utils.h index 13ed64661c..34ff1d05bb 100644 --- a/tests/utils/gauge_utils.h +++ b/tests/utils/gauge_utils.h @@ -93,6 +93,17 @@ void constructQudaGaugeField(void *const *gauge, GaugeFieldConstructionType type void applyGaugeTemporalBCs(void *const *gauge, int Vh, QudaTboundary t_boundary, QudaPrecision precision, bool three_link = false); +/** + * @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 From b047115eb46833f4b605a093d98455e93fb899f6 Mon Sep 17 00:00:00 2001 From: Evan Weinberg Date: Mon, 23 Nov 2026 11:18:10 -0800 Subject: [PATCH 13/19] Run gauge_phase_test with x, z and t partitioned, so that extended fields of halo width 1 have sites labelled with the opposite parity to their coordinates, on a single GPU and on CI's grid partitioned in z and t --- tests/CMakeLists.txt | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 9253291677..a5aa8384f8 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1698,6 +1698,13 @@ add_test(NAME gauge_phase --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) From 36d6a839c2cc796613e9116b55dd52a9d3d4a366 Mon Sep 17 00:00:00 2001 From: Evan Weinberg Date: Mon, 23 Nov 2026 11:48:49 -0800 Subject: [PATCH 14/19] Label the sites of extended gauge fields by the parity of their regular coordinates in the extended ghost exchange, as the rest of QUDA does, so that halos of odd total width with an odd x radius no longer overwrite interior sites --- include/kernels/extract_gauge_ghost_extended.cuh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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); From 065f0a7aca6724ef042fab218814f64896b6f14e Mon Sep 17 00:00:00 2001 From: Evan Weinberg Date: Mon, 23 Nov 2026 14:46:17 -0800 Subject: [PATCH 15/19] Make getGlobalCoords a member of GaugePhaseGeometry, with the wrapping of one coordinate onto the global lattice in a globalCoord member that boundaryTimeSlices shares --- include/kernels/gauge_phase.cuh | 41 ++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/include/kernels/gauge_phase.cuh b/include/kernels/gauge_phase.cuh index fd08f6ad34..d166a361e8 100644 --- a/include/kernels/gauge_phase.cuh +++ b/include/kernels/gauge_phase.cuh @@ -41,32 +41,37 @@ namespace quda */ __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((t - border[3] + offset[3] + G[3]) % G[3]); + for (int t = 0; t < E[3]; t++) n += crossesTBoundary(globalCoord(3, t)); return n; } }; - /** - @brief The global coordinates of a site of a gauge field, halo included, which wraps around the global lattice - @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 - @param[in] arg The field's geometry - */ - template - __device__ __host__ inline void getGlobalCoords(int x[4], int x_cb, int parity, const Arg &arg) - { - getCoords(x, x_cb, arg.E, (parity + arg.parity_shift) & 1); -#pragma unroll - for (int d = 0; d < 4; d++) x[d] = (x[d] - arg.border[d] + arg.offset[d] + arg.G[d]) % arg.G[d]; - } - template struct GaugePhaseArg : kernel_param<>, GaugePhaseGeometry { using Float = Float_; @@ -158,7 +163,7 @@ namespace quda __device__ __host__ void operator()(int x_cb, int parity) { int x[4]; - getGlobalCoords(x, x_cb, parity, arg); + 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); @@ -191,7 +196,7 @@ namespace quda __device__ __host__ void operator()(int x_cb, int parity) { int x[4]; - getGlobalCoords(x, x_cb, parity, arg); + 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); From d8e2dde126d73297926607a8edc00c13f02df537 Mon Sep 17 00:00:00 2001 From: Evan Weinberg Date: Mon, 23 Nov 2026 14:57:43 -0800 Subject: [PATCH 16/19] Compare gauge_phase_test's extended fields, halo included, with the test utilities' compare_link_fields, which takes QDP-ordered host fields of any one layout, instead of a site-by-site comparison of its own --- tests/gauge_phase_test.cpp | 43 ++++++++++---------------------------- tests/utils/host_utils.h | 3 ++- 2 files changed, 13 insertions(+), 33 deletions(-) diff --git a/tests/gauge_phase_test.cpp b/tests/gauge_phase_test.cpp index 8a04c8bb62..0d38ca8851 100644 --- a/tests/gauge_phase_test.cpp +++ b/tests/gauge_phase_test.cpp @@ -1,7 +1,6 @@ #include #include #include -#include #include #include #include @@ -126,13 +125,15 @@ namespace } /** - @brief Whether two fields hold the same links on every rank, compared on the host in QDP order, printing the - first few that differ. Collective. + @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 - @return Whether they are equal + @param[in] msg_b Its label in messages */ - bool same_links(const quda::GaugeField &a, const quda::GaugeField &b) + 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; @@ -141,31 +142,7 @@ namespace quda::GaugeField ha(host_param), hb(host_param); ha.copy(a); hb.copy(b); - - // sites by their coordinates in the field, halo included; R is the halo width of an extended field, whose sites - // are labelled by the parity of their interior coordinates - const auto &E = ha.X(); - const auto &R = ha.R(); - const size_t volume = ha.Volume(); - const size_t site_bytes = 18 * ha.Precision(); - size_t wrong = 0; - for (int t = 0; t < E[3]; t++) - for (int z = 0; z < E[2]; z++) - for (int y = 0; y < E[1]; y++) - for (int x = 0; x < E[0]; x++) { - const size_t lex = x + E[0] * (y + static_cast(E[1]) * (z + static_cast(E[2]) * t)); - const int parity = (x + y + z + t + R[0] + R[1] + R[2] + R[3]) & 1; - const size_t index = parity * (volume / 2) + lex / 2; - for (int dir = 0; dir < 4; dir++) { - const auto pa = static_cast(ha.data(dir)) + index * site_bytes; - const auto pb = static_cast(hb.data(dir)) + index * site_bytes; - if (memcmp(pa, pb, site_bytes) != 0 && wrong++ < 4) - printf("rank %d: link dir %d at (%d %d %d %d) in the field, (%d %d %d %d) in the interior, differs\n", - quda::comm_rank(), dir, x, y, z, t, x - R[0], y - R[1], z - R[2], t - R[3]); - } - } - quda::comm_allreduce_sum(wrong); - return wrong == 0; + expect_links(ha, msg_a, hb, msg_b); } /** @@ -421,7 +398,8 @@ TEST_P(GaugePhaseTest, extended_remove) std::unique_ptr expected(quda::createExtendedGauge(plain, R)); std::unique_ptr extended(quda::createExtendedGauge(phased, R)); extended->removeStaggeredPhase(); - EXPECT_TRUE(same_links(*extended, *expected)) << "halo width " << width; + expect_same_links(*extended, "extended unphased, halo width " + std::to_string(width), *expected, + "extended original"); } } @@ -535,7 +513,8 @@ TEST_P(GaugeTBoundaryTest, extended) std::unique_ptr expected(quda::createExtendedGauge(applied, R)); std::unique_ptr extended(quda::createExtendedGauge(plain, R)); extended->applyTBoundary(); - EXPECT_TRUE(same_links(*extended, *expected)) << "halo width " << width; + expect_same_links(*extended, "extended signed, halo width " + std::to_string(width), *expected, + "extended copy of signed"); } } 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 From 1c544c8d124fea609676c36458f4519310fe6b3d Mon Sep 17 00:00:00 2001 From: Evan Weinberg Date: Mon, 23 Nov 2026 15:01:25 -0800 Subject: [PATCH 17/19] Compare gauge_phase_test's gauge forces with the test utilities' momentum checks, compare_floats and strong_check_mom, on MILC-ordered copies, and check the force is not zero by its action, from momentumActionCPU, against a zero field's --- tests/gauge_phase_test.cpp | 68 ++++++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 24 deletions(-) diff --git a/tests/gauge_phase_test.cpp b/tests/gauge_phase_test.cpp index 0d38ca8851..c5d31883da 100644 --- a/tests/gauge_phase_test.cpp +++ b/tests/gauge_phase_test.cpp @@ -13,6 +13,7 @@ #include "host_utils.h" #include "gauge_utils.h" +#include "momentum_utils.h" #include "misc.h" #include "test.h" @@ -238,23 +239,50 @@ namespace } /** - @brief The largest difference between two momentum fields over all ranks. Collective. - @tparam real double or float - @param[in] a A host momentum field in QDP order + @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 - @return The largest absolute difference of any real + @param[in] tol The largest difference allowed of any real */ - template double max_mom_deviation(const quda::GaugeField &a, const quda::GaugeField &b) + void expect_same_mom(const quda::GaugeField &a, const quda::GaugeField &b, double tol = 0.0) { - const size_t reals = a.Volume() * a.Reconstruct(); - double dev = 0.0; - for (int dir = 0; dir < 4; dir++) { - const auto pa = a.data(dir); - const auto pb = b.data(dir); - for (size_t i = 0; i < reals; i++) dev = std::max(dev, std::abs(static_cast(pa[i]) - pb[i])); - } - quda::comm_allreduce_max(dev); - return dev; + 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; } /** @@ -442,16 +470,8 @@ TEST_P(GaugePhaseInterfaceTest, gauge_force) 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); - const double dev = prec == QUDA_DOUBLE_PRECISION ? max_mom_deviation(phased, plain) : - max_mom_deviation(phased, plain); - EXPECT_EQ(dev, 0.0); - - quda::GaugeFieldParam zero_param(plain); - zero_param.create = QUDA_ZERO_FIELD_CREATE; - const quda::GaugeField zero(zero_param); - const double size - = prec == QUDA_DOUBLE_PRECISION ? max_mom_deviation(plain, zero) : max_mom_deviation(plain, zero); - EXPECT_GT(size, 0.0) << "the force is zero, so the comparison shows nothing"; + 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; From 82464b06dcfbc93fd5dd6c961e9aaa0bc58b21ab Mon Sep 17 00:00:00 2001 From: Evan Weinberg Date: Mon, 23 Nov 2026 15:04:39 -0800 Subject: [PATCH 18/19] Name gauge_phase_test's staggered phase conventions with a new get_staggered_phase_str test utility, and skip its disabled reconstructs with getReconstructNibble, as the other tests do --- tests/gauge_phase_test.cpp | 36 +++--------------------------------- tests/utils/misc.cpp | 15 +++++++++++++++ tests/utils/misc.h | 1 + 3 files changed, 19 insertions(+), 33 deletions(-) diff --git a/tests/gauge_phase_test.cpp b/tests/gauge_phase_test.cpp index c5d31883da..a464c8ce22 100644 --- a/tests/gauge_phase_test.cpp +++ b/tests/gauge_phase_test.cpp @@ -285,22 +285,6 @@ namespace return momentumActionCPU(m.data(), links, m.Precision()) + 4.0 * all_links; } - /** - @param[in] recon A reconstruct - @return Whether this build instantiates it - */ - bool recon_enabled(QudaReconstructType recon) - { - switch (recon) { - case QUDA_RECONSTRUCT_NO: return quda::is_enabled(); - case QUDA_RECONSTRUCT_13: return quda::is_enabled(); - case QUDA_RECONSTRUCT_12: return quda::is_enabled(); - case QUDA_RECONSTRUCT_9: return quda::is_enabled(); - case QUDA_RECONSTRUCT_8: return quda::is_enabled(); - default: return false; - } - } - /** @brief A host field's links with the t links that cross the t boundary negated, by the test utilities @param[in] u The links, in QDP order @@ -336,20 +320,6 @@ namespace return v; } - /** - @param[in] type A staggered phase convention - @return Its name in test names - */ - const char *get_phase_str(QudaStaggeredPhase type) - { - switch (type) { - case QUDA_STAGGERED_PHASE_MILC: return "milc"; - case QUDA_STAGGERED_PHASE_TIFR: return "tifr"; - case QUDA_STAGGERED_PHASE_CHROMA: return "chroma"; - default: return "invalid"; - } - } - } // namespace using test_t = ::testing::tuple; @@ -489,7 +459,7 @@ class GaugeTBoundaryTest : public ::testing::TestWithParam TEST_P(GaugeTBoundaryTest, apply_remove) { const auto [prec, recon, link_type] = GetParam(); - if (!quda::is_enabled(prec) || !recon_enabled(recon)) GTEST_SKIP(); + 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 bool exact = recon == QUDA_RECONSTRUCT_NO || recon == QUDA_RECONSTRUCT_12; const double tol = exact ? 0.0 : getTolerance(prec); @@ -554,7 +524,7 @@ INSTANTIATE_TEST_SUITE_P(Phase, GaugePhaseTest, 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_phase_str(::testing::get<1>(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; }); @@ -565,7 +535,7 @@ INSTANTIATE_TEST_SUITE_P(Interface, GaugePhaseInterfaceTest, QUDA_STAGGERED_PHASE_CHROMA)), [](testing::TestParamInfo param) { return std::string(get_prec_str(::testing::get<0>(param.param))) + "_" - + get_phase_str(::testing::get<1>(param.param)); + + get_staggered_phase_str(::testing::get<1>(param.param)); }); INSTANTIATE_TEST_SUITE_P(TBoundary, GaugeTBoundaryTest, 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); From 42172dfdd68ba4bc1ead972fbcea1dfc7191b572 Mon Sep 17 00:00:00 2001 From: Evan Weinberg Date: Mon, 23 Nov 2026 15:06:53 -0800 Subject: [PATCH 19/19] Add an applyGaugeTemporalBCs overload for host gauge fields, which takes long links from the field's link type, and build gauge_phase_test's phased and signed references with it on plain copies, comparing reconstructs other than NO within tolerance instead of through a device round trip --- tests/gauge_phase_test.cpp | 67 +++++++------------------------------ tests/utils/gauge_utils.cpp | 22 ++++++++++++ tests/utils/gauge_utils.h | 10 ++++++ 3 files changed, 44 insertions(+), 55 deletions(-) diff --git a/tests/gauge_phase_test.cpp b/tests/gauge_phase_test.cpp index a464c8ce22..a120710615 100644 --- a/tests/gauge_phase_test.cpp +++ b/tests/gauge_phase_test.cpp @@ -1,7 +1,4 @@ -#include #include -#include -#include #include #include #include @@ -94,17 +91,13 @@ namespace @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 in QDP order of u's link type recording t_boundary + @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( - field_param(QUDA_CPU_FIELD_LOCATION, u.Precision(), QUDA_STAGGERED_PHASE_NO, t_boundary, u.LinkType())); - for (int dir = 0; dir < 4; dir++) memcpy(phased.data(dir), u.data(dir), u.Volume() * 18 * u.Precision()); + quda::GaugeField phased(u); applyGaugeStaggeredPhase(phased, type); - auto arrays = phased.data_array(); - applyGaugeTemporalBCs(arrays.data, static_cast(phased.VolumeCB()), t_boundary, phased.Precision(), - u.LinkType() == QUDA_THREE_LINKS); + applyGaugeTemporalBCs(phased, t_boundary); return phased; } @@ -285,41 +278,6 @@ namespace return momentumActionCPU(m.data(), links, m.Precision()) + 4.0 * all_links; } - /** - @brief A host field's links with the t links that cross the t boundary negated, by the test utilities - @param[in] u The links, in QDP order - @param[in] t_boundary The t boundary the returned field records - @return The links, as a host field in QDP order of u's link type - */ - quda::GaugeField negate_boundary(const quda::GaugeField &u, QudaTboundary t_boundary) - { - quda::GaugeField v( - field_param(QUDA_CPU_FIELD_LOCATION, u.Precision(), QUDA_STAGGERED_PHASE_NO, t_boundary, u.LinkType())); - for (int dir = 0; dir < 4; dir++) memcpy(v.data(dir), u.data(dir), u.Volume() * 18 * u.Precision()); - auto arrays = v.data_array(); - applyGaugeTemporalBCs(arrays.data, static_cast(v.VolumeCB()), QUDA_ANTI_PERIODIC_T, v.Precision(), - u.LinkType() == QUDA_THREE_LINKS); - return v; - } - - /** - @brief A host field's links as a device field with a reconstruct stores them: packed into the device field and - unpacked again, under the host field's t boundary - @param[in] u The links, in QDP order - @param[in] recon The reconstruct - @return The links, as a host field in QDP order like u - */ - quda::GaugeField stored(const quda::GaugeField &u, QudaReconstructType recon) - { - quda::GaugeField device(field_param(QUDA_CUDA_FIELD_LOCATION, u.Precision(), QUDA_STAGGERED_PHASE_NO, u.TBoundary(), - u.LinkType(), recon)); - device.copy(u); - quda::GaugeField v( - field_param(QUDA_CPU_FIELD_LOCATION, u.Precision(), QUDA_STAGGERED_PHASE_NO, u.TBoundary(), u.LinkType())); - v.copy(device); - return v; - } - } // namespace using test_t = ::testing::tuple; @@ -451,20 +409,19 @@ 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 and 12 store the negated -// links exactly, so the fields must agree exactly with the links as the reconstruct stores them, u_r. Reconstruct -// 8, 13 and 9 store phase angles, which negating a link shifts by pi, so the fields agree with the links 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. +// 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 bool exact = recon == QUDA_RECONSTRUCT_NO || recon == QUDA_RECONSTRUCT_12; - const double tol = exact ? 0.0 : getTolerance(prec); + const double tol = recon == QUDA_RECONSTRUCT_NO ? 0.0 : getTolerance(prec); const auto u = random_field(prec, QUDA_PERIODIC_T, link_type); - const auto reference = exact ? stored(u, recon) : u; + 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); @@ -474,13 +431,13 @@ TEST_P(GaugeTBoundaryTest, apply_remove) 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", negate_boundary(reference, QUDA_ANTI_PERIODIC_T), "host signed", tol); + 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", reference, "host unsigned", tol); + 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 diff --git a/tests/utils/gauge_utils.cpp b/tests/utils/gauge_utils.cpp index ec71c84c57..fce09e3731 100644 --- a/tests/utils/gauge_utils.cpp +++ b/tests/utils/gauge_utils.cpp @@ -88,6 +88,28 @@ void applyGaugeTemporalBCs(void *const *gauge, int Vh, QudaTboundary t_boundary, 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 * diff --git a/tests/utils/gauge_utils.h b/tests/utils/gauge_utils.h index 34ff1d05bb..a1868e3e17 100644 --- a/tests/utils/gauge_utils.h +++ b/tests/utils/gauge_utils.h @@ -93,6 +93,16 @@ void constructQudaGaugeField(void *const *gauge, GaugeFieldConstructionType type 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