다음을 통해 공유


B. 런타임 라이브러리 함수의 스텁

이 단원에서는 스텁 OpenMP C 및 C++ API에 정의 된 런타임 라이브러리 함수를 제공 합니다. 이식성에 OpenMP C 및 C++ API를 지원 하지 않는 플랫폼을 사용 하는 스텁은 제공 합니다. 이러한 플랫폼에서는 OpenMP 프로그램이 스텁 함수를 포함 하는 라이브러리와 연결 되어야 합니다. 지시문에 OpenMP 프로그램 무시 하도록 스텁 함수를 가정 합니다. 는 직렬 의미를 에뮬레이션 합니다.

참고

잠금 함수에 나타나는 잠금 변수 이러한 함수를 단독으로 액세스 해야 합니다.이 해야 없습니다 수 초기화 되거나 사용자 프로그램에서 수정 합니다.사용자가 OpenMP C 및 C++ 구현에서 사용 되는 잠금 스텁 함수에 의해 사용 된 스키마를 기반으로 구현 하는 메커니즘에 대 한 가정을 합니다.

코드

#include <stdio.h>
#include <stdlib.h>
#include "omp.h"
#ifdef __cplusplus
extern "C" {
#endif


void omp_set_num_threads(int num_threads)
{
}
int omp_get_num_threads(void)
{
    return 1;
}
int omp_get_max_threads(void)
{
    return 1;
}
int omp_get_thread_num(void)
{
    return 0;
}
int omp_get_num_procs(void)
{
    return 1;
}
void omp_set_dynamic(int dynamic_threads)
{
}
int omp_get_dynamic(void)
{
    return 0;
}
int omp_in_parallel(void)
{
    return 0;
}
void omp_set_nested(int nested)
{
}
int omp_get_nested(void)
{
    return 0;
}
enum {UNLOCKED = -1, INIT, LOCKED};
void omp_init_lock(omp_lock_t *lock)
{
    *lock = UNLOCKED;
}
void omp_destroy_lock(omp_lock_t *lock)
{
    *lock = INIT;
}
void omp_set_lock(omp_lock_t *lock)
{
    if (*lock == UNLOCKED) 
    {
        *lock = LOCKED;
    } 
    else 
        if (*lock == LOCKED) 
        {
         fprintf_s(stderr, "error: deadlock in using lock variable\n");
         exit(1);
        } else {
         fprintf_s(stderr, "error: lock not initialized\n");
         exit(1);
        }
}


void omp_unset_lock(omp_lock_t *lock)
{
    if (*lock == LOCKED) 
    {
        *lock = UNLOCKED;
    } 
    else 
        if (*lock == UNLOCKED) 
        {
            fprintf_s(stderr, "error: lock not set\n");
            exit(1);
        } else {
            fprintf_s(stderr, "error: lock not initialized\n");
            exit(1);
        }
}

int omp_test_lock(omp_lock_t *lock)
{
    if (*lock == UNLOCKED) 
    {
        *lock = LOCKED;
        return 1;
    } else if (*lock == LOCKED) {
        return 0;
    } else {
        fprintf_s(stderr, "error: lock not initialized\n");
        exit(1);
    }
}

#ifndef OMP_NEST_LOCK_T
typedef struct {  // This really belongs in omp.h 
    int owner;
    int count;
} omp_nest_lock_t;
#endif
enum {MASTER = 0};
void omp_init_nest_lock(omp_nest_lock_t *lock)
{
    lock->owner = UNLOCKED;
    lock->count = 0;
}
void omp_destroy_nest_lock(omp_nest_lock_t *lock)
{
    lock->owner = UNLOCKED;
    lock->count = UNLOCKED;
}


void omp_set_nest_lock(omp_nest_lock_t *lock)
{
    if (lock->owner == MASTER && lock->count >= 1) 
    {
        lock->count++;
    } else 
        if (lock->owner == UNLOCKED && lock->count == 0) 
        {
            lock->owner = MASTER;
            lock->count = 1;
        } else 
        {
       fprintf_s(stderr, "error: lock corrupted or not initialized\n");
         exit(1);
    }
}

void omp_unset_nest_lock(omp_nest_lock_t *lock)
{
    if (lock->owner == MASTER && lock->count >= 1) 
    {
        lock->count--;
        if (lock->count == 0) 
        {
            lock->owner = UNLOCKED;
        }
    } else 
        if (lock->owner == UNLOCKED && lock->count == 0) 
        {
            fprintf_s(stderr, "error: lock not set\n");
            exit(1);
        } else 
        {
       fprintf_s(stderr, "error: lock corrupted or not initialized\n");
       exit(1);
    }
}


int omp_test_nest_lock(omp_nest_lock_t *lock)
{
    omp_set_nest_lock(lock);
    return lock->count;
}


double omp_get_wtime(void)
{
    // This function does not provide a working
    // wallclock timer. Replace it with a version
    // customized for the target machine.
    return 0.0;
}

double omp_get_wtick(void)
{
    // This function does not provide a working
    // clock tick function. Replace it with
    // a version customized for the target machine.
    return 365. * 86400.;
}

#ifdef __cplusplus
}
#endif