Solved

dividing matrix into small blocks

Posted on 2003-12-09
4
331 Views
Last Modified: 2010-04-15

hi i want to divide a matrix of 512 x 512 dimension into small matrices of 8 x 8  blocks . how can i do it???
thanks
0
Comment
Question by:sanko50
  • 2
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
skypalae earned 30 total points
ID: 9910487
are you kidding that you can't do it yourself? ok, ok .. here's some code. since you haven't specified any details i don't care about indexes. if they don't fit you, change the code. but you get the idea.



int i, j, k, l ; // just indexes
int old_matrix [512][512] ;
int new_matrix [64][64][8][8] ; // there will be 64x64 array of 8x8 matrices

// one possibility .... do it the hard way
for (i=0; i<64; i++)
    for (j=0; j<64; j++)
        for (k=0; k<8; k++)
            for (l=0; l<8; l++)
                new_matrix [i][j][k][l] = old_matrix [(i*8)+k][(j*8)+l] ; // you can replace the (i*8) by (i<<3) and same with j. should be little faster

// second possibility .. bit faster i think. but the indexes could be damaged. you have to check yourself
memcpy (new_matrix, old_matrix, 512*512*sizeof(int)) ;

S.
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 9911764
> you can replace the (i*8) by (i<<3) and same with j. should be little faster

Most compilers will generate the same code in both cases. I'd stick with the multiplication, because it is easier to read.
0
 
LVL 4

Expert Comment

by:skypalae
ID: 9911878
Yes, rstaveley, youre right. I use VC++ and i thought that compiling it in 'debug' mode without any optimizations would make different code, but it surprisingly created exactly same thing.

Anyway, have you checked the difference between (i*9) and ((i<<3)+i) ?? The first thing compiles to 'imul ecx,ecx,9' which i would expect, but the second one compiles into:

mov         edx,dword ptr [ebp-4]
mov         eax,dword ptr [ebp-4]
lea         ecx,[eax+edx*8]

which is completely different from what i've expected (well, not completely, but almost completely). I've done some research on different mlutipliers, checked the assembler code. It really looked interesting.

And what's the point? That when i tried to compile it in 'release' mode using all optimizations the resulting assembler code was exactly the same. So don't use any shifting or other'shortcuts', because the compiler will recognize it and compile the code its own way no matter how you try to fool it.

S.
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 9912247
If I optimise ((i<<3)+i) on VC7.1 with /Ox, I get...

      lea      eax, DWORD PTR [eax+eax*8]

...which makes more sense, because you get the whole shooting match in one 80386 instruction. Isn't it funny, though, to see it using a multiplication instead of the well-intended shift? The main trouble with attempting to help the compiler, is that it is tempting to think that we are still writing code for the 8086 instruction set :-)
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question