我要编写一个Fortran代码,该代码以长度为10的向量(Z = [1,2,3,...,8,9,10])开头,并取向量Z的每个元素为生成随机长度为m(0到5000)的向量(名称Z_ne),并将Z_ne添加到初始分配的长度为零的向量(名称Z_ne)。
program mc1
implicit none
real(8),allocatable:: Z_ne(:),Z_net(:)
real(8)::Z_n(10) ,z0 ! mean and standard deviation
integer :: j
integer::ti_1, ti_2,clock_rate, clock_max
call system_clock ( ti_1, clock_rate, clock_max )
Z_n=[1,2,3,4,5,6,7,8,9,10];
allocate(Z_net(0))
!$OMP PARALLEL default( shared ) private( j )
!$OMP DO
do j = 1, 10
print*,'j=',j
z0=Z_n(j)
call monte_carlo(z0,Z_ne)
!print*,'--------Z_ne=',Z_ne
call ArraysizeVEC(Z_net,Z_ne)
deallocate(Z_ne)
end do
!$OMP END DO
!$OMP END PARALLEL
Print*,'size(Z_net)=',size(Z_net)
!-------------------------------------------
call system_clock ( ti_2, clock_rate, clock_max )
print*,'Elapsed real time = ', real ( ti_2 - ti_1 ) / real ( clock_rate ),'Sec'
contains
SUBROUTINE monte_carlo(z0i,Z_nei)
integer::ni,mi
real(8)::ksi,z0i
real(8),allocatable:: ks1i(:),Z_nei(:)
ni=5000
call random_number(ksi)
mi=nint(ksi*ni)
allocate(ks1i(mi),Z_nei(mi))
call random_number(ks1i)
Z_nei=z0i*ks1i;
deallocate(ks1i )
ENDSUBROUTINE monte_carlo
SUBROUTINE ArraysizeVEC(x_VEC,y_VEC) an extra elements into array x
real(8),allocatable:: y_VEC(:),x_VEC(:),x1_VEC(:)
call move_alloc(x_VEC,x1_VEC)
allocate(x_VEC(size(x1_VEC)+size(y_VEC)))
x_VEC=[x1_VEC,y_VEC]
ENDSUBROUTINE ArraysizeVEC
end program mc1
以串行形式可以正常工作。但是以并行形式会给出错误:
j= 1
j= 6
At line 39 of file main.f95
Fortran runtime error: Attempting to allocate already allocated variable 'z_nei'
我是Fortran的新手,有人可以帮助我吗? 谢谢