473,387 Members | 1,897 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Doubt-Efficiecy of 3D arrays

hi all,

I am developing a tool for scientific application which must be time
and memory efficient,my tool uses 1D,2D and 3D arrays. Is it a good
practice to use 3D arrays in applications or I should use convert my
3D arrays to 1D arrays for efficiency.
Thanks in advance,
Chella mani
Nov 14 '05 #1
5 2147

"chella mani" <ch********@gmail.com> wrote in message
news:90**************************@posting.google.c om...
hi all,

I am developing a tool for scientific application which must be time
and memory efficient,my tool uses 1D,2D and 3D arrays. Is it a good
practice to use 3D arrays in applications or I should use convert my
3D arrays to 1D arrays for efficiency.

If you can avoid repeated non-sequential access to your data by copying them
over to another array, that should improve efficiency. For example, matrix
multiply should go faster if one of the operands is transposed, in order to
use dot products with stride 1. It needn't be written as a 1D array.
Making your program obscure could ruin the efficiency of your development.
Nov 14 '05 #2
On Mon, 20 Sep 2004 06:34:03 GMT
"Tim Prince" <tp*****@nospamcomputer.org> wrote:

"chella mani" <ch********@gmail.com> wrote in message
news:90**************************@posting.google.c om...
hi all,

I am developing a tool for scientific application which must be time
and memory efficient,my tool uses 1D,2D and 3D arrays. Is it a good
practice to use 3D arrays in applications or I should use convert my
3D arrays to 1D arrays for efficiency. If you can avoid repeated non-sequential access to your data by
copying them over to another array, that should improve efficiency.


Actually, it is very dependant on the compiler and processor. A compiler
is likely to convert a 3D to a calculated pointer access. Then it can do
strength reduction of multiplication to repeated addition if you are
looping over the array (and yes, I have seen compiler documentation
listing this as one of the optimisations) and so you are down to what
you would have had if you had implemented it as a 1D array.
For example, matrix multiply should go faster if one of the operands
is transposed, in order to use dot products with stride 1. It needn't
be written as a 1D array.
Making your program obscure could ruin the efficiency of your
development.


Agreed. Write your code to be understandable otherwise it will be hell
trying to get something that works.

For something like this I would first worry about writing an algorithms
document detailing all the algorithms and the optimisations to the
algorithms. The only low level optimisation I would do is making sure
that for nested loops to scan the 2D and 3D arrays are ordered such that
it is just stepping through memory where possible. I.e. (if I'm not
going mad)

double a[5][5][5];
int x,y,z;
for (x=0; x<5; x++) {
for (y=0; y<5; y++) {
for (z=0; z<5; z++) {
/* Do stuff with a[x][y][z] */
}
}
}

However, what's fastest for your particular implementation is OT here.
--
Flash Gordon
Sometimes I think shooting would be far too good for some people.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #3
ch********@gmail.com (chella mani) wrote:
# hi all,
#
# I am developing a tool for scientific application which must be time
# and memory efficient,my tool uses 1D,2D and 3D arrays. Is it a good
# practice to use 3D arrays in applications or I should use convert my
# 3D arrays to 1D arrays for efficiency.

Whether you use A[i][j][k] or A[i*m*n+j*n+k], you're still using three dimensional
arrays. If your model doesn't permit abstraction to higher order features, but must
be some variant if three dimensional celluar automata, then it is what it is. Then the
best you can get is to access the elements to minimise page faults and cache trashing.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
There are subtler ways of badgering a witness.
Nov 14 '05 #4
chella mani wrote:
hi all,

I am developing a tool for scientific application which must be time
and memory efficient,my tool uses 1D,2D and 3D arrays. Is it a good
practice to use 3D arrays in applications or I should use convert my
3D arrays to 1D arrays for efficiency.
Thanks in advance,
Chella mani

make your code readable, and let the optimization to the compiler, if
you REALLY want optimization, then use assembly, but even in assembly it
is very difficult to make it faster than the compiled code.

--
Felipe Magno de Almeida
UIN: 2113442
email: felipe.almeida@ic unicamp br, felipe.m.almeida@gmail com
I am a C, modern C++, MFC, ODBC, Windows Services, MAPI developer
from synergy, and Computer Science student from State
University of Campinas(UNICAMP).
To know more about:
Unicamp: http://www.ic.unicamp.br
Synergy: http://www.synergy.com.br
current work: http://www.mintercept.com
Nov 14 '05 #5
chella mani wrote:
Is it a good practice to use 3D arrays in applications or I should use convert my
3D arrays to 1D arrays for efficiency.


On platforms with a data cache, it may be benficial to lay out your
array elements such that your loops traverse the elements in linear
order in memory, or at least do so more often. You may also want to
produce similar versions of a function to operate on both dynamic and
static arrays. If this is likely to be a concern, my personal
recommendation is this, supposing your array has dimensions m, n, p:
* define, dynamically allocate, or pass in a 1D array of size m*n*p
holding the elements
* define a "local" macro specific to that array taking three arguments
which performs the address calculation in the standard way (by "local"
macro, I mean #undef it at the end of the function, to avoid name
collisions later)
* get the algorithm working
* first, try rearranging loops to get better cache performance
* if this fails, fiddle with the macros until you get good performance

Here's an example of 2-operand destructive matrix addition with
fixed-size matrices:

#define M 20
#define N 50
#define P 100

void matrixAdd(int* arg_a, int* arg_b) {
#define a(i,j,k) (arg_a[(i)*N*P + (j)*P + (k)])
#define b(i,j,k) (arg_b[(i)*N*P + (j)*P + (k)])
int i,j,k;
for(i=0; i<M; i++)
for(j=0; j<N; j++)
for(k=0; k<P; k++)
a(i,j,k) += b(i,j,k);
#undef b
#undef a
}

Now with dynamic-sized:

void matrixAdd(int* arg_a, int* arg_b, int m, int n, int p) {
#define a(i,j,k) (arg_a[(i)*n*p + (j)*p + (k)])
#define b(i,j,k) (arg_b[(i)*n*p + (j)*p + (k)])
int i,j,k;
for(i=0; i<m; i++)
for(j=0; j<n; j++)
for(k=0; k<p; k++)
a(i,j,k) += b(i,j,k);
#undef b
#undef a
}

As macros go these are pretty safe - no reevaluation. Just watch out for
macro redefinitions and make sure your address calculations are actually
one-to-one.
--
Derrick Coetzee
I grant this newsgroup posting into the public domain. I disclaim all
express or implied warranty and all liability. I am not a professional.
Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Guilherme Pinto | last post by:
Hello. I am reading the book written by Bjarne Stroustrup called " The C++ Programming Language - Special Edition" and had a doubt which a think is really important to distinguish between the...
138
by: ambika | last post by:
Hello, Am not very good with pointers in C,but I have a small doubt about the way these pointers work.. We all know that in an array say x,x is gonna point to the first element in that...
4
by: dam_fool_2003 | last post by:
I am just a beginner in tree data – struct. I have this little doubt. Left node ‘weights' lesser than the right one. I have seen, so far it is algorithm implementations. But why not vice-versa that...
20
by: maadhuu | last post by:
firstly, i am thankful to all those who answered the 1st set of doubts. And i am not yet enlightened to that extent , coz ' i keep getting doubts. is the following defined in the language ?? int...
3
by: SMG | last post by:
Hi All, It might be a silly doubt, but it is a doubt.... I am using form authentication for my website, now my web application is gonna be deployed on two web servers with Load Balancing...
77
by: muttaa | last post by:
Hello all, My doubt is going to be so primitive that i ask you all to forgive me beforehand.... Here's the code snippet: int main() { int x=5;
11
by: Bob Nelson | last post by:
I don't remember seeing the term ``doubt'' used much in c.l.c. back in the 90's. When did this word become nearly synonymous with ``question'' or ``query'' and does it have static duration?
122
by: ivan | last post by:
hi all, if I have: if(A && B || C) which operation gets executed first? If I remeber well should be &&, am I correct? thanks
5
by: Paulo | last post by:
Hi, I have a RadioButtonList and I need to do some verifications on a "OnChange" event on client... because on classic asp/html I just add a "onChange" event on <input type="radio" onChange="">,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.