Skip to content

Hoist malloc/free out of loops. #1500

@Leporacanthicus

Description

@Leporacanthicus

Avoiding malloc/free altogether would be even better, but not always doable.

The following is an example of code that calls malloc & free [as an implementation of fir.allocmem and fir.freemem] to implement the WHERE functionality.

This is another performance enhancement found when investigating performance of the SNAP application.

Here's an example:

program m
  implicit none
  REAL(8), ALLOCATABLE, DIMENSION(:,:,:,:) :: qim

  integer(4) :: nang, noct,nx, ny, ng
  integer(4) :: ll

  real(8) :: total

  real :: time_start
  real :: time_end

  nx = 40
  ny = 40
  noct = 6
  ng = 18


  print *, "Total size = ", nx * ny * noct * ng * 8
  ALLOCATE(qim(nx,ny,noct,ng))

  qim = 1.0

  total = 0.0

  call CPU_TIME(time_start)
  do ll = 1, 20000
     where (qim >= 1.0) qim = 1.0
  end do
  total = sum(qim)
  call CPU_TIME(time_end)
  
  print "(A, F6.3)", " Time taken (s): ", time_end - time_start
  print *, total * 8, " This should match total size"

end program m

Metadata

Metadata

Assignees

No one assigned

    Labels

    FIRFIR language relatedOptimizeroptimization pass(es) relatedcodegenconversion of FIR/MLIR to LLVM IR

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions