OpenMP編程學習筆記五

來源:互聯網
上載者:User

critical 使用:

如果程式碼片段只需要一個thread執行,可以使用single標明。如果程式碼片段需要ID為0的thread執行,使用master標明。

那麼,如果程式碼片段需要各個CPU互斥執行,也就是要求每個CPU都執行一次,但任何時候只用一個CPU在執行。這種

情況可以使用critical。從宏觀上看,該程式碼片段被依次在各個CPU上被執行,各個CPU在執行該代碼的時序上沒有重疊。

測試代碼:

void testCritical()
{
    omp_set_num_threads( 4 );
    clock_t t1 = clock();
#pragma omp parallel
    {
       
        printf("test OpenMP/n");
#pragma omp critical
        {
            clock_t t11 = clock();
            printf("test OpenMP critical/n");
            printf("execute thread ID is %d/n", omp_get_thread_num());
                for (int i = 0; i < 100000000; i++)
             {
                 int a = i+1;
             }

            clock_t t22 = clock();
            printf("thread time = %d/n", t22 - t11);
        }
    }
    clock_t t2 = clock();
    printf("total time = %d/n", t2 - t1);
}


運行結果:

test OpenMP
test OpenMP
test OpenMP
test OpenMP
test OpenMP critical
execute thread ID is 0
thread time = 291
test OpenMP critical
execute thread ID is 2
thread time = 290
test OpenMP critical
execute thread ID is 1
thread time = 294
test OpenMP critical
execute thread ID is 3
thread time = 286
total time = 1164

可以看出,整個啟動並執行時間為1164ms,四個thread不是同步執行critical程式碼片段,而是依次執行的。

為了對比,注釋掉critical,

void testCritical()
{
    omp_set_num_threads( 4 );
    clock_t t1 = clock();
#pragma omp parallel
    {
       
        printf("test OpenMP/n");
//#pragma omp critical
        {
            clock_t t11 = clock();
            printf("test OpenMP critical/n");
            printf("execute thread ID is %d/n", omp_get_thread_num());
                for (int i = 0; i < 100000000; i++)
             {
                 int a = i+1;
             }

            clock_t t22 = clock();
            printf("thread time = %d/n", t22 - t11);
        }
    }
    clock_t t2 = clock();
    printf("total time = %d/n", t2 - t1);
}


再運行,結果為:

test OpenMP
test OpenMP critical
execute thread ID is 0
test OpenMP
test OpenMP critical
execute thread ID is 2
test OpenMP
test OpenMP critical
execute thread ID is 3
test OpenMP
test OpenMP critical
execute thread ID is 1
thread time = 224
thread time = 228
thread time = 237
thread time = 248
total time = 250

此時,total time基本上等於一個thread的運行所耗時間。


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.