[Closed] multiThread with Arrays
Hi There,
I guess everyone is interested in the subject a bit.
So, it is still about a geometry exporter. I figured out, if I fill an array withe vertex data it fills extremly quick, and then I will print that out into a file, I will have at least 20-30% performance gain. Now, when I tried to do that in two task at the same time, then the performace dropped 3-400%, I am puzzled, is that possible that maxscript can work only on one array at a time? Here is a very simplified situation below. There is two object $t1 and $t2 (two teapots). So it will cycle through the vertices and then put them formatted into an array. Is that also possible that the getvert command creating the slowdown?
Thanks a lot!
clearlistener()
Global Thread, MainThread, SecondaryThread
global str=#()
global str2=#()
fn geom1 = (
time1 = timestamp()
obj = $t1
nverts = obj.numverts
for i=1 to nverts do (
vert = getvert obj i
str[i] = "vertex"+ " " + vert[1] as string + vert[2] as string + vert[3] as string
)
time1b = timestamp()
finish = (time1b-time1)/1000 as float
print "FIRST TASK"
print finish
)
fn geom2 = (
time2 = timestamp()
obj2 = $t2
nverts2 = obj2.numverts
for k=1 to nverts2 do (
vert2 = getvert obj2 k
str2[k] = "vertex"+ " " + vert2[1] as string + vert2[2] as string + vert2[3] as string
)
time2b = timestamp()
finish2 = (time2b-time2)/1000 as float
print "SECOND TASK"
print finish2
)
MainThread = dotnetobject "System.ComponentModel.BackGroundWorker"
MainThread.WorkerSupportsCancellation = true
dotNet.addEventHandler MainThread "DoWork" geom1
--------------------
SecondaryThread = dotnetobject "System.ComponentModel.BackGroundWorker"
SecondaryThread.WorkerSupportsCancellation = true
dotNet.addEventHandler SecondaryThread "DoWork" geom2
-----------------------
MainThread.RunWorkerAsync()
SecondaryThread.RunWorkerAsync()
Hi losbellos,
I only looked into this on a basic level and from what i could work out was the backgroundworker and synchronizingbackgroundworker classes will allow you to free a computational process but not if it directly calls a function that Maxscript uses (for example getverts() on an object). If you are using it for background calculations whilst still being able to use that instance of max it works as expected. I dont understand the exact deal with which thread is working at any given time in max, but for example mine is currently running 49 threads. Essentially you can use the new thread for calculations, but especially not if you are accessing a maxscript fucntion that references an object created outside the thead. Bobo had mentioned that MXS isnt separate from the main interface, so this would make sense i guess.
I have an other method, which uses the same command, but it writes directly to the hdd that scales pretty well in that sense. On a job with multiple objects 1 thread = 30 sec, 2 thread = 20 sec, 3 thread 38 sec. ( I have only two cores) So I assign as many threads as many processor cores are there.
And that system basically is like:
for v = 1 to num_verts do
(
vert = getVert tmesh v
format " vertex % % %
" vert[1] vert[2] vert[3] to:external_file
)
In the meantime I figured in the array case if I call only one background thread, while the main thing goes as just a normal function call it works. So I guess it was wrong to call two threads in this way, because that way there was no main thread
so put this on the end and see the times…
But still, I cant get the full load from the processors.
geom1()
SecondaryThread.RunWorkerAsync()