Inheritance: IDisposable
Esempio n. 1
0
        private static void Run(int n, int threadCount)
        {
            Dispatcher d = new Dispatcher(threadCount, "ccr pool");
            DispatcherQueue dq = new DispatcherQueue("disp", d);
            Port<int> p1 = new Port<int>(),  p2 = new Port<int>();
            EventWaitHandle done1 = new AutoResetEvent(false), done2 = new AutoResetEvent(false);

            PingPonger pp1 = new PingPonger(done1, n, p2),
                pp2 = new PingPonger(done2, n, p1);

            Arbiter.Activate(dq, Arbiter.Interleave(
                new TeardownReceiverGroup(),
                new ExclusiveReceiverGroup(Arbiter.Receive(true, p1, pp1.Update)),
                new ConcurrentReceiverGroup()));

            Arbiter.Activate(dq, Arbiter.Interleave(
                new TeardownReceiverGroup(),
                new ExclusiveReceiverGroup(Arbiter.Receive(true, p2, pp2.Update)),
                new ConcurrentReceiverGroup()));

            p1.Post(0);

            EventWaitHandle.WaitAll(new[] { done1, done2 });

            dq.Dispose();
            d.Dispose();
        }
Esempio n. 2
0
        public void DispatchEventWithDispatcherQueueExceptionPort()
        {
            using (Dispatcher d = new Dispatcher ()) {
                var dq = new DispatcherQueue ("foo", d);
                var evt = new AutoResetEvent (false);
                var port = new Port<Exception> ();
                dq.UnhandledExceptionPort = port;

                int portPost = 0;
                int dispEx = 0;
                d.UnhandledException += delegate { ++dispEx; };

                var rec = Arbiter.Receive (true, port, (e) => { ++portPost; evt.Set(); });
                rec.TaskQueue = dq;
                rec.Execute ();

                dq.Enqueue (Arbiter.FromHandler (() => { throw new Exception (); }));
                Assert.IsTrue (evt.WaitOne (2000), "#1");
                Assert.AreEqual (1, portPost, "#2");
                Assert.AreEqual (0, dispEx, "#3");

                dq.Enqueue (Arbiter.FromHandler (() => { throw new Exception (); }));
                Assert.IsTrue (evt.WaitOne (2000), "#4");
                Assert.AreEqual (2, portPost, "#5");
                Assert.AreEqual (0, dispEx, "#6");
            }
        }
Esempio n. 3
0
        public DispatcherQueue(string name, Dispatcher dispatcher, TaskExecutionPolicy policy, int maximumQueueDepth)
            : this(name, dispatcher, policy)
        {
            if (policy == TaskExecutionPolicy.ConstrainSchedulingRateDiscardTasks || policy == TaskExecutionPolicy.ConstrainSchedulingRateThrottleExecution)
                throw new ArgumentException ("schedulingRate");
            if (maximumQueueDepth < 1 && policy != TaskExecutionPolicy.Unconstrained)
                throw new ArgumentException ("maximumQueueDepth");

            MaximumQueueDepth = maximumQueueDepth;
        }
 public ThrottledQueue(string threadPoolName, string dispatcherQueueName, 
     PostMessageDelegate postMessage, int maxPoolItemReuse)
 {
     matchMaker = new MatchMaker(maxPoolItemReuse);
     dispatcher = new Dispatcher(1, threadPoolName);
     dispatcherQueue = new DispatcherQueue(dispatcherQueueName, dispatcher);
     messagePort = new Port<QueueItem>();
     Handler<QueueItem> handler = new Handler<QueueItem>(postMessage);
     Arbiter.Activate(dispatcherQueue, Arbiter.Receive(true, messagePort, handler));
 }
Esempio n. 5
0
        static void ParallelIntegration(dynamic step, dynamic Low, dynamic Up, ScriptScope scope)
        {
            // создание массива объектов для хранения параметров
            InputData[] ClArr = new InputData[nc];
            for (int i = 0; i < nc; i++)
                ClArr[i] = new InputData();

            //Далее, задаются исходные данные для каждого экземпляра
            //вычислительного метода:
            // заполняем массив параметров
            dynamic Low_temp = Low;
            for (int i = 0; i < nc; i++)
            {
                ClArr[i].start = Low_temp;
                if (i + 1 == nc)
                    ClArr[i].stop = Up;
                else
                    ClArr[i].stop = Low_temp + step;

                Low_temp = Low_temp + step;
            }
            //Создаётся диспетчер с пулом из двух потоков:
            Dispatcher d = new Dispatcher(nc, "Test Pool");
            DispatcherQueue dq = new DispatcherQueue("Test Queue", d);
            //Описывается порт, в который каждый экземпляр метода Int()
            //отправляет сообщение после завершения вычислений:
            Port<int> p = new Port<int>();
            //Метод Arbiter.Activate помещает в очередь диспетчера две задачи(два
            //экземпляра метода Mul):

            System.Diagnostics.Stopwatch ssWatch = new System.Diagnostics.Stopwatch();
            ssWatch.Start();

            for (int i = 0; i < nc; i++)
                Arbiter.Activate(dq, new Task<InputData, Port<int>, ScriptScope>(ClArr[i], p, scope, Int));
            //Первый параметр метода Arbiter.Activate – очередь диспетчера,
            //который будет управлять выполнением задачи, второй параметр –
            //запускаемая задача.
            //С помощью метода Arbiter.MultipleItemReceive запускается задача
            //(приёмник), которая обрабатывает получение двух сообщений портом p:
            Arbiter.Activate(dq, Arbiter.MultipleItemReceive(true, p, nc, delegate (int[] array)
            {
                Console.WriteLine("Вычисления завершены");
                ssWatch.Stop();
                Console.WriteLine("Полное время работы {0} мс", ssWatch.ElapsedMilliseconds.ToString());
                Console.ReadKey(true);
                Environment.Exit(0);
            }));
            

            
            Console.ReadKey(true);
            Environment.Exit(0);
        }
Esempio n. 6
0
        /// <summary>
        /// Initializes new instance with specified path used for loading processing nodes, and number of worker threads
        /// </summary>
        /// <param name="path"></param>
        public AlgoGrid(string path, int workerThreadCount)
        {
            m_dispatcher = new Dispatcher(workerThreadCount, "ThreadPool");
            m_dispatcherQueue = new DispatcherQueue("Queue", m_dispatcher);
            m_queue = new Port<IBaseItem>();
            ITask task = Arbiter.Receive(true, m_queue, InnerHandle);
            Arbiter.Activate(m_dispatcherQueue, task);

            DirectoryCatalog catalog = new DirectoryCatalog(path);
            CompositionContainer container = new CompositionContainer(catalog);
            container.SatisfyImportsOnce(this);
        }
Esempio n. 7
0
            static void Main(string[] args)
        {
            int i;
            nc = 4;// количество ядер
            n = 100000000;

            Arr = new int[n];
            int Result = 0;

            Random r = new Random();
            for (int j = 0; j < n; j++)
                Arr[j] = r.Next(5);

            System.Diagnostics.Stopwatch sWatch = new System.Diagnostics.Stopwatch();
            sWatch.Start();
            for (i = 9; i < n; i++)//последовательный алгоритм
            {
                Result = Result + Arr[i];
            }

            Console.WriteLine("Сумма:");
            Console.WriteLine(Result);

            sWatch.Stop();
         
            Console.WriteLine("Последовательный алгоритм = {0} мс.", sWatch.ElapsedMilliseconds.ToString());


            InputData[] ClArr = new InputData[nc];// создание массива объектов для хранения параметров 
            for (i = 0; i < nc; i++)
                ClArr[i] = new InputData();

            int step = (Int32)(n / nc); // делим количество элементов  в массиве на nc частей 
            
            int c = -1;
            for (i = 0; i < nc; i++)// заполняем массив параметров 
            {
                ClArr[i].start = c + 1;
                ClArr[i].stop = c + step;
                ClArr[i].i = i;
                c = c + step;
            }
            Dispatcher d = new Dispatcher(nc, "Test Pool");
            DispatcherQueue dq = new DispatcherQueue("Test Queue", d);
            Port<int> p = new Port<int>();

            for (i = 0; i < nc; i++)
                Arbiter.Activate(dq, new Task<InputData, Port<int>>(ClArr[i], p, Sum));

            Arbiter.Activate(dq, Arbiter.MultipleItemReceive(true, p, nc, delegate(int[] array)
     {   }));}}}
Esempio n. 8
0
        //Метод параллельной сортировки массива
        static void parral_qsort(int[] a, int left, int right)
        {
            //Создаём диспетчеры
            Dispatcher d = new Dispatcher(2, " Test Pool");
            DispatcherQueue dq = new DispatcherQueue(" Test Queue", d);

            //Описываем порт
            Port<int> p = new Port<int>();

            //Первый этап параллельной сортировки
            //Параллельно сортируются две половины массива
            InputData data = new InputData();
            data.str = "Первая половина массива ";
            data.array = a; data.start = 0; data.stop = right/2 + 1;
            Arbiter.Activate(dq, new Task<InputData, Port<int>>(data, p, thread_fun));
            Console.WriteLine("Первый этап");
            Console.ReadKey();
            data.str = "Вторая половина массива ";
            data.array = a; data.start = right/2 + 1; data.stop = right;
            Arbiter.Activate(dq, new Task<InputData, Port<int>>(data, p, thread_fun));
            Console.ReadKey();

            //Второй этап параллельной сортировки
            //Сортируется центральная часть массива
            Console.WriteLine();
            Console.WriteLine("Второй этап");
            Stopwatch sWatch = new Stopwatch();
            sWatch.Start();
            qsort(a,right/4,right/4+right/2 +1);
            sWatch.Stop();
            Console.WriteLine("Средняя часть массива   Время работы: " + sWatch.ElapsedMilliseconds);
            Console.ReadKey();

            //Третий этап параллельной сортировки
            //Параллельно сортируются две половины массива
            data.str = "Первая половина массива ";
            data.array = a; data.start = 0; data.stop = right / 2 + 1;
            Arbiter.Activate(dq, new Task<InputData, Port<int>>(data, p, thread_fun));
            Console.WriteLine("Первый этап");
            Console.ReadKey();
            data.str = "Вторая половина массива ";
            data.array = a; data.start = right / 2 + 1; data.stop = right;
            Arbiter.Activate(dq, new Task<InputData, Port<int>>(data, p, thread_fun));
            Console.ReadKey();
            return;
        }
Esempio n. 9
0
        static void ParallelShellSort()
        {
            Dispatcher dispatcher = new Dispatcher(arraySize, "THREAD_POOL");
            DispatcherQueue dsipQueue = new DispatcherQueue("Dispatcher", dispatcher);

            Port<int> port = new Port<int>();

            for (int i = 0; i < arraySize; ++i)
            {
                Data d = new Data();
                d.row = i;
                Arbiter.Activate(dsipQueue, new Task<Data, Port<int>>(d, port, Task));

            }

            return;
        }
Esempio n. 10
0
		internal MessageTracer(short maxTypeId, TraceSettings traceSettings)
		{
			//dispatchers and dispatcher queues need unique names. it's unlikely that a single class could want more than one of these.
			//blatantly stolen from mr custenborders logwrapper class
			string name = ExtractClassName(new StackFrame(1).GetMethod()); 

			_sampleTimer = new Timer(SampleTimerCallback);

			ReloadConfig(maxTypeId, traceSettings);

			_traceDispatcher = new Dispatcher(1, ThreadPriority.BelowNormal, true, name + "TraceDispatcher"); //using only 1 thread keeps us from needing any locking on the stream output

			_traceMessageQueue = new DispatcherQueue(name + "DispatcherQueue", _traceDispatcher, 
				TaskExecutionPolicy.ConstrainQueueDepthDiscardTasks, 10000);

			Arbiter.Activate(_traceMessageQueue,
								Arbiter.Receive<RelayMessage>(true, _tracePort, DoWriteTraceInfo));
			
			Arbiter.Activate(_traceMessageQueue,
						Arbiter.Receive<IList<RelayMessage>>(true, _traceListPort, DoWriteTraceInfo));

			Arbiter.Activate(_traceMessageQueue,
				Arbiter.Receive<string>(true, _stringPort, DoWriteTraceInfo));
		}
Esempio n. 11
0
		/// <summary>
		/// Initializes the <see cref="RelayNode"/> with the given <see cref="ComponentRunState"/>s,
		/// must be called before calling <see cref="Start"/>
		/// </summary>
		/// <param name="componentRunStates"></param>
		public void Initialize(ComponentRunState[] componentRunStates)
		{
			try
			{
                if (log.IsInfoEnabled)
                {
                    if (componentRunStates == null)
                    {
                        log.Info("Initializing Relay Node.");
                    }
                    else
                    {
                        log.Info("Initialzing Relay Node with Component Run States.");
                    }
                }

				EnvironmentManager.EnvironmentChanged += EnvironmentChangedHandler;

                GetConfig();
				
				if (configuration == null) throw new ConfigurationErrorsException("config failed to load, is null");

				SetClusterAddresses(configuration);

				fatalFailureTimeout = configuration.FatalShutdownTimeout < 0
					? TimeSpan.FromMinutes(5)
					: TimeSpan.FromSeconds(configuration.FatalShutdownTimeout);

				components = new RelayComponents(configuration);

				if (configuration != null)
				{
					messageTracer = new MessageTracer(configuration.TypeSettings.MaxTypeId, configuration.TraceSettings);
					messageTracer.Activated = configuration.OutputTraceInfo;

					const string inThreadsName = "DataRelayNode";
					if (configuration.NumberOfThreads > 0)
					{
						inDispatcher = new Dispatcher(configuration.NumberOfThreads, ThreadPriority.Normal, true, inThreadsName);
					}
					else
					{
						inDispatcher = new Dispatcher() { Name = inThreadsName } ;
					}

					const string outThreadsName = "DataRelayNodeOUT";
					if (configuration.OutMessagesOnRelayThreads)
					{
						if (configuration.NumberOfOutMessageThreads > 0)
						{
							outDispatcher = new Dispatcher(configuration.NumberOfOutMessageThreads, ThreadPriority.Normal, true, outThreadsName);
						}
						else
						{
							outDispatcher = new Dispatcher { Name = outThreadsName };
						}

						outMessagePort = new Port<RelayMessageAsyncResult>();
						outMessagesPort = new Port<RelayMessageListAsyncResult>();

						outMessageQueue = new DispatcherQueue("DataRelayDispatcherQueueOUT", outDispatcher, TaskExecutionPolicy.ConstrainQueueDepthThrottleExecution, configuration.MaximumOutMessageQueueDepth);
						Arbiter.Activate(outMessageQueue,
								Arbiter.ReceiveWithIterator(true, outMessagePort, HandleOutMessage));
						Arbiter.Activate(outMessageQueue,
								Arbiter.ReceiveWithIterator(true, outMessagesPort, HandleOutMessages));
					}

					inMessageQueue = new DispatcherQueue("DataRelayDispatcherQueue", inDispatcher, TaskExecutionPolicy.ConstrainQueueDepthThrottleExecution, configuration.MaximumMessageQueueDepth);
					
					queuedTaskThreshold = (int)Math.Floor(0.9 * configuration.MaximumMessageQueueDepth);
					
					
					// setup RelayServicesClient before initalizing components
					RelayServicesClient.Instance.RelayNodeServices = this;
					
					Arbiter.Activate(inMessageQueue,
						Arbiter.Receive<RelayMessage>(true, inMessagePort, HandleInMessage));
					Arbiter.Activate(inMessageQueue,
								Arbiter.Receive<RelayMessageWithContext>(true, inMessageWithContextPort, HandleInMessage));
					Arbiter.Activate(inMessageQueue,
								Arbiter.Receive<IList<RelayMessage>>(true, inMessagesPort, HandleInMessages));
					

					//by having after the Arbiter.Activate it allows Initialize components to use 
					//IRelayNodeServices that require Message handling
					components.Initialize(componentRunStates, configuration.IgnoredMessageTypes);

					queuedMessageCounterTimer = new Timer(CountQueuedMessages, null, 5000, 5000);
				}
			}
			catch (Exception ex)
			{
                if (log.IsErrorEnabled)
                    log.ErrorFormat("Exception initializing relay node: {0}", ex);
				throw; //should bring server down
			}
		}
Esempio n. 12
0
		internal void ReloadConfig(RelayNodeConfig newConfiguration)
		{
			if (newConfiguration != null)
			{
                if (log.IsInfoEnabled)
                    log.Info("Reloading configs.");

				fatalFailureTimeout = newConfiguration.FatalShutdownTimeout < 0
					? TimeSpan.FromMinutes(5)
					: TimeSpan.FromSeconds(newConfiguration.FatalShutdownTimeout);

				if (newConfiguration.GetMyNode() != null)
				{
					MyZone = newConfiguration.GetMyNode().Zone;
				}

				SetClusterAddresses(newConfiguration);
				
				messageTracer.ReloadConfig(newConfiguration.TypeSettings.MaxTypeId, newConfiguration.TraceSettings);
				messageTracer.Activated = newConfiguration.OutputTraceInfo;

				//TODO: handle changes in component definition
				components.ReloadConfig(newConfiguration, newConfiguration.IgnoredMessageTypes);

				if (newConfiguration.TransportSettings != null)  
				{
                    if(newConfiguration.TransportSettings.ListenPort != portNumber)
                    {
                    	log.InfoFormat("Changing Socket Transport Port to {0}",
                    		               newConfiguration.TransportSettings.ListenPort);
                    	portNumber = newConfiguration.TransportSettings.ListenPort;
                    	SocketServerAdapter.ChangePort(portNumber);
                    }
					if(newConfiguration.TransportSettings.HttpListenPort != httpPortNumber)
					{
						if (httpPortNumber < 1 && newConfiguration.TransportSettings.HttpListenPort > 0) //there was no http server and now we want one
						{
							httpPortNumber = newConfiguration.TransportSettings.HttpListenPort;
							StartHttpServer();

						}
						else if (newConfiguration.TransportSettings.HttpListenPort < 1 && httpPortNumber > 0) //shut off a running server
						{
							httpPortNumber = newConfiguration.TransportSettings.HttpListenPort;
							StopHttpServer();
						}
						else //just change the port on an existing server
						{
							log.InfoFormat("Changing Http Transport Port to {0}",
											   newConfiguration.TransportSettings.HttpListenPort);
							httpPortNumber = newConfiguration.TransportSettings.HttpListenPort;
							_httpServer.ChangePort(httpPortNumber);	
						}
						
					}
				}

				if (newConfiguration.NumberOfThreads != configuration.NumberOfThreads)
				{
					if(log.IsInfoEnabled)
                        log.InfoFormat("Changing number of relay node threads from {0} to {1}", 
                            configuration.NumberOfThreads, newConfiguration.NumberOfThreads);
					try
					{
						Dispatcher oldInDispatcher = inDispatcher;
						Dispatcher newInDispatcher;
						const string inThreadsName = "DataRelayNode";
						if (newConfiguration.NumberOfThreads > 0)
						{
							newInDispatcher = new Dispatcher(newConfiguration.NumberOfThreads, ThreadPriority.Normal, true, inThreadsName);
						}
						else
						{
							newInDispatcher = new Dispatcher() { Name = inThreadsName };
						}

						DispatcherQueue newInQueue = new DispatcherQueue("DataRelayDispatcherQueue", newInDispatcher, TaskExecutionPolicy.ConstrainQueueDepthThrottleExecution, newConfiguration.MaximumMessageQueueDepth);

						Interlocked.Exchange(ref inMessagePort, new Port<RelayMessage>());
						Interlocked.Exchange(ref inMessageWithContextPort, new Port<RelayMessageWithContext>());
						Interlocked.Exchange(ref inMessagesPort, new Port<IList<RelayMessage>>());

						Arbiter.Activate(newInQueue,
							 Arbiter.Receive<RelayMessage>(true, inMessagePort, HandleInMessage));
						Arbiter.Activate(newInQueue,
							 Arbiter.Receive<RelayMessageWithContext>(true, inMessageWithContextPort, HandleInMessage));
						Arbiter.Activate(newInQueue,
							 Arbiter.Receive<IList<RelayMessage>>(true, inMessagesPort, HandleInMessages));

						inMessageQueue = newInQueue;
						inDispatcher = newInDispatcher;
						oldInDispatcher.Dispose();
					}
					catch (Exception e)
					{
                        if (log.IsErrorEnabled)
                            log.ErrorFormat("Error changing number of relay node threads: {0}", e);
					}
				}
				else
				{
					//not rebuilding the queue, but reset its max queue depth anyway
					inMessageQueue.MaximumQueueDepth = newConfiguration.MaximumMessageQueueDepth;
				}

				SetupOutMessagesOnRelayThreads(newConfiguration);

				queuedTaskThreshold = (int)Math.Floor(0.9 * newConfiguration.MaximumMessageQueueDepth);
				configuration = newConfiguration;
                if (log.IsInfoEnabled)
                    log.Info("Done Reloading configs.");
			}
			else
			{
                if (log.IsErrorEnabled)
                    log.Error("Attempt to reload null config");
			}
		}
Esempio n. 13
0
		private void SetupOutMessagesOnRelayThreads(RelayNodeConfig newConfiguration) 
		{
			//if it was off and is now on, or if it was on and the number of threads changed
			bool setupNewOutMessages = (newConfiguration.OutMessagesOnRelayThreads && configuration.OutMessagesOnRelayThreads == false)
			                           || (configuration.OutMessagesOnRelayThreads && newConfiguration.OutMessagesOnRelayThreads
			                               && newConfiguration.NumberOfOutMessageThreads != configuration.NumberOfOutMessageThreads);

			Dispatcher oldOutDispatcher = outDispatcher;
			DispatcherQueue oldOutMessageQueue = outMessageQueue;

			if (setupNewOutMessages)
			{
				try
				{
					const string outThreadsName = "DataRelayNodeOUT";
						
					outMessagePort = new Port<RelayMessageAsyncResult>(); //atomic
					outMessagesPort = new Port<RelayMessageListAsyncResult>(); //atomic

					if (newConfiguration.NumberOfOutMessageThreads > 0)
					{
						outDispatcher = new Dispatcher(newConfiguration.NumberOfOutMessageThreads, ThreadPriority.Normal, true, outThreadsName);
					}
					else
					{
						outDispatcher = new Dispatcher { Name = outThreadsName };
					}

					outMessageQueue = new DispatcherQueue("DataRelayDispatcherQueueOUT", outDispatcher, TaskExecutionPolicy.ConstrainQueueDepthThrottleExecution, newConfiguration.MaximumOutMessageQueueDepth);

					Arbiter.Activate(outMessageQueue,
									 Arbiter.ReceiveWithIterator(true, outMessagePort, HandleOutMessage));
					Arbiter.Activate(outMessageQueue,
									 Arbiter.ReceiveWithIterator(true, outMessagesPort, HandleOutMessages));
						
				}
				catch (Exception e)
				{	
                    if(log.IsErrorEnabled)
                        log.ErrorFormat("Error setting up Out Message Threads on RelayNode: {0}", e);                    
					throw;
				}
			}

			if (newConfiguration.OutMessagesOnRelayThreads == false)
			{
				outMessagePort = null;
				outMessagesPort = null;
				if (oldOutDispatcher != null) oldOutDispatcher.Dispose();
				if (oldOutMessageQueue != null) oldOutMessageQueue.Dispose();
			}
		}
Esempio n. 14
0
            static void Main(string[] args)
        {
            int i;
            nc = 2;
            n = 100000000;
            a = new int[n];
            b = new int[nc];

            Random r = new Random();
            for (int j = 0; j < n; j++)
                a[j] = r.Next(100);
            System.Diagnostics.Stopwatch sWatch = new System.Diagnostics.Stopwatch();
            sWatch.Start();
            for (i = 0; i <= n; i++)
            {
                for (int j = i; i <= n; i++)
                {
                    if (a[j] > a[j + 1])
                    {
                        int e = a[j]; //change for elements
                        a[j] = a[j + 1];
                        a[j + 1] = e;
                    }
                }
            }
            sWatch.Stop();
            Console.WriteLine("Последовательный алгоритм = {0} мс.", sWatch.ElapsedMilliseconds.ToString());
            
            // создание массива объектов для хранения параметров 
            InputData[] ClArr = new InputData[nc];
            for (i = 0; i < nc; i++)
                ClArr[i] = new InputData();
            // делим количество элементов  в массиве на nc частей 
            int step = (Int32)(n / nc);
            // заполняем массив параметров 
            int c = -1;
            for (i = 0; i < nc; i++)
            {
                ClArr[i].start = c + 1;
                ClArr[i].stop = c + step;
                ClArr[i].i = i;
                c = c + step;
            }
            Dispatcher d = new Dispatcher(nc, "Test Pool");
            DispatcherQueue dq = new DispatcherQueue("Test Queue", d);
            Port<int> p = new Port<int>();                
            for (i = 0; i < nc; i++)
                Arbiter.Activate(dq, new Task<InputData, Port<int>>(ClArr[i], p, Mul));

            Arbiter.Activate(dq, Arbiter.MultipleItemReceive(true, p, nc, delegate(int[] array)
     {   }));
        }
Esempio n. 15
0
        public void PortSetReceiveToBeUsedWithIterators()
        {
            iterPort = new PortSet (typeof (string), typeof (char), typeof (int));
            iterEvent = new AutoResetEvent (false);
            iterRes = 0;

            using (Dispatcher d = new Dispatcher ()) {
                var disp = new DispatcherQueue ("bla", d);
                disp.Enqueue (new IterativeTask (this.SimpleTaskIterator));
                for (int i = 0; i < 5; ++i)
                    iterPort.PostUnknownType ((i + 1) * 10);
                Assert.IsTrue (iterEvent.WaitOne (2000), "#1");
                Assert.AreEqual (150, iterRes, "#2");
            }
        }
Esempio n. 16
0
		private void InitializeInstance(RelayNodeConfig config, ForwardingConfig forwardingConfig, Dictionary<string, Dictionary<string, MessageQueue>> errorQueues)
		{
			Counters = new ForwardingCounters();

			if (config != null)
			{
				//without this we'll get a null ref exception
				if (forwardingConfig == null) throw new ArgumentNullException("forwardingConfig", "Requires a forwardingConfig to initialize");
				  
				Config = config;
				ForwardingConfig = forwardingConfig;
				_zoneDefinitions = config.RelayNodeMapping.ZoneDefinitions;		
				Counters.Initialize(Config.InstanceName);

				if (InMessageDispatcher != null)
				{
					if (_log.IsInfoEnabled)
						_log.Info("Relay Forwarder Node Manager Initialized with non-null Dispatcher.");                    
				}
				else
				{
					InMessageDispatcher = new Dispatcher(forwardingConfig.NumberOfThreads, ThreadPriority.Normal, true, "Relay Forwarder");
				}

				if (OutMessageDispatcher == null)
				{
					OutMessageDispatcher = new Dispatcher(forwardingConfig.NumberOfOutMessageThreads, ThreadPriority.Normal, true, "Relay Forwader Out Messages");
				}
				
				NodeGroup.MaximumQueuedItems = forwardingConfig.MaximumTaskQueueDepth;

				BuildNodeGroups(config, errorQueues);

				if (config.MyAddresses != null && config.MyAddresses.Count > 0)
				{	
					for (int i = 0; i < config.MyAddresses.Count; i++)
					{
						AddressFamily family = config.MyAddresses[i].AddressFamily;
						if (family == AddressFamily.InterNetwork &&
							!IPAddress.Loopback.Equals(config.MyAddresses[i])
							)
						{
							MyIpAddress = config.MyAddresses[i];
							break;
						}
					}
				}

				_queuedMessageCounterTimer = new Timer(CountQueuedMessages, null, 5000, 5000);

				_aggregateCounterTickTimer = new Timer(AggregateCounterTicker, null, 500, 500);
				_myNodeDefinition = GetMyNodeDefinition();
			}
		}
Esempio n. 17
0
        static void Main(string[] args)
        {
            int i;
            nc = 2;
            n = 50000;

            Console.WriteLine("\nМассив включает в себя {0} элементов\n", n);

            a = new int[n];
            mem = new int[n];
            b = new int[nc];

            Random r = new Random();
            for (int j = 0; j < n; j++)
                a[j] = r.Next(10000);
            a.CopyTo(mem,0); //запомнили полученный массив
            Console.WriteLine("Исходный массив успешно заполнен случайными значениями!\n");

            arrDisplay();

            System.Diagnostics.Stopwatch sWatch = new System.Diagnostics.Stopwatch();
            Console.WriteLine("Начата последовательная сортировка массива...\n");
            sWatch.Start();
            for (i = 0; i <= n - 1; i++)
            {
                for (int j = i + 1; j < n; j++)
                {
                    if (a[j] < a[i])
                    {
                        var temp = a[i];
                        a[i] = a[j];
                        a[j] = temp;
                    }
                }
            }
            sWatch.Stop();
            Console.WriteLine("Массив отсортирован последовательным алгоритмом!\n");
            Console.WriteLine("Последовательный алгоритм = {0} мс.", sWatch.ElapsedMilliseconds.ToString());

            arrDisplay(); //показали отсортированный массив

            mem.CopyTo(a, 0); ; // восстановили массив со случайными числами 

            // создание массива объектов для хранения параметров 
            InputData[] tempArray = new InputData[nc];
            i = 0;
            while (i < nc)
                {tempArray[i] = new InputData(); i++;}
            // делим количество элементов  в массиве на nc частей 
            int step = (Int32)(n / nc);
            // заполняем массив параметров 
            int c = -1;
            for (i = 0; i < nc; i++)
            {
                tempArray[i].start = c + 1;
                tempArray[i].stop = c + step;
                c = c + step;
            }
            Dispatcher d = new Dispatcher(nc, "Test Pool");
            DispatcherQueue dq = new DispatcherQueue("Test Queue", d);
            Port<int> p = new Port<int>();


            for (i = 0; i < nc; i++)
                Arbiter.Activate(dq, new Task<InputData, Port<int>>(tempArray[i], p, Mul));

            Console.WriteLine("Начата параллельная сортировка массива...\n");
            Arbiter.Activate(dq, Arbiter.MultipleItemReceive(true, p, nc, delegate(int[] array)
     {
         Console.WriteLine("Массив отсортирован параллельным алгоритмом!\n");
         System.Diagnostics.Stopwatch newWatch = new System.Diagnostics.Stopwatch();
         Console.WriteLine("Начата последовательная сортировка массива...\n");
         newWatch.Start();
         Array.Sort(a); //тот самый делегат в приемнике, с помощью которого шлейфуется результат параллельной сортировки пузырьком
         newWatch.Stop();
         Console.WriteLine("Окончательная сортировка средтвами C#: {0} мс.\n", newWatch.ElapsedMilliseconds.ToString());
         arrDisplay();
         Console.WriteLine("Вычисления завершены");
         Console.ReadKey(true);
         Environment.Exit(0);
     }));

        }
Esempio n. 18
0
        public DispatcherQueue(string name, Dispatcher dispatcher, TaskExecutionPolicy policy, double schedulingRate)
            : this(name, dispatcher, policy)
        {
            if (policy == TaskExecutionPolicy.ConstrainQueueDepthDiscardTasks || policy == TaskExecutionPolicy.ConstrainQueueDepthThrottleExecution)
                throw new ArgumentException ("maximumQueueDepth");
            if (schedulingRate < 1 && policy != TaskExecutionPolicy.Unconstrained)
                throw new ArgumentException ("schedulingRate");

            MaximumSchedulingRate = schedulingRate;
            watch = Stopwatch.StartNew ();
        }
Esempio n. 19
0
        static void Main(string[] args)
        {
            int min, max;
            countTask = 2;
            size = 1000000;

            arr = new int[size];
            task = new int[countTask];

            Random r = new Random();
            for (int j = 0; j < size; j++) arr[j] = r.Next(100000);
            System.Diagnostics.Stopwatch sWatch = new System.Diagnostics.Stopwatch();
            sWatch.Start();
            min = arr[0];
            for (int i = 1; i < size; i++)
            {
                if (arr[i] < min)
                    min = arr[i];
            }
            max = arr[0];
            for (int i = 1; i < size; i++)
            {
                if (arr[i] > max)
                    max = arr[i];
            }

            Console.WriteLine(min);
            Console.WriteLine(max);

            sWatch.Stop();

            Console.WriteLine("Time of Search = {0} ms.", sWatch.ElapsedMilliseconds.ToString());
            Data[] TaskArr = new Data[countTask];
            for (int i = 0; i < countTask; i++) TaskArr[i] = new Data();
            int step = (Int32)(size / countTask);
            int count = -1;
            for (int i = 0; i < countTask; i++)
            {
                TaskArr[i].start = count + 1;
                TaskArr[i].stop = count + step;
                TaskArr[i].i = i;
                count = count + step;
            }
            Dispatcher disp = new Dispatcher(countTask, "Test Pool");
            DispatcherQueue dispQueue = new DispatcherQueue("Test Queue", disp);
            Port<int> p = new Port<int>();


            for (int i = 0; i < countTask; i++) Arbiter.Activate(dispQueue, new Task<Data, Port<int>>(TaskArr[i], p, ParSearch));
            Arbiter.Activate(dispQueue, Arbiter.MultipleItemReceive(true, p, countTask, delegate(int[] array){ }));
        }
Esempio n. 20
0
		private static bool TryGetExistingQueue(Dispatcher dispatcher, string queueName, out DispatcherQueue existingQueue)
		{
			List<DispatcherQueue> queues = dispatcher.DispatcherQueues;
			foreach (DispatcherQueue queue in queues)
			{
				if (queue.Name == queueName)
				{
					existingQueue = queue;
					return true;
				}
			}
			existingQueue = null;
			return false;
		}
Esempio n. 21
0
		internal void ReloadConfig(RelayNodeConfig config, ForwardingConfig newForwardingConfig)
		{

			if (config.RelayNodeMapping == null)
			{
				if (_log.IsErrorEnabled)
					_log.Error("Got new config with no defined groups.");
			}
			else
			{
				if (config.RelayNodeMapping.Validate())
				{
					_zoneDefinitions = config.RelayNodeMapping.ZoneDefinitions;	//make sure this is set before reloading the mapping so any changes propogate	
					Dictionary<string, Dictionary<string, MessageQueue>> errorQueues = GetErrorQueues();
					NodeGroups.ReloadMapping(config, newForwardingConfig);
					//note that if a node changes groups, the error queue won't make it!
					NodeGroups.PopulateQueues(errorQueues, false);
				}
				else
				{
					if (_log.IsErrorEnabled)
						_log.Error("Forwarder not reloading invalid config.");
				}
			}

			Config = config;

			_myNodeDefinition = GetMyNodeDefinition();
			bool doNewInDispatcher, doNewOutDispatcher;
			if (newForwardingConfig.NumberOfThreads != ForwardingConfig.NumberOfThreads)
			{
				doNewInDispatcher = true;
			}
			else
			{
				doNewInDispatcher = false;
			}
			if (newForwardingConfig.NumberOfOutMessageThreads != ForwardingConfig.NumberOfOutMessageThreads)
			{
				doNewOutDispatcher = true;
			}
			else
			{
				doNewOutDispatcher = false;
			}


			if (doNewInDispatcher || doNewOutDispatcher)
			{
				Dispatcher oldInDispatcher = null, newInDispatcher, oldOutDispatcher = null, newOutDispatcher;

				if (doNewInDispatcher)
				{
					if (_log.IsInfoEnabled)
						_log.InfoFormat("Changing number of messaging threads from {0} to {1}", ForwardingConfig.NumberOfThreads, newForwardingConfig.NumberOfThreads);
					oldInDispatcher = InMessageDispatcher;
					newInDispatcher = new Dispatcher(newForwardingConfig.NumberOfThreads, ThreadPriority.Normal, true, "Relay Forwarder");

				}
				else
				{
					newInDispatcher = InMessageDispatcher;
				}
				if (doNewOutDispatcher)
				{
					if (_log.IsInfoEnabled)
						_log.InfoFormat("Changing number of out message threads from {0} to {1}", ForwardingConfig.NumberOfOutMessageThreads, newForwardingConfig.NumberOfOutMessageThreads);
					oldOutDispatcher = OutMessageDispatcher;
					newOutDispatcher = new Dispatcher(newForwardingConfig.NumberOfOutMessageThreads, ThreadPriority.Normal, true, "Relay Forwarder");
				}
				else
				{
					newOutDispatcher = OutMessageDispatcher;
				}

				InMessageDispatcher = newInDispatcher;
				OutMessageDispatcher = newOutDispatcher;

				NodeGroups.SetNewDispatchers(newInDispatcher, newOutDispatcher);

				ForwardingConfig = newForwardingConfig;

				if (doNewInDispatcher)
				{
					if (_log.IsInfoEnabled)
						_log.Info("Disposing old in message Dispatcher");
					oldInDispatcher.Dispose();
				}
				if (doNewOutDispatcher)
				{
					if (_log.IsInfoEnabled)
						_log.Info("Disposing old out message Dispatcher");
					oldOutDispatcher.Dispose();
				}
			}
			else
			{
				ForwardingConfig = newForwardingConfig;
			}
		}
Esempio n. 22
0
        static void paral_qsort(int[] a, int left, int right)
        {
            int l = left;
            int r = right;
            int val = 0;
            int mid = a[(l + r) / 2];

            while (l <= r)
            {
                while ((a[l] < mid) && (l <= right))
                {
                    l++;
                }
                while ((a[r] > mid) && (r >= left))
                {
                    r--;
                }

                if (l <= r)
                {
                    val = a[l];
                    a[l] = a[r];
                    a[r] = val;
                    l++;
                    r--;
                }
            }

            InputData data1 = new InputData();
            InputData data2 = new InputData();

            if (r > left)
            {
                data1.str="Нить исполнения 1";
                data1.array = a; data1.start = left; data1.stop = r;
                //qsort(a, left, r);
            }
            if (l < right)
            {
                data2.str = "Нить исполнения 2";
                data2.array = a; data2.start = l; data2.stop = right;
                //qsort(a, l, right);
            }

            //Создаём диспетчеры
            Dispatcher d = new Dispatcher(2, " Test Pool");
            DispatcherQueue dq = new DispatcherQueue(" Test Queue", d);

            //Описываем порт
            Port<int> p = new Port<int>();

            Arbiter.Activate(dq, new Task<InputData, Port<int>>(data1, p, thread_fun));
            Arbiter.Activate(dq, new Task<InputData, Port<int>>(data2, p, thread_fun));

            return;
        }
Esempio n. 23
0
        static void Main(string[] args)
        {

            int[] A;//вектор а
            int[] B;//вектор b
            int[] C;//результат вычисления скалярного произведения векторов
            int m;
            int nc;

            nc = 2;//количество ядер
            m = 500000;//количество строк матрицы
            A = new int[m];
            B = new int[m];
            C = new int[m];
            Random r = new Random();
            for (int i = 0; i < m; i++)
            {
                A[i] = r.Next(100);

                B[i] = r.Next(100);
            }

            Stopwatch sWatch = new Stopwatch();//определение время выполнения вычислений для последовательного алгоритма
            sWatch.Start();
            for (int i = 0; i < m; i++)
            {
                C[i] = 0;
                C[i] += A[i] * B[i];
            }
            sWatch.Stop();
            Console.WriteLine("Последовательный алгоритм = {0} мс.",
            sWatch.ElapsedMilliseconds.ToString());
            //Console.ReadKey();


            // создание массива объектов для хранения параметров
            InputData[] ClArr = new InputData[nc];
            for (int i = 0; i < nc; i++)
                ClArr[i] = new InputData();

            // делим количество строк в матрице на nc частей 
            int step = (Int32)(m / nc);
            // заполняем массив параметров 
            int c = -1;
            for (int i = 0; i < nc; i++)
            {
                ClArr[i].start = c + 1;
                ClArr[i].stop = c + step;
                c = c + step;
            }

            Dispatcher d = new Dispatcher(nc, "Test Pool");//Создание диспетчера с пулом из двух потоков
            DispatcherQueue dq = new DispatcherQueue("Test Queue", d);
            Port<int> p = new Port<int>();

            for (int i = 0; i < nc; i++)
                Arbiter.Activate(dq, new Task<InputData, Port<int>>(ClArr[i], p, Mul));
            
            Arbiter.Activate(dq,Arbiter.MultipleItemReceive(true, p, nc, delegate(int[] array) //запуск задачи, обрабатывающей получение двух сообщений портом p
  {
      Console.WriteLine("Вычисления завершены");
  }));

        }
Esempio n. 24
0
        static void ParallelMul()
        {
            // создание массива объектов для хранения параметров
            InputData[] ClArr = new InputData[nc];
            for (int i = 0; i < nc; i++)
                ClArr[i] = new InputData();

            //Далее, задаются исходные данные для каждого экземпляра
            //вычислительного метода:
            // делим количество строк в матрице на nc частей
            int step = (Int32)(m / nc);
            // заполняем массив параметров
            int c = -1;
            for (int i = 0; i < nc; i++)
            {
                ClArr[i].start = c + 1;
                ClArr[i].stop = c + step;
                c = c + step;
            }
            //Создаётся диспетчер с пулом из двух потоков:
            Dispatcher d = new Dispatcher(nc, "Test Pool");
            DispatcherQueue dq = new DispatcherQueue("Test Queue", d);
            //Описывается порт, в который каждый экземпляр метода Mul()
            //отправляет сообщение после завершения вычислений:
            Port<int> p = new Port<int>();
            //Метод Arbiter.Activate помещает в очередь диспетчера две задачи(два
            //экземпляра метода Mul):
            for (int i = 0; i < nc; i++)
                Arbiter.Activate(dq, new Task<InputData, Port<int>>(ClArr[i], p, Mul));
            //Первый параметр метода Arbiter.Activate – очередь диспетчера,
            //который будет управлять выполнением задачи, второй параметр –
            //запускаемая задача.

            //С помощью метода Arbiter.MultipleItemReceive запускается задача
            //(приёмник), которая обрабатывает получение двух сообщений портом p:
            Arbiter.Activate(dq, Arbiter.MultipleItemReceive(true, p, nc, delegate (int[] array)
            {
                dispResult();
                Console.WriteLine("Вычисления завершены");
                Console.ReadKey(true);
                Environment.Exit(0);
            }));
        }
Esempio n. 25
0
        static void ParallelMul()
        {

            InputData[] ClArr = new InputData[nc];
            for (int i = 0; i < nc; i++)
                ClArr[i] = new InputData();


            int step = (Int32)(m / nc);

            int c = -1;
            for (int i = 0; i < nc; i++)
            {
                ClArr[i].start = c + 1;
                ClArr[i].stop = c + step;
                c = c + step;
            }

            Dispatcher d = new Dispatcher(nc, "Test Pool");
            DispatcherQueue dq = new DispatcherQueue("Test Queue", d);

            Port<int> p = new Port<int>();

            for (int i = 0; i < nc; i++)
                Arbiter.Activate(dq, new Task<InputData, Port<int>>(ClArr[i], p, Mul));

            Arbiter.Activate(dq, Arbiter.MultipleItemReceive(true, p, nc, delegate (int[] array)
            {
  
                Console.WriteLine("Вычисления завершены");
                Console.ReadKey(true);
                Environment.Exit(0);
            }));
        }
Esempio n. 26
0
 internal CcrWorker(Dispatcher dispatcher, int currentQueue)
 {
     this.dispatcher = dispatcher;
     this.currentQueue = currentQueue;
 }
Esempio n. 27
0
        DispatcherQueue(string name, Dispatcher dispatcher, TaskExecutionPolicy policy)
        {
            if (name == null)
                throw new ArgumentNullException ("name");

            if (dispatcher == null)
                throw new ArgumentNullException ("dispatcher");

            this.dispatcher = dispatcher;

            Name = name;

            ThrottlingSleepInterval = new TimeSpan (0,0,0,0,10);
            Timescale = 1;

            Policy = policy;
            dispatcher.Register (this);
        }
Esempio n. 28
0
		internal static void GetMessageQueues(
			Dispatcher inMessageDispatcher, Dispatcher outMessageDispatcher, string queueName, int queueDepth, 
			out DispatcherQueue inMessageQueue, out DispatcherQueue outMessageQueue)
		{
			//do the in message queue
			if (!TryGetExistingQueue(inMessageDispatcher, queueName, out inMessageQueue))
			{ //queues can't be removed from a dispatcher once attached. To handle dynamic remapping, always look for an existing one first.
				Debug.WriteLine(String.Format("Creating In Message DispatcherQueue {0} with maximum depth {1}.", queueName, queueDepth));
				try
				{
					inMessageQueue = new DispatcherQueue(queueName, inMessageDispatcher,
						TaskExecutionPolicy.ConstrainQueueDepthThrottleExecution, queueDepth);
				}
				catch (Exception ex)
				{
					if(_log.IsErrorEnabled)
					_log.ErrorFormat("Exception creating In Message DispatcherQueue {0} with maximum depth {1}: {2}", queueName, queueDepth, ex);
					throw;
				}
			}
			else
			{
				Debug.WriteLine(String.Format("Setting existing DispatcherQueue {0} to maximum depth {1}.", queueName, queueDepth));                
				inMessageQueue.MaximumQueueDepth = queueDepth;
			}
			//aaaaand the out message queue
			if (!TryGetExistingQueue(outMessageDispatcher, queueName, out outMessageQueue))
			{ //queues can't be removed from a dispatcher once attached. To handle dynamic remapping, always look for an existing one first.
				Debug.WriteLine(String.Format("Creating Out Message DispatcherQueue {0} with maximum depth {1}.", queueName, queueDepth));
				try
				{
					outMessageQueue = new DispatcherQueue(queueName, outMessageDispatcher,
						TaskExecutionPolicy.ConstrainQueueDepthThrottleExecution, queueDepth);
				}
				catch (Exception ex)
				{
					if(_log.IsErrorEnabled)
						_log.ErrorFormat("Exception creating Out Message DispatcherQueue {0} with maximum depth {1}: {2}", queueName, queueDepth, ex);
					throw;
				}
			}
			else
			{
				Debug.WriteLine(String.Format("Setting existing Out Message DispatcherQueue {0} to maximum depth {1}.", queueName, queueDepth));                
				outMessageQueue.MaximumQueueDepth = queueDepth;
			}
		}
Esempio n. 29
0
		internal void SetNewDispatchers(Dispatcher newInDispatcher, Dispatcher newOutDispatcher)
		{
			for (int i = 0; i < Clusters.Count; i++)
			{
				Clusters[i].SetNewDispatchers(newInDispatcher, newOutDispatcher);
			}
		}
Esempio n. 30
0
 public DispatcherQueue(string name, Dispatcher dispatcher)
     : this(name, dispatcher, TaskExecutionPolicy.Unconstrained)
 {
     MaximumSchedulingRate = 1;
 }