﻿{"id":1475,"date":"2021-01-24T09:16:51","date_gmt":"2021-01-24T09:16:51","guid":{"rendered":"http:\/\/81.70.49.155\/?p=1475"},"modified":"2021-01-24T09:19:31","modified_gmt":"2021-01-24T09:19:31","slug":"cuda-fortran-%e6%b5%8b%e8%af%95%e7%a8%8b%e5%ba%8f","status":"publish","type":"post","link":"http:\/\/81.70.49.155\/?p=1475","title":{"rendered":"CUDA FORTRAN \u6d4b\u8bd5\u7a0b\u5e8f"},"content":{"rendered":"<div style=\"margin:0px;padding:0px;font-family:punctuation, \u5fae\u8f6f\u96c5\u9ed1, Tohoma;font-size:14px;background-color:#FFFFFF;\">\n\t<span style=\"background-color:#E53333;font-size:16px;\"><strong>module simpleOps_m<\/strong><\/span><br \/>\ncontains<br \/>\nattributes(global) subroutine inc(a, b)<br \/>\nimplicit none<br \/>\nreal :: a(:,:)<br \/>\nreal, value :: b<br \/>\ninteger :: i, j, n(2)<br \/>\ni = (blockIdx%x-1)*blockDim%x + threadIdx%x<br \/>\nj = (blockIdx%y-1)*blockDim%y + threadIdx%y<br \/>\n<span style=\"color:#E53333;\">!threadIdx%x\uff0cthreadIdx%y\u6bcf\u4e2a\u7ebf\u7a0b\u5757\u5185\u7684\u7ebf\u7a0b\u7f16\u53f7\uff08\u4e8c\u7ef4\uff09<\/span><br \/>\n<span style=\"color:#E53333;\">!blockDim%x\uff0cblockDim%y\u6bcf\u4e2a\u7ebf\u7a0b\u5757\u5185\u542b\u6709\u7684\u7ebf\u7a0b\u6570\uff08\u4e8c\u7ef4\uff09<\/span><br \/>\n<span style=\"color:#E53333;\">!blockIdx%x\uff0cblockIdx%y\u7ebf\u7a0b\u5757\u7f16\u53f7\uff08\u4e8c\u7ef4\uff09<\/span><br \/>\nn = size(a)<br \/>\nif (i&lt;=n(1) .and. j&lt;=n(2)) a(i,j) = a(i,j) + b<br \/>\n<span style=\"color:#E53333;\">!\u4e0d\u80fd\u8d8a\u754c<\/span><br \/>\nend subroutine inc<br \/>\nend module simpleOps_m<br \/>\n&nbsp;<\/p>\n<p><span style=\"background-color:#E53333;font-size:16px;\"><strong>program incTest<\/strong><\/span><br \/>\nuse cudafor<br \/>\nuse simpleOps_m<br \/>\nimplicit none<br \/>\ninteger, parameter :: nx=1024, ny=512<br \/>\n!\u5b9a\u4e491024\u884c512\u5217\u7684\u77e9\u9635<br \/>\nreal :: a(nx,ny), b<br \/>\nreal, device :: a_d(nx,ny)<br \/>\ntype(dim3) :: grid, tBlock<br \/>\na = 1; b = 3<br \/>\ntBlock = dim3(32,8,1)<br \/>\n<span style=\"color:#E53333;\">!\u5355\u4f4d\u7ebf\u7a0b\u5757\u884c\u8ba1\u7b97\u7ebf\u7a0b\u657032\uff0c\u5355\u4f4d\u7ebf\u7a0b\u5757\u5217\u8ba1\u7b97\u7ebf\u7a0b\u6570\u4e3a8<\/span><br \/>\ngrid = dim3(ceiling(real(nx)\/tBlock%x), ceiling(real(ny)\/tBlock%y), 1)<br \/>\n<span style=\"color:#E53333;\">!\u53c2\u4e0e\u884c\u8ba1\u7b97\u7684\u7ebf\u7a0b\u5757\u6570\u5757\u4e3a32\uff0c\u53c2\u4e0e\u5217\u8ba1\u7b97\u7684\u7ebf\u7a0b\u5757\u6570\u4e3a64<\/span><br \/>\n<span style=\"color:#E53333;\">!\u7ebf\u7a0b\u5757\u6570\u4e0d\u80fd\u5927\u4e8emax grid dims<\/span><br \/>\na_d = a<br \/>\ncall inc&lt;&lt;&lt;grid,tBlock&gt;&gt;&gt;(a_d, b)<br \/>\n!\u8c03\u7528\u683c\u5f0fkernelfunction&lt;&lt;&lt;dimGrid,dimBlock&gt;&gt;&gt;(parameters)<br \/>\na = a_d<br \/>\nwrite(*,*) 'Max error: ', maxval(abs(a-4))<br \/>\nend program incTest<\/p>\n<p><span style=\"background-color:#009900;\">From\uff1ahttp:\/\/blog.163.com\/zpfzcjndx@126\/blog\/static\/635456812014121104122345\/&nbsp;<\/span><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>module simpleOps_m contains attributes(global) subrouti &hellip; <a href=\"http:\/\/81.70.49.155\/?p=1475\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201cCUDA FORTRAN \u6d4b\u8bd5\u7a0b\u5e8f\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"views":1100,"_links":{"self":[{"href":"http:\/\/81.70.49.155\/index.php?rest_route=\/wp\/v2\/posts\/1475"}],"collection":[{"href":"http:\/\/81.70.49.155\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/81.70.49.155\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/81.70.49.155\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/81.70.49.155\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1475"}],"version-history":[{"count":3,"href":"http:\/\/81.70.49.155\/index.php?rest_route=\/wp\/v2\/posts\/1475\/revisions"}],"predecessor-version":[{"id":1478,"href":"http:\/\/81.70.49.155\/index.php?rest_route=\/wp\/v2\/posts\/1475\/revisions\/1478"}],"wp:attachment":[{"href":"http:\/\/81.70.49.155\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1475"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/81.70.49.155\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1475"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/81.70.49.155\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1475"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}