From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,bf03d731a6ef511f X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news2.google.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.sun.com!news.sun.com.POSTED!not-for-mail NNTP-Posting-Date: Mon, 22 Mar 2010 03:56:08 -0500 Newsgroups: comp.lang.ada Subject: Re: Copying rows in a two dimensional array. References: <4b6637a1$0$4586$4d3efbfe@news.sover.net> <44a00a9c-da00-42c3-90fa-ab6f3c4237b8@w31g2000yqk.googlegroups.com> From: Ole-Hjalmar Kristensen Organization: Sun Microsystems Date: Mon, 22 Mar 2010 09:56:07 +0100 Message-ID: User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.2 (usg-unix-v) Cancel-Lock: sha1:JnOAyxqwCefZpWjWhWZbLOjgwK8= MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Cache-Post-Path: news1nwk!unknown@khepri33.norway.sun.com X-Cache: nntpcache 3.0.1 (see http://www.nntpcache.org/) X-Usenet-Provider: http://www.giganews.com X-Trace: sv3-7OwXYmzfb9dCBQP3TBKuzBeuzXmbfKKR0XjyJ+NE/Zq/B73jMelI+CtR2RCrW1/UtMG9geL57yrAbMp!Uu83DMfRaK7Iw2RFsMIYY4SGcDhQ2JrGRACftuv2xjijHBTTz5DksTFKHjCpz9eM3cJAqNdeMxYF!EmNBMn1A X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 Xref: g2news2.google.com comp.lang.ada:10651 Date: 2010-03-22T09:56:07+01:00 List-Id: jonathan writes: > 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.