comp.lang.ada
 help / color / mirror / Atom feed
* efficient vector/matrix operations in Fortran, was Re: ... in Ada
  2001-08-14  4:56 efficient vector/matrix operations " Russ
@ 2001-08-14  7:32 ` tmoran
  0 siblings, 0 replies; 3+ messages in thread
From: tmoran @ 2001-08-14  7:32 UTC (permalink / raw)


>but not have the operators one really needs for efficient code?
  The existence of Fortran, which has long been used successfully
for highly efficient vector/matrix code, demonstrates that += is
not required.
  One possible reason is that significant matrix/vector programs
don't normally spend a lot of their time doing a += b type O(n**2)
operations, but rather spend their time doing O(n**3) operations.
+= would be of minor benefit in that case.
---------------------------------
"Premature optimization is the root of all evil"  Knuth (?)



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: efficient vector/matrix operations in Fortran, was Re: ... in Ada
@ 2001-08-14 10:11 Gautier Write-only-address
  2001-08-14 10:37 ` Lutz Donnerhacke
  0 siblings, 1 reply; 3+ messages in thread
From: Gautier Write-only-address @ 2001-08-14 10:11 UTC (permalink / raw)
  To: comp.lang.ada

>   The existence of Fortran, which has long been used successfully
>for highly efficient vector/matrix code, demonstrates that += is
>not required.
>   One possible reason is that significant matrix/vector programs
>don't normally spend a lot of their time doing a += b type O(n**2)
>operations, but rather spend their time doing O(n**3) operations.
>+= would be of minor benefit in that case.

The real reason is the "+" operator is defined in the language
for vectors or matrices, not a programmable function like in Ada.
Or, say, it has a pragma Import(intrinsic,...) like for functions
in Standard or Interfaces.
So the Fortran compiler optimizes with full latitude.
With a <<function "+"(a,b:vector) return vector>>, and no
trick to tell the compiler that it is intrisic,
you have a local variable in your function, perform the addition,
and return the variable. Maybe a pragma Inline and a strong
optimization can do the job on some compilers. It would be
interesting to see if at least one is able to suppress the
local variable in an inlined call. If not, there is no chance
that a user-defined "+" is as efficient as a user-defined "+=".

________________________________________________________
Gautier  --  http://www.mysunrise.ch/users/gdm/gsoft.htm

NB: Do not answer to sender address, visit the Web site!
    Ne r�pondez pas � l'exp�diteur, visitez le site ouaibe!


_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: efficient vector/matrix operations in Fortran, was Re: ... in Ada
  2001-08-14 10:11 efficient vector/matrix operations in Fortran, was Re: ... in Ada Gautier Write-only-address
@ 2001-08-14 10:37 ` Lutz Donnerhacke
  0 siblings, 0 replies; 3+ messages in thread
From: Lutz Donnerhacke @ 2001-08-14 10:37 UTC (permalink / raw)


* Gautier Write-only-address wrote:
>and return the variable. Maybe a pragma Inline and a strong
>optimization can do the job on some compilers. It would be
>interesting to see if at least one is able to suppress the
>local variable in an inlined call. If not, there is no chance
>that a user-defined "+" is as efficient as a user-defined "+=".

GNAT does a pretty bad job on this subject. Using the "+=" Procedure is
works considerably better. Please play around with compiler flags.

OTOH it is possible to enhance the Kernel Interface vastly if you are
switching to direct syscalls (using System.Machine_Code). Inlining does a
pretty good job on this subject:

\f
with Kernel.Calls;
use Kernel.Calls;

procedure test_kernel is
   res : long := sys_kill (0, 0);
begin
   null;
end test_kernel;
\f
_ada_test_kernel:
	movb $37,%al
	xorl %ebx,%ebx
	movl %ebx,%ecx
	int $0x80
	ret
\f
with Ada.Text_IO;

procedure t is
   type Vector is array (Positive range <>) of Integer;

   function "+" (a, b : Vector) return Vector is
      c : Vector(a'Range);
   begin
      for i in a'Range loop
         c(i) := a(i) + b(i);
      end loop;
      return c;
   end "+";

   procedure Add_To (a : in out Vector; b : in Vector) is
   begin
      for i in a'Range loop
         a(i) := a(i) + b(i);
      end loop;
   end Add_To;
   pragma Inline("+", Add_To);
   
   procedure Print (a : in Vector) is
      package IIO is new Ada.Text_IO.Integer_IO (Integer);
      use IIO, Ada.Text_IO;
   begin   
      for i in a'Range loop
         Put (i);
         Put (a (i));
      end loop;
      New_Line;
   end Print;
   
   a : Vector(1..3) := (others => 3);
   b : Vector(1..3) := (1, 2, 3);
   c : Vector(1..3) := (4, 5, 6);
   d : Vector(1..3);
begin
   d := a + b + c;
   Print(d);
   d := a; Add_To (d, b); Add_To (d, c);
   Print(d);
end t;



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2001-08-14 10:37 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-08-14 10:11 efficient vector/matrix operations in Fortran, was Re: ... in Ada Gautier Write-only-address
2001-08-14 10:37 ` Lutz Donnerhacke
  -- strict thread matches above, loose matches on Subject: below --
2001-08-14  4:56 efficient vector/matrix operations " Russ
2001-08-14  7:32 ` efficient vector/matrix operations in Fortran, was Re: ... " tmoran

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox