Programowanie w systemie UNIX/CPU
CPU[edytuj]
Epiphany[edytuj]
Technologia Epiphany[1] i przykładowy program z użyciem OpenCl[2]
Koprocesor Xeon Phi[edytuj]
Wstęp do programowania procesorów Xeon i koprocesora Xeon Phi [3]
OpenMP[edytuj]
testy wstępne[edytuj]
wersja[edytuj]
- openmp_version : "integer parameter openmp_version with a value yyyymm where yyyy and mm are 19 the year and month designations of the version of the OpenMP API that the implementation supports"
- OMP_DISPLAY_ENV
# bash
export OMP_DISPLAY_ENV="TRUE"
Wywołanie programu korzystajaćego z OpenMP spowoduje wyświetlenie dodatkowego komunikatu:
OPENMP DISPLAY ENVIRONMENT BEGIN
_OPENMP = '201511'
OMP_DYNAMIC = 'FALSE'
OMP_NESTED = 'FALSE'
OMP_NUM_THREADS = '8'
OMP_SCHEDULE = 'DYNAMIC'
OMP_PROC_BIND = 'FALSE'
OMP_PLACES = ''
OMP_STACKSIZE = '0'
OMP_WAIT_POLICY = 'PASSIVE'
OMP_THREAD_LIMIT = '4294967295'
OMP_MAX_ACTIVE_LEVELS = '2147483647'
OMP_CANCELLATION = 'FALSE'
OMP_DEFAULT_DEVICE = '0'
OMP_MAX_TASK_PRIORITY = '0'
OPENMP DISPLAY ENVIRONMENT END
procesor[edytuj]
Sprawdzamy ile rdzeni (wątków) ma procesor:[4]
grep 'processor.*:' /proc/cpuinfo | wc -l
Im więcej tym lepiej. Jeśli 1 to nie ma potrzeby używania OpenMP.
kompilator[edytuj]
W konsoli sprawdzamy wersję:[5]
gcc --version ## get compiler version
lub [6]
echo |cpp -fopenmp -dM |grep -i open
przykładowy wynik:
#define _OPENMP 201307
biblioteki[edytuj]
Biblioteki:
- libgomp (GNU Offloading and Multi Processing Runtime Library)[7]
Wyszukujemy pakiety (uwaga na openmpi - to inna biblioteka)
sudo apt-cache search openmp
Stan biblioteki:
dpkg --status libgomp1
wyniki:
Package: libgomp1 Status: install ok installed Priority: optional Section: libs Installed-Size: 156 Maintainer: Ubuntu Core developers <ubuntu-devel-discuss@lists.ubuntu.com> Architecture: amd64 Multi-Arch: same Source: gcc-5 Version: 5.4.0-6ubuntu1~16.04.4 Depends: gcc-5-base (= 5.4.0-6ubuntu1~16.04.4), libc6 (>= 2.17) Breaks: gcc-4.3 (<< 4.3.6-1), gcc-4.4 (<< 4.4.6-4), gcc-4.5 (<< 4.5.3-2) Description: GCC OpenMP (GOMP) support library GOMP is an implementation of OpenMP for the C, C++, and Fortran compilers in the GNU Compiler Collection. Homepage: http://gcc.gnu.org/ Original-Maintainer: Debian GCC Maintainers <debian-gcc@lists.debian.org>
lub:
ldconfig -p | grep gomp
przykładowy wynik:
libgomp.so.1 (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libgomp.so.1
Instalacja[edytuj]
Nie instalujemy biblioteki, ponieważ OpenMP jest to opcja kompilatora[8]
ale:
apt-get install libgomp1 sudo apt-get install gcc-multilib
użycie[edytuj]
Użycie OpenMP[9][10] polega na dodaniu do istniejącego kodu (tu przykład z kodem w C):
- dyrektywy preprocesora:
#include <omp.h>
- Tworzenie wątków za pomocą dyrektywy:
#pragma omp parallel
- kompilacji z dodaną opcję:
-fopenmp
[11]
Ta niewielka ingerencja w istniejący kod[12] pozwala na przyspieszenie wykonywania n-razy (n jest liczbą możliwych wątków = liczba rdzeni * liczba wątków_na_rdzeń).
Przykłady[edytuj]
Hello world![edytuj]
/*
export OMP_DISPLAY_ENV="TRUE"
gcc.c.c -fopenmp -Wall
./a.out
*/
#include <omp.h>
#include <stdio.h>
int main() {
#pragma omp parallel
printf("Hello from thread %d, nthreads %d\n", omp_get_thread_num(), omp_get_num_threads());
}
Zagnieżdzone pętle ( ang. nested loops)[edytuj]
/*
C program that uses nested for loops
gcc o.c -Wall -fopenmp
./a.out
i = 10000 xMax*yMax = 10000
i = 444514 = 0.444514 *xMax*yMax where xMax*yMax = 1000000 // no reduction
i = 437097 = 0.437097*(xMax*yMax) where xMax*yMax = 1000000
i = 1000000 = 1.000000*(xMax*yMax) where xMax*yMax = 1000000 // reduction
*/
#include <stdio.h>
#include <stdlib.h>
#include <omp.h> // OpenMP
int i= 0;
int main()
{
int x;
int xMax = 1000;
int y;
int yMax = 1000;
int all = xMax*yMax;
#pragma omp parallel for collapse(2) schedule(dynamic) reduction(+:i)
for (x = 0; x < xMax; x++) {
for (y = 0; y < yMax; y++)
{i++;}
}
printf("i = %d = %f*(xMax*yMax) \t where xMax*yMax = %d\n", i, (double)i/all, all);
return 0;
}
Wypełnianie tablicy[edytuj]
// fill array using symmetry of image
// uses global var : ...
int FillArraySymmetric(unsigned char data[] )
{
unsigned char Color; // gray from 0 to 255
printf("axis of symmetry \n");
iy = iyAxisOfSymmetry;
#pragma omp parallel for schedule(dynamic) private(ix,Color) shared(ixMin,ixMax, iyAxisOfSymmetry)
for(ix=ixMin;ix<=ixMax;++ix) PlotPoint(ix, iy, GiveColor(ix, iy));
/*
The use of ‘shared(variable, variable2) specifies that these variables should be shared among all the threads.
The use of ‘private(variable, variable2)’ specifies that these variables should have a seperate instance in each thread.
*/
#pragma omp parallel for schedule(dynamic) private(iyAbove,ix,iy,Color) shared(iyAboveMin, iyAboveMax,ixMin,ixMax, iyAxisOfSymmetry)
// above and below axis
for(iyAbove = iyAboveMin; iyAbove<=iyAboveMax; ++iyAbove)
{printf(" %d from %d\n", iyAbove, iyAboveMax); //info
for(ix=ixMin; ix<=ixMax; ++ix)
{ // above axis compute color and save it to the array
iy = iyAxisOfSymmetry + iyAbove;
Color = GiveColor(ix, iy);
PlotPoint(ix, iy, Color );
// below the axis only copy Color the same as above without computing it
PlotPoint(ixMax-ix, iyAxisOfSymmetry - iyAbove , Color );
}
}
return 0;
}
zbiór Mandelbrota[edytuj]
Błędy[edytuj]
Poradniki[edytuj]
- commons:Category:OpenMP - kod uzywajacy OpenMP do tworzenia obrazów
- http://www.cmmsigma.eu/labs/parallel_comp/openmp_tutorial/index.pl.html
- https://gcc.gnu.org/wiki/Offloading
- https://computing.llnl.gov/tutorials/openMP/exercise.html
- https://gcc.gnu.org/wiki/openmp
- https://www.ibm.com/developerworks/aix/library/au-aix-openmp-framework/index.html
- https://docs.loni.org/wiki/Introduction_to_OpenMP
- Guide into OpenMP: Easy multithreading programming for C++. By Joel Yliluoma
- Fractal - Mandelbrot Set using OpenGL C++ OpenMP video by Marek Bruchatý
- beej blog : parallel-programming-openmp
- https://github.com/clang-omp/clang_trunk/tree/master/test/OpenMP
Źródła[edytuj]
- ↑ Technologia Epiphany - superkomputer dla każdego
- ↑ Mandelbrot set using OpenCL on Epiphany
- ↑ Tutorial: Parallel programming for Xeon and Phi at goparallel
- ↑ OpenMP in 30 Minutes By Joe Landman
- ↑ gcc wiki: openmp
- ↑ stackoverflow question how-to-check-the-version-of-openmp-on-linux
- ↑ libgomp
- ↑ askubuntu question how-can-i-install-openmp-in-ubuntu
- ↑ OpenMP w wikipedii
- ↑ OpenMP in 30 Minutes By Joe Landman
- ↑ OpenMP - kompilatory
- ↑ Kurs openMP - Paweł Przybyłowicz - asystent na Wydziale Matematyki Stosowanej AGH.
- ↑ 32 OpenMP Traps For C++ Developers 20.11.2009 by Alexey Kolosov, Andrey Karpov, Evgeniy Ryzhkov
- ↑ Common Mistakes in OpenMP and How To Avoid Them A Collection of Best Practices by Michael Suß and Claudia Leopold