Jump to content

Best way to find Max & Min in array subset


Recommended Posts

Hello,

I am writing an application that loads a set of temperature vs. time data from a text file and displays the data on a graph. At the time the data is loaded, I run it through the Array Max/Min function to calculate these values and display them on the screen. The user can zoom in on part of the graph using the built-in zoom capability. Now I would like to recalculate the max and min values within the window that the user has selected. (I would like to maintain all of the original data in case the user zooms back out.)

I am trying to determine the most efficient way to accomplish this. What I have done so far is to get the chart x-scale minimum and maximum values from a chart property node, then find the index of these values in the original data array. I then use the indices with Array Subset to remove first the end, then the beginning from a copy of the array. Finally, I pass the data to the Array Max & Min function. This method seems to be very slow. This may be because it requires me to make copies of a large data array.

Is there a better way to do this without manipulating large arrays too much? Would it be faster to just iterate through the array and start capturing the max and min values only after the chart x-scale min is found? Ideally, I would like to create a version of the Max & Min function that takes a start and end index. Has anyone written such a function that is relatively fast?

Thank you,

Peter Laskey

Link to comment

Attached is the SubVI that I wrote to iterate through the values of the array (ignoring values outside the start and end x index values) and storing the maximum and minimum values. The VI uses minimal buffer allocations so it is probably not too hard on the memory manager (versus array subset, array index, and delete from array). The question is, is there a faster way to accomplish this?

I wrote this VI in LabVIEW 8.6.

-Peter

Link to comment

Thank you for the information. I fixed the bug that you noticed so my VI now detects the minimum correctly. I'll post my fixed VI here in case anyone else in interested in trying this method. I will be looking at your example this morning - I think it shows exactly what I need.

-Peter

Link to comment

If you care about memory usage, you need to make sure the subset array wire is a true subarray, and you need to scrutinize the buffer allocations nearby on the original array wire. AFAIK, the only way to know about subarrays is to look at the wire's context help.

Make sure you read this thread about sub-arrays

Even if the min-max is performed on a subarray, you need to check the array going into everything. If you run jgcode's fine VI, I think the caller will often have to make a copy of the entire source array, which you can detect by viewing the buffer allocations. If you took that VI and wired the array in and passed it back out, and did not fork the wire in the caller, you can probably avoid any array copies. Notice I am hedging here because it's hard to find clear documentation on this complex subject.

Link to comment

QUOTE (jdunham @ Oct 17 2008, 02:30 AM)

Even if the min-max is performed on a subarray, you need to check the array going into everything. If you run jgcode's fine VI, I think the caller will often have to make a copy of the entire source array, which you can detect by viewing the buffer allocations. If you took that VI and wired the array in and passed it back out, and did not fork the wire in the caller, you can probably avoid any array copies. Notice I am hedging here because it's hard to find clear documentation on this complex subject.

Would running the above VI and forking data to the subvi in an inplaceness structure guarantee that a copy will not be made?

Link to comment

QUOTE (jgcode @ Oct 16 2008, 05:28 PM)

Would running the above VI and forking data to the subvi in an inplaceness structure guarantee that a copy will not be made?

I don't think so, though I was hoping for someone who knows more than me to be chiming in by now.

It helps to think about the requirements on LabVIEW. When you fork a wire, LabVIEW can only continue sharing the data between the wires if it knows no changes to either side will violate the integrity of data on the the other side of the fork. If you fork a wire and send one tine into a subvi, then that subVI might modify the array, so the caller is going to have to make a copy to protect the other tine(s) on the calling diagram. If you wire the array into the subvi and back out, then you can avoid the fork and the caller won't have to make copies to protect the data.

Also, if one side of the fork doesn't change the data, like it just gets the array size, then there should be no extra buffer allocation. Even if you send the other tine into some mysterious subvi, the compiler is smart enough to take the size first before it allows the subvi call to have its way with the data. Use the Show Buffer Allocations tool to get the actual situation for a particular diagram.

I think the inplaceness structure is just a hint, since you can't force the compiler to violate dataflow.

This would all make sense with pictures and circles and arrows, and I just don't have the time or knowledge to do that right.

Link to comment

QUOTE (jdunham @ Oct 17 2008, 11:08 AM)

It helps to think about the requirements on LabVIEW. When you fork a wire, LabVIEW can only continue sharing the data between the wires if it knows no changes to either side will violate the integrity of data on the the other side of the fork. If you fork a wire and send one tine into a subvi, then that subVI might modify the array, so the caller is going to have to make a copy to protect the other tine(s) on the calling diagram. If you wire the array into the subvi and back out, then you can avoid the fork and the caller won't have to make copies to protect the data.

Also, if one side of the fork doesn't change the data, like it just gets the array size, then there should be no extra buffer allocation. Even if you send the other tine into some mysterious subvi, the compiler is smart enough to take the size first before it allows the subvi call to have its way with the data. Use the Show Buffer Allocations tool to get the actual situation for a particular diagram.

Thanks jdunham

With passing an array into and out of the subvi: you could still edit the array in the subVI and pass out the same data type but with a different allocation (e.g. perform a build array).

So I am wondering how smart the compiler is?

Isn't the point of inplaceness to guarentee buffer reuse.

I can see the problem if another operation was made in parallel in the inplaceness structure...

But I would love to know if the subvi was in an inplaceness structure and no other functions were run on the data in the structure - would buffer reuse be guaranteed :question:

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.