Parallel Computing _ simple parallel computing instance

Source: Internet
Author: User

From: Click to open the link


Computing from 1 ~ Sum of 1e10

// Paralle. cpp: defines the entry point of the console application. // # Include "stdafx. H "# include <OMP. h> # include <stdio. h> # include <stdlib. h> # include <windows. h> # include <time. h> # define num_threads 4int _ tmain (INT argc, _ tchar * argv []) {omp_set_num_threads (num_threads); long sum = 0; long sumtmp [num_threads]; clock_t T1 = clock (); # pragma OMP parallel {long I; long id = omp_get_thread_num (); long temp = 0l; # pragma OMP forfor (I = 1; I <= 1000000000; I ++) {temp + = I;} sumtmp [ID] = temp;} For (long I = 0; I <num_threads; I ++) {sum + = sumtmp [I];} clock_t t2 = clock (); printf ("sum = % LLD \ n", sum ); printf ("parallel time = % d \ n", (t2-t1); sum = 0; T1 = clock (); For (long I = 1; I <= 1000000000; I ++) sum + = I; t2 = clock (); printf ("sum = % LLD \ n", sum ); printf ("Serial Time = % d \ n", (t2-t1); System ("pause"); Return 0 ;}


Parallel Computing _ simple parallel computing instance

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.