《并行程序设计导论》-第五章.pptx
Chapter5SharedMemoryProgrammingwithOpenMPAnIntroductiontoParallelProgrammingPeterPacheco
RoadmapWritingprogramsthatuseOpenMP.UsingOpenMPtoparallelizemanyserialforloopswithonlysmallchangestothesourcecode.Taskparallelism.Explicitthreadsynchronization.Standardproblemsinshared-memoryprogramming.#ChapterSubtitle
OpenMPAnAPIforshared-memoryparallelprogramming.MP=multiprocessingDesignedforsystemsinwhicheachthreadorprocesscanpotentiallyhaveaccesstoallavailablememory.SystemisviewedasacollectionofcoresorCPU’s,allofwhichhaveaccesstomainmemory.
Asharedmemorysystem
PragmasSpecialpreprocessorinstructions.Typicallyaddedtoasystemtoallowbehaviorsthataren’tpartofthebasicCspecification.Compilersthatdon’tsupportthepragmasignorethem.#pragma
gcc?g?Wall?fopenmp?oomp_helloomp_hello.c./omp_hello4compilingrunningwith4threadsHellofromthread0of4Hellofromthread1of4Hellofromthread2of4Hellofromthread3of4Hellofromthread1of4Hellofromthread2of4Hellofromthread0of4Hellofromthread3of4Hellofromthread3of4Hellofromthread1of4Hellofromthread2of4Hellofromthread0of4possible
outcomes
OpenMppragmas#pragmaompparallel
Mostbasicparalleldirective.Thenumberofthreadsthatrun
thefollowingstructuredblockofcode
isdeterminedbytherun-timesystem.
Aprocessforkingandjoiningtwothreads
clauseTextthatmodifiesadirective.Thenum_threadsclausecanbeaddedtoaparalleldirective.Itallowstheprogrammertospecifythenumberofthreadsthatshouldexecutethefollowingblock.#pragmaompparallelnum_threads(thread_count)
Ofnote…Theremaybesystem-definedlimitationsonthenumberofthreadsthataprogramcanstart.TheOpenMPstandarddoesn’tguaranteethatthiswillactuallystartthread_countthreads.Mostcurrentsystemscanstarthundredsoreventhousandsofthreads.Unlesswe’retryingtostartalo