Coffeehouse Thread

32 posts

http://plugins.codeplex.com

Back to Forum: Coffeehouse
  • User profile image
    hack2rootho​tmai

    Everyone welcome to see my code for Parallel alternative, XML serializationalternative and MEF alternative at

    http://plugins.codeplex.com/">http://plugins.codeplex.com

        public class PluginWorkItem<T> : IPluginWorkItem
        {
            private volatile bool _isRunning;
            private List<PluginThreadSafeStack<T>> _data;
            private volatile int _dataCount = 0;
            private BackgroundWorker _mainThread;
            protected void PushData(T item, int threadID)
            {
                lock (this)
                {
                    if (_dataCount > 0)
                    {
                        _dataCount--;
                    }
                    _data[threadID].Push(item);
                }
            }
            protected T PopData(int threadID)
            {
                lock (this)
                {
                    return _data[threadID].Pop();
                }
            }
            protected void PushData(T item)
            {
                lock (this)
                {
                    if (_dataCount > 0)
                    {
                        _dataCount--;
                    }
                    _data[GetMinIndex(GetThreadCount())].Push(item);
                }
            }
            protected int GetDataCount()
            {
                lock (this)
                {
                    return _dataCount;
                }
            }
            protected T PopData()
            {
                lock (this)
                {
                    return _data[GetMaxIndex(GetThreadCount())].Pop();
                }
            }
            private int GetThreadCount()
            {
                if (ThreadCount > 0 && ThreadCount < 64)
                {
                    int threadCount = ThreadCount > Environment.ProcessorCount ? Environment.ProcessorCount : ThreadCount;
                    return threadCount;
                }
                return 1;
            }
            protected virtual int ThreadCount { get { return Environment.ProcessorCount; } }
            protected virtual int DataCount { get { return 0; } }
            protected virtual void ProcessItem(int threadID)
            {
                int threadCount = GetThreadCount();
                bool useDataCount = DataCount > 0;
                T item;
                do
                {
                    if (!IsRunning) return;
                    item = PopData();
                    if (item != null)
                    {
                        if (!IsRunning) return;
                        ProcessData(item/*, threadID*/);
                    }
                } while (!object.Equals(item, default(T)));
            }
            protected virtual void ProcessData(T item/*, int threadID*/) { }
            protected virtual void InitializeData() { }
            protected void InitializeThreadPoolData()
            {
                int threadCount = GetThreadCount();
     
                this._waitHandles = new WaitHandle[threadCount];
                for (int i = 0; i < threadCount; i++)
                {
                    _waitHandles[i] = new AutoResetEvent(false);
                }
     
                this._data = new List<PluginThreadSafeStack<T>>(threadCount);
                for (int i = 0; i < threadCount; i++)
                {
                    _data.Add(new PluginThreadSafeStack<T>());
                }
     
                _dataCount = DataCount;
                InitializeData();
                T item = default(T); //PopData();
                //if (item != null)
                //{
                //    ProcessData(item/*, 0*/);
                //}
     
                if (threadCount > 1)
                {
                    int count = threadCount;
                    int loop = 0;
                    for (; loop < count; )
                    {
                        while (_data[loop].Count == 0 && count > 1)
                        {
                            count--;
                        }
                        loop++;
                    }
                    if (count < threadCount)
                    {
                        item = PopData(0);
     
                        for (int i = 0; (i + 1) < threadCount && !object.Equals(item, default(T)); )
                        {
                            int offset = 0;
                            do
                            {
                                ProcessData(item/*, i + offset + 1*/);
                                item = PopData(GetMaxIndex(i + offset + 2));
                                offset++;
                            }
                            while ((i + offset + 1) < threadCount && !object.Equals(item, default(T)));
                            count = i + offset + 1;
                            loop = 0;
                            for (; loop < count; )
                            {
                                while (_data[loop].Count == 0 && count > 1)
                                {
                                    _data.RemoveAt(loop);
                                    _data.Add(new PluginThreadSafeStack<T>());
                                    count--;
                                }
                                loop++;
                            }
                            if (count == threadCount)
                            {
                                break;
                            }
                            item = PopData(GetMaxIndex(loop));
                            i = count - 1;
                        }
                    }
                }
                for (int i = 0; i < threadCount - 1; i++)
                {
                    if (_data[i + 1].Count > 0 && _data[i].Count == 0)
                    {
                        throw new InvalidProgramException();
                    }
                }
            }
            protected void Run()
            {
                int threadCount = GetThreadCount();
                for (int i = 0; i < threadCount; i++)
                {
                    ThreadPool.QueueUserWorkItem(new WaitCallback(ProcessHandle), i);
                }
            }
            private int GetMaxIndex(int index)
            {
                int max = 0;
                int maxIndex = 0;
                for (int i = 0; i < index; i++)
                {
                    if (_data[i].Count > max)
                    {
                        max = _data[i].Count;
                        maxIndex = i;
                    }
                }
                return maxIndex;
            }
            private int GetMinIndex(int index)
            {
                int min = int.MaxValue;
                int minIndex = 0;
                for (int i = 0; i < index; i++)
                {
                    if (_data[i].Count < min)
                    {
                        min = _data[i].Count;
                        minIndex = i;
                    }
                }
                return minIndex;
            }
            private void ProcessHandle(object sync)
            {
                int threadID = (int)sync;
                AutoResetEvent waitHandle = (AutoResetEvent)_waitHandles[threadID];
                if (IsRunning)
                {
                    ProcessItem(threadID);
                }
                waitHandle.Set();
                //MessageBox.Show(DateTime.Now.ToString());
            }
            protected bool IsRunning
            {
                get
                {
                    return _isRunning;
                }
            }
            #region IWorkItem Members
            private WaitHandle[] _waitHandles;
            protected virtual void Initialize()
            {
            }
            protected virtual void Terminate()
            {
            }
            void IPluginWorkItem.Run()
            {
                lock (this)
                {
                    if (_mainThread == null)
                    {
                        _isRunning = true;
                        _mainThread = new BackgroundWorker();
                        _mainThread.DoWork += (object sender, DoWorkEventArgs e) =>
                        {
                            Initialize();
                            InitializeThreadPoolData();
                            Run();
                            WaitHandle.WaitAll(_waitHandles);
                            lock (this)
                            {
                                Terminate();
                                if (_mainThread != null)
                                {
                                    _mainThread = null;
                                    _isRunning = false;
                                }
                            }
                        };
                        _mainThread.WorkerSupportsCancellation = true;
                        _mainThread.RunWorkerAsync();
                    }
                }
            }
            bool IPluginWorkItem.IsRunning
            {
                get
                {
                    return _isRunning;
                }
            }
            void IPluginWorkItem.Terminate()
            {
                lock (this)
                {
                    Terminate();
                    if (_mainThread != null)
                    {
                        _mainThread.CancelAsync();
                        _mainThread = null;
                        _isRunning = false;
                    }
                }
            }
            #endregion
        }

  • User profile image
    blowdart

    Umm yea, right. Why exactly?

  • User profile image
    stevo_

    YAY! just what I wanted.. Expressionless

  • User profile image
    hack2rootho​tmai

    stevo_ said:

    YAY! just what I wanted.. Expressionless

    I;m preparing samples (just like futures .NET 4.0)

    I can nave you a boost //So i'm currently working on a Pi calculation example.

    In a lighting sample, you can take use of parallelism in programs like below (working sudoku generator):

     

        public abstract class PluginBaseWorkItem<T>

        {

            private static object _sync = new object();

            private T _item;

            public T Item { get { return _item; } }

            public void Run()

            {

                List<WaitHandle> _waitHandles = new List<WaitHandle>();

                for (int i = 0; i < TaskCount; i++)

                {

                    _waitHandles.Add(new AutoResetEvent(false));

                }

                for (int i = 0; i < TaskCount; i++)

                {

                    ThreadPool.QueueUserWorkItem(new WaitCallback(ProcessHandle), _waitHandles[i]);

                }

                WaitHandle.WaitAll(_waitHandles.ToArray());

            }

            protected abstract void ProcessItem();

            protected void SetValue(T item)

            {

                _item = item;

            }

            protected T GetValue()

            {

                return _item;

            }

            protected virtual int TaskCount { get { return Environment.ProcessorCount; } }

            private void Process()

            {

                ProcessItem();

            }

            private void ProcessHandle(object sync)

            {

                AutoResetEvent waitHandle = (AutoResetEvent)sync;

                Process();

                waitHandle.Set();

            }

        }

     

             private class WorkItem : PluginBaseWorkItem<Puzzle>

            {

                protected override void ProcessItem()

                {

                    DlxEngine engine = new DlxEngine();

                    Puzzle value = engine.Generate(20);

                    lock (this)

                    {

                        Puzzle item = GetValue();

                        if (item == null || value.Rating > item.Rating)

                        {

                            SetValue(value);

                        }

                    }

                }

            }

     

            public static IPuzzle Create()

            {

                WorkItem workItem = new WorkItem();

                workItem.Run();

                IPuzzle puzzle = workItem.Item;

                return Create(puzzle.Text, puzzle.Seed);

            }

  • User profile image
    blowdart

    hack2roothotmai said:
    stevo_ said:
    *snip*

    I;m preparing samples (just like futures .NET 4.0)

    I can nave you a boost //So i'm currently working on a Pi calculation example.

    In a lighting sample, you can take use of parallelism in programs like below (working sudoku generator):

     

        public abstract class PluginBaseWorkItem<T>

        {

            private static object _sync = new object();

            private T _item;

            public T Item { get { return _item; } }

            public void Run()

            {

                List<WaitHandle> _waitHandles = new List<WaitHandle>();

                for (int i = 0; i < TaskCount; i++)

                {

                    _waitHandles.Add(new AutoResetEvent(false));

                }

                for (int i = 0; i < TaskCount; i++)

                {

                    ThreadPool.QueueUserWorkItem(new WaitCallback(ProcessHandle), _waitHandles[i]);

                }

                WaitHandle.WaitAll(_waitHandles.ToArray());

            }

            protected abstract void ProcessItem();

            protected void SetValue(T item)

            {

                _item = item;

            }

            protected T GetValue()

            {

                return _item;

            }

            protected virtual int TaskCount { get { return Environment.ProcessorCount; } }

            private void Process()

            {

                ProcessItem();

            }

            private void ProcessHandle(object sync)

            {

                AutoResetEvent waitHandle = (AutoResetEvent)sync;

                Process();

                waitHandle.Set();

            }

        }

     

             private class WorkItem : PluginBaseWorkItem<Puzzle>

            {

                protected override void ProcessItem()

                {

                    DlxEngine engine = new DlxEngine();

                    Puzzle value = engine.Generate(20);

                    lock (this)

                    {

                        Puzzle item = GetValue();

                        if (item == null || value.Rating > item.Rating)

                        {

                            SetValue(value);

                        }

                    }

                }

            }

     

            public static IPuzzle Create()

            {

                WorkItem workItem = new WorkItem();

                workItem.Run();

                IPuzzle puzzle = workItem.Item;

                return Create(puzzle.Text, puzzle.Seed);

            }

    So what you've go is a wrapper around the thread pool. And this replaces MEF how exactly?

  • User profile image
    hack2rootho​tmai

    blowdart said:
    hack2roothotmai said:
    *snip*

    So what you've go is a wrapper around the thread pool. And this replaces MEF how exactly?

    Due to the luck of free scree space i can not post whole sources from http://plugins.codeplex.com

    Other stuff like dynamic domain loading / unloading, calling code form another domain etc. there.

    I'm working on examples Wink

  • User profile image
    littleguru

    hack2roothotmai said:
    blowdart said:
    *snip*

    Due to the luck of free scree space i can not post whole sources from http://plugins.codeplex.com

    Other stuff like dynamic domain loading / unloading, calling code form another domain etc. there.

    I'm working on examples Wink

    Keep up the work! Smiley Try to evaluate on each piece of code that it really adds more than just a wrapper. Your multithreading stuff is a nice idea but the Parallel FX adds a lot more than just a wrapper around the ThreadPool!

    I don't know if you have seen these two videos. They explain some of the ideas that have been implemented in the Parallel FX:

    http://channel9.msdn.com/shows/Going+Deep/Inside-Parallel-Extensions-for-NET-2008-CTP-Part-1/
    http://channel9.msdn.com/shows/Going+Deep/Inside-Parallel-Extensions-for-NET-2008-CTP-Part-2/

  • User profile image
    hack2rootho​tmai

    blowdart said:
    hack2roothotmai said:
    *snip*

    So what you've go is a wrapper around the thread pool. And this replaces MEF how exactly?

    Does anybody can look into my profile?

    I've go these:

    <html>

        <head>
            <title>The resource cannot be found.</title>
            <style>
             body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;}
             p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px}
             b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px}
             H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red }
             H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon }
             pre {font-family:"Lucida Console";font-size: .9em}
             .marker {font-weight: bold; color: black;text-decoration: none;}
             .version {color: gray;}
             .error {margin-bottom: 10px;}
             .expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; }
            </style>
        </head>
        <body bgcolor="white">
                <span><H1>Server Error in '/' Application.<hr width=100% size=1 color=silver></H1>
                <h2> <i>The resource cannot be found.</i> </h2></span>
                <font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif ">
                <b> Description: </b>HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. &nbsp;Please review the following URL and make sure that it is spelled correctly.
                <br><br>
                <b> Requested URL: </b>/Niners/hack2roothotmai./<br><br>
        </body>
    </html>

  • User profile image
    littleguru

    hack2roothotmai said:
    blowdart said:
    *snip*

    Does anybody can look into my profile?

    I've go these:

    <html>

        <head>
            <title>The resource cannot be found.</title>
            <style>
             body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;}
             p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px}
             b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px}
             H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red }
             H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon }
             pre {font-family:"Lucida Console";font-size: .9em}
             .marker {font-weight: bold; color: black;text-decoration: none;}
             .version {color: gray;}
             .error {margin-bottom: 10px;}
             .expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; }
            </style>
        </head>
        <body bgcolor="white">
                <span><H1>Server Error in '/' Application.<hr width=100% size=1 color=silver></H1>
                <h2> <i>The resource cannot be found.</i> </h2></span>
                <font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif ">
                <b> Description: </b>HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. &nbsp;Please review the following URL and make sure that it is spelled correctly.
                <br><br>
                <b> Requested URL: </b>/Niners/hack2roothotmai./<br><br>
        </body>
    </html>

    That's because you have a . in your nickname... The website seems to have problems with it.

  • User profile image
    hack2rootho​tmai

    littleguru said:
    hack2roothotmai said:
    *snip*

    Keep up the work! Smiley Try to evaluate on each piece of code that it really adds more than just a wrapper. Your multithreading stuff is a nice idea but the Parallel FX adds a lot more than just a wrapper around the ThreadPool!

    I don't know if you have seen these two videos. They explain some of the ideas that have been implemented in the Parallel FX:

    http://channel9.msdn.com/shows/Going+Deep/Inside-Parallel-Extensions-for-NET-2008-CTP-Part-1/
    http://channel9.msdn.com/shows/Going+Deep/Inside-Parallel-Extensions-for-NET-2008-CTP-Part-2/

    Phankx, but, you know, all we (me, alone, and aall boys and girls form Parallel extensions) are doing well -

    is just to write simple wrappers on wrappers for Win32 threads!!!

    At least we are all frameworkers Wink

  • User profile image
    littleguru

    hack2roothotmai said:
    littleguru said:
    *snip*

    Phankx, but, you know, all we (me, alone, and aall boys and girls form Parallel extensions) are doing well -

    is just to write simple wrappers on wrappers for Win32 threads!!!

    At least we are all frameworkers Wink

    Sure, but you could write a thin wrapper or a wrapper that adds a lot of logic. Your's is more like a thin wrapper (that does not add much on their own) and theirs is a wrapper that adds a lot of logic and algorithms.

  • User profile image
    hack2rootho​tmai

    littleguru said:
    hack2roothotmai said:
    *snip*

    Sure, but you could write a thin wrapper or a wrapper that adds a lot of logic. Your's is more like a thin wrapper (that does not add much on their own) and theirs is a wrapper that adds a lot of logic and algorithms.

    You are right! i prefer Keep It Stupid Simple. Frameworkers goes far from this idiom nowadays, that means, that i nedd to spend time trying to deal with comlicated APIs, more time when things are not going the right way you want it to, more time waiting for a next .NET version (just because nobody understands it better) tips and tricks, and so on.

    In m opinion, if i can do complicated thing using things as simple as pissible, i.e. thin wrappers - this is a best, just because you can understang what beneath API is going on by simply lookin under the hood Wink.

    KISS Wink

  • User profile image
    littleguru

    hack2roothotmai said:
    littleguru said:
    *snip*

    You are right! i prefer Keep It Stupid Simple. Frameworkers goes far from this idiom nowadays, that means, that i nedd to spend time trying to deal with comlicated APIs, more time when things are not going the right way you want it to, more time waiting for a next .NET version (just because nobody understands it better) tips and tricks, and so on.

    In m opinion, if i can do complicated thing using things as simple as pissible, i.e. thin wrappers - this is a best, just because you can understang what beneath API is going on by simply lookin under the hood Wink.

    KISS Wink

    Don't make the error to set thin wrappers equal with simplicity! The Parallel FX has a simple API but still a lot of logic runs underneath. A simple API doesn't mean that there's no logic underneath. Sometimes thin wrappers are even more complicated because the user needs to deal with everything from the underlying system, whereas logic could hide that away!

    In your case the API is simple but does not add much to the ThreadPool. In the case of the Parallel FX the API is simple as well but adds a lot more logic so that load balancing works out better and the caches of the cores are used more efficiently.

    Why do you want to kiss me? Tongue Out

  • User profile image
    stevo_

    The challenge is to make a powerful abstraction thats simple.. (balanced) its easy to make a simple abstraction thats simple.. you should try and find something more unique (and perhaps more simple (simple != useless) because MEF and concurrency / threading are big tasks to take on).

    I've found naturally the things I've ended up building are things that have caused me the biggest development friction (this is natural), and I'm certainly not having friction points with ioc/di frameworks.. or at least if I want more from them, would require a sizable dev effort.

  • User profile image
    Bass

    I use Mono.Addins Smiley

     

  • User profile image
    joechung

    Could you state clearly why you are writing alternatives to MEF, ParallelFx, and XML serialization?  Not that it matters for you, since self-edification is a worthwhile goal, but for others to be interested, you need incentive.

  • User profile image
    hack2rootho​tmai

    joechung said:

    Could you state clearly why you are writing alternatives to MEF, ParallelFx, and XML serialization?  Not that it matters for you, since self-edification is a worthwhile goal, but for others to be interested, you need incentive.

    Ok, i'm willing to be sequential.

    1st point: Full source code access.

    2st point: Framework size.

    3st point: It challenging to do the better product as do the guys at Microsoft.

    In your opinion, why Linus Torvalds stated his own project formerly known as Linux Project, even he knows that Unix/Windows/QNX etc. OS-es already exists?

    If you are asking me, why I'n doing so. I have the answer.

    I'm tired, because nobody hires me, nobody supports me, nobody follows me. Do you really think that big fat cats in Microsoft will ever give me a chance to do the best i can do? No. Misrosoft is a suxx as a work place, because people like me are eaten for the breakfast by the corporate approach, so I'm smart enouth mice to understand it. Too many guys tries to be a one-man-at-the-top. Probably, I'm going to continue to develop software while working as system administrator in no-one-knows-it-company for the end of my life. On the other hand I can do better than Microsoft Teams and I will.

    So, if you can't challenge the best of Microsoft teams for your own, give me chance to do this for you.

    If you are from one ot the teams you mention it earlier (ParallelFx, MEF, etc), just be ready to know what i'm thinking about you.

    I'm trying to prove that you can create by YOUR own code which can do the parallelization in several assemblies with 4-8K size, and it will work BETTER that in .NET FW 4.0 or ParallelFx. YOU can do better. YOU can make use of it. YOU. Just learn what is incapsulation, interfaces, inheritance, factory pattern, etc, exploring my code. It is a prove that YOU can do better.

  • User profile image
    PaoloM

    hack2roothotmai said:
    joechung said:
    *snip*

    Ok, i'm willing to be sequential.

    1st point: Full source code access.

    2st point: Framework size.

    3st point: It challenging to do the better product as do the guys at Microsoft.

    In your opinion, why Linus Torvalds stated his own project formerly known as Linux Project, even he knows that Unix/Windows/QNX etc. OS-es already exists?

    If you are asking me, why I'n doing so. I have the answer.

    I'm tired, because nobody hires me, nobody supports me, nobody follows me. Do you really think that big fat cats in Microsoft will ever give me a chance to do the best i can do? No. Misrosoft is a suxx as a work place, because people like me are eaten for the breakfast by the corporate approach, so I'm smart enouth mice to understand it. Too many guys tries to be a one-man-at-the-top. Probably, I'm going to continue to develop software while working as system administrator in no-one-knows-it-company for the end of my life. On the other hand I can do better than Microsoft Teams and I will.

    So, if you can't challenge the best of Microsoft teams for your own, give me chance to do this for you.

    If you are from one ot the teams you mention it earlier (ParallelFx, MEF, etc), just be ready to know what i'm thinking about you.

    I'm trying to prove that you can create by YOUR own code which can do the parallelization in several assemblies with 4-8K size, and it will work BETTER that in .NET FW 4.0 or ParallelFx. YOU can do better. YOU can make use of it. YOU. Just learn what is incapsulation, interfaces, inheritance, factory pattern, etc, exploring my code. It is a prove that YOU can do better.

    Proper spelling would be a wonderful starting point.

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.