none
OpenMP in VS 2005 C++ restart openmp thread RRS feed

  • Question

  • Hi, new here, I've got the following code working as an experiment, the key point is to allow an external event do some operation on the openmp thread, such as changing the starting index(so called restart in my program) and stop the openmp thread. No the problem is that I need to find a way to restart the individual openmp thread! Is there anything allow me to identify the equivalent MS thread to the openmp thread so that I can get the MS thread under the openmp section and just call ->start() on it? or is there any way to start the openmp thread itself directly? I looked through materials and unfortunately no luck so far. 

    #pragma once
    
    #include "stdafx.h"
    #include <stdio.h>
    #include <string>
    #include <omp.h>
    
    
    namespace testCLRWinForm1 {
    
    	using namespace System;
    	using namespace System::ComponentModel;
    	using namespace System::Collections;
    	using namespace System::Windows::Forms;
    	using namespace System::Data;
    	using namespace System::Drawing;
    	using namespace System::Threading;
    	
    	/// <summary>
    	/// Summary for Form1
    	///
    	/// WARNING: If you change the name of this class, you will need to change the
    	///     'Resource File Name' property for the managed resource compiler tool
    	///     associated with all .resx files this class depends on. Otherwise,
    	///     the designers will not be able to interact properly with localized
    	///     resources associated with this form.
    	/// </summary>
    	public ref class Form1 : public System::Windows::Forms::Form
    	{
    	public:
    		Form1(void)
    		{
    			InitializeComponent();
    			//
    			//TODO: Add the constructor code here
    			//
    			DATASIZE = 1000;
    			isRestart1 = false;
    			isStop1 = false;
    			isRestart2 = false;
    			isStop2 = false;
    			isRestart3 = false;
    			isStop3 = false;
    			isRestart4 = false;
    			isStop4 = false;
    			//createIntData();
    		}
    
    	protected:
    		/// <summary>
    		/// Clean up any resources being used.
    		/// </summary>
    		~Form1()
    		{
    			if (components)
    			{
    				delete components;
    			}
    		}
    
    	private: static Mutex^ mut = gcnew Mutex();
    	private: System::Windows::Forms::Button^ FireOmpBtn;
    	protected: 
    
    	private: System::Windows::Forms::ListBox^ listBox1;
    	
    	private: 
    		int DATASIZE;
    
    		bool isRestart1;
    		bool isStop1;
    		bool isRestart2;
    		bool isStop2;
    		bool isRestart3;
    		bool isStop3;
    		bool isRestart4;
    		bool isStop4;
    
    	private: Thread ^trd;
    
    	private: System::Windows::Forms::ListBox^ listBox2;
    
    	private: System::Windows::Forms::ListBox^ listBox4;
    	private: System::Windows::Forms::ListBox^ listBox5;
    	private: System::Windows::Forms::Button^ restart_btn1;
    	private: System::Windows::Forms::Button^ stop_btn1;
    	private: System::Windows::Forms::Button^ stop_btn2;
    	private: System::Windows::Forms::Button^ stop_btn3;
    	private: System::Windows::Forms::Button^ stop_btn4;
    
    
    
    
    
    
    
    
    	private: System::Windows::Forms::Button^ restart_btn4;
    	private: System::Windows::Forms::Button^ restart_btn3;
    	private: System::Windows::Forms::Button^ restart_btn2;
    
    	private:
    		/// <summary>
    		/// Required designer variable.
    		/// </summary>
    		System::ComponentModel::Container ^components;
    
    #pragma region Windows Form Designer generated code
    		/// <summary>
    		/// Required method for Designer support - do not modify
    		/// the contents of this method with the code editor.
    		/// </summary>
    		void InitializeComponent(void)
    		{
    			this->FireOmpBtn = (gcnew System::Windows::Forms::Button());
    			this->listBox1 = (gcnew System::Windows::Forms::ListBox());
    			this->listBox2 = (gcnew System::Windows::Forms::ListBox());
    			this->listBox4 = (gcnew System::Windows::Forms::ListBox());
    			this->listBox5 = (gcnew System::Windows::Forms::ListBox());
    			this->restart_btn1 = (gcnew System::Windows::Forms::Button());
    			this->stop_btn1 = (gcnew System::Windows::Forms::Button());
    			this->stop_btn2 = (gcnew System::Windows::Forms::Button());
    			this->stop_btn3 = (gcnew System::Windows::Forms::Button());
    			this->stop_btn4 = (gcnew System::Windows::Forms::Button());
    			this->restart_btn4 = (gcnew System::Windows::Forms::Button());
    			this->restart_btn3 = (gcnew System::Windows::Forms::Button());
    			this->restart_btn2 = (gcnew System::Windows::Forms::Button());
    			this->SuspendLayout();
    			// 
    			// FireOmpBtn
    			// 
    			this->FireOmpBtn->Location = System::Drawing::Point(67, 56);
    			this->FireOmpBtn->Name = L"FireOmpBtn";
    			this->FireOmpBtn->Size = System::Drawing::Size(158, 23);
    			this->FireOmpBtn->TabIndex = 0;
    			this->FireOmpBtn->Text = L"Fire Omp";
    			this->FireOmpBtn->UseVisualStyleBackColor = true;
    			this->FireOmpBtn->Click += gcnew System::EventHandler(this, &Form1::FireOmpBtn_Click);
    			// 
    			// listBox1
    			// 
    			this->listBox1->FormattingEnabled = true;
    			this->listBox1->Location = System::Drawing::Point(12, 102);
    			this->listBox1->Name = L"listBox1";
    			this->listBox1->Size = System::Drawing::Size(242, 173);
    			this->listBox1->TabIndex = 2;
    			// 
    			// listBox2
    			// 
    			this->listBox2->FormattingEnabled = true;
    			this->listBox2->Location = System::Drawing::Point(278, 102);
    			this->listBox2->Name = L"listBox2";
    			this->listBox2->Size = System::Drawing::Size(244, 173);
    			this->listBox2->TabIndex = 3;
    			// 
    			// listBox4
    			// 
    			this->listBox4->FormattingEnabled = true;
    			this->listBox4->Location = System::Drawing::Point(12, 304);
    			this->listBox4->Name = L"listBox4";
    			this->listBox4->Size = System::Drawing::Size(242, 160);
    			this->listBox4->TabIndex = 5;
    			// 
    			// listBox5
    			// 
    			this->listBox5->FormattingEnabled = true;
    			this->listBox5->Location = System::Drawing::Point(278, 304);
    			this->listBox5->Name = L"listBox5";
    			this->listBox5->Size = System::Drawing::Size(244, 160);
    			this->listBox5->TabIndex = 6;
    			// 
    			// restart_btn1
    			// 
    			this->restart_btn1->Location = System::Drawing::Point(566, 42);
    			this->restart_btn1->Name = L"restart_btn1";
    			this->restart_btn1->Size = System::Drawing::Size(75, 23);
    			this->restart_btn1->TabIndex = 7;
    			this->restart_btn1->Text = L"restart sec1";
    			this->restart_btn1->UseVisualStyleBackColor = true;
    			this->restart_btn1->Click += gcnew System::EventHandler(this, &Form1::restart_btn1_Click);
    			// 
    			// stop_btn1
    			// 
    			this->stop_btn1->Location = System::Drawing::Point(566, 195);
    			this->stop_btn1->Name = L"stop_btn1";
    			this->stop_btn1->Size = System::Drawing::Size(75, 23);
    			this->stop_btn1->TabIndex = 11;
    			this->stop_btn1->Text = L"stop sec1";
    			this->stop_btn1->UseVisualStyleBackColor = true;
    			this->stop_btn1->Click += gcnew System::EventHandler(this, &Form1::stop_btn1_Click);
    			// 
    			// stop_btn2
    			// 
    			this->stop_btn2->Location = System::Drawing::Point(566, 224);
    			this->stop_btn2->Name = L"stop_btn2";
    			this->stop_btn2->Size = System::Drawing::Size(75, 23);
    			this->stop_btn2->TabIndex = 12;
    			this->stop_btn2->Text = L"stop sec2";
    			this->stop_btn2->UseVisualStyleBackColor = true;
    			this->stop_btn2->Click += gcnew System::EventHandler(this, &Form1::stop_btn2_Click);
    			// 
    			// stop_btn3
    			// 
    			this->stop_btn3->Location = System::Drawing::Point(566, 253);
    			this->stop_btn3->Name = L"stop_btn3";
    			this->stop_btn3->Size = System::Drawing::Size(75, 23);
    			this->stop_btn3->TabIndex = 13;
    			this->stop_btn3->Text = L"stop sec3";
    			this->stop_btn3->UseVisualStyleBackColor = true;
    			this->stop_btn3->Click += gcnew System::EventHandler(this, &Form1::stop_btn3_Click);
    			// 
    			// stop_btn4
    			// 
    			this->stop_btn4->Location = System::Drawing::Point(566, 282);
    			this->stop_btn4->Name = L"stop_btn4";
    			this->stop_btn4->Size = System::Drawing::Size(75, 23);
    			this->stop_btn4->TabIndex = 14;
    			this->stop_btn4->Text = L"stop sec4";
    			this->stop_btn4->UseVisualStyleBackColor = true;
    			this->stop_btn4->Click += gcnew System::EventHandler(this, &Form1::stop_btn4_Click);
    			// 
    			// restart_btn4
    			// 
    			this->restart_btn4->Location = System::Drawing::Point(566, 131);
    			this->restart_btn4->Name = L"restart_btn4";
    			this->restart_btn4->Size = System::Drawing::Size(75, 23);
    			this->restart_btn4->TabIndex = 15;
    			this->restart_btn4->Text = L"restart sec4";
    			this->restart_btn4->UseVisualStyleBackColor = true;
    			this->restart_btn4->Click += gcnew System::EventHandler(this, &Form1::restart_btn4_Click);
    			// 
    			// restart_btn3
    			// 
    			this->restart_btn3->Location = System::Drawing::Point(566, 102);
    			this->restart_btn3->Name = L"restart_btn3";
    			this->restart_btn3->Size = System::Drawing::Size(75, 23);
    			this->restart_btn3->TabIndex = 16;
    			this->restart_btn3->Text = L"restart sec3";
    			this->restart_btn3->UseVisualStyleBackColor = true;
    			this->restart_btn3->Click += gcnew System::EventHandler(this, &Form1::restart_btn3_Click);
    			// 
    			// restart_btn2
    			// 
    			this->restart_btn2->Location = System::Drawing::Point(566, 71);
    			this->restart_btn2->Name = L"restart_btn2";
    			this->restart_btn2->Size = System::Drawing::Size(75, 23);
    			this->restart_btn2->TabIndex = 17;
    			this->restart_btn2->Text = L"restart sec2";
    			this->restart_btn2->UseVisualStyleBackColor = true;
    			this->restart_btn2->Click += gcnew System::EventHandler(this, &Form1::restart_btn2_Click);
    			// 
    			// Form1
    			// 
    			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
    			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
    			this->ClientSize = System::Drawing::Size(718, 501);
    			this->Controls->Add(this->restart_btn2);
    			this->Controls->Add(this->restart_btn3);
    			this->Controls->Add(this->restart_btn4);
    			this->Controls->Add(this->stop_btn4);
    			this->Controls->Add(this->stop_btn3);
    			this->Controls->Add(this->stop_btn2);
    			this->Controls->Add(this->stop_btn1);
    			this->Controls->Add(this->restart_btn1);
    			this->Controls->Add(this->listBox5);
    			this->Controls->Add(this->listBox4);
    			this->Controls->Add(this->listBox2);
    			this->Controls->Add(this->listBox1);
    			this->Controls->Add(this->FireOmpBtn);
    			this->Name = L"Form1";
    			this->Text = L"stop sec4";
    			this->ResumeLayout(false);
    
    		}
    #pragma endregion
    
    		//private: System::Void createIntData(int** intData)
    		//{
     	//		//Create the test data 
    		//	int n=DATASIZE;
    
    		//	intData=(int **)malloc(sizeof(int*)*n);
    
    		//	if(intData==NULL)
    		//	{
    		//		printf("failed to allocate array\n");
    		//		exit(1);
    		//	} 
    
    		//	int j;			
    		//	for(j=0;j<n;j++)
    		//	{
    		//		intData[j]=(int *)malloc(sizeof(int)*4);
    		//		intData[j][0]=4;
    		//		if (j%2==0)
    		//		{
    		//			intData[j][1]=-2;
    		//		}
    		//		else
    		//		{
    		//			intData[j][1]=2;
    		//		}
    		//		//initiate the result element to be 0
    		//		intData[j][2]=-9;
    		//		intData[j][3]=-9; 
    		//	}
    		//}	
    		private: System::Void FireOmpBtn_Click(System::Object^ sender, System::EventArgs^ e) 
    		{
    			ThreadStart^ myThreadDelegate = gcnew ThreadStart(this, &Form1::startOmpProcesses);
    			trd = gcnew Thread(myThreadDelegate);
    			trd->IsBackground = true;
    			trd->Start();
    		}
    	
    	 private: System::Void startOmpProcesses()
    	 {	
    			 //todo: put everything in class and intData is the member variable
    			int p_size = DATASIZE;
     			int i=0;
    
    			#pragma omp parallel num_threads(4) private(i) //shared(intData)
    			{
    				//PrintResult1("outside");
    
    				#pragma omp sections
    				{
    					// sec1: 
    					#pragma omp section
    					{
    						while(true)
    						{
    							if(isRestart1)
    							{
    								i=0;
    								isRestart1 = false; 
    							}
    							if(isStop1)
    							{
    								isStop1 = false; 
    								break; 
    							}
    							String^ strNumber1 = "Thread: " + Convert::ToString(omp_get_thread_num()) 
    								+ "; Sec1: " + Convert::ToString(i) + " Name: " + Thread::CurrentThread->Name;
    							mut->WaitOne();	
    								PrintResult1(strNumber1);
    								SetListbox1AutoScroll();						 
    							mut->ReleaseMutex();
    							i++;
    						}
    					}
    
    					// sec2:
    					#pragma omp section
    					{
    						while(true)
    						{
    							if(isRestart2)
    							{
    								i=0;
    								isRestart2 = false; 
    							}
    							if(isStop2)
    							{
    								isStop2 = false;
    								break; 
    							}
    							String^ strNumber2 = "Thread: " + Convert::ToString(omp_get_thread_num()) 
    								+ "; Sec2: " + Convert::ToString(i) + " Name: " + Thread::CurrentThread->Name;
    							mut->WaitOne();	
    								PrintResult2(strNumber2);
    								SetListbox2AutoScroll();						 
    							mut->ReleaseMutex();
    							i++;
    						}
    					}
    					
    					// sec3:
    					#pragma omp section
    					{
    						while(true)
    						{
    							if(isRestart3)
    							{
    								i=0;
    								isRestart3 = false; 
    							}
    							if(isStop3)
    							{
    								isStop3 = false;
    								break; 
    							}
    							String^ strNumber3 = "Thread: " + Convert::ToString(omp_get_thread_num()) 
    								+ "; Sec3: " + Convert::ToString(i) + " Name: " + Thread::CurrentThread->Name;
    							mut->WaitOne();	
    								PrintResult5(strNumber3);
    								SetListbox5AutoScroll();						 
    							mut->ReleaseMutex();
    							i++;
    						}
    					}
    
    					// sec4:
    					#pragma omp section
    					{
    						while(true)
    						{
    							if(isRestart4)
    							{
    								i=0;
    								isRestart4 = false; 
    							}
    							if(isStop4)
    							{
    								isStop4 = false;
    								break; 
    							}
    							String^ strNumber4 = "Thread: " + Convert::ToString(omp_get_thread_num()) 
    								+ "; Sec4: " + Convert::ToString(i) + " Name: " + Thread::;
    							mut->WaitOne();	
    								PrintResult4(strNumber4);
    								SetListbox4AutoScroll();						 
    							mut->ReleaseMutex();
    							i++;
    						}
    					}
    				}
    			}
    			//#pragma omp barrier
    
    			return; 
    	 }	
    
    		public: System::Void PrintResult1(String^ str)
    		{
    			Control::CheckForIllegalCrossThreadCalls = false;
    			this->listBox1->Items->Add(str);
    			Control::CheckForIllegalCrossThreadCalls = true;
    		}
    
       public: System::Void SetListbox1AutoScroll()
       {
         Control::CheckForIllegalCrossThreadCalls = false;
         this->listBox1->TopIndex = this->listBox1->Items->Count-1;
         Control::CheckForIllegalCrossThreadCalls = true;
       } 
    
    		public: System::Void PrintResult2(String^ str)
    		{
    			Control::CheckForIllegalCrossThreadCalls = false;
    			this->listBox2->Items->Add(str);
    			Control::CheckForIllegalCrossThreadCalls = true;
    		}
    
    	 public: System::Void SetListbox2AutoScroll()
    	 {
    		 Control::CheckForIllegalCrossThreadCalls = false;
    		 this->listBox2->TopIndex = this->listBox2->Items->Count-1;
    		 Control::CheckForIllegalCrossThreadCalls = true;
    	 } 
    
    		public: System::Void PrintResult4(String^ str)
    		{
    			Control::CheckForIllegalCrossThreadCalls = false;
    			this->listBox4->Items->Add(str);
    			Control::CheckForIllegalCrossThreadCalls = true;
    		}
    
    	 public: System::Void SetListbox4AutoScroll()
    	 {
    		 Control::CheckForIllegalCrossThreadCalls = false;
    		 this->listBox4->TopIndex = this->listBox4->Items->Count-1;
    		 Control::CheckForIllegalCrossThreadCalls = true;
    	 } 
    
    
    		public: System::Void PrintResult5(String^ str)
    		{
    			Control::CheckForIllegalCrossThreadCalls = false;
    			this->listBox5->Items->Add(str);
    			Control::CheckForIllegalCrossThreadCalls = true;
    		}
    
    	 public: System::Void SetListbox5AutoScroll()
    	 {
    		 Control::CheckForIllegalCrossThreadCalls = false;
    		 this->listBox5->TopIndex = this->listBox5->Items->Count-1;
    		 Control::CheckForIllegalCrossThreadCalls = true;
    	 } 
    
    
    		private: System::Void restart_btn1_Click(System::Object^ sender, System::EventArgs^ e) 
    		{
    			isRestart1 = true;
    		}		
    		private: System::Void stop_btn1_Click(System::Object^ sender, System::EventArgs^ e) 
    		{
    			isStop1 = true; 
    		}
    
    		private: System::Void restart_btn2_Click(System::Object^ sender, System::EventArgs^ e) 
    		{
    			isRestart2 = true;
    		}		
    		private: System::Void stop_btn2_Click(System::Object^ sender, System::EventArgs^ e) 
    		{
    			isStop2 = true; 
    		}
    
    		private: System::Void restart_btn3_Click(System::Object^ sender, System::EventArgs^ e) 
    		{
    			isRestart3 = true;
    		}		
    		private: System::Void stop_btn3_Click(System::Object^ sender, System::EventArgs^ e) 
    		{
    			isStop3 = true; 
    		}
    
    		private: System::Void restart_btn4_Click(System::Object^ sender, System::EventArgs^ e) 
    		{
    			isRestart4 = true;
    		}		
    		private: System::Void stop_btn4_Click(System::Object^ sender, System::EventArgs^ e) 
    		{
    			isStop4 = true; 
    		}
    
    };
    }
    
    

    Sunday, July 18, 2010 4:52 PM