Prev: IDE for ifort on Linux
Next: a weird COMMON fact
From: Eli Osherovich on 10 Jul 2010 15:26 Can somebody explain me the reason(s) to deallocate a procedure's arguments whose intent is "out"? I often find myself in a situation that a certain function is called several times. On the first call its output argument is deallocated and the function performs the allocation. In the subsequent calls the argument is already allocated and no time is wasted on repetitive allocations. Now, according to the Fortran standard, any allocatable output argument becomes deallocated upon the function call. Hence, if I want to avoid unnecessary allocations I declare the argument to be "in out" which does not reflect its actual status. So, can somebody explain me the reason to deallocate output argument? Thank you.
From: Eli Osherovich on 10 Jul 2010 15:31 On Jul 10, 10:26 pm, Eli Osherovich <eli.osherov...(a)gmail.com> wrote: > Can somebody explain me the reason(s) to deallocate a procedure's > arguments whose intent is "out"? > > I often find myself in a situation that a certain function is called > several times. On the first call its output argument is deallocated > and the function performs the allocation. In the subsequent calls the > argument is already allocated and no time is wasted on repetitive > allocations. Now, according to the Fortran standard, any allocatable > output argument becomes deallocated upon the function call. Hence, if > I want to avoid unnecessary allocations I declare the argument to be > "in out" which does not reflect its actual status. > > So, can somebody explain me the reason to deallocate output argument? > > Thank you. A small correction. It should be: .... on the first call its output array is NOT ALLOCATED...
From: Richard Maine on 10 Jul 2010 16:29 Eli Osherovich <eli.osherovich(a)gmail.com> wrote: > Now, according to the Fortran standard, any allocatable > output argument becomes deallocated upon the function call. Hence, if > I want to avoid unnecessary allocations I declare the argument to be > "in out" which does not reflect its actual status. > > So, can somebody explain me the reason to deallocate output argument? Because that's what intent(out) means - that no information comes in from the argument. It is only used to send information out. What you are missing is that the size is part of the information for an allocatable array. You are probably just thinking that only the values in the array count, but the size is information as well. I'll try nother way to explain it as well - really the same point, but just looked at from a slightly different angle. For an intent(out) argument, you aren't supposed to have to worry about the status of the argument when you call the procedure because any prior value does not make any difference. But if an intent(out) allocatable were not deallocated on entry, then its prior status would affect things. That's not like intent(out). It is like intent(inout). In a way, it is much like having a (non-allocatable) array argument where you want to define the value of one element within the subroutine. You might think of that as being appropriate for intent(out) because all you are doing is seding out the value of that element. But the intent attribute applies to the whole argument - not just to some part of it. If you want the other elements of the array to remain unchanged, you need intent(inout) instead of intent(out). If you use intent(out) all the other element values are lost in entry. Likewise, if you want the size of an allocatable array to remain unchanged, you need intent(inout) or else the size is lost on entry. I'd say they were very parallel situations. As an aside, if your array is to stay the same size for all calls, one alternative to avoid the repetitive allocations might be to allocate the array outside of the subroutine before the first call. Then you can just make the dummy argument an assumed shape non-allocatable. I don't know whether that would fit with your code or not; just mentioing the possibility. -- Richard Maine | Good judgment comes from experience; email: last name at domain . net | experience comes from bad judgment. domain: summertriangle | -- Mark Twain
From: Eli Osherovich on 11 Jul 2010 02:45 On Jul 10, 11:29 pm, nos...(a)see.signature (Richard Maine) wrote: > Eli Osherovich <eli.osherov...(a)gmail.com> wrote: > > Now, according to the Fortran standard, any allocatable > > output argument becomes deallocated upon the function call. Hence, if > > I want to avoid unnecessary allocations I declare the argument to be > > "in out" which does not reflect its actual status. > > > So, can somebody explain me the reason to deallocate output argument? > > Because that's what intent(out) means - that no information comes in > from the argument. It is only used to send information out. > > What you are missing is that the size is part of the information for an > allocatable array. You are probably just thinking that only the values > in the array count, but the size is information as well. > > I'll try nother way to explain it as well - really the same point, but > just looked at from a slightly different angle. > > For an intent(out) argument, you aren't supposed to have to worry about > the status of the argument when you call the procedure because any prior > value does not make any difference. But if an intent(out) allocatable > were not deallocated on entry, then its prior status would affect > things. That's not like intent(out). It is like intent(inout). > > In a way, it is much like having a (non-allocatable) array argument > where you want to define the value of one element within the subroutine. > You might think of that as being appropriate for intent(out) because all > you are doing is seding out the value of that element. But the intent > attribute applies to the whole argument - not just to some part of it. > If you want the other elements of the array to remain unchanged, you > need intent(inout) instead of intent(out). If you use intent(out) all > the other element values are lost in entry. Likewise, if you want the > size of an allocatable array to remain unchanged, you need intent(inout) > or else the size is lost on entry. I'd say they were very parallel > situations. > > As an aside, if your array is to stay the same size for all calls, one > alternative to avoid the repetitive allocations might be to allocate the > array outside of the subroutine before the first call. Then you can just > make the dummy argument an assumed shape non-allocatable. I don't know > whether that would fit with your code or not; just mentioing the > possibility. > > -- > Richard Maine | Good judgment comes from experience; > email: last name at domain . net | experience comes from bad judgment. > domain: summertriangle | -- Mark Twain Richard, Thank you very much for your explanation. Not it makes more sense,
From: Eli Osherovich on 11 Jul 2010 02:46
On Jul 10, 11:29 pm, nos...(a)see.signature (Richard Maine) wrote: > Eli Osherovich <eli.osherov...(a)gmail.com> wrote: > > Now, according to the Fortran standard, any allocatable > > output argument becomes deallocated upon the function call. Hence, if > > I want to avoid unnecessary allocations I declare the argument to be > > "in out" which does not reflect its actual status. > > > So, can somebody explain me the reason to deallocate output argument? > > Because that's what intent(out) means - that no information comes in > from the argument. It is only used to send information out. > > What you are missing is that the size is part of the information for an > allocatable array. You are probably just thinking that only the values > in the array count, but the size is information as well. > > I'll try nother way to explain it as well - really the same point, but > just looked at from a slightly different angle. > > For an intent(out) argument, you aren't supposed to have to worry about > the status of the argument when you call the procedure because any prior > value does not make any difference. But if an intent(out) allocatable > were not deallocated on entry, then its prior status would affect > things. That's not like intent(out). It is like intent(inout). > > In a way, it is much like having a (non-allocatable) array argument > where you want to define the value of one element within the subroutine. > You might think of that as being appropriate for intent(out) because all > you are doing is seding out the value of that element. But the intent > attribute applies to the whole argument - not just to some part of it. > If you want the other elements of the array to remain unchanged, you > need intent(inout) instead of intent(out). If you use intent(out) all > the other element values are lost in entry. Likewise, if you want the > size of an allocatable array to remain unchanged, you need intent(inout) > or else the size is lost on entry. I'd say they were very parallel > situations. > > As an aside, if your array is to stay the same size for all calls, one > alternative to avoid the repetitive allocations might be to allocate the > array outside of the subroutine before the first call. Then you can just > make the dummy argument an assumed shape non-allocatable. I don't know > whether that would fit with your code or not; just mentioing the > possibility. > > -- > Richard Maine | Good judgment comes from experience; > email: last name at domain . net | experience comes from bad judgment. > domain: summertriangle | -- Mark Twain Richard, Thank you very much for your explanation. Now it makes more sense. |