====== 5. Seminar - Parallel programming for SMS (C language + OpenMP) ======
Task to practice on seminar: EN: {{ .:05_seminar_assignment-en.pdf |}} CZ: {{.:05_cviceni_zadani.pdf|}}\\
Prepared basic program for image sharpening (template code for seminar project 4):
* win {{courses:B4M35PAP:tutorials:05:program_ostreni_obrazu.zip|}}
* linux {{:courses:B4M35PAP:tutorials:05:program_ostreni_obrazu_linux.zip|}}
OpenMP specification and documentation
* http://www.openmp.org/
* https://computing.llnl.gov/tutorials/openMP/
* http://pages.tacc.utexas.edu/~eijkhout/pcse/html/
Complete lectures of Tim Mattson’s (Intel)
* http://www.openmp.org/wp-content/uploads/Intro_To_OpenMP_Mattson.pdf
* http://tinyurl.com/OpenMP-Tutorial (kompletnĂ kurz na Youtube)
\\ \\
Measure the execution time of part of a program under Windows using //QueryPerformanceCounter//() from //windows.h// library:
LARGE_INTEGER start, stop, frequency;
long overhead;
QueryPerformanceFrequency(&frequency);
QueryPerformanceCounter(&start);
QueryPerformanceCounter(&stop);
overhead = stop.QuadPart-start.QuadPart;
//------------------------------
QueryPerformanceCounter(&start);
// ...
QueryPerformanceCounter(&stop);
//------------------------------
cout << " Execution time of observed program portion is "
<< 1000*(stop.QuadPart - start.QuadPart - overhead)/(double)frequency.QuadPart
<< " ms" << endl;
\\ \\
Execution time of part of a program under Linux with use of //clock_gettime//() from //time.h// header file.
struct timespec start, stop;
double elapsed;
//------------------------------
clock_gettime(CLOCK_REALTIME, &start);
// ...
clock_gettime(CLOCK_REALTIME, &stop);
//------------------------------
elapsed = ( stop.tv_sec - start.tv_sec )*1000
+ (double)( stop.tv_nsec - start.tv_nsec )/(double)1000000;
printf( " Execution time of observed program portion is %lf ms\n", elapsed);
\\ \\ \\
-------
\\
Files for installation (to work on tasks at home):
- Cygwin (C/C++ a OpenMP) http://www.cygwin.com/
- MPICH 2, Win IA32 (library MPI) http://www.mcs.anl.gov/research/projects/mpich2/
- NetBeans IDE with C/C++ language support http://netbeans.org/downloads/index.html
Remark NetBeans installation is not mandatory. Programs can be edited even in Notepad (http://notepad-plus-plus.org).
\\ \\ \\
It is necessary to mark for in Cygwin installation next packages/components from development section:
* binutils
* gcc core
* gcc g++
* gcc mingw core
* gcc mingw g++
* gdb
* libboost
* make
* mingw runtime
Install also all automatically selected components.
\\ \\ \\
If you decide to use NetBeans:
It is necessary to add path to Cygwin binaries to executables search path %PATH%. It can be done by next command in command line: setx path "%path%;C:\Cygwin \\
MPI executables directory neds to be added to search path as well: setx path "%path%;C:\Program Files (x86)\MPICH2\bin
Option setup in NetBeans IDE: \\
Tools -> Options -> C/C++ -> karta "Build Tools" -> Tool Collection -> Add.. -> Cygwin \\
Tools -> Options -> C/C++ -> karta "Code Assistance" -> Tool Collection -> Cygwin \\
and insert MPICH2 in Include directories list: C:\Program Files (x86)\MPICH2\include
On the "Build Tools" tab in the same dialog (Tools -> Options -> C/C++) next setup should be seen:
* Base Director: C:\Cygwin\bin
* C compiler C:\Cygwin\bin\gcc.exe
* C++ Compiler: C:\Cygwin\bin\g++-3.exe
* Assembler: C:\cygwin\bin\as.exe
* Make Command: C:\Cygwin\bin\make.exe
* Debugger: C:\Cygwin\bin\gdb.exe
NetBeans setup for correct compilation of developed programs:
**OpenMP**: \\
File -> Project Properties:
C Compiler:
- Additional Options: -fopenmp
\\
-----------------------------------------------------------------------------------
\\
**MPI**: \\
File -> Project Properties:
C Compiler:
- Include directories: .../Program Files/MPICH2/include
Linker:
- Additional Library directories: .../Program Files/MPICH2/lib
- Libraries: mpi.lib
\\
-----------------------------------------------------------------------------------
\\
**OpenMP & MPI**: \\
File -> Project Properties:
C Compiler:
- Include directories: .../Program Files/MPICH2/include
- Additional Options: -fopenmp
Linker:
- Additional Library directories: .../Program Files/MPICH2/lib
- Libraries: mpi.lib
The program.c compilation from Cygwin command line "Cygwin Bash Shell" (without NetBeans use) with g++ (gcc, mpiCC):
**OpenMP**: \\
g++ program.c -fopenmp
\\
-----------------------------------------------------------------------------------
\\
**MPI**: \\
g++ program.c -I"C:\Program Files\MPICH2\include" -L"C:\Program Files\MPICH2\lib" -lmpi
\\
-----------------------------------------------------------------------------------
\\
**OpenMP & MPI**: \\
g++ program.c -fopenmp -I"C:\Program Files\MPICH2\include" -L"C:\Program Files\MPICH2\lib" -lmpi
Example program for development environment testing - program.c:
**OpenMP**:
#include
#include
int main(int argc, char *argv[]) {
int iam = 0, np = 1;
#pragma omp parallel default(shared) private(iam, np)
{
np = omp_get_num_threads();
iam = omp_get_thread_num();
printf("Hello from thread %d out of %d \n", iam, np);
}
return 0;
}
\\
-----------------------------------------------------------------------------------
\\
**MPI**:
#include
#include "mpi.h"
int main(int argc, char *argv[]) {
int numprocs, rank, namelen;
char processor_name[MPI_MAX_PROCESSOR_NAME];
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 from process %d out of %d on %s\n",
rank, numprocs, processor_name);
MPI_Finalize();
return 0;
}
\\
-----------------------------------------------------------------------------------
\\
**OpenMP & MPI**:
#include
#include "mpi.h"
#include
int main(int argc, char *argv[]) {
int numprocs, rank, namelen;
char processor_name[MPI_MAX_PROCESSOR_NAME];
int iam = 0, np = 1;
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);
#pragma omp parallel default(shared) private(iam, np)
{
np = omp_get_num_threads();
iam = omp_get_thread_num();
printf("Hello from thread %d out of %d from process %d out of %d on %s\n",
iam, np, rank, numprocs, processor_name);
}
MPI_Finalize();
return 0;
}
Remark: Do not forget to copy or locate required libraries in directory with compiled and started program (usual file names: cyggcc_s-1.dll, cyggomp-1.dll, cygstdc++-6.dll, cygwin1.dll).