mlp.lpomp.html
来自「国外MPI教材」· HTML 代码 · 共 91 行
HTML
91 行
<html><head><title>C Version of OpenMP Laplacian Solver</title><link rel="stylesheet" href="/MULTI/parcomp.css"></head><body><h2>C Version of Laplace Program with OpenMP Parallelism</h2>In the OpenMP C library, the reduction clause can onlybe used for mathematical operations. In this code, thereduction variable dumax is calculated by comparison ofvalues to determine the maximum. Thus, we had to usea CRITICAL region to insure that dumax is updated correctlyby each processor.<p><FONT FACE="COURIER NEW"><pre>#include <stdio.h>#include <stdlib.h>#include <math.h>#include <omp.h>main(int argc, char* argv[]) { const int imax=2001,jmax=2001; const int itmax=100; int it; double u[imax][jmax], du[imax][jmax],dumax; const double umax=10.0; int i,j; omp_set_num_threads(4);#pragma omp parallel default(shared) private(i,j,it) {#pragma omp for schedule(static) for(j=0;j<jmax;++j) { for(i=0;i<(imax-1);++i) { u[i][j]=0.0; du[i][j]=0.0; } u[imax-1][j]=umax; } // Main Computation Loop for(it=0;it<itmax;++it) {#pragma omp single dumax=0.0; #pragma omp for schedule(static) for (j=1;j<(jmax-1);++j) { for (i=1;i<(imax-1);++i) { du[i][j]=0.25*(u[i-1][j]+u[i+1][j]+u[i][j-1]+u[i][j+1]) -u[i][j];#pragma omp critical { if (dumax < fabs(du[i][j])) dumax=fabs(du[i][j]); } } } #pragma omp for schedule(static) for (j=1;j<(jmax-1);++j) { for (i=1;i<(imax-1);++i) { u[i][j]=u[i][j]+du[i][j]; } }#pragma omp single printf("%d %g\n",it,dumax); } }}</pre></FONT></body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?