Jump to content
darshanKM

multiple SC_THREAD inside one Module

Recommended Posts

Hi all,

I am new to SystemC and I have a course in my current semester.

It would be great if someone answers my below questions:

Case1: Consider a SC_MODULE(mod_1) with two SC_THREAD(p1); SC_THREAD(p2);

           Question1: which thread starts first?

           Question2: What is the order these two threads run? Do they run parallel or one after the other?

Case2: Consider a SC_MODULE(mod_1) with one SC_THREAD(p1); SC_METHOD(p2);

           Question1: which thread starts first?

           Question2: What is the order these two threads run? Who starts first? 

           Question3: Does SC_METHOD has priority than SC_THREAD? i.e if suppose SC_THREAD was running and same time SC_METHOD received a sensitivity. Does SC_THREAD pause and SC_THREAD starts execution?

My questions are very primitive but it is very much important with exam point of view..:-)

Thanks in Advance..

Share this post


Link to post
Share on other sites

Hello, 

All the answers can be found in standard: http://standards.ieee.org/getieee/1666/download/1666-2011.pdf

Questions are tricky, so it's hard to guess answers without reading documentation carefully.

Especially:

Quote

           Question2: What is the order these two threads run? Do they run parallel or one after the other?

Afaik, standard does not prohibit running processes in parallel, in case it is invisible to user. So implementation of standard can run some processes in parallel.  But Accellera opensource SystemC implementation is single-threaded.

Share this post


Link to post
Share on other sites

Hello,

As Roman Popov rightly pointed out the processes can run in parallel and this is crucial to model the hardware behavior as well. Spec states that at time 0 all processes (SC_METHOD and SC_THREAD) will run in an undefined order until they suspend. The ordering of the processes may not be of much importance here as it has no impact on the overall behavior of the system. However, the ordering may be based on the order in which you define these processes in the constructor. May be experts can throw some lights on this.

If you look at the following code and the simulation result,

  


class ThreadOrdering  : public sc_module
{
  public:
	SC_CTOR(ThreadOrdering)
	{
		SC_METHOD(methodOne) ;
		sensitive << terminateMethod_ev ;
		SC_THREAD(threadOne) ;
		SC_THREAD(threadTwo) ;
	}
	void threadOne()
	{
		cout << "Thread One is Running...! @ " << sc_time_stamp()  << endl;
		wait(1.0, SC_NS);
		cout << " Thread One Processing Done @ " << sc_time_stamp() << endl;
	}
	void threadTwo()
	{
		cout << "Thread Two is Running...! @ " << sc_time_stamp() << endl;
		wait(1.0, SC_NS);
		terminateMethod_ev.notify();
		cout << " Thread Two Processing Done @ " << sc_time_stamp() << endl;
	}
	void methodOne()
	{
		cout << "Method One Processing...!" <<" @ " << sc_time_stamp() << endl ;
		next_trigger(terminateMethod_ev);
	}
private:
	sc_event terminateMethod_ev;
};

Simulation Result

Compile done. Starting run...

        SystemC 2.3.1-Accellera --- Jun  1 2014 14:49:19
        Copyright (c) 1996-2014 by all Contributors,
        ALL RIGHTS RESERVED
Method One Processing...! @ 0 s
Thread One is Running...! @ 0 s
Thread Two is Running...! @ 0 s
 Thread One Processing Done @ 1 ns
 Thread Two Processing Done @ 1 ns
Method One Processing...! @ 1 ns

Thanks,

R.Adiga

Share this post


Link to post
Share on other sites
12 hours ago, Roman Popov said:

Hello, 

All the answers can be found in standard: http://standards.ieee.org/getieee/1666/download/1666-2011.pdf

Questions are tricky, so it's hard to guess answers without reading documentation carefully.

Especially:

Afaik, standard does not prohibit running processes in parallel, in case it is invisible to user. So implementation of standard can run some processes in parallel.  But Accellera opensource SystemC implementation is single-threaded.

Thank You Roman for the file link.. I read the Simulation chapter and understood how SC_THREAD and SC_METHOD run and how SystemC channels are updated in evaluation and update phases.. Thanks once again:-)

Share this post


Link to post
Share on other sites
5 hours ago, R.Adiga said:

Hello,

As Roman Popov rightly pointed out the processes can run in parallel and this is crucial to model the hardware behavior as well. Spec states that at time 0 all processes (SC_METHOD and SC_THREAD) will run in an undefined order until they suspend. The ordering of the processes may not be of much importance here as it has no impact on the overall behavior of the system. However, the ordering may be based on the order in which you define these processes in the constructor. May be experts can throw some lights on this.

If you look at the following code and the simulation result,

  



class ThreadOrdering  : public sc_module
{
  public:
	SC_CTOR(ThreadOrdering)
	{
		SC_METHOD(methodOne) ;
		sensitive << terminateMethod_ev ;
		SC_THREAD(threadOne) ;
		SC_THREAD(threadTwo) ;
	}
	void threadOne()
	{
		cout << "Thread One is Running...! @ " << sc_time_stamp()  << endl;
		wait(1.0, SC_NS);
		cout << " Thread One Processing Done @ " << sc_time_stamp() << endl;
	}
	void threadTwo()
	{
		cout << "Thread Two is Running...! @ " << sc_time_stamp() << endl;
		wait(1.0, SC_NS);
		terminateMethod_ev.notify();
		cout << " Thread Two Processing Done @ " << sc_time_stamp() << endl;
	}
	void methodOne()
	{
		cout << "Method One Processing...!" <<" @ " << sc_time_stamp() << endl ;
		next_trigger(terminateMethod_ev);
	}
private:
	sc_event terminateMethod_ev;
};

Simulation Result


Compile done. Starting run...

        SystemC 2.3.1-Accellera --- Jun  1 2014 14:49:19
        Copyright (c) 1996-2014 by all Contributors,
        ALL RIGHTS RESERVED
Method One Processing...! @ 0 s
Thread One is Running...! @ 0 s
Thread Two is Running...! @ 0 s
 Thread One Processing Done @ 1 ns
 Thread Two Processing Done @ 1 ns
Method One Processing...! @ 1 ns

Thanks,

R.Adiga

Dear Adiga,

Thanks for the explanation with  a code snippet.. the messages: 

"Method One Processing....! @ 0 s",

"Thread One Processing....! @ 0 s"

"Thread Two Processing....! @ 0 s",, are because of Initilaization phase(first phase of simluation cycle) I guess..correct me if I am wrong.

Regards

darshan

Share this post


Link to post
Share on other sites
6 hours ago, darshanKM said:

Dear Adiga,

Thanks for the explanation with  a code snippet.. the messages: 

"Method One Processing....! @ 0 s",

"Thread One Processing....! @ 0 s"

"Thread Two Processing....! @ 0 s",, are because of Initilaization phase(first phase of simluation cycle) I guess..correct me if I am wrong.

Regards

darshan

Yes. You can exclude some process from initialization phase by calling dont_initialize() after creating process.  For example:

        SC_METHOD(methodOne) ;
        sensitive << terminateMethod_ev ;
        dont_initialize();

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×