
loop nest optimization

sanganak vidnyanat aanee visheshtah sankalak rachnet, loop nest optimization (ell. aine. oo.) heey eka optimization tantra aahe jeey sthanik optimization kinwa samantar kinwa loop nestchya loop overhead qamee karnyachya uddeshane loop parivartanancha sanch lagoo karate. (jevha eka loop dusya loopchya aat asato tevha nested loops udbhavtat.) kahee samanya rekheey bijganita algoridamsathi cashechya punarvaparamule aavashyak asaleli smrutee pravesh vilambata kinwa cashey bandwidth qamee karane haah eka shastriy vapar aahe.
heey optimization tayar karnyasathi vaparlya jaanarya tantrala loop tiling mhanatat, jyala loop blocking kinwa strip mine aanee interchange asehi mhanatat.
vihangavalokan.
loop tiling loopchya punaravrutti jaageche lahan bhaganmadhye kinwa bloxmadhye vibhajan karate, jenekarun loopmadhye vaparlela deta punha vaparlya jaiparyant cashemadhye raheel yachee khatri karnyat madat hoil. loop punaravrutti jaageche vibhajan kelyane mothya arreche lahan bloxmadhye vibhajan hote, asha prakare pravesh kelelya arrey ghatakanna cashechya aakarat basavale jaate, cashecha punarvapar vadhavala jaato aanee cashechya aakarachya aavashyakta door kelya jaatat.
eka samanya loop
(i = 0; i <n; + + i) sathee {
blockchya jaagi blockcha aakar buub vaparoon block keela jaau shakato. 
(jeey = 0; jeey <aine; jeey + = bii) sathee {
 (i = j; i <kiman (n, j + b); + + i) sathee {
jethe cocew _ 1 heey tyache kiman yuktivad parat karnare karya aahe.
udaharaneh matricks-vector gunakar.
khaleel udaharan matricks vector gunakarache aahe. teen arrey aahet, pratyekamadhye 100 ghatak aahet. code arreche lahan aakarat vibhajan karat nahee.
 int i, j, a [100] [100], b [100], c [100];
 int aine = 100;
 (i = 0; i <n; i + +) sathee {
 c [i] = 0;
 (jeey = 0; jeey <aine; jeey + +) sathee {
 c [i] = c [i] + a [i] [j] * b [j];
2 * 2 thokalyancha vapar karoon loop tiling lavlyanantar, code asey disateh
 int i, j, x, y, a [100] [100], b [100], c [100];
 int aine = 100;
 (i = 0; i <n; i + = 2) sathee {
 c [i] = 0;
 c [i + 1] = 0;
 (jeey = 0; jeey <aine; jeey + = 2) sathee {
 (x = i; x <minit (i + 2, n); x + +) sathee {
 (y = j; y <minit (j + 2, n); y + +) sathee {
 c [x] = c [x] + a [x] [y] * b [y];
mool loop punaravrutti jaaga "aine" bhagile "aine" aahe. arrey a [i, j] chaa pravesh kelela bhag dekhil "n" bhagile "n" aahe. jevha "aine" khoop motha asato aanee yantracha cashey aakar khoop lahan asato, tevha eka loop punaravruttitil pravesh kelele arrey ghatak (udaharanarth, cocksey _ 2, cocksey _ 3) cashey resha olandu shakatat, jyamule cashey chukate.
farasbandicha aakar.
eka loopsathi tyling aakarache konate moolya ishtatam aahe heey tharvane nehmich sope nasate kaaran teey loopmadhil pravesh kelelya arrey kshetrancha aanee lakshya yantrachya cashechya aakaracha achook andaj magate. loop nestscha kram (loop interchange) dekhil cashechi changli kamgiri sadhya karnyat mahattvachi bhoomika bajavate. spasht avarodhanasathi yaa ghatakanchya aadhare tilecha aakar nivadane aavashyak aahe. yaulat, cashey-oblivius algoridam spasht avarodhit naan karata cashecha karyaksham vapar karnyasathi tayar kele aahet.
udaharaneh matricks gunakar.
sangankanvar anek mothya ganiteey karye tyancha barachsa vel matricks gunakar karnyat ghalavatat. operation aaheh
jethe a, b aanee c heey n × n arrey aahet. khaleel varnanasathi, vargani, cockoge _ 4 swarupat aahet.
moolbhut loop aaheh
int aay, jeey, keey;
(i = 0; i <n; + + i) sathee
 (jeey = 0; jeey <aine; + + jeey) sathee
 c [i] [j] = 0;
 (k = 0; k <n; + + k) sathee
 c [i] [j] + = a [i] [k] * b [k] [j];
sodavanyasathi teen samasya aaheth
mool loop eka velee nikal matrixmadhil eka nondinsathi nikalachi ganana karato. ekach velee nondinchya chhotya khandachi ganana karoon, khaleel loop pratyek bharit mulyacha donada punarvapar karato, jenekarun aateel loopmadhye chaar bhaar aanee chaar gunakar-jodya asateel, asha prakare samasya sodavata yeil #2. ekach velee chaar sanchayak gheuun, haah code 4 chya vilambasah ekach floating point yojak jawaljawal nehmich vyast thevu shakato (samasya #1). tathapi, code tisya samasyeche nirakaran karat nahee. (n visham asatana aavashyak asalelya swachchatechya kamakadehi teey laksh dett nahee. pudhil charchetoon asey tapasheel vagalale jaatil.)
(i = 0; i <n; i + = 2) sathee
 (jeey = 0; jeey <aine; jeey + = 2) sathee
 ac00 = ac01 = ac10 = ac11 = 0;
 (k = 0; k <n; k + +) sathee
 a. c. 00 + = b [k] [j + 0] * a [i + 0] [k];
 ac01 + = b [k] [j + 1] * a [i + 0] [k];
 ac10 + = b [k] [j + 0] * a [i + 1] [k];
 ac11 + = b [k] [j + 1] * a [i + 1] [k];
 c [i + 0] [j + 0] = ac00;
 c [i + 0] [j + 1] = ac01;
 c [i + 1] [j + 0] = ac10;
 c [i + 1] [j + 1] = ac11;
yaa kodmadhye cocksey _ 5 aanee cocksey _ 6 punaravrutti donhi done ghatkandware avarodhit kelya aahet aanee parinami done-punaravrutti aateel loop purnapane unrolled aahet.
haah code vaay-emm. pii. (1980 chya dashakachya suruvatila tayar kelela) yaa craver agadi swikararhpane chalato, joou mukhya memarimadhye prati memory operation 0.8 gunakar-jodani tikavoon thevu shakato. 2003 madhye tayar jhalelya 2.8 gigahertz pentium 4 sarkhya yantramadhye thodi qamee smrutee bandwidth aanee khoopach changla floating point aahe, jenekarun teey prati smrutee operation 16.5 gunakar-jodani tikavoon thevu shakel. parinami, vareel code 166 megahertz vaay-mpchya tulanet 2.8 gigahertz pentium 4 varr haloo chalel!
jaast lamb taranganara bindu asalelya kinwa anek jodanare vilambata asalelya yantrala samantar chalanyasathi adhik sanchayakanchi aavashyakta asel. 2x2 blockaivaji 3x3 blockchi ganana karnyasathi vareel loop badalane sope aahe, parantu parinami code nehmich vegwan nasato. sanchayike aanee bharit aanee punha vaparleli aii aanee bii moolye donhi thevanyasathi loopsathi nondvastu aavashyak asatat. 2x2 blocksathi 7 nondinchi aavashyakta asate. 3x3 blocksathi 13 chee aavashyakta asate, jeey aay. ess. aii. madhye fakt 8 floating point register asalelya mashinavar kaam karnar nahee. see. pii. yuu. kadey puresha nondi naslyas, sanchayak atirikt bhaar aanee sathyanche velapatrak tayar karel aanee sathyanchya sanchanna sanch slotmadhye takel, jyamule bund kelelya chhotya looppeksha loop haloo chalel.
matricks gunakar haah itar anek sanketanpramanech aahe jyamadhye too memory bandwidthdware maryadit keela jaau shakato aanee adhik register heey compiler aanee programmerla memory bandwidthchi garaj qamee karnyas madat karoo shakatat. haah "nondani daab" mhanunach, samanya hetu x86 aanee 68000 cpus peksha adhik samantar yantre tayar karnyacha hetu asalelya risk cpus chya vikretyanni 32-entry floating-point register fayali swikarlya.
vareel code cashecha farsa vapar karat nahee. see parinamanchya aadvya pattyachya gananedarmyan, aaa chee eka aadvi patti bharali jaate aanee sampoorn matricks bii bharali jaate. sampoorn gananesathi, see ekada sangrahit keela jaato (teey changle aahe), aii ekada cashemadhye loaded keela jaato (cashemadhye beechya pattyasah fitt honari patti gruheet dharoon), parantu bii aine/ib vela loaded keela jaato, jethe ib haah see matrixmadhil patticha aakar aahe, mukhya memarimadhun ekoon aine3/ib duheri shabd lodasathi. vareel kodmadhye, "ib" heey 2 aahe.
smrutichi rahdari qamee karnyasathi pudhil payari mhanje aay. bii. shakya titka motha karane. teey pravahanni nondavalelya "shillak" sankhyepeksha mothe asane aavashyak aahe. yaa udaharanasathi vaparlya jaanarya eka vishisht 2.8 gigahertz pentium 4 pranalichya babatit, shillak sankhya 16.5 aahe. vareel dusya code udaharanala thet vadhavta yet nahee, kaaran tyasathi aanakhi anek sanchayak nondinchi aavashyakta asel. tyaeivaji, loop i varr "avarodhit" aahe. (tantrikdrushtya, pratyakshat hii dusari vel aahe jevha i block keela gela aahe, kaaran prathamach 2 chaa ghatak hota.)
(ii = 0; ii <n; ii + = ib) sathee
 (jeey = 0; jeey <aine; jeey + = 2) sathee
 (i = ii; i <ii + ib; i + = 2) sathee
 ac00 = ac01 = ac10 = ac11 = 0;
 (k = 0; k <n; k + +) sathee
 a. c. 00 + = b [k] [j + 0] * a [i + 0] [k];
 ac01 + = b [k] [j + 1] * a [i + 0] [k];
 ac10 + = b [k] [j + 0] * a [i + 1] [k];
 ac11 + = b [k] [j + 1] * a [i + 1] [k];
 c [i + 0] [j + 0] = ac00;
 c [i + 0] [j + 1] = ac01;
 c [i + 1] [j + 0] = ac10;
 c [i + 1] [j + 1] = ac11;
yaa codes, aay. bii. konatyahi ichchit parametervar seet kele jaau shakate aanee bii matrixchya bharanchi sankhya tyaa ghatkadvare qamee kelee jaeel. yaa swatantryachi kimmat aaheh matrixche aine × ib slices cashemadhye thewale jaat aahet. joparyant teey julate, toparyant haah code memory pranalidware maryadit rahnar nahee.
tarr, konatya aakaracha matricks basato? udaharan pranali, 2.8 gigahertz pentium 4 madhye 16kebby prathamik deta cashey aahe. aay. bii. = 20 saha, yaa kodmadhil matrixcha tukada prathamik kaishepeksha motha asel jevha aine> 100. tyapeksha mothya samasyansathi, aanakhi eka yukti aavashyak aahe.
tee yukti mhanje keey loop avarodhit karoon bii matrixchya pattyacha aakar qamee karane jenekarun pattyacha aakar ib × kebby asel. keey loop avarodhit karane mhanje ekoon memory hastantaranasathi see arey aine/keey. bii. vela loaded aanee sangrahit keela jaeel. hastantaranasathi b ajunahi n/ib vela hastantarit keela jaato. joparyant
yantrachi smrutee pranali floating point unitshee susangat raheel aanee code jastit jaast karyakshamatene chalel. pentium 4 chee 16keey. bii. cashey itaki mothee naheeh tyaeivaji jarr aay. bii. = 24 aanee keey. bii. = 64 nivadle gele, tarr cashecha 12keey. bii. vapar keela jaeel-too purnapane bharane talhane, jeey isht aahe, tyamule see aanee bii arreymadhye pravahit honyasathi thodi jaaga asane aavashyak aahe. hii sankhya processorchya sarvoch floating-point vegachya 20 takkyanchya aat yete.
yethe loop cocksey _ 7 avarodhit kelela code aahe.
(ii = 0; ii <n; ii + = ib) sathee
 (kk = 0; kk <n; kk + = kb) sathee
 (jeey = 0; jeey <aine; jeey + = 2) sathee
 (i = ii; i <ii + ib; i + = 2) sathee
 jarr (kk = = 0)
 ac00 = ac01 = ac10 = ac11 = 0;
 itar
 ac00 = c [i + 0] [j + 0];
 ac01 = c [i + 0] [j + 1];
 aii. see. 10 = see [aay + 1] [jeey + 0];
 aii. see. 11 = see [aay + 1] [jeey + 1];
 (k = kk; k <kk + kb; k + +) sathee
 a. c. 00 + = b [k] [j + 0] * a [i + 0] [k];
 ac01 + = b [k] [j + 1] * a [i + 0] [k];
 ac10 + = b [k] [j + 0] * a [i + 1] [k];
 ac11 + = b [k] [j + 1] * a [i + 1] [k];
 c [i + 0] [j + 0] = ac00;
 c [i + 0] [j + 1] = ac01;
 c [i + 1] [j + 0] = ac10;
 c [i + 1] [j + 1] = ac11;
vareel code udaharane enchya mulyanshi vyavhar karnyache tapasheel darshavat naheet, jeey avarodhak ghatakanche gunakar naheet. loop nest optimization karnare compiler gananechya kadaa swachchh karnyasathi code utsarjit karatat. udaharanarth, bahutek ell. aine. oo. sankalak kadachit keey. keey. = = 0 punaravrutti urvarit keey. keey. see. _ 8 punaravruttipasun vibhajit kartil, keey. keey. see. _ 5 loopmadhun aay. eff. vidhan kadhoon taknyasathi. asha sankalkanchya mulyanpaiki hii eka moolya aaheh yaa optimization sopya prakarananna code karane sope asale taree, kodchi pratikruti tayar aanee rupantarit kelyapramane serve tapasheel yogya thevane hii truti-pravan prakriya aahe.
16keey. bii. ell. 1 cashey aakarasathi avarodhit kelyavar vareel loop udaharan pranalivaril 80 takke sarvoch flop sadhya karel. adhik asantulit smrutee pranali asalelya pranalinvar heey adhik white karel. sudaivane, pentium 4 madhye 256kebby (kinwa modelnusar adhik) uchch-bandwidth star-2 cashey tasech star-1 cashey aahe. eka paryay aaheh
eka vishisht cashechya aakarasathi visheshtah tune karnyaivaji, pahilya udaharanapramane, cashey-oblivius algoridam konatyahi upalabdh cashecha fayda ghenyasathi tayar keela gela aahe, tyacha aakar kahihi asso. upalabdh asalyas heey aapoaap done kinwa adhik staranchya smrutee padanukramacha fayda ghete. matricks gunakarasathi cashey-oblivius algoridam dnyat aahet.