{"id":1110,"date":"2013-02-26T20:07:39","date_gmt":"2013-02-26T12:07:39","guid":{"rendered":"http:\/\/blog.dynox.cn\/?p=1110"},"modified":"2013-03-14T11:41:06","modified_gmt":"2013-03-14T03:41:06","slug":"mpi%e5%b0%8f%e8%af%95%e7%89%9b%e5%88%80","status":"publish","type":"post","link":"https:\/\/blog.dynox.cn\/?p=1110","title":{"rendered":"MPI\u5c0f\u8bd5\u725b\u5200"},"content":{"rendered":"<div class=\"gruber-markdown\"><p><span style=\"font-size: small;\">\u6709\u670b\u53cb\u95ee\u6211\u4e00\u4e2aFortran\u7a0b\u5e8f\u7684\u95ee\u9898\uff0cFortran\u8bed\u8a00\u4ee5\u524d\u66fe\u5b66\u8fc7\uff0c\u53ea\u662f\u65f6\u95f4\u4e0a\u592a\u8fc7\u4e45\u8fdc\uff0c\u6240\u6709\u7684\u8bed\u6cd5\u5168\u8fd8\u7ed9\u8001\u5e08\u4e86\u3002\u770b\u7740\u7c7b\u4f3c\u5929\u4e66\u7684\u4ee3\u7801\uff0c\u4e0d\u7981\u60f3\u5728\u5355\u673a\u7cfb\u7edf\u4e0a\u5df2\u7ecf\u6709\u5982Matlab\u8fd9\u6837\u7684\u5982\u6b64\u5f3a\u5927\u7684\u6570\u503c\u8ba1\u7b97\u8f6f\u4ef6\u4e3a\u4ec0\u4e48\u8fd8\u8981\u8d39\u529b\u7528Fortran\u6765\u5b9e\u73b0\u5462\uff1f\uff01\u5728\u591a\u673a\u6216\u96c6\u7fa4\u73af\u5883\u4e0a\u53ef\u80fd\u4f1a\u4e0d\u4e00\u6837\uff0c\u53ea\u662f\u6211\u5bf9\u5e76\u884c\u8ba1\u7b97\u6240\u77e5\u751a\u5c11\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u96c6\u7fa4\u73af\u5883\u4e0aMPI\u4f7f\u7528\u76f8\u5f53\u591a\uff0c\u4e5f\u53ea\u662f\u4e86\u89e3\u4e00\u4e9b\uff0c\u4ece\u672a\u5b9e\u9645\u4f7f\u7528\u8fc7\uff0c\u6b63\u597d\u8d81\u73b0\u5728\u6709\u4e9b\u5174\u8da3\u4fbf\u505a\u4e86\u505a\u5b9e\u9a8c\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u5728\u7f51\u4e0a\u770b\u5230\u6709\u4e2a\u8ba1\u7b97Pi\u7684\u7a0b\u5e8f &lt;<a title=\"http:\/\/chpc.wustl.edu\/mpi-fortran.html\" href=\"http:\/\/chpc.wustl.edu\/mpi-fortran.html\"><a href=\"http:\/\/chpc.wustl.edu\/mpi-fortran.html\">http:\/\/chpc.wustl.edu\/mpi-fortran.html<\/a><\/a>&gt;\uff0c\u7528Fortran\u5199\u7684\uff0c\u5176\u5b9e\u5c31\u662f\u8ba1\u7b97\u5b9a\u79ef\u5206:<\/span><\/p>\n<p><a href=\"https:\/\/blog.dynox.cn\/wp-content\/uploads\/2013\/02\/image.png\" class=\"highslide-image\" onclick=\"return hs.expand(this);\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;\" title=\"image\" src=\"https:\/\/blog.dynox.cn\/wp-content\/uploads\/2013\/02\/image_thumb.png\" alt=\"image\" width=\"90\" height=\"44\" border=\"0\" \/><\/a><\/p>\n<p><span style=\"font-size: small;\">\u6587\u4e2d\u91c7\u7528\u7684\u662f\u8499\u7279\u5361\u6d1b\u65b9\u6cd5\uff0c\u6211\u91cd\u5199\u4e86\u4e00\u4e2aC\u8bed\u8a00\u7684\u5b9e\u73b0\uff0c\u7a0b\u5e8fMPI_mc_pi.c\uff0c\u5e76\u5728VMware\u73af\u5883\u4e0b\u505a\u4e86\u505a\u5b9e\u9a8c\uff0c\u4f7f\u7528\u4e862E9\u4e2a\u91c7\u6837\u70b9\uff0c\u5206\u522b\u505a\u4e86\u5355\u673a\u4e0d\u540c\u5e76\u884c\u8fdb\u7a0b\u6570\u53ca\u53cc\u7ed3\u70b9\u7684\u5bf9\u6bd4\uff0c\u968f\u5e76\u884c\u5ea6\u7684\u589e\u52a0\uff0c\u6240\u9700\u65f6\u95f4\u57fa\u672c\u4e0a\u662f\u7ebf\u6027\u51cf\u5c11\uff0c\u7531\u4e8e\u6d4b\u8bd5\u673a\u662f\u53cc\u6838\u56db\u7ebf\u7a0b\u7684Intel i5 CPU\uff0c\u5f53\u5e76\u884c\u5ea6\u8d85\u8fc74\u65f6\u4fbf\u6ca1\u6709\u4ec0\u4e48\u610f\u4e49\u4e86\uff0c\u7eaf\u7cb9\u662f\u4e3a\u6d4b\u8bd5\u7740\u73a9\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u540e\u9762\u53c8\u7528\u5dee\u5206\u7684\u529e\u6cd5\u91cd\u65b0\u505a\u4e86\u540c\u6837\u7684\u8ba1\u7b97\uff0c\u5c06[0, 1)\u5206\u62102E9\u4e2a\u7f51\u683c\uff0c\u6bcf\u4e2a\u8fdb\u7a0b\u5e73\u5206\u7a7a\u95f4[0, 1)\uff0c\u5982\u8fdb\u7a0b1\u4e3a[0, 1\/nprocs)\uff0c\u8fdb\u7a0b0\u5904\u7406\u6700\u540e\u5269\u4e0b\u7684\u90e8\u5206[1-(nprocs-1)\/nprocs, 1)\uff0c\u89c1\u7a0b\u5e8fMPI_dc_pi.c\uff0c\u8ba1\u7b97\u7ed3\u679c\u57fa\u672c\u9075\u5faa\u7ebf\u6027\u51cf\u5c11\u89c4\u5f8b\uff0c\u4f46\u662f\u65f6\u95f4\u4e0a\u5374\u8981\u6bd4\u8499\u7279\u5361\u6d1b\u65b9\u6cd5\u9ad8\u51fa\u5f88\u591a\uff0c\u7279\u522b\u662f\u5355\u8fdb\u7a0b\u7684\u60c5\u51b5\u4e0b\uff0c\u6548\u7387\u5dee\u4e86\u8fd150%\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u4e8e\u662f\u53c8\u6539\u8fdb\u4e86\u5dee\u5206\u7a0b\u5e8f\uff0c\u5c06\u7a7a\u95f4\u5e73\u5747\u5206\u5e03\uff0c\u6539\u6210\u5404\u8fdb\u7a0b\u6563\u5e03\u5728[0,1)\u7a7a\u95f4\u4e0a\uff0c\u5373\u6bcf\u4e2a\u8fdb\u7a0b\u5728\u7f51\u683c\u91c7\u6837\u4e0a\u5fc3nprocs\u4e3a\u6b65\u8fdb\uff0c\u53c2\u89c1\u7a0b\u5e8fMPI_dc_pi2.c\uff0c\u5982\u6b64\u6539\u8fdb\u540e\uff0c\u7ed3\u679c\u548c\u5747\u5206\u91c7\u6837\u7a7a\u95f4\u57fa\u672c\u5dee\u4e0d\u591a\uff0c\u6ca1\u6709\u663e\u8457\u53d8\u5316\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u540e\u9762\u53c8\u7528\u53cc\u673a\u505a\u4e86Beowulf Cluster\u6d4b\u8bd5(VMware + X61)\uff0c\u6bcf\u4e2a\u8282\u70b9\u9650\u5236\u4e3a2\u4e2a\u8fdb\u7a0b\uff0c\u56e0\u4e3a\u53e6\u4e00\u53f0\u673a\u5668\u662f\u53cc\u6838\u7684CPU\uff08X61\uff09\uff0c\u53d1\u73b0\u53cc\u8282\u70b9\u60c5\u51b5\u4e0b\u6027\u80fd\u660e\u663e\u597d\u4e8e\u5355\u8282\u70b9\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u5c06\u7ed3\u679c\u6c47\u603b\u540e\u505a\u6210\u56fe\u8868\u5982\u4e0b\uff1a<\/span><\/p>\n<p><a href=\"https:\/\/blog.dynox.cn\/wp-content\/uploads\/2013\/02\/MPI-mc_vs_dc.jpg\" class=\"highslide-image\" onclick=\"return hs.expand(this);\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;\" title=\"MPI-mc_vs_dc\" src=\"https:\/\/blog.dynox.cn\/wp-content\/uploads\/2013\/02\/MPI-mc_vs_dc_thumb.jpg\" alt=\"MPI-mc_vs_dc\" width=\"476\" height=\"311\" border=\"0\" \/><\/a><\/p>\n<p><span style=\"font-size: small;\">\u5982\u56fe\u6240\u793a\uff0c\u4e24\u79cd\u5dee\u5206\u65b9\u5f0f\u57fa\u672c\u4e00\u81f4\uff0c\u8fd9\u70b9\u5b8c\u5168\u53ef\u4ee5\u7406\u89e3\uff0c\u4f46\u662f\u8499\u7279\u5361\u6d1b\u65b9\u6cd5\u4e3a\u4ec0\u4e48\u80fd\u5feb\u8fd9\u4e48\u591a\uff0c\u786e\u5b9e\u8ba9\u4eba\u8d39\u89e3\uff0c\u96be\u9053\u662fdrand48()\u7684\u6548\u7387\u6bd4double\u578b\u9664\u6cd5\u8fd8\u5feb\uff1f\u60f3\u8d77\u6765\u603b\u89c9\u4e0d\u592a\u53ef\u80fd\uff0c\u90a3\u5c31\u8ba9\u6570\u636e\u8bf4\u8bdd\u5427\uff1a<\/span><\/p>\n<p><span style=\"font-size: small;\">\u5206\u522b\u5bf9drand48()\u548cdouble\u578b\u9664\u6cd5\u505a\u4e86\u6d4b\u8bd5\uff0c\u7ed3\u679c\u6b63\u5982\u9884\u6599\uff0cdrand48()\u7684\u6548\u7387\u66f4\u5dee\uff0c\u5206\u522b\u6267\u884c1E10\u6b21\u7684\u65f6\u95f4\u4e3a\uff1a<\/span><\/p>\n<p><span style=\"font-size: small;\">drand48():\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 4m21.065s\ndouble\u578b\u9664\u6cd5\uff1a 3m27.399s<\/span><\/p>\n<p><span style=\"font-size: small;\">\u6392\u9664\u4e86\u8fd9\u4e2a\u539f\u56e0\u4e4b\u540e\uff0c\u5269\u4e0b\u7684\u53ef\u80fd\u53ea\u80fd\u5728drand48()\u91cc\u9762\u4e86\uff0cdrand48()\u867d\u662f\u9ad8\u7cbe\u5ea6\u768448\u4f4d\u7684\u4f2a\u968f\u673a\u6570\u4ea7\u751f\u5668\uff0c\u5176\u7ed3\u679c\u5e94\u8be5\u8fd8\u662f\u4e0d\u591f\u5e73\u5747\u548c\u968f\u673a\u3002\u8bc4\u4f30drand48()\u7684\u4e8b\u54b1\u4e0d\u5e72\uff0c\u6240\u4ee5\u5c31\u505a\u4e86\u4e2a\u7b80\u5355\u5b9e\u9a8c\uff0c\u53ea\u8981\u80fd\u9a8c\u8bc1\u8ba1\u7b97\u65f6\u95f4\u548cdrand48()\u751f\u6210\u7684\u968f\u673a\u6570\u4e0d\u5e73\u5747\u5ea6\u663e\u8457\u76f8\u5173\u5373\u53ef\u3002\u9a8c\u8bc1\u7a0b\u5e8f\u8fd8\u662f\u91c7\u7528\u8499\u7279\u5361\u6d1b\u65b9\u6cd5\uff0c\u53ea\u662f\u5c06\u968f\u673a\u6570\u7684\u4ea7\u751f\u8303\u56f4\u9650\u5b9a\u5728[0.5,1]\u4e4b\u5185\uff0c\u4e0d\u662f\u4e4b\u524d\u7684[0, 1]\uff0c\u7ed3\u679c\u5355\u8fdb\u7a0b\u8ba1\u7b97\u65f6\u95f4\u4e3a1m59.160s\uff0c\u53cc\u8fdb\u7a0b\u4e3a1m35.185s\uff0c\u8fd9\u8db3\u4ee5\u8bf4\u660e\u968f\u673a\u6570\u7684\u968f\u673a\u5ea6\u5bf9\u8ba1\u7b97\u65f6\u95f4\u6709\u8f83\u5927\u5f71\u54cd\uff08Pi\u7684\u503c\u5bf9\u4e0e\u4e0d\u5bf9\u6ca1\u5173\u7cfb\uff0c\u6b64\u65f6\u53ea\u5173\u6ce8\u8ba1\u7b97\u8fc7\u7a0b\u6240\u82b1\u7684\u65f6\u95f4\uff09\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u6700\u540e\u5728Mathematica\u4e0a\u4e5f\u505a\u4e86\u4e2a\u6d4b\u8bd5\uff0c\u51fa\u4eba\u610f\u6599\u7684\u662fMathematica\u53ea\u7528\u51e0\u4e86\u79d2\u949f\u5c31\u7b97\u51fa\u4e861E10\u7f51\u683c\u7684\u5dee\u5206\uff0c\u5373\u4fbf\u662f\u5b9a\u79ef\u5206\u8ba1\u7b97\uff0c\u4e5f\u662f\u5b8c\u5b8c\u5168\u5168\u7684\u79d2\u6740\uff0c\u592a\u725bx\u4e86\uff0c\u4e0d\u670d\u4e0d\u884c\uff01<\/span><\/p>\n<p><a href=\"https:\/\/blog.dynox.cn\/wp-content\/uploads\/2013\/02\/image1.png\" class=\"highslide-image\" onclick=\"return hs.expand(this);\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;\" title=\"image\" src=\"https:\/\/blog.dynox.cn\/wp-content\/uploads\/2013\/02\/image_thumb1.png\" alt=\"image\" width=\"476\" height=\"84\" border=\"0\" \/><\/a><\/p>\n<p><span style=\"font-size: small;\">\u672c\u6587\u76f8\u5173\u4ee3\u7801\uff1a<\/span><\/p>\n<blockquote><strong>mpiuser@Ubunut-X64:~\/MPI$ cat MPI_mc_pi.c<\/strong>\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;time.h&gt;\n#include &lt;mpi.h&gt;\n\nint main(int argc, char** argv) {\n\nint myrank, nprocs, rc;\nunsigned long t, times, avg_times;\ndouble l_sum = 0, g_sum = 0, x = 1.0;\n\ntimes = 2000000000;\n\nMPI_Init(&amp;argc, &amp;argv);\nMPI_Comm_size(MPI_COMM_WORLD, &amp;nprocs);\nMPI_Comm_rank(MPI_COMM_WORLD, &amp;myrank);\n\navg_times = times \/ nprocs;\n\nif (0 == myrank) {\navg_times = times - avg_times * (nprocs - 1);\n}\n\n\/* printf(\"%d: points: %lu\\n\", myrank, avg_times); *\/\n\nsrand48((myrank + 1) * (time(NULL) &amp; 0xFFF));\n\nfor (t = 0; t &lt; avg_times; t++) {\nx = drand48();\nl_sum = l_sum + 4.0\/(x * x + 1.0);\n}\n\nrc = MPI_Reduce(&amp;l_sum, &amp;g_sum, 1, MPI_DOUBLE_PRECISION, MPI_SUM,\n0, MPI_COMM_WORLD);\nif (0 != rc) {\nprintf(\"MPI_Reduce failed with error code: %d\\n\", rc);\n} else if (0 == myrank) {\nprintf(\"PI result: %f\/%lu =\u00a0 %f\\n\", g_sum, times, g_sum \/ times);\n}\n\nMPI_Finalize();\nreturn 0;\n}\n\n<strong>mpiuser@Ubunut-X64:~\/MPI$ cat MPI_dc_pi.c<\/strong>\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;time.h&gt;\n#include &lt;mpi.h&gt;\n\nint main(int argc, char** argv) {\n\nint myrank, nprocs, rc;\nunsigned long t, grids, avg_grids, start, end;\ndouble l_sum = 0, g_sum = 0, x = 1.0;\n\ngrids = 2000000000;\n\nMPI_Init(&amp;argc, &amp;argv);\nMPI_Comm_size(MPI_COMM_WORLD, &amp;nprocs);\nMPI_Comm_rank(MPI_COMM_WORLD, &amp;myrank);\n\navg_grids = grids \/ nprocs;\n\nif (0 == myrank) {\nstart = avg_grids * (nprocs - 1);\nend = grids;\n} else {\nstart = avg_grids * (myrank - 1);\nend = start + avg_grids;\n}\n\n\/* printf(\"%d: [%lu - %lu)\\n\", myrank, start, end); *\/\n\nfor (t = start; t &lt; end; t++) {\nx = 1.0 * t \/ grids;\nl_sum = l_sum + 4.0\/(x * x + 1.0);\n}\n\nrc = MPI_Reduce(&amp;l_sum, &amp;g_sum, 1, MPI_DOUBLE_PRECISION, MPI_SUM,\n0, MPI_COMM_WORLD);\nif (0 != rc) {\nprintf(\"MPI_Reduce failed with error code: %d\\n\", rc);\n} else if (0 == myrank) {\nprintf(\"PI result: %f with %lu grids.\\n\", g_sum\/grids, grids);\n}\n\nMPI_Finalize();\nreturn 0;\n}\n\n<strong>mpiuser@Ubunut-X64:~\/MPI$ cat MPI_dc_pi2.c<\/strong>\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;time.h&gt;\n#include &lt;mpi.h&gt;\n\nint main(int argc, char** argv) {\n\nint myrank, nprocs, rc;\nunsigned long t, grids;\ndouble l_sum = 0, g_sum = 0, x = 1.0;\n\ngrids = 2000000000;\n\nMPI_Init(&amp;argc, &amp;argv);\nMPI_Comm_size(MPI_COMM_WORLD, &amp;nprocs);\nMPI_Comm_rank(MPI_COMM_WORLD, &amp;myrank);\n\nfor (t = myrank; t &lt; grids; t += nprocs) {\nx = 1.0 * t \/ grids;\nl_sum = l_sum + 4.0\/(x * x + 1.0);\n}\n\nrc = MPI_Reduce(&amp;l_sum, &amp;g_sum, 1, MPI_DOUBLE_PRECISION, MPI_SUM,\n0, MPI_COMM_WORLD);\nif (0 != rc) {\nprintf(\"MPI_Reduce failed with error code: %d\\n\", rc);\n} else if (0 == myrank) {\nprintf(\"PI result: %f with %lu grids.\\n\", g_sum\/grids, grids);\n}\n\nMPI_Finalize();\nreturn 0;\n}\n\n\u7f16\u8bd1\u547d\u4ee4\uff1a\n<a href=\"mailto:mpiuser@Ubunut-X64:~\/MPI$\"><strong>mpiuser@Ubunut-X64:~\/MPI$<\/strong><\/a><strong> cat build\n<\/strong>mpicc MPI_mc_pi.c -std=c99 -W -Wall -pedantic -D_SVID_SOURCE -g -o MPI_mc_pi\nmpicc MPI_dc_pi.c -std=c99 -W -Wall -pedantic -g -o MPI_dc_pi\nmpicc MPI_dc_pi2.c -std=c99 -W -Wall -pedantic -g -o MPI_dc_pi2<\/blockquote><\/div>","protected":false},"excerpt":{"rendered":"<p>\u6709\u670b\u53cb\u95ee\u6211\u4e00\u4e2aFortran\u7a0b\u5e8f\u7684\u95ee\u9898\uff0cFortran\u8bed\u8a00\u4ee5\u524d\u66fe\u5b66\u8fc7\uff0c\u53ea\u662f\u65f6\u95f4\u4e0a\u592a\u8fc7\u4e45\u8fdc\uff0c\u6240\u6709\u7684\u8bed\u6cd5\u5168\u8fd8\u7ed9\u8001\u5e08 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[8,9],"tags":[492,487,486,490,489,491],"views":4509,"_links":{"self":[{"href":"https:\/\/blog.dynox.cn\/index.php?rest_route=\/wp\/v2\/posts\/1110"}],"collection":[{"href":"https:\/\/blog.dynox.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.dynox.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.dynox.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.dynox.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1110"}],"version-history":[{"count":3,"href":"https:\/\/blog.dynox.cn\/index.php?rest_route=\/wp\/v2\/posts\/1110\/revisions"}],"predecessor-version":[{"id":1138,"href":"https:\/\/blog.dynox.cn\/index.php?rest_route=\/wp\/v2\/posts\/1110\/revisions\/1138"}],"wp:attachment":[{"href":"https:\/\/blog.dynox.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.dynox.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.dynox.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}