More than one core PC and thread*** sentences

New to FreeBASIC? Post your questions here.
Luis Babboni
Posts: 303
Joined: Mar 15, 2015 12:41

More than one core PC and thread*** sentences

Postby Luis Babboni » May 27, 2015 10:11

Hi,

those thread**** sentences are to use more than one core at the same time in a some kind of parallel execution of more than one program?

For example for my attempt of chess engine I could use one core to search and evaluate positions given an score to each one while at the same time another core is using to be ordering those scores from higher to lower for example without need to stop the search and evaluation the first core is doing?

Thanks.
RockTheSchock
Posts: 226
Joined: Mar 12, 2006 16:25

Re: More than one core PC and thread*** sentences

Postby RockTheSchock » May 27, 2015 10:31

Luis Babboni wrote: use more than one core at the same time in a some kind of parallel execution of more than one program?
Please tell us what you want to do in the big picture. Has it something to do with your 1.6TB matrix? Do you want to process a large quantitiy of values?

If your question is how to make execution time of certain calculations as small as possible we need to know exactly what you want to process and the context of it. Does parallel execution make sense? Well it depends. There are algorithms which dont scale at all, so you have to switch the algorithm. There are cases where an optimized single threaded algorithm is more than enough and it would just complicate things. Threading / Parallelising is useful where you have many isolated calculations of same type not depending on the result of the other ones
Luis Babboni
Posts: 303
Joined: Mar 15, 2015 12:41

Re: More than one core PC and thread*** sentences

Postby Luis Babboni » May 27, 2015 14:41

Thanks RockTheSchock!

Actually my 1.6TB matrix (just 6GB to be exactly) was reduced without lost of functionality (I think) to just 180MB!!! :-D

Actually I´m not reach yet the point to make a code about what I exactly need.
But the idea is what I said, sorry for my poor english and programming lack of skills to be more clear :-(

Again trying to make my explanation differently:
In few words and not so much exactly but enoguh to understand the question, for a chess engine, you need to give an score to possibles positions you could reach from actual and then move the piece that put the situation in the position with better score for you.
Cause the amount of possible positions after some moves are too large, what good engines do is not to give an score to all possibles positions. They do something like score all positions that you can reach after one move but not study all the positions you could reach after two moves. Instead, just study the positions you could reach after two moves starting from the higher scores postions after one move.
To do this, in a way I could imagine how to do it, is needed to order the scores obteined for positions after one move.
So my idea is to use one core just to make that order of scores without stop the work of the core that working in making those scores.

Mmmm..... not sure if now is even less clear than before. :-(
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Re: More than one core PC and thread*** sentences

Postby MichaelW » May 27, 2015 15:41

This is crude Windows-only example that (I hope) shows how to create and execute one thread per core. I compiled to a 64-bit app with Version 1.02.0 (03-23-2015), built for win64 (64bit) and tested on my laptop with 2 physical cores and no HTT.

Code: Select all

#include "windows.bi"

dim shared as integer g_exit

''-------------------------------------------------
'' This implements the popcount_3 code from here:
'' http://en.wikipedia.org/wiki/Hamming_weight
''-------------------------------------------------
const m1  = &H5555555555555555LL
const m2  = &H3333333333333333LL
const m4  = &H0f0f0f0f0f0f0f0fLL
const h01 = &H0101010101010101LL
function PopCount(x as integer) as integer
    x -= (x shr 1) and m1
    x = (x and m2) + ((x shr 2) and m2)
    x = (x + (x shr 4)) and m4
    return (x * h01) shr 56
end function   

''---------------------------------------------------------------------------
'' This is the thread procedure for all threads other than the main thread.
''---------------------------------------------------------------------------

sub ThreadProc( byval userdata as any ptr )
    do
        sleep 2000
        print GetCurrentThreadId()
    loop until g_exit
end sub

'print PopCount(&b0)               ' 0
'print PopCount(&b010)             ' 1
'print PopCount(&b0110)            ' 2
'print PopCount(&b000100100100)    ' 3
'print PopCount(&b101111111111101) ' 13
'print

dim as DWORD_PTR processAffinityMask,systemAffinityMask
dim as integer cores

print GetProcessAffinityMask( GetCurrentProcess(), @processAffinityMask, _
                                                   @systemAffinityMask )
cores = PopCount( systemAffinityMask )

print bin(processAffinityMask,8)
print bin(systemAffinityMask,8)
print "System Affinity Mask shows";cores;" cores"
print

dim as HANDLE hThread(1 to cores-1)

''---------------------------------------------------------------------------
'' The affinity masks are bit vectors where each bit represents a processor
'' core, so the core numbers are powers of 2. Set the affinity masks so each
'' thread is restricted to a single core, starting with the main thread
'' running on core 0.
''---------------------------------------------------------------------------

SetThreadAffinityMask( GetCurrentThread(), 1 )

for i as integer = 1 to cores - 1
    hThread(i) = CreateThread( NULL, 0, cast(LPTHREAD_START_ROUTINE,@ThreadProc), _
                               0, CREATE_SUSPENDED, NULL )
    SetThreadAffinityMask( hThread(i), 2^i )
next   

print "Main thread ID:";GetCurrentThreadId()

for i as integer = 1 to cores - 1
    ResumeThread( hThread(i) )
next

do
    sleep 2000
    print GetCurrentThreadId()
loop until inkey<>""

g_exit += 1

WaitForMultipleObjects( cores - 1, @hThread(1), TRUE, INFINITE )
 
sleep

Running on my laptop under Windows 8.1-64, it sometimes takes a while before the two threads appear to run at the same rate.

And note that a core with HTT is not equivalent to 2 physical cores, and that I currently have no idea how to determine which cores are physical cores and which are HTT cores.
St_W
Posts: 1483
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: More than one core PC and thread*** sentences

Postby St_W » May 27, 2015 21:17

MichaelW wrote:This is crude Windows-only example that (I hope) shows how to create and execute one thread per core.
[...]

You should never need such a thing like shown in the example above (binding a thread to certain processors). Managing which thread runs on which core is a task of the operating system and you should never interfere with that, except you do have a very good reason to do so.

You need a clear concept how to spread the processing among multiple threads and - likely more important and difficult - where and how to synchronize the parallel tasks.
fxm
Posts: 9529
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: More than one core PC and thread*** sentences

Postby fxm » May 28, 2015 4:48

St_W wrote:You should never need such a thing like shown in the example above (binding a thread to certain processors). Managing which thread runs on which core is a task of the operating system and you should never interfere with that, except you do have a very good reason to do so.

So you can use the only FreeBASIC keywords for coding your multi-threading.
RockTheSchock
Posts: 226
Joined: Mar 12, 2006 16:25

Re: More than one core PC and thread*** sentences

Postby RockTheSchock » May 28, 2015 6:08

Luis Babboni wrote:In few words and not so much exactly but enoguh to understand the question, for a chess engine, you need to give an score to possibles positions you could reach from actual and then move the piece that put the situation in the position with better score for you. Cause the amount of possible positions after some moves are too large, what good engines do ...
you an read here:
https://chessprogramming.wikispaces.com/Programming

And how parallel processing in particular is done:
https://chessprogramming.wikispaces.com/Parallel+Search


You should start with implementing an Alpha-Beta algortithm and bitboards.
Luis Babboni
Posts: 303
Joined: Mar 15, 2015 12:41

Re: More than one core PC and thread*** sentences

Postby Luis Babboni » May 29, 2015 0:04

Thanks RockTheSchock!

I saw this web and I use to come bak to it but still I´m very basic yet.... I´m trying to be understanding little by little.

I think I need a two more years to have my first working chess program.

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 1 guest