This is the mail archive of the
fortran@gcc.gnu.org
mailing list for the GNU Fortran project.
Re: timeline?
- From: "Thomas Breuel" <tmbdev at gmail dot com>
- To: "Janne Blomqvist" <blomqvist dot janne at gmail dot com>
- Cc: fortran at gcc dot gnu dot org
- Date: Thu, 6 Nov 2008 03:15:19 +0100
- Subject: Re: timeline?
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to :subject:cc:in-reply-to:mime-version:content-type :content-transfer-encoding:content-disposition:references; bh=j7Bdq02nM6Uw2ooNMBn7HzMF001AwDxDn0TW+Jr9YjY=; b=ZYdGh7x0/NJA0ySqhaYv1cDidwbXXSRAB6swRE2sM9P0o6TpT/MWp8Cr2ycV5u8E9g soZzbuNgKW61CDp0lMpFpIfQ5zllsGNrT7PFqggdlIHaWeCrmoXJ2kPLd8FjMFAR/eIv DsPh65P+jLvz9fvfsK4lqgtogVnNdeCTluKBE=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:cc:in-reply-to:mime-version :content-type:content-transfer-encoding:content-disposition :references; b=TSpYk8Yxn9gLRo25UvEKZ1dyj5Ox4ib3oL1cnBS5jGSxgXfqBvSmZefN25g71IwztD vLZ6XTsWQ4PUjU48gnD4UpWwsUzjyhZMZiy1xhPzQ+LEKmjv0mCkGIlEKtv8SRq6slTc gQ5FGgN+Bpop+mEGgBccHTE126yJpSuxsv1BI=
- References: <7e51d15d0811050926r75d2414fsc65729a62fa5b08b@mail.gmail.com> <7e51d15d0811050933h6c6a8ca0jcc9b3343ec5b63f2@mail.gmail.com> <4911E06F.3090203@net-b.de> <7e51d15d0811051101n3495fc89pe7cd467b5c3d4ba3@mail.gmail.com> <4911F428.2050504@net-b.de> <7e51d15d0811051338l292ccc7ob1f5007b53854ff@mail.gmail.com> <4912283B.1040603@gmail.com>
>> Are you saying that a = [a,1] actually uses realloc internally?
>>
>> Even with MOVE_ALLOC, if you try to do the same thing, you end up
>> making one copy.
>
> IIRC both of these use realloc behind the covers.
In GFortran 4.3.2, a = [a,1] calls malloc, even with -O3. It would be
nice if it did call realloc.
The standard way of using MOVE_ALLOC for resizing is (Metcalf, Reid, Cohen):
allocate(temp(n,m))
temp(1:size(a,1),1:size(a,2)) = a
call move_alloc(temp,a)
The copy happens during the assignment statement, before the
move_alloc. At that point, both a and temp are in memory which, for
large arrays, can be far too large.
Replacing this with a realloc would require recognizing and optimizing
the entire pattern, and GFortran doesn't do that; it just calls
malloc.
(Tracing this, I just discovered that statements like temp(1:size(a))
= a make one shared library call to _gfortran_size* for each element
transferred, even on -O3, which would seem to be a potential
performance problem.)
> There is no guarantee of this. Yes, realloc() tries to avoid copying, but
> it's not always possible.
Of course, there is no guarantee. If a C compiler doesn't implement
realloc efficiently, then some programs will simply not work on some
hardware because they run out of memory, and some other programs will
run slowly. That's no different from any other intrinsic in C or
Fortran. There is no guarantee, for example, that my Fortran compiler
lets me allocate large arrays or doesn't fragment memory
unnecessarily.
Tom
PS:
> As an aside, p = realloc(p, ...) is a bad idea
I suggest you think that through a little more carefully.