Main Menu

Job Processing

Started by kevin, December 29, 2009, 10:35:11 AM

Previous topic - Next topic

kevin

 Job Processing

  This snippet looks into the idea of  job processing.   Sometimes in our programs we'll want to limit the amount of the time our code spends doing some calculation.   The reason for this is that sometimes a particular  calculation might take a very long time  to compute.   Which will halt our program until the calc is complete.     So one way around this is, is to build routines that have the ability to exit early and continue from where they left off.  So we're effectively controlling how much time this process takes, if it takes longer than we're willing to accept this frame, we halt it and exit.  Then fire it up again next frame.

  I tend to use this type of approach whenever I need my program to keep running while performing tasks of an unknown length. One that comes to mind is the assembler in the Kyruss tech demo..   Kyruss



 Anyway, this example is more to show the theory, rather than a practical application.

 Required PB1.64k2 or higher

PlayBASIC Code: [Select]
   Constant JobState_Complete       =ac(1)
Constant JobState_InitialData =ac(1)
Constant JobState_Sorting =ac(1)

Type tJobs
State ; 0 = job done, 1 sorting
ArrayHandle
ArraySize ; size of the array
DataRangeLow ; lowest value in the array
DataRangeHi ; higest value in the array
PassCount ; The number of sort passes
DeleteTime ; Time when this job should be deleted
EndType

Dim Job as tJobs list


// Set up a bunch of the jobs for the routine to do
Setfps 30

Do
Cls 0
ink rgb(255,0,0)
print fps()
ink rgb(255,0,255)
print "Jobs="+str$(Getlistsize(job()) )
ink $ffffff


; Add a random sorting job to the handle
if rnd(100)>25
AddNewJob(rndrange(50,500),10,1000)
endif

; run through and display all the arrays
for each Job()

if Job.ArrayHandle
makearray ThisArray()
ThisArray()=Job.ArrayHandle
print ShowArray(ThisArray())
endif
if Job.state=JobState_Complete
if Timer()> Job.deleteTime
DeleteJob()
endif
endif
if GetCursorY()>GetScreenHeight() then exit
next


; Process all the jobs
ProcessSortJobs()


Sync
loop



Function ShowArray(me())
Size=getarrayelements(me())
if Size>40 then Size=30
for lp=0 to Size
s$=s$+str$(me(lp))+","
next
EndFunction s$



Function DeleteJob()
MakeArray ThisArray()

ThisArray()=Job.ArrayHandle
Undim ThisArray()
Job = null
EndFunction



Function AddNewJob(Size,Lowest,Highest)
Job = new tJobs
Job.State = JobState_InitialData

Job.ArraySize =Size
Job.PassCount=0
Job.DataRangeLow=lowest
Job.DataRangehi=highest


EndFunction




Function ProcessSortJobs()

CurrentTime=Timer()

; The max time this can take is 15 milliseconds
EndOfProcessTime=CurrentTime+15
makeArray ThisArray()

repeat

ActiveThreads=0

; runt through all the jobs and update them
for each job()
Select Job.State

case JobState_InitialData
; this job was added, but has not be initialized
Size=Job.ArraySize
Job.ArrayHandle=NewArray(Size)
ThisArray()=Job.ArrayHandle
for lp=0 to size
ThisArray(lp)=RndRange(Job.DataRangeLow,Job.DataRangeHi)
next
Job.state=JobState_Sorting

case JobState_Sorting
; this job is sorting pass over the array

; move the this jobs array handle into our array stub
ThisArray()=Job.ArrayHandle

; Get the number of passes this jobs has already had
PassCount=Job.PassCount

; Do a sort pass of the array data
Sorted=BubbleSortPass(ThisArray(),PassCount)

; is this array sorted ?
if Sorted =0
Job.State=JobState_Complete
Job.DeleteTime=CurrentTime
endif
Job.PassCount=Passcount+1
ActiveThreads++

endselect


if Thread_Jobs =true
if Timer()>EndOfProcessTime then exit
endif

Login required to view complete source code




Adaz


Ádáz

Hungary

kevin