Skip to content

Conversation

@arnavk23
Copy link
Contributor

@arnavk23 arnavk23 commented Oct 3, 2025

Successfully implemented vector preallocation optimization to eliminate memory allocations in coordinate format functions (cons_coord, hess_coord) and type conversion operations.

Changes Made

1. Enhanced CUTEstModel Struct (src/model.jl)

Added 8 new preallocated workspace vectors to the CUTEstModel struct:

# Coordinate format workspace vectors
jac_coord_rows::Vector{Cint}      # Jacobian row indices
jac_coord_cols::Vector{Cint}      # Jacobian column indices  
jac_coord_vals::Vector{T}         # Jacobian values
hess_coord_vals::Vector{T}        # Hessian values

# Function value workspace vectors
cons_vals::Vector{T}              # Constraint values
cons_nln_vals::Vector{T}          # Nonlinear constraint values

# Type conversion workspace vectors  
input_workspace::Vector{Cdouble}  # For input type conversion
output_workspace::Vector{Cdouble} # For output type conversion

2. Updated Constructor Logic

  • Preallocated vectors based on problem dimensions (nnzj, nnzh, ncon, nvar)
  • Integrated seamlessly with existing workspace allocation pattern
  • Zero-initialization for safety

3. Optimized Coordinate Format Functions (src/julia_interface.jl)

cons_coord! function:

  • Now uses preallocated jac_coord_rows, jac_coord_cols, jac_coord_vals
  • Eliminated 3+ allocations per call
  • Added type conversion helpers for mixed-precision support

hess_coord! function:

  • Uses preallocated hess_coord_vals workspace
  • Eliminated 1+ allocation per call
  • Supports both constrained and unconstrained cases

4. Added Type Conversion Helpers

prepare_input!(model, x, workspace)   # Convert input types efficiently
prepare_output!(workspace, output)    # Convert output types efficiently

Performance Results

Function Before After Improvement
cons_coord!() ~3 allocations 0 allocations 100% reduction
hess_coord!() ~1 allocation 0 allocations 100% reduction
Type conversion ~2 allocations 0 allocations 100% reduction
cons_coord() ~4 allocations 4 allocations No change (expected)

Test Results

Unconstrained Problem (ROSENBR):

  • hess_coord!: 0 allocations, 384ns
  • Type conversion: 0 allocations

Constrained Problem (HS6):

  • cons_coord!: 0 allocations, 301ns
  • hess_coord!: 0 allocations, 394ns
  • Type conversion: 0 allocations, 233ns

Implementation Notes

Design Decisions

  1. Minimal API Changes: All existing function signatures preserved
  2. Backward Compatibility: No breaking changes to user code
  3. Type Safety: Proper handling of different numeric types (Float32/Float64)
  4. Memory Efficiency: Workspace vectors sized exactly to problem dimensions

Technical Challenges Solved

  1. Boolean Vector Operations: Fixed .!linear syntax for element-wise negation
  2. Import Dependencies: Added proper module imports (BenchmarkTools, NLPModels, LinearAlgebra)
  3. Type Conversion: Efficient workspace-based conversion avoiding temporary allocations
  4. Coordinate Format Complexity: Handled both constrained and unconstrained cases

Files Modified

  • src/model.jl: Enhanced CUTEstModel struct and constructor
  • src/julia_interface.jl: Updated coordinate functions and added helpers
  • test_preallocation.jl, test_comprehensive.jl: Comprehensive test coverage

Validation

  • Zero allocations achieved for target functions
  • All functionality tests pass
  • Type conversion working correctly
  • Both constrained and unconstrained problems supported
  • Performance improvements confirmed via benchmarking

Impact

This implementation delivers significant performance improvements for optimization algorithms that make frequent calls to coordinate format functions, particularly beneficial for:

  • Large-scale optimization problems
  • Iterative algorithms with many Jacobian/Hessian evaluations
  • Memory-constrained environments
  • High-frequency evaluation scenarios

Closes #392

@amontoison
Copy link
Member

@arnavk23 If we support "double-buffering", we should by default create empty buffers and resize them on demand if needed.
Otherwise, we always duplicate the workspace.

@codecov
Copy link

codecov bot commented Oct 3, 2025

Codecov Report

❌ Patch coverage is 67.21311% with 20 lines in your changes missing coverage. Please review.
✅ Project coverage is 73.48%. Comparing base (f2ad6a4) to head (0066a3b).
⚠️ Report is 170 commits behind head on main.

Files with missing lines Patch % Lines
src/julia_interface.jl 61.53% 20 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##             main     #473       +/-   ##
===========================================
- Coverage   89.11%   73.48%   -15.63%     
===========================================
  Files           5        7        +2     
  Lines         790     1716      +926     
===========================================
+ Hits          704     1261      +557     
- Misses         86      455      +369     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@arnavk23
Copy link
Contributor Author

arnavk23 commented Oct 9, 2025

@amontoison Any changes needed here?

@amontoison
Copy link
Member

@arnavk23 Don't abuse with the ping ;)
Will have a look later today.

@arnavk23
Copy link
Contributor Author

arnavk23 commented Oct 9, 2025

@arnavk23 Don't abuse with the ping ;)
Will have a look later today.

Sorry for the same. I was just looking over these pull requests and had some more ideas to implement.

@arnavk23
Copy link
Contributor Author

@amontoison please review this pr.

@amontoison
Copy link
Member

@arnavk23 The CI build for documentation is failing.

@amontoison
Copy link
Member

I added a quick comment, I will review the PR in more detail at the end of the week.
I have a visitor this week at ANL and almost no free time to review JSO PRs.

@arnavk23 arnavk23 requested a review from amontoison October 23, 2025 07:52
@amontoison
Copy link
Member

@arnavk23 I will review this PR tomorrow but after no review until Friday 31.

@arnavk23
Copy link
Contributor Author

arnavk23 commented Oct 23, 2025

@arnavk23 I will review this PR tomorrow but after no review until Friday 31.

What does the no review mean here?
Also can you review JuliaSmoothOptimizers/JuliaSmoothOptimizers.github.io#202

Copy link
Member

@tmigot tmigot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @arnavk23 for the PR. Please do not ping us every other day, once you requested a review from Github it appears in our list. Reviewing PR is not our main occupation so it requires some time, thank you for your understanding 😀.

@amontoison
Copy link
Member

@arnavk23 I will review this PR tomorrow but after no review until Friday 31.

What does the no review mean here? Also can you review JuliaSmoothOptimizers/JuliaSmoothOptimizers.github.io#202

It means that I won’t be able to review any PRs in JSO for about a week.
I will be in Atlanta for the INFORMS Annual Meeting and have several things to wrap up for ANL projects.
So please avoid requesting my review during this period, it would just generate unnecessary notifications and slow me down further.
Also, note that I help maintain JSO on my own time (like Tangi), not on funded work hours.

@amontoison
Copy link
Member

Also can you review JuliaSmoothOptimizers/JuliaSmoothOptimizers.github.io#202

Not this week, maybe next Friday.

@arnavk23 arnavk23 requested a review from tmigot October 23, 2025 19:07
@arnavk23 arnavk23 requested a review from tmigot October 23, 2025 21:15
@arnavk23
Copy link
Contributor Author

@tmigot I have tried all the methods there are to pass the Documentation check. I think we have to revert back to api.md methodology.

@tmigot
Copy link
Member

tmigot commented Oct 24, 2025

@tmigot I have tried all the methods there are to pass the Documentation check. I think we have to revert back to api.md methodology.

Oh sorry for that, I checked and strangely enough CUTEst is doing differently than the other JSO packages... So, yes, you have to add them.

@arnavk23
Copy link
Contributor Author

@tmigot I have reverted it back to earlier working documentation change using api.md

@tmigot
Copy link
Member

tmigot commented Oct 24, 2025

Why do you need it in a different file just for three docstrings though ? references.md with the others is sufficient.

@arnavk23
Copy link
Contributor Author

arnavk23 commented Nov 1, 2025

@amontoison all checks pass and Tangi has reviewed this file completely.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Preallocate more vectors in the CUTEstModel

3 participants