Threads and computers with different number of cores

Hi

My coauthor and I have computers with a different number of cores (I have 4 and he has 8). I have written a program using up to 8 simultaneous threads which runs in my machine, but is this the most efficient way of writting the program given that it is going to be executed in two different machines? Just in case it matters I have GAUSS 9 and he has GAUSS 12.

Thanks.

1 Answer



0



In this particular case, it probably works out perfect, because GAUSS 12 has automatic internal threading of many functions. GAUSS 9 does not have this feature. So, your coauthor with more cores will be creating more threads anyway.

In the general case, though, if your threaded section if you have abstracted the section of code that is run in parallel, then you can use #define to control the number of threads fairly easily. Here is a trivial example:

new;
#define TWO_THREADS 1
#ifdef TWO_THREADS
#define NUM_THREADS 2
#endif

#ifdef TWO_THREADS
threadStat a = rndn(1,1);
threadStat b = rndn(1,1);
threadJoin;
print "running with 2 threads";
#else
threadStat a = rndn(1,1);
threadStat b = rndn(1,1);
threadStat c = rndn(1,1);
threadStat d = rndn(1,1);
threadJoin;
print "runing with 4 threads";
#endif

As is, the code above will execute the the block of code with two threads. If you comment out the line:

//#define TWO_THREADS 1

like this, you will run the second section with 4 threads.

The way that you would use it is to place the first section:

#define TWO_THREADS 1
#ifdef TWO_THREADS
#define NUM_THREADS 2
#endif

at the top of your program so it can easily be found and turned on/off. Then the threaded block could be anywhere in your file.

aptech

1,773

Your Answer

1 Answer

0

In this particular case, it probably works out perfect, because GAUSS 12 has automatic internal threading of many functions. GAUSS 9 does not have this feature. So, your coauthor with more cores will be creating more threads anyway.

In the general case, though, if your threaded section if you have abstracted the section of code that is run in parallel, then you can use #define to control the number of threads fairly easily. Here is a trivial example:

new;
#define TWO_THREADS 1
#ifdef TWO_THREADS
#define NUM_THREADS 2
#endif

#ifdef TWO_THREADS
threadStat a = rndn(1,1);
threadStat b = rndn(1,1);
threadJoin;
print "running with 2 threads";
#else
threadStat a = rndn(1,1);
threadStat b = rndn(1,1);
threadStat c = rndn(1,1);
threadStat d = rndn(1,1);
threadJoin;
print "runing with 4 threads";
#endif

As is, the code above will execute the the block of code with two threads. If you comment out the line:

//#define TWO_THREADS 1

like this, you will run the second section with 4 threads.

The way that you would use it is to place the first section:

#define TWO_THREADS 1
#ifdef TWO_THREADS
#define NUM_THREADS 2
#endif

at the top of your program so it can easily be found and turned on/off. Then the threaded block could be anywhere in your file.


You must login to post answers.

Have a Specific Question?

Get a real answer from a real person

Need Support?

Get help from our friendly experts.