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

Map operation returns array with unnecessary size #29

Open
pdrocaldeira opened this issue Aug 18, 2016 · 1 comment
Open

Map operation returns array with unnecessary size #29

pdrocaldeira opened this issue Aug 18, 2016 · 1 comment
Labels
Milestone

Comments

@pdrocaldeira
Copy link
Member

Consider this map operation code:

        Array<Float32> arrayRetMapImage = image.par().map(Float32.class, new Map<Float32, Pixel>() {
            @Override
            public Float32 function(Pixel pixel) {
                Float32 ret = new Float32();
                ret.value = pixel.rgba.red;
                return ret;
            }
        });
        float[] floatArrayRetMapImage = arrayRetMapImage.toJavaArray();

Code generated by this operation return a 4 * (image size) float array when it shouldn't.

User function returns only a single float so the right way to do this is to analyse what type user function uses and create an array with compatible size.

@wcmjunior wcmjunior added bug and removed enhancement labels Aug 19, 2016
@wcmjunior wcmjunior added this to the version 0.x milestone Aug 19, 2016
@wcmjunior
Copy link
Member

Label updated, since this is a bug.
Just as a future reminder, I'll describe below the cause and the solution for this issue.

The problem is related to those output bind operations which automatically creates the memory allocation to move data from low-level runtime to SDK. Being a little bit more specific, in lines line this:

float[] floatArrayRetMapImage = arrayRetMapImage.toJavaArray();

When translating the output bind, the compiler creates a boolean variable "somethingFromImage" to inform if this array was created after a call to a map operation in an image. If this is true, it will automatically multiply by 4 the size of its element. Thus, the solution must be implemented in three steps:

  1. Change the boolean variable to a short variable and initialize it to 1;
  2. Assign to the variable, after each call to "map" operations, the size of the element being manipulated by the Array at that moment. Example: Pixel = 4, Int32 = 1, Float32 = 1, Int32_2 = 2, Int32_4 = 4, etc.
  3. Use the variable to multiply to the array size.

It may seen odd, but check this example:

Array<Pixel> array = image.par().map(...);
float[] result1 = array.toJavaArray();
array = new Array<Pixel>(Pixel.class, data);
float[] result2 = array.toJavaArray();

In the above example, the code generated in line 2 should multiply array length by 4 in order to properly transform the result into a float[], since the array was created from an image, where each pixel takes 4 floats to be stored in user library. In line 4, though we are handling the same Array, the new reference was created from a constructor call and where the user have already provided the data as a float[] where each pixel is coded as a set of 4 elements, so it does not have to multiply the output array length.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants