473,657 Members | 2,624 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2157

"chella mani" <ch********@gma il.com> wrote in message
news:90******** *************** ***@posting.goo gle.com...
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*****@nospam computer.org> wrote:

"chella mani" <ch********@gma il.com> wrote in message
news:90******** *************** ***@posting.goo gle.com...
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********@gmai l.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.almeid a@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(UNICAM P).
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
1995
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 main features of modules, namespaces, and User-Defined types. The text above was copied from page 31. --------------------------------------------------------
138
5214
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 array(i.e)it will have the address of the first element.In the the program below am not able to increment the value stored in x,which is the address of the first element.Why am I not able to do that?Afterall 1 is also a hexadecimal number then...
4
2692
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 is right node ‘weights' lesser than the left one? Why the trees are implemented in that way? Can any body clarify? Thanks in advance
20
1646
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 main() { int a = 1; int *p = &a; p++; printf("%d",*p);
3
1336
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 software in place. Now if I login at one place, can I be treated as logged in at other place also or it will again give me a login screen. Form Authentication : Is it internally uses cookies?
77
3666
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
2096
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
4238
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
1711
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="">, etc... How can it be done on RadioButtonList server component on asp.net 2.0 C# VS 2005 ? Thanks!
0
8427
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8330
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8850
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8523
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8626
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4334
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2749
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.