Jump to content

multiple SC_THREAD inside one Module


darshanKM

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..

Link to comment
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.

Link to comment
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

Link to comment
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:-)

Link to comment
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

Link to comment
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();

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...