Программа 1 #include <stdio.h> #include <omp.h> using namespace std; int main() { omp_set_num_threads( 2 ); printf("It is the first sequential region. \n"); #pragma omp parallel { printf("Thread ID = %d is working in the parallel region. \n", omp_get_thread_num()); } printf("It is the second sequential region. \n"); return 0; } Программа 2 #include <stdio.h> #include <omp.h> using namespace std; int main() { int threadID; omp_set_num_threads(2); #pragma omp parallel num_threads(3) { printf("The first parallel region. \n"); printf("The Thread ID = %d from %d threads is working... \n", omp_get_thread_num(), omp_get_num_threads()); } #pragma omp parallel { printf("The second parallel region. \n"); printf("The Thread ID = %d from %d threads is working... \n", omp_get_thread_num(), omp_get_num_threads()); } return 0; } Программа 3 #include <stdio.h> #include <omp.h> using namespace std; int factorial(int initValue) { double value = 1; if (initValue==0) return 1; if (initValue>0) value = initValue*factorial(initValue - 1); return value; } int main() { int sum = 0 ; omp_set_num_threads( 4 ); int thread_ID; #pragma omp parallel private(thread_ID) reduction(+:sum) { thread_ID = omp_get_thread_num(); sum=factorial(thread_ID); printf("The factorial for the thread ID= %d equals to %d. \n", thread_ID, sum); } printf("The sum of factorials equals to %d", sum); return 0; } Программа 4 #include <stdio.h> #include <stdlib.h> #include <omp.h> #include <time.h> using namespace std; int main() { int num_of_threads = 4; int dimension = 10000; int chunk_size = dimension/num_of_threads; double *a, *b; double *local_sums, inner_product = 0; a = new double[dimension]; b = new double[dimension]; local_sums = new double[num_of_threads]; srand(time(NULL)); for (int i=0; i<dimension; i++) { a[i] = ((double)rand())/32767; b[i] = ((double)rand())/32767; } for (int i=0; i<num_of_threads; i++) local_sums[i] = 0; omp_set_num_threads(num_of_threads); printf("It's an example of low-level worksharing. \n"); printf("The start of processing. \n"); double start_time = omp_get_wtime(); int thread_ID; #pragma omp parallel private (thread_ID) { thread_ID = omp_get_thread_num(); for (int i = 0; i<chunk_size; i++) local_sums[thread_ID] += a[thread_ID * chunk_size + i] * b[thread_ID * chunk_size + i]; } for (int i=0; i<num_of_threads; i++) { printf("Local sum of the %d chunk %f. \n", i, local_sums[i]); inner_product += local_sums[i]; } double end_time = omp_get_wtime(); printf("Inner product of the A and the B vectors = %f.\n", inner_product); printf("The end of processing. \n"); printf("Estimation time: %f sec. \n", end_time - start_time); delete []a; delete []b; delete []local_sums; return 0; } Программа 5 #include <stdio.h> #include <omp.h> #include <stdlib.h> using namespace std; int main() { int const height = 6; int const width = 30000; int i, j; int matrix[height][width], vector[width]; omp_set_num_threads(4); #pragma omp parallel for schedule(dynamic) for(i=0; i<height; i++) for(j=0; j<width; j++) matrix[i][j] = (int)(rand()/3267.6); printf("The start of matrix processing. \n"); double start_time_for_static = omp_get_wtime(); #pragma omp parallel for schedule(static, 100) for (int i=0; i<width; i++) { vector[i] = matrix[0][i]; matrix[0][i] = matrix[1][i]; matrix[1][i] = vector[i]; } double end_time_for_static = omp_get_wtime(); #pragma omp parallel for schedule(dynamic, 100) for (int i=0; i<width; i++) { vector[i] = matrix[0][i]; matrix[0][i] = matrix[1][i]; matrix[1][i] = vector[i]; } double end_time_for_dynamic = omp_get_wtime(); #pragma omp parallel for schedule(guided, 100) for (int i=0; i<width; i++) { vector[i] = matrix[0][i]; matrix[0][i] = matrix[1][i]; matrix[1][i] = vector[i]; } double end_time_for_guided = omp_get_wtime(); printf("The end of matrix processing. \n"); printf("Static scheduling: %f sec. \n", end_time_for_static - start_time_for_static); printf("Dynamic scheduling: %f sec. \n", end_time_for_dynamic end_time_for_static); printf("Guided scheduling: %f sec. \n", end_time_for_guided - end_time_for_dynamic); return 0; } Программа 6 #include <stdio.h> #include <omp.h> #include <math.h> using namespace std; int main() { int num_of_intervals = 100000; double integral, x, sum = 0; double leftBorder = -5; double rightBorder = 3; double step = (rightBorder - leftBorder) / num_of_intervals; printf("Estimation of the integral value for the function -0.1*x*x*x + 1.6 * x + 6. \n"); printf("x value is changing between [-5; 3] \n"); printf("The number of splitting intervals is %d.\n", num_of_intervals); double start_time = omp_get_wtime(); for (int i = 0; i<num_of_intervals; i++) { x = leftBorder + step * ((double)i - 0.5); sum += -0.1*pow(x, 3) + 1.6 * x + 6; } integral = sum * step; double end_time = omp_get_wtime(); printf("Estimated integral value equals to %f. \n", integral); printf("The time of estimation is %f sec.",end_time - start_time); return 0; } Программа 7 #include <stdio.h> #include <omp.h> #include <math.h> using namespace std; int main() { int num_of_intervals = 100000; double integral, x, sum = 0; double leftBorder = -5; double rightBorder = 3; double step = (rightBorder - leftBorder) / num_of_intervals; printf("Estimation of the integral value for the 0.1*x*x*x + 1.6 * x + 6. \n"); printf("x value is changing between [-5; 3]. \n"); printf("The number of splitting intervals is %d.\n", num_of_intervals); omp_set_num_threads(omp_get_num_procs()); double start_time = omp_get_wtime(); #pragma omp parallel for reduction(+:sum) private(x) for (int i = 0; i<num_of_intervals; i++) { x = leftBorder + step * ((double)i - 0.5); sum += -0.1*pow(x, 3) + 1.6 * x + 6; } integral = sum * step; double end_time = omp_get_wtime(); printf("Estimated integral value equals to %f. \n", integral); printf("The time of estimation is %f sec.",end_timestart_time); return 0; } function - Программа 8 #include <stdio.h> #include <omp.h> #include <math.h> #include <time.h> #include <stdlib.h> using namespace std; double recursive_function01(double a, double b, int i) { double value; if(i==0) return pow(a, 1.2) * sin(7.4 * b); else value = cos(i * a) - sin(-b) * recursive_function01(a, b, i-1); return value; }; double recursive_function02(double a, double b, int i) { double value; if(i==0) return cos(2.5* a) * pow(b, 1.3); else value = cos(b) - 1/(1 - exp(- i * a)) * recursive_function01(a, b, i-1); return value; }; double recursive_function03(double a, double b, int i) { double value; if(i==0) return exp(-a)/exp(-b); else value = 10 * cos(i * a) * sin(-i * b) * recursive_function01(a, b, i-1); return value; }; int main() { int num_of_threads = 3, num_of_elements_in_column = 5; double *data; data = new double[num_of_threads * num_of_elements_in_column]; srand(time(NULL)); for(int i = 0; i < num_of_elements_in_column; i++) for(int j = 0; j < num_of_threads; j++) data[i * num_of_threads + j]= rand()/3276.7; double a_factor = 1.2, b_factor = 2.5; double factor; int thread_ID; omp_set_num_threads(num_of_threads); double start_time = omp_get_wtime(); #pragma omp parallel private (thread_ID) { thread_ID = omp_get_thread_num(); #pragma omp sections lastprivate(factor) { #pragma omp section { factor = recursive_function01( a_factor, b_factor, 20); for(int i = 0; i < num_of_elements_in_column; i++) data[i * num_of_threads] += factor; printf("1st section: Thread ID = %d factor = %f. \n",thread_ID, factor); } #pragma omp section { factor = recursive_function02( a_factor, b_factor, 20); for(int i = 0; i < num_of_elements_in_column; i++) data[i * num_of_threads + 1] += factor; printf("2nd section: Thread ID = %d factor = %f. \n",thread_ID, factor); } #pragma omp section { factor = recursive_function03(a_factor, b_factor, 20); for(int i = 0; i < num_of_elements_in_column; i++) data[i * num_of_threads + 2] += factor; printf("3rd section: Thread ID = %d factor = %f. \n",thread_ID, factor); } } printf("Before the last loop: Thread ID = %d factor = %f. \n",thread_ID, factor); #pragma omp for for(int i=0; i<num_of_threads * num_of_elements_in_column; i++) data[i] += factor; } double end_time=omp_get_wtime(); printf("Executing time: %f sec. \n",end_time - start_time); return 0; } Программа 8