В качестве примера рассмотрим классическую программу «Hello World»:
#pragma omp parallel
{
printf("Hello World\n");
}
В двухпроцессорной системе результат, как правило, будут следующим:
Hello World
Hello World
Более сложный вариант программы выводит сообщение с соответствующим номером нити (функция omp_get_thread_num()). При этом основной нитью выводится общее число нитей (функция omp_get_num_threads()).
C / C++ - Пример параллельной обработки
#include <omp.h>
main () {
int nthreads, tid;
/* Fork a team of threads giving them their own copies of variables */
#pragma omp parallel private(tid)
{
/* Obtain and print thread id */
tid = omp_get_thread_num();
printf("Hello World from thread = %d\n", tid);
/* Выполняется только основной нитью. Only master thread does this */
if (tid == 0)
{
nthreads = omp_get_num_threads();
printf("Number of threads = %d\n", nthreads);
}
} /* All threads join master thread and terminate */
}
|
C / C++ - Пример параллельной обработки
#include <omp.h>
main () {
int nthreads, tid;
/* Fork a team of threads giving them their own copies of variables */
#pragma omp parallel private(tid)
{
/* Obtain and print thread id */
tid = omp_get_thread_num();
printf("Hello World from thread = %d\n", tid);
/* Выполняется только основной нитью. Only master thread does this */
if (tid == 0)
{
nthreads = omp_get_num_threads();
printf("Number of threads = %d\n", nthreads);
}
} /* All threads join master thread and terminate */
}






