stan-dev / stan-dev/stanc3

Optimize code by merging expressions

Open
#860 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

optimization
Dominant language
OCaml
Stars
160
Forks
59
Avg merge
21h 45m
Merged PRs (30d)
26

Description

Now that we are using Eigen expressions in Stan Math it makes sense for compiler to merge multiple expressions into singe one where possible to avoid unnecessary memory accesses. This is possible where we have a variable of matrix, vector or row_vector type (or if using OpenCL any type that is replaced with a matrix_cl) that is used in exactly one place in further code. Such a variable can be removed and its use case replaced with the expression that was assigned to the variable.

For example from stan code:

matrix a, b;
matrix c = a + b;
matrix d = c * 3;

We want to generate (assuming c is not used anywhere else):

Eigen::MatrixXd a, b;
Eigen::MatrixXd d = (a + b) * 3;

instead of:

Eigen::MatrixXd a, b;
Eigen::MatrixXd c = a + b;
Eigen::MatrixXd d = c * 3;

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the Stan-to-C++ expression-generation path illustrated by the matrix, vector, and row_vector examples, and trace how single-use variables are represented. Done means eligible assignments are merged into later expressions to reduce memory accesses, while variables used in multiple places remain available.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, ocaml
Domain
compilers, performance
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.