Thursday, July 28, 2016

Running Functions in Parallel with Microsoft .NET

Parallel computing is a type of computation in which many calculations are carried out simultaneously, or the execution of processes are carried out simultaneously.

.NET 4.5 simplifies this parallel development from the following a normal for loop:

       
for (int i = 0; i < 10; i++)
{
     doSomethingFast(i); //1 execution at a time in this loop.
}



into the following:

Parallel.For(0, 10, new ParallelOptions { MaxDegreeOfParallelism = 4 }, delegate (int i)
{
 doSomethingFast(i); //4 execution at a time in this loop.                
});


If you prefer foreach loop syntax then use the following:




       
String[] fileList = System.IO.Directory.GetFiles(@"C:\Users\Public\Pictures\Sample Pictures", "*.jpg");

foreach(string file in fileList)
{
 doSomethingWithFile(file);
}





       
String[] fileList = System.IO.Directory.GetFiles(@"C:\Users\Public\Pictures\Sample Pictures", "*.jpg");

Parallel.ForEach(fileList, new ParallelOptions { MaxDegreeOfParallelism = 10 }, (file) =>
{
 doSomethingWithFile(file);
});





Below is a full sample program with output screenshot for demonstration.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace SimpleParallelProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            //This will call function doSomethingFast in series one at a time.
            Console.WriteLine("This will call function doSomethingFast in series one at a time.");
            executeFunctionSingleSeries();


            Console.WriteLine("\n\nThis will call function doSomethingFast in paralle.");
            //This will call function doSomethingFast in paralle.
            executeFunctionInParalle();

            Console.WriteLine("\n\nThis function will call doSomethingFast and doSomethingFastAtomically in paralle and series respectively.");
            //This function will call doSomethingFast and doSomethingFastAtomically in paralle and series respectively.
            executeFunctionInParalleAndSeries();



            Console.WriteLine("\n\n\nDone, press any key to continue . . . .");
            Console.ReadKey();
        }

        public static void executeFunctionSingleSeries()
        {
            for (int i = 0; i < 10; i++)
            {
                doSomethingFast(i); //1 execution at a time in this loop.
            }
        }
        public static void executeFunctionInParalle()
        {
            Parallel.For(0, 10, new ParallelOptions { MaxDegreeOfParallelism = 4 }, delegate (int i)
            {
                doSomethingFast(i); //4 execution at a time in this loop.                
            });
        }
        private static void executeFunctionInParalleAndSeries()
        {
            Parallel.For(0, 10, new ParallelOptions { MaxDegreeOfParallelism = 4 }, delegate (int i)
            {
                doSomethingFast(i); //4 execution at a time in this loop.                
                doSomethingFastAtomically(i);//1 execution at a time in this loop.
            });
        }




        public static void doSomethingFast(int i)
        {
            Console.WriteLine("doSomethingFast: {0} on thread {1}", i, Thread.CurrentThread.ManagedThreadId);

            //rand sleep time between 1 to 3  seconds
            int randomSleepTime = new Random().Next(1, 3) * 1000;
            Thread.Sleep(randomSleepTime);
        }


        private static readonly object doSomethingFastAtomicallyLock = new object();
        public static void doSomethingFastAtomically(int i)
        {
            lock (doSomethingFastAtomicallyLock)
            {
                Console.WriteLine("updateDatabaseAtomically: {0} on thread {1}", i, Thread.CurrentThread.ManagedThreadId);

                //rand sleep time between 1 to 3  seconds
                int randomSleepTime = new Random().Next(1, 3) * 1000;
                Thread.Sleep(randomSleepTime);
            }
        }
    }
}


Function execution series vs parallel

Thursday, April 21, 2016

2016 Self Improvement - 8 key skills for productivity


Below is quote from Charles Duhigg on eight key tools or skills for productivity. For the full podcast click here

Motivation: We trigger self-motivation by making choices that make us feel in control. The act of asserting ourselves and taking control helps trigger the parts of our neurology where self-motivation resides. 

Focus: We train ourselves how to pay attention to the right things and ignore distractions by building mental models, which means that we essentially narrate to ourselves what’s going on as it goes on around us.

Goal-setting: Everyone actually needs two different kinds of goals. You need a stretch goal, which is like this big ambition, but then you have to pair that with a specific plan on how to get started tomorrow morning.

Decision making: People who make the best decisions tend to think probabilistic-ally. They envision multiple, often contradictory, futures and then try and figure out which one is more likely to occur.

Innovation: The most creative environments are ones that allow people to take clichés and mix them together in new ways. And the people who are best at this are known as innovation brokers. They’re people who have their feet in many different worlds and, as a result, they know which ideas can click together in a novel combination. 

Absorbing data: Sometimes the best way to learn is to make information harder to absorb. This is known in psychology as disfluency. The harder we have to work to understand an idea or to process a piece of data, the stickier it becomes in our brain.

Managing others: The best managers put responsibility for solving a problem with the person who’s closest to that problem, because that’s how you tap into everyone’s unique expertise.
Teams: Who is on a team matters much, much less than how a team interacts.


Wednesday, April 13, 2016

Problem: How many processors, cores, logical processors (Hyper Threading) are running on your workstation ?

Windows "Computer > Properties" and Task Managers is misleading.


Computer > Properties will show 2 CPU when only 1 CPU running on this workstation.
Task Manager will show 8 CPU or Cores.

Solution: 


There are two ways to check for CPU, Cores, and  Hyper Threading.

Download [ CPU-ID ]

If you need to check large number of windows devices then deploy the following VBA or MS-DOS script.


VBA Script



       
strComputer = "." 
strResult = ""
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
Set colItems = objWMIService.ExecQuery( _
    "SELECT * FROM Win32_ComputerSystem",,48) 
For Each objItem in colItems 
    strResult= strResult & "NumberOfProcessors: " & objItem.NumberOfProcessors & vbcrlf
Next


Set objWMIService2 = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
Set colItems = objWMIService2.ExecQuery( _
    "SELECT * FROM Win32_Processor",,48) 
For Each objItem in colItems 
    strResult= strResult & "NumberOfCores: " & objItem.NumberOfCores & vbcrlf
    strResult= strResult & "NumberOfLogicalProcessors: " & objItem.NumberOfLogicalProcessors & vbcrlf
Next

    Wscript.Echo strResult

The script revels the workstation has 1 CPU, 4 cores, with 8 logic processors (Hyper Threading).



MS-DOS Script


       
echo wmic cpu get NumberOfCores, NumberOfLogicalProcessors


The script revels the workstation has 4 cores with 8 logic processors.



Wednesday, September 30, 2015

Fix for Windows Server 2012 Remote Desktop Session Sharing/Shadowing


Windows Server 2012 does now allow remote session sharing or shadowing with Remote Desktop. Remote Desktop Protocol version 8.1 packages have been re-released to address this problem. . RDP 8.1 is available on the Microsoft Download Center. In order to install the Remote Desktop Protocol 8.1 client for Windows 7 SP1,the following updates should be installed in the order shown:


  1. KB 2574819: An update is available that adds support for DTLS in Windows 7 SP1 and Windows Server 2008 R2 SP1 
  2. KB 2857650: Update that improves the RemoteApp and Desktop Connections features is available for Windows 7
  3. KB 2830477: Update for RemoteApp and Desktop Connections feature is available for Windows
  4. KB 2913751: Smart card redirection in remote sessions fails in a Windows 7 SP1-based RDP 8.1 client
  5. (Optional) If you experience connection reliability issues after installing KB 2913751, then installing KB 2923545: Update for RDP 8.1 is available for Windows 7 SP1 .

I wrote the following program that will allow you to list all server user sessions. After that you should be able to share session. 


Best wishes,
Amin