Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

Highlighted
##

dboggs

New Contributor I

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

05-22-2017
02:22 PM

1 View

reordering a vector

I have a vector, REAL(4)::V(1000). Say that the largest one happens to be V(123). I need to re-order the values so that V begins with the largest, i.e. Vnew(1) = Vold(123)

Vnew(2) = Vold(124)

;

Vnew(878) = Vold(1000)

Vnew(879) = Vold(1)

Vnew(880) = Vold(2)

:

Vnew(1000) = Vold(122)

That is, the sequence is to remain but the starting point must shift (it is NOT a matter of sorting in numerical order). Of course this can be done in an elementary straightforward manner by creating a copy, say Vaux, reassigning every element in a loop, and then copying Vaux back to V. I suspect there is a better way. Can anyone suggest an optimal algorithm, reducing both the memory required and the execution time?

6 Replies

Highlighted
##

andrew_4619

Valued Contributor II

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

05-22-2017
03:32 PM

1 View

You could mess with pointers

You could mess with pointers maybe but it isn't that big I would do a couple of block copies and a move_alloc rather than copying everything twice.

subroutine order implicit none integer, parameter :: vsize = 1000 integer :: ip1, isz, l1, istat real, allocatable :: V(:), Vtmp(:) allocate(V(vsize), stat = istat) if (istat /= 0) stop 'poo!' do l1 = 1 , vsize ! fill V V(l1) = 1.0*l1 enddo ip1 = 123 ! the pivot point in V isz = vsize - ip1 + 1 ! size of the lower chunk allocate (Vtmp(vsize), stat = istat) if (istat /= 0) stop 'poo!' vtmp(1:isz) = V(ip1:vsize) vtmp(isz+1:vsize) = V(1:ip1-1) call move_alloc(Vtmp, V) end subroutine order

Highlighted
##

FortranFan

Valued Contributor III

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

05-22-2017
05:11 PM

1 View

You can simply do the

You can simply do the following using the intrinsic CSHIFT

V = cshift( V, shift=maxloc(V, dim=1)-1 )

Note though MAXLOC returns "a value equal to the first element of MAXLOC .."

CSHIFT is very efficient for rank-one arrays.

Highlighted
##

andrew_4619

Valued Contributor II

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

05-22-2017
05:47 PM

1 View

Quote:FortranFan wrote:

FortranFan wrote:

You can simply do the following using the intrinsic CSHIFT

V = cshift( V, shift=maxloc(V, dim=1)-1 )Note though MAXLOC returns "a value equal to the first element of MAXLOC .."

CSHIFT is very efficient for rank-one arrays.

Certainly concise but that would surely create temporaries and data (2?) data copies?

Highlighted
##

cshift looks a useful function! Another bit of learning! I would guess that will copy into a temp and then copy the temp back to the original but that isn't really a penalty if the data size is not huge.

andrew_4619

Valued Contributor II

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

05-22-2017
05:54 PM

1 View

cshift looks a useful

Highlighted
##

FortranFan

Valued Contributor III

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

05-22-2017
06:34 PM

1 View

Quote:andrew_4619 wrote:

andrew_4619 wrote:

.. I would guess that will copy into a temp and then copy the temp back to the original but that isn't really a penalty if the data size is not huge.

With array transformations, one might as well as employ intrinsics as much as possible: if one can do better than Intel Fortran, then bring it up with Intel to improve their implementaton.

Highlighted
##

andrew_4619

Valued Contributor II

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

05-23-2017
03:36 AM

1 View

Quote:FortranFan wrote:With

FortranFan wrote:

With array transformations, one might as well as employ intrinsics as much as possible: if one can do better than Intel Fortran, then bring it up with Intel to improve their implementaton.

Yes I agree with that, in a bored moment later in the week I might make some tests for my own amusement and report back if there is anything of interest.

For more complete information about compiler optimizations, see our Optimization Notice.