Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Rapidsai Cuml GLM C API

From Leeroopedia


Knowledge Sources
Domains Machine_Learning, Linear_Models
Last Updated 2026-02-08 12:00 GMT

Overview

Provides the C-language API for fitting Generalized Linear Models (GLM) using quasi-Newton optimization on the GPU, with single-precision and double-precision variants.

Description

This header defines C-compatible wrapper functions for the quasi-Newton GLM fitting routines in cuML. It provides two functions:

  • cumlSpQnFit: Fits a GLM using quasi-Newton methods with single-precision (float) data. Takes a C-API handle (cumlHandle_t), model parameters (qn_params), feature matrix, labels, and initial weights, returning the fitted weights, final objective value, and iteration count.
  • cumlDpQnFit: Identical to cumlSpQnFit but operates on double-precision data.

Both functions return cumlError_t status codes for C-compatible error handling. The functions are wrapped in extern "C" when compiled as C++ (within the ML::GLM namespace) and are accessible as plain C functions otherwise.

Usage

Use these functions when interfacing with cuML's GLM fitting from C code, language bindings (Python ctypes, Rust FFI, etc.), or any context requiring a C-compatible ABI. Create a cumlHandle_t first using the C API, then call the appropriate precision variant.

Code Reference

Source Location

  • Repository: Rapidsai_Cuml
  • File: cpp/include/cuml/linear_model/glm_api.h

Signature

cumlError_t cumlSpQnFit(cumlHandle_t cuml_handle,
                        const qn_params* pams,
                        float* X,
                        float* y,
                        int N,
                        int D,
                        int C,
                        float* w0,
                        float* f,
                        int* num_iters,
                        bool X_col_major);

cumlError_t cumlDpQnFit(cumlHandle_t cuml_handle,
                        const qn_params* pams,
                        double* X,
                        double* y,
                        int N,
                        int D,
                        int C,
                        double* w0,
                        double* f,
                        int* num_iters,
                        bool X_col_major);

Import

#include <cuml/linear_model/glm_api.h>

I/O Contract

Inputs

Name Type Required Description
cuml_handle cumlHandle_t Yes C-API cuML handle for GPU resource management
pams const qn_params* Yes Pointer to model parameters (loss type, regularization, etc.)
X float*/double* Yes Device pointer to feature matrix [N x D]
y float*/double* Yes Device pointer to label vector [N]
N int Yes Number of examples (rows)
D int Yes Number of features (columns)
C int Yes Number of output classes (or 1 for regression)
w0 float*/double* Yes (inout) Device pointer to initial weights; overwritten with fitted result
X_col_major bool Yes Whether X is stored in column-major layout

Outputs

Name Type Description
return value cumlError_t CUML_SUCCESS on success, error code on failure
w0 float*/double* Device pointer to the fitted model weights (overwritten in-place)
f float*/double* Host pointer to the final objective function value
num_iters int* Host pointer to the actual number of iterations taken

Usage Examples

#include <cuml/cuml_api.h>
#include <cuml/linear_model/glm_api.h>
#include <cuml/linear_model/qn.h>

void fit_logistic_regression_c_api() {
    // Create cuML handle
    cumlHandle_t handle;
    cudaStream_t stream;
    cudaStreamCreate(&stream);
    cumlCreate(&handle, stream);

    int N = 500;   // samples
    int D = 20;    // features
    int C = 2;     // binary classification

    // Configure QN parameters
    qn_params params;
    params.loss = QN_LOSS_LOGISTIC;
    params.penalty_l1 = 0.0;
    params.penalty_l2 = 1.0;
    params.fit_intercept = true;
    params.max_iter = 100;

    // Allocate device memory
    float* X;    // feature matrix [N x D]
    float* y;    // labels [N]
    float* w0;   // weights [(D+1) * C]
    cudaMalloc(&X, N * D * sizeof(float));
    cudaMalloc(&y, N * sizeof(float));
    cudaMalloc(&w0, (D + 1) * C * sizeof(float));
    cudaMemset(w0, 0, (D + 1) * C * sizeof(float));

    // Load data into X and y...

    float f;
    int num_iters;

    // Fit logistic regression using single-precision C API
    cumlError_t err = cumlSpQnFit(handle, &params, X, y, N, D, C,
                                   w0, &f, &num_iters, false);
    if (err != CUML_SUCCESS) {
        // Handle error...
    }

    // Clean up
    cudaFree(X);
    cudaFree(y);
    cudaFree(w0);
    cumlDestroy(handle);
    cudaStreamDestroy(stream);
}

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment