Massive Multithreading demo

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
mrminecrafttnt
Posts: 131
Joined: Feb 11, 2013 12:23

Massive Multithreading demo

Post by mrminecrafttnt »

This one is a massive multithreading demo that reduces the calculation time by increasing the number of threads.
It calculates a simple values and saves to allocated data to generate work that can be benchmarked.
You can increase the threads to reduce the needed time (up to 1024 threads and more, only your pc is the limit..)
It needs a lot of ram (16GB or more recommed)
Have fun! :)

Code: Select all

'settings are here
const nr_threads = 4 ' how much threads (more = faster, try as example 2048 threads this works!!(when you have a good pc..))
const ed = 1500000000 'how much integers to calulate(warning: needs many ram..)


'the threaddata, this will be transfered to the mt_fill with threadcreate to every single thread i called this header
type threaddata
    s as integer 'startposition
    e as integer 'endposition
    dataadr as integer ptr 'adress of allocated data
end type


dim as double benchmark_start = timer,benchmark_end ' used to benchmark



sub mt_fill(d as any ptr)
    dim as threaddata ptr mydata  = d
        for i as integer = mydata->s to mydata->e
            mydata->dataadr[i]=i*100 ' here will the data be calculated
    next
end sub


dim as any ptr thread(nr_threads) ' create thread handlers here
dim as threaddata t(nr_threads)' create threaddata here
Print "Pleas wait..."
Print "Allocate";len(integer)*ed;" bytes"
Print "Calulate with ";nr_threads;" threads"
Print "Headersize:";len(t)*nr_threads; "bytes"
dim int_data as any ptr = allocate(len(integer)*ed)
Print "Create Threaddata..";

'Here will the threaddata be caluclated
for i as integer = 0 to nr_threads-1
    with (t(i))
    if i = 0 then
'        .t = 1
        .s = 0
        .e = int(ed / nr_threads)
        .dataadr = int_data
    else
'        .t = i+1
        .s = (t(i-1).e)+1
        .e = int(ed / ((nr_threads-1)/i))
        .dataadr = int_data
    end if
    end with
    thread(i)=threadcreate(@mt_fill,@t(i))
next
Print "done."

'Waiting for all threads are ready
Print "Waiting for Threads are ready..";
for i as integer = 0 to nr_threads-1
    threadwait(thread(i))
next
PRINT "done."

benchmark_end = timer-benchmark_start 'calculate the nedded time
deallocate (int_data) ' makes memory free

'Prints the need time to benchmark
print "NEEDED TIME:";
print using "###.#####";benchmark_end
Print "Press a key to exit"

sleep
end 'shutdown
fxm
Moderator
Posts: 12082
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Massive Multithreading demo

Post by fxm »

In the 'ThreadCreate' loop, the first value of '.s' is '0' and the last value of '.e' is 'ed'.
So 'ed+1' integers are used.
A simple solution to not exceed the allocated memory by an integer could be for example to increase this memory by one integer:
dim int_data as any ptr = allocate(len(integer)*(ed+1))
Post Reply