Subroutines, shared variables, etc.

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

User avatar
Luis Babboni
Posts: 464
Joined: Sat Feb 28, 2015 4:37 pm
Location: Argentina

Subroutines, shared variables, etc.

Post by Luis Babboni »

Hi.

What about the suggested way to use subroutines and shared variables with them?

I mean, if I use subroutines with no shared variables, I think the code will be more clear and simpler to debug, but I´m afraid it is a not good for engine speed.

Thanks.
abulmo2
Posts: 433
Joined: Fri Dec 16, 2016 11:04 am
Location: France
Full name: Richard Delorme

Re: Subroutines, shared variables, etc.

Post by abulmo2 »

I am not sure to understand what you mean.
Subroutine is a general concept that translates into function, method, etc. in modern languages and leads to the structured programming approach.
Shared variables is a feature of the programming language APL.

I guess you mean something else. Maybe you want to avoid the usage of global variable in your program?
Richard Delorme
User avatar
Luis Babboni
Posts: 464
Joined: Sat Feb 28, 2015 4:37 pm
Location: Argentina

Re: Subroutines, shared variables, etc.

Post by Luis Babboni »

abulmo2 wrote: Sun Apr 11, 2021 6:07 pm I am not sure to understand what you mean.
Subroutine is a general concept that translates into function, method, etc. in modern languages and leads to the structured programming approach.
Shared variables is a feature of the programming language APL.

I guess you mean something else. Maybe you want to avoid the usage of global variable in your program?
Thanks abulmo2 for your time!

May be I must said it before: I use FreeBasic. May be this is important to say.

Yes, I´m referred to global variables. If I do not use it, I understand every time you call a subroutine, there is a need to load again all the variables that have a value load in other part of the program. So I guess is a waste of time compared to use global variables. But on the other hand, not use global variables allow you to test your subroutines easier and make a less messed code.
Note: I´m not a programmer, so sorry if I asked a nonsense or very obvious things.
abulmo2
Posts: 433
Joined: Fri Dec 16, 2016 11:04 am
Location: France
Full name: Richard Delorme

Re: Subroutines, shared variables, etc.

Post by abulmo2 »

Using global variables is usually not recommended. As they may be modified anywhere, it could then be hard to understand from where comes their values. However, chess programs are relatively simple and small, so tracking a global variable is not so hard in practice. If you plan to make your program multithreaded in the future, sharing the same variable between threads is probably not what you want.

For the cost of passing variables to subroutine, you can use a ByRef variable or a pointer to just pass the address of the variable instead of its value. For big custom type, it can make your program faster.
Richard Delorme
Sesse
Posts: 300
Joined: Mon Apr 30, 2018 11:51 pm

Re: Subroutines, shared variables, etc.

Post by Sesse »

In general: Don't micro-optimize until you have gotten down your large-scale structure first.

Making your program uglier by trying to send fewer variables to subroutines (functions) might save you 5% CPU time (just to say a number; take it with a grain of salt), and will easily cause bugs costing you days and weeks of development time. That time could probably be better spent implementing a better move generator, or a tree pruning technique, or tuning your evaluation.

Or moving to a faster language than FreeBASIC, which is likely to win you much more than 5%…
User avatar
Luis Babboni
Posts: 464
Joined: Sat Feb 28, 2015 4:37 pm
Location: Argentina

Re: Subroutines, shared variables, etc.

Post by Luis Babboni »

abulmo2 wrote: Mon Apr 12, 2021 12:33 am Using global variables is usually not recommended. As they may be modified anywhere, it could then be hard to understand from where comes their values. However, chess programs are relatively simple and small, so tracking a global variable is not so hard in practice. If you plan to make your program multithreaded in the future, sharing the same variable between threads is probably not what you want.

For the cost of passing variables to subroutine, you can use a ByRef variable or a pointer to just pass the address of the variable instead of its value. For big custom type, it can make your program faster.
So I understand you recommended to make each subroutine as "standalone code" like this:

Sub test (ByRef a As Integer, ByRef b As Integer)
a=2*a
b=2*b
End Sub

Use ByVal is not as recommended?

Sorry but my english and my skills as programmer* are not good. What you mean with "For big custom type"?

*I did yet an engine that works without bugs, but its ELO is just around 1100 and its code is a big mess. So now I want to do something more tidy. i´ll go now for with a bitboards one but still not for multithread.
abulmo2
Posts: 433
Joined: Fri Dec 16, 2016 11:04 am
Location: France
Full name: Richard Delorme

Re: Subroutines, shared variables, etc.

Post by abulmo2 »

Luis Babboni wrote: Mon Apr 12, 2021 1:09 am
abulmo2 wrote: Mon Apr 12, 2021 12:33 am Using global variables is usually not recommended. As they may be modified anywhere, it could then be hard to understand from where comes their values. However, chess programs are relatively simple and small, so tracking a global variable is not so hard in practice. If you plan to make your program multithreaded in the future, sharing the same variable between threads is probably not what you want.

For the cost of passing variables to subroutine, you can use a ByRef variable or a pointer to just pass the address of the variable instead of its value. For big custom type, it can make your program faster.
So I understand you recommended to make each subroutine as "standalone code" like this:

Sub test (ByRef a As Integer, ByRef b As Integer)
a=2*a
b=2*b
End Sub

Use ByVal is not as recommended?
For small data types, you can pass them by value, except if you want them to be modified externally.

Code: Select all

Sub test (ByRef a As Integer, ByRef b As Integer)
	a=2*a
        b=2*b
End Sub

x = 2
y = 3
test(x, y)
now x = 4, y = 6
If you replace ByRef with ByVal, x and y should keep their original value of 2 and 3.
What you mean with "For big custom type"?
I meant this: https://documentation.help/FreeBASIC/KeyPgType.html
If you have a lot of fields, it is faster to pass them ByRef than by ByVal.
Richard Delorme
jdart
Posts: 4366
Joined: Fri Mar 10, 2006 5:23 am
Location: http://www.arasanchess.org

Re: Subroutines, shared variables, etc.

Post by jdart »

Generally speaking, it is harder to understand and debug code that might affect global variables in addition to whatever arguments are passed to the function (aka "side effects"). It shouldn't really affect speed though.
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: Subroutines, shared variables, etc.

Post by Sven »

I would disagree only slightly. A program using only global variables and no subroutine parameters (or even worse, no subroutines at all ...) is probably 5% faster and 500% more buggy than a "normal" program. :lol:
Sven Schüle (engine author: Jumbo, KnockOut, Surprise)
User avatar
Luis Babboni
Posts: 464
Joined: Sat Feb 28, 2015 4:37 pm
Location: Argentina

Re: Subroutines, shared variables, etc.

Post by Luis Babboni »

Sven wrote: Mon Apr 12, 2021 9:36 pm I would disagree only slightly. A program using only global variables and no subroutine parameters (or even worse, no subroutines at all ...) is probably 5% faster and 500% more buggy than a "normal" program. :lol:

Good point! Thanks!