arrays - 数组的上界如何在分配时为 0?
问题描述
我面临分段错误,因此我尝试编译代码-fcheck=all
并在执行后得到:
Program received signal SIGSEGV: Segmentation fault - invalid memory reference.
Backtrace for this error:
At line 237 of file app_management_test.f90
Fortran runtime error: Index '1' of dimension 1 of array 'free_workers_list' above upper bound of 0
我不明白上限怎么可能是 0。这是我使用数组的部分代码free_workers_list
。
!Thread Application Master (numero 1)
if (num_thread==1) then
do ff=5,20 ! 16 tasks
if (associated(tasklist_GRAD(ff)%f_ptr) .eqv. .true.) then
tasks_ready_master(ff) = tasklist_GRAD(ff)
tasks_ready_master(ff)%state=STATE_READY
end if
end do
end if
if ((num_thread .ne. 0) .and. (num_thread .ne. 1)) then
do ff=1,nthreads-2
if (num_thread==ff+1) then
tasks_ready_master(ff)%state=STATE_RUNNING
free_workers_list(ff)=1 !!!!!!!!!!!!!!!!!!!!!!!!! line 237 (error)
call tasks_ready_master(ff)%f_ptr(self,var)
tasks_ready_master(ff)%state=STATE_INACTIVE
free_workers_list(ff)=0
s=size(tasks_ready_master)
call erase(1,s,tasks_ready_master)
end if
end do
这是子程序的一部分:
subroutine management(tasklist_GRAD,var)
INTEGER ::ff,l
type(tcb)::self
type(variables)::var
integer::s
!OpenMP variables
integer::num_thread,nthreads
integer:: OMP_GET_THREAD_NUM, OMP_GET_NUM_THREADS
type(tcb),dimension(20)::tasklist_GRAD,tasks_ready_master
integer::STATUS
integer,allocatable,dimension(:)::free_workers_list !liste contenant les nums des threads workers
!Variables pour gestion des threads
INTEGER(KIND=omp_lock_kind), SAVE :: lock
CALL omp_init_lock (lock)
!=======================================================================================================================================================
!$OMP PARALLEL PRIVATE(num_thread,nthreads,ff) &
!$OMP SHARED(tasklist_GRAD,tasks_ready_master,free_workers_list)
num_thread=OMP_GET_THREAD_NUM() ! le rang du thread
nthreads=OMP_GET_NUM_THREADS() ! le nombre de threads
if (nthreads<3) then
print *, 'This program is going to exit.'
call EXIT(STATUS)
end if
!Thread Master (number 0)
if (num_thread==0) then
allocate(free_workers_list(nthreads-2)) ! liste des threads workers
do ff=1,nthreads-2
free_workers_list(ff)=0 ! 2,3,..,nombre de threads-2
end do
end if
!Thread Application Master (number 1)
if (num_thread==1) then
do ff=1,3 ! 3 tâches
if (associated(tasklist_GRAD(ff)%f_ptr) .eqv. .true. ) then ! Si tâche attribuée
tasks_ready_master(ff) = tasklist_GRAD(ff) ! égalité de pointeurs
tasks_ready_master(ff)%state=STATE_READY
end if
end do
end if
!Threads Workers (number diff than 0 and 1)
if (nthreads==5) then ! 5 threads ==> 3 workers
if ((num_thread .ne. 0) .and. (num_thread .ne. 1)) then
do ff=1,nthreads-2
if (num_thread==ff+1) then
tasks_ready_master(ff)%state=STATE_RUNNING
free_workers_list(ff)=1 !!!!!!!!! error
call tasks_ready_master(ff)%f_ptr(self,var)
tasks_ready_master(ff)%state=STATE_INACTIVE
free_workers_list(ff)=0
s=size(tasks_ready_master)
call erase(1,s,tasks_ready_master)
end if
end do
end if
end subroutine management
对于EXIT
:https ://gcc.gnu.org/onlinedocs/gfortran/EXIT.html
For erase
:这是我实现的从数组中删除元素的子程序。
编译器:gfortran
解决方案
推荐阅读
- django - 返回 CSRF_FAILURE_VIEW 的 GraphiQL 查询
- javascript - 谷歌标签管理器丢失结帐步骤
- python - 通过组合索引#、字符串创建列
- bash - bash如何替换/删除行首后跟空格
- powershell - 如何将档案从 dir/subdir/*.* 提取到 dir/%zipname%/
- io - fsync 与原始设备 fd
- mysql - 使用复合主键作为外键
- javascript - 使用事件 onClick window.open 更改 Images src
- reactjs - 如何在功能性 HOC 中访问道具?
- python - 哪种文件类型最适合这种情况?