Welcome to GameHourz.com!
FAQFAQ      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Algorithm for spawning any number of threads.

 
Goto page 1, 2
   Game Forums (Home) -> Core War RSS
Next:  Lightning usually never strike twice idea !  
Author Message
Skybuck Flying

External


Since: May 25, 2006
Posts: 295



(Msg. 1) Posted: Thu Dec 06, 2007 6:06 pm
Post subject: Algorithm for spawning any number of threads.
Archived from groups: rec>games>corewar (more info?)

Hello,

Listen if you want to spawn a number of threads then do the following:

First substract 1 from the number of threads.

Then determine where it lies on the binary scale for example
1-2-4-8-16-32-64-128-256-512-1024

For example (101-1) = 100 threads to be created to get 101 threads lies
between 64 and 128.

Simple idea, start with 1, multiply it by 2, keep multiplieing it by 2 until
it's greater than the number of threads.

Now divide it once and you have something which you can use to find the
binary number for 100.

Call it the divisor. For example for 100 the divisor starts with 64.

Here's the rest of algo:

Divide Number of Threads (100) by the divisor (64).

If it can divide it gives 1 if not it gives a 0.

Now comes the interesting part.

For a 1, the number of threads need to dubbel.
For a 0, the number of threads need to dubbel-1.

Depending on the outcome, dubbel or dubbel-1 the threads.

To dubbel use spl 1
To dubbel-1 use mov spawn, 0 (spawn is spl 1)

Then make all threads jump to generated instruction as described in the two
lines above.

So for a 1:

spl 1 should be executed.

So for a zero

mov spawn, 0 will be executed for the first thread.
all other threads will execute
spl 1, 0

This has the effect that one less thread will be created which is exactly
what it should.

Now divide the divisor by 2 and repeat the loop, until the divisor is zero.

Here is an example:

100 div 64 = 1, remainder 35
36 div 32 = 1, remainder 4
4 div 16 = 0, remainder 4
4 div 8 = 0, remainder 4
4 div 4 = 1, remainder 0
0 div 2 = 0, remainder 0
0 div 1 = 0, remainder 0
0 div 0 = death of thread. you could try to avoid it or use it to your
adventage to kill off any calculating thread or so.

binary 1100100 = 100

Now you have an algorithm to extract the binary one's and zero's from the
number 100.

And thus you can create a nice spawning program which will execute fast.

The spawning program should look something like:
spl 1
spl 1
mov -1, 0
mov -1, 0
spl 1
mov -1, 0
mov -1, 0
jmp 0

An alternative method (untested but will probably work just as well):

spl 1
spl 1
mov SplInstruction, 0
mov SplInstruction, 0
spl 1
mov SplInstruction, 0
mov SplInstruction, 0
jmp 0
SplInstruction spl 1

After this code has been executed there will be 100 threads created plus the
main thread so 101 threads ! nice eh ! Wink

Now the only thing to do is for example write some nice redcode code which
implements the algorithm with div's, mod's and such.

Bye,
Skybuck.

 >> Stay informed about: Algorithm for spawning any number of threads. 
Back to top
Login to vote
CoreChild

External


Since: Nov 30, 2007
Posts: 13



(Msg. 2) Posted: Thu Dec 06, 2007 6:06 pm
Post subject: Re: Algorithm for spawning any number of threads. [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Dec 6, 5:06 pm, "Skybuck Flying" <s... RemoveThis @hotmail.com> wrote:
Now the only thing to do is for example write some nice redcode code
which
> implements the algorithm with div's, mod's and such.

Hi Skybuck,

Here's some code which creates the code to generate any required
number of processes using spl 1 and mov -1,0. There's room for
improvement - it can be made smaller and faster! Wink

Cheers,

John

org demo

stack dat 0, count+1

pspl spl 1
pmov mov.i -1, #0

demo mov.a #13, {stack

sub.a #1, *stack
para mov.a *stack, {stack
div.a #2, }stack
mod.a #2, *stack
add #1, count
jmn.a para, {stack
add.a #1, stack
ploop mov pmov, @stack
seq.a #0, }stack
mov pspl, @stack
add #1, stack
count djn ploop, #0

end

 >> Stay informed about: Algorithm for spawning any number of threads. 
Back to top
Login to vote
CoreChild

External


Since: Nov 30, 2007
Posts: 13



(Msg. 3) Posted: Fri Dec 07, 2007 9:55 am
Post subject: Re: Algorithm for spawning any number of threads. [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Dec 7, 10:08 am, "Skybuck Flying" <s....RemoveThis@hotmail.com> wrote:
> I haven't tried your code yet, but if it's working and probably will then
> thanks in advance.

Mine doesn't work if you try to generate 0 processes. Apart from
that, it should be fine. Does you code work for 0 processes. How
about 1 or 2 processes. 6145 might be tricky since it's bigger than
4096 and 8192 is larger than CORESIZE.

> However I will try to write my own implementation first and then compare it
> to yours Wink

And I see your code is faster, I'll have to optimize mine now Wink

> 1. Did you write it yourself ?

Yes, wrote it myself.

> 3. If you did write it yourself, when did you write it ? Was it before or
> after I posted my algorithm idea Wink
> So in other words was the code based on my algorithm or on something else ?

After you posted, but not based on your algorithm.

Cheers,

John
 >> Stay informed about: Algorithm for spawning any number of threads. 
Back to top
Login to vote
Skybuck Flying

External


Since: May 25, 2006
Posts: 295



(Msg. 4) Posted: Fri Dec 07, 2007 11:08 am
Post subject: Re: Algorithm for spawning any number of threads. [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Hello,

I haven't tried your code yet, but if it's working and probably will then
thanks in advance.

However I will try to write my own implementation first and then compare it
to yours Wink

But I already do have a couple of short/quick questions about your posted
code:

1. Did you write it yourself ?

2. Or did you acquire it from the link/website you posted ?

3. If you did write it yourself, when did you write it ? Was it before or
after I posted my algorithm idea Wink

So in other words was the code based on my algorithm or on something else ?
Smile

Bye,
Skybuck.


"CoreChild" <grumpy3039 DeleteThis @hotmail.com> wrote in message
news:d8bfdb10-3467-46d3-be50-de8ea0ac4ea1@e1g2000hsh.googlegroups.com...
> On Dec 6, 5:06 pm, "Skybuck Flying" <s... DeleteThis @hotmail.com> wrote:
> Now the only thing to do is for example write some nice redcode code
> which
>> implements the algorithm with div's, mod's and such.
>
> Hi Skybuck,
>
> Here's some code which creates the code to generate any required
> number of processes using spl 1 and mov -1,0. There's room for
> improvement - it can be made smaller and faster! Wink
>
> Cheers,
>
> John
>
> org demo
>
> stack dat 0, count+1
>
> pspl spl 1
> pmov mov.i -1, #0
>
> demo mov.a #13, {stack
>
> sub.a #1, *stack
> para mov.a *stack, {stack
> div.a #2, }stack
> mod.a #2, *stack
> add #1, count
> jmn.a para, {stack
> add.a #1, stack
> ploop mov pmov, @stack
> seq.a #0, }stack
> mov pspl, @stack
> add #1, stack
> count djn ploop, #0
>
> end
 >> Stay informed about: Algorithm for spawning any number of threads. 
Back to top
Login to vote
Skybuck Flying

External


Since: May 25, 2006
Posts: 295



(Msg. 5) Posted: Fri Dec 07, 2007 2:07 pm
Post subject: Version1: Skybuck's Spawning code vs John's Spawning Code (Winner: John Loser: Skybuck :) ) [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Cool:

Skybuck's FastSpawnGeneratorV1

versus

John's FastSpawnGeneratorV1

For generator 100 threads:

Skybuck's code slightly modified to mimic John's code behaviour for fair
comparision:

Skybuck's Final Code Size: 37
Skybuck's Cycles Needed: 235

John's Final Code Size: 31
John's Cycles Needed: 172

John's code is the clear winner for now ! LOL.

NICE/AWESOME =D

Bye,
Skybuck Wink
 >> Stay informed about: Algorithm for spawning any number of threads. 
Back to top
Login to vote
Skybuck Flying

External


Since: May 25, 2006
Posts: 295



(Msg. 6) Posted: Fri Dec 07, 2007 9:09 pm
Post subject: Re: Algorithm for spawning any number of threads. [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Ok, thanks for the answers.

Be sure to read this thread as well it's called:

"Spawning threads: John's algorithm vs Skybuck's Algorithm (analyzation and
future improvement ideas/suggestions)"

I describe your algorithm there.

My fastest code is based on your algorithm with my own modifications to it.

Sorta a mix of yours and mine Smile

No the old code malfunctions for 8000.

Good point you make ! Smile LOL Wink =D

Didn't even think about that LOL.

Good good ! Wink

New code handles it properly though Wink

I'll give your code a maximum test as well, ok your new code working fine as
well. Didn't test your own code with maximum value Wink

My code works for 2 to coresize.

0 and 1 is ofcourse not so interesting, to simple, could be catched with
simply jumps or so Wink

Bye,
Skybuck.


"CoreChild" <grumpy3039.RemoveThis@hotmail.com> wrote in message
news:71820d06-e578-4f4f-b1e3-69504e0b4f7e@w40g2000hsb.googlegroups.com...
> On Dec 7, 10:08 am, "Skybuck Flying" <s....RemoveThis@hotmail.com> wrote:
>> I haven't tried your code yet, but if it's working and probably will then
>> thanks in advance.
>
> Mine doesn't work if you try to generate 0 processes. Apart from
> that, it should be fine. Does you code work for 0 processes. How
> about 1 or 2 processes. 6145 might be tricky since it's bigger than
> 4096 and 8192 is larger than CORESIZE.
>
>> However I will try to write my own implementation first and then compare
>> it
>> to yours Wink
>
> And I see your code is faster, I'll have to optimize mine now Wink
>
>> 1. Did you write it yourself ?
>
> Yes, wrote it myself.
>
>> 3. If you did write it yourself, when did you write it ? Was it before or
>> after I posted my algorithm idea Wink
>> So in other words was the code based on my algorithm or on something else
>> ?
>
> After you posted, but not based on your algorithm.
>
> Cheers,
>
> John
 >> Stay informed about: Algorithm for spawning any number of threads. 
Back to top
Login to vote
Skybuck Flying

External


Since: May 25, 2006
Posts: 295



(Msg. 7) Posted: Fri Dec 07, 2007 9:14 pm
Post subject: Re: Version1: Skybuck's Spawning code vs John's Spawning Code, Winner is now: Skybuck :) Loser: John LOL. [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

See new thread:

Skybuck's FasterGeneratorV3

Kicks ass Smile at 154 cycles for 100 threads and only 26 instructions =D

Motch gratious amigo's ! LOL =D

Bye,
Skybuck.
 >> Stay informed about: Algorithm for spawning any number of threads. 
Back to top
Login to vote
pauldkline

External


Since: Dec 01, 2007
Posts: 19



(Msg. 8) Posted: Sun Dec 09, 2007 5:15 am
Post subject: Re: Algorithm for spawning any number of threads. [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Dec 6, 11:06 am, "Skybuck Flying" <s....TakeThisOut@hotmail.com> wrote:
> Hello,
....
> After this code has been executed there will be 100 threads created plus the
> main thread so 101 threads ! nice eh ! Wink
>
> Now the only thing to do is for example write some nice redcode code which
> implements the algorithm with div's, mod's and such.
>
> Bye,
> Skybuck.

A nice description of an algorithm also discovered some time back. An
improvement was also discovered for certain values that is even faster
because it eliminates a mov:

spl 2
spl 1

Voila! 3 processes.

In general, the existing processes executingspl 2 skip the spl 1, but
their children all execute it, so it's a x3 multiplier.
Maybe you could restate your algorithm in terms of base 3?

P. Kline
 >> Stay informed about: Algorithm for spawning any number of threads. 
Back to top
Login to vote
pauldkline

External


Since: Dec 01, 2007
Posts: 19



(Msg. 9) Posted: Sun Dec 09, 2007 5:21 am
Post subject: Re: Algorithm for spawning any number of threads. [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Corewarrior's 1 and 69 discuss this topic.

P. Kline
 >> Stay informed about: Algorithm for spawning any number of threads. 
Back to top
Login to vote
Roy

External


Since: Dec 04, 2007
Posts: 4



(Msg. 10) Posted: Sun Dec 09, 2007 7:42 am
Post subject: Re: Algorithm for spawning any number of threads. [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

JM, isn't this easier:

JM:

demo dat 100,0
zero mov -1 , 0
one spl 1 , 21

start nop {demo ;silly, but has to be done Sad

mov.ab demo , zero
mod.ab #2 , zero
mov.i @zero , <one
div.a #2 , demo
jmn.ba -4 , demo
jmp @one

end start
 >> Stay informed about: Algorithm for spawning any number of threads. 
Back to top
Login to vote
CoreChild

External


Since: Nov 30, 2007
Posts: 13



(Msg. 11) Posted: Sun Dec 09, 2007 10:14 am
Post subject: Re: Algorithm for spawning any number of threads. [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Dec 9, 6:05 pm, "Skybuck Flying" <s....RemoveThis@hotmail.com> wrote:
> I quickly read over them, I did not find any section that explained how to
> convert a base 3 number to a spawn program ?
>
> I also tried to deduce it from your example but your example is inefficient,
> it doesn't cover digit 2 of base 3:
>
> Base 3 has three digits:
>
> 0, 1, 2
>
> base 10: base 3:
>
> 000 000
> 001 001
> 002 002
> 003 010
> 004 011
> 005 012
> 006 020
> 007 021
> 008 022
> 009 100
> 010 101
> 011 102
> 012 110
> 013 111
> 014 112
> 015 120
> 016 121
> 017 122
> 018 200
> 019 201
> 020 202
>
> Paul gave an example how to convert decimal 3 to a spawn program:
>
> Decimal 3 is written as 10 in base 3:
>
> spl 2 ; 1 adds one thread to next, next instruction
> spl 1 ; 0 adds one thread to next, instruction
> ; plus main thread is 3 threads in total.
>
> Possible conclusion:
>
> 1 needs to be replaced with spl 2
> 0 needs to be replaced with spl 1
> 2 needs to be replaced with mov -1, 0 ?
>
> Let's try it with decimal 15, which is 120 in base 3:
>
> spl 2 ; 1
> mov -1, 0 ; 2
> spl 1 ; 0
>
> Creates 4 threads, wrong answer. So this doesn't work.
>
> Maybe -1 needs to be subtracted first so the real example could have been:
>
> Decimal 2 is 02 in base 3:
>
> spl 2 ; 0
> spl 1 ; 2
>
> Possible conclusion:
>
> 0 needs to be replaced with spl 2
> 2 needs to be replaced with spl 1
> 1 needs to be replaced with mov -1, 0 ?
>
> Let's try it again with decimal 15, which is 120 in base 3:
>
> mov -1, 0 ; 1
> spl 1 ; 2
> spl 2 ; 0
> jmp 0
> jmp 0
>
> Still wrong creates 4 threads.
>
> So first we need to figure out how to convert a base 3 number to a spawn
> program Wink
>
> Bye,
> Skybuck.
 >> Stay informed about: Algorithm for spawning any number of threads. 
Back to top
Login to vote
CoreChild

External


Since: Nov 30, 2007
Posts: 13



(Msg. 12) Posted: Sun Dec 09, 2007 10:37 am
Post subject: Re: Algorithm for spawning any number of threads. [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Sorry for spamming, this didn't post properly last time. (Message
dated 6:14pm)

On Dec 9, 5:33 pm, "Skybuck Flying" <s....RemoveThis@hotmail.com> wrote:
> Roy's code is excellent it seems:

Agreed Smile

> I think Roy's is the best one so far... for using in real programs.

I wouldn't suggest using any of the code posted so far in a real
warrior. Instead use the following:

processes equ 100 ; works for 1 to 512, should be adequate!
for 9+0*a=512
for ((processes-1)/(a=a/2))%2==1 && processes>a
spl 1
rof
for ((processes-1)/a)%2==0 && processes>a
mov.i -1,#0
rof
rof

Cheers,

John
 >> Stay informed about: Algorithm for spawning any number of threads. 
Back to top
Login to vote
Skybuck Flying

External


Since: May 25, 2006
Posts: 295



(Msg. 13) Posted: Sun Dec 09, 2007 6:33 pm
Post subject: Re: Algorithm for spawning any number of threads. (*** NEW WINNER: ROY, *** DING DING DING *** ) [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Roy's code is excellent it seems:

1. It works for 1 to 8000. (Not zero though, but that can easily be skipped
Smile Wink)
2. It's only 9 instructions for initial program, smaller than
corechild/john's code.
3. It's only slightly slower, about 10 cycles for 8000 threads, and 2 cycles
for 100 threads.

Roy's code has a fixed position where the program will be begin... otherwise
have that too though..

However roy's has one adventage, it starts below the main program... and it
always skips some places...
so it's rather quite good !

I think Roy's is the best one so far... for using in real programs.

So Roy's gets extra points for re-useability and flexibility and compactness
! =D

Bye,
Skybuck.


"Roy" <roy.van.rijn.RemoveThis@gmail.com> wrote in message
news:639d6135-67b4-4c7b-a010-eb1752e74c23@b15g2000hsa.googlegroups.com...
> JM, isn't this easier:
>
> JM:
>
> demo dat 100,0
> zero mov -1 , 0
> one spl 1 , 21
>
> start nop {demo ;silly, but has to be done Sad
>
> mov.ab demo , zero
> mod.ab #2 , zero
> mov.i @zero , <one
> div.a #2 , demo
> jmn.ba -4 , demo
> jmp @one
>
> end start
>
 >> Stay informed about: Algorithm for spawning any number of threads. 
Back to top
Login to vote
Skybuck Flying

External


Since: May 25, 2006
Posts: 295



(Msg. 14) Posted: Sun Dec 09, 2007 7:05 pm
Post subject: Re: Algorithm for spawning any number of threads. [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

I quickly read over them, I did not find any section that explained how to
convert a base 3 number to a spawn program ?

I also tried to deduce it from your example but your example is inefficient,
it doesn't cover digit 2 of base 3:

Base 3 has three digits:

0, 1, 2

base 10: base 3:

000 000
001 001
002 002
003 010
004 011
005 012
006 020
007 021
008 022
009 100
010 101
011 102
012 110
013 111
014 112
015 120
016 121
017 122
018 200
019 201
020 202

Paul gave an example how to convert decimal 3 to a spawn program:

Decimal 3 is written as 10 in base 3:


spl 2 ; 1 adds one thread to next, next instruction
spl 1 ; 0 adds one thread to next, instruction
; plus main thread is 3 threads in total.


Possible conclusion:

1 needs to be replaced with spl 2
0 needs to be replaced with spl 1
2 needs to be replaced with mov -1, 0 ?

Let's try it with decimal 15, which is 120 in base 3:

spl 2 ; 1
mov -1, 0 ; 2
spl 1 ; 0

Creates 4 threads, wrong answer. So this doesn't work.




Maybe -1 needs to be subtracted first so the real example could have been:

Decimal 2 is 02 in base 3:

spl 2 ; 0
spl 1 ; 2


Possible conclusion:

0 needs to be replaced with spl 2
2 needs to be replaced with spl 1
1 needs to be replaced with mov -1, 0 ?


Let's try it again with decimal 15, which is 120 in base 3:

mov -1, 0 ; 1
spl 1 ; 2
spl 2 ; 0
jmp 0
jmp 0

Still wrong creates 4 threads.

So first we need to figure out how to convert a base 3 number to a spawn
program Wink

Bye,
Skybuck.
 >> Stay informed about: Algorithm for spawning any number of threads. 
Back to top
Login to vote
Skybuck Flying

External


Since: May 25, 2006
Posts: 295



(Msg. 15) Posted: Sun Dec 09, 2007 7:09 pm
Post subject: Re: Algorithm for spawning any number of threads. [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Oh wait I found the section you ment:

Warrior 69.txt:

N OLD CODE NEW CODE COMMENTS

The new code shows it, I shall examine it and see if it's any use Smile

Bye,
Skybuck.
 >> Stay informed about: Algorithm for spawning any number of threads. 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Score Surface for 94nop - Hi, some anonymous person (still called "bvowk" for simplicity ;-) is so kind to provide access to a pile of computers. I have suggested to calculate one score surface for standard settings. It takes roughly 1000 times the time of one "no...

Bug in pMARS - Hi, either I don't know how EQUs work or I have found a bug in the parser of pMARS. So far I cound pin it down to: ;redcode-tiny ;name test ;assert CORESIZE == 800 v3 EQU 3 * (3 / 2 + 1) + 3 v4 EQU (CORESIZE - v3) dat.f v3, v4 With the..

KOTH.ORG: Status - ICWS Experimental 94 03/06/06 - Weekly Status on 03/06/06 -=- irc.KOTH.org is up! Meetings held in #corewars -=- Tons of new features on www.KOTH.org/koth.html pages -=- *FAQ* page located at: www.KOTH.org/corewar-faq.html Current Status of the KOTH.ORG ICWS Experimental 94..

KOTH.ORG: Status - MultiWarrior 94 03/06/06 - Weekly Status on 03/06/06 -=- irc.KOTH.org is up! Meetings held in #corewars -=- Tons of new features on www.KOTH.org/koth.html pages -=- *FAQ* page located at: www.KOTH.org/corewar-faq.html Current Status of the KOTH.ORG Multiwarrior 94 CoreWar..

KOTH.ORG: Status - 94 No Pspace 03/06/06 - Weekly Status on 03/06/06 -=- irc.KOTH.org is up! Meetings held in #corewars -=- Tons of new features on www.KOTH.org/koth.html pages -=- *FAQ* page located at: www.KOTH.org/corewar-faq.html Current Status of the KOTH.ORG 94 No Pspace CoreWar Hill...
   Game Forums (Home) -> Core War All times are: Ekaterinburg, Islamabad, Karachi, Tashkent (change)
Goto page 1, 2
Page 1 of 2

 
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]