source: proiecte/ParallelANN/BCMToolbox/Para.cs @ 171

Last change on this file since 171 was 171, checked in by (none), 14 years ago
File size: 3.9 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading;
6
7namespace BCMToolbox
8{
9    /*
10     * Connects Hebbian3 networks with message pipes and handles execution in parallel.
11     */
12    public abstract class ParallelHebbian3Executive : IDisposable
13    {
14        /// <summary>
15        /// Classes which inherit this build different network configurations.
16        /// </summary>       
17        protected abstract SynchronousHebbian3Network BuildNetwork();
18
19        int __processorCount = 0;
20        List<SynchronousHebbian3Network> __networks = new List<SynchronousHebbian3Network>();
21        List<Thread> __executionThreads = new List<Thread>();
22        bool __started = false;
23        ManualResetEvent __mainSync = new ManualResetEvent(false);
24        int __waiting = 0;
25        object __syncRoot = new object();
26
27        public ParallelHebbian3Executive(int newProcessorCount)
28        {
29            __processorCount = newProcessorCount;
30        }
31
32        public ParallelHebbian3Executive()
33            : this(Environment.ProcessorCount)
34        {
35        }
36
37        public void Initialize()
38        {
39            for (int i = 0; i < __processorCount; i++)           
40                __networks.Add(BuildNetwork());
41
42            // bind message-passing pipes
43            for(int i = 0; i < __networks.Count; i++)
44                for (int j = i + 1; j < __networks.Count; j++)
45                {
46                    LocalPipe pipe = __networks[i].RegisterOtherNetwork(__networks[j]);
47                    __networks[j].LinkPipe(pipe);
48                }
49
50            // create execution threads
51            for (int i = 0; i < __networks.Count; i++)
52            {
53                Thread th = new Thread(
54                    new ParameterizedThreadStart(this.runNetwork));
55                __executionThreads.Add(th);
56                th.IsBackground = true;
57            }
58        }
59
60        public object SyncRoot
61        {
62            get
63            {
64                return __syncRoot;
65            }
66        }
67
68        void runNetwork(object net)
69        {
70            SynchronousHebbian3Network network = net as SynchronousHebbian3Network;
71            while (true)
72            {
73                bool wait = false;
74                network.Propagate();
75                lock (this)
76                {
77                    if (__waiting == (__processorCount - 1)) // all have finished
78                    {
79                        __waiting = 0;
80                        __mainSync.Set();
81                        __mainSync.Reset();
82                    }
83                    else
84                    {
85                        wait = true;
86                        __waiting++;
87                    }
88                }
89               
90                if (wait)
91                    __mainSync.WaitOne();
92            }
93        }
94
95        public int Count
96        {
97            get
98            {
99                return __processorCount;
100            }
101        }
102
103        public void Start()
104        {
105            if (__started)
106                throw new NotSupportedException("Execution has already started.");
107
108            for (int i = 0; i < __executionThreads.Count; i++)
109                __executionThreads[i].Start(__networks[i]);
110
111            __started = true;
112        }
113
114        public void Stop()
115        {
116            if (!__started)
117                return;
118
119            for (int i = 0; i < __executionThreads.Count; i++)
120                if ((__executionThreads[i].ThreadState == ThreadState.Running) ||
121                    (__executionThreads[i].ThreadState == ThreadState.WaitSleepJoin))
122                    __executionThreads[i].Abort();
123        }
124
125
126
127        #region IDisposable Members
128
129        public void Dispose()
130        {
131            if (__started)
132                Stop();
133        }
134
135        #endregion
136    }
137}
Note: See TracBrowser for help on using the repository browser.