comp.lang.ada
 help / color / mirror / Atom feed
From: Ole-Hjalmar Kristensen <ole-hjalmar.kristensen@substitute_employer_here.com>
Subject: Re: Copying rows in a two dimensional array.
Date: Mon, 22 Mar 2010 09:56:07 +0100
Date: 2010-03-22T09:56:07+01:00	[thread overview]
Message-ID: <wvbrfx3sg2fs.fsf@substitute_employer_here.com> (raw)
In-Reply-To: 44a00a9c-da00-42c3-90fa-ab6f3c4237b8@w31g2000yqk.googlegroups.com

jonathan <johnscpg@googlemail.com> writes:

<snip>
> It's the heart of the matter, and it is just getting worse.  Helps
> convince me
> anyway that I did not waste time on an unimportant matter! In
> numerical linear
> algebra the usual solution is to restructure matrices as a collection
> of
> blocks.  That has a few of its own problems though. Minor footnote: I
> did

Another approach is to keep the matrix as a single big matrix, but
reformulate your loops as recursive procedure which divides the matrix
a number of times and then loops at the lowest level. The increased
efficiency from tha cache will typically more than outweigh the
overhead from the recursion.

The following is a java version of the idea applied to transposing a
lagre matrix. (I have Ada and C versions as well) The recursive
version is typically about ten times faster than the pure nested loop
version. It is also interesting to see the speed of tha java version
relative to a C or Ada version. Try it and see...


import java.util.*;

class transpose
{
    private static final int u1 = 6000;
    private static final int u2 = 6000;
    private static int[][] a = new int[u1][u2];
    private static int[][] b = new int[u1][u2];

    private static void init(int[][]x)
    {
        int i = 0;
        int j = 0;
        for (i = 0; i < x.length; i++) {
            for (j = 0; j < x[i].length; j++) {
                x[i][j] = i; 
            }
        }
    }

    private static void loop_transpose(int[][]a, int[][]b, int ll1, int ul1, int ll2, int ul2)
    {
        int i = 0;
        int j = 0;
        for (i = ll1; i <= ul1; i++) {
            for (j = ll2; j <= ul2; j++) {
                b[j][i] = a[i][j]; 
            }
        }
    }

    private static void recursive_transpose(int[][]a, int[][]b, int ll1, int ul1, int ll2, int ul2, int cutoff) 
    {
        int split = 0;
        if (ul1 - ll1 > cutoff || ul2 - ll2 > cutoff) {
            if (ul1 - ll1 > ul2 - ll2 ) {
                split = (ul1 - ll1)/2;
                recursive_transpose(a,b,ll1,ll1+split,ll2,ul2,cutoff);
                recursive_transpose(a,b,ll1+1+split,ul1,ll2,ul2,cutoff);
            }else{
                split = (ul2 - ll2)/2;
                recursive_transpose(a,b,ll1,ul1,ll2,ll2+split,cutoff);
                recursive_transpose(a,b,ll1,ul1,ll2+1+split,ul2,cutoff);
            }
        }else {
            loop_transpose(a,b,ll1,ul1,ll2,ul2);
        }
    }

    public static void main(String[] args)
    {
        init(a); init(b);

        long start = (new Date()).getTime();
        loop_transpose(a,b,0,u1-1,0,u2-1);

        long stop = (new Date()).getTime();
        System.out.println( "loop " + (stop-start)); 

        start = (new Date()).getTime();
        recursive_transpose(a,b,0,u1-1,0,u2-1,16);

        stop = (new Date()).getTime();
        System.out.println( "recursive " + (stop-start)); 
    }
}
 
-- 
   C++: The power, elegance and simplicity of a hand grenade.



  reply	other threads:[~2010-03-22  8:56 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-02-01  2:11 Copying rows in a two dimensional array Peter C. Chapin
2010-02-01  4:42 ` Jeffrey R. Carter
2010-02-01  6:55 ` Niklas Holsti
2010-02-01 23:36   ` Peter C. Chapin
2010-02-04  4:27   ` Hibou57 (Yannick Duchêne)
2010-02-01  8:37 ` Dmitry A. Kazakov
2010-02-02  0:11   ` Randy Brukardt
2010-02-07 16:13     ` Robert A Duff
2010-02-08  6:30       ` tmoran
2010-02-08 13:15         ` Robert A Duff
2010-02-08 13:45           ` Dmitry A. Kazakov
2010-02-08 21:20             ` Robert A Duff
2010-02-08 23:26               ` (see below)
2010-02-09  0:36                 ` Randy Brukardt
2010-02-09  1:03                   ` (see below)
2010-02-09  7:11                   ` Pascal Obry
2010-02-09  8:14                     ` AdaMagica
2010-02-09 14:33                 ` Robert A Duff
2010-02-09  1:05               ` Adam Beneschan
2010-02-09 14:45                 ` Robert A Duff
2010-02-09 18:50                   ` tmoran
2010-02-09 19:51                   ` Pascal Obry
2010-02-09 23:03                     ` Robert A Duff
2010-02-08 18:53           ` tmoran
2010-02-08 21:14             ` Robert A Duff
2010-02-08 21:29               ` Pascal Obry
2010-02-09  8:56                 ` Jean-Pierre Rosen
2010-02-09  9:14                   ` AdaMagica
2010-02-09 11:19                     ` Jean-Pierre Rosen
2010-02-09 14:26                 ` Robert A Duff
2010-02-09  6:34               ` tmoran
2010-02-09 14:29                 ` Robert A Duff
2010-02-09 18:49                   ` tmoran
2010-02-09 22:58                     ` Robert A Duff
2010-02-01 22:10 ` Jerry
2010-02-02  0:07   ` Randy Brukardt
2010-02-02  8:52   ` Jean-Pierre Rosen
2010-02-02 22:23     ` Jerry
2010-02-03  1:24       ` Adam Beneschan
2010-02-04  4:42     ` Hibou57 (Yannick Duchêne)
2010-02-14  0:42     ` jonathan
2010-02-14  1:54       ` Hibou57 (Yannick Duchêne)
2010-02-14 16:16         ` jonathan
2010-03-22  8:56           ` Ole-Hjalmar Kristensen [this message]
2010-02-16  6:51     ` David Thompson
2010-02-04  4:13 ` Hibou57 (Yannick Duchêne)
2010-02-04  9:10   ` Dmitry A. Kazakov
2010-02-04  9:23     ` Hibou57 (Yannick Duchêne)
replies disabled

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