/*
 * Copyright 2025-2026 NVIDIA CORPORATION & AFFILIATES.  All rights reserved.
 *
 * NOTICE TO LICENSEE:
 *
 * This source code and/or documentation ("Licensed Deliverables") are
 * subject to NVIDIA intellectual property rights under U.S. and
 * international Copyright laws.
 *
 * These Licensed Deliverables contained herein is PROPRIETARY and
 * CONFIDENTIAL to NVIDIA and is being provided under the terms and
 * conditions of a form of NVIDIA software license agreement by and
 * between NVIDIA and Licensee ("License Agreement") or electronically
 * accepted by Licensee.  Notwithstanding any terms or conditions to
 * the contrary in the License Agreement, reproduction or disclosure
 * of the Licensed Deliverables to any third party without the express
 * written consent of NVIDIA is prohibited.
 *
 * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE
 * LICENSE AGREEMENT, NVIDIA MAKES NO REPRESENTATION ABOUT THE
 * SUITABILITY OF THESE LICENSED DELIVERABLES FOR ANY PURPOSE.  IT IS
 * PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY OF ANY KIND.
 * NVIDIA DISCLAIMS ALL WARRANTIES WITH REGARD TO THESE LICENSED
 * DELIVERABLES, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY,
 * NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE.
 * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE
 * LICENSE AGREEMENT, IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY
 * SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, OR ANY
 * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
 * OF THESE LICENSED DELIVERABLES.
 *
 * U.S. Government End Users.  These Licensed Deliverables are a
 * "commercial item" as that term is defined at 48 C.F.R. 2.101 (OCT
 * 1995), consisting of "commercial computer software" and "commercial
 * computer software documentation" as such terms are used in 48
 * C.F.R. 12.212 (SEPT 1995) and is provided to the U.S. Government
 * only as a commercial end item.  Consistent with 48 C.F.R.12.212 and
 * 48 C.F.R. 227.7202-1 through 227.7202-4 (JUNE 1995), all
 * U.S. Government End Users acquire the Licensed Deliverables with
 * only those rights set forth herein.
 *
 * Any use of the Licensed Deliverables in individual and commercial
 * software must include, in the user documentation and internal
 * comments to the code, the above Disclaimer and U.S. Government End
 * Users Notice.
 */

#ifndef DENSITY_FITTED_COULOMB_COMPUTE_API_H
#define DENSITY_FITTED_COULOMB_COMPUTE_API_H

#include <stddef.h>

#include "../macros.h"
#include "../types/cuest_types.h"
#include "../types/cuest_parameter_types.h"
#include "../types/workspace_api.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
 * @brief Compute the AO Coulomb (J) matrix using density fitting (DF) with the supplied DF integral plan and density matrix.
 * @ingroup Coulomb
 *
 * This routine computes the Coulomb matrix (J) from a density matrix using an auxiliary basis and
 * density-fitted integrals. The user must provide a preallocated density matrix (size: nao × nao),
 * a \c cuestDFIntPlan_t, and a preallocated output buffer (size: nao × nao).
 * All required temporary workspace must be allocated using the size returned by 
 * \c cuestDFCoulombComputeWorkspaceQuery.
 *
 * \b Kernel and precision selection. This routine can use JIT-compiled kernels and/or reduced-precision
 * "ffloat" compute, selected through the compute parameters:
 *   - \c CUEST_DFCOULOMBCOMPUTE_PARAMETERS_JIT_USAGE_MODE (\c cuestJITUsageMode_t): \c CUEST_JIT_USAGE_MODE_ON
 *     uses JIT-compiled kernels, \c CUEST_JIT_USAGE_MODE_OFF uses AOT-compiled kernels. JIT-compiled kernels
 *     are only available in the cuEST CUDA 13 package; the default is \c CUEST_JIT_USAGE_MODE_ON in the
 *     cuEST CUDA 13 package and \c CUEST_JIT_USAGE_MODE_OFF in the cuEST CUDA 12 package. Requesting
 *     \c CUEST_JIT_USAGE_MODE_ON with the cuEST CUDA 12 package returns \c CUEST_STATUS_INVALID_ARGUMENT.
 *   - \c CUEST_DFCOULOMBCOMPUTE_PARAMETERS_FFLOAT_USAGE_MODE (\c cuestFfloatUsageMode_t) selects the compute precision:
 *     \c CUEST_FFLOAT_USAGE_MODE_ON requests ffloat compute, \c CUEST_FFLOAT_USAGE_MODE_OFF requests fp64
 *     compute, and \c CUEST_FFLOAT_USAGE_MODE_DEFAULT requests ffloat compute when it is expected to provide
 *     a performance advantage on the present GPU architecture.
 *   - ffloat compute is only compatible with JIT-compiled kernels. Requesting ffloat compute while JIT is
 *     disabled (\c CUEST_FFLOAT_USAGE_MODE_ON together with \c CUEST_JIT_USAGE_MODE_OFF) is not supported and
 *     returns \c CUEST_STATUS_INVALID_ARGUMENT.
 *
 * @param[in]  handle                cuEST handle. Must not be NULL.
 * @param[in]  plan                  Density-fitted integral plan (opaque handle) constructed with \c cuestDFIntPlanCreate. Must not be NULL.
 * @param[in]  parameters            Compute parameters (\c cuestDFCoulombComputeParameters_t). Must not be NULL
 * @param[in]  temporaryWorkspace    Temporary workspace buffer for this operation. Must not be NULL.
 * @param[in]  densityMatrix         Input density matrix (size: nao × nao) on GPU. Must not be NULL.
 * @param[out] outCoulombMatrix      Output buffer for computed Coulomb matrix (size: nao × nao) on GPU. Must not be NULL.
 *                                   Elements of this array are overwritten by the calculation results.
 *
 * @return
 *   - \c CUEST_STATUS_SUCCESS on success;
 *   - \c CUEST_STATUS_INVALID_HANDLE if the cuEST handle is NULL;
 *   - \c CUEST_STATUS_NULL_POINTER if any required pointer is NULL;
 *   - \c CUEST_STATUS_INVALID_TYPE if opaque handles are not the correct type;
 *   - \c CUEST_STATUS_INVALID_PLAN if the \c cuestDFIntPlan_t is not correctly configured or in a dirty state;
 *   - \c CUEST_STATUS_INVALID_ARGUMENT if a parameter value is invalid, if JIT-compiled kernels are requested
 *     with the cuEST CUDA 12 package, or if ffloat compute is requested while JIT is disabled (incompatible
 *     JIT/ffloat combination);
 *   - \c CUEST_STATUS_EXCEPTION or \c CUEST_STATUS_UNKNOWN_ERROR otherwise.
 */
CUEST_API
cuestStatus_t cuestDFCoulombCompute(
    cuestHandle_t handle,
    const cuestDFIntPlan_t plan,
    const cuestDFCoulombComputeParameters_t parameters,
    cuestWorkspace_t* temporaryWorkspace,
    const double* densityMatrix,
    double* outCoulombMatrix);

/**
 * @brief Query the temporary workspace required (in bytes) for a DF Coulomb matrix computation.
 * @ingroup Coulomb
 *
 * This function returns the workspace needed (host/device) for a single computation,
 * based on the provided \c cuestDFIntPlan_t. Only sizes are computed; output and density
 * buffers may be NULL.
 *
 * \b Kernel and precision selection. This routine can use JIT-compiled kernels and/or reduced-precision
 * "ffloat" compute, selected through the compute parameters:
 *   - \c CUEST_DFCOULOMBCOMPUTE_PARAMETERS_JIT_USAGE_MODE (\c cuestJITUsageMode_t): \c CUEST_JIT_USAGE_MODE_ON
 *     uses JIT-compiled kernels, \c CUEST_JIT_USAGE_MODE_OFF uses AOT-compiled kernels. JIT-compiled kernels
 *     are only available in the cuEST CUDA 13 package; the default is \c CUEST_JIT_USAGE_MODE_ON in the
 *     cuEST CUDA 13 package and \c CUEST_JIT_USAGE_MODE_OFF in the cuEST CUDA 12 package. Requesting
 *     \c CUEST_JIT_USAGE_MODE_ON with the cuEST CUDA 12 package returns \c CUEST_STATUS_INVALID_ARGUMENT.
 *   - \c CUEST_DFCOULOMBCOMPUTE_PARAMETERS_FFLOAT_USAGE_MODE (\c cuestFfloatUsageMode_t) selects the compute precision:
 *     \c CUEST_FFLOAT_USAGE_MODE_ON requests ffloat compute, \c CUEST_FFLOAT_USAGE_MODE_OFF requests fp64
 *     compute, and \c CUEST_FFLOAT_USAGE_MODE_DEFAULT requests ffloat compute when it is expected to provide
 *     a performance advantage on the present GPU architecture.
 *   - ffloat compute is only compatible with JIT-compiled kernels. Requesting ffloat compute while JIT is
 *     disabled (\c CUEST_FFLOAT_USAGE_MODE_ON together with \c CUEST_JIT_USAGE_MODE_OFF) is not supported and
 *     returns \c CUEST_STATUS_INVALID_ARGUMENT.
 *
 * @param[in]  handle                              cuEST handle. Must not be NULL.
 * @param[in]  plan                                Density-fitted integral plan (opaque handle). Must not be NULL.
 * @param[in]  parameters                          Compute parameters (\c cuestDFCoulombComputeParameters_t). Must not be NULL.
 * @param[out] temporaryWorkspaceDescriptor        Output descriptor for temporary workspace sizes. Must not be NULL.
 * @param      densityMatrix                       Optional density matrix (may be NULL).
 * @param      outCoulombMatrix                    Optional output Coulomb matrix (may be NULL).
 *
 * @return
 *   - \c CUEST_STATUS_SUCCESS on success;
 *   - \c CUEST_STATUS_INVALID_HANDLE if the cuEST handle is NULL;
 *   - \c CUEST_STATUS_NULL_POINTER if any required pointer is NULL;
 *   - \c CUEST_STATUS_INVALID_TYPE if opaque handles are not the correct type;
 *   - \c CUEST_STATUS_INVALID_PLAN if the \c cuestDFIntPlan_t is not correctly configured or in a dirty state;
 *   - \c CUEST_STATUS_INVALID_ARGUMENT if a parameter value is invalid, if JIT-compiled kernels are requested
 *     with the cuEST CUDA 12 package, or if ffloat compute is requested while JIT is disabled (incompatible
 *     JIT/ffloat combination);
 *   - \c CUEST_STATUS_EXCEPTION or \c CUEST_STATUS_UNKNOWN_ERROR otherwise.
 */
CUEST_API
cuestStatus_t cuestDFCoulombComputeWorkspaceQuery(
    cuestHandle_t handle,
    const cuestDFIntPlan_t plan,
    const cuestDFCoulombComputeParameters_t parameters,
    cuestWorkspaceDescriptor_t* temporaryWorkspaceDescriptor,
    const double* densityMatrix,
    double* outCoulombMatrix);

#ifdef __cplusplus
}
#endif

#endif /* DENSITY_FITTED_COULOMB_COMPUTE_API_H */
