插入一个值分配向量FORTRAN造型多变 [英] Insert a value changing shape in allocated vector fortran

查看:105
本文介绍了插入一个值分配向量FORTRAN造型多变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想请教各位,我经常在使用Fortran的CSR / CSC *矩阵的管理问题。
假设我们有N个实值的矢量V。该载体已经分配previously具有一定的大小。现在我们要在它的中间指标P.在添加一个值
蛮力code将是:

I would like to ask you about a problem that I have frequently in the management of CSR/CSC* matrices using Fortran. Suppose we have a vector V with N real values. The vector has been allocated previously with a certain size. Now we have to add a value in the middle of it at the index P. A brute force code would be:

allocate(tempV(N))
tempV=V
deallocate(V)
allocate(V(N+1))
V=(/tempV(1:P-1), newValue, tempV(P:N)/)
deallocate(tempV)

显然,如果是进行一次它不是一个问题,但重复它几千倍不会那么有效。内存将填补而空4次,每次价值,我想插入。

Clearly, if it is done once it is not a problem, but repeating it thousands times would not be so efficient. Memory would fill and empty 4 times every value I would like to insert.

我想知道这将是一个更好的方法来解决这个问题。你可以提出普通的Fortran(preferred),而且还喜欢MKL / LAPACK /布拉斯库的一些解决方案。

I would like to know which would be a better procedure to tackle this problem. You can propose plain Fortran (preferred), but also some solution by libraries like MKL/Lapack/Blas.

附录:可能我RESHAPE办呢?通过这个定义(同我的Fortran书定义),我可以做类似

Addendum: could I do it with RESHAPE? going through this definition (same of my Fortran-book definition), I could do something like

REAL, DIMENSION(1:1) :: newPad = (/ newValue /)
V=RESHAPE(V, (/ N+1 /), PAD=newPad)

现在的值已在V的末尾添加,所以我做了置换

Now the values has been added at the end of V, so I make a permutation with

V =(/ V(1:P-1),V(N + 1:N + 1),v(P:N)/)

V=(/ V(1:P-1), V(N+1:N+1), V(P:N) /)

在这种方式,这将避免显式地创建一个临时的向量,并输分配

In this way, it would avoid to create explicitly a temporary vector and to lose allocation.

它是否是有效的和可扩展的,因为RESHAPE可以在库中已并行化

Would it be efficient and scalable since RESHAPE could be parallelized already in the libraries?

* PS :为了把事情说清楚CSR = com的pressed稀疏行格式,CSC = com的pressed稀疏列格式,这里更多的相关信息:

*PS: To make things clear CSR = Compressed Sparse Row format, CSC = Compressed Sparse Column format, more infos here:

MKL定义:的<一个href=\"http://software.intel.com/sites/products/documentation/hpc/mkl/mklman/GUID-9FCEB1C4-670D-4738-81D2-F378013412B0.htm\" rel=\"nofollow\">http://software.intel.com/sites/products/documentation/hpc/mkl/mklman/GUID-9FCEB1C4-670D-4738-81D2-F378013412B0.htm

推荐答案

Fortran 2003的子程序 MOVE_ALLOC 中引入了这一目的。它移动的分配状态,数组边界,动态类型,类型参数和值从源到目标,而无需实际复制数据。源变量变成释放。

The Fortran 2003 subroutine move_alloc was introduced for this purpose. It moves the allocation status, array bounds, dynamic type, type parameters and values from the source to the target, without actually copying the data. The source variable becomes deallocated.

您code与 MOVE_ALLOC 的一个简短的例子,只需要一个复制操作的修改:

A modification of your code with a short example of move_alloc that requires only one copy operation:

allocate(tempV(N+1)) 
tempV(:P-1) = V(:P-1)
tempV(P)    = newValue
tempV(P+1:) = V(P:) 
call move_alloc(tempV, V)

当然,在同一时间做这行的多个项目将减少分配开销,但可能不适合你是不可能的。

Of course, doing this for multiple items at a time would reduce allocation overhead, but that may not be possible for you.

修改结果
至于指针的建议,如果你不能使用任何F2003功能,你很可能在一个子程序有点像这样的:

Edit
As for the recommendation of pointers if you cannot use any F2003 features, you could probably use a subroutine somewhat like this:

pure subroutine insert(arr, val, pos)
  real, pointer, intent(inout) :: arr(:)
  real, intent(in)             :: val
  integer, intent(in)          :: pos

  real, pointer :: temp(:)

  if(associated(arr)) then
    allocate(temp(lbound(arr,1):ubound(arr,1) + 1))
    ! ...or perhaps check/do something if pos is not within these bounds

    temp(:pos-1) = arr(:pos-1)
    temp(pos)    = val 
    temp(pos+1:) = arr(pos:)

    deallocate(arr)
    arr => temp
  endif
end subroutine insert

当然,你可以轻松适应给你的目的,或使其更通用。您可以使用分配的指针变量使用:

Of course you can easily adapt this to your purposes or make it more generic. You can use this with allocated pointer variables:

real, pointer :: V(:)
! :
allocate(V(10))
V = 1.
! :
call insert(V, 3.141592, 5)

这篇关于插入一个值分配向量FORTRAN造型多变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆