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

Skybuck's FastSpawnGeneratorV1 (module/demonstration code)

 
   Game Forums (Home) -> Core War RSS
Next:  Feature Request: Add multi-core and gpu simulatio..  
Author Message
Skybuck Flying

External


Since: May 25, 2006
Posts: 295



(Msg. 1) Posted: Fri Dec 07, 2007 1:55 pm
Post subject: Skybuck's FastSpawnGeneratorV1 (module/demonstration code)
Archived from groups: rec>games>corewar (more info?)

;redcode
;name FastSpawnGeneratorV1
;author Skybuck Flying
;version 1
;history version 1 created on 7 december 2007

; Tested with 100 and working ok Smile
;
; Code/comments still a bit messy but it's working ! =D
;
; Let's write a faster spawn creator.
;
; 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.


mov.ab #100, NumberOfThreadsToSpawn ; number of threads to spawn

; Algorithm:
;
; Step 1: start with 1 and keep multiplieing it until it's greater then the
number of threads to spawn
; the multiplieing result will ultimately be used as a divisor so the
variable will be called "Divisor"

mov #1, Divisor
Multiplier mul #2, Divisor
mov.b $Divisor, Comparision
mov.ba $NumberOfThreadsToSpawn, Comparision
; as long as divisor is equal or more then number of threads multiply it
; slt skips next instruction if less than, so it executes next instruction
of equal or more than.
; slt can be thought of as: execute next instruction if b >= a otherwise
skip
Comparision slt #0, #0
jmp Multiplier

; now we need to divide the divisor once
div #2, Divisor

;
; Step 2: Divide the NumberOfThreadsToSpawn by the divisor, the outcome
indicates a 1 for spl 1 or mov spl 1, 0 for a zero.
; then calculate the remainder for number of threads to spawn
; then divide the divisor by 2, repeat until divisor is zero
Step2
mov.ba $Divisor, Divider
mov.b $NumberOfThreadsToSpawn, Divider
Divider div #0, #0

; look at result, generate instruction etc

; look at the result,
; if it's a 1 we need to add instruction: spl 1, to the spawn program
; if it's a 0 we need to add instruction: mov doubleinstruction, 0 to the
spawn program
jmz.b StepDoubleMinusOne, Divider
StepDouble:
mov DoubleInstruction, >SpawnProgramPointer

jmp Skip
StepDoubleMinusOne:
mov DoubleMinusOneInstruction, >SpawnProgramPointer
Skip

; calculate the remainder for the number of threads to spawn
mov.ba $Divisor, Modder
mov.b $NumberOfThreadsToSpawn, Modder
Modder mod #0, #0

; move result back into number of threads to spawn
mov.b Modder, NumberOfThreadsToSpawn

; divide the divisor by 2
mov.b $Divisor, Divider2
Divider2 div #2, #0

; move result back into the divisor
mov.b Divider2, Divisor

; repeat
jmn Step2, Divisor

; add a spinner to the spawn program to keep the threads spinning to
demonstrate how many there are Wink
; could be handy for final programs as well. probably would have to alter
jump instruction to compensate for generated program length
; ignored for now... just jump to end of program for now Wink
mov SpinnerInstruction, >SpawnProgramPointer

; jump to spawn program to execute it
; jmp @SpawnProgramPointer ; wrong offset ignored for now, might not even
matter as long as one knows where to jump to at begin of the algo
jmp EndOfProgram

DoubleInstruction spl $1, 0
DoubleMinusOneInstruction mov DoubleInstruction, 0
SpinnerInstruction jmp $0, $0

NumberOfThreadsToSpawn dat $0, #0
Divisor dat $0, #0
SpawnProgramPointer dat $0, #EndOfProgram

EndOfProgram

 >> Stay informed about: Skybuck's FastSpawnGeneratorV1 (module/demonstration code) 
Back to top
Login to vote
Skybuck Flying

External


Since: May 25, 2006
Posts: 295



(Msg. 2) Posted: Fri Dec 07, 2007 1:57 pm
Post subject: Re: Skybuck's FastSpawnGeneratorV1 (module/demonstration code) [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Little extra comment:

; 237 cycles needed to generate 100 threads.

Bye,
Skybuck.

 >> Stay informed about: Skybuck's FastSpawnGeneratorV1 (module/demonstration code) 
Back to top
Login to vote
Skybuck Flying

External


Since: May 25, 2006
Posts: 295



(Msg. 3) Posted: Fri Dec 07, 2007 2:07 pm
Post subject: Re: Skybuck's FastSpawnGeneratorV1 (module/demonstration code) [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: Skybuck's FastSpawnGeneratorV1 (module/demonstration code) 
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)
Page 1 of 1

 
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 ]