{"id":220927,"date":"2014-05-16T05:10:03","date_gmt":"2014-05-16T01:10:03","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=220927"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=220927","title":{"rendered":"<span class=\"post_title\">R + C + CUDA =\u2026<\/span>"},"content":{"rendered":"<div class=\"content html_format\">     \t<img decoding=\"async\" align=\"left\" src=\"http:\/\/habrastorage.org\/getpro\/habr\/post_images\/ef6\/ad5\/ec5\/ef6ad5ec526ad577bccf516e99761c06.png\"\/>\u0418\u043d\u043e\u0433\u0434\u0430 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c \u0443\u0441\u043a\u043e\u0440\u0438\u0442\u044c \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f, \u043f\u0440\u0438\u0447\u0435\u043c \u0436\u0435\u043b\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0441\u0440\u0430\u0437\u0443 \u0432 \u0440\u0430\u0437\u044b. \u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043e\u0442\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u043e\u0442 \u0443\u0434\u043e\u0431\u043d\u044b\u0445, \u043d\u043e \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u044b\u0445 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0438 \u043f\u0440\u0438\u0431\u0435\u0433\u0430\u0442\u044c \u043a \u0447\u0435\u043c\u0443-\u0442\u043e \u0431\u043e\u043b\u0435\u0435 \u043d\u0438\u0437\u043a\u043e\u0443\u0440\u043e\u0432\u043d\u0435\u0432\u043e\u043c\u0443 \u0438 \u0431\u044b\u0441\u0442\u0440\u043e\u043c\u0443. R \u0438\u043c\u0435\u0435\u0442 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0440\u0430\u0437\u0432\u0438\u0442\u044b\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u043c\u0438 \u0431\u0438\u0431\u0438\u043e\u0442\u0435\u043a\u0430\u043c\u0438, \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u043c\u0438 \u043d\u0430 \u0421\/\u0421++, Fortran \u0438\u043b\u0438 \u0434\u0430\u0436\u0435 Java. \u042f \u043f\u043e \u043f\u0440\u0438\u0432\u044b\u0447\u043a\u0435 \u043f\u0440\u0435\u0434\u043f\u043e\u0447\u0438\u0442\u0430\u044e \u0421\/\u0421++.<br \/>  <a name=\"habracut\"><\/a><\/p>\n<h3>R \u0438 \u0421<\/h3>\n<p>  \u0421\u0440\u0430\u0437\u0443 \u043e\u0433\u043e\u0432\u043e\u0440\u044e\u0441\u044c, \u0447\u0442\u043e \u044f \u0440\u0430\u0431\u043e\u0442\u0430\u044e \u043f\u043e\u0434 Debian Wheezy (\u043f\u043e\u0434 Windows, \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e, \u0435\u0441\u0442\u044c \u043a\u0430\u043a\u0438\u0435-\u0442\u043e \u043d\u044c\u044e\u0430\u043d\u0441\u044b). \u041f\u0440\u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0438 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u043d\u0430 \u0421 \u0434\u043b\u044f R \u043d\u0430\u0434\u043e \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435:  <\/p>\n<ul>\n<li>\u0424\u0443\u043d\u043a\u0446\u0438\u0438, \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0435 \u043d\u0430 \u0421 \u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c\u044b\u0435 \u0432 R, \u0434\u043e\u043b\u0436\u043d\u044b \u0438\u043c\u0435\u0442\u044c \u0442\u0438\u043f void. \u042d\u0442\u043e \u0437\u043d\u0430\u0447\u0438\u0442, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043a\u0430\u043a\u0438\u0435-\u0442\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b, \u0442\u043e \u0438\u0445 \u043d\u0430\u0434\u043e \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0447\u0435\u0440\u0435\u0437 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u044b \u0444\u0443\u043d\u043a\u0446\u0438\u0438<\/li>\n<li>\u0412\u0441\u0435 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u044e\u0442\u0441\u044f \u043f\u043e \u0441\u0441\u044b\u043b\u043a\u0435 (\u0430 \u043f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438 \u043d\u0430\u0434\u043e \u043d\u0435 \u0442\u0435\u0440\u044f\u0442\u044c \u0431\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c!)<\/li>\n<li>\u0412 \u0421 \u043a\u043e\u0434 \u0436\u0435\u043b\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0432\u043a\u043b\u044e\u0447\u0430\u0442\u044c <code>R.h<\/code> \u0438 <code>Rmath.h<\/code> (\u0435\u0441\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 R)<\/li>\n<\/ul>\n<p>  \u041d\u0430\u0447\u043d\u0435\u043c \u0441 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u0442 \u0441\u043a\u0430\u043b\u044f\u0440\u043d\u043e\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0434\u0432\u0443\u0445 \u0432\u0435\u043a\u0442\u043e\u0440\u043e\u0432:   <\/p>\n<div style=\"text-align:center;\"><img decoding=\"async\"  src=\"http:\/\/habrastorage.org\/getpro\/habr\/post_images\/82d\/a31\/598\/82da3159820a34881903605d2cab7b89.png\"\/><\/div>\n<p>  <\/p>\n<pre><code>#include &lt;R.h&gt;  void iprod(double *v1, double *v2, int *n, double *s) {   *s = 0;   for (int i = 0; i &lt; *n; i++) {     *s += v1[i] * v2[i];   } } <\/code><\/pre>\n<p>  \u0414\u0430\u043b\u0435\u0435 \u043d\u0430\u0434\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 \u2014 \u043c\u043e\u0436\u043d\u043e \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0447\u0435\u0440\u0435\u0437 gcc, \u0430 \u043c\u043e\u0436\u043d\u043e \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0442\u0430\u043a\u043e\u0439 \u043a\u043e\u043c\u0430\u043d\u0434\u043e\u0439 (\u043a\u0441\u0442\u0430\u0442\u0438, \u0441\u0442\u043e\u0438\u0442 \u0437\u0430\u043f\u043e\u043c\u043d\u0438\u0442\u044c \u0432\u044b\u0432\u043e\u0434, \u0442.\u043a. \u043e\u043d \u043f\u0440\u0438\u0433\u043e\u0434\u0438\u0442\u0441\u044f \u043d\u0430\u043c \u0432 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c):  <\/p>\n<pre><code>R CMD SHLIB inner_prod.c <\/code><\/pre>\n<p>  \u041d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0444\u0430\u0439\u043b inner_prod.so, \u0434\u043b\u044f \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439 <code>dyn.load()<\/code>. \u0414\u043b\u044f \u0432\u044b\u0437\u043e\u0432\u0430 \u0436\u0435 \u0441\u0430\u043c\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043d\u0430 \u0421 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c <code>.C()<\/code> (\u0435\u0441\u0442\u044c \u0435\u0449\u0435 <code>.Call()<\/code> \u0438 <code>.External()<\/code>, \u043d\u043e \u0441 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0434\u0440\u0443\u0433\u0438\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u043e\u043c, \u043f\u0440\u0438\u0447\u0435\u043c \u043c\u0435\u0436\u0434\u0443 \u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0438\u043a\u0430\u043c\u0438 <a href=\"http:\/\/mazamascience.com\/WorkingWithData\/?p=1099\">.C() \u0438 .Call()<\/a> \u043f\u043e\u0434\u0447\u0430\u0441 \u0438\u0434\u0443\u0442 <a href=\"http:\/\/permalink.gmane.org\/gmane.comp.lang.r.devel\/30552\">\u0436\u0430\u0440\u043a\u0438\u0435 \u0441\u043f\u043e\u0440\u044b<\/a> ). \u041e\u0442\u043c\u0435\u0447\u0443 \u0442\u043e\u043b\u044c\u043a\u043e, \u0447\u0442\u043e \u043f\u0440\u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0438 \u043a\u043e\u0434\u0430 \u043d\u0430 \u0421 \u0434\u043b\u044f \u0432\u044b\u0437\u043e\u0432\u0430 \u0447\u0435\u0440\u0435\u0437 <code>.C()<\/code> \u043e\u043d \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0431\u043e\u043b\u0435\u0435 \u0447\u0438\u0441\u0442\u044b\u043c \u0438 \u0443\u0434\u043e\u0431\u043e\u0447\u0438\u0442\u0430\u0435\u043c\u044b\u043c. \u041e\u0441\u043e\u0431\u043e\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u0441\u0442\u043e\u0438\u0442 \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c \u043d\u0430 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0435 \u0442\u0438\u043f\u043e\u0432 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0432 R \u0438 C (\u0432 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 \u043f\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>.C()<\/code> \u043e\u0431 \u044d\u0442\u043e\u043c \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043e). \u0424\u0443\u043d\u043a\u0446\u0438\u044f-\u043e\u0431\u0435\u0440\u0442\u043a\u0430 \u043d\u0430 R:  <\/p>\n<pre><code>iprod\u0441 &lt;- function(a, b) {   if (!is.loaded('iprod')) {     dyn.load(&quot;inner_prod.so&quot;)   }   n &lt;- length(a)   v &lt;- 0     return(.C(&quot;iprod&quot;, as.double(a), as.double(b), as.integer(n), as.double(v))[[4]]) } <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u0443\u0437\u043d\u0430\u0442\u044c, \u0447\u0435\u0433\u043e \u0436\u0435 \u043c\u044b \u0434\u043e\u0431\u0438\u043b\u0438\u0441\u044c:  <\/p>\n<pre><code>&gt; n &lt;- 1e7; a &lt;- rnorm(n); b &lt;- rnorm(n); &gt; iprod\u0441(a, b) [1] 3482.183 <\/code><\/pre>\n<p>  \u0418 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0430\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430:  <\/p>\n<pre><code>&gt; sum(a * b) [1] 3482.183 <\/code><\/pre>\n<p>  \u0412\u043e \u0432\u0441\u044f\u043a\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u0441\u0447\u0438\u0442\u0430\u0435\u0442 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e. <\/p>\n<h3>R \u0438 \u0421UDA<\/h3>\n<p>  \u0427\u0442\u043e\u0431\u044b \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0432\u0441\u0435\u043c\u0438 \u0431\u043b\u0430\u0433\u0430\u043c\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0433\u0440\u0430\u0444\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0443\u0441\u043a\u043e\u0440\u0438\u0442\u0435\u043b\u044c Nvidia, \u0432 Debian, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e, \u0447\u0442\u043e\u0431\u044b \u0431\u044b\u043b\u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u044b <a href=\"https:\/\/wiki.debian.org\/NvidiaGraphicsDrivers\">\u043f\u0440\u043e\u043f\u0440\u0438\u0435\u0442\u0440\u0430\u043d\u044b\u0439 \u0434\u0440\u0430\u0439\u0432\u0435\u0440 Nvidia<\/a> \u0438 \u043f\u0430\u043a\u0435\u0442 <code>nvidia-cuda-toolkit<\/code>. CUDA, \u0431\u0435\u0437\u0443\u0441\u043b\u043e\u0432\u043d\u043e, \u0437\u0430\u0441\u043b\u0443\u0436\u0438\u0432\u0430\u0435\u0442 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0439 \u043e\u0433\u0440\u043e\u043c\u043d\u043e\u0439 \u0442\u0435\u043c\u044b, \u0438 \u0442\u0430\u043a \u043a\u0430\u043a \u043c\u043e\u0439 \u0443\u0440\u043e\u0432\u0435\u043d\u044c \u0432 \u044d\u0442\u043e\u0439 \u0442\u0435\u043c\u0435 \u00ab\u043d\u043e\u0432\u0438\u0447\u043e\u043a\u00bb, \u044f \u043d\u0435 \u0431\u0443\u0434\u0443 \u043f\u0443\u0433\u0430\u0442\u044c \u043b\u044e\u0434\u0435\u0439 \u0441\u0432\u043e\u0438\u043c \u043a\u0440\u0438\u0432\u044b\u043c \u0438 \u043d\u0435\u0434\u043e\u043f\u0438\u0441\u0430\u043d\u044b\u043c \u043a\u043e\u0434\u043e\u043c, \u0430 \u043f\u043e\u0437\u0432\u043e\u043b\u044e \u0441\u0435\u0431\u0435 \u0441\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u0442\u0440\u043e\u043a \u0438\u0437 \u043c\u0435\u0442\u043e\u0434\u0438\u0447\u043a\u0438. <br \/>  \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043a\u0430\u0436\u0434\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432\u0435\u043a\u0442\u043e\u0440\u0430 \u0432\u043e\u0437\u0432\u0435\u0441\u0442\u0438 \u0432 \u0442\u0440\u0435\u0442\u044c\u044e \u0441\u0442\u0435\u043f\u0435\u043d\u044c \u0438 \u043d\u0430\u0439\u0442\u0438 \u0435\u0432\u043a\u043b\u0438\u0434\u043e\u0432\u0443 \u043d\u043e\u0440\u043c\u0443 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u043e\u0433\u043e \u0432\u0435\u043a\u0442\u043e\u0440\u0430:   <\/p>\n<div style=\"text-align:center;\"><img decoding=\"async\"  src=\"http:\/\/habrastorage.org\/files\/0b0\/399\/405\/0b03994055c541d09ea07bd7513930a4.png\"\/><\/div>\n<p>  \u0427\u0442\u043e\u0431\u044b \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043e\u0431\u043b\u0435\u0433\u0447\u0438\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 GPU, \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u043e\u0439 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u044b\u0445 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432 <code>Thrust<\/code>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0430\u0431\u0441\u0442\u0440\u0430\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043e\u0442 \u043d\u0438\u0437\u043a\u043e\u0443\u0440\u043e\u0432\u043d\u0435\u0432\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 CUDA\/C. \u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0432 \u0432\u0438\u0434\u0435 \u0432\u0435\u043a\u0442\u043e\u0440\u043e\u0432, \u043a \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u044e\u0442\u0441\u044f \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b (elementwise operations, reductions, prefix-sums, sorting).  <\/p>\n<pre><code>#include &lt;thrust\/transform_reduce.h&gt; #include &lt;thrust\/device_vector.h&gt; #include &lt;cmath&gt;  \/\/ \u0424\u0443\u043d\u043a\u0442\u043e\u0440, \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0449\u0438\u0439 \u0432\u043e\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0447\u0438\u0441\u043b\u0430 \u0432 6 \u0441\u0442\u0435\u043f\u0435\u043d\u044c \u043d\u0430 GPU (device) template &lt;typename T&gt; struct power{   __device__    T operator()(const T& x) const{     return std::pow(x, 6);   } };  extern &quot;C&quot; void nrm(double *v, int *n, double *vnorm) {   \/\/ \u0412\u0435\u043a\u0442\u043e\u0440, \u0445\u0440\u0430\u043d\u0438\u043c\u044b\u0439 \u0432 \u043f\u0430\u043c\u044f\u0442\u0438 GPU, \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043a\u043e\u043f\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 *v   thrust::device_vector&lt;double&gt; dv(v, v + *n);      \/\/ Reduce-\u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u0432\u0435\u043a\u0442\u043e\u0440\u0430, \u0442.\u0435. \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043a \u043a\u0430\u0436\u0434\u043e\u043c\u0443 \u0447\u043b\u0435\u043d\u0443 \u0432\u0435\u043a\u0442\u043e\u0440\u0430 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u0441\u044f \u0444\u0443\u043d\u043a\u0442\u043e\u0440 power   \/\/ \u043f\u043e\u0442\u043e\u043c \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0435 \u0447\u0438\u0441\u043b\u0430 \u0441\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u044e\u0442\u0441\u044f.   *vnorm = std::sqrt( thrust::transform_reduce(dv.begin(), dv.end(), power&lt;double&gt;(), 0.0, thrust::plus&lt;double&gt;()) ); } <\/code><\/pre>\n<p>  \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 <code>extern &quot;C&quot;<\/code> \u0442\u0443\u0442 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u0438\u043d\u0430\u0447\u0435 R \u043d\u0435 \u0443\u0432\u0438\u0434\u0438\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u044e nrm(). \u0414\u043b\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 \u043a\u043e\u0434\u0430 \u0442\u0435\u043f\u0435\u0440\u044c \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c nvcc. \u041f\u043e\u043c\u043d\u0438\u0442\u0435 \u0432\u044b\u0432\u043e\u0434 \u043a\u043e\u043c\u0430\u043d\u0434\u044b <code>R CMD SHLIB...<\/code>? \u0412\u043e\u0442 \u0435\u0433\u043e \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0438 \u0430\u0434\u0430\u043f\u0442\u0438\u0440\u0443\u0435\u043c, \u0447\u0442\u043e\u0431\u044b \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0449\u0430\u044f CUDA\/Thrust \u0431\u0435\u0437 \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u0432\u044b\u0437\u044b\u0432\u0430\u043b\u0430\u0441\u044c \u0438\u0437 R:  <\/p>\n<pre><code>nvcc -g -G -O2 -arch sm_30 -I\/usr\/share\/R\/include -Xcompiler &quot;-Wall -fpic&quot; -c thr.cu thr.o nvcc -shared -lm thr.o -o thr.so  -L\/usr\/lib\/R\/lib -lR <\/code><\/pre>\n<p>  \u041d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043c DSO thr.so. \u0424\u0443\u043d\u043a\u0446\u0438\u044f-\u043e\u0431\u0435\u0440\u0442\u043a\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043d\u0438\u0447\u0435\u043c \u043d\u0435 \u043e\u0442\u043b\u0438\u0447\u0430\u0435\u0442\u0441\u044f:  <\/p>\n<pre><code>gpunrm &lt;- function(v) {   if (!is.loaded('nrm'))     dyn.load(&quot;thr.so&quot;)      n &lt;- length(v)   vnorm &lt;- 0    return(.C(&quot;nrm&quot;, as.double(v), as.integer(n), as.double(vnorm))[[3]]) } <\/code><\/pre>\n<p>  \u041d\u0438\u0436\u0435 \u043d\u0430 \u0433\u0440\u0430\u0444\u0438\u043a\u0435 \u0445\u043e\u0440\u043e\u0448\u043e \u0432\u0438\u0434\u043d\u043e, \u043a\u0430\u043a \u0440\u0430\u0441\u0442\u0435\u0442 \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0434\u043b\u0438\u043d\u044b \u0432\u0435\u043a\u0442\u043e\u0440\u0430. \u0421\u0442\u043e\u0438\u0442 \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u0432 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f\u0445 \u043f\u0440\u0435\u043e\u0431\u043b\u0430\u0434\u0430\u044e\u0442 \u043f\u0440\u043e\u0441\u0442\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0442\u0438\u043f\u0430 \u0441\u043b\u043e\u0436\u0435\u043d\u0438\u044f\/\u0432\u044b\u0447\u0438\u0442\u0430\u043d\u0438\u044f, \u0442\u043e \u0440\u0430\u0437\u043d\u0438\u0446\u044b \u043c\u0435\u0436\u0434\u0443 \u0432\u0440\u0435\u043c\u0435\u043d\u0435\u043c \u0441\u0447\u0435\u0442\u0430 \u043d\u0430 CPU \u0438 GPU \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442. \u0411\u043e\u043b\u0435\u0435 \u0442\u043e\u0433\u043e, \u043e\u0447\u0435\u043d\u044c \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e, \u0447\u0442\u043e GPU \u043f\u0440\u043e\u0438\u0433\u0440\u0430\u0435\u0442 \u0438\u0437-\u0437\u0430 \u043d\u0430\u043a\u043b\u0430\u0434\u043d\u044b\u0445 \u0440\u0430\u0441\u0445\u043e\u0434\u043e\u0432 \u043f\u043e \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u043f\u0430\u043c\u044f\u0442\u044c\u044e.  <\/p>\n<div style=\"text-align:center;\"><img decoding=\"async\"  src=\"http:\/\/habrastorage.org\/files\/170\/959\/8ed\/1709598ed0ad451099c188f3cdd97f8d.png\"\/><\/div>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0421\u043a\u0440\u044b\u0442\u044b\u0439 \u0442\u0435\u043a\u0441\u0442<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code>gpu_time &lt;- c() cpu_time &lt;- c() n &lt;- seq.int(1e4, 1e8, length.out=30) for (i in n) {   v &lt;- rnorm(i, 1000)   gpu_time &lt;- c(gpu_time, system.time({p1 &lt;- gpunrm(v)})[1])   cpu_time &lt;- c(cpu_time, system.time({p2 &lt;- sqrt(sum(v^6))})[1]) } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<h3>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h3>\n<p>  \u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0432 R \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043c\u0430\u0442\u0440\u0438\u0446\u0430\u043c\u0438 \u0438 \u0432\u0435\u043a\u0442\u043e\u0440\u0430\u043c\u0438 \u043e\u0447\u0435\u043d\u044c \u0445\u043e\u0440\u043e\u0448\u043e \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u044b, \u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c \u0432 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 GPU \u0432 \u043e\u0431\u044b\u0447\u043d\u043e\u0439 \u0436\u0438\u0437\u043d\u0438 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u043d\u0435 \u0442\u0430\u043a \u0443\u0436 \u0438 \u0447\u0430\u0441\u0442\u043e, \u043d\u043e \u0438\u043d\u043e\u0433\u0434\u0430 GPU \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0437\u0430\u043c\u0435\u0442\u043d\u043e \u0441\u043e\u043a\u0440\u0430\u0442\u0438\u0442\u044c \u0432\u0440\u0435\u043c\u044f \u0440\u0430\u0441\u0447\u0435\u0442\u0430. \u0412 CRAN \u0443\u0436\u0435 \u0435\u0441\u0442\u044c \u0433\u043e\u0442\u043e\u0432\u044b\u0435 \u043f\u0430\u043a\u0435\u0442\u044b (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <code>gputools<\/code>), \u0430\u0434\u0430\u043f\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 GPU (<a href=\"https:\/\/thirdwing.github.io\/2013\/10\/06\/r-and-gpu\/\">\u0442\u0443\u0442<\/a> \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u043f\u0440\u043e \u044d\u0442\u043e \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435). <\/p>\n<h4>\u0421\u0441\u044b\u043b\u043a\u0438<\/h4>\n<p>  1. <a href=\"http:\/\/www.biostat.jhsph.edu\/~rpeng\/docs\/interface.pdf\">An Introduction to the .C Interface to R<\/a><br \/>  2. <a href=\"https:\/\/www.stat.berkeley.edu\/classes\/s243\/calling.pdf\">Calling C Functions in R and Matlab<\/a><br \/>  3. <a href=\"http:\/\/www.themattsimpson.com\/2012\/06\/29\/writing-cuda-c-extensions-for-r\/\">Writing CUDA C extensions for R<\/a><br \/>  4. <a href=\"http:\/\/docs.nvidia.com\/cuda\/thrust\/\">Thrust::CUDA Toolkit Documentation<\/a>      \t<\/p>\n<div class=\"clear\"><\/div>\n<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/220927\/\"> http:\/\/habrahabr.ru\/post\/220927\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\">     \t<img decoding=\"async\" align=\"left\" src=\"http:\/\/habrastorage.org\/getpro\/habr\/post_images\/ef6\/ad5\/ec5\/ef6ad5ec526ad577bccf516e99761c06.png\"\/>\u0418\u043d\u043e\u0433\u0434\u0430 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c \u0443\u0441\u043a\u043e\u0440\u0438\u0442\u044c \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f, \u043f\u0440\u0438\u0447\u0435\u043c \u0436\u0435\u043b\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0441\u0440\u0430\u0437\u0443 \u0432 \u0440\u0430\u0437\u044b. \u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043e\u0442\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u043e\u0442 \u0443\u0434\u043e\u0431\u043d\u044b\u0445, \u043d\u043e \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u044b\u0445 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0438 \u043f\u0440\u0438\u0431\u0435\u0433\u0430\u0442\u044c \u043a \u0447\u0435\u043c\u0443-\u0442\u043e \u0431\u043e\u043b\u0435\u0435 \u043d\u0438\u0437\u043a\u043e\u0443\u0440\u043e\u0432\u043d\u0435\u0432\u043e\u043c\u0443 \u0438 \u0431\u044b\u0441\u0442\u0440\u043e\u043c\u0443. R \u0438\u043c\u0435\u0435\u0442 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0440\u0430\u0437\u0432\u0438\u0442\u044b\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u043c\u0438 \u0431\u0438\u0431\u0438\u043e\u0442\u0435\u043a\u0430\u043c\u0438, \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u043c\u0438 \u043d\u0430 \u0421\/\u0421++, Fortran \u0438\u043b\u0438 \u0434\u0430\u0436\u0435 Java. \u042f \u043f\u043e \u043f\u0440\u0438\u0432\u044b\u0447\u043a\u0435 \u043f\u0440\u0435\u0434\u043f\u043e\u0447\u0438\u0442\u0430\u044e \u0421\/\u0421++.  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-220927","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/220927","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=220927"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/220927\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=220927"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=220927"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=220927"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}