多核程序设计

一. 实验目的:
1. 掌握OpenMP常用编译指导语句的用法
2.掌握OpenMP常用运行时库函数的用法。
3.掌握临界区和同步障碍的用法。
二. 实验内容:
1.理解private,firstprivate,lastprivate的区别,分析理解PPT中P91~99页程序,并写出如下程序:
设置工作线程个数为4;串行区中两个共享变量a,b的初值分别为1、2。设置并行区域,要求在并行区域中修改a、b的值分别为a+10、20,结束并行区,输出a,b的值。要求变量a设置为firstprivate和lastprivate类型,b设置为private类型。共输出三次a、b的值,并行区域输出一次,进入并行区之前和之后各输出一次。

源代码:

#include “stdafx.h”
#include “stdafx.h”
#include
#include
int main(int argc, char* argv[]){
omp_set_num_threads(4);
int a=1,b=2;
printf(“a=%d,b=%d,当前工作线程号=%d\n”,a,b,omp_get_thread_num());
printf(“并行区开始\n”);
#pragma omp parallel for firstprivate(a), lastprivate(a), private(b)
for(int i=0;i<4;i++){
int tid=omp_get_thread_num();
a=10+a;
b=20;
printf(“a=%d,b=%d,当前工作线程号=%d\n”,a,b,tid);}
printf(“并行区结束\n”);
printf(“a=%d,b=%d,当前工作线程号=%d\n”,a,b,omp_get_thread_num());}

运行结果截图:

2.掌握threadprivate()、copyin()、copyprivate的用法,运行P114、125、128程序,分析运行结果。

P114源代码:
#include “stdafx.h”

#include “stdafx.h”
#include
#include
int x;
#pragma omp threadprivate(x)
int main()
{
int a;
omp_set_num_threads(4);
a=-1;x=-2;
printf(“a=%d,x=%d,当前工作线程号=%d\n”,a,x,omp_get_thread_num());
#pragma omp parallel private(a)
{a=0;
x=omp_get_thread_num();
printf(“a=%d,x=%d\n”,a,x);
}
printf(“a=%d,x=%d,当前工作线程号=%d\n”,a,x,omp_get_thread_num());
return 0;
}
运行结果:

P125源代码:
#include “stdafx.h”

#include “stdafx.h”
#include
#include
int global=6;
#pragma omp threadprivate (global)
int main()
{ global=1000;
#pragma omp parallel copyin (global)
{ printf(“firstglobal=%d\n”,global);
global=omp_get_thread_num(); }
printf(“midglobal=%d\n”,global);
printf(“parallel again\n”);
#pragma omp parallel
printf(“lastglobal=%d\n”,global);
return 0;}
运行截图

P128源代码
#include “stdafx.h”

#include “stdafx.h”
#include
#include
int counter = 0;
#pragma omp threadprivate(counter)
int increment_counter()
{counter++;
return(counter);
}
int main()
{
#pragma omp parallel
{int count;
#pragma omp single copyprivate(counter)
{counter = 50;}
count = increment_counter();
printf(“ThreadId: %ld, count = %ld\n”, omp_get_thread_num(), count);
}
}
运行截图:

  1. 把下列快速排序串行代码修改为并行代码。
    #include “stdafx.h”
    #include
    void quickSort(int *num,int low,int high);//进行分区
    int Partition(int num,int low,int high);//返回分离点
    int _tmain(int argc, _TCHAR
    argv[])
    {
    int num[]={2,3,5,623,32,4324,3,24};//8
    quickSort(num,0,7);
    int i;
    for(i=0;i<8;i++)
    printf("%d\n",num[i]);
    return 0;
    }

void quickSort(int *num,int low,int high)
{
if(low {
int split=Partition(num,low,high);
quickSort(num,low,split-1);
quickSort(num,split+1,high);
}

}

}

int Partition(int *num,int low,int high)
{
int temp=num[low];//作为中轴
while(low {
while(low=temp)high–;
num[low]=num[high];
while(low num[high]=num[low];
}
num[low]=temp;
return low;//返回中轴的位置,再进行分离
}
并发执行代码:
#include “stdafx.h”
#include
void quickSort(int *num,int low,int high);//进行分区
int Partition(int num,int low,int high);//返回分离点
int _tmain(int argc, _TCHAR
argv[])
{
int num[]={2,3,5,623,32,4324,3,24};//8
quickSort(num,0,7);
int i;
for(i=0;i<8;i++)
printf("%d\n",num[i]);
return 0;
}

void quickSort(int *num,int low,int high)
{
if(low {
int split=Partition(num,low,high);
#pragma omp parallel sections
{
#pragma omp section
quickSort(num,low,split-1);
#pragma omp section
quickSort(num,split+1,high);
}
}
}

int Partition(int *num,int low,int high)
{
int temp=num[low];//作为中轴
while(low {
while(low=temp)high–;
num[low]=num[high];
while(low num[high]=num[low];
}
num[low]=temp;
return low;//返回中轴的位置,再进行分离
}

运行结果截图

你可能感兴趣的:(多核程序设计)