course news - university of washingtoncourses.cs.washington.edu/courses/csep590a/17sp/...dan...
TRANSCRIPT
5/16/17
1
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSEP590–ProgrammingSystemsUniversityofWashington
Lecture6:Potpourri
MichaelRingenburgSpring2017
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CourseNews• PresentaJons
– Startnextweek!– Schedulepostedoncourseweb
• Today–mixofinteresJngtopicsthatwehaven’tcoveredyet– TypeChecking– LoopParallelism– JVMandJITcompilaJon– QueryopJmizaJon,ifJmepermits
• FinalHomeworkpostedtoday– Dueendofquarter(June2)
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 1
5/16/17
2
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 2
TypesfromtheCompiler’sPerspec3ve
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Types
• Typesplayakeyroleinmostprogramminglanguages.E.g.,– Run-Jmesafety– Compile-JmeerrordetecJon– Improvedexpressiveness(inheritance,overloading,etc)
– ProvideinformaJontoopJmizer• Stronglytypedlanguages–whatdatamightbeusedwhere
• Typequalifiers(e.g.,constandrestrictinC)
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 3
5/16/17
3
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
TypeCheckingTerminology
StaJcvs.dynamictyping•staJc:checkingdonepriortoexecuJon(e.g.compile-Jme)•dynamic:checkingduringexecuJon
Strongvs.weaktyping•strong:guaranteesnoillegaloperaJonsperformed•weak:can’tmakeguarantees
static dynamic
strong Java, ML Scheme, Ruby
weak C PERL
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 4
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
TypeSystems
• BaseTypes– Fundamental,atomictypes– Typicalexamples:int,double,char,bool
• Compound/ConstructedTypes– Builtupfromothertypes(recursively)viaconstructors
– Constructorsincludearrays,records/structs/classes,pointers,enumeraJons,funcJons,modules,…
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 5
5/16/17
4
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
TypesvsASTs• TypesarenottypicallyASTnodes
– ASTnodesofenhaveatypefield,however• AST=abstractrepresentaJonofsourceprogram(includingsourceprogramtypeinfo)
• Types=abstractrepresentaJonoftypesemanJcsfortypechecking,inference,etc.– CanincludeinformaJonnotexplicitlyrepresentedinthesourcecode,ormaydescribetypesinwaysmoreconvenientforprocessing
• Needaseparate“type”classhierarchyinyourcompilerdisJnctfromtheAST
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 6
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
BaseTypes
• Foreachbasetype(int,boolean,etc),cancreateasingleobjecttorepresentit– BasetypesinsymboltableentriesandASTnodesaredirectreferencestotheseobjects
– Basetypeobjectsusuallycreatedatcompilerstartup• Usefultocreateatype“void”objecttotagfuncJonsthatdonotreturnavalue
• Alsousefultocreateatype“unknown”objectforerrors– (“void”and“unknown”typesreducetheneedforspecialcasecodeinvariousplacesinthetypechecker)
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 7
5/16/17
5
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CompoundTypes
• Basicidea:useaappropriate“typeconstructor”objectthatreferstothecomponenttypes– Limitednumberofthese–corresponddirectlytotypeconstructorsinthelanguage(record/struct/class,array,funcJon,…)
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 8
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
ArrayTypes• ForregularJavathisissimple:onlypossibilityis#ofdimensionsandelementtypeclassArrayTypeextendsType{ intnDims; TypeelementType;}
• Lengthnotpartoftype• MoreinteresJnginlanguageslikePascal(morecomplexarrayindexing–indextypes!)
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 9
5/16/17
6
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Methods/FuncJons
• Typeofamethodisitsresulttypeplusanorderedlistofparametertypes
classMethodTypeextendsType{ TyperesultType; //typeor“void” ListparameterTypes;}
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 10
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
ClassTypes
• Typefor:classId{fieldsandmethods}classClassTypeextendsType{TypebaseClassType;//reftobaseclassMapfields; //typeinfoforfieldsMapmethods; //typeinfoformethods(later)
}– Baseclasspointer,sowecancheckfieldreferencesagainstbaseclass
ifwedon’tfindinthisclass.– (Note:maynotwanttodothisliterally,dependingonhowclass
symboltablesarerepresented;i.e.,classsymboltablesmightbeusefulorsufficientastherepresentaJonoftheclasstype.)
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 11
5/16/17
7
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
TypeEquivalance
• Forbasetypesthisissimple– Ifyouhavejustasingleinstanceofeachbasetype(asrecommend),thentypesarethesameifandonlyiftheyareidenJcal
• Pointer/referencecomparisoninthetypechecker– NormallytherearewelldefinedrulesforcoercionsbetweenarithmeJctypes
• Dependingonlanguagerules,compilerinsertstheseautomaJcallyorwhenrequestedbyprogrammer(casts)–ofeninvolvesinserJngcast/conversionnodesinAST
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 12
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
TypeEquivalenceforCompoundTypes
• Twobasicstrategies– Structuralequivalence:twotypesarethesameiftheyarethesamekindoftypeandtheircomponenttypesareequivalent,recursively
• E.g.,twostructtypes,eachwithexactlytwointfields– Nameequivalence:twotypesarethesameonlyiftheyhavethesamename.Iftheirstructuresmatch,buthavedisJnctnames,theyarenotequal.
• Differentlanguagedesignphilosophies– Mixiscommon,e.g.,C/C++nameforstructs/classes,structuralotherwise.
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 13
5/16/17
8
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
StructuralEquivalence
• Structuralequivalencesaystwotypesareequalifftheyhavesamestructure– IdenJcalbasetypesclearlyhavethesamestructure– iftypeconstructors:
• sameconstructor• recursively,equivalentargumentstoconstructor
• Ex:atomictypes,arraytypes,pointertypes• ImplementwithrecursiveimplementaJonofequals
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 14
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
NameEquivalence
• Nameequivalencesaysthattwotypesareequalifftheycamefromthesametextualoccurrenceofatypeconstructor– Ex:classtypes,Cstructtypes(structtagname),datatypesinML
– specialcase:typesynonyms(e.g.typedefinC)donotdefinenewtypes–usesstructuralequivalence
• Implementwithpointer/referenceequalityassumingappropriaterepresentaJonoftypeinfo
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 15
5/16/17
9
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
TypeEquivalenceandInheritance
• SupposewehaveclassBase{…}classExtendedextendsBase{…}
• AvariabledeclaredwithtypeBasehasacompile-6metypeofBase
• DuringexecuJon,thatvariablemayrefertoanobjectofclassBaseoranyofitssubclasseslikeExtended(orcanbenull)– SomeJmescalledtherun6metype– Subclassesguaranteedtohaveallfields/methodsofbaseclass,sotypecheckingasbaseclasssuffices
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 16
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
TypeCasts
• Inmostlanguages,onecanexplicitlycastanobjectofonetypetoanother– someJmescastmeansaconversion(e.g.,castsbetweennumerictypes)
– someJmescastmeansachangeofstaJctypewithoutdoinganycomputaJon(castsbetweenpointertypesorpointerandnumerictypes)
– Withclasstypes,mayalsomeanupcast(free)ordowncast(runJmecheck)s
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 17
5/16/17
10
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
TypeConversionsvsCoercions
• InJava,wecanexplicitlyconvertanvalueoftypedoubletooneoftypeint– Canrepresentasunaryoperator– Typecheck,generatecodenormally
• InJava,canimplicitlycoerceanvalueoftypeinttooneoftypedouble– Compilermustinsertunaryconversionoperators,basedonresultoftypechecking
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 18
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CandJava:typecasts• InC:safety/correctnessofcastsnotchecked
– AllowswriJnglow-levelcodethat’stype-unsafe– ResultisofenimplementaJondependent/undefined.Notportable,butsomeJmesuseful.
• InJava:downcastsfromsuperclasstosubclassneedrun-Jmechecktopreservetypesafety– Otherwise,mightusefield(orcallmethod)thatisnotpresentinsuperclass
– StaJctypecheckerallowsthecast– Codegeneratorintroducesrun-Jmecheck– Java’smainformofdynamictypechecking
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 19
5/16/17
11
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
FinalNote:VariousNoJonsofEquivalance
• ThereareusuallyseveralrelaJonsontypesthatcompilerneedstodealwith:– “isthesameas”– “isassignableto”– “issameorasubclassof”– “isconverJbleto”
• Besuretocheckfortherightone(s)
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 20
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
UsefulCompilerFuncJons
• CreateahandfulofmethodstodecidedifferentkindsoftypecompaJbility,e.g.:– TypesareidenJcal– Typet1isassignmentcompaJblewitht2– ParameterlistiscompaJblewithtypesofexpressionsinthecall
• Usualmodularityreasons:isolatesthesedecisionsinoneplaceandhidestheactualtyperepresentaJonfromtherestofthecompiler
• ProbablybelongsinthesamepackagewiththetyperepresentaJonclasses(packagefordealingwithtypes)
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 21
5/16/17
12
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 22
LoopParallelism
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Whatisloopparallelism?
• Aserial(non-parallelized)loopconsistsofaseriesofiteraJonsthatrunoneataJme(inorder)onasinglethread.
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 23
y A “normal” (non-parallelized) loop consists of a series of iterations that run one at a time (in order) on a single thread.
for(i=0; i<N; i++) {foo[i] = bar[i];
}453721
000000
453721
bar foo
y A “normal” (non-parallelized) loop consists of a series of iterations that run one at a time (in order) on a single thread.
for(i=0; i<N; i++) {foo[i] = bar[i];
}453721
000000
453721
bar foo1
2
3
4
5
6
5/16/17
13
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Whatisloopparallelism?
• AparallelizedloopconsistsofaseriesofiteraJonsthatmayrunsimultaneouslyonmulJplethreads.
• EverythreadexecutesadisJnctsubsetoftheiteraJons
• IteraJonsnotordered.
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 24
y A “normal” (non-parallelized) loop consists of a series of iterations that run one at a time (in order) on a single thread.
for(i=0; i<N; i++) {foo[i] = bar[i];
}453721
000000
453721
bar foo
y A “normal” (non-parallelized) loop consists of a series of iterations that run one at a time (in order) on a single thread.
for(i=0; i<N; i++) {foo[i] = bar[i];
}453721
000000
453721
bar foo1
1
2
2
2
1
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
MoJvaJonsforLoopParallelizaJon
• Takeadvantageofavailableparallelisminarchitecture– MulJ-coreandmany-coreprocessors– VectorizaJoninstrucJons– Hyperthreading
• Latencyhiding– SwitchcontextsratherthanwaiJng
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 25
5/16/17
14
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CondiJonsforParallelizaJon
• TypicalnecessarycondiJonsforcompilertoauto-parallelizealoop– ItcanfigureouthowtocomputethenumberofiteraJonspriortoexecuJngtheloop
– ItcanprovethattherearenodependencesbetweeniteraJons
– TherearenofuncJoncallswithunknownsideeffects(e.g.,output)
– Theloophasasimplestructure(e.g.,nomulJpleexits)• UsersmayinsertextrainformaJontohelpthecompilerestablishthatthesecondiJonshold.– Compilerreliesoninfo:iffalse,compiledprogrammaybehaveunepectedly
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 26
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Examples
• Parallelizableloop:
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 27
void foo(int n) { int i; int my_array[n]; for (i = 0; i < n; i++) { my_array[i] = i; } return; }
5/16/17
15
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Examples
• Non-parallelizableloop:
• aandbmaypointtooverlappingmemory:
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 28
void foo(int *a, int *b) { int i; for (i = 0; i < 10000; i++) { a[i] = b[i]; }
}
foo(x, x+5000)
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
HelpingtheCompiler
• Commontypesofhints/informaJon– Thispointerisnotaliasedwithanyotherpointers(doesn’tpointtodatathatoverlapswithanotherpointer).E.g.,restrictkeywordinC
– TherearenodependenciesbetweeniteraJons(loop-carrieddependencies)introducedbythispointer
– Trustme,thisloopcanbeparallelized• OfenbeuertogivethecompilerinformaJonaboutwhyitissafetoparallelize(allowsmoreopJmizaJon–“trustmeit’ssafe”onlysayssafeaswriuen)
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 29
5/16/17
16
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CompilerCanHelpItself,Too
• Ofencompilerswillauempttorestructurecodetofindorenhanceparallelism
• Somecommonexamples– Scalarexpansion– ReducJons– Loopcollapse
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 30
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
ScalarExpansion• Thisloopcannotbeparallelizedaswriuenbecauseof
dependencesbetweenthereadsandwritesoftindifferentiteraJons(wriJngtinoneiteraJonmayoverwritethevalueoftfromanotheriteraJonbeforeitisused):
Spring2017 UWCSEP590(PMPProgrammingSystems):
Ringenburg 31
int t; for (i = 0; i < n; ++i) { t = sqrt(b[i]); ... a[i] = t + 5; }
5/16/17
17
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
ScalarExpansion• Thisloopcannotbeparallelizedaswriuenbecauseof
dependencesbetweenthereadsandwritesoftindifferentiteraJons(wriJngtinoneiteraJonmayoverwritethevalueoftfromanotheriteraJonbeforeitisused):
• CompilercansolvethisbyconverJngthescalarintegertintoanarrayofintegers.
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 32
int t[n]; for (i = 0; i < n; ++i) { t[i] = sqrt(b[i]); ... a[i] = t[i] + 5; }
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
ReducJons• Compilersofenauempttorecognizeloopsthatcalculatesums,products,minimums,andmaximumsoveranarray.E.g.:
• ThecompilercanconvertthesetoreducJons– Eachthreadcomputesthemin/max/sum/productoverasub-secJonofthearray
– Threadsthencombineresultstodeterminethefinalvalue(canusetree-structureforefficiency)
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 33
int min = MAX_VAL; for (i = 0; i < n; i++) { if (x[i] < min) min = x[i]; }
5/16/17
18
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
LoopCollapse
• Howdowehandlenestedparallelloops?• OpJon1:Goparallelfortheouterloop,andthenagainfor
theinnerloop.– Inefficient–thereisasignificantoverheadtogoingparallel.Ifwenest,theneveryiteraJonoftheouterloophastopaythatoverhead.
– MaylimiteffecJvenessoftheloadbalancingobtainedbysomeloopiteraJonschedulingmethods.
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 34
void foo(int* restrict num_bars, int size_x, int* restrict x, int* restrict bar) { for (int i = 0; i < size_x; i++) for (int j = 0; j < num_bars[i]; j++) x[i] += bar[i + j]; }
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
LoopCollapse
• OpJon2:Loopcollapse.ConvertthenestedpairofparallelloopstoasingleparallelloopthatsimulatestheexecuJonofthenestedloops.
• Manhauanstyle,wheninnerloopiteraJonsmayvary– CreateanewparallellooptocalculatethetotalnumberofiteraJonsofthe
innerloop(acrossalliteraJonsoftheouterloop).– ConvertthepairofloopsintoasingleloopwhereeachiteraJoncorresponds
toadisJnctouter/inneriteraJonpair.• IfinnerloopiteraJonsarefixed,asimplerectangularcollapsesuffices
– MiteraJonsnestedinNiteraJons=MxNcollapsedloopiteraJons• BigperformancewinSpring2017 UWCSEP590(PMPProgrammingSystems):
Ringenburg 35
void foo(int* restrict num_bars, int size_x, int* restrict x, int* restrict bar) { for (int i = 0; i < size_x; i++) for (int j = 0; j < num_bars[i]; j++) x[i] += bar[i + j]; }
5/16/17
19
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
ManhauanCollapsePsuedocode
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 36
// t[i] = total # of inner loop iterations // in first i iterations of outer loop t[0] = 0; for (i = 0; i < size_x; i++) t[i + 1] = t[i] + num_bars[i]; for (k = 0; k < t[size_x]; k++) { // Set i to index of largest element of t // less than k (use binary search) // Optimization: Don’t recompute every time i = max_element_less_than(t, k); j = k - t[i]; x[i] += bar[i + j]; // original loop body }
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Example
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 37
bool foo(int *a, int *b, int n, int sought, int *old_val) { int i; for (i = 0; i < n; i++) { if (b[i] == sought) break; a[i] = b[i]; } return (i < n); }
5/16/17
20
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Example
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 38
1 X | for (i = 0; i < n; i++) { ** loop exit ** multiple exits 1 X | if (b[i] == sought) | break; 1 X | a[i] = b[i]; | }
• CompilerfeedbacktoolsofenprovidedtotellyouwhereopJmizaJon/parallelizaJonoccurredordidn’t,andwhy.
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Example
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 39
bool foo(int *a, int *b, int n, int sought, int *old_val) { int i; int found_index = n; for (i = 0; i < n; i++) { if (b[i] == sought) if (i < found_index) found_index = i; } for (int i = 0; i < found_index; i++) a[i] = b[i]; return (found_index < n); }
5/16/17
21
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Example
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 40
| for (i = 0; i < n; i++) { 3 P:$| if (b[i] == sought) ** reduction moved out of 1 loop | if (i < found_index) | found_index = i; | } | for (int i = 0; i < found_index; i++) 4 S | a[i]=b[i];
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Example
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 41
bool foo(int * restrict a, int *b, int n, int sought, int *old_val) { int i; int found_index = n; for (i = 0; i < n; i++) { if (b[i] == sought) if (i < found_index) found_index = i; } for (int i = 0; i < found_index; i++) a[i] = b[i]; return (found_index < n); }
5/16/17
22
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Example
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 42
| for (i = 0; i < n; i++) { 3 P:$| if (b[i] == sought) ** reduction moved out of 1 loop | if (i < found_index) | found_index = i; | } | for (int i = 0; i < found_index; i++) 4 P | a[i]=b[i];
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 43
TheJVMandJIT
5/16/17
23
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Java Implementation Overview
• Java compiler (javac et al) produces machine-independent .class files – Target architecture is Java Virtual Machine
(JVM) – simple stack machine • Java execution engine (java)
– Loads .class files (often from libraries) – Executes code
• Either interprets stack machine code or compiles to native code (JIT)
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 44
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
JVM Architecture • Abstract stack machine
– Bytecodes pop operands, – and push results
• Implementation not required to use JVM specification literally – Only requirement is that
execution of .class files has specified effect
– Multiple implementation strategies depending on goals
• Compilers vs interpreters • Optimizing for servers vs
workstations
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 45
iconst_1
5/16/17
24
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
JVM Architecture • Abstract stack machine
– Bytecodes pop operands, – and push results
• Implementation not required to use JVM specification literally – Only requirement is that
execution of .class files has specified effect
– Multiple implementation strategies depending on goals
• Compilers vs interpreters • Optimizing for servers vs
workstations
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 46
iconst_1
1
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
JVM Architecture • Abstract stack machine
– Bytecodes pop operands, – and push results
• Implementation not required to use JVM specification literally – Only requirement is that
execution of .class files has specified effect
– Multiple implementation strategies depending on goals
• Compilers vs interpreters • Optimizing for servers vs
workstations
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 47
sipush100
1
5/16/17
25
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
JVM Architecture • Abstract stack machine
– Bytecodes pop operands, – and push results
• Implementation not required to use JVM specification literally – Only requirement is that
execution of .class files has specified effect
– Multiple implementation strategies depending on goals
• Compilers vs interpreters • Optimizing for servers vs
workstations
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 48
100
sipush100
1
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
JVM Architecture • Abstract stack machine
– Bytecodes pop operands, – and push results
• Implementation not required to use JVM specification literally – Only requirement is that
execution of .class files has specified effect
– Multiple implementation strategies depending on goals
• Compilers vs interpreters • Optimizing for servers vs
workstations
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 49
100
iadd
1
5/16/17
26
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
JVM Architecture • Abstract stack machine
– Bytecodes pop operands, – and push results
• Implementation not required to use JVM specification literally – Only requirement is that
execution of .class files has specified effect
– Multiple implementation strategies depending on goals
• Compilers vs interpreters • Optimizing for servers vs
workstations
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 50
iadd
101
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
JVM Data Types
• Primitive types – byte, short, int, long, char, float, double,
boolean
• Reference types – Non-generic only (more on this later)
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 51
5/16/17
27
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
JVM Runtime Data Areas
• Semantics defined by the JVM Specification – Implementer may do anything that preserves
these semantics • Per-thread data
– pc register – Stack
• Holds frames (details below) • May be a real stack or may be heap allocated
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 52
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
JVM Runtime Data Areas
• Per-VM data – shared by all threads – Heap – objects allocated here – Method area – per-class data
• Runtime constant pool • Field and method data • Code for methods and constructors
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 53
5/16/17
28
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Frames
• Created when method invoked; destroyed when method completes
• Allocated on stack of creating thread • Contents
– Local variables – Operand stack for JVM instructions – Reference to runtime constant pool
• Symbolic data that supports dynamic linking
– Anything else the implementer wants
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 54
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
JVM Instruction Set
• Stack machine • Byte stream • Instruction format
– 1 byte opcode – 0 or more bytes of operands
• Instructions encode type information – Verified when class loaded
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 55
5/16/17
29
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Instruction Sampler
• Load/store – Transfer values between local variables and
operand stack – Different opcodes for int, float, double,
addresses – Load, store, load immediate
• Special encodings for load0, load1, load2, load3 to get compact code for first few local vars
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 56
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Instruction Sampler
• Arithmetic – Again, different opcodes for different types
• byte, short, char & boolean use int instructions – Pop operands from operand stack, push result
onto operand stack – Add, subtract, multiply, divide, remainder,
negate, shift, and, or, increment, compare • Stack management
– Pop, dup, swap
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 57
5/16/17
30
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Instruction Sampler
• Type conversion – Widening – int to long, float, double; long to
float, double, float to double – Narrowing – int to byte, short, char; double to
int, long, float, etc.
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 58
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Instruction Sampler
• Object creation & manipulation – New class instance – New array – Static field access – Array element access – Array length – Instanceof, checkcast
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 59
5/16/17
31
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Instruction Sampler
• Control transfer – Unconditional branch – goto – Conditional branch – ifeq, iflt, ifnull, etc. – Compound conditional branches - switch
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 60
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Instruction Sampler
• Method invocation – invokevirtual – invokeinterface – invokespecial (constructors, superclass, private) – invokestatic
• Method return – Typed value-returning instructions – Return for void methods
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 61
5/16/17
32
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
BytecodeExample
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 62
outer:for(inti=2;i<1000;i++){for(intj=2;j<i;j++){if(i%j==0)con3nueouter;}System.out.println(i);}
0:iconst_21:istore_12:iload_13:sipush10006:if_icmpge449:iconst_210:istore_211:iload_212:iload_113:if_icmpge3116:iload_1…
17:iload_218:irem19:ifne2522:goto3825:iinc2,128:goto1131:getsta3c#84;System.out34:iload_135:invokevirtual#85;println38:iinc1,141:goto244:return
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Execution Engines
• Basic Choices – Interpret JVM bytecodes directly – Compile bytecodes to native code, which then
executes on the native processor • Just-In-Time compiler (JIT)
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 63
5/16/17
33
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
JITLevels
• C1:Fast,simplelight-weightopJmizaJons– Ofenusedforshortercodes(“client”mode)
• C2:MoreaggressiveopJmizaJon,significantlyslower– Ofenusedforlongrunningcodes(“server”mode)
• Butbothcauseoverheadwheninvoked
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 64
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
JITProfiling
• JITcompilaJontypicallyprofile-driven– CompilaJon(evenC1)hasacost– CountexecuJonsofmethods,idenJfyhotloopnests– JITonlyhotcode
• TieredJIT– MulJplehot-nessthresholds– Uselight-weightJIT(C1)oncehitlowerthreshold– Paycostofheavy-weightJIT(C2)ifhithigherthreshold
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 65
5/16/17
34
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
SpeculaJveJIT• ProfilingcandomorethanjustidenJfycodeblocksexecuted
– Certainbranchesalways/nevertaken– Actualtypesused/methodimplementaJonscalled– DetectifNULLpointersneverpassed– Etc…
• CanspeculaJvelyopJmizebasedonprofile– Removeunusedbranches– InlineparJcularimplementaJonsofvirtualmethods– RemoveNULLchecks
• MustdetectandbackoffifspeculaJonincorrect– Detectvia“guards”,e.g.,checkingtypeorcondiJon,handlingSIGSEGV– “DeopJmizaJon”…switchbacktointerpreter– Switchingbackcanbeexpensive
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 66
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
SpeculaJonExample
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 67
o = receiver object ; x = receiver class (o) ; if ( x == expected-class ) { // virtual guard x.foo(a, b, c); // direct call can be inlined } else { o.foo(a, b, c); // guard failed, virtual call }
o.foo(a, b, b);
FromIBMJust-In-TimeCompiler(JIT)forJava:BestpracJcesandcodingguidelinesforimprovingperformance
5/16/17
35
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Escape Analysis
• Another optimization based on observation that many methods allocate local objects as temporaries
• Idea: Compiler tries to prove that no reference to a locally allocated object can “escape” – Not stored in a global variable or object – Not passed as a parameter
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 68
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Using Escape Analysis
• If all references to an object are local, it doesn’t need to be allocated on the heap in the usual manner – Can allocate storage for it in local stack frame
• Essentially zero cost
– Still need to preserve the semantics of new, constructor, etc.
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 69
5/16/17
36
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
OtherTechniques• SaveprofileinformaJonfrompreviousexecuJons– CanalsosaveJIT’edresults– Eliminate“warm-up”– Possiblybeuerprofileinfothanfakewarm-upssomeJmesemployed
• AzulFalcon:recentlyannouncedJITusingLLVM– Takeadvantageofpowerfulopensourcecompiler– MoreopJmizaJonspotenJallyavailable– MoreprocessorspecificopJmizaJons–e.g.,vectorizaJoninstrucJons
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 70
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 71
SQLQueryOp3miza3on(ExcerptsfromSparkSummitCatalystOp3mizerDeepDive,SparkSummit2016)