项目作者: ysh329

项目描述 :
Learn OpenMP examples step by step
高级语言: C
项目地址: git://github.com/ysh329/OpenMP-101.git
创建时间: 2019-01-12T00:47:34Z
项目社区:https://github.com/ysh329/OpenMP-101

开源协议:Apache License 2.0

下载


OpenMP-101

Optimization Notice

opt-img-01
opt-img-zh

0. Fast Guide: OMP in Caffe

0.1 What’s the OMP

  • an easy, portable and scalable way to parallelize applications for many cores. – Multi-threaded, shared memory model (like pthreads)
  • a standard API
  • omp pragmas are supported by major C/C++ , Fortran compilers (gcc, icc, etc).

A lot of good tutorials on-line:

0.2 OpenMP programming model

omp program model

0.3 Example

naive implementation

  1. int main(int argc, char *argv[])
  2. {
  3. int idx;
  4. float a[N], b[N], c[N];
  5. for(idx=0; idx<N; ++idx)
  6. {
  7. a[idx] = b[idx] = 1.0;
  8. }
  9. for(idx=0; idx<N; ++idx)
  10. {
  11. c[idx] = a[idx] + b[idx];
  12. }
  13. }

omp implementation

  1. #include <omp.h>
  2. int main(int argc, char *argv[])
  3. {
  4. int idx;
  5. float a[N], b[N], c[N];
  6. #pragma omp parallel for
  7. for(idx=0; idx<N; ++idx)
  8. {
  9. a[idx] = b[idx] = 1.0;
  10. }
  11. #pragma omp parallel for
  12. for(idx=0; idx<N; ++idx)
  13. {
  14. c[idx] = a[idx] + b[idx];
  15. }
  16. }
  1. #include <omp.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #define N (100)
  5. int main(int argc, char *argv[])
  6. {
  7. int nthreads, tid, idx;
  8. float a[N], b[N], c[N];
  9. nthreads = omp_get_num_threads();
  10. printf("Number of threads = %d\n", nthreads);
  11. #pragma omp parallel for
  12. for(idx=0; idx<N; ++idx)
  13. {
  14. a[idx] = b[idx] = 1.0;
  15. }
  16. #pragma omp parallel for
  17. for(idx=0; idx<N; ++idx)
  18. {
  19. c[idx] = a[idx] + b[idx];
  20. tid = omp_get_thread_num();
  21. printf("Thread %d: c[%d]=%f\n", tid, idx, c[idx]);
  22. }
  23. }

0.4 Compiling, linking etc

You need to add flag –fopenmp

  1. # compile using gcc
  2. gcc -fopenmp omp_vecadd.c -o vecadd
  3. # compile using icc
  4. icc -openmp omp_vecadd.c -o vecad

Control number of threads through set enviroment variable on command line:

  1. export OMP_NUM_THREADS=8

0.5 Exercise

  1. Implement
    • vector dot-product: c=
    • matrix-matrix multiply
    • 2D matrix convolution
  2. Add openmp support to relu, and max-pooling layers

note

synch and critical sections,

  • use critical section to reduce false sharing
  • BUT don’t put critical sections inside tight loops - doing so serializes things

improve_performance_for_deep_learning_frameworks_on_cpu

Tutorial1: Introduction to OpenMP

Intel’s Tim Mattson’s Introduction to OpenMP video tutorial is now available.

Outline:

Unit 1: Getting started with OpenMP

  • Module 1: Introduction to parallel programming
  • Module 2: The boring bits: Using an OpenMP compiler (hello world)
  • Discussion 1: Hello world and how threads work

Unit 2: The core features of OpenMP

  • Module 3: Creating Threads (the Pi program)
  • Discussion 2: The simple Pi program and why it sucks
  • Module 4: Synchronization (Pi program revisited)
  • Discussion 3: Synchronization overhead and eliminating false sharing
  • Module 5: Parallel Loops (making the Pi program simple)
  • Discussion 4: Pi program wrap-up

Unit 3: Working with OpenMP

  • Module 6: Synchronize single masters and stuff
  • Module 7: Data environment
  • Discussion 5: Debugging OpenMP programs
  • Module 8: Skills practice … linked lists and OpenMP
  • Discussion 6: Different ways to traverse linked lists

Unit 4: a few advanced OpenMP topics

  • Module 9: Tasks (linked lists the easy way)
  • Discussion 7: Understanding Tasks
  • Module 10: The scary stuff … Memory model, atomics, and flush (pairwise synch).
  • Discussion 8: The pitfalls of pairwise synchronization
  • Module 11: Threadprivate Data and how to support libraries (Pi again)
  • Discussion 9: Random number generators

Unit 5: Recapitulation

Thanks go to the University Program Office at Intel for making this tutorial available.

Tutorial2: OpenMP

Author: Blaise Barney, Lawrence Livermore National Laboratory

OpenMP

Tutorial3: OpenMP tutorial | Goulas Programming Soup

https://goulassoup.wordpress.com/2011/10/28/openmp-tutorial/

reference