After some more research i am not clear what programming language are using that syntax.
Looks like this:
Code: Select all
Parallel.For (0, texture.height, y => {
for (int x = 0; x < texture.width; x++) {
int sourceIndex = (y * texture.width) + x;
dest[sourceIndex].r = source[sourceIndex].r;
dest[sourceIndex].g = source[sourceIndex].g;
dest[sourceIndex].b = source[sourceIndex].b;
}
});
Sinds i am not very good at multi core programming or memory pointer stuff, i think you can do this better job than i did

Still made a little code to try it out my self.
compiled with fbc -w all "%f" -target win32
Code: Select all
#include "fbthread.bi"
Const MAX_THREADS = 31
Function GetCPUCores_x2 As UByte
Dim As UByte numcores
Asm
mov eax,&h0A
cpuid
mov [numcores],al
End Asm
If numcores = 0 Then numcores = 1
Return numcores*2-1 'multiplay by 2
End Function
type range_type
as long low
as long high
as any ptr sub_ptr
end type
sub Launch_sub(byval mysub as sub(byref a as long, byref b as long), myptr as range_type ptr )
dim as range_type ptr parallel_ptr ' pointer to org memory
parallel_ptr = myptr
mysub( myptr->low, myptr->high )
end sub
Sub Parallel_thread(byval userdata as any ptr)
' Work (other thread)
dim as range_type ptr test3 ' pointer to org memory
test3 = userdata
Launch_sub(test3->sub_ptr, userdata)
End Sub
sub thread_launch(num_of_cores as byte, byref a as any ptr, range_low as long, range_high as long)
dim as long size = range_high - range_low
if size - range_low < num_of_cores or num_of_cores > MAX_THREADS then exit sub
dim as any ptr thread(MAX_THREADS)
dim as range_type range(MAX_THREADS)
dim as byte i
if num_of_cores > 0 then
dim as range_type testers(MAX_THREADS)
'range(0).low = range_low
for i = 0 to num_of_cores
var steps = (size \ (num_of_cores+1)) * i
'print range_low + steps, range_low + steps + (size \ (num_of_cores + 1))-1
range(i).low = range_low + steps
range(i).high = range_low + steps + (size \ (num_of_cores +1))-1
next
range(num_of_cores).high = range_high
for i = 0 to num_of_cores
testers(i).low = range(i).low
testers(i).high = range(i).high
testers(i).sub_ptr = a
thread(i) = ThreadCreate(@Parallel_thread, @testers(i))
'ThreadWait(thread(i))
next i
for i = 0 to num_of_cores
ThreadWait(thread(i))
next
else
dim as range_type test = ( range_low ,range_high, a)
thread(0) = ThreadCreate(@Parallel_thread, @test)
ThreadWait(thread(0))
end if
end sub
sub tt(byref a as long, byref b as long)
'print "hallo", a, b
dim as double do_somthing = 0
for i as longint = a to b
do_somthing ^= i
do_somthing += i
next i
end sub
dim as double t = timer
print "number of threads: "; GetCPUCores_x2+1
do
t = timer
thread_launch(GetCPUCores_x2, @tt(), 0 , 100000000)
print "multi core",timer - t
'print
t = timer
thread_launch(0, @tt(), 0 , 100000000)
print "single core",timer - t
print
sleep 1000
loop until inkey <> ""
print
sleep
end
number of threads: 8
multi core 2.703375900006378
single core 16.82610790000416
I wish this was possible in freebasic in a easier way than this.
The loop is spited up into pieces for each core in my code.
Problem is that it has to create that threats at that point.
It is probably better to pre create the threats and call them when needed.
Also give it a more realistic load to compute.
Detecting number cores sometime is incorrect i think (by 2 cores ?)
Now days computers get more and more cores and still not easy to work with.