阵列分配速度问题

时间:2014-05-22 03:54:53

标签: fortran fortran90

我目前正在撰写蒙特卡罗代码,其数量正在波动。为了算法速度的原因,我将我的模拟单元格(立方体)划分为许多较小的立方体。然而,随着体积波动,这些较小立方体的数量也会波动。因此,我一直在重新分配包含这些较小立方体信息的数组:

 !.....................................................................................!
  ! want to assign each atom to appropriate cell
  ! generate list of atoms in each cell for GONET algorithm
  ! arrays are allocated and deallocated in this routine. 
  !......................................................................................!
  subroutine indiv_cell_lists_alloc(r)
    implicit none
    double precision :: r(3,param%np)
    integer :: i,icell,size_cl
    integer :: temp_num(0:listvar%ncellT-1) !...listvar%ncellT is total number of cells.
    integer :: temp_cell(param%np)


    temp_num(:) = 0

    !--- param%np is total number of particles in simulation
    do i = 1 ,param%np

       !--- based off coordinates, finds what cell particle i is in
       icell = cell(r(1,i),r(2,i),r(3,i))

       temp_cell(i) = icell
       temp_num(icell) = temp_num(icell) + 1     

       !--- keep particles current cell
       atom(i)%cell = icell 
       atom(i)%loc  = temp_num(icell)       
    enddo


    !--- listvar%cl is an array. listvar%cl(i) contains info for cell i
    !--- listvar%cl(i)%cmem(:) is an array containing the particles that are in the ith cell
    !--- deallocate cell member lists
    !--- subtract one since array is 0:ncellT-1, where ncellT is total number of cells
    size_cl = size(listvar%cl)-1
    do i = 0, size_cl
       deallocate(listvar%cl(i)%cmem)
    enddo

    !--- deallocate cell list
    deallocate(listvar%cl)

    !--- allocate new celllist
    allocate(listvar%cl(0:listvar%ncellT-1))

    !--- allocate new arrays
    do i = 0, listvar%ncellT-1
       !--- allocate new array based off new size
       allocate(listvar%cl(i)%cmem(temp_num(i)))

       !--- number of molecules in cell 
       listvar%cl(i)%num = temp_num(i)
    enddo



    do i= 1,param%np
       !--- get cell for particle
       icell = temp_cell(i)

       !--- place particles in cell
       listvar%cl(icell)%cmem((atom(i)%loc)) = i
    enddo


  end subroutine indiv_cell_lists_alloc

现在我确保这些数组只有它们需要的大小的原因是,最终这些数组将被导出到Xeon Phi协处理器。由于内存减少,我认为只是分配大量内存并忘记它会导致性能不佳。但是在串行执行中,这个子程序占用了我运行时间的70%左右。

您对如何更有效地完成此重新分配有任何建议,或者对我可以使用的其他方法有任何建议吗?

0 个答案:

没有答案