Synchronization mechanism of unified Parallel C language

Source: Internet
Author: User
Keywords C language unified sync mechanism
Tags based example expression integral it is join learn listing

This paper mainly focuses on the synchronization mechanism of UPC, through a large number of program instances, so that the learners of UPC language can learn more comprehensively the usage of upc_barrier, upc_notify, upc_wait and upc_fence statements.

Unified Parallel C (UPC) is a parallel programming language that supports multithreading. It is based on the C language to add upc_barrier,upc_notify, and upc_wait statements to control the synchronization between multithreading. In addition, it adds a upc_fence statement to control the synchronization of a single thread to the http://www.aliyun.com/zixun/aggregation/6267.html "> shared data access.

Upc_barrier statement

Role

Upc_barrier statements, also known as blocking barriers. In a multithreaded environment, the execution speed of each thread is different. Using the Upc_barrier statement ensures that no thread can continue to execute down until all threads have been executed to a single point. For example, as shown in Figure 1, the program is executed by 3 threads at the same time, and the program point A has a upc_barrier statement, and when thread 0 first executes to this point A, thread 0 stops to wait until thread 1 and thread 2 also execute to program point A, and thread 0 continues to execute downward. The Upc_barrier statement at program Point a guarantees that any thread cannot proceed forward until all threads have executed to program point A.

Figure 1. Barrier-upc_barrier Statement

Syntax

Upc_barrier integral type expression;

Usage rules

The integer expression here is optional and the type must be an integral type. The presence or absence of an integral expression does not affect the performance of the program, and the role of an integral expression is primarily to help the user diagnose the existence of upc_barrier statements that do not match.

The Upc_barrier integral expression is equivalent to the compound statement {upc_notify integral expression; upc_wait integral expression;

The function of Upc_barrier is equivalent to compound statement {upc_notify; upc_wait;}. We can use the example in Listing 1 to understand the usage of the Upc_barrier statement.

Listing 1. Upc_barrier statement with no integral expression

# include <upc.h>/assume four threads to run the program # include <stdio.h> shared int A; int main () {if (mythread = 0)//is initialized by thread begins to variable a {a=-1} upc_barrier;//No thread must be executed down until all threads have executed to this statement. This ensures that each thread sees the latest initial value of a//1. Assuming there are no upc_barrier statements here, the value of a for each thread below is//may not be the same. printf ("th=%d,a=%d\n", mythread,a); Each thread outputs a value of variable a of return 0; }

Figure 2. Listing 1 Program output

The Upc_barrier statement needs to be invoked by all threads. If an integer expression is attached to the Upc_barrier statement, it is necessary to ensure that its integral expression is consistent for all threads, otherwise a run-time error occurs. As shown in the example in Listing 2.

Listing 2. Example of a upc_barrier statement with an integral type expression

#include <upc.h>//Assume that the program is run by four threads #include <stdio.h> shared int A; int main (void) {if (Mythread = 0) {a=1} if (mythread = 0)//Here causes a run error because the Upc_barrier statement of thread 0 has an integral expression of 1,//For other threads (thread 1, thread 2, line Process 3, the integer expression of the Upc_barrier statement//is called 2. Upc_barrier 1; An integral expression is 1 else upc_barrier 2; Integral Expression 2}

Figure 3. Listing 1 Program output

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.