Skip to content

Enable two-dimensional array in the existing data structure#51

Merged
mphoward merged 13 commits intorefactor-anisofrom
feature/ndarray
Mar 19, 2025
Merged

Enable two-dimensional array in the existing data structure#51
mphoward merged 13 commits intorefactor-anisofrom
feature/ndarray

Conversation

@mayukh33
Copy link
Contributor

No description provided.

@mayukh33 mayukh33 changed the title Enable two-dimenisonal array in the existing data structure Enable two-dimensional array in the existing data structure Feb 20, 2025
@mayukh33 mayukh33 requested a review from mphoward February 21, 2025 16:46
Copy link
Contributor

@mphoward mphoward left a comment

Choose a reason for hiding this comment

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

Generally a good first pass! Here are some comments that need addressing / clarification.

{
class DataLayout
{
//! DataLayout class generates the template for the custom data types used in the software
Copy link
Contributor

Choose a reason for hiding this comment

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

These are not proper doxygen comments. Please read about how you mark either short vs. long descriptions, and we can discuss if you have questions.

Comment on lines +10 to +13
DataLayout::DataLayout()
{
const std::vector<int>& shape_ = {0, 0};
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Wouldn't the default be an empty array now?

Suggested change
DataLayout::DataLayout()
{
const std::vector<int>& shape_ = {0, 0};
}
DataLayout::DataLayout()
{
}

Comment on lines +19 to +20
//! Strided indexing scheme for converting
//! N dimensional array into one dimensional array
Copy link
Contributor

Choose a reason for hiding this comment

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

Doxygen comments do not go in regular code, use regular comments

int DataLayout::size() const
std::vector<int> DataLayout::size() const
{
return shape();
Copy link
Contributor

Choose a reason for hiding this comment

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

This isn't right anymore: you want to take the product of the shape_ to return the total size

reference operator()(const std::vector<int>& idx) const
{
return data_[layout_(start_ + idx)];
std::transform(idx.begin(), idx.end(), start_, idx.begin(), std::plus<int>());
Copy link
Contributor

Choose a reason for hiding this comment

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

What is this doing, and why can't you just pass the idx directly? It doesn't seem like this should do anything / work because idx is const.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was trying elementwise summation of the idx vector with start_ to replicate the operation performed in the previous implementation of this function without using loops

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, got it! Yes, this is necessary then, but you should use a new local variable to hold the result. std::transform is essentially a loop, so I doubt it will be more performant than a loop.

}

private:
/// @brief
Copy link
Contributor

Choose a reason for hiding this comment

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

???

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That was auto-generated by accident. I will remove it

@mayukh33 mayukh33 requested a review from mphoward February 25, 2025 16:24
Comment on lines +10 to +15
//! DataLayout
/*!
* Generates data structure for the custom data types used in the software.
* The class provides get and set functions for the array index and implements N-dimensional array
* mapping into one dimensional array using strided indexing scheme.
*/
Copy link
Contributor

Choose a reason for hiding this comment

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

The short description should be what the object is (the class name will be part of what doxygen generates), then the long description should describe what the class does (potentially with examples longer term)

Suggested change
//! DataLayout
/*!
* Generates data structure for the custom data types used in the software.
* The class provides get and set functions for the array index and implements N-dimensional array
* mapping into one dimensional array using strided indexing scheme.
*/
//! Multidimensional array layout
/*!
* DataLayout maps an N-dimensional index to one-dimensional
* index using row-major ordering. Negative indexes are supported
* and interpreted as relative to the zero-index.
*/

Comment on lines +20 to +23
//! Constructor for the DataLayout
/*!
* \param shape_ Shape of the index array
*/
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't include class name in short descriptions. Use periods to end parameter descriptions. Parameter name needs to match argument name.

Suggested change
//! Constructor for the DataLayout
/*!
* \param shape_ Shape of the index array
*/
//! Constructor
/*!
* \param shape Number of elements in array along each dimension.
*/

Comment on lines +26 to +27
// Getter for the class
int operator()(const std::vector<int>& idx) const;
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not a doxygen comment and not descriptive

Suggested change
// Getter for the class
int operator()(const std::vector<int>& idx) const;
//! Map a multidimensional index to a one-dimensional index
/*!
* \param idx Multidimensional index.
* \return One-dimensional index.
*
* Row-major ordering is used.
*/
int operator()(const std::vector<int>& idx) const;

Comment on lines +29 to +30
// Setter for shape
std::vector<int> shape() const;
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not doxygen and it is also not a setter. It's OK, though, to only use a short description for getters.

Suggested change
// Setter for shape
std::vector<int> shape() const;
//! Shape of the layout in each dimension
std::vector<int> shape() const;


// Setter for shape
std::vector<int> shape() const;
// Getter for shape
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// Getter for shape
//! Total number of elements in the layout

Comment on lines 34 to 35
bool operator==(const DataLayout& other) const;
bool operator!=(const DataLayout& other) const;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
bool operator==(const DataLayout& other) const;
bool operator!=(const DataLayout& other) const;
//! Test if two layouts are equal
/*!
* \return True if the layouts have the same shape.
*/
bool operator==(const DataLayout& other) const;
//! Test if two layouts are not equal
bool operator!=(const DataLayout& other) const;


private:
int shape_;
std::vector<int> shape_;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
std::vector<int> shape_;
std::vector<int> shape_; //!< Multidimensional shape of layout

{

DataLayout::DataLayout() : shape_(0) {}
// Default constructor
Copy link
Contributor

Choose a reason for hiding this comment

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

I tend to like putting all the doxygen comments in the header. They only need to be in one spot.

{
//! Multidimensional array layout
/*!
* DataLayout maps an N-dimensional index to one-dimensional
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* DataLayout maps an N-dimensional index to one-dimensional
* DataLayout maps an N-dimensional index to a one-dimensional

public:
DataLayout();
explicit DataLayout(int shape_);
//! Constructor for the DataLayout
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
//! Constructor for the DataLayout
//! Constructor

explicit DataLayout(int shape_);
//! Constructor for the DataLayout
/*!
* \param shape_ Shape of the index array.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* \param shape_ Shape of the index array.
* \param shape Shape of the index array.

//! Shape of the layout in each dimension
std::vector<int> shape() const;

//! Shape of the layout in each dimension
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
//! Shape of the layout in each dimension
//! Total number of elements in the layout

/*!
* DataView provides ability to view a certain section of the multidimensional array
*/

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change

Comment on lines 97 to 107
//! Overloading the constructor
/*!
* \param data_ Pointer to the data.
* \param layout_ Layout of the data.
* \param start_ Start of the array.
* \param end_ End of the array.
*/

DataView() : DataView(nullptr, DataLayout()) {}

DataView(pointer data, const DataLayout& layout) : DataView(data, layout, 0, layout.shape()) {}
Copy link
Contributor

Choose a reason for hiding this comment

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

You need to document each constructor with the actual arguments of it, as they are named in the signature of the function. Documentation is needed for the third constructor still along with a short description.

Suggested change
//! Overloading the constructor
/*!
* \param data_ Pointer to the data.
* \param layout_ Layout of the data.
* \param start_ Start of the array.
* \param end_ End of the array.
*/
DataView() : DataView(nullptr, DataLayout()) {}
DataView(pointer data, const DataLayout& layout) : DataView(data, layout, 0, layout.shape()) {}
//! Empty constructor
/*!
* The default is a null pointer to a zero-size layout.
*/
DataView() : DataView(nullptr, DataLayout()) {}
//! Constructor
/*!
* \param data Pointer to the data.
* \param layout Layout of the data.
*/
DataView(pointer data, const DataLayout& layout) : DataView(data, layout, 0, layout.shape()) {}

}

reference operator()(int idx) const
//! Map multi-dimensional array to one dimension
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not what this function does... it returns a reference to the referenced element of the data.

Comment on lines 126 to 136
//! Shape of the one-dimensional array
int shape() const
{
return end_ - start_;
}

//! Number of elements in the one-dimensional array
int size() const
{
return shape();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

These are not correct for a multidimensional layout... the shape should be multidimensional. Additionally, I am noticing that start and end need to be generalized to be multi-indexes.

size should be the total number of elements in the view, though.

Comment on lines +138 to 145
//! Test if pointer to the data is not a null pointer
/*!
* \return True if pointer is not a null pointer.
*/
explicit operator bool() const
{
return (data_ != nullptr);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This comment is describing the implementation, not the actual method. A DataView is not null if it is a view of valid data.

@mayukh33 mayukh33 requested a review from mphoward March 17, 2025 21:50
Copy link
Contributor

@mphoward mphoward left a comment

Choose a reason for hiding this comment

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

This looks good to go! I had one small comment: let me know if you want to make that change, and otherwise I'll merge so we can move onto the next steps.

Comment on lines +34 to +40
int N = shape_.size();
int size = 1;
for (int i = 0; i < N; ++i)
{
size *= shape_[i];
}
return size;
Copy link
Contributor

Choose a reason for hiding this comment

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

Could this be done with std::accumulate like you did in data_view.h? I'm good with this solution if you prefer, though!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will change this to the same implementation as the data_view.h one so that it is consistent. Thank you for noticing it!

@mayukh33 mayukh33 requested a review from mphoward March 18, 2025 22:55
@mphoward mphoward changed the base branch from main to refactor-aniso March 19, 2025 15:57
Copy link
Contributor

@mphoward mphoward left a comment

Choose a reason for hiding this comment

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

Thanks! I just created a new branch refactor-aniso that we can use to stage these changes as we work on them. Please base your next branches and PRs off refactor-aniso, thanks!

@mphoward mphoward merged commit c2dea16 into refactor-aniso Mar 19, 2025
1 check passed
@mphoward mphoward deleted the feature/ndarray branch March 19, 2025 15:58
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.

2 participants