第六章-OpenMp多线程编程笔记(intel)
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |
期末考点
- 6.1
- 6.10
- 6.11
- 6.15
- 6.18
- 6.21
6.1
#include <stdio.h>
#include "omp.h"
int main(int argc, char * argv[])
{
printf("Hello from serial.\n");
printf("Thread number=%d\n", omp_get_thread_num());
#pragma omp parallel
{
printf("Hello from parallel. Thread number=%d,Total number of threads=%d\n", omp_get_thread_num(), omp_get_num_threads());
}
printf("The total number of threads = %d\n", omp_get_num_threads());
printf("Hello from serial again.\n");
return 0;
}
并行区域多线程并行执行,所以多次执行产生结果不同,每个线程都完整的复制一遍代码执行一遍。
6.10
#include <stdio.h>
#include "omp.h"
int main(int argc,char *argv[])
{
#pragma omp parallel for
for (int i = 0;i<5;i++)
{
printf("hello world i=%d,The current thread number is %d\n", i, (int)omp_get_thread_num());
}
return 0;
}
循环并行化采用工作分配的执行方式,将循环所需要的所有工作量按照一定的方式分配到各个执行线程中,其工作量与串行工作量一致
6.11
#include <stdio.h>
int counter = 0;
#pragma omp threadprivate(counter) //using threadprivate
void inc_counter()
{
counter++;
}
int main(int argc, char* argv[])
{
#pragma omp parallel
for (int i = 0;i<100000000;i++)
{
inc_counter();
}
printf("counter=%d\n", counter);
//printf("counter=%d,The current thread number is %d\n",counter,omp_get_thread_num());
return 0;
}
结果恒定
若将程序第三行注释掉的话,下面的例子
#include <stdio.h>
int counter = 0;
//#pragma omp threadprivate(counter) //using threadprivate
void inc_counter()
{
counter++;
}
int main(int argc, char* argv[])
{
#pragma omp parallel
for (int i = 0;i<100000000;i++)
{
inc_counter();
}
printf("counter=%d\n", counter);
//printf("counter=%d,The current thread number is %d\n",counter,omp_get_thread_num());
return 0;
}
结果未定
6.15
#include <stdio.h>
#include "omp.h"
int main(int argc,char *argv[])
{
#pragma omp parallel
{
printf("outside loop thread=%d\n", omp_get_thread_num());
#pragma omp for
for (int i = 0;i<3;i++)
{
printf("inside loop i=%d thread =%d\n", i, omp_get_thread_num());
}
}
return 0;
}
尽管创建的线程数改变了,但是循环次数还是跟按照串行执行次数一致。只不过循环的所有任务被各个线程分别完成。
6.18
#include <stdio.h>
#include "omp.h"
long long counter = 0;
int main(int argc,char *argv[])
{
#pragma omp parallel
{
for (int i = 0; i < 10000000;i++)
//#pragma omp atomic //atomic operation
counter++;
}
printf("counter=%lld\n", counter);
return 0;
}
使用了锁机制,所以输出等于循环次数*创建的线程数,若将锁机制去掉,结果不确定。
6.21
#include <stdio.h>
#include "omp.h"
void initialization()
{
int counter = 0;
printf("thread %d start initialization\n", omp_get_thread_num());
for (int i = 0;i<100000;i++)
{
counter++;
}
printf("thread %d finish initialization\n", omp_get_thread_num());
}
void process()
{
int counter = 0;
printf("thread %d start process\n", omp_get_thread_num());
for (int i = 0;i<100000;i++)
{
counter += i;
}
printf("thread %d finish process\n", omp_get_thread_num());
}
int main(int argc,char *argv[])
{
#pragma omp parallel
{
initialization();
#pragma omp barrier
process();
}
return 0;
}
使用明确的同步屏障语句,只有等所有的线程都完成初始化操作以后,才能够进行下一步的处理动作。故运行结果都是先初始化后运行。
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |