2016년 10월 16일 일요일

OpenMP 예제: For loop iterations w/ no data dependency

Notes:

1. Visual Studio 2015 setting은:

2. #include <omp.h>
  • omp_get_thread_num()를 위해 필요함.

3. OpenMP constructs:
  • #pragma omp parallel for
  • #pragma omp critical
    • Optional name을 이용해서 서로 다른 여러 개의 critical section을 정의할 수 있음:
      #pragma omp critical (name_of_the_critical_section)
    • All CRITICAL sections which are unnamed, are treated as the same section (https://computing.llnl.gov/tutorials/openMP/#CRITICAL).

4. 참고로, #include "Windows.h"Sleep()을 위한 것임 (OpenMP와 상관 없음).

#include <iostream>

#include <omp.h>

#include "Windows.h"

using namespace std;

void First();
void Second();
void Third();

int main() {
#pragma omp parallel for
  for (int i = 0; i < 100; ++i) {
    const int tid = omp_get_thread_num();
#pragma omp critical
    cout << tid << ": * Processing: " << i << endl;

#pragma omp critical
    cout << tid << ":   1. The first job" << endl;
    First();

#pragma omp critical
    cout << tid << ":   2. The second job" << endl;
    Second();

#pragma omp critical
    cout << tid << ":   3. The third job" << endl;
    Third();

    cout << endl;
  }

  return 0;
}

void First()  { Sleep(1000); }
void Second() { Sleep(500);  }
void Third()  { Sleep(2000); }