This document discusses parallel programming using C/C++ and provides examples of using MPI and OpenMP. It describes why C/C++ is commonly used for parallel programming, how to compile C/C++ on different platforms for free, and the differences between distributed memory parallelism using MPI versus shared memory parallelism using OpenMP. It also gives short MPI and OpenMP code examples and discusses issues around portability when programming across platforms.
This document discusses parallel programming using C/C++ and provides examples of using MPI and OpenMP. It describes why C/C++ is commonly used for parallel programming, how to compile C/C++ on different platforms for free, and the differences between distributed memory parallelism using MPI versus shared memory parallelism using OpenMP. It also gives short MPI and OpenMP code examples and discusses issues around portability when programming across platforms.
Compiling C/C++ on Windows (for free) Compiling C/C++ on other platforms for free is not an issue Parallel Programming in C/C++ - OpenMP versus MPI MPI Examples OpenMP Examples Project Assessed Work (50%) Why Choose C/C++ as the parallel programming language? It is the language most computer science students and scientists are familiar with. A large number of parallel algorithm collections/sources contains descriptions (implementations) written in C or in C-like languages. C/C++ is closer to the real machine. C/C++ is closer to the real machine. It is nice to use something different from Jaa. NOTE: there exists support for parallel programming using a wide range of languages on a variety of Os! Compiling C/C++ on Windows "for free# If !ou want to compile C/C++ on a windows machine " without pa!ing for it - !ou hae a number of (reasonable) choices# $icrosoft %isual C++ &'(' )*press Limited libraries (especially for parallel/concurrent development) +orland C++ Compiler ,., Limited IDE (basic command line) -./ C/C++ Compiler0 and associated deelopement s!stems# 1J-22 " 1J 1elorie3s -./ 2rogramming 2latform Limited to executing inside the DOS box as it is a DOS compiler C!gwin " C!gnus4s gnuwin5& cross compiler and emulator $in-65& - Minimalistic GNU for Windows 32 bit The most interesting $hoi$e is %etween Cygwin and &in'W() * where there are trade*offs in terms of porta%ility and li$ensing! -./ - a recursie acron!m for 7-./3s .ot /ni*87 From Cygwin's web site (http://www.cygwin.com/ ) Cygwin is a Linux-like environment for Windows. It consists of two parts: A DLL (cygwin1.dll) which acts as a Linux API emulation layer providing substantial Linux API functionality. A collection of tools which provide Linux look and feel. Cygwin uses a DLL, cygwin.dll, (or maybe a set of DLLs) to provide a POSIX-like runtime on Windows. If you build something with Cygwin, any system you install it to will also need the Cygwin DLL(s). Cygwin versus &in'W will also need the Cygwin DLL(s). From MinGW's website (http://www.mingw.org/wiki/MinGW ) MinGW("Minimalistic GNU for Windows") is a collection of freely available and freely distributable Windows specific header files and import libraries combined with GNU toolsets that allow one to produce native Windows programs that do not rely on any 3rd-party C runtime DLLs MinGWcompiles to a native Win32 application. A MinGW application does not need any special runtime. Compile something in C!gwin and !ou are compiling it for yg!in. Compile something in $ing6and !ou are compiling it for "indo!s. C!gwin is good when !our app a%solutely needs a 29:I; enironment to run - it is sometimes easier to port something to C!gwin than it is to port it to 6indows0 because C!gwin is a la!er on top of 6indows that emulates a 29:I; enironment. If !ou compile something for C!gwin then it will need to be run within the C!gwin enironment0 as proided b! c!gwin(.dll. <or portabilit!0 !ou could distribute this dll with !our pro=ect0 if Cygwin versus &in'W proided b! c!gwin(.dll. <or portabilit!0 !ou could distribute this dll with !our pro=ect0 if !ou were willing to compl! with the releant license. $ing6is a 6indows port of the -./ compiler tools0 like -CC0 $ake0 +ash0 etc0 which run directl! in 6indows without an! emulation la!er. +! default it will compile to a natie 6in5& target0 complete with .e*e and .dll files0 though !ou could also cross-compile with the right settings. It is an alternatie to $icrosoft %isual C compiler and its associated linking/make tools in a wa!. POSIX - Portable Operating System Interface for Unix A family of related standards specified by the IEEE to define the application programming interface (API), along with shell and utilities interfaces, for software compatible with variants of the Unix operating system, although the standard can apply to any operating system. POSIX for Windows +O,- * Why would we need this in multi*platformdevelopment? Cygwin - near full compliance (the most free/open choice) Microsoft POSIX subsystem partial compliance Microsoft Windows Services for UNIX - full POSIX compliance for certain Microsoft Windows products UWIN from AT&T Research - near full compliance MKS Toolkit - near full compliance Portability Issues Case 1: I want to create an application where I write source code once, compile it once and run it in any platforms (e.g. Windows, Linux and Mac OS X). Best (?) Solution : Perhaps C/C++ is not the best choice. Why not write your source code in JAVA. Compile the source code once and run it anywhere. Case 2: I want to create an application where I write source code once, and compile the source code separately for each platform. Best (?) Solution : Write your source code in C or C++. Use standard header files only. Use any suitable compiler for any platform. Do not used advanced, machine dependent, language features. Portability Issues Case 3: I want to create an application where I write source code once, and compile the source code separately for each platform. However, I need some advanced cross platform features. Best (?) Solution : Use GCC as it is cross platform compiler. On Windows, the simplest solution is MinGW. Do not use programming features from the Windows API! Case 4: I want to create an application where I write source code once, and compile the source code separately for each platform. However, I need advanced features (like mulit-threading) not supported by MinGW. Best (?) Solution : You should use POSIX (Portable Operating System Interface [for UNIX]) standard. It provides many advanced programming features (including multi-threading) and tools. On Windows, Cygwin provides a largely POSIX- compliant development and run-time environment. .istri%uted versus hared: &+, versus Open&+? Distributed Memory why MPI? Sockets (standardized but low level) PVM - Parallel Virtual Machine (obsolete?) MPI - Message Passing Interface (de-facto standard) Shared Memory why OpenMP? Shared Memory why OpenMP? Posix Threads (standardized, low level) OpenMP Open Multi-Processing (de-facto standard) Automatic Parallelization (compiler does it for you) EN',NEE/,N' 0.1,CE: If in doubt - stick to the standards .istri%uted versus hared: &+, versus Open&+? Note: 9pen$2 and $2I are often combined - in a h!brid s!stem- to proide parallelismat different leels of granularit!. It is often said that $2I is more complicated than $20 but that it scales better> howeer0 this is not accepted b! all parallel programmers/engineers MPI Examples <irst check that !ou hae0 at the minimum0 the following installed# mpicc mpirun Check where these are on !our file s!stem0 and make sure the! are in !our path. <or e*ample0 on linu* s!stems !ou can use the which command# gibson@gibson-laptop:~/Teaching/OpenMPI$ which mpicc /usr/bin/mpicc /usr/bin/mpicc gibson@gibson-laptop:~/Teaching/OpenMPI$ which mpirun /usr/bin/mpirun If !ou wish to compile mpi code using an I1) (like )clipse) !ou may hae to configure the build to be able to find these e*ecutables. ?our I1) ma! proide a specific plug-in to support deelopment in parallel (using $2I and/or other libraries). <or e*ample )clipse has a parallel tools platformat# http://www!e$lipse!org/ptp/downloads!php mpicc The mpicc command can be used to compile and link MPI programs written in C. It provides the options and any special libraries that are OS dependent The MPI library may be used with any compiler that uses the same lengths for basic data objects (such as long double) and that uses compatible run-time libraries. On many systems, the various compilers are compatible and may be used interchangably. The environment variable MPICH_CC may be used to select different C compiler The environment variable MPICH_CC may be used to select different C compiler and linker. Note that changing the compilers used can cause problems. Use this only if you could intermix code compiled with the different compilers and you know what you are doing. Combining compilation and linking in a single command is usually done as follows: mpicc -o code code.c The full list of options is found in the man page. mpirun mpirun is a shell script which is required to run other mpi based programs. It attempts to: hide the differences in starting jobs for various devices from the user. determine what kind of machine it is running on and start the required number of jobs on that machine. On clusters, you must supply a file that lists the different machines that mpirun can use to run remote jobs or specify this file every time you run mpirun with the -machine file option. run remote jobs or specify this file every time you run mpirun with the -machine file option. mpirun is typicall invoked as follows: mpirun -np <number of processes> <program name and arguments> If mpirun cannot determine what kind of machine you are on, and it is supported by the mpi implementation, you can use the machine and -arch options to tell it what kind of machine you are running on The full list of options is found in the man page. MPI Examples #include <stdio.h> #include <mpi.h> int main(int argc, char *argv[]) { int numprocs, rank, namelen; char processor_name[MPI_MAX_PROCESSOR_NAME]; MPI_Init(&argc, &argv); HelloWorldMPI.c MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &numprocs); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Get_processor_name(processor_name, &namelen); printf("Hello World, from process %d on %s out of %d\n", rank, processor_name, numprocs); MPI_Finalize(); } MPI Examples COMPILE gibson@gibson-laptop:~/Teaching/OpenMPI$ mpicc -o HelloWorldMPI HelloWorldMPI.c EXECUTE ON 4 PROCESSORS HelloWorldMPI.c gibson@gibson-laptop:~/Teaching/OpenMPI$ mpirun -np 4 HelloWorldMPI Hello World, from process 0 on gibson-laptop out of 4 Hello World, from process 1 on gibson-laptop out of 4 Hello World, from process 2 on gibson-laptop out of 4 Hello World, from process 3 on gibson-laptop out of 4 MPI Examples testCommStarMPI.c 2' 2& 2( 2 2' 25 2@ 2, 2 MPI Examples COMPILE mpicc -o testCommStarMPI testCommStarMPI.c EXECUTE ON 6 PROCESSORS testCommStarMPI.c mpirun -np 6 testCommStarMPIProcess 0 will try to receive messages from 6 processes. Try to receive message from process 1 Messaged received from process 1 is - Greetings from process 1! Try to receive message from process 2 Messaged received from process 2 is - Greetings from process 2! Try to receive message from process 3 Messaged received from process 3 is - Greetings from process 3! Try to receive message from process 4 Messaged received from process 4 is - Greetings from process 4! Try to receive message from process 5 Messaged received from process 5 is - Greetings from process 5! MPI Examples testCommStarMPI.c #include <stdio.h> #include <string.h> #include "mpi.h" int main(int argc, char* argv[]){ int my_rank; /* rank of process */ int p; /* number of processes */ int source; /* rank of sender */ int dest; /* rank of receiver */ int dest; /* rank of receiver */ int tag=0; /* tag for messages */ char message[100]; /* storage for message */ MPI_Status status ; /* return status for receive */ /* start up MPI */ MPI_Init(&argc, &argv); /* find out process rank */ MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); /* find out number of processes */ MPI_Comm_size(MPI_COMM_WORLD, &p); MPI Examples testCommStarMPI.c if (my_rank !=0){ /* create message */ sprintf(message, "Greetings from process %d!", my_rank); dest = 0; /* use strlen+1 so that '\0' get transmitted */ MPI_Send(message, strlen(message)+1, MPI_CHAR, dest, tag, MPI_COMM_WORLD); } else{printf("Process 0 will try to receive messages from %d processes.\n",p); for (source = 1; source < p; source++) { printf("Try to receive message from process %d\n",source); MPI_Recv(message, 100, MPI_CHAR, source, tag, MPI_Recv(message, 100, MPI_CHAR, source, tag, MPI_COMM_WORLD, &status); printf("Messaged received from process %d is - %s\n",source, message); } } /* shut down MPI */ MPI_Finalize(); return 0; } MPI Examples testCommStarMPI.c main(){ ... MPI_Comm_Rank(MPI_COMM_WORLD,&id); MPI_Comm_size(MPI_COMM_WORLD,&np); if (id ==0 ) Master(); else 6e could/should restructure the program to separate the master and slave code# else Slave(); ... } A9 19# 1ownload the program testCommStarMPI.c fromthe web site http://www-public.int-evry.fr/~gibson/Teaching/CSC5021/Code/testCommStarMPI.c and restructure to master-slave architecture , as above. MPI Examples testCommRingMPI.c 2' 2& 2( 2 TO .O: Write a program that esta%lishes a $ommuni$ation 2' 25 2@ 2, 2 $ommuni$ation ring ar$hite$ture %etween pro$esses! MPI Examples parallelMergeSortMPI.c 9riginal 6ork " http#//penguin.ewu.edu/Btrolfe/2arallel$erge/inde*.html Timothy J. Rolfe. 2010. A specimen of parallel programming: parallel merge sort implementation. ACM Inroads 1, 4 (December 2010), 72-79. http://www-public.int-evry.fr/~gibson/Teaching/CSC5021/ReadingMaterial/Rolfe10.pdf 2rocess Canks in a <our-Deel :orting Aree for E processes 1ownload the source code director! ParallelMerge.zip fromthe web site# http://www-public.int-evry.fr/~gibson/Teaching/CSC5021/Code/ParallelMerge.zip MPI Examples parallelMergeSortMPI.c mpirun -np 1 parallelMergeSortMPI 1 processes mandates root height of 0 Size: 2671396 Sorting succeeds. Parallel: 1.072 Sequential: 1.082 Speed-up: 1.009 gibson@gibson-laptop:~/Teaching/OpenMPI$ mpirun -np 2 parallelMergeSortMPI 2 processes mandates root height of 1 Size: 14295844 Sorting succeeds. Parallel: 6.327 Sequential: 6.177 Speed-up: 0.976 gibson@gibson-laptop:~/Teaching/OpenMPI$ mpirun -np 4 parallelMergeSortMPI 4 processes mandates root height of 2 Size: 3494692 34ET,ON# can !ou e*plain the speed-up aluesF Size: 3494692 Sorting succeeds. Parallel: 1.553 Sequential: 1.379 Speed-up: 0.888 gibson@gibson-laptop:~/Teaching/OpenMPI$ mpirun -np 6 parallelMergeSortMPI 6 processes mandates root height of 3 Size: 2949924 Sorting succeeds. Parallel: 1.440 Sequential: 1.156 Speed-up: 0.803 gibson@gibson-laptop:~/Teaching/OpenMPI$ mpirun -np 8 parallelMergeSortMPI 8 processes mandates root height of 3 Size: 16315172 Sorting succeeds. Parallel: 8.446 Sequential: 7.444 Speed-up: 0.881 MPI Examples Checking for prime numbers 1ownload the program parallelprimeCountMPI.c fromthe web site# http://www-public.int-evry.fr/~gibson/Teaching/CSC5021/Code/parallelprimeCountMPI.c TO DO: Write test code to verify that this functions correctly Write test code to verify that this functions correctly Try to understand how the code works Open&+ (9pen $ulti-2rocessing) is an A2I (application programming interface) that consists of a set of compiler directies0 librar! routines0 and enironment ariables that influence run-time behaior. In 9pen$2 with C/C++ the programmer uses Gpragmas to control the parallel aspects Open&+ * resour$es Ahe first port of call should be# http#//openmp.org/wp/ Ahere is a good tutorial presentation at http#//www.openmp.org/mp-documents/omp-hands-on-:C'E.pdf A :ummar! of 9pen$2 5.' C/C++ :!nta* can be found at# http#//www.openmp.org/mp-documents/9pen$25.'-:ummar!:pec.pdf Ahere is a good web site on 9pen$2 with C++ at# http#//bisHwit.iki.fi/stor!/howto/openmp/G)*ampleInitialiIingAAableIn2arallel http#//bisHwit.iki.fi/stor!/howto/openmp/G)*ampleInitialiIingAAableIn2arallel Ahere is a good guide at# http#//geco.mines.edu/workshop/class&/e*amples/openmp/openmp.pdf 9ne of the first published articles was# Deonardo 1agumand Camesh $enon. (JJE. 9pen$2# An Industr!-:tandard A2I for :hared-$emor! 2rogramming. IEEE omput# Sci# Eng# ,0 ( (Januar! (JJE)0 @K-,,. http://www-public.int-evry.fr/~gibson/Teaching/CSC5021/ReadingMaterial/DagumMenon98.pdf OpenMP Examples Ginclude Lstdio.hM Ginclude Lomp.hM int main(int argc0 char NargOP) Q int iamR '0 np R (S Gpragma omp parallel default(shared) priate(iam0 np) Q 5elloWorldO&+!$ Q Gif defined (T92).$2) np R ompTgetTnumTthreads()S iamR ompTgetTthreadTnum()S Gendif printf(7Uello fromthread Vd out of VdWn70 iam0 np)S X X OpenMP Examples Yuestion# what do !ou think this code is doingF #pragma omp parallel for for(int x=0; x < width; x++) { for(int y=0; y < height; y++) { finalImage[x][y] = RenderPixel(x,y, &sceneData); finalImage[x][y] = RenderPixel(x,y, &sceneData); } } TO DO: Compile and execute some openMP examples. Look for code that does parallel sorting to compare with the MPI merge sort. Project Assessed Work (50%) ?ou are to design0 implement0 test and compare & separate solutions to a string sear$h pro%lem# (. :eHuential &. 2arallel +ro%lemOverview: :cientists hae discoered different t!pes of alien genetic material0 sharing common structure# 5 dimensional lattice of cubes of distinctie components (letters) 5 dimensional lattice of cubes of distinctie components (letters) )ach lattice has a finite number of letters in its alphabet " from& up to ('' Ahe lattice siIes range from@N@N@ up to ('''N('''N(''' :cientists hae also discoered dictionaries of words fromthe genetic alphabet# Ahe dictionar! siIe ranges from5 up to (''' words All words in a common dictionar! share the same length0 from& up to ('' Ahe scientists h!pothesise that dictionaries are associated to some of the genetic material0 proided all words in the dictionar! can be found in seHuence (horiIontal0 ertical or diagonal) in the material lattice. ?ou are to write a program that takes as input an! lattice material and an! dictionar!0 and returns a boolean stating whether there is an association between the two. Project Assessed Work (50%) Example 1ictionar!( R abcb0 bcca0 aabc 1ictionar!& R abcde0 ebcda0 edbac 1ictionar!5 R abbba0 bbbba0 aabaa 6hich of these dictionaries are associated to the ,N, lattice of genetic material (if an!)F