Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Allow Mat and Vec objects to use existing memory #120

Open
A-CGray opened this issue Nov 13, 2024 · 0 comments
Open

Feature Request: Allow Mat and Vec objects to use existing memory #120

A-CGray opened this issue Nov 13, 2024 · 0 comments

Comments

@A-CGray
Copy link
Contributor

A-CGray commented Nov 13, 2024

Currently, if you pass an existing array to the constructor of an A2D mat/vec, the mat/vec allocates it's own memory and copies the values from the array you supplied. In certain situations it would be useful to have the mat/vec directly use the arrays you've already allocated for the primal value and possibly even the derivatives. This could be useful both for reducing memory usage (e.g in GPU kernels), and in reducing the need to copy out derivative values.

For example:

template<typename T, int N>
void computedfdx(T x[], T dfdx[]){
  ADObj<Vec<T,N>> xVec(x); // At this point we have 4 arrays of length N allocated (x, dfdx, xVec.A, and xVec.Ab)
  ADObj<T> f;
  // Make stack that computes f(x)
  auto stack = MakeStack(...);
  f.bvalue = 1.0;
  stack.reverse();
  // Copy derivative back into dfdx
  for (int ii=0; ii<N;ii++){
    dfdx[ii] = xVec.bvalue[ii];
  }
}

Could instead be implemented as:

template<typename T, int N>
void computedfdx(T x[], T dfdx[]){
  ADObj<Vec<T,N>> xVec(&x, &dfdx); // No additional arrays created here
  ADObj<T> f;
  // Make stack that computes f(x)
  auto stack = MakeStack(...);
  f.bvalue = 1.0;
  stack.reverse();
  // No need to copy derivative back into dfdx
}
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

No branches or pull requests

1 participant