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

Introduce DNDarray.__array__() method #1154

Merged
merged 12 commits into from
May 22, 2023
Merged

Conversation

ClaudiaComito
Copy link
Contributor

Description

This method returns a view of the underlying process-local array as numpy ndarray. If the DNDarray is on GPU, the array is copied to CPU first.

With this change, passing a DNDarray to np.asarray() returns a valid np.ndarray. This is needed for interoperability with other libraries in the numpy ecosystem.

Basic example with xarray:

>>> import xarray as xr
>>> import heat as ht
>>> a = ht.arange(10).reshape(5,2)
>>> a
DNDarray([[0, 1],
          [2, 3],
          [4, 5],
          [6, 7],
          [8, 9]], dtype=ht.int32, device=cpu:0, split=None)
>>> xr_a = xr.DataArray(a)
>>> xr_a
<xarray.DataArray (dim_0: 5, dim_1: 2)>
array([[0, 1],
       [2, 3],
       [4, 5],
       [6, 7],
       [8, 9]], dtype=int32)
Dimensions without coordinates: dim_0, dim_1

If the DNDarray is distributed, the example above will return an xarray of the local slice of data on each MPI process.

Related: #1031

Issue/s resolved: #1153

Changes proposed:

  • Implement DNDarray.__array__() method
  • Update documentation of DNDarray.numpy() method
  • Implement tests

Type of change

  • New feature (non-breaking change which adds functionality)

Memory requirements

NA

Performance

NA

Due Diligence

  • All split configurations tested
  • Multiple dtypes tested in relevant functions
  • Documentation updated (if needed)
  • Title of PR is suitable for corresponding CHANGELOG entry

Does this change modify the behaviour of other functions? If so, which?

no

@ghost
Copy link

ghost commented May 19, 2023

👇 Click on the image for a new way to code review

Review these changes using an interactive CodeSee Map

Legend

CodeSee Map legend

@github-actions
Copy link
Contributor

Thank you for the PR!

@github-actions
Copy link
Contributor

Thank you for the PR!

Copy link
Collaborator

@mrfh92 mrfh92 left a comment

Choose a reason for hiding this comment

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

From my point of view the changes look fine 👍
Therefore I recommend merging if the CI succeeds.

@ClaudiaComito ClaudiaComito added this to the 1.3.0 milestone May 19, 2023
@github-actions
Copy link
Contributor

Thank you for the PR!

Copy link
Member

@bhagemeier bhagemeier left a comment

Choose a reason for hiding this comment

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

Looks good to me.

@mrfh92 mrfh92 removed their assignment May 22, 2023
@ClaudiaComito ClaudiaComito merged commit 6205a5e into main May 22, 2023
39 checks passed
@ClaudiaComito ClaudiaComito deleted the features/1153_array_method branch May 22, 2023 07:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement __array__() method for DNDarrays
3 participants