MODULE Compute
USE Global_variables
IMPLICIT NONE
CONTAINS
attributes ( global ) SUBROUTINE Age_add(People_d)
INTEGER :: I,N1
type(Person), allocatable :: People_d(:)
I=(BLOCKIDX%X-1)*BLOCKDIM%X+THREADIDX%X
N1=SIZE(People_d,1)
IF (I>N1) RETURN
People_d(I)%age = People_d(I)%age + 1
END SUBROUTINE
END
MODULE Global_variables
IMPLICIT NONE
type Person
integer(kind=4) :: age ! 年龄
integer(kind=4) :: height ! 身高
integer(kind=4) :: weight ! 体重
end type
END
program Test
use cudafor
use Global_variables
implicit none
integer , parameter :: PersonNum = 10000
type(Person), allocatable :: People(:)
type(Person),device,allocatable :: People_d(:)
integer :: i
allocate(People(PersonNum))
allocate(People_d(PersonNum))
DO i = 1, PersonNum
People(i)%age = i
People(i)%height = i
People(i)%weight = i
ENDDO
People_d = People
CALL Age_add<<<CEILING(REAL(PersonNum)/256),256>>>(People_d)
People = People_d
write(*,*) People(500)%age
write(*,*) SIZE(People, 1)
end program Test
QQ图片20211016151752.png (77.14 KB, 下载次数: 295)
MODULE Global_variables
IMPLICIT NONE
type Person
integer(kind=4) :: age ! 年龄
integer(kind=4) :: height ! 身高
integer(kind=4) :: weight ! 体重
end type
END module
MODULE Compute
USE Global_variables
IMPLICIT NONE
CONTAINS
attributes ( global ) SUBROUTINE Age_add(People_d)
INTEGER :: I,N1
type(Person) :: People_d(:)
I=(BLOCKIDX%X-1)*BLOCKDIM%X+THREADIDX%X
N1=SIZE(People_d,1)
IF (I>N1) RETURN
People_d(I)%age = People_d(I)%age + 1
END SUBROUTINE
END
program Test
use cudafor
use Global_variables
use Compute
implicit none
integer , parameter :: PersonNum = 10000
type(Person), allocatable :: People(:)
type(Person),device,allocatable :: People_d(:)
integer :: i
allocate(People(PersonNum))
allocate(People_d(PersonNum))
DO i = 1, PersonNum
People(i)%age = i
People(i)%height = i
People(i)%weight = i
ENDDO
People_d = People
CALL Age_add<<<CEILING(REAL(PersonNum)/256),256>>>(People_d)
People = People_d
write(*,*) People(500)%age
write(*,*) SIZE(People, 1)
end program Test
11.jpg (41.7 KB, 下载次数: 311)
li913 发表于 2021-10-20 12:18
1、去掉 第六行的 allocatable;
2、MODULE Global_variables 放在最开始;
3、主程序 use Compute。
欢迎光临 Fortran Coder (http://bbs.fcode.cn/) | Powered by Discuz! X3.2 |