Parallel Computing _ simple parallel computing instance
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
# Include
# Include
# Include
# Include
# 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 ;}