[CA-3]openMP详细介绍

本文详细介绍了OpenMP的概念和原理,包括阿姆达尔定律、多处理系统、多线程以及Hyperthreading。通过示例展示了如何使用OpenMP进行线程创建、并行区域的设定,以及在多线程中应用锁进行同步控制,同时讨论了多线程在向量加法和点乘计算中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

阿姆达尔定律(Amdahl’s Law)

系统部分性能的提升对于系统整体的影响。这取决于该部分的提升程度与significance.
具体的计算为
在这里插入图片描述
REFERENCE:https://zhuanlan.zhihu.com/p/48022905
在这里插入图片描述

多处理系统(multiprocessor)

在这里插入图片描述
每一个处理器拥有自己的PC(program counter),并且处理互相独立的指令。他们共用一个内存
两种用途1.Deliver high throughput for independent jobs via job-level
parallelism
2.Improve the run time of a single program that has been
specially crafted to run on a multiprocessor - a parallel- processing program

Why Multicore

在这里插入图片描述

线程(thread)

Thread: a sequential flow of instructions that performs some task
每一个线程都有自己的PC和寄存器,并且共用一个内存

Hyperthreading

在这里插入图片描述

openMP

在这里插入图片描述

原理

从作为一个主线程开始,当运行到第一个平行展开区域时
Fork:主线程创造一些平行的线程
Join:当这些子线程完成指令时,他们终止,仅留下主线程
在这里插入图片描述

progma

在这里插入图片描述
在这里插入图片描述

Thread Creation

在这里插入图片描述
注意!!!openMP创建的thread是 software thread,但是在每一个thread都会分配一个hardware thread

omp_set_num_threads(x);//OpenMP intrinsic to set number of threads
num_th = omp_get_num_threads();//OpenMP intrinsic to get number of threads
th_ID = omp_get_thread_num();//OpenMP intrinsic to get Thread ID number

Hello Word

#include <stdio.h>
#include <omp.h>
int main () {
   
   
int nthreads, tid;
/* Fork team of threads with private var tid */
#pragma omp parallel private(tid)
{
   
   
tid = omp_get_thread_num(); /* get thread id */
printf("Hello World from thread = %d\n", tid);
if (tid == 0) {
   
   
/* Only master thread does this */
nthreads = omp_get_num_threads();
printf("Number of threads = %d\n", nthreads);
}
} /* All threads join master and terminate */
}

在这里插入图片描述

锁(lock)

对于某一些元素,我们希望多线程在执行他的时候一个一个执行,比如说求和,那么我没就会在做加法的区域加锁
在这里插入图片描述

多线程向量加法

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <omp.h>

#define ARRAY_SIZE 1000000
#define REPEAT     100

double* gen_array(int n) {
   
   
	double* array = (double*) malloc(n*sizeof(double));
	for(int i=0; i<n; i++)
		array[i] = drand48();
	return array;
}


//original
double dotp(double* x, double* y
PackagesNotFoundError: The following packages are not available from current channels: - zlib==1.2.13=h5eee18b_0 - xz==5.4.2=h5eee18b_0 - wheel==0.38.4=py38h06a4308_0 - tk==8.6.12=h1ccaba5_0 - tbb==2021.8.0=hdb19cb5_0 - sqlite==3.41.2=h5eee18b_0 - readline==8.2=h5eee18b_0 - python==3.8.17=h955ad1f_0 - pysocks==1.7.1=py38h06a4308_0 - pyopenssl==23.2.0=py38h06a4308_0 - pip==23.2.1=py38h06a4308_0 - openssl==3.0.10=h7f8727e_2 - ncurses==6.4=h6a678d5_0 - mpfr==3.1.2=0 - mpc==1.0.1=0 - mkl_random==1.2.2=py38h417a72b_1 - mkl_fft==1.3.6=py38h417a72b_1 - mkl-service==2.4.0=py38h5eee18b_1 - mkl==2023.1.0=h6d00ec8_46342 - libstdcxx-ng==11.2.0=h1234567_1 - libgomp==11.2.0=h1234567_1 - libgfortran5==11.2.0=h1234567_1 - libgfortran-ng==11.2.0=h00389a5_1 - libgcc-ng==11.2.0=h1234567_1 - libffi==3.4.4=h6a678d5_0 - ld_impl_linux-64==2.38=h1181459_1 - isl==0.17.1=0 - intel-openmp==2023.1.0=hdb19cb5_46305 - idna==3.4=py38h06a4308_0 - gmp==6.1.0=1 - gcc-6==6.1.0=2 - cudatoolkit==11.7.0=hd8887f6_10 - cryptography==41.0.2=py38h22a60cf_0 - cffi==1.15.1=py38h5eee18b_3 - ca-certificates==2023.7.22=hbcca054_0 - brotlipy==0.7.0=py38h27cfd23_1003 - _openmp_mutex==5.1=1_gnu Current channels: - https://conda.anaconda.org/omgarcia - https://conda.anaconda.org/conda-forge - https://repo.anaconda.com/pkgs/main - https://repo.anaconda.com/pkgs/r - https://repo.anaconda.com/pkgs/msys2 To search for alternate channels that may provide the conda package you&#39;re looking for, navigate to https://anaconda.org and use the search bar at the top of the page. 这是为什么
06-11
(ostrack_win) D:\anaconda\OSTrack-main>conda env create -f ostrack_cuda113_env.yaml Collecting package metadata (repodata.json): done Solving environment: failed ResolvePackageNotFound: - gmp==6.2.1=h58526e2_0 - ca-certificates==2022.3.29=h06a4308_0 - cudatoolkit==11.3.1=h2bc3f7f_2 - certifi==2021.10.8=py38h578d9bd_2 - sqlite==3.38.2=hc218d9a_0 - torchvision==0.11.0=py38_cu113 - python_abi==3.8=2_cp38 - freetype==2.10.4=h0708190_1 - openssl==1.1.1n=h7f8727e_0 - nettle==3.6=he412f7d_0 - pip==21.2.4=py38h06a4308_0 - pillow==6.2.1=py38h6b7be26_0 - libiconv==1.16=h516909a_0 - openh264==2.1.1=h780b84a_0 - libgcc-ng==9.3.0=h5101ec6_17 - _openmp_mutex==4.5=1_gnu - mkl-service==2.4.0=py38h497a2fe_0 - zlib==1.2.11=h7f8727e_4 - ncurses==6.3=h7f8727e_2 - readline==8.1.2=h7f8727e_1 - intel-openmp==2021.4.0=h06a4308_3561 - mkl_random==1.2.2=py38h1abd341_0 - tk==8.6.11=h1ccaba5_0 - numpy==1.21.2=py38h20f2e39_0 - numpy-base==1.21.2=py38h79a1101_0 - gnutls==3.6.13=h85f3911_1 - typing_extensions==4.1.1=pyha770c72_0 - python==3.8.13=h12debd9_0 - libuv==1.40.0=h7b6447c_0 - lz4-c==1.9.3=h9c3ff4c_1 - libgomp==9.3.0=h5101ec6_17 - torchaudio==0.10.0=py38_cu113 - libpng==1.6.37=h21135ba_2 - mkl==2021.4.0=h06a4308_640 - setuptools==58.0.4=py38h06a4308_0 - xz==5.2.5=h7b6447c_0 - libtiff==4.0.10=hc3755c2_1005 - ffmpeg==4.3=hf484d3e_0 - bzip2==1.0.8=h7f98852_4 - jpeg==9d=h7f8727e_0 - olefile==0.46=pyh9f0ad1d_1 - six==1.16.0=pyh6c4a22f_0 - pytorch-mutex==1.0=cuda - libstdcxx-ng==9.3.0=hd4cf53a_17 - zstd==1.4.9=ha95c52a_0 - lame==3.100=h7f98852_1001 - pytorch==1.10.0=py3.8_cuda11.3_cudnn8.2.0_0 - libffi==3.3=he6710b0_2 - mkl_fft==1.3.1=py38hd3c417c_0
05-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值