[Autogenerated] we've seen the previous clip that we don't get linear
improvement when we increase the number of threads here.
And also we get the strange behavior perhaps that more threats
than processors provides even better performance,
which is really counterintuitive and doesn't really sit well
with what we've seen in the course so far.
So both of these things, in fact, can be explained by exactly the same thing.
And here's a hint for you if you didn't figure that out.
The hint is everything lies in this little function.
And the thing is really that all the threats are
not doing the same kind of work.
They're not doing the same amount of work,
although we've chunked up the range of numbers into equal pieces roughly,
it doesn't mean the actual work done by the thread is
by all the threats is the same.
In fact it is not in an ideal world.
If each thread would be doing exactly the same amount of work,
then yes,
we will get linear improvement and you will get that the number of
processors is the best number of threats to use and more than that
will be detrimental because you have context,
which is threats fighting with each other because only that number of threats
can be run at the same time based on number of processors.
But here's the idea,
the square root function here is not um It's not constant
in the sense that it depends on end the smaller the end
the smaller the square root,
the larger the end the larger the square root it's a monotone,
quickly increasing functions as as it's sometimes referred to in mathematics.
So essentially what we have here is the following, let's say I have two threats.
One of them is looking at the numbers from 1 to 10 million.
And the other one is looking from 10 million and 1 to 20 million.
So the first thread will finish sooner because it has to work with smaller
numbers and smaller numbers result in smaller square roots,
which means that this loop will finish faster.
The second thread on the other hand, has to deal with larger numbers,
which means the square root of those numbers is our larger still and
that would require the threat to do more work.
The loop is going to be working more uh more because
the limit is going to be higher.
That's really the problem.
The work their threats are doing is not the same.
The first threat will finish very early,
which means the process of there will be now free doing nothing.
And this is why adding more threats actually improve
performance at least two to some extent.
Because now the other thread that perhaps was in the
register because it couldn't run,
it would now be able to capture that first
processor and continue doing some work.
So the basic idea in order to provide good performance or the best
performance is always to keep the processors busy.
So in this case, uh it's not,
it's not really the case and the last one is going to do the most
amount of work has the biggest numbers to contend with.
And this is why we don't get a linear improvement and also why using
more threats and processors is actually beneficial.
At least up to a point in general.
What we learned from this is that multi threading is hard in in several aspects.
One of them is ready to synchronization,
which we didn't have to do here because the threats don't really share anything.
So no synchronization is required.
That's a fairly simple case.
And and of course these cases exist but it's there are other,
much more complex cases but in general movie threading is
difficult because if you even imagine that some of these
operations might require eye operations,
not just CPU bound calculations.
Then again,
you get to the point where a threat performance i operation Now the process it
was running on is now free because the threat goes into a wait state and now
another threat might be able to work in its place.
But of course you have to account for that,
you have to to prepare for that in some way.
So generally speaking,
multi threading is not easy and there are many libraries out there
uh to help with that but we're dealing here with the raw or the
lowest things we can work with on windows.
So I'm not going to describe any high level libraries
to make this perhaps slightly easier