Skip to content

Commit

Permalink
Merge pull request #1714 from CEED/jeremy/at-points-basis-flops
Browse files Browse the repository at this point in the history
Fix FLOPs estimates AtPoints
  • Loading branch information
jeremylt authored Dec 3, 2024
2 parents 290fc47 + 3f919cb commit c8c64f4
Show file tree
Hide file tree
Showing 5 changed files with 246 additions and 33 deletions.
3 changes: 2 additions & 1 deletion include/ceed/backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,8 @@ CEED_EXTERN int CeedBasisGetData(CeedBasis basis, void *data);
CEED_EXTERN int CeedBasisSetData(CeedBasis basis, void *data);
CEED_EXTERN int CeedBasisReference(CeedBasis basis);
CEED_EXTERN int CeedBasisGetNumQuadratureComponents(CeedBasis basis, CeedEvalMode eval_mode, CeedInt *q_comp);
CEED_EXTERN int CeedBasisGetFlopsEstimate(CeedBasis basis, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedSize *flops);
CEED_EXTERN int CeedBasisGetFlopsEstimate(CeedBasis basis, CeedTransposeMode t_mode, CeedEvalMode eval_mode, bool is_at_points, CeedInt num_points,
CeedSize *flops);
CEED_EXTERN int CeedBasisGetFESpace(CeedBasis basis, CeedFESpace *fe_space);
CEED_EXTERN int CeedBasisGetTopologyDimension(CeedElemTopology topo, CeedInt *dim);
CEED_EXTERN int CeedBasisGetTensorContract(CeedBasis basis, CeedTensorContract *contract);
Expand Down
90 changes: 65 additions & 25 deletions interface/ceed-basis.c
Original file line number Diff line number Diff line change
Expand Up @@ -779,17 +779,21 @@ int CeedBasisGetNumQuadratureComponents(CeedBasis basis, CeedEvalMode eval_mode,
/**
@brief Estimate number of FLOPs required to apply `CeedBasis` in `t_mode` and `eval_mode`
@param[in] basis `CeedBasis` to estimate FLOPs for
@param[in] t_mode Apply basis or transpose
@param[in] eval_mode @ref CeedEvalMode
@param[out] flops Address of variable to hold FLOPs estimate
@param[in] basis `CeedBasis` to estimate FLOPs for
@param[in] t_mode Apply basis or transpose
@param[in] eval_mode @ref CeedEvalMode
@param[in] is_at_points Evaluate the basis at points or quadrature points
@param[in] num_points Number of points basis is evaluated at
@param[out] flops Address of variable to hold FLOPs estimate
@ref Backend
**/
int CeedBasisGetFlopsEstimate(CeedBasis basis, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedSize *flops) {
int CeedBasisGetFlopsEstimate(CeedBasis basis, CeedTransposeMode t_mode, CeedEvalMode eval_mode, bool is_at_points, CeedInt num_points,
CeedSize *flops) {
bool is_tensor;

CeedCall(CeedBasisIsTensor(basis, &is_tensor));
CeedCheck(!is_at_points || is_tensor, CeedBasisReturnCeed(basis), CEED_ERROR_INCOMPATIBLE, "Can only evaluate tensor-product bases at points");
if (is_tensor) {
CeedInt dim, num_comp, P_1d, Q_1d;

Expand All @@ -802,32 +806,68 @@ int CeedBasisGetFlopsEstimate(CeedBasis basis, CeedTransposeMode t_mode, CeedEva
Q_1d = P_1d;
}
CeedInt tensor_flops = 0, pre = num_comp * CeedIntPow(P_1d, dim - 1), post = 1;

for (CeedInt d = 0; d < dim; d++) {
tensor_flops += 2 * pre * P_1d * post * Q_1d;
pre /= P_1d;
post *= Q_1d;
}
switch (eval_mode) {
case CEED_EVAL_NONE:
*flops = 0;
break;
case CEED_EVAL_INTERP:
*flops = tensor_flops;
break;
case CEED_EVAL_GRAD:
*flops = tensor_flops * 2;
break;
case CEED_EVAL_DIV:
case CEED_EVAL_CURL: {
// LCOV_EXCL_START
return CeedError(CeedBasisReturnCeed(basis), CEED_ERROR_INCOMPATIBLE, "Tensor basis evaluation for %s not supported",
CeedEvalModes[eval_mode]);
break;
// LCOV_EXCL_STOP
if (is_at_points) {
CeedInt chebyshev_flops = (Q_1d - 2) * 3 + 1, d_chebyshev_flops = (Q_1d - 2) * 8 + 1;
CeedInt point_tensor_flops = 0, pre = CeedIntPow(Q_1d, dim - 1), post = 1;

for (CeedInt d = 0; d < dim; d++) {
point_tensor_flops += 2 * pre * Q_1d * post * 1;
pre /= P_1d;
post *= Q_1d;
}

switch (eval_mode) {
case CEED_EVAL_NONE:
*flops = 0;
break;
case CEED_EVAL_INTERP:
*flops = tensor_flops + num_points * (dim * chebyshev_flops + point_tensor_flops + (t_mode == CEED_TRANSPOSE ? CeedIntPow(Q_1d, dim) : 0));
break;
case CEED_EVAL_GRAD:
*flops = tensor_flops + num_points * (dim * (d_chebyshev_flops + (dim - 1) * chebyshev_flops + point_tensor_flops +
(t_mode == CEED_TRANSPOSE ? CeedIntPow(Q_1d, dim) : 0)));
break;
case CEED_EVAL_DIV:
case CEED_EVAL_CURL: {
// LCOV_EXCL_START
return CeedError(CeedBasisReturnCeed(basis), CEED_ERROR_INCOMPATIBLE, "Tensor basis evaluation for %s not supported",
CeedEvalModes[eval_mode]);
break;
// LCOV_EXCL_STOP
}
case CEED_EVAL_WEIGHT:
*flops = num_points;
break;
}
} else {
switch (eval_mode) {
case CEED_EVAL_NONE:
*flops = 0;
break;
case CEED_EVAL_INTERP:
*flops = tensor_flops;
break;
case CEED_EVAL_GRAD:
*flops = tensor_flops * 2;
break;
case CEED_EVAL_DIV:
case CEED_EVAL_CURL: {
// LCOV_EXCL_START
return CeedError(CeedBasisReturnCeed(basis), CEED_ERROR_INCOMPATIBLE, "Tensor basis evaluation for %s not supported",
CeedEvalModes[eval_mode]);
break;
// LCOV_EXCL_STOP
}
case CEED_EVAL_WEIGHT:
*flops = dim * CeedIntPow(Q_1d, dim);
break;
}
case CEED_EVAL_WEIGHT:
*flops = dim * CeedIntPow(Q_1d, dim);
break;
}
} else {
CeedInt dim, num_comp, q_comp, num_nodes, num_qpts;
Expand Down
44 changes: 37 additions & 7 deletions interface/ceed-operator.c
Original file line number Diff line number Diff line change
Expand Up @@ -1097,8 +1097,14 @@ int CeedOperatorAtPointsGetPoints(CeedOperator op, CeedElemRestriction *rstr_poi
CeedCheck(is_at_points, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Only defined for operator at points");
CeedCall(CeedOperatorCheckReady(op));

if (rstr_points) CeedCall(CeedElemRestrictionReferenceCopy(op->rstr_points, rstr_points));
if (point_coords) CeedCall(CeedVectorReferenceCopy(op->point_coords, point_coords));
if (rstr_points) {
*rstr_points = NULL;
CeedCall(CeedElemRestrictionReferenceCopy(op->rstr_points, rstr_points));
}
if (point_coords) {
*point_coords = NULL;
CeedCall(CeedVectorReferenceCopy(op->point_coords, point_coords));
}
return CEED_ERROR_SUCCESS;
}

Expand Down Expand Up @@ -1693,16 +1699,39 @@ int CeedOperatorGetFlopsEstimate(CeedOperator op, CeedSize *flops) {
*flops += suboperator_flops;
}
} else {
CeedInt num_input_fields, num_output_fields, num_elem = 0;
bool is_at_points;
CeedInt num_input_fields, num_output_fields, num_elem = 0, num_points = 0;
CeedQFunction qf;
CeedQFunctionField *qf_input_fields, *qf_output_fields;
CeedOperatorField *op_input_fields, *op_output_fields;

CeedCall(CeedOperatorIsAtPoints(op, &is_at_points));
CeedCall(CeedOperatorGetNumElements(op, &num_elem));
if (is_at_points) {
CeedMemType mem_type;
CeedElemRestriction rstr_points = NULL;

CeedCall(CeedOperatorAtPointsGetPoints(op, &rstr_points, NULL));
CeedCall(CeedGetPreferredMemType(CeedOperatorReturnCeed(op), &mem_type));
if (mem_type == CEED_MEM_DEVICE) {
// Device backends pad out to the same number of points per element
CeedCall(CeedElemRestrictionGetMaxPointsInElement(rstr_points, &num_points));
} else {
num_points = 0;
for (CeedInt i = 0; i < num_elem; i++) {
CeedInt points_in_elem = 0;

CeedCall(CeedElemRestrictionGetNumPointsInElement(rstr_points, i, &points_in_elem));
num_points += points_in_elem;
}
num_points = num_points / num_elem + (num_points % num_elem > 0);
}
CeedCall(CeedElemRestrictionDestroy(&rstr_points));
}
CeedCall(CeedOperatorGetQFunction(op, &qf));
CeedCall(CeedQFunctionGetFields(qf, &num_input_fields, &qf_input_fields, &num_output_fields, &qf_output_fields));
CeedCall(CeedQFunctionDestroy(&qf));
CeedCall(CeedOperatorGetFields(op, NULL, &op_input_fields, NULL, &op_output_fields));
CeedCall(CeedOperatorGetNumElements(op, &num_elem));

// Input FLOPs
for (CeedInt i = 0; i < num_input_fields; i++) {
Expand All @@ -1721,7 +1750,7 @@ int CeedOperatorGetFlopsEstimate(CeedOperator op, CeedSize *flops) {
*flops += rstr_flops;
CeedCall(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
CeedCall(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
CeedCall(CeedBasisGetFlopsEstimate(basis, CEED_NOTRANSPOSE, eval_mode, &basis_flops));
CeedCall(CeedBasisGetFlopsEstimate(basis, CEED_NOTRANSPOSE, eval_mode, is_at_points, num_points, &basis_flops));
CeedCall(CeedBasisDestroy(&basis));
*flops += basis_flops * num_elem;
}
Expand All @@ -1733,7 +1762,8 @@ int CeedOperatorGetFlopsEstimate(CeedOperator op, CeedSize *flops) {
CeedSize qf_flops;
CeedQFunction qf;

CeedCall(CeedOperatorGetNumQuadraturePoints(op, &num_qpts));
if (is_at_points) num_qpts = num_points;
else CeedCall(CeedOperatorGetNumQuadraturePoints(op, &num_qpts));
CeedCall(CeedOperatorGetQFunction(op, &qf));
CeedCall(CeedQFunctionGetFlopsEstimate(qf, &qf_flops));
CeedCall(CeedQFunctionDestroy(&qf));
Expand All @@ -1759,7 +1789,7 @@ int CeedOperatorGetFlopsEstimate(CeedOperator op, CeedSize *flops) {
*flops += rstr_flops;
CeedCall(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
CeedCall(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
CeedCall(CeedBasisGetFlopsEstimate(basis, CEED_TRANSPOSE, eval_mode, &basis_flops));
CeedCall(CeedBasisGetFlopsEstimate(basis, CEED_TRANSPOSE, eval_mode, is_at_points, num_points, &basis_flops));
CeedCall(CeedBasisDestroy(&basis));
*flops += basis_flops * num_elem;
}
Expand Down
125 changes: 125 additions & 0 deletions tests/t595-operator.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/// @file
/// Test FLOP estimation for mass matrix operator at points
/// \test Test FLOP estimation for mass matrix operator at points
#include "t595-operator.h"

#include <ceed.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
Ceed ceed;
CeedInt num_elem_1d = 3, num_elem = num_elem_1d * num_elem_1d, dim = 2, p = 3, q = 5;
CeedInt num_nodes = (num_elem_1d * (p - 1) + 1) * (num_elem_1d * (p - 1) + 1), num_points_per_elem = 4, num_points = num_elem * num_points_per_elem;
CeedSize flop_estimate = 0;
CeedVector x_points, q_data;
CeedElemRestriction elem_restriction_x_points, elem_restriction_q_data, elem_restriction_u;
CeedBasis basis_x, basis_u;
CeedQFunction qf_mass;
CeedOperator op_mass;
bool is_at_points;

CeedInit(argv[1], &ceed);

// Point reference coordinates
CeedVectorCreate(ceed, dim * num_points, &x_points);
{
CeedScalar x_array[dim * num_points];

for (CeedInt e = 0; e < num_elem; e++) {
for (CeedInt d = 0; d < dim; d++) {
x_array[num_points_per_elem * (e * dim + d) + 0] = 0.25;
x_array[num_points_per_elem * (e * dim + d) + 1] = d == 0 ? -0.25 : 0.25;
x_array[num_points_per_elem * (e * dim + d) + 2] = d == 0 ? 0.25 : -0.25;
x_array[num_points_per_elem * (e * dim + d) + 3] = 0.25;
}
}
CeedVectorSetArray(x_points, CEED_MEM_HOST, CEED_COPY_VALUES, x_array);
}
{
CeedInt ind_x[num_elem + 1 + num_points];

for (CeedInt i = 0; i <= num_elem; i++) ind_x[i] = num_elem + 1 + i * num_points_per_elem;
for (CeedInt i = 0; i < num_points; i++) ind_x[num_elem + 1 + i] = i;
CeedElemRestrictionCreateAtPoints(ceed, num_elem, num_points, dim, num_points * dim, CEED_MEM_HOST, CEED_COPY_VALUES, ind_x,
&elem_restriction_x_points);
CeedElemRestrictionCreateAtPoints(ceed, num_elem, num_points, 1, num_points, CEED_MEM_HOST, CEED_COPY_VALUES, ind_x, &elem_restriction_q_data);
}

// Q data
CeedVectorCreate(ceed, num_points, &q_data);

CeedBasisCreateTensorH1Lagrange(ceed, dim, dim, 2, q, CEED_GAUSS, &basis_x);

// Cell solution
{
CeedInt ind_u[num_elem * p * p];

for (CeedInt e = 0; e < num_elem; e++) {
CeedInt elem_xy[2] = {1, 1}, n_d[2] = {0, 0};

for (CeedInt d = 0; d < dim; d++) n_d[d] = num_elem_1d * (p - 1) + 1;
{
CeedInt r_e = e;

for (CeedInt d = 0; d < dim; d++) {
elem_xy[d] = r_e % num_elem_1d;
r_e /= num_elem_1d;
}
}
CeedInt num_nodes_in_elem = p * p, *elem_nodes = ind_u + e * num_nodes_in_elem;

for (CeedInt n = 0; n < num_nodes_in_elem; n++) {
CeedInt g_node = 0, g_node_stride = 1, r_node = n;

for (CeedInt d = 0; d < dim; d++) {
g_node += (elem_xy[d] * (p - 1) + r_node % p) * g_node_stride;
g_node_stride *= n_d[d];
r_node /= p;
}
elem_nodes[n] = g_node;
}
}
CeedElemRestrictionCreate(ceed, num_elem, p * p, 1, 1, num_nodes, CEED_MEM_HOST, CEED_COPY_VALUES, ind_u, &elem_restriction_u);
}
CeedBasisCreateTensorH1Lagrange(ceed, dim, 1, p, q, CEED_GAUSS, &basis_u);

// Mass operator
CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass);
CeedQFunctionAddInput(qf_mass, "u", 1, CEED_EVAL_INTERP);
CeedQFunctionAddInput(qf_mass, "rho", 1, CEED_EVAL_NONE);
CeedQFunctionAddOutput(qf_mass, "v", 1, CEED_EVAL_INTERP);

CeedOperatorCreateAtPoints(ceed, qf_mass, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_mass);
CeedOperatorSetField(op_mass, "u", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
CeedOperatorSetField(op_mass, "rho", elem_restriction_q_data, CEED_BASIS_NONE, q_data);
CeedOperatorSetField(op_mass, "v", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
CeedOperatorAtPointsSetPoints(op_mass, elem_restriction_x_points, x_points);

CeedOperatorIsAtPoints(op_mass, &is_at_points);
if (!is_at_points) printf("Error: Operator should be at points\n");

// Estimate FLOPs
CeedQFunctionSetUserFlopsEstimate(qf_mass, 1);
CeedOperatorGetFlopsEstimate(op_mass, &flop_estimate);

// Check output
if (flop_estimate != 16317) {
// LCOV_EXCL_START
printf("Incorrect FLOP estimate computed, %ld != 16317\n", flop_estimate);
// LCOV_EXCL_STOP
}

CeedVectorDestroy(&x_points);
CeedVectorDestroy(&q_data);
CeedElemRestrictionDestroy(&elem_restriction_x_points);
CeedElemRestrictionDestroy(&elem_restriction_q_data);
CeedElemRestrictionDestroy(&elem_restriction_u);
CeedBasisDestroy(&basis_x);
CeedBasisDestroy(&basis_u);
CeedQFunctionDestroy(&qf_mass);
CeedOperatorDestroy(&op_mass);
CeedDestroy(&ceed);
return 0;
}
17 changes: 17 additions & 0 deletions tests/t595-operator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other CEED contributors.
// All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
//
// SPDX-License-Identifier: BSD-2-Clause
//
// This file is part of CEED: http://github.com/ceed

#include <ceed/types.h>

CEED_QFUNCTION(mass)(void *ctx, const CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
const CeedScalar *u = in[0], *rho = in[1];
CeedScalar *v = out[0];

// Quadrature point loop
CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) { v[i] = rho[i] * u[i]; }
return 0;
}

0 comments on commit c8c64f4

Please sign in to comment.