Jump to content

Is it possible to use VEX for accumulated calculations?


Recommended Posts

Basically I am wondering if it's possible to calculate accumulated values using VEX?

Let's say a basic example of calculating the average or sum of all points.

Something like:

vector sum;

sop vexop()
{
    sum += P;
}

AFAIK VEX doesn't allow this kind of shared/static variables outside functions, right?

How would one do something like this? Can you do it using a detail attribute? I couldn't find a way to access them in VEX.

So I thought of doing this:

sop vexop()
{
    if (ptnum == 0)
    {
        vector sum;
        for (int i=0; i<Npt; i++)
        {
            vector p;
	    import ( "P", p, 0, i );
            sum += p;
        }
    }
}

Is this reasonable? Also how can I get this value out reliably? Right now I just print it. If I stored it in an attribute, the last point would have the correct value (if VEX loops points in order)?

Thanks :)

Edited by magneto
  • Like 1
Link to comment
Share on other sites

Basically it won't work, unless you're in POPs, or inside SolverSOP. VEX virtual machine has a very specific architecture:

1) Copy attributes from geometry into Vex memory arrays.

2) Split array into chunks.

3) Run vex code in one thread per chunk, one time per point.

4) Copy all modified arrays back onto geometry.

Accumulating values, recursion etc, are possible only outside VEX.

As to attributes, you can access detail's in the same way as points' attributes. VEX will find it by name.

  • Like 4
Link to comment
Share on other sites

Thanks Symek. I understand it now.

So you wouldn't use it even for 1 time accumulation-like calculations? By this I mean things like sum/average but only once, but not time/history dependent like solver SOP.

I wanted to use VEX because Python version way very slow, so thought VEX might be much faster, and didn't want to dip into HDK yet :)

Link to comment
Share on other sites

Guest mantragora

...and didn't want to dip into HDK yet :)

Heh, well, HDK is not a one day trip for sure ;). But you can try InlineCPP. That's like one-two week trip to get to now each other a little.

Edited by mantragora
  • Like 1
Link to comment
Share on other sites

Yeah I need to make some solid time before I cross the HDK waters :)

@Symek, I knew I could call VEX in Python, but if I can't use VEX for this code, then it doesn't matter for me for this case.

Inlinecpp looks like something I might try :)

Link to comment
Share on other sites

Sorry if I'm stating the obvious here but using Attribute Promote sum attributes to Detail class is pretty handily the speed and ease of use champion. If you can work that in as a precursor to whatever you're putting together you'll be well ahead of any other solution I can imagine.

  • Like 2
Link to comment
Share on other sites

Is this reasonable? Also how can I get this value out reliably? Right now I just print it. If I stored it in an attribute, the last point would have the correct value (if VEX loops points in order)?

Offhand, the hack should work in theory to me. Have you tried some simple examples?Of course, it's not clear to me if this will be fast at all.

Link to comment
Share on other sites

Thanks Edward, I tried. It seems to work :) In fact it was pretty fast, still have to measure because I haven't finished it completely.

But one thing I would appreciate if you can clear for me is if VEX loops through points in order? Like is it guaranteed for 0 to be executed before 1, and 1 before 2, etc?

This is not relevant for my case because I use my own for loop, but still thought I would ask :)

Link to comment
Share on other sites

No, points in VEX are not looped in order. If you think about it, there's no way for it to do that for multi-threading. Of course, you can always get lucky. :) The points are processed in "chunks" at a time so for a small number of points less than the "chunk" size, it won't be threaded and you may get it processed in ascending/descending order.

  • Like 2
Link to comment
Share on other sites

VEX is based on SIMD architecture

each instruction is run on all points before the next one

so there is no way to access result value of any points to compute another, it simply doesn't exist yet, the only data you have access to are from input geometry

and this is regardless of thread settings

  • Like 2
Link to comment
Share on other sites

Would it be just like you said, if you set your VEX node to "No Threading"? I thought maybe that one might be handled differently.

<shrugs> It may work today (or not) but expect the behaviour to change at any time. Basically, the order is *undefined*, so don't rely on it.

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

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...