SlideShare a Scribd company logo
‫درس‬4–emory Traffic
Fundamentals of Parallelism & Code Optimization
(C/C++,Fortran)
‫در‬ ‫کدها‬ ‫سازی‬ ‫بهینه‬ ‫و‬ ‫سازی‬ ‫موازی‬ ‫مبانی‬
‫زبانهای‬C/C++,Fortran
Amin Nezarat (Ph.D.)
Assistant Professor at Payame Noor University
aminnezarat@gmail.com www.astek.ir - www.hpclab.ir
‫عناوین‬
‫دوره‬
1.‫های‬ ‫پردازنده‬ ‫معماری‬ ‫با‬ ‫آشنایی‬
‫اینتل‬
2.Vectorization‫معماری‬ ‫در‬
‫اینتل‬ ‫کامپایلرهای‬
3.‫نویسی‬ ‫برنامه‬ ‫با‬ ‫کار‬ ‫و‬ ‫آشنایی‬
‫در‬OpenMP
4.‫با‬ ‫داده‬ ‫تبادل‬ ‫قواعد‬ ‫و‬ ‫اصول‬
‫حافظه‬(Memory Traffic)
Cheap FLOPs
‫سازی‬ ‫برداری‬ ‫از‬ ‫بعد‬
‫کنیم‬ ‫کار‬ ‫چه‬
‫برداری‬‫سازی‬‫عملیاتهای‬‫ریاضی‬‫کم‬
‫هزینه‬،‫است‬‫اما‬‫دسترسی‬‫به‬‫حافظه‬
‫خیلی‬‫پر‬‫هزینه‬‫است‬.
‫ا‬‫بهینه‬ ‫را‬ ‫کش‬ ‫از‬ ‫استفاده‬ ‫شما‬ ‫گر‬
‫فایده‬ ‫کم‬ ‫نیز‬ ‫سازی‬ ‫برداری‬ ،‫نکنید‬
‫بود‬ ‫خواهد‬.
‫به‬ ‫دسترسی‬ ‫نام‬ ‫به‬ ‫ای‬ ‫مسئله‬ ‫با‬ ‫شما‬
FMA‫چیست؟‬
•The FMA instruction set is an extension to the 128 and 256-bit Streaming SIMD
Extensions instructions.
•FMA perform fused multiply–add (FMA) operations.
•FMA4 operation has the form d = round(a · b + c)
•FMA3 operation has the form a = round(a · b + c)
the three-operand form (FMA3) requires that d be the same register as a, b or c
An FMA has only one rounding (it effectively keeps infinite precision for the
internal temporary multiply result), while an ADD + MUL has two
FMA‫چیست؟‬
•‫در‬C‫و‬ ‫استاندارد‬IEEE‫با‬ ‫توان‬ ‫می‬#pragma STDC FP_CONTRACT
ON‫کرد‬ ‫فعال‬ ‫را‬ ‫آن‬
•‫در‬Gcc‫سویچ‬ ‫با‬ ‫فرض‬ ‫پیش‬ ‫صورت‬ ‫به‬-std=gnu*‫فعال‬‫است‬
•‫در‬Clang‫با‬-ffp-contract=fast‫می‬ ‫فعال‬‫شود‬
•‫از‬ ‫توانید‬ ‫می‬ ‫آن‬ ‫از‬ ‫استفاده‬ ‫برای‬Intrinsic‫زبان‬ ‫های‬
‫نمایید‬ ‫استفاده‬
FMA3 Intrinsics: (AVX2 - Intel Haswell(
_mm_fmadd_pd(), _mm 256dp_ddamf_)(
_mm_fmadd_ps(), _mm 256sp_ddamf_)(
FMA4 Intrinsics: (XOP - AMD Bulldozer)
_mm_macc_pd(), _mm 256dp_ccam_)(
_mm_macc_ps(), _mm 256sp_ccam_)(
‫با‬ ‫سازی‬ ‫پیاده‬ ‫از‬ ‫مثالی‬
‫قابلیت‬FMA
float mul_add(float a, float b, float c) {
return a*b + c;
}
__m256 mul_addv(__m256 a, __m256 b, __m256 c) {
return _mm256_add_ps(_mm256_mul_ps(a, b), c);
}
•‫می‬ ‫کامپایلری‬ ‫های‬ ‫سویچ‬ ‫از‬ ‫مناسب‬ ‫استفاده‬ ‫با‬
‫کنید‬ ‫تولید‬ ‫را‬ ‫مناسب‬ ‫خروجی‬ ‫توانید‬
GCC: -O2 -mavx2 -mfma
Clang: -O1 -mavx2 -mfma -ffp-contract=fast
ICC: -O1 -march=core-avx2
MSVC: /O1 /arch:AVX2 /fp:fast
‫تعادل‬ ‫و‬ ‫کاری‬ ‫بار‬
‫ماشین‬
‫کد‬ ‫تعادل‬
How Cheap are FLOPs
Intel Xeon Phi processor 7250
68 cores × 1.2 GHz × 8 vec.lanes × 2 FMA × 2 IPC ≈ 2.6 TFLOP/s
2.6 TFLOP/s × 8 bytes ≈ 21 TB/s MCDRAM bandwidth ≈ 0.48
TB/s
Ratio = 21/0.48 ≈ 43 (FLOPs)/(Memory Access)
𝐵 𝑚 = (
0.48
8
)/2.6 =0.02 words/flops
▷ Ratio > 50 FLOPs/Memory Access — ‫است‬Compute-bound ‫برنامه‬
▷Ratio< 50 FLOPs/Memory Access — ‫است‬Bandwidth-bound ‫برنامه‬
‫مدل‬Roofline‫ظرفیت‬ ‫و‬
‫ریاضی‬ ‫محاسبات‬
Memory Hierarchy
‫پردازنده‬ ‫در‬ ‫حافظه‬ ‫ساختار‬Intel
Xeon Phi-KNL
▷‫دارای‬High Bandwidth Memory (HBM) -
MCDRAM
▷‫پهنای‬ ‫و‬ ‫جبری‬ ‫عملکردهای‬ ‫برای‬ ‫شده‬ ‫بهینه‬
‫باند‬
▷‫سیستم‬ ‫حافظه‬ ‫به‬ ‫مستقیم‬ ‫دسترسی‬
‫پردازنده‬ ‫در‬ ‫حافظه‬ ‫ساختار‬
‫های‬Intel Xeon
▷‫مراتبی‬ ‫سلسله‬ ‫ساختار‬Cache
▷‫دارای‬ ‫دومسیره‬ ‫های‬ ‫پردازنده‬
‫معماری‬NUMA
‫پردازنده‬ ‫در‬ ‫حافظه‬ ‫ساختار‬Intel
Xeon Phi-KNC
▷‫حافظه‬ ‫به‬ ‫مستقیم‬ ‫دسترسی‬16
GiB‫نوع‬ ‫از‬GDDR5
▷‫طریق‬ ‫از‬ ‫فقط‬ ‫سیستم‬ ‫حافظه‬ ‫به‬PCIe
‫دارد‬ ‫دسترسی‬
High-Bandwidth
Memory
‫مختلف‬ ‫حاالت‬HBWM
‫حالت‬Hybrid
▷‫ترکیب‬‫حاالت‬Flat‫و‬
Cache‫است‬
▷‫دو‬ ‫این‬ ‫تقسیم‬ ‫نرخ‬
‫در‬BIOS‫قابل‬
‫است‬ ‫تنظیم‬
‫حالت‬Cache
▷MCDRAM‫یک‬ ‫همانند‬
Last Level Cache(LLC)‫عمل‬
‫میکند‬
▷‫از‬ ‫استفاده‬MCDRAM
‫است‬ ‫خودکار‬ ‫صورت‬ ‫به‬
‫حالت‬Flat
▷MCDRAM‫گره‬ ‫یک‬ ‫همانند‬
NUMA‫کند‬ ‫می‬ ‫عمل‬
▷‫در‬ ‫که‬ ‫آنچه‬ ‫بر‬ ‫کاربران‬
MCDRAM‫گیرد‬ ‫می‬ ‫قرار‬
‫دارند‬ ‫کنترل‬
‫های‬ ‫برنامه‬ ‫کار‬ ‫گردش‬
Bandwidth Bound
numactl Memkind Cache mode
▷Simply run the whole
program in MCDRAM
▷No code modification
required
▷ Manually allocate
BW-critical memory to
MCDRAM
▷Memkind calls need to
be added.
▷ Allow the chip to figure
out how to use
MCDRAM
▷No code modification
required
‫در‬ ‫ها‬ ‫برنامه‬ ‫اجرای‬HBM
‫با‬numactl
▷‫های‬ ‫ماشین‬ ‫درباره‬ ‫اطالعاتی‬ ‫آوردن‬ ‫بدست‬
NUMA‫در‬‫سیستم‬.
▷‫برنامه‬ ‫یک‬ ‫انتساب‬‫به‬HBM
(Flat/Hybrid)
amin@astek% # In Flat mode of MCDRAM
amin@astek% numactl -H available: 3 nodes (0-2)
node 0 cpus: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 . . . 254 255
node 0 size: 98207 MB
node 1 cpus:
node 1 size: 16384 MB
node 2 cpus:
node 2 size: 98207 MB
amin@astek% icc yourapp.c -o run.out -xMIC_AVX2
amin@astek% numactl --membind 1 ./run.out
// ... Application running in HBM ... //
‫حافظه‬ ‫تخصیص‬
‫در‬ ‫برنامه‬ ‫اجرای‬HBM‫با‬
‫کتابخانه‬Memkind
1 #include <hbwmalloc.h>
2 const int n = 1<<10;
3 // Allocation to MCDRAM
4 double* A = (double*) hbw_malloc(sizeof(double)*n);
5 // No replacement for _mm_malloc. Use posix_memalign
6 double* B;
7 int ret = hbw_posix_memalign((void**) &B, 64, sizeof(double)*n);
8 .....
9 // Free with hbw_free
10 hbw_free(A); hbw_free(B);
‫کتابخانه‬ ‫با‬ ‫کامپایل‬Memkind‫و‬
hbwmalloc
‫برنامه‬ ‫کامپایل‬ ‫برای‬C/C++
‫باز‬ ‫متن‬ ‫نسخه‬ ‫دریافت‬ ‫برای‬memkind‫مراجعه‬ ‫زیر‬ ‫آدرس‬ ‫به‬
‫کنید‬:
memkind.github.io/memkind
‫بیشتر‬ ‫اطالعات‬:
http://astek.ir/index.php/articles/hpc/262-mcdram-guide
amin@astek% icpc -lmemkind foo.cc -o runme
amin@astek% g++ -lmemkind foo.cc -o runme
‫سازی‬ ‫ذخیره‬Streaming
▷‫در‬ ،‫بزنید‬ ‫دور‬ ‫را‬ ‫کش‬
RAM‫بنویسید‬
▷‫کش‬ ‫از‬ ‫مواقع‬ ‫سایر‬ ‫در‬
‫کنید‬ ‫استفاده‬
▷ #pragma vector nontemporal
▷ -qopt-streaming-stores=always
Locality in Space
Cache Lines
▷‫کش‬ ‫و‬ ‫مموری‬ ‫بین‬ ‫داده‬ ‫های‬ ‫بالک‬ ‫حداقل‬
‫شود‬ ‫می‬ ‫داده‬ ‫انتقال‬
▷‫آن‬ ‫طول‬ ‫اینتل‬ ‫معماری‬ ‫در‬64‫است‬ ‫بایت‬
▷‫های‬ ‫محدوده‬ ‫در‬64‫مموری‬ ‫در‬ ‫بایتی‬
Align‫شود‬ ‫می‬
8 double precision values
16 single precision
values
64 bytes
‫قاعده‬
‫دسترسی‬ ‫رعایت‬ ‫برای‬ ‫را‬ ‫حلقه‬ ‫ترتیب‬unit-stride
‫کنید‬ ‫تنظیم‬ ‫حافظه‬ ‫به‬
‫بتواند‬ ‫است‬ ‫ممکن‬ ‫کامپایلر‬/‫خودکار‬ ‫صورت‬ ‫به‬ ‫نتواند‬
‫دهد‬ ‫انجام‬ ‫را‬ ‫حلقه‬ ‫تکرارهای‬ ‫جابجایی‬
‫مثال‬:‫دو‬ ‫ضرب‬ ‫سازی‬ ‫ساده‬
‫ماتریس‬
#pragma omp parallel for
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
#pragma vector aligned
for (int k = 0; k < n; k++)
C[i*n+j]+=A[i*n+k]*B[k*n+j];
#pragma omp parallel for
for (int i = 0; i < n; i++)
for (int k = 0; k < n; k++)
#pragma vector aligned
for (int j = 0; j < n; j++)
C[i*n+j]+=A[i*n+k]*B[k*n+j];
Before: After:
‫تبدیل‬ ‫چرا‬AoS‫به‬SoA‫به‬Unit-Stride
‫کند‬ ‫می‬ ‫کمک‬
‫قاعده‬ ‫چند‬
•‫ترتیب‬ ،‫محلی‬ ‫دسترسی‬ ‫بیشترین‬ ‫آوردن‬ ‫بدست‬ ‫برای‬
‫دسترسی‬ ‫که‬ ‫دهید‬ ‫تغییر‬ ‫نحوی‬ ‫به‬ ‫را‬ ‫آرایه‬ ‫ایندکس‬
Unite-Stride‫باشد‬
•‫آرگومان‬ ‫از‬–O2‫حلقه‬ ‫است‬ ‫ممکن‬ ‫کامپایلر‬ ،‫بعد‬ ‫به‬
‫کند‬ ‫تبدیل‬ ‫را‬
•‫دستی‬ ‫صورت‬ ‫به‬ ‫را‬ ‫حلقه‬ ‫تبدیل‬ ،‫پیچیده‬ ‫مورد‬ ‫در‬
‫دهید‬ ‫انجام‬
•‫آوردن‬ ‫بدست‬ ‫برای‬Unit-Stride‫باز‬ ‫به‬ ‫نیاز‬ ‫است‬ ‫ممکن‬
‫باشید‬ ‫داشته‬ ‫متغیرها‬ ‫نوع‬ ‫و‬ ‫سازی‬ ‫ذخیره‬ ‫روش‬ ‫طراحی‬
Locality in Time
‫حلقه‬ ‫بندی‬ ‫قطعه‬:Cache
Blocking
‫حلقه‬ ‫بندی‬ ‫قطعه‬:Cache
Blocking
for (int i = 0; i < m; i++) // Original code:
for (int j = 0; j < n; j++)
compute(a[i], b[j]); // Memory access is unit-stride in j
// Step 1: strip-mine inner loop
for (int i = 0; i < m; i++)
for (int jj = 0; jj < n; jj += TILE)
for (int j = jj; j < jj + TILE; j++)
compute(a[i], b[j]); // Same order of operation as original
// Step 2: permute
for (int jj = 0; jj < n; jj += TILE)
for (int i = 0; i < m; i++)
for (int j = jj; j < jj + TILE; j++) compute(a[i], b[j]);
// Re-use to j=jj sooner
1
2
3
1
2
3
4
5
1
2
3
4
5
‫حلقه‬ ‫بندی‬ ‫قطعه‬:Register
Blocking
‫حلقه‬ ‫بندی‬ ‫قطعه‬:Unroll/Register
Blocking
for (int i = 0; i < m; i++) // Original code:
for (int j = 0; j < n; j++)
compute(a[i], b[j]); // Memory access is unit-stride in j
// Step 1: strip-mine outer loop
for (int ii = 0; ii < m; ii += TILE)
for (int i = ii; i < ii + TILE; i++)
for (int j = 0; j < n; j++)
compute(a[i], b[j]); // Same order of operation as original
// Step 2: permute and vectorize outer loop
for (int ii = 0; ii < m; ii += TILE)
#pragma simd
for (int j = 0; j < n; j++)
for (int i = ii; i < ii + TILE; i++)
compute(a[i], b[j]); //each vector in b[j] a total of TILE time
1
2
3
1
2
3
4
5
1
2
3
4
5
6
‫روش‬Loop Fusion(‫ادغام‬
‫حلقه‬)
‫ها‬ ‫حلقه‬ ‫ادغام‬ ‫بوسیله‬ ‫کش‬ ‫از‬ ‫مجدد‬ ‫استفاده‬
‫الینی‬ ‫پایپ‬ ‫پردازش‬ ‫فرآیند‬ ‫یک‬ ‫در‬
MyData* data = new MyData(n);
for (int i = 0; i < n; i++)
Initialize(data[i]);
for (int i = 0; i < n; i++)
Stage1(data[i]);
for (int i = 0; i < n; i++)
Stage2(data[i]);
MyData* data = new MyData(n);
for (int i = 0; i < n; i++) {
Initialize(data[i]);
Stage1(data[i]);
Stage2(data[i]);
}
‫ا‬‫جانبی‬ ‫مثبت‬ ‫ثرات‬:،‫شوند‬ ‫می‬ ‫جابجا‬ ‫مراحل‬ ‫بین‬ ‫کمتری‬ ‫داده‬
‫کارایی‬ ‫افزایش‬ ،‫حافظه‬ ‫به‬ ‫ارجاعات‬ ‫کاهش‬
1
2
3
4
5
6
7
8
9
10
1
2
3
4
5
6
7
8
9
10
11
‫مثال‬:Stencil Code
‫عملگرهای‬Stencil
•‫خطی‬ ‫سیستمهای‬
‫معادالت‬
•‫معادالت‬
‫جزئی‬ ‫دیفرانسیل‬
Fluid dynamics, heat transfer, image processing (convolution matrix),
cellular automata.
‫لبه‬ ‫تشخیص‬
‫باند‬ ‫پهنای‬ ‫با‬ ‫حافظه‬
‫زیاد‬HBM
‫اول‬ ‫روش‬:numactl
‫دوم‬ ‫روش‬:Memkind
amin@astek% numactl -m 1 ./stencil my-image.png
#include <hbwmalloc.h>
...
hbw_posix_memalign((void**)&pixel, 64, sizeof(P)*width*height);
amin@astek% icpc -o stencil *.o -lpng -lmemkind
‫سازهای‬ ‫ذخیره‬Streaming
#pragma omp parallel for
for (int i = 1; i < height-1; i++)
#pragma omp simd
#pragma vector nontemporal
for (int j = 1; j < width-1; j++) out[i*width + j] =
-in[(i-1)*width + j-1] -in[(i-1)*width + j] - in[(i-1)*width + j+1]
-in[(i)*width + j-1] + 8*in[(i)*width + j] - in[(i)*width + j+1]
-in[(i+1)*width + j-1] -in[(i+1)*width+ j] - in[(i+1)*width+ j+1];
1
2
3
4
5
6
7
8
‫عملکرد‬
‫کمکی‬ ‫اطالعات‬
FLOPs‫از‬ ‫تعدادی‬
‫ها‬ ‫پردازنده‬
Intel Core 2 and Nehalem:
4 DP FLOPs/cycle: 2-wide SSE2 addition + 2-wide SSE2 multiplication
8 SP FLOPs/cycle: 4-wide SSE addition + 4-wide SSE multiplication
Intel Sandy Bridge/Ivy Bridge:
8 DP FLOPs/cycle: 4-wide AVX addition + 4-wide AVX multiplication
16 SP FLOPs/cycle: 8-wide AVX addition + 8-wide AVX multiplication
Intel Haswell/Broadwell/Skylake/Kaby Lake:
16 DP FLOPs/cycle: two 4-wide FMA (fused multiply-add) instructions
32 SP FLOPs/cycle: two 8-wide FMA (fused multiply-add) instructions
AMD K10:
4 DP FLOPs/cycle: 2-wide SSE2 addition + 2-wide SSE2 multiplication
8 SP FLOPs/cycle: 4-wide SSE addition + 4-wide SSE multiplication
AMD Bulldozer/Piledriver/Steamroller/Excavator, per module (two cores):
8 DP FLOPs/cycle: 4-wide FMA
16 SP FLOPs/cycle: 8-wide FMA
AMD Ryzen
8 DP FLOPs/cycle: 4-wide FMA
16 SP FLOPs/cycle: 8-wide FMA
Intel Atom (Bonnell/45nm, Saltwell/32nm, Silvermont/22nm):
1.5 DP FLOPs/cycle: scalar SSE2 addition + scalar SSE2 multiplication every other cycle
6 SP FLOPs/cycle: 4-wide SSE addition + 4-wide SSE multiplication every other cycle
Intel Xeon Phi (Knights Corner), per thread:
8 DP FLOPs/cycle: 8-wide FMA every other cycle
16 SP FLOPs/cycle: 16-wide FMA every other cycle
Intel Xeon Phi (Knights Landing), per core:
32 DP FLOPs/cycle: two 8-wide FMA every cycle
64 SP FLOPs/cycle: two 16-wide FMA every cycle
FLOPs‫از‬ ‫تعدادی‬
‫ها‬ ‫پردازنده‬AMD Bobcat:
1.5 DP FLOPs/cycle: scalar SSE2 addition + scalar SSE2 multiplication every other cycle
4 SP FLOPs/cycle: 4-wide SSE addition every other cycle + 4-wide SSE multiplication every other cycle
AMD Jaguar:
3 DP FLOPs/cycle: 4-wide AVX addition every other cycle + 4-wide AVX multiplication in four cycles
8 SP FLOPs/cycle: 8-wide AVX addition every other cycle + 8-wide AVX multiplication every other cycle
ARM Cortex-A9:
1.5 DP FLOPs/cycle: scalar addition + scalar multiplication every other cycle
4 SP FLOPs/cycle: 4-wide NEON addition every other cycle + 4-wide NEON multiplication every other cycle
ARM Cortex-A15:
2 DP FLOPs/cycle: scalar FMA or scalar multiply-add
8 SP FLOPs/cycle: 4-wide NEONv2 FMA or 4-wide NEON multiply-add
Qualcomm Krait:
2 DP FLOPs/cycle: scalar FMA or scalar multiply-add
8 SP FLOPs/cycle: 4-wide NEONv2 FMA or 4-wide NEON multiply-add
IBM PowerPC A2 (Blue Gene/Q), per core:
8 DP FLOPs/cycle: 4-wide QPX FMA every cycle
SP elements are extended to DP and processed on the same units
IBM PowerPC A2 (Blue Gene/Q), per thread:
4 DP FLOPs/cycle: 4-wide QPX FMA every other cycle
SP elements are extended to DP and processed on the same units
Intel Xeon Phi (Knights Corner), per core:
16 DP FLOPs/cycle: 8-wide FMA every cycle
32 SP FLOPs/cycle: 16-wide FMA every cycle

More Related Content

Similar to 04 memory traffic_fundamentals_of_parallelism_and_code_optimization-www.astek.ir - copy

Pdn tech-netfilter&iptables-ver2.1.0
Pdn tech-netfilter&iptables-ver2.1.0Pdn tech-netfilter&iptables-ver2.1.0
Pdn tech-netfilter&iptables-ver2.1.0
pdnsoftco
 
Pdn tech-netfilter&iptables-ver2.1.0
Pdn tech-netfilter&iptables-ver2.1.0Pdn tech-netfilter&iptables-ver2.1.0
Pdn tech-netfilter&iptables-ver2.1.0pdnsoftco
 
دانلود رایگان کد فایل آموزشی الگوریتم ژنتیک چند هدفه NSGA II در متلب
دانلود رایگان کد فایل آموزشی الگوریتم ژنتیک چند هدفه NSGA II در متلبدانلود رایگان کد فایل آموزشی الگوریتم ژنتیک چند هدفه NSGA II در متلب
دانلود رایگان کد فایل آموزشی الگوریتم ژنتیک چند هدفه NSGA II در متلب
کتابخانه خانه متلب
 
Usefull Linux command
Usefull Linux commandUsefull Linux command
Usefull Linux command
Hosein Zare
 
Radmanesh c#-1
Radmanesh c#-1Radmanesh c#-1
Radmanesh c#-1
neginrmn
 
Recovery in gnu/linux
Recovery in gnu/linux Recovery in gnu/linux
Recovery in gnu/linux
Yashar Esmaildokht
 
تنظیم فایروال میکروتیک برای دسترسی سیستمها به اینترنت
تنظیم فایروال میکروتیک برای دسترسی سیستمها به اینترنت   تنظیم فایروال میکروتیک برای دسترسی سیستمها به اینترنت
تنظیم فایروال میکروتیک برای دسترسی سیستمها به اینترنت
abbas pirnazaraine
 
Machine & Assembly Language - Chapter 2
Machine & Assembly Language - Chapter 2Machine & Assembly Language - Chapter 2
Machine & Assembly Language - Chapter 2
M Nemati
 
Softwares : Vuln discovery , attack and deffence technologies
Softwares : Vuln discovery , attack and deffence technologiesSoftwares : Vuln discovery , attack and deffence technologies
Softwares : Vuln discovery , attack and deffence technologies
hamid.k
 
راهنماي راه اندازی سرویس Golden Gate Microservices
راهنماي  راه اندازی سرویس Golden Gate  Microservicesراهنماي  راه اندازی سرویس Golden Gate  Microservices
راهنماي راه اندازی سرویس Golden Gate Microservices
Mojtaba Khandan
 
آموزش مرتبه اجرایی در ساختمان داده و طراحی الگوریتم (مرور – تست کنکور ارشد)
آموزش مرتبه اجرایی در ساختمان داده و طراحی الگوریتم (مرور – تست کنکور ارشد)آموزش مرتبه اجرایی در ساختمان داده و طراحی الگوریتم (مرور – تست کنکور ارشد)
آموزش مرتبه اجرایی در ساختمان داده و طراحی الگوریتم (مرور – تست کنکور ارشد)
faradars
 
مسیردهی و پروتوکولهای مسیریابی
مسیردهی و پروتوکولهای مسیریابیمسیردهی و پروتوکولهای مسیریابی
مسیردهی و پروتوکولهای مسیریابی
Muhibullah Aman
 
Cisco Exploration 2 In Persion-Muhibullah Aman
Cisco Exploration 2 In Persion-Muhibullah AmanCisco Exploration 2 In Persion-Muhibullah Aman
Cisco Exploration 2 In Persion-Muhibullah Aman
Muhibullah Aman
 
Bords
BordsBords
Bords
FS Karimi
 
Memory forensics - مبانی پزشکی قانونی حافظه
Memory forensics - مبانی پزشکی قانونی حافظهMemory forensics - مبانی پزشکی قانونی حافظه
Memory forensics - مبانی پزشکی قانونی حافظه
Hosein Khoshraftar
 
آموزش روش تقسیم و حل در طراحی الگوریتم (مرور – تست کنکور ارشد)
آموزش روش تقسیم و حل در طراحی الگوریتم (مرور – تست کنکور ارشد)آموزش روش تقسیم و حل در طراحی الگوریتم (مرور – تست کنکور ارشد)
آموزش روش تقسیم و حل در طراحی الگوریتم (مرور – تست کنکور ارشد)
faradars
 
06 hpc library_fundamentals_of_parallelism_and_code_optimization-www.astek.ir
06 hpc library_fundamentals_of_parallelism_and_code_optimization-www.astek.ir06 hpc library_fundamentals_of_parallelism_and_code_optimization-www.astek.ir
06 hpc library_fundamentals_of_parallelism_and_code_optimization-www.astek.ir
aminnezarat
 
lunch box plugin
 lunch box plugin  lunch box plugin
lunch box plugin
Sajad Omidipour
 

Similar to 04 memory traffic_fundamentals_of_parallelism_and_code_optimization-www.astek.ir - copy (20)

khazeni_taghizade
khazeni_taghizadekhazeni_taghizade
khazeni_taghizade
 
Pdn tech-netfilter&iptables-ver2.1.0
Pdn tech-netfilter&iptables-ver2.1.0Pdn tech-netfilter&iptables-ver2.1.0
Pdn tech-netfilter&iptables-ver2.1.0
 
Pdn tech-netfilter&iptables-ver2.1.0
Pdn tech-netfilter&iptables-ver2.1.0Pdn tech-netfilter&iptables-ver2.1.0
Pdn tech-netfilter&iptables-ver2.1.0
 
دانلود رایگان کد فایل آموزشی الگوریتم ژنتیک چند هدفه NSGA II در متلب
دانلود رایگان کد فایل آموزشی الگوریتم ژنتیک چند هدفه NSGA II در متلبدانلود رایگان کد فایل آموزشی الگوریتم ژنتیک چند هدفه NSGA II در متلب
دانلود رایگان کد فایل آموزشی الگوریتم ژنتیک چند هدفه NSGA II در متلب
 
Usefull Linux command
Usefull Linux commandUsefull Linux command
Usefull Linux command
 
Radmanesh c#-1
Radmanesh c#-1Radmanesh c#-1
Radmanesh c#-1
 
Recovery in gnu/linux
Recovery in gnu/linux Recovery in gnu/linux
Recovery in gnu/linux
 
تنظیم فایروال میکروتیک برای دسترسی سیستمها به اینترنت
تنظیم فایروال میکروتیک برای دسترسی سیستمها به اینترنت   تنظیم فایروال میکروتیک برای دسترسی سیستمها به اینترنت
تنظیم فایروال میکروتیک برای دسترسی سیستمها به اینترنت
 
Machine & Assembly Language - Chapter 2
Machine & Assembly Language - Chapter 2Machine & Assembly Language - Chapter 2
Machine & Assembly Language - Chapter 2
 
Softwares : Vuln discovery , attack and deffence technologies
Softwares : Vuln discovery , attack and deffence technologiesSoftwares : Vuln discovery , attack and deffence technologies
Softwares : Vuln discovery , attack and deffence technologies
 
راهنماي راه اندازی سرویس Golden Gate Microservices
راهنماي  راه اندازی سرویس Golden Gate  Microservicesراهنماي  راه اندازی سرویس Golden Gate  Microservices
راهنماي راه اندازی سرویس Golden Gate Microservices
 
آموزش مرتبه اجرایی در ساختمان داده و طراحی الگوریتم (مرور – تست کنکور ارشد)
آموزش مرتبه اجرایی در ساختمان داده و طراحی الگوریتم (مرور – تست کنکور ارشد)آموزش مرتبه اجرایی در ساختمان داده و طراحی الگوریتم (مرور – تست کنکور ارشد)
آموزش مرتبه اجرایی در ساختمان داده و طراحی الگوریتم (مرور – تست کنکور ارشد)
 
مسیردهی و پروتوکولهای مسیریابی
مسیردهی و پروتوکولهای مسیریابیمسیردهی و پروتوکولهای مسیریابی
مسیردهی و پروتوکولهای مسیریابی
 
Cisco Exploration 2 In Persion-Muhibullah Aman
Cisco Exploration 2 In Persion-Muhibullah AmanCisco Exploration 2 In Persion-Muhibullah Aman
Cisco Exploration 2 In Persion-Muhibullah Aman
 
SystemCall in Linux
SystemCall in LinuxSystemCall in Linux
SystemCall in Linux
 
Bords
BordsBords
Bords
 
Memory forensics - مبانی پزشکی قانونی حافظه
Memory forensics - مبانی پزشکی قانونی حافظهMemory forensics - مبانی پزشکی قانونی حافظه
Memory forensics - مبانی پزشکی قانونی حافظه
 
آموزش روش تقسیم و حل در طراحی الگوریتم (مرور – تست کنکور ارشد)
آموزش روش تقسیم و حل در طراحی الگوریتم (مرور – تست کنکور ارشد)آموزش روش تقسیم و حل در طراحی الگوریتم (مرور – تست کنکور ارشد)
آموزش روش تقسیم و حل در طراحی الگوریتم (مرور – تست کنکور ارشد)
 
06 hpc library_fundamentals_of_parallelism_and_code_optimization-www.astek.ir
06 hpc library_fundamentals_of_parallelism_and_code_optimization-www.astek.ir06 hpc library_fundamentals_of_parallelism_and_code_optimization-www.astek.ir
06 hpc library_fundamentals_of_parallelism_and_code_optimization-www.astek.ir
 
lunch box plugin
 lunch box plugin  lunch box plugin
lunch box plugin
 

More from aminnezarat

Health-medicine-and-Block-chain1402-1-12.pptx
Health-medicine-and-Block-chain1402-1-12.pptxHealth-medicine-and-Block-chain1402-1-12.pptx
Health-medicine-and-Block-chain1402-1-12.pptx
aminnezarat
 
ارائه ابزار.pptx
ارائه ابزار.pptxارائه ابزار.pptx
ارائه ابزار.pptx
aminnezarat
 
00 - BigData-Chapter_01-PDC.pdf
00 - BigData-Chapter_01-PDC.pdf00 - BigData-Chapter_01-PDC.pdf
00 - BigData-Chapter_01-PDC.pdf
aminnezarat
 
Smart Data Strategy EN (1).pdf
Smart Data Strategy EN (1).pdfSmart Data Strategy EN (1).pdf
Smart Data Strategy EN (1).pdf
aminnezarat
 
slides8 SharedMemory.ppt
slides8 SharedMemory.pptslides8 SharedMemory.ppt
slides8 SharedMemory.ppt
aminnezarat
 
BASIC_MPI.ppt
BASIC_MPI.pptBASIC_MPI.ppt
BASIC_MPI.ppt
aminnezarat
 
Chap2 GGKK.ppt
Chap2 GGKK.pptChap2 GGKK.ppt
Chap2 GGKK.ppt
aminnezarat
 
01 introduction fundamentals_of_parallelism_and_code_optimization-www.astek.ir
01 introduction fundamentals_of_parallelism_and_code_optimization-www.astek.ir01 introduction fundamentals_of_parallelism_and_code_optimization-www.astek.ir
01 introduction fundamentals_of_parallelism_and_code_optimization-www.astek.ir
aminnezarat
 
Machine learning and big-data-in-physics 13970711-Dr. Amin Nezarat
Machine learning and big-data-in-physics 13970711-Dr. Amin NezaratMachine learning and big-data-in-physics 13970711-Dr. Amin Nezarat
Machine learning and big-data-in-physics 13970711-Dr. Amin Nezarat
aminnezarat
 
Big data HPC Convergence-Dr. Amin-Nezarat-(aminnezarat@gmail.com)-2019
Big data HPC Convergence-Dr. Amin-Nezarat-(aminnezarat@gmail.com)-2019Big data HPC Convergence-Dr. Amin-Nezarat-(aminnezarat@gmail.com)-2019
Big data HPC Convergence-Dr. Amin-Nezarat-(aminnezarat@gmail.com)-2019
aminnezarat
 
Camera ready-nash equilibrium-ngct2015-format
Camera ready-nash equilibrium-ngct2015-formatCamera ready-nash equilibrium-ngct2015-format
Camera ready-nash equilibrium-ngct2015-format
aminnezarat
 
Data set cloudrank-d-hpca_tutorial
Data set cloudrank-d-hpca_tutorialData set cloudrank-d-hpca_tutorial
Data set cloudrank-d-hpca_tutorial
aminnezarat
 

More from aminnezarat (12)

Health-medicine-and-Block-chain1402-1-12.pptx
Health-medicine-and-Block-chain1402-1-12.pptxHealth-medicine-and-Block-chain1402-1-12.pptx
Health-medicine-and-Block-chain1402-1-12.pptx
 
ارائه ابزار.pptx
ارائه ابزار.pptxارائه ابزار.pptx
ارائه ابزار.pptx
 
00 - BigData-Chapter_01-PDC.pdf
00 - BigData-Chapter_01-PDC.pdf00 - BigData-Chapter_01-PDC.pdf
00 - BigData-Chapter_01-PDC.pdf
 
Smart Data Strategy EN (1).pdf
Smart Data Strategy EN (1).pdfSmart Data Strategy EN (1).pdf
Smart Data Strategy EN (1).pdf
 
slides8 SharedMemory.ppt
slides8 SharedMemory.pptslides8 SharedMemory.ppt
slides8 SharedMemory.ppt
 
BASIC_MPI.ppt
BASIC_MPI.pptBASIC_MPI.ppt
BASIC_MPI.ppt
 
Chap2 GGKK.ppt
Chap2 GGKK.pptChap2 GGKK.ppt
Chap2 GGKK.ppt
 
01 introduction fundamentals_of_parallelism_and_code_optimization-www.astek.ir
01 introduction fundamentals_of_parallelism_and_code_optimization-www.astek.ir01 introduction fundamentals_of_parallelism_and_code_optimization-www.astek.ir
01 introduction fundamentals_of_parallelism_and_code_optimization-www.astek.ir
 
Machine learning and big-data-in-physics 13970711-Dr. Amin Nezarat
Machine learning and big-data-in-physics 13970711-Dr. Amin NezaratMachine learning and big-data-in-physics 13970711-Dr. Amin Nezarat
Machine learning and big-data-in-physics 13970711-Dr. Amin Nezarat
 
Big data HPC Convergence-Dr. Amin-Nezarat-(aminnezarat@gmail.com)-2019
Big data HPC Convergence-Dr. Amin-Nezarat-(aminnezarat@gmail.com)-2019Big data HPC Convergence-Dr. Amin-Nezarat-(aminnezarat@gmail.com)-2019
Big data HPC Convergence-Dr. Amin-Nezarat-(aminnezarat@gmail.com)-2019
 
Camera ready-nash equilibrium-ngct2015-format
Camera ready-nash equilibrium-ngct2015-formatCamera ready-nash equilibrium-ngct2015-format
Camera ready-nash equilibrium-ngct2015-format
 
Data set cloudrank-d-hpca_tutorial
Data set cloudrank-d-hpca_tutorialData set cloudrank-d-hpca_tutorial
Data set cloudrank-d-hpca_tutorial
 

04 memory traffic_fundamentals_of_parallelism_and_code_optimization-www.astek.ir - copy

  • 1. ‫درس‬4–emory Traffic Fundamentals of Parallelism & Code Optimization (C/C++,Fortran) ‫در‬ ‫کدها‬ ‫سازی‬ ‫بهینه‬ ‫و‬ ‫سازی‬ ‫موازی‬ ‫مبانی‬ ‫زبانهای‬C/C++,Fortran Amin Nezarat (Ph.D.) Assistant Professor at Payame Noor University aminnezarat@gmail.com www.astek.ir - www.hpclab.ir
  • 2. ‫عناوین‬ ‫دوره‬ 1.‫های‬ ‫پردازنده‬ ‫معماری‬ ‫با‬ ‫آشنایی‬ ‫اینتل‬ 2.Vectorization‫معماری‬ ‫در‬ ‫اینتل‬ ‫کامپایلرهای‬ 3.‫نویسی‬ ‫برنامه‬ ‫با‬ ‫کار‬ ‫و‬ ‫آشنایی‬ ‫در‬OpenMP 4.‫با‬ ‫داده‬ ‫تبادل‬ ‫قواعد‬ ‫و‬ ‫اصول‬ ‫حافظه‬(Memory Traffic)
  • 4. ‫سازی‬ ‫برداری‬ ‫از‬ ‫بعد‬ ‫کنیم‬ ‫کار‬ ‫چه‬ ‫برداری‬‫سازی‬‫عملیاتهای‬‫ریاضی‬‫کم‬ ‫هزینه‬،‫است‬‫اما‬‫دسترسی‬‫به‬‫حافظه‬ ‫خیلی‬‫پر‬‫هزینه‬‫است‬. ‫ا‬‫بهینه‬ ‫را‬ ‫کش‬ ‫از‬ ‫استفاده‬ ‫شما‬ ‫گر‬ ‫فایده‬ ‫کم‬ ‫نیز‬ ‫سازی‬ ‫برداری‬ ،‫نکنید‬ ‫بود‬ ‫خواهد‬. ‫به‬ ‫دسترسی‬ ‫نام‬ ‫به‬ ‫ای‬ ‫مسئله‬ ‫با‬ ‫شما‬
  • 5. FMA‫چیست؟‬ •The FMA instruction set is an extension to the 128 and 256-bit Streaming SIMD Extensions instructions. •FMA perform fused multiply–add (FMA) operations. •FMA4 operation has the form d = round(a · b + c) •FMA3 operation has the form a = round(a · b + c) the three-operand form (FMA3) requires that d be the same register as a, b or c An FMA has only one rounding (it effectively keeps infinite precision for the internal temporary multiply result), while an ADD + MUL has two
  • 6. FMA‫چیست؟‬ •‫در‬C‫و‬ ‫استاندارد‬IEEE‫با‬ ‫توان‬ ‫می‬#pragma STDC FP_CONTRACT ON‫کرد‬ ‫فعال‬ ‫را‬ ‫آن‬ •‫در‬Gcc‫سویچ‬ ‫با‬ ‫فرض‬ ‫پیش‬ ‫صورت‬ ‫به‬-std=gnu*‫فعال‬‫است‬ •‫در‬Clang‫با‬-ffp-contract=fast‫می‬ ‫فعال‬‫شود‬ •‫از‬ ‫توانید‬ ‫می‬ ‫آن‬ ‫از‬ ‫استفاده‬ ‫برای‬Intrinsic‫زبان‬ ‫های‬ ‫نمایید‬ ‫استفاده‬ FMA3 Intrinsics: (AVX2 - Intel Haswell( _mm_fmadd_pd(), _mm 256dp_ddamf_)( _mm_fmadd_ps(), _mm 256sp_ddamf_)( FMA4 Intrinsics: (XOP - AMD Bulldozer) _mm_macc_pd(), _mm 256dp_ccam_)( _mm_macc_ps(), _mm 256sp_ccam_)(
  • 7. ‫با‬ ‫سازی‬ ‫پیاده‬ ‫از‬ ‫مثالی‬ ‫قابلیت‬FMA float mul_add(float a, float b, float c) { return a*b + c; } __m256 mul_addv(__m256 a, __m256 b, __m256 c) { return _mm256_add_ps(_mm256_mul_ps(a, b), c); } •‫می‬ ‫کامپایلری‬ ‫های‬ ‫سویچ‬ ‫از‬ ‫مناسب‬ ‫استفاده‬ ‫با‬ ‫کنید‬ ‫تولید‬ ‫را‬ ‫مناسب‬ ‫خروجی‬ ‫توانید‬ GCC: -O2 -mavx2 -mfma Clang: -O1 -mavx2 -mfma -ffp-contract=fast ICC: -O1 -march=core-avx2 MSVC: /O1 /arch:AVX2 /fp:fast
  • 8. ‫تعادل‬ ‫و‬ ‫کاری‬ ‫بار‬ ‫ماشین‬
  • 10. How Cheap are FLOPs Intel Xeon Phi processor 7250 68 cores × 1.2 GHz × 8 vec.lanes × 2 FMA × 2 IPC ≈ 2.6 TFLOP/s 2.6 TFLOP/s × 8 bytes ≈ 21 TB/s MCDRAM bandwidth ≈ 0.48 TB/s Ratio = 21/0.48 ≈ 43 (FLOPs)/(Memory Access) 𝐵 𝑚 = ( 0.48 8 )/2.6 =0.02 words/flops ▷ Ratio > 50 FLOPs/Memory Access — ‫است‬Compute-bound ‫برنامه‬ ▷Ratio< 50 FLOPs/Memory Access — ‫است‬Bandwidth-bound ‫برنامه‬
  • 13. ‫پردازنده‬ ‫در‬ ‫حافظه‬ ‫ساختار‬Intel Xeon Phi-KNL ▷‫دارای‬High Bandwidth Memory (HBM) - MCDRAM ▷‫پهنای‬ ‫و‬ ‫جبری‬ ‫عملکردهای‬ ‫برای‬ ‫شده‬ ‫بهینه‬ ‫باند‬ ▷‫سیستم‬ ‫حافظه‬ ‫به‬ ‫مستقیم‬ ‫دسترسی‬
  • 14. ‫پردازنده‬ ‫در‬ ‫حافظه‬ ‫ساختار‬ ‫های‬Intel Xeon ▷‫مراتبی‬ ‫سلسله‬ ‫ساختار‬Cache ▷‫دارای‬ ‫دومسیره‬ ‫های‬ ‫پردازنده‬ ‫معماری‬NUMA
  • 15. ‫پردازنده‬ ‫در‬ ‫حافظه‬ ‫ساختار‬Intel Xeon Phi-KNC ▷‫حافظه‬ ‫به‬ ‫مستقیم‬ ‫دسترسی‬16 GiB‫نوع‬ ‫از‬GDDR5 ▷‫طریق‬ ‫از‬ ‫فقط‬ ‫سیستم‬ ‫حافظه‬ ‫به‬PCIe ‫دارد‬ ‫دسترسی‬
  • 17. ‫مختلف‬ ‫حاالت‬HBWM ‫حالت‬Hybrid ▷‫ترکیب‬‫حاالت‬Flat‫و‬ Cache‫است‬ ▷‫دو‬ ‫این‬ ‫تقسیم‬ ‫نرخ‬ ‫در‬BIOS‫قابل‬ ‫است‬ ‫تنظیم‬ ‫حالت‬Cache ▷MCDRAM‫یک‬ ‫همانند‬ Last Level Cache(LLC)‫عمل‬ ‫میکند‬ ▷‫از‬ ‫استفاده‬MCDRAM ‫است‬ ‫خودکار‬ ‫صورت‬ ‫به‬ ‫حالت‬Flat ▷MCDRAM‫گره‬ ‫یک‬ ‫همانند‬ NUMA‫کند‬ ‫می‬ ‫عمل‬ ▷‫در‬ ‫که‬ ‫آنچه‬ ‫بر‬ ‫کاربران‬ MCDRAM‫گیرد‬ ‫می‬ ‫قرار‬ ‫دارند‬ ‫کنترل‬
  • 18. ‫های‬ ‫برنامه‬ ‫کار‬ ‫گردش‬ Bandwidth Bound numactl Memkind Cache mode ▷Simply run the whole program in MCDRAM ▷No code modification required ▷ Manually allocate BW-critical memory to MCDRAM ▷Memkind calls need to be added. ▷ Allow the chip to figure out how to use MCDRAM ▷No code modification required
  • 19. ‫در‬ ‫ها‬ ‫برنامه‬ ‫اجرای‬HBM ‫با‬numactl ▷‫های‬ ‫ماشین‬ ‫درباره‬ ‫اطالعاتی‬ ‫آوردن‬ ‫بدست‬ NUMA‫در‬‫سیستم‬. ▷‫برنامه‬ ‫یک‬ ‫انتساب‬‫به‬HBM (Flat/Hybrid) amin@astek% # In Flat mode of MCDRAM amin@astek% numactl -H available: 3 nodes (0-2) node 0 cpus: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 . . . 254 255 node 0 size: 98207 MB node 1 cpus: node 1 size: 16384 MB node 2 cpus: node 2 size: 98207 MB amin@astek% icc yourapp.c -o run.out -xMIC_AVX2 amin@astek% numactl --membind 1 ./run.out // ... Application running in HBM ... //
  • 21. ‫در‬ ‫برنامه‬ ‫اجرای‬HBM‫با‬ ‫کتابخانه‬Memkind 1 #include <hbwmalloc.h> 2 const int n = 1<<10; 3 // Allocation to MCDRAM 4 double* A = (double*) hbw_malloc(sizeof(double)*n); 5 // No replacement for _mm_malloc. Use posix_memalign 6 double* B; 7 int ret = hbw_posix_memalign((void**) &B, 64, sizeof(double)*n); 8 ..... 9 // Free with hbw_free 10 hbw_free(A); hbw_free(B);
  • 22. ‫کتابخانه‬ ‫با‬ ‫کامپایل‬Memkind‫و‬ hbwmalloc ‫برنامه‬ ‫کامپایل‬ ‫برای‬C/C++ ‫باز‬ ‫متن‬ ‫نسخه‬ ‫دریافت‬ ‫برای‬memkind‫مراجعه‬ ‫زیر‬ ‫آدرس‬ ‫به‬ ‫کنید‬: memkind.github.io/memkind ‫بیشتر‬ ‫اطالعات‬: http://astek.ir/index.php/articles/hpc/262-mcdram-guide amin@astek% icpc -lmemkind foo.cc -o runme amin@astek% g++ -lmemkind foo.cc -o runme
  • 23. ‫سازی‬ ‫ذخیره‬Streaming ▷‫در‬ ،‫بزنید‬ ‫دور‬ ‫را‬ ‫کش‬ RAM‫بنویسید‬ ▷‫کش‬ ‫از‬ ‫مواقع‬ ‫سایر‬ ‫در‬ ‫کنید‬ ‫استفاده‬ ▷ #pragma vector nontemporal ▷ -qopt-streaming-stores=always
  • 25. Cache Lines ▷‫کش‬ ‫و‬ ‫مموری‬ ‫بین‬ ‫داده‬ ‫های‬ ‫بالک‬ ‫حداقل‬ ‫شود‬ ‫می‬ ‫داده‬ ‫انتقال‬ ▷‫آن‬ ‫طول‬ ‫اینتل‬ ‫معماری‬ ‫در‬64‫است‬ ‫بایت‬ ▷‫های‬ ‫محدوده‬ ‫در‬64‫مموری‬ ‫در‬ ‫بایتی‬ Align‫شود‬ ‫می‬ 8 double precision values 16 single precision values 64 bytes
  • 26. ‫قاعده‬ ‫دسترسی‬ ‫رعایت‬ ‫برای‬ ‫را‬ ‫حلقه‬ ‫ترتیب‬unit-stride ‫کنید‬ ‫تنظیم‬ ‫حافظه‬ ‫به‬ ‫بتواند‬ ‫است‬ ‫ممکن‬ ‫کامپایلر‬/‫خودکار‬ ‫صورت‬ ‫به‬ ‫نتواند‬ ‫دهد‬ ‫انجام‬ ‫را‬ ‫حلقه‬ ‫تکرارهای‬ ‫جابجایی‬
  • 27. ‫مثال‬:‫دو‬ ‫ضرب‬ ‫سازی‬ ‫ساده‬ ‫ماتریس‬ #pragma omp parallel for for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) #pragma vector aligned for (int k = 0; k < n; k++) C[i*n+j]+=A[i*n+k]*B[k*n+j]; #pragma omp parallel for for (int i = 0; i < n; i++) for (int k = 0; k < n; k++) #pragma vector aligned for (int j = 0; j < n; j++) C[i*n+j]+=A[i*n+k]*B[k*n+j]; Before: After:
  • 29. ‫قاعده‬ ‫چند‬ •‫ترتیب‬ ،‫محلی‬ ‫دسترسی‬ ‫بیشترین‬ ‫آوردن‬ ‫بدست‬ ‫برای‬ ‫دسترسی‬ ‫که‬ ‫دهید‬ ‫تغییر‬ ‫نحوی‬ ‫به‬ ‫را‬ ‫آرایه‬ ‫ایندکس‬ Unite-Stride‫باشد‬ •‫آرگومان‬ ‫از‬–O2‫حلقه‬ ‫است‬ ‫ممکن‬ ‫کامپایلر‬ ،‫بعد‬ ‫به‬ ‫کند‬ ‫تبدیل‬ ‫را‬ •‫دستی‬ ‫صورت‬ ‫به‬ ‫را‬ ‫حلقه‬ ‫تبدیل‬ ،‫پیچیده‬ ‫مورد‬ ‫در‬ ‫دهید‬ ‫انجام‬ •‫آوردن‬ ‫بدست‬ ‫برای‬Unit-Stride‫باز‬ ‫به‬ ‫نیاز‬ ‫است‬ ‫ممکن‬ ‫باشید‬ ‫داشته‬ ‫متغیرها‬ ‫نوع‬ ‫و‬ ‫سازی‬ ‫ذخیره‬ ‫روش‬ ‫طراحی‬
  • 32. ‫حلقه‬ ‫بندی‬ ‫قطعه‬:Cache Blocking for (int i = 0; i < m; i++) // Original code: for (int j = 0; j < n; j++) compute(a[i], b[j]); // Memory access is unit-stride in j // Step 1: strip-mine inner loop for (int i = 0; i < m; i++) for (int jj = 0; jj < n; jj += TILE) for (int j = jj; j < jj + TILE; j++) compute(a[i], b[j]); // Same order of operation as original // Step 2: permute for (int jj = 0; jj < n; jj += TILE) for (int i = 0; i < m; i++) for (int j = jj; j < jj + TILE; j++) compute(a[i], b[j]); // Re-use to j=jj sooner 1 2 3 1 2 3 4 5 1 2 3 4 5
  • 34. ‫حلقه‬ ‫بندی‬ ‫قطعه‬:Unroll/Register Blocking for (int i = 0; i < m; i++) // Original code: for (int j = 0; j < n; j++) compute(a[i], b[j]); // Memory access is unit-stride in j // Step 1: strip-mine outer loop for (int ii = 0; ii < m; ii += TILE) for (int i = ii; i < ii + TILE; i++) for (int j = 0; j < n; j++) compute(a[i], b[j]); // Same order of operation as original // Step 2: permute and vectorize outer loop for (int ii = 0; ii < m; ii += TILE) #pragma simd for (int j = 0; j < n; j++) for (int i = ii; i < ii + TILE; i++) compute(a[i], b[j]); //each vector in b[j] a total of TILE time 1 2 3 1 2 3 4 5 1 2 3 4 5 6
  • 35. ‫روش‬Loop Fusion(‫ادغام‬ ‫حلقه‬) ‫ها‬ ‫حلقه‬ ‫ادغام‬ ‫بوسیله‬ ‫کش‬ ‫از‬ ‫مجدد‬ ‫استفاده‬ ‫الینی‬ ‫پایپ‬ ‫پردازش‬ ‫فرآیند‬ ‫یک‬ ‫در‬ MyData* data = new MyData(n); for (int i = 0; i < n; i++) Initialize(data[i]); for (int i = 0; i < n; i++) Stage1(data[i]); for (int i = 0; i < n; i++) Stage2(data[i]); MyData* data = new MyData(n); for (int i = 0; i < n; i++) { Initialize(data[i]); Stage1(data[i]); Stage2(data[i]); } ‫ا‬‫جانبی‬ ‫مثبت‬ ‫ثرات‬:،‫شوند‬ ‫می‬ ‫جابجا‬ ‫مراحل‬ ‫بین‬ ‫کمتری‬ ‫داده‬ ‫کارایی‬ ‫افزایش‬ ،‫حافظه‬ ‫به‬ ‫ارجاعات‬ ‫کاهش‬ 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 11
  • 39. ‫باند‬ ‫پهنای‬ ‫با‬ ‫حافظه‬ ‫زیاد‬HBM ‫اول‬ ‫روش‬:numactl ‫دوم‬ ‫روش‬:Memkind amin@astek% numactl -m 1 ./stencil my-image.png #include <hbwmalloc.h> ... hbw_posix_memalign((void**)&pixel, 64, sizeof(P)*width*height); amin@astek% icpc -o stencil *.o -lpng -lmemkind
  • 40. ‫سازهای‬ ‫ذخیره‬Streaming #pragma omp parallel for for (int i = 1; i < height-1; i++) #pragma omp simd #pragma vector nontemporal for (int j = 1; j < width-1; j++) out[i*width + j] = -in[(i-1)*width + j-1] -in[(i-1)*width + j] - in[(i-1)*width + j+1] -in[(i)*width + j-1] + 8*in[(i)*width + j] - in[(i)*width + j+1] -in[(i+1)*width + j-1] -in[(i+1)*width+ j] - in[(i+1)*width+ j+1]; 1 2 3 4 5 6 7 8
  • 42.
  • 44. FLOPs‫از‬ ‫تعدادی‬ ‫ها‬ ‫پردازنده‬ Intel Core 2 and Nehalem: 4 DP FLOPs/cycle: 2-wide SSE2 addition + 2-wide SSE2 multiplication 8 SP FLOPs/cycle: 4-wide SSE addition + 4-wide SSE multiplication Intel Sandy Bridge/Ivy Bridge: 8 DP FLOPs/cycle: 4-wide AVX addition + 4-wide AVX multiplication 16 SP FLOPs/cycle: 8-wide AVX addition + 8-wide AVX multiplication Intel Haswell/Broadwell/Skylake/Kaby Lake: 16 DP FLOPs/cycle: two 4-wide FMA (fused multiply-add) instructions 32 SP FLOPs/cycle: two 8-wide FMA (fused multiply-add) instructions AMD K10: 4 DP FLOPs/cycle: 2-wide SSE2 addition + 2-wide SSE2 multiplication 8 SP FLOPs/cycle: 4-wide SSE addition + 4-wide SSE multiplication AMD Bulldozer/Piledriver/Steamroller/Excavator, per module (two cores): 8 DP FLOPs/cycle: 4-wide FMA 16 SP FLOPs/cycle: 8-wide FMA AMD Ryzen 8 DP FLOPs/cycle: 4-wide FMA 16 SP FLOPs/cycle: 8-wide FMA Intel Atom (Bonnell/45nm, Saltwell/32nm, Silvermont/22nm): 1.5 DP FLOPs/cycle: scalar SSE2 addition + scalar SSE2 multiplication every other cycle 6 SP FLOPs/cycle: 4-wide SSE addition + 4-wide SSE multiplication every other cycle Intel Xeon Phi (Knights Corner), per thread: 8 DP FLOPs/cycle: 8-wide FMA every other cycle 16 SP FLOPs/cycle: 16-wide FMA every other cycle Intel Xeon Phi (Knights Landing), per core: 32 DP FLOPs/cycle: two 8-wide FMA every cycle 64 SP FLOPs/cycle: two 16-wide FMA every cycle
  • 45. FLOPs‫از‬ ‫تعدادی‬ ‫ها‬ ‫پردازنده‬AMD Bobcat: 1.5 DP FLOPs/cycle: scalar SSE2 addition + scalar SSE2 multiplication every other cycle 4 SP FLOPs/cycle: 4-wide SSE addition every other cycle + 4-wide SSE multiplication every other cycle AMD Jaguar: 3 DP FLOPs/cycle: 4-wide AVX addition every other cycle + 4-wide AVX multiplication in four cycles 8 SP FLOPs/cycle: 8-wide AVX addition every other cycle + 8-wide AVX multiplication every other cycle ARM Cortex-A9: 1.5 DP FLOPs/cycle: scalar addition + scalar multiplication every other cycle 4 SP FLOPs/cycle: 4-wide NEON addition every other cycle + 4-wide NEON multiplication every other cycle ARM Cortex-A15: 2 DP FLOPs/cycle: scalar FMA or scalar multiply-add 8 SP FLOPs/cycle: 4-wide NEONv2 FMA or 4-wide NEON multiply-add Qualcomm Krait: 2 DP FLOPs/cycle: scalar FMA or scalar multiply-add 8 SP FLOPs/cycle: 4-wide NEONv2 FMA or 4-wide NEON multiply-add IBM PowerPC A2 (Blue Gene/Q), per core: 8 DP FLOPs/cycle: 4-wide QPX FMA every cycle SP elements are extended to DP and processed on the same units IBM PowerPC A2 (Blue Gene/Q), per thread: 4 DP FLOPs/cycle: 4-wide QPX FMA every other cycle SP elements are extended to DP and processed on the same units Intel Xeon Phi (Knights Corner), per core: 16 DP FLOPs/cycle: 8-wide FMA every cycle 32 SP FLOPs/cycle: 16-wide FMA every cycle