Zināšanu bāze

LINQ satiek PLINQ

Sveicināti,

Šoreiz īsi par Microsoft .NET Framework 4.0 paralēlisma iespēju strādājot ar LINQ izteiksmēm. Par pamatu paņēmu savu rakstu (Izstrādātāja ikdienas, klasiskā uzdevuma atrisinājums ar LINQ iespējām).

Tātad, vispirms īri par uzdevumu. Man ir teksta fails ar datiem, kurus var pārveidot struktūrā.

Izveidoju metodi, kas veiks teksta rindas apstrādi un pārveido to vārdnīcā ar atslēgu. Par atslēgu kalpo LineTags pārskaitījums(Enum).

public static Dictionary<LineTags, string> ParseLine(this string line)
{
    return line.Split('\t').Select(
        (s, index) => new { Tag = (LineTags)index, Substring = s }).ToDictionary(
            c => c.Tag, c => c.Substring);
}

Šādi izskatās kods, kas izsauc datu apstrādi

var lines = new[] { "Andrejs M.\t1\tmale", "Peteris L.\t2\tmale", "Ivars A.\t3\tmale"};

var result = (from l in lines select l.ParseLine());

foreach (var s in result.Select(i => i[LineTags.FullName]))
{
    Debug.WriteLine(s);
}

Koda izpildes rezultāts tiek izvadīts Visual Studio atkļūdošanas ekrānā. Piemērs:

Andrejs M.
Peteris L.
Ivars A.

Datu apstrāde šinī gadījumā tiek veikta secīgi, viena pavediena ietvaros (To var redzēt pēc izvades rezultāta). Gadījumā, ja man datorā ir 4 kodolu procesors, tiek izmantots tikai viens kodols. Tātad, uz lieliem datu apjomiem netiek optimāli izmantota vasa pieejamā veiktspēja. Svarīgi ir nodrošināt iespēju veikt darbības paralēli(Paralēlisms), kopējo datu apjomu apstrādāt vairākos pavedienos(izmantojot visus sistēmā pieejamos kodolus).

Paralēlisma programmēšana līdz nesenam laikam bija apgrūtinoša! Skatoties uz kopējo izstrādātāju skaitu, tikai nedaudzie pārzina šo te apgabalu. Puiši no Microsoft balstoties uz uzkrātās pieredzes pacentās maksimāli paslēpt visus šos specifiskos momentus, vienkāršojot paralēlisma izmantošanu.

Atgriežoties pie piemēra. Lai sāktu pielietots visu paralēlisma spēku ir jāizmaina tikai viena koda rindiņa!

var result = (from l in lines.AsParallel() select l.ParseLine());
Ivars A.
Andrejs M.
Peteris L.

Atkļūdošanas ekrānā ieraksti izvadīsies nenoteiktā secībā. Tā ir laba pazīme, kas liecina par paralēlo apstrādi. Arī kodā sastopama metodes AsParallel par to liecina. Kāpēc vispār jāizsauc šī metode? Vienkāršoti sakot, LINQ darbojās ar kolekcijām balstoties uz paplašinošām metodēm,  galvenais lai tiek kolekcijas atbalsta IEnumerable interfeisu. Ar metodes AsParallel starpniecību tiek atgriezts objekts ar tipu ParallelQuery<T>. Speciāli šim te jaunajam tipam tiek nodublētas paplašinošās metodes no LINQ. Tādā veidā saglabājot visu līdz šim eksistējošu funkcionalitāte, nav nepieciešamības pārrakstīt vaicājumus.

Cerams noderēs!

 


Posted May 05 2009, 10:52 AM by andrejs.mamontovs

Comments

valdis.iljuconoks wrote re: LINQ satiek PLINQ
on 05-05-2009 21:57

patīk pievienotā abstrakcija, ka par to, cik daudz pavedienus izdalīt, lai CPU neaizrijas ar kontekstu switchošanu, ir atstāts runtime ziņā un izstrādātājam par to nav jādomā :)

andrejs.mamontovs wrote re: LINQ satiek PLINQ
on 05-05-2009 22:39

Jā,  ļoti svarīgi lai paralēlisma iespēja būti viegli lietojama. Jāpiemin, ka pastāv arī opcijas,  bet tās ir vērts apskatīt, kad būs pieejams gala produkts.

Mei wrote re: LINQ satiek PLINQ
on 02-29-2012 10:16

Gr?ti spirest vai lab?ks...pierasts, tas gan! :) Varb?t bloga administratori var?tu izdom?t k?das ?pasas atlaides blogiem, kuri paties?m eksist? un ir apmekl?ti?Grib?tos jau cer?t, jo p?ksni main?t adresi ar? negribas....Tad viss atkal j?s?k no s?kuma...:(

Gangbuntu wrote re: LINQ satiek PLINQ
on 03-02-2012 4:06

Err... Is it nrlmaoly provided in any other performance comparison? Is there a single performance score in our tests we can use? What should be done with differently priced editions of each product?In short, price / performance is actually a very subjective number. IMO, even this question is more meaningful: let's say you get 30% better performance for X$ (fixed amount of money) for your Windows Azure application requiring you to pay C$ / month in average. How much time would it take to return the initial investment of X$?

google plus hangouts wrote re: LINQ satiek PLINQ
on 03-24-2012 8:18

usPtj5 Looking forward to reading more. Great post.Really thank you!

cheap oem software wrote re: LINQ satiek PLINQ
on 05-02-2012 10:44

vB38bc Great blog post. Great.

Seo Services wrote re: LINQ satiek PLINQ
on 07-13-2012 22:03

ItbJhn Im thankful for the blog post.Thanks Again. Really Cool.

click here wrote re: LINQ satiek PLINQ
on 08-08-2012 19:30

OnXdT5 I cannot thank you enough for the blog article.Really looking forward to read more. Much obliged.

cheap bookmarking service wrote re: LINQ satiek PLINQ
on 09-12-2012 3:00

38XT8k Great article.Thanks Again. Much obliged.

crork wrote re: LINQ satiek PLINQ
on 09-24-2012 15:19

L0K7xF Im thankful for the blog post.Really thank you! Great.

archeage gold wrote re: LINQ satiek PLINQ
on 02-26-2013 12:57

Wow, that's what I was searching for, what a information! present here at this webpage, thanks admin of this web site. archeage gold www.cheap-archeage-gold.com

nikerow.com wrote re: LINQ satiek PLINQ
on 03-04-2013 7:17

Fancy is frail with delivery, yet it gets bigger more substantial as they age when it is adequately fed. nikerow.com http://www.nikerow.com/

casquette obey wrote re: LINQ satiek PLINQ
on 03-15-2013 19:33

All of the the worst approach let pass a partner shall be taking up space correctly by these animals once you learn it's possible to‘t encourage them. casquette obey http://www.b77.fr/

casquette los angeles wrote re: LINQ satiek PLINQ
on 03-22-2013 8:42

If you'd prefer a good sales of any worth, include friends and family. casquette los angeles http://e99.fr/

e22.fr wrote re: LINQ satiek PLINQ
on 03-23-2013 23:28

Some pal are not partner, then again partner will be an absolute pal. e22.fr http://e22.fr/

usine23 wrote re: LINQ satiek PLINQ
on 03-24-2013 16:15

Exactly you will find there's partnership without requiring like, it will have like without requiring partnership. usine23 http://e55.fr/

coach handbags outlet wrote re: LINQ satiek PLINQ
on 04-05-2013 8:01

Enjoyment is the aromatise it is impossible rain cats and dogs to other companies and it doesn't involve finding a a small amount of declines to that you are. coach handbags outlet www.coachoutletcoupon66.com

tee shirt femme wrote re: LINQ satiek PLINQ
on 04-06-2013 21:55

A honest neighbor is certainly one people who overlooks ones own outages also tolerates ones own positive results. tee shirt femme http://www.footcenterfr.fr/

veste cuir wrote re: LINQ satiek PLINQ
on 04-15-2013 17:03

Assume‘r rubbish your schedule on the woman/ladies,what individuals isn‘r able to rubbish your effort you. veste cuir http://www.marquefr.fr/

Add a Comment

(obligāts)  
(brīvizvēles)
(obligāts)  
Remember Me?