Multithreading in CLR

March 14, 2010 .NET, C#, Concurrency No comments

Here I have code that provides some explanations to the multithreading in .NET. Wrote it to refresh what I know on concurrency.

    1 /********************************************************
    2  *
    3  *                CODE IS MY PRESENTATION
    4  *
    5  ********************************************************/
    6 using System;
    7 using System.IO;
    8 
    9 //1. CLR threading is based on Windows threading.
   10 //2. CLR threading is going away from correspondence with Windows threading.
   11 //SO do not use native windows functions if you already writing CLR code
   12 //that is for future, of course Microsoft is not sleeping
   13 
   14 //Let’s start
   15 //This is namespace where Multithreading lives…
   16 using System.Threading;
   17 
   18 namespace TheoryConsole
   19 {
   20     class Program
   21     {
   22         static void Main(string[] args)
   23         {
   24             MultiThreadingInCLR presentation = new MultiThreadingInCLR();
   25 
   26             //presentation.CLRHasPoolOfThreads();
   27             //presentation.UsingThreadPoolForAsynchCalculations();
   28             //presentation.UsingDedicatedThread();
   29             //presentation.PeriodicalCallinOfAsynchOperations();
   30             presentation.AsynchronousProgrammingModel();
   31 
   32             Thread.Sleep(100);
   33             //Console.WriteLine(“Press any key to exit…”);
   34             //Console.ReadLine();
   35         }
   36     }
   37 
   38     public class MultiThreadingInCLR
   39     {
   40         //Many developers uses multithreading too often,
   41         //but it is not cheap.
   42 
   43         //Because
   44         //to Switch context system needs:
   45         // – go to core -> save processor registers ->
   46         // put spin-blocker -> take another thread ->
   47         // unput blocker -> load registers > run thread
   48 
   49         //Pool of Threads
   50         public void CLRHasPoolOfThreads()
   51         {
   52             //Each Process has its oun Pool wich can be used
   53             // in all AppDomains of that Process
   54             ThreadPool.QueueUserWorkItem(DoAsynchWork, 10);
   55 
   56             //If there are no requests to Pool,
   57             //CLR will delete threads from it in about 2 minutes
   58 
   59             // Amount of threads in Pool
   60             // – Do not try to limit amount of threads in Pool
   61             // There are two types of threads: Worker and I/O
   62             int workingThreads = 0;
   63             int completionThreads = 0;
   64             //Get/Set Max/Min Threads
   65             ThreadPool.GetMaxThreads(out workingThreads, out completionThreads);
   66 
   67             //To GetAvailableThreads call it:
   68             //ThreadPool.GetAvailableThreads();
   69 
   70             for (int i = 0; i < 506; i++)
   71             {
   72                 ThreadPool.QueueUserWorkItem(DoAsynchWork, 100);
   73             }
   74 
   75             ThreadPool.GetMaxThreads(out workingThreads, out completionThreads);
   76             workingThreads = 1000;
   77         }
   78 
   79 
   80         public void UsingThreadPoolForAsynchCalculations()
   81         {
   82             //If you do not need I/O operations use Worker type of thread
   83             //For example spell-checker in some editor need to work asynchronously
   84             //
   85             //Most commonly these three methods are called:
   86             //
   87             //static Boolean QueueUserWorkItem(WaitCallback callBack);
   88             //static Boolean
   89             //   QueueUserWorkItem(WaitCallback callBack, Object state);
   90             //static Boolean
   91             //   UnsafeQueueUserWorkItem(WaitCallback callBack, Object state);
   92 
   93             //callBack is delegate with the next signature:
   94             //delegate void WaitCallBack(Object state);
   95             WaitCallBackExample_IDoHardWork(null);
   96 
   97             //Here is how to start doing hard work asynchromously
   98             ThreadPool.QueueUserWorkItem(WaitCallBackExample_IDoHardWork);
   99 
  100             //Use UnsafeQueueUserWorkItem if the time resource is very critical
  101             //Reason: QueueUserWorkItem is verifying
  102             // if the assemblies from Thread stack has permissions
  103             // to access file for example
  104             //SecurityException could be generated if there is no presmissions
  105         }
  106 
  107         #region Dedicated Thread
  108         public void UsingDedicatedThread()
  109         {
  110             //Commonly is used if we need to set some priority to thread
  111             // or if we need to change other properties of Thread
  112             // – we need set some properties to the thread
  113             // – we need set priority to our thread
  114             //        (ThreadPool has one priority for all threads in it)
  115             // – if we want run Thread in foreground
  116             //        (ThreadPoole runs threads in background)
  117 
  118             //please note that here we do NOT create actual thread,
  119             // because thread will be created only
  120             //when we will call our thread object
  121             Thread someNewThread = new Thread(ComputeBoundWork);
  122             //When we call Start method actuall thread is created
  123             // and process starts
  124 
  125             someNewThread.Start(1);
  126             Thread.Sleep(1000);//this imitates some work in main thread
  127 
  128             //Please note!
  129             // that after ComputeBoundWork finished its work,
  130             // actual system thread was deleted
  131 
  132             // this waits while the dedicated thread will finish all its work…
  133             someNewThread.Join();
  134 
  135             // so this is some kind of sysnch of threads..
  136         }
  137         #endregion
  138 
  139         #region Timer
  140         public void PeriodicalCallinOfAsynchOperations()
  141         {
  142             //System.Threading namespace has Timer defined
  143             //it has 4 very similar constructors
  144             //this used only TimerCallback
  145             //Timer timer1 = new Timer(ComputeBoundWork);
  146             Timer timer2 = new Timer(ComputeBoundWork, null, 0, 1000);
  147             // method will be called only once
  148             //Timer timer3 =
  149             //  new Timer(ComputeBoundWork, null, 0, Timeout.Infinite);
  150 
  151             //if you want to change timing behaviour of the timer
  152             // you could use Change() method, or Dispose() to stop…
  153             Thread.Sleep(10000);// this imitates some work here – 10 seconds
  154             timer2.Dispose();
  155 
  156             // Please note that there are 3 different Timers in FCL
  157             // – System.Threading.Timer
  158             //      is most suitable for doing asynch operations
  159             // – System.Windows.Forms.Timer
  160             //      is equivalent to WinAPI SetTimer.. events are added WM_TIMER
  161             // – System.Timers.Timer
  162             //      is some kind of wrapper of the Threading.Timer
  163         }
  164         #endregion
  165 
  166         #region Asynchronous Programming Model
  167 
  168         private IAsyncResult synchResultForFileWriting;
  169         private FileStream fs;
  170         public void AsynchronousProgrammingModel()
  171         {
  172             // Using of the asynchronous operations is the key to big programms :)
  173             // Combination of it with the ThreadPool
  174             //  is way to effectively use all processors of the system.
  175             // Examples where it is used…
  176             // – System.IO.Stream -> FileStream and NetworkStream
  177             //        derived classes has methods BeginRead(), BeginWrite()
  178             // – System.Net.Dns -> BeginGetHostAddresses(),
  179             //        BeginGetHostByName(), and so on…
  180             // – System.Net.Sockets -> BeginAccept(), BeginConnect(), …
  181             // all delegate types has BeginInvoke(), what could be used in APM
  182 
  183             //ok let we see file reading example
  184             fs = new FileStream(
  185                 “test.bin”, FileMode.OpenOrCreate,
  186                 FileAccess.Write, FileShare.None, 35000, FileOptions.Asynchronous);
  187             synchResultForFileWriting =
  188                 fs.BeginWrite(new byte[32738], 0, 32000, CallBackMethod, null);
  189             Console.WriteLine(
  190                 “This is called just after we said to write something to file.”);
  191 
  192 
  193             // There are 3 types of joining threads.. im APM
  194             // 1. Waiting for finish of work
  195             //Int32 readMeFile.EndRead(IAsyncResutl asyncResult)
  196 
  197             // 2. Regualar polling – is not effective method
  198             Stream readMeFile = new FileStream(“film.mp4”, FileMode.Open,
  199                   FileAccess.Read, FileShare.Read, 1024, FileOptions.Asynchronous);
  200 
  201             byte[] fileContent = new byte[450000000];
  202             IAsyncResult ar =
  203                 readMeFile.BeginRead
  204                     (fileContent, 0, fileContent.Length, null, null);
  205 
  206             while (!ar.IsCompleted)
  207             {
  208                 Console.WriteLine(
  209                     “Reading Large Music file operation is not completed…”);
  210                 Thread.Sleep(100);
  211             }
  212 
  213             //please note that this will not stop actuall
  214             //     thread because we were asking for finish above
  215             readMeFile.EndRead(ar);
  216             Console.WriteLine(“We already finished reading large music file.”);
  217 
  218             // 3. Callback!!
  219             // is the most efficient and commonly used joining type
  220         }
  221 
  222         private void UserAlreadyWantsToReadThatFile()
  223         {
  224             fs.EndWrite(synchResultForFileWriting);
  225             //now we are sure that ansynch writing to file has finished its work!!
  226             fs.Close();
  227         }
  228 
  229 
  230         private void CallBackMethod(IAsyncResult ar)
  231         {
  232             //this method will be called when writing to file will finish its work
  233             Console.WriteLine(“We just finished writing to file…”);
  234         }
  235 
  236         #endregion
  237 
  238         public void WaitCallBackExample_IDoHardWork(object hardParameter)
  239         {
  240             //actually if you see my body you know that I do not do work
  241             // I sleep for 2 seconds :)
  242             Thread.Sleep(2000);
  243         }
  244 
  245         public void DoAsynchWork(object miliseconds)
  246         {
  247             Thread.Sleep((int)miliseconds);
  248         }
  249 
  250         public void ComputeBoundWork(object opCode)
  251         {
  252             Thread.Sleep(1000);// this imitates some work…
  253             Console.WriteLine(
  254                 string.Format(“Compuing bound work with param: {0}”, opCode));
  255         }
  256     }
  257 }


No comments


Few notes about Designing System

March 14, 2010 Book Reviews, Design No comments

Designing is “dry”, non-deterministic and heuristic process. 

I just read one chapter from S. McConnell’s “Clean Code” book, and want make some review and summary without taking a look back into book. This should help me reinforce memories of what I’ve learnt.

When you are designing you always should keep in mind that you will not be able to make design good for all situations of life. Thus overthinking on problems could lead to overengineering, so you will get complex system with lot of unneeded tails. Future developers will not be able to work with your design. And project could even fail at some point of time.

To avoid this you should keep building system on easy to understand interfaces, that define how system behaves on different levels and that provide loose coupling.

Building your application like one solid system will lead to situation when you have everything coupled. I’m familiar with system, which is done like single exe file with more than 50 Mb size. You could not even imagine how everything there is coupled – you can get access to any part of the system from any other part. I understand that it is a legacy system and some things where done cause of ‘old school’ and so on and I understand that developers take this into account, but how on earth new guy will know how to work in that system? But how could it be sweet when you have system divided into subsystems, which of them has own responsibilities and depends only on one-two other subsystems, but at the same time provides functionality to good portion of other subsystems.

So, here we talk about low coupling, high cohesion and subsystems ordering. Here is how I do understand this terms:

Low coupling means that parts of the system should interact with each other by clearly defined interfaces, and you should work on decreasing number of interaction. Few characteristics that lead to low coupling are: Volume of connections should be low, Visibility of how two parts connects to each other should be high, Flexibility of changing connection should be also high.

High cohesion means that inside of one part of the system everything should be done compactly, part should have one responsibility well encapsulated inside.

Subsystems ordering means that once you have A and B, and B uses A, then A should not use B. If you need to grow your system you should decrease number of subsystems that you depend on and you should increase number of subsystems that depends on current subsystem. So you are getting tree.

When designing use Design Patterns. Why? Because they are found heuristically and if you think that you have a better decision to the same problem that known design pattern solves it is very possible that your solution will fail.

When designing ask Questions. Why? Designing is iterational  process, you always can improve your solution. Just ask yourself if your current design is enough good and if so how could you prove that. Ask if there are other solutions to problem and why did you select your. Ask a lot. Invite a friend let he ask you.

You can design top-bottom and bottom-top ways. Use both. Why and how? Because they have advantages and disadvantages – you can get stuck with top-bottom if you will not have tech solution to some problem, that was defined at the top or solution is very cumbersome, also you can lost vision of what you do with bottom-top and will lost how to clue everything. We are all now far from waterfall so I think that nowadays it is possible to combine those two ways to design.


No comments


Simple Web Page doesn’t work after Ctrl+F5 hit in VS under Windows 7

March 14, 2010 ASP.NET, Environment, QuickTip No comments

I’ve upgraded my system to Windows 7 couple of months ago, but I haven’t used web development.

The Problem
Few days ago, I created empty web application, added new page and added <p>bla-bla<p>, then I hit Ctrl+F5 and my default browser opened, but nothing happened. I was thinking that it is because of my browser, so I switched over 3 browsers and nothing helped, I also thought that it VS2010 broke something causing asp.net development server issues, so uninstalled it and installed back, but nothing helped.

I even had a thought to reinstall system, but decided to search over the internet more deeper and found that:

Reason
Reason is that when system uses IPv6 browsers could have issues with resolving localhost. 

Solution
Disabling IPv6 is solution for this problem.

You can disable IP 6 in your system by ensuring that you have this in your registry:
HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServicesTcpip6ParametersDisabledComponents set to 0xffffffff

Firefox settings change
Also when searching I found for myself that we can change settings of Firefox with writing about:config in address bar.


No comments


k-means lecture text

March 11, 2010 Education, Pedagogic Practice No comments

Привітання та дуже загальний вступ

Привіт. Мене звати Андрій Будай і сьогодні я вестиму цю пару, темою якої є k-means алгорим. Some parts will be in English, so please do not scare if I switch to English unexpectedly, like I just did.

As you all know we are talking about the clustering of data. Human brains are very good at finding similarity in things. For example, biologists have discovered that things belong to one of two types: things are either brown and run away or things are green and don’t run away. The first group they call animals, and the second is plants.
Процес групування речей по певних ознаках ми називаємо кластеризацією. Якщо ми говоримо про то, що біологи почали поділяти тварин на ряди, роди та види, то ми говоримо про ієрархічну класифікацію. Яку я передбачаю ви вже розглянули, зоокрема підхід ієрархічної кластиризації згори-вниз та знизу-вгору.

Розуміння Partitioning алгоритмів

А що якщо ми хочемо групувати дані не в порядку збільшення або зменшення об”єму груп, якщо ми хочемо погрупувати N векторів даних на K кластерів за один логічний хід? Такий різновид кластерізації називається Partitioning алгоритмами. Іншими словами ми розбиваємо множину.

Ще раз нагадаємо що таке розбиття множини:

Система множин S={X1Xn} називається розбиттям множини M, якщо ця система задовольняє наступним умовам:

  • будь-яка множина Xk з S є підмножиною множини M:
XS: XM
  • будь-які дві множини Xi, Xj з S мають порожній перетин:
Xi, XjS: XiXjXiXj = ∅.
  • об’єднання всіх множин, які входять в розбиття M, дає множину M:
bigcup_{X in S} X = M

Partitioning алгоритми розбивають множину на певну кількість груп за один крок, проте результати покращуються із процесом ітерування. До найвідоміших partitioning алгоритмів відносяться k-means та його модифікації із доповненнями, quality threshold алгоритм також для кластеризації можуть бути використані Locality-sensitive hashing та Graph-theoretic methods. Якщо буде час то ми поговорими і про них також.

K-means алгоритм

Отже, k-means. Насправді алгорим не є складним і я сподіваюся що всі із вас будуть розуміти суть алгоритму та будуть мати палке бажання його реалізувати. Це моя мета на сьогоднішню пару. []

Завданням алгоритму є здійснити розбиття N векторів даних на K груп. Причому число k є попередньо відомим – або є вхідним параметром алгоритму як і дані.

Опис
[Цю частину я збираюся продиктувати]

Маючи список даних спостережень (x1, x2, …, xn), де кожен x є вектором розмірності d, k-means кластеризація призначена для розбиття цього списку із n спостережень на k підмножин (k < n), в результаті чого ми повиння отримати таке розбиття S={S1, S2, …, Sk}, в якому внутрішньокластерна сума квадратів норм є мінімізованою:

underset{mathbf{S}} operatorname{arg,min} sum_{i=1}^{k} sum_{mathbf x_j in S_i} left| mathbf x_j - boldsymbolmu_i right|^2

де μi є центром Si.

Кроки алгоритму

Перш за все нам слід мати множину із k центрів m1(1),…,mk(1), які можуть бути визначені або випадковим чином, або евристичним шляхом. Алгоритм складається із двох основних кроків – кроку присвоєння та оновлення.

Ініціалізація: Перш за все нам слід мати множину із k центрів m1(1),…,mk(1),
які можуть бути визначені або випадковим чином, або евристичним шляхом.
Алгоритм складається із двох основних кроків – кроку присвоєння та
оновлення.
Крок присвоєння: Присвоюємо кожен вхідний вектор до кластеру із найближчим центром.

S_i^{(t)} = left{ mathbf x_j : big| mathbf x_j - mathbf m^{(t)}_i big| leq big| mathbf x_j - mathbf m^{(t)}_{i^*} big| text{ for all }i^*=1,ldots,k right}
Крок оновлення: Вираховуємо нові центри, щоб вони найкраще підходили векторам за які вони відповідають.

mathbf m^{(t+1)}_i = frac{1}{|S^{(t)}_i|} sum_{mathbf x_j in S^{(t)}_i} mathbf x_j
Повторюємо кроки присвоєння та оновлення допоки присвоєння ще змінюються.

Демонстрація
[here I’m going to switch to English and demonstrate steps of the algo at board] 

1) k initial “means”
(in this case k=3) are randomly selected from the data set (shown in color).
2) k clusters are created by associating every observation with the nearest mean.
3) The centroid of each of the k clusters becomes the new means.
4) Steps 2 and 3 are repeated until convergence has been reached.

Оскільки алгоритм є емпіристичним, то немає гаранції, що він досягне глобального мінімуму. А також результат залежить від початкових вхідних кластерів. Оскільки алгоритм є досить швидким, то є така практика, як прогонка алгоритму деяку кількість раз із різними початковими центрами.

Other variations of the K-means algorithm

Because of some disadvantages of standard algorithm we’ve got few
different variations of it like Soft K-means, when data element is
fuzzy associated with mean, or like K-means++, which is additions to
the K-means to initialize cluster means better.

K-means++
With the intuition of spreading the k initial cluster centers away from
each other, the first cluster center is chosen uniformly at random from
the data points that are being clustered, after which each subsequent
cluster center is chosen from the remaining data points with
probability proportional to its distance squared to the point’s closest
cluster center.

Soft K-means алгоритм

В Soft K-means алгоритму кожен вектор має часткову приналежність до якогось кластеру, так як же і в розмитій логіці – існуть не тільки 1 та 0, а ще й 0.667. Таким чином, вектори, що знаходяться ближче до центру кластеру мають більшу приналежність до нього, а ті які знаходяться по краях мають дуже малу приналежність. Отже, для кожного вектора x ми маємо коефіцієнт який відповідає за приналежність до k-того кластеру uk(x). Звичайно, що сума коефіцієнтів для заданого вектора x є рівною 1:

 forall x left(sum_{k=1}^{mathrm{num.} mathrm{clusters}} u_k(x)  =1right).

Таким чином, центр кластеру вираховується як середнє всіх векторів, домножених на цей коефіцієнт приналежності до кластеру:

mathrm{center}_k = {{sum_x u_k(x)^m x} over {sum_x u_k(x)^m}}.

Коефіцієнт є обернено пропорційний до відстані до центру кластеру:

u_k(x) = {1 over d(mathrm{center}_k,x)},

опісля коефіцієнти нормалізуються із використання параметра m > 1 щоб сума була рівною 1 за такою формулою:

u_k(x) = frac{1}{sum_j left(frac{d(mathrm{center}_k,x)}{d(mathrm{center}_j,x)}right)^{2/(m-1)}}.

Коли m
близьке до 1, кластери ближчі до центру мають більшу приналежність, а ті що дальше мають меншу приналежність. Для m = 2, це відповідає лінійній нормалізації коефіцієнтів, щоб отримати суму 1 і алгоритм подібний до звичайного k-means.

Кроки цього алгоритму є дуже подібними до звичайного:

  • Вибираємо кількість кластерів.
  • Випадковим чином вибираємо приналежність кожного із векторів до якогось кластеру.
  • Повторюємо наступне допоки алгоритм не збіжиться (це коли зміна коефіцієнтів між ітераціями не перевищує деякого числа varepsilon, що є порогом чутливості) :

    • Вираховуємо центр для кожного кластеру за допомогою формули наведеної вище.
    • Для кожного вектору вираховуємо коефіцієнти приналежності до кластерів.

Цей алгоритм також має деякі проблеми що й у звичайному алгоритму, зокрема залежність від початково вибраних кластерів, та не обов”язкове знаходження глобального мінімуму.

Можливий підхід до реалізації

Обов’язково розділяємо класи для зчитуванна та утримання даних, із класом, що буде здійснювати кроки алгоритму, якщо планується графічне зображення то воно також має бути відділене. Залишаємо можливість обирати норму.

[Не дуже хочу закидати реалізацію, щоб студенти завчасно не використали її]

He-he! I’ve got it in about 3.5 hours.

I would say that core algorithm code looks very good, at least take a look at method Process:

        public void Process()
        {
            Initialize();

            while (!ClustersRemainTheSame())
            {
                AssignStep();
                UpdateStep();
            }
        }
I’ve also used realized algorithm in two applications – one is clusterization of animals by theirs properties and another is graphical demoing usage. Below is nice screenshoot:

Application

[If I’ll see that we are ok with English this part will be in it, otherwise we will have this in Ukrainian.]

“The k-means clustering algorithm is commonly used in computer vision as a form of image segmentation. The results of the segmentation are used to aid border detection and object recognition. In this context, the standard Euclidean distance is usually insufficient in forming the clusters. Instead, a weighted distance measure utilizing pixel coordinates, RGB pixel color and/or intensity, and image texture is commonly used

One of the application examples is clustering colors in image to compress image. A good article on this is written up here.

Завершення пари

Побажання зробити реалізацію алгоритму

Час на питання

P.S. So far I have some good plan for Friday… not 100% complete, but I’m satisfied with what I have. And tomorrow I’m going to work on this once again. Also not really good that I just translated a lot of stuff from wiki pages.


No comments


K-means algorithm for Students of Lviv National University

March 9, 2010 Education, Pedagogic Practice, Ukrainian No comments

On Friday, I will have my first teaching experience (or at least teaching students of my University). This is part of the Master Year  pedagogic practice. Subject is called “Data Mining” and theme of Friday’s lesson will be “K-means” algorithm.

Few things here before I start
I will have this post mixed of English and Ukrainian. Why? Because the way I’ll be presenting algorithm in both languages and because I need some report in Ukrainian, so do not want to double write a lot – I’m lazy ass as someone said.

План уроку (Agenda)

1. Привітання та дуже загальний вступ

Даю знати хто я і про що буду розповідати. Also I have a nice joke in English that could grab attention of the students. Hope they will be able to recognize and understand my English.

2. Розуміння Partitioning алгоритмів

Тут я нагадаю що таке Cluster analysis та згадую про Hierarchical і Partitional різновиди. І дещо про різниці між ними.

3. K-means алгоритм

– завдання алгоритму
– введення позначень
– кроки алгоритму
– демонстрація алгоритму як на дошці так і на комп”ютері
– переваги та недоліки

4. Other variations of the K-means algorithm

Because of some disadvantages of standard algorithm we’ve got few different variations of it like Soft K-means, when data element is fuzzy associated with mean, or like K-means++, which is additions to the K-means to initialize cluster means better.

5. Soft K-means алгоритм

– різниця в кроках алгоритму

6. Можливий підхід до реалізації (можливо напишу програму ще раз)

7. Application

If I’ll see that we are ok with English this part will be in it, otherwise we will have this in Ukrainian.
One of the application examples is clustering colors in image to compress image.

8. Побажання зробити реалізацію алгоритму

9. Час на питання

I hope tomorrow I’ll have article with whole lesson text, more structured and with examples, also hope to write k-means plus demo from scratch. Will see how fast am I.


No comments


There is no “Edit WCF Configuration”

March 3, 2010 IDE, WCF, Workaround No comments

If you have no “Edit WCF Configuration” in the context menu on right click on the App.config.

I’m talking about this one:

Go to Tools -> WCF Service Configuration Editor

Open it and then close.
This is funny and strange solution to our issue.


No comments


Am I still on track?

March 3, 2010 Revision of my activity, Success No comments

I just want to ask my self if I’m still on the track. Accordingly to my 7th point in the list of “Things you need to remember to become successful developer” I need to have revision of what I’m doing.
This is year list:
1. Get MCTS, MCPD.
I’m out. Why? Because I was to lazy to schedule exam and learn something. I do not think that MCTS could be big problem for me. Just need week for learning.
2. Read 24 book.
I’m on. Read 3 books in 2 months… almost as desired.
3. Become known employee in my company.
I’m on and out. I did few presentations, but I think that need more. Will try to get something tomorrow.
4. Familiarize with Java and contribute research and development work to Kohonen Map.
I’m on regarding of Java – wrote few GoF patterns on it.
I’m off the Kohonen Maps – will fix this.
5. Become Senior Developer
I’m on.
6. Improve English.
I’m on. Joining English courses.
7. Gather good capital and look for investments.
I’m still on (before marriage :) ) 1/3 of what I need for month are income like interest, and that is very good.
This is keep doing list:
1. Keep on learning.
I’m on.
2. Establish what your goal is
I’m on.
3. Every challenge is opportunity
I’m on and off.
4. Be positively charged
Getting better with this.
5. Find a mentor.
I have such.
6. Be more public
Search google me, hope will get more than 700 results.
7. Do you gym
I’m completely out. I need fix this 100%, either I’ll be dead before get to the goal.


No comments


Possible Parallel Algorithm for Self-Organizing Maps

March 3, 2010 KohonenAlgorithm, MasterDiploma No comments

I already have some implementation of the parallelization in algorithm of SOM.
Let us quickly remind the main steps of the classical Kohonen learning algorithm.

  1. Initialization – means setting values to the network, either random or from input space.
  2. Sampling – select random vector x from the input space.
  3. Similarity matching – find best matching neuron, i.e. neuron which is most similar to the x.
  4. Update – need to update neighbourhood neurons to the BMN in particular radius r, which is decreasing with time.
  5. Continuation (2-5) – we repeat 2-5 many times, while do not have map with enough quality.

As you see algorithm could be very expensive if we have big amounts of data.

Also, steps 3 and 4 takes the most of time, what if we execute 2-3-5 in separate threads? Yes, we could do this to some extend. Main issue is when we have overlapping of affected area by two best matched neurons wich we found in separate threads.

I’m bit tired to write a lot of explanations of this algorithm, so I prepared 3 images that says for themselves. Hope will have detailed explanations and different tuning things for this soon.

Overlapping case, not good for parallelization


Free to go with two threads case

Master-Slave schema for algorithm


No comments


Question about AutoMapper Performance

February 28, 2010 .NET, AutoMapper, Frameworks 4 comments

If you haven’t read my previous article on AutoMapper, please read it first.

After I’ve posted this article one guy was really concerned about the performance of AutoMapper.
So, I have decided to measure execution time of the AutoMapper mapping and manual mapping code.

First of all I have code which returns me 100000 almost random Customers which goes to the customers list.

Measurement of AutoMapper mapping time:

            stopwatch.Start();
            var autoMapperCVI = new List<CustomerViewItem>();
            foreach (var customer in customers)
            {
               autoMapperCVI.Add(Mapper.Map<Customer,CustomerViewItem>(customer));
            }
            stopwatch.Stop();
            Console.WriteLine(string.Format(“AutoMapper: {0}”, stopwatch.ElapsedMilliseconds));

Measurement of the manual mapping time:

            stopwatch.Start();
            var manualCVI = new List<CustomerViewItem>();
            foreach (var customer in customers)
            {
                var customerViewItem = new CustomerViewItem()
                                           {
                                               FirstName = customer.FirstName,
                                               LastName = customer.LastName,
                                               FullName = customer.LastName + ” “ + customer.FirstName,
                                               DateOfBirth = customer.DateOfBirth.ToLongDateString(),
                                               CompanyName = customer.Company.Name,
                                               NumberOfOrders = customer.NumberOfOrders,
                                               VIP = customer.VIP ? “Y” : “N”
                                           };
                manualCVI.Add(customerViewItem);
            }
            stopwatch.Stop();           
            Console.WriteLine(string.Format(“Manual Mapping: {0}”, stopwatch.ElapsedMilliseconds));

I ran my tests many times and one of the possible outputs could be:

AutoMapper: 2117
Manual Mapping: 293

It looks like manual mapping is 7 times faster than automatical. But hey, it took 2 sec to map handrend thouthands of customers.

It is one of the situations where you should decide if the performance is so critical for you or no. I don’t think that there are a lot of cases when you really need to choice manual mapping exactly because of performance issue.


4 comments


I wanna be not so noisy and decoupled to AutoMapper

February 27, 2010 .NET, AutoMapper, Clean Code 2 comments

If you haven’t read my previous article on AutoMapper, please read it first.

I just read an article on the noisiness which could be introduced with AutoMapper.

My Mapping Code is Noisy

Accordingly to that article my code:

   CustomerViewItem customerViewItem = Mapper.Map<CustomerCustomerViewItem>(customer);

is noisy code.

Why?

In few words that is because I’m coupled to the specific mapping engine and because I’m providing too much unneeded information on about how to map my objects.
Take a look on that line once again: we used Customer , but we already have customer, so we can know its type.


What to do to make it not so noisy?


In that article, that I pointed, there is proposition to change my code to:


            CustomerViewItem customerViewItem = customer.Map<CustomerViewItem>();


So, I’ve got interested to apply it to my previous AutoMapper article’s code. As you already caught Map is just extension method for my Customer  class.


Here it is:

    public static class MappingExtentions
    {
        public static TDest Map<TDest>(this Customer customer)
        {
            return Mapper.Map<Customer, TDest>(customer);
        }
    }

Everything becomes simple if you are trying it.


2 comments