473,406 Members | 2,312 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,406 software developers and data experts.

Is metaprogramming suitable for iterative method

Hi there,
I am working a project in numerical computation in which iterative
method is applied for solving equation. The problem is so big and slow.
Few days ago, I found a paper on metaprogramming , it seems that such
technique has good performance for some case. For ordinary case, my
code for implementing iterative method like

for (int i=1; i<N; i++)
{
x[i] = f(x[i], x[i-1]);
}

where f(_, _) represents a function of iterative expression

I try to write a metaprogramming verion like

template <int size>
inline void meta_iter(double *x)
{
*x = f(*x, *(x-1));
metaThomas<size-1>(x+1);
}

template<> inline void meta_iter(double *x) {}

However, the performance of the metaprogramming version is not better
than ordinary version. In contrast, the former is slower than the
later!!!

Oct 26 '05 #1
3 1411
wa***@wakun.com wrote:
Hi there,
I am working a project in numerical computation in which iterative
method is applied for solving equation. The problem is so big and slow.
Few days ago, I found a paper on metaprogramming , it seems that such
technique has good performance for some case. For ordinary case, my
code for implementing iterative method like

for (int i=1; i<N; i++)
{
x[i] = f(x[i], x[i-1]);
}

where f(_, _) represents a function of iterative expression

I try to write a metaprogramming verion like

template <int size>
inline void meta_iter(double *x)
{
*x = f(*x, *(x-1));
metaThomas<size-1>(x+1);
}

template<> inline void meta_iter(double *x) {}

However, the performance of the metaprogramming version is not better
than ordinary version. In contrast, the former is slower than the
later!!!


Just a wild guess: for large values of N, you might unroll more of the loop
than is good for you. The code for the loop needs to be fetched from
memory. For a large body of code that is going to trigger cache misses.
Your compiler probably knows better how to avoid these than the template.
Best

Kai-Uwe Bux
Oct 26 '05 #2
Kai-Uwe Bux wrote:
wa***@wakun.com wrote:

Hi there,
I am working a project in numerical computation in which iterative
method is applied for solving equation. The problem is so big and slow.
Few days ago, I found a paper on metaprogramming , it seems that such
technique has good performance for some case. For ordinary case, my
code for implementing iterative method like

for (int i=1; i<N; i++)
{
x[i] = f(x[i], x[i-1]);
}

where f(_, _) represents a function of iterative expression

I try to write a metaprogramming verion like

template <int size>
inline void meta_iter(double *x)
{
*x = f(*x, *(x-1));
metaThomas<size-1>(x+1);
}

template<> inline void meta_iter(double *x) {}

However, the performance of the metaprogramming version is not better
than ordinary version. In contrast, the former is slower than the
later!!!

Just a wild guess: for large values of N, you might unroll more of the loop
than is good for you. The code for the loop needs to be fetched from
memory. For a large body of code that is going to trigger cache misses.
Your compiler probably knows better how to avoid these than the template.
Best

Kai-Uwe Bux

Ignoring catastrophic cache missing, is there ever going to be a case
where you get massive, giant, life-and-death speed gains from this sort
of an optimazation? I've always viewed this as belonging to the class
of witch-doctoring optimizations, things which may or may not improve
your speed by a reasonably small fraction depending on the specifics.
[As opposed to optimizations which reduce the complexity of your code,
and thus almost-always give wild, huge gains.]
Oct 26 '05 #3
wa***@wakun.com wrote:
Hi there,
I am working a project in numerical computation in which iterative
method is applied for solving equation. The problem is so big and slow.
Few days ago, I found a paper on metaprogramming , it seems that such
technique has good performance for some case. For ordinary case, my
code for implementing iterative method like

for (int i=1; i<N; i++)
{
x[i] = f(x[i], x[i-1]);
}

where f(_, _) represents a function of iterative expression

I try to write a metaprogramming verion like

template <int size>
inline void meta_iter(double *x)
{
*x = f(*x, *(x-1));
metaThomas<size-1>(x+1);
}

template<> inline void meta_iter(double *x) {}

However, the performance of the metaprogramming version is not better
than ordinary version. In contrast, the former is slower than the
later!!!


Let's examine the current routine:

for (int i=1; i<N; i++)
{
x[i] = f(x[i], x[i-1]);
}

There are few changes that could be made to this routine that would
probably speed it up, and none of them necessarily require templates.

First, I would note that x[i] is calculated twice in one statement.
Second, x[i-1] is calculated even though the previous iteration had
just stored the value at that location. Eliminating both redundant and
unnecessary memory accesses as well as unrolling the loop should
improve performance:

double *index = &x[1];
double value = x[0];

for (int i = 0; i < N/4; i++)
{
value = f(*index, value);
*(++index) = value;
value = f(*index, value);
*(++index) = value;
value = f(*index, value);
*(++index) = value;
value = f(*index, value);
*(++index) = value;
}

for (int i = 0; i < N%4; i++)
{
value = f(*index, value);
*(++index) = value;
}

Greg

Oct 27 '05 #4

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

Similar topics

12
by: Dave | last post by:
Would people agree with the statement that to a large degree, using template metaprogramming techniques turns a C++ compiler into a C++ interpreter (but just for the metaprogrammed portions of the...
9
by: Rock Johnson | last post by:
I understand that template metaprogramming is a technique that allows for calcualations to occur at compile-time rather than run-time. Can someone explain what is the benefit of this, and when it...
21
by: Protoman | last post by:
I've been looking at template metaprogramming. It seems really cool, make the compiler do most of the work. I have very simple program that uses TMP,it calculates the square of a number, but it...
9
by: PengYu.UT | last post by:
Hi, I have the code below this email. I want to replace the last 4 lines with a Metaprogramming loop to get something like the following (I don't know the syntax). Is it possible? for type in...
7
by: Joe | last post by:
Hi, I found a concept named template metaprogramming that can be used in C+ + code at compile-time. I am a beginner at C++. But I am a programmer on the .NET platform. Do you know if template...
5
by: iapx86 | last post by:
My parser project calls for a computed goto (see code below). The C preprocessor delivers the desired result, but is ugly. Template metaprogramming delivers results I do not understand. Can...
9
by: andrew cooke | last post by:
Hi, Thanks for the help a couple of days ago. I completed what I was doing and wrote a summary which I've posted at http://acooke.org/cute/PythonMeta0.html (it's kind of long to post here). I...
16
by: Wilson | last post by:
Hi all, I have an interesting problem that I'm hoping can be solved with metaprogramming, but I don't know how far Python supports code generation (and I don't know if I'm taking the correct...
12
by: nooneinparticular314159 | last post by:
Hello. If I declare the following: template<int a, int b, int SomeArray> class DoSomething{ public: .. .. ..
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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...
0
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...

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.