首页 > 解决方案 > 数组的上界如何在分配时为 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

对于EXIThttps ://gcc.gnu.org/onlinedocs/gfortran/EXIT.html

For erase:这是我实现的从数组中删除元素的子程序。

编译器:gfortran

标签: arrayssegmentation-faultfortranindexoutofboundsexception

解决方案


推荐阅读