Skip to content

Add a vector class to GridKit#418

Open
pelesh wants to merge 9 commits into
developfrom
slaven/vector
Open

Add a vector class to GridKit#418
pelesh wants to merge 9 commits into
developfrom
slaven/vector

Conversation

@pelesh
Copy link
Copy Markdown
Collaborator

@pelesh pelesh commented May 29, 2026

Description

Add vector class to GridKit to replace std::vector and provide portable data management.

Proposed changes

The LinearAlgebra::Vector class was ported from Re::Solve. It has basic initialization and data management functionality. I can be use as a vector, multivector or vector view class.

Checklist

  • All tests pass.
  • Code compiles cleanly with flags -Wall -Wpedantic -Wconversion -Wextra.
  • The new code follows GridKit™ style guidelines.
  • There are unit tests for the new code.
  • The new code is documented.
  • The feature branch is rebased with respect to the target branch.
  • I have updated CHANGELOG.md to reflect the changes in this PR. If this is a minor PR that is part of a larger fix already included in the file, state so.

Further comments

The multivector capability in this class should replace DenseMatrix object in GridKit.

@pelesh pelesh self-assigned this May 29, 2026
@pelesh pelesh added the enhancement New feature or request label May 29, 2026
Copy link
Copy Markdown
Collaborator

@PhilipFackler PhilipFackler left a comment

Choose a reason for hiding this comment

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

For an all-in-one solution like this, I would prefer some revisions as explained in my line comments. None of those is a deal-breaker if this is meant only for "advanced" use-cases, but I still think it would be helpful.

Comment thread GridKit/LinearAlgebra/Vector/Vector.hpp
Comment thread GridKit/LinearAlgebra/Vector/Vector.cpp Outdated
* you are doing.
*/
template <typename ScalarT, typename IdxT>
int Vector<ScalarT, IdxT>::setData(ScalarT* data, memory::MemorySpace memspace)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This would make more sense as part of a constructor. An instance should be constructed with ownership and data (if non-owning) for host and device respectively. Ownership should not be allowed to change. And I think the data you're wrapping in the non-owning case shouldn't be allowed to change either. This function wouldn't exist in that case.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I agree, changing ownership after construction is looking for trouble.

This method was experimental and should not be a part of the vector class.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This method was used to get a vector from a multivector. Better solution is to construct non-data-owning vector object instead, or create a separate VectorView object as @lukelowry suggested.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I restricted this function to be able to operate only on vectors with data not allocated. Intended use would be:

Vector v(n)
...
double* data = new double[n];
...
v.setData(data);

The danger is still that user needs to set data that matches the vector size. This is a makeshift solution to create vector views for now.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is why I think it should be part of the constructor.

Vector v;
...
double* data = new double[n];
...
v = Vector(n, data);

Comment thread GridKit/LinearAlgebra/Vector/Vector.cpp Outdated
@pelesh
Copy link
Copy Markdown
Collaborator Author

pelesh commented May 30, 2026

For an all-in-one solution like this, I would prefer some revisions as explained in my line comments. None of those is a deal-breaker if this is meant only for "advanced" use-cases, but I still think it would be helpful.

All of your suggestions are spot on and should be incorporated before this PR is merged.

Copy link
Copy Markdown
Collaborator

@lukelowry lukelowry left a comment

Choose a reason for hiding this comment

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

I think we need a zombie approach if we do merge this implementation. We should take the best parts of this and graft them onto a more scalable implementation that can isolate the Component in memory (is "closure" the right word here?).

Comment thread GridKit/LinearAlgebra/Vector/Vector.cpp
Comment thread GridKit/LinearAlgebra/Vector/Vector.cpp
Comment on lines +165 to +175
switch (memspace)
{
case HOST:
setHostUpdated(true);
setDeviceUpdated(false);
break;
case DEVICE:
setHostUpdated(false);
setDeviceUpdated(true);
break;
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

What if the memspace not passed to the function had already been tagged as updated?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

It will be tagged as out of date. This is desired behavior.

@pelesh pelesh requested a review from shakedregev June 5, 2026 20:22
Copy link
Copy Markdown
Collaborator

@nkoukpaizan nkoukpaizan left a comment

Choose a reason for hiding this comment

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

A few minor comments.

I'm not convinced the class in GridKit needs to mirror Re::Solve, and I tend to prefer solutions that are driven by the application needs first, but I'm willing to see what happens when we try to use this in GridKit.

#include <GridKit/LinearAlgebra/MemoryUtils.hpp>
// #include <resolve/vector/Vector.hpp>
// #include <resolve/vector/VectorHandler.hpp>
// #include <resolve/workspace/LinAlgWorkspace.hpp>
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I recommend removing references to resolve, even if commented.

result += test.setToConst(50);
}

#ifdef GRIDKIT_USE_GPU
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I don't recall that we've defined this yet. Now may be a good time. Need to clarify GRIDKIT_ENAGLE_ vs GRIDKIT_USE_ if we choose to differentiate between CMake options and pre-cprocessor directives.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I would use the same names for both.

result += test.setData(50);

result += test.copyFromExternal(50);
// result += test.copyToExternal(50);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why is this test commented out?

Copy link
Copy Markdown
Collaborator

@shakedregev shakedregev left a comment

Choose a reason for hiding this comment

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

Address the comment and clarify that this is ready for testing.

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

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants