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

How to handle 1D and 2D Arrays? #73

Open
tpietzsch opened this issue Oct 9, 2024 · 3 comments
Open

How to handle 1D and 2D Arrays? #73

tpietzsch opened this issue Oct 9, 2024 · 3 comments

Comments

@tpietzsch
Copy link
Contributor

Arrays have width, height, and depth, as well as number of dimensions.

I wonder how to consistently handle construction and reporting of these properties. There were tests (TestAbsolute) that did construct arrays with width=2, height=2. depth=0, numDimensions=2. However, the resulting array would report depth()==1.

I see the following approaches:

  1. Use depth=0 to indicate that numDimensions<3. Use depth=0 and height=0 to indicate that numDimensions=1. Then we don't need the numDimensions argument explicitly.
    However, then ArrayJ.depth() should be 0, too, not 1.
    This might be inconvenient. For example, it is nice if the number of elements of an array can always be computed as width*height*depth, regardless of dimensionality.

  2. Require depth>0 always. Require depth=1 if numDimensions<3.
    This is what I'm currently implementing in Revise JavaCPP bindings and core package #72.
    However, it is also not super-elegant.

  3. I also implemented a var-args alternative that takes int... size. That takes 1, 2, or 3 arguments and the number of arguments is the number of dimensions, the "missing" dimensions are set to 1. This is available through DeviceJ.createArray(DataType, MemoryType, int...) in Revise JavaCPP bindings and core package #72.
    For example the above TestAbsolute example (2 by 2 image, 2D) would be constructed as
    device.createArray(DataType.UINT8, MemoryType.BUFFER, 2, 2), a 3D 4x3x2 image would be constructed with
    device.createArray(DataType.UINT8, MemoryType.BUFFER, 4, 3, 2), etc.
    If this works well, I would remove the direct ArrayJ constructor (make it package-private).

@StRigaud
Copy link
Member

StRigaud commented Oct 9, 2024

Hi @tpietzsch,

Thanks for all the work and improvement!

A quick info here about the dimensionnality of arrays and how it is processed below:

  • GPU requires a region to process (w,h,d) of the buffer where the minimum value accepted is 1, hence bellow all code, all Array are kind of 3D (e.g. scalar is a buffer region of (1,1,1) )
  • Hence, I already enforce, in the C++ code, a w,h,d parameters >= 1 at the array creation.
  • The dimension parameter requested at the Array creation is a (bad) patch on my side to fit to the numpy API for the python side (numpy allows you to create a 2D and 3D array both with the same dim=[x,y,1] and I needed to keep the difference).

from memory, this does not really have an impact computationnaly in the array processing after, so we can manage it the way we want on the Java side (including hiding/removing it), especially if we assume that array of dimension [x,y,1] are always 2D.

In future I will try to better manage this from the python side and remove it from the C++ side.

@StRigaud
Copy link
Member

Just to clarify:

  • I would not be in favor of option 1 because like I said, i enforce width, height and depth to be >0.
  • Option 2 is not elegant but fit to the logic.
  • The option 3 is looks perfect to me, if we take the principe that a 2 by 2 image cannot be 3D (which I am very fine with!).

@tpietzsch
Copy link
Contributor Author

Yes, I also like option 3 the best

  • The option 3 is looks perfect to me, if we take the principe that a 2 by 2 image cannot be 3D (which I am very fine with!).

Right, one would have to explicitly make a 2 by 2 by 1 image to get 3D. 👍

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

2 participants