Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

Prototype designs always work. -- Don Vonada


devel / comp.lang.fortran / Re: A question on reallocation

SubjectAuthor
* A question on reallocationGianLuigi Piacentini
+- Re: A question on reallocationgah4
+- Re: A question on reallocationm_b_metcalf
`- Re: A question on reallocationGianLuigi Piacentini

1
A question on reallocation

<uj33a1$1rqp5$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=3749&group=comp.lang.fortran#3749

  copy link   Newsgroups: comp.lang.fortran
Path: i2pn2.org!i2pn.org!news.1d4.us!usenet.goja.nl.eu.org!weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ggpiace@tin.it (GianLuigi Piacentini)
Newsgroups: comp.lang.fortran
Subject: A question on reallocation
Date: Wed, 15 Nov 2023 19:40:00 +0100
Organization: A noiseless patient Spider
Lines: 36
Message-ID: <uj33a1$1rqp5$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 15 Nov 2023 18:40:01 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="69e1c94d2d3cf28eaa55adc4b9e0ac8f";
logging-data="1960741"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/baqqqyOu855WOjP04Exsg"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:9K8b7cLjA9s1WqFGfizDb3a2iw0=
Content-Language: en-US
 by: GianLuigi Piacentini - Wed, 15 Nov 2023 18:40 UTC

Hi all,

I have a perhaps silly question, that puzzles me before committing to
write actual code.

Please consider a data structure which is basically an array of arrays
type element
integer, allocatable :: element_core(:)
end type
....
type(elements), allocatable :: array_of_elements(:)
....
allocate ( array_of_elements(some_size) )

(this is not a matrix, each element may significantly differ in size,
and someone may be long (and subjected to reallocation cycles, but his
is plain reallocation).

Now I have to increase size of previously allocated array_of_elements,
using the usual pattern (at least I think it's usual)

type(elements), allocatable :: tmp(:)

allocate ( tmp(new_size) )
tmp(1:some_size) = array_of_elements ! ***
call move_alloc(from = tmp, to = array_of_elements)

Seems to me that during the operation marked with the *** comment the
various elements are also copied, and this seems vasteful.
Is there a way to avoid such copying, if it really happens ?

Perhaps making array_of_elements an array of pointers to allocated
elements ?

Thanks in advance
Gigi Piacentini

Re: A question on reallocation

<d355265a-e676-48d5-ad62-d444c99f957a@ugcs.caltech.edu>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=3752&group=comp.lang.fortran#3752

  copy link   Newsgroups: comp.lang.fortran
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: gah@ugcs.caltech.edu (gah4)
Newsgroups: comp.lang.fortran
Subject: Re: A question on reallocation
Date: Wed, 15 Nov 2023 20:19:01 -0800
Organization: UW
Lines: 41
Message-ID: <d355265a-e676-48d5-ad62-d444c99f957a@ugcs.caltech.edu>
References: <uj33a1$1rqp5$1@dont-email.me>
Reply-To: gah4@u.washington.edu
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: dont-email.me; posting-host="fd5366f1010c5ede7730a67ee858882e";
logging-data="2246206"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19HO0e+4gWndjQpXRqV52hT"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:drTr2ZbBdxY8LV+IiXyeEigoyps=
Content-Language: en-US
In-Reply-To: <uj33a1$1rqp5$1@dont-email.me>
 by: gah4 - Thu, 16 Nov 2023 04:19 UTC

On 11/15/23 10:40 AM, GianLuigi Piacentini wrote:
> Hi all,
>
> I have a perhaps silly question, that puzzles me before committing to
> write actual code.
>
> Please consider a data structure which is basically an array of arrays
> type element
>   integer, allocatable :: element_core(:)
> end type
> ...
> type(elements), allocatable :: array_of_elements(:)
> ...
> allocate ( array_of_elements(some_size) )

(snip)

> type(elements), allocatable :: tmp(:)

> allocate ( tmp(new_size) )
> tmp(1:some_size) = array_of_elements                   ! ***
> call  move_alloc(from = tmp, to = array_of_elements)

I think you want something like:

allocate(tmp(new_size))

do i=1, old_size
call move_alloc(from=array_of_elements(i), to=tmp(i))
end do

call move_alloc(from = tmp, to = array_of_elements)

So, move all the subarrays over, then move the array of arrays.

Since only the, fairly small, array_of_elements is being reallocated,
it should be pretty fast.

Before move_alloc, you had to create a temporary, copy them over,
reallocate the original one, and copy them back. Lots of copying!

Re: A question on reallocation

<gou4OpXhf_oG-akBNGA8IvIYzBs@jntp>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=3759&group=comp.lang.fortran#3759

  copy link   Newsgroups: comp.lang.fortran
Path: i2pn2.org!i2pn.org!news.niel.me!pasdenom.info!from-devjntp
Message-ID: <gou4OpXhf_oG-akBNGA8IvIYzBs@jntp>
JNTP-Route: news2.nemoweb.net
JNTP-DataType: Article
Subject: Re: A question on reallocation
References: <uj33a1$1rqp5$1@dont-email.me>
Newsgroups: comp.lang.fortran
JNTP-HashClient: l-hZMGz679e4f3OE7CBSqeEgKY0
JNTP-ThreadID: uj33a1$1rqp5$1@dont-email.me
JNTP-Uri: http://news2.nemoweb.net/?DataID=gou4OpXhf_oG-akBNGA8IvIYzBs@jntp
User-Agent: Nemo/0.999a
JNTP-OriginServer: news2.nemoweb.net
Date: Sat, 18 Nov 23 09:29:31 +0000
Organization: Nemoweb
JNTP-Browser: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0
Injection-Info: news2.nemoweb.net; posting-host="62487d76289a9c2372000ba1045bdb3dd98cd49b"; logging-data="2023-11-18T09:29:31Z/8407761"; posting-account="211@news2.nemoweb.net"; mail-complaints-to="newsmaster@news2.nemoweb.net"
JNTP-ProtocolVersion: 0.21.1
JNTP-Server: PhpNemoServer/0.94.5
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-JNTP-JsonNewsGateway: 0.96
From: michaelmetcalf@compuserve.com (m_b_metcalf)
 by: m_b_metcalf - Sat, 18 Nov 2023 09:29 UTC

GianLuigi,

Does this do what you wanted?

Mike

P.S. This is also for me a trial posting using nemoweb.

type elements
integer, allocatable :: element_core(:)
end type

type(elements), allocatable :: array_of_elements(:)

type(elements), allocatable :: tmp(:)

allocate ( array_of_elements(3) )

allocate(array_of_elements(3)%element_core(3))

array_of_elements(3)%element_core(3) = 3

allocate ( tmp(4) )

call move_alloc(from=array_of_elements(1:3), to=tmp(1:3)) !<------

call move_alloc(from = tmp, to = array_of_elements)
print*, array_of_elements(3)%element_core(3)
end

Re: A question on reallocation

<uje862$p8n$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=3771&group=comp.lang.fortran#3771

  copy link   Newsgroups: comp.lang.fortran
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ggpiace@tin.it (GianLuigi Piacentini)
Newsgroups: comp.lang.fortran
Subject: Re: A question on reallocation
Date: Mon, 20 Nov 2023 01:10:41 +0100
Organization: A noiseless patient Spider
Lines: 83
Message-ID: <uje862$p8n$1@dont-email.me>
References: <uj33a1$1rqp5$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 20 Nov 2023 00:10:42 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="7dade537343e9a98db61730165f478cc";
logging-data="25879"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+bS/1yldagugI+jP/BtnZ/"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:Cf/1RNJuJ6qenObkPf9rY+i2els=
In-Reply-To: <uj33a1$1rqp5$1@dont-email.me>
Content-Language: en-US
 by: GianLuigi Piacentini - Mon, 20 Nov 2023 00:10 UTC

On 15/11/23 19:40, GianLuigi Piacentini wrote:
> Hi all,
>
> I have a perhaps silly question, that puzzles me before committing to
> write actual code.
>
> Please consider a data structure which is basically an array of arrays
> type element
>   integer, allocatable :: element_core(:)
> end type
> ...
> type(elements), allocatable :: array_of_elements(:)
> ...
> allocate ( array_of_elements(some_size) )
>
> (this is not a matrix, each element may significantly differ in size,
> and someone may be long (and subjected to reallocation cycles, but his
> is plain reallocation).
>
> Now I have to increase size of previously allocated array_of_elements,
> using the usual pattern (at least I think it's usual)
>
> type(elements), allocatable :: tmp(:)
>
> allocate ( tmp(new_size) )
> tmp(1:some_size) = array_of_elements                   ! ***
> call  move_alloc(from = tmp, to = array_of_elements)
>
> Seems to me that during the operation marked with the *** comment the
> various elements are also copied, and this seems vasteful.
> Is there a way to avoid such copying, if it really happens ?
>
> Perhaps making array_of_elements an array of pointers to allocated
> elements ?
>
> Thanks in advance
> Gigi Piacentini
Thanks to all replier.

I ewconsidered the problem in light of the whole project, coming with 2
possible solutions:

1) since data come from analysis of several files, which do not change
during program execution, I can do a 1st pass counting dimensions, then
allocate, then do a 2nd pass reading data into the data structure.

2) as suggested, I could do
type element
integer, allocatable :: element_core(:) ! an array of integers
end type
type element_pointer
type(element), pointer :: e_p => null() ! pointer to the above
end type element_pointer

type(element_pointer), allocatable :: array_of_elements(:)

allocate( array_of_elements(3) ) ! an array of 3 pointers
do i = 1, size(array_of_elements)
allocate (array_of_elements(i)%e_p%element_core(n))
! where n is the size required for the ith array of integers
end do
.... ! loading integers

Now when reallocating

type(element_pointer), allocatable :: tmp(:)

allocate( tmp(new size for array of array of integers) )

! copying pointers:
tmp(1:size(array_of_elements))%e_p => array_of_elements%e_p
! during this copy, in my intention, the underlying element_cores will
not be moved
call move_alloc (from = tmp, to = array_of_elements)

However, at the moment I cannot test the above machinery, I will do it
as soon as I can restart my hobby project, which will happen in some days.
But I am still interested in the subject, and in your comments, if any.

Thanks
Gigi

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor