Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 5 của 5

Chủ đề: OpenMP for beginner!!!

  1. #1
    Ngày tham gia
    Sep 2015
    Bài viết
    0

  2. #2
    - Whenever you want to use OpenMP, follow these steps above. I think it's simple enough.

    Mã:
    #pragma omp forfor(int i = 0;i < 10;++i){    Arr[i] += i;}

  3. #3
    Ngày tham gia
    Sep 2015
    Bài viết
    0

    OpenMP for beginner!!!

    1.What is OpenMP:
    - Application Program Interface (API).
    - Enable parallel programming in C/C++ and Fortran.
    - Supports multi-platform.

    *****Notes*****
    - Extreme useful when developing high computing applications.
    - No performance improvement with memory transfering.

    2.How to use OpenMP in our project:
    - In this post, I only mention about OpenMP in Visual Studio (I only use this one :lol: ). To enable OpenMP for any project in Visual Studio, follow these steps below:
    • Right-click on your project and choose properties Under Configuration Properties -> C/C++ -> Language, choose Yes(/openmp) in OpenMP Support to enable it. Now, source files, include omp.h: #include "omp.h"



    - We're all set. Now you can begin using OpenMP [IMG]images/smilies/wink.png[/IMG] !

    3.OpenMP directives:
    - Directives may be the only thing people care about when using OpenMP. Other functions and clauses can be used to support but not too much (at least in my experience, my project has never used any clause or function :lol: )

    FOR Directive:
    Mã:
    #pragma omp [parallel] for [clauses]
    	for_statement
    - Clauses supported: firstprivate,lastprivate,nowait,ordered,private,re duction,schedule

    - This is a useful and simple one. It will distribute your loop into several parts and assign them to each thread. For example, if your loop has to run through 0 to 9, with 2 threads as default, it will separate the loop into two parts:
    • Part 1 from 0 to 4Part 2 from 5 to 9

    - In this case, do not change index variable in the loop. If your loop run through 0 to 9, but the last statement is: i = i + 2, what will happen !
    Mã:
    // Normal code
    int Arr[10];
    for(int i = 0;i < 10;++i)
    {
    	Arr[i] = Arr[i] + 1;
    }
    
    // OpenMP code
    int Arr[10];
    #pragma omp for
    for(int i = 0;i < 10;++i)
    {
    	Arr[i] += i;
    }
    - We only add #pragma omp for before the loop :lol: ! But when executing the code, the loop will be separated into two parts for two threads. You can change the number of thread with: omp_set_num_threads(NUM_THREADS). NUM_THREADS is the number of thread you want to use, powered by 2 is alway better (2,4,8,16,...).

    SECTIONS Directive:
    Mã:
    #pragma omp [parallel] sections [clauses]
    {
    	#pragma omp section
    	{
    		code_block 
    	} 
    }
    - Clauses supported: firstprivate,lastprivate,nowait,private,reduction

    - This is the one I mostly use. It is the same with FOR directive but you can manually separate your code into several parts. For that reason, it's more flexible. For example, you can use one section for doing job 1 and the other for job 2, they dont have to be in the same loop.
    Mã:
    int Arr[10];
    int Arr2[50];
    
    // Normal code
    for(int i = 0;i < 10;++i)
    {
    	Arr[i] += i;
    }
    for(int i = 0; i < 50;++i)
    {
    	Arr2[i] += i;
    }
    
    // OpenMP code
    #pragma omp parallel sections num_threads(2)
    {
    	#pragma omp section
    	{
    		for(int i = 0;i < 10;++i)
    		{
    			Arr[i] += i;
    		}
    	}
    	#pragma omp section
    	{
    		for(int i = 0;i < 20;++i)
    		{
    			Arr2[i] += i;
    		}
    	}
    }
    ATOMIC
    Mã:
    #pragma omp atomic
    	expression
    - Protects memory location against multiple writes.
    Mã:
    int Arr[10];
    int Sum = 0;
    
    #pragma omp for
    for(int i = 0;i < 10;++i)
    {
    	#pragma omp atomic
    	Sum += Arr[i];
    }
    BARRIER
    Mã:
    #pragma omp barrier
    - Synchronizes all threads. All threads will stop running and wait for the others untill they all reach the barrier.

    CRITICAL
    Mã:
    #pragma omp critical [(name)]
    {
    	code_block
    }
    - Specifies that code is only be executed on one thread at a time.
    Mã:
    max = a[0];
    #pragma omp parallel for num_threads(2)
    for (int i = 1;i < SIZE;i++) 
    {
    	if (a[i] > max)
    	{
    		#pragma omp critical
    		{
    			// compare a[i] and max again because max 
    			// could have been changed by another thread after 
    			// the comparison outside the critical section
    			if (a[i] > max)
    				max = a[i];
    		}
    	}
    }
    MASTER
    Mã:
    #pragma omp master
    {
    	code_block
    }
    - Specifies that only the master threadshould execute a section of the program.

    ORDERED
    Mã:
    #pragma omp ordered
    	structured-block
    - Specifies that code under a parallelized for loop should be executed like a sequential loop.

    - These are directives which i think are mostly used. Other clauses are not needed if you use FOR or SECTION directive, so i dont mention about them here.

    4.CLAUSE:
    - I will mention only two clause here: private and shared
    • private(var) : var is private to one thread and can not be accessed by other threads. shared(var) : var is shared among threads. That's mean all thread can use them at a time. Be sure that you ATOMIC directive to prevent multiple write for that variable.

  4. #4
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    ai đôn ăn đơ sờ ten

  5. #5
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Trích dẫn Gửi bởi EGXD
    ai đôn ăn đơ sờ ten
    diu mớt sờ tờ im pờ ru vờ gio inh lịch sờ.
    đít sờ i dờ ôn lờ bây dich thinh dờ.

    bạn không hiểu là do tiếng anh bạn chưa vững và kiến thì chưa thức, thế thôi

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •