Intel® oneAPI Threading Building Blocks
Ask questions and share information about adding parallelism to your applications when using this threading library.

How to control # of threads?

farzaneh
Beginner
686 Views
Hi,
I have wrote a program in tbb.
but runtime is almost the same for sequential and parallel version.
I want to make sure that my sequential program uses only 1 core (I have a corei5 cpu)
I'm not sure how to do this and where is the problem (runtime)
my program computes shortest path. My graph is stored asadjacencylist. I have an array as inputquires.
in sequential program I used a for loop for answering to queries.
in parallel program I used the same code and replace the simple loop by a parallel loop
sequential code:
for(int i=0;i
X=shortest_path(S,T,i+q);
parallel loop definition:
class parallel_shortest_path{
int * i1, * i2;
int *sp;
public:parallel_shortest_path(int *I1, int *I2,int *SP): i1(I1), i2(I2), sp(SP) { }
void operator() (const blocked_range &r) const
{
for(size_t SPI=r.begin(); SPI!=r.end();SPI++)
if(SPI
sp[SPI]=shortest_path(i1[SPI],i2[SPI],SPI);
}
};

using parallel loop:
parallel_for(blocked_range<size_t>(0,q,1000),parallel_shortest_path(S,T,X) ); I
I usesdtask_scheduler_init init();too.
but runtime does not change if I pass it no parameter or with inputs 1, 2 or 4!
I'd really appreciate it if someone could help me.
Where is my problem?
how could I make sure my program is running on how many cores?
and how could I get speed up? (it's just answering a number of queries)


Thanks in advance
0 Kudos
10 Replies
RafSchietekat
Valued Contributor III
686 Views
Try to have task_scheduler_init somewhere on the stack before anything else happens, e.g., as an automatic variable in main().
0 Kudos
farzaneh
Beginner
686 Views
thank you!
I moved the "task_scheduler_init" to first line but again no change in runtime!
BUT
I add this line to my code:
cout<<:TASK_SCHEDULER_INIT::AUTOMATIC><
and the result was -1
!!!!!!!!
What is the problem?!
I ran the tbb sample codes and everything was fine!
for example "prime numbers" example
How could I fix it?
Thanks,
0 Kudos
RafSchietekat
Valued Contributor III
686 Views
automatic is the default argument, telling the scheduler to use whatever parallelism it detects, and is if no real use since in TBB 2.2 (I think) a scheduler got to be allocated implicitly without need for task_scheduler_init. Use a strictly positive number such as 1 instead to override the default choice. Make sure to use task_scheduler_init as a type to declare a variable that lives from before any TBB work starts until after it ends for it to reliably have the desired effect.

(Added) Anyway, if you would only read the Reference Manual, you would find that it has an example "Sketch of one way to do a scaling study" that shows what's required. I am assuming that the scheduler knows whether it was allocated implicitly or through a task_scheduler_init and therefore also knows to excuse itself when the responsible task_scheduler_init goes away, so that the example code is stll valid and does not need to be revised, but I have not tested this assumption.
0 Kudos
farzaneh
Beginner
686 Views
Thank you!
But it seems my problem is completely differenet.
As I said, I tried task_scheduler_init wih parameter 1, 2, 4, automatic ,etc.... before asking in this forum
It seems that my system can not do any parallelism .(automatic==-1 is one reason showing this fact!)
(but tbb primes example is showing speedup!!!!!!!!!)
for example, trying openMP, a hello world code(code at the end), the output is:
Hello World from thread = 0
Number of threads = 1
I have a corei5 cpu and my OS is Windows7, coding in Visual Studio.
I'm completely confused and can not undrestand why this happens?
any help?
/******************************************************************************
* FILE: omp_hello.c
* DESCRIPTION:
* OpenMP Example - Hello World - C/C++ Version
* In this simple example, the master thread forks a parallel region.
* All threads in the team obtain their unique thread number and print it.
* The master thread only prints the total number of threads. Two OpenMP
* library routines are used to obtain the number of threads and each
* thread's number.
* AUTHOR: Blaise Barney 5/99
* LAST REVISED: 04/06/05
******************************************************************************/
#include
#include
#include
int main (int argc, char *argv[])
{
int nthreads, tid;
/* Fork a team of threads giving them their own copies of variables */
#pragma omp parallel private(nthreads, tid)
{
/* Obtain thread number */
tid = omp_get_thread_num();
printf("Hello World from thread = %d\n", tid);
/* Only master thread does this */
if (tid == 0)
{
nthreads = omp_get_num_threads();
printf("Number of threads = %d\n", nthreads);
}
} /* All threads join master thread and disband */
}
/******************************************************************************* FILE: omp_hello.c* DESCRIPTION:* OpenMP Example - Hello World - C/C++ Version* In this simple example, the master thread forks a parallel region.* All threads in the team obtain their unique thread number and print it.* The master thread only prints the total number of threads. Two OpenMP* library routines are used to obtain the number of threads and each* thread's number.* AUTHOR: Blaise Barney 5/99* LAST REVISED: 04/06/05******************************************************************************/#include #include #include
int main (int argc, char *argv[]){int nthreads, tid;
/* Fork a team of threads giving them their own copies of variables */#pragma omp parallel private(nthreads, tid) {
/* Obtain thread number */ tid = omp_get_thread_num(); printf("Hello World from thread = %d\n", tid);
/* Only master thread does this */ if (tid == 0) { nthreads = omp_get_num_threads(); printf("Number of threads = %d\n", nthreads); }
} /* All threads join master thread and disband */
}
0 Kudos
RafSchietekat
Valued Contributor III
686 Views
Between "I want to make sure that my sequential program uses only 1 core" and "It seems that my system can not do any parallelism", I'm afraid you've lost me. Please start with the basics: what operating system and compiler, exactly what TBB version (preinstalled or from the TBB website?), what do the graphical examples that come with TBB show (in particular the one with the spheres fractal clearly shows how many threads are being used)? tbb::get_num_threads() is also a good source of information.
0 Kudos
farzaneh
Beginner
686 Views
well. I explain everything from the begining again!
I got same runtime for both sequential and parallel program.
"prime" tbb example was showing speed up and I examined task_scheduler_init with different parameters, so I thought it may be the sequential program that is getting parallel by OS or something like that.
But, when I saw thattbb::task_scheduler_init::automatic is returning -1 and openMP program just shows 1 thread, I doubt that the problem is parallel program runing like a sequential one!
I am confused too!
I use Win7. I downloaded tbb from website "tbb30_20110427oss_win.zip"
I can't run TBB graphical examples. They got a lot of starge compile errors like:
"Error 1 error C2146: syntax error : missing ';' before identifier '__TBB_LockByte' c:\program files (x86)\intel\composerxe-2011\tbb\include\tbb\tbb_machine.h 653
Error 1 error C2146: syntax error : missing ';' before identifier '__TBB_LockByte' c:\program files (x86)\intel\composerxe-2011\tbb\include\tbb\tbb_machine.h 653"
I have DiretcX SDK installed.
I use Visual Studio. I have Intel compiler installed too and I think it is the compiler used.
(how could I make sure which compiler is used?)
using tbb::get_num_threads() returns this error message:
"Error 2 error C3861: 'get_num_threads': identifier not found "
Error 3 error C2039: 'get_num_threads' : is not a member of 'tbb'
!!!!
Thanks again for your kind attention.
0 Kudos
RafSchietekat
Valued Contributor III
686 Views
Sorry, I meant tbb::task_scheduler_init::default_num_threads() instead of tbb::get_num_threads().

I'll have a look at that specific TBB release later today if you don't get an answer by then.
0 Kudos
farzaneh
Beginner
686 Views
cout<<:TASK_SCHEDULER_INIT::DEFAULT_NUM_THREADS>
output is 4.
It seems its somthing about OS or so,BecauseI tested other openMP programs and all of them have just one thread.
for example the code below has this output:
"
Hello World from thread = 0
4
Number of threads = 1
Number of processors = 4
"
I really need someone help!
Thanks,
code:
#include
#include
#include
int main (int argc, char *argv[])
{
int nthreads, tid,nproc,nmaxthreads;
/* Fork a team of threads giving them their own copies of variables */
#pragma omp parallel num_threads(4)
{
/* Obtain thread number */
tid = omp_get_thread_num();
printf("Hello World from thread = %d\n", tid);
nmaxthreads=omp_get_max_threads();
printf("%d\n", nmaxthreads);
/* Only master thread does this */
if (tid == 0)
{
nthreads = omp_get_num_threads();
nproc =omp_get_num_procs();
printf("Number of threads = %d\n", nthreads);
printf("Number of processors = %d\n", nproc);
}
} /* All threads join master thread and disband */
}
0 Kudos
RafSchietekat
Valued Contributor III
686 Views
#6 "Error 1 error C2146: syntax error : missing ';' before identifier '__TBB_LockByte' c:\program files (x86)\intel\composerxe-2011\tbb\include\tbb\tbb_machine.h 653
Error 1 error C2146: syntax error : missing ';' before identifier '__TBB_LockByte' c:\program files (x86)\intel\composerxe-2011\tbb\include\tbb\tbb_machine.h 653"
These files are from the TBB that comes with Intel Composer XE 2011. I suggest that you remove the installation from tbb30_20110427oss_win.zip again to see what happens. If you wish, look for any instructions with Composer XE on how to safely upgrade to a more recent TBB version, and decide whether you want to deal with that or just keep the included version.
0 Kudos
Alexey-Kukanov
Employee
686 Views
tbb::task_scheduler_init::automatic is a constant set to -1 everywhere (it just should be different from any valid number of threads), so it indicates nothing to you; don't be confused by it.
0 Kudos
Reply