List Info

Thread: C-Sharp (C#) Group: I'm inefficient at threading




C-Sharp (C#) Group: I'm inefficient at threading
country flaguser name
United States
2007-08-09 06:28:57
Hi there,

I'm trying to build a naive algorithm to find all prime
numbers
starting from 2.

The goal is to see how faster the algorithm ends up being on
a quad
core, thus, using threads.

I read somewhere that the overhead in creating a thread has
to be
lower than the amount of operations in the subroutine the
thread is
going to calculate and I assume my problem might be there.

What I do is wait till I have enough primes (let's say 100k)
and then
split the range of divisor in 4 different threads and wait
for my
threads to finish. If it turns out thread i finishes and has
found a
divisor, I kill the remaining threads to save time.


As soon as my threads kicks in, the algorithm becomes very
slow and my
4 cores drops at 0% usage and it still get to find about 10
primes per
second... on the other hand before using any threads the
algorithm
could find the first 100k primes in about 2 seconds.

Naive = I try to divide the current value to be tested with
all primes
I found before it that are smaller than the square root of
the current
value.

1- I read starting and destroying a thread in c# took about
300k
operations, ain't going through a 25k elements table
comparing the
value, i++ and modulo taking me more than 300k operations?
2- Does int[] is a reference type?
3- More often than not, I'm joining thread 0 (which contains
elements
1 to maxDivisor/4) why is that?

Can we paste code in here so you can give me tips?


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "C-Sharp (C#)" group.
To post to this group, send email to C_Sharpgooglegroups.com
To unsubscribe from this group, send email to
C_Sharp-unsubscribegooglegroups.com
For more options, visit this group at http://g
roups.google.com/group/C_Sharp?hl=en
-~----------~----~----~----~------~----~------~--~---


C-Sharp (C#) Group: Re: I'm inefficient at threading
country flaguser name
United States
2007-08-09 09:40:54
Also why is uint.MaxValue 4294967295 when the application is
compiled
in x64 native? Shouldn't compiling in x64 automatically
scale the int
to 8 bytes?

Thanks

On Aug 9, 7:28 am, "DavidGren...gmail.com"
<DavidGren...gmail.com>
wrote:
> Hi there,
>
> I'm trying to build a naive algorithm to find all prime
numbers
> starting from 2.
>
> The goal is to see how faster the algorithm ends up
being on a quad
> core, thus, using threads.
>
> I read somewhere that the overhead in creating a thread
has to be
> lower than the amount of operations in the subroutine
the thread is
> going to calculate and I assume my problem might be
there.
>
> What I do is wait till I have enough primes (let's say
100k) and then
> split the range of divisor in 4 different threads and
wait for my
> threads to finish. If it turns out thread i finishes
and has found a
> divisor, I kill the remaining threads to save time.
>
> As soon as my threads kicks in, the algorithm becomes
very slow and my
> 4 cores drops at 0% usage and it still get to find
about 10 primes per
> second... on the other hand before using any threads
the algorithm
> could find the first 100k primes in about 2 seconds.
>
> Naive = I try to divide the current value to be tested
with all primes
> I found before it that are smaller than the square root
of the current
> value.
>
> 1- I read starting and destroying a thread in c# took
about 300k
> operations, ain't going through a 25k elements table
comparing the
> value, i++ and modulo taking me more than 300k
operations?
> 2- Does int[] is a reference type?
> 3- More often than not, I'm joining thread 0 (which
contains elements
> 1 to maxDivisor/4) why is that?
>
> Can we paste code in here so you can give me tips?


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "C-Sharp (C#)" group.
To post to this group, send email to C_Sharpgooglegroups.com
To unsubscribe from this group, send email to
C_Sharp-unsubscribegooglegroups.com
For more options, visit this group at http://g
roups.google.com/group/C_Sharp?hl=en
-~----------~----~----~----~------~----~------~--~---


C-Sharp (C#) Group: Re: I'm inefficient at threading
country flaguser name
United States
2007-08-09 22:05:49
I used to struggle with threading till I read this:

http://www.albahar
i.com/threading/

Hope this helps!
- Matt


On Aug 9, 10:40 am, "DavidGren...gmail.com"
<DavidGren...gmail.com>
wrote:
> Also why is uint.MaxValue 4294967295 when the
application is compiled
> in x64 native? Shouldn't compiling in x64 automatically
scale the int
> to 8 bytes?
>
> Thanks
>
> On Aug 9, 7:28 am, "DavidGren...gmail.com" <DavidGren...gmail.com>
> wrote:
>
> > Hi there,
>
> > I'm trying to build a naive algorithm to find all
prime numbers
> > starting from 2.
>
> > The goal is to see how faster the algorithm ends
up being on a quad
> > core, thus, using threads.
>
> > I read somewhere that the overhead in creating a
thread has to be
> > lower than the amount of operations in the
subroutine the thread is
> > going to calculate and I assume my problem might
be there.
>
> > What I do is wait till I have enough primes (let's
say 100k) and then
> > split the range of divisor in 4 different threads
and wait for my
> > threads to finish. If it turns out thread i
finishes and has found a
> > divisor, I kill the remaining threads to save
time.
>
> > As soon as my threads kicks in, the algorithm
becomes very slow and my
> > 4 cores drops at 0% usage and it still get to find
about 10 primes per
> > second... on the other hand before using any
threads the algorithm
> > could find the first 100k primes in about 2
seconds.
>
> > Naive = I try to divide the current value to be
tested with all primes
> > I found before it that are smaller than the square
root of the current
> > value.
>
> > 1- I read starting and destroying a thread in c#
took about 300k
> > operations, ain't going through a 25k elements
table comparing the
> > value, i++ and modulo taking me more than 300k
operations?
> > 2- Does int[] is a reference type?
> > 3- More often than not, I'm joining thread 0
(which contains elements
> > 1 to maxDivisor/4) why is that?
>
> > Can we paste code in here so you can give me
tips?


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "C-Sharp (C#)" group.
To post to this group, send email to C_Sharpgooglegroups.com
To unsubscribe from this group, send email to
C_Sharp-unsubscribegooglegroups.com
For more options, visit this group at http://g
roups.google.com/group/C_Sharp?hl=en
-~----------~----~----~----~------~----~------~--~---


C-Sharp (C#) Group: Re: I'm inefficient at threading
country flaguser name
United States
2007-08-10 06:15:08
I found a few good thing's over the last day in that website
thanks,
it's pretty good.

On the other hand, I realized my algorithm could find 10k
primes per
second before work is being dispatched to primes.... besides
there's
about 10 non-primes per prime around those number which
means it can
calculates the division for 100k numbers which means I'm
perhaps not
givien enough work to my threads.

Also, once the job is done I'm actually deleting the thread
and
creating a new one, there's got to be a better way of doing
that.

Will passing parameter to the object of the thread and doing
a
thread.start(); be more efficient?

What consumes the juice, creating the thread via new
Thread()? or
actually starting it?

Thanks,

On Aug 9, 11:05 pm, "mattster...gmail.com"
<mattster...gmail.com>
wrote:
> I used to struggle with threading till I read this:
>
> http://www.albahar
i.com/threading/
>
> Hope this helps!
> - Matt
>
> On Aug 9, 10:40 am, "DavidGren...gmail.com" <DavidGren...gmail.com>
> wrote:
>
> > Also why is uint.MaxValue 4294967295 when the
application is compiled
> > in x64 native? Shouldn't compiling in x64
automatically scale the int
> > to 8 bytes?
>
> > Thanks
>
> > On Aug 9, 7:28 am, "DavidGren...gmail.com" <DavidGren...gmail.com>
> > wrote:
>
> > > Hi there,
>
> > > I'm trying to build a naive algorithm to find
all prime numbers
> > > starting from 2.
>
> > > The goal is to see how faster the algorithm
ends up being on a quad
> > > core, thus, using threads.
>
> > > I read somewhere that the overhead in
creating a thread has to be
> > > lower than the amount of operations in the
subroutine the thread is
> > > going to calculate and I assume my problem
might be there.
>
> > > What I do is wait till I have enough primes
(let's say 100k) and then
> > > split the range of divisor in 4 different
threads and wait for my
> > > threads to finish. If it turns out thread i
finishes and has found a
> > > divisor, I kill the remaining threads to save
time.
>
> > > As soon as my threads kicks in, the algorithm
becomes very slow and my
> > > 4 cores drops at 0% usage and it still get to
find about 10 primes per
> > > second... on the other hand before using any
threads the algorithm
> > > could find the first 100k primes in about 2
seconds.
>
> > > Naive = I try to divide the current value to
be tested with all primes
> > > I found before it that are smaller than the
square root of the current
> > > value.
>
> > > 1- I read starting and destroying a thread in
c# took about 300k
> > > operations, ain't going through a 25k
elements table comparing the
> > > value, i++ and modulo taking me more than
300k operations?
> > > 2- Does int[] is a reference type?
> > > 3- More often than not, I'm joining thread 0
(which contains elements
> > > 1 to maxDivisor/4) why is that?
>
> > > Can we paste code in here so you can give me
tips?


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "C-Sharp (C#)" group.
To post to this group, send email to C_Sharpgooglegroups.com
To unsubscribe from this group, send email to
C_Sharp-unsubscribegooglegroups.com
For more options, visit this group at http://g
roups.google.com/group/C_Sharp?hl=en
-~----------~----~----~----~------~----~------~--~---


[1-4]

about | contact  Other archives ( Real Estate discussion Medical topics )