Hidden global bugs of bad programming practices?

I am writing some linear algebra code (in Fortran 2003, but that would be the same problem in Fortran 90 or C), which requires several work vectors to perform the calculations. My idea for solving this problem is to make a working array w(:,:)that is private for a linear algebra module, that is, “hidden global”, as defined in this discussion , why true global variables are horrible.

I assume this is a problem with a big problem on the board, and for each part of the problem you select the area of ​​the board to solve it.

According to this analogy, I could also have a bunch of little boards: determine the type of data work_arrayand pass it to the solvers as needed. (PETSc effectively uses this approach through a different level of abstraction, a solveris a data type that includes some pointers to methods used as several work vectors.) When there are nested calls from one solver to another, this complicates the blade, so I better like the first way. It also does not require the same wrong direction.

Any thoughts on which approach is suitable for best programming practice?

EDIT: I also don't think this will be a problem when I start using OpenMP, which I already did in the old incarnation of this code. Each thread gets access to its part of the unknown, and not to other topics after the installation of the problem. However, concurrency problems are probably a good reason not to use static variables in general.

Should I have to dynamically allocate space for scratch arrays every time I call a solver that often will not impose a lot of overhead?

+5
source share
3 answers

- , malloc free , , , , , , , , , (, , ). , , - .

, :

  • .
  • , ( , ).
  • / .
  • , , .
  • , , , , , , , - .
+6

" " ( C static), . , , . . , " " .

+5

: , ( w(:,:)). , , , - :

module test
   implicit none

    type :: buffer
        integer, allocatable :: work(:,:)
    end buffer

contains

    subroutine init(mybuffer, whatever_else_you_need_for_determinig_allocation_size)
        type(buffer), intent(out) :: mybuffer

         allocate(mybuffer%work(dim1, dim2))
     end subroutine init

     subroutine solver(mybuffer, whatever_else_you_need_for_the_solver)
         type(buffer), intent(inout) :: mybuffer

          ! You can access mybuffer%work here as it is allocated

      end subroutine solver

end module test

, , , .

+1

All Articles