473,804 Members | 3,278 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Scripting and block data manipulation- how to preserve performance

I have an application that manipulates large arrays of image data of
various types, all the usual arithmetic operations on the data objects
are supported. With careful design and tricks similar to those in
UBLAS (http://www.boost.org/libs/numeric/ublas/doc/) I have been able
to avoid temporaries where appropriate and carefully optimize code
(C++) that evaluates expressions, for example

Image A, B, C, D, E
A=((B+C)/D)+E

In this expression A is evaluated directly by looping through the
elements of B,C,D,E.

This all works fine and dandy if the image expressions are known at
compile time, but when it comes to scripting at run-time the
opimizations are lost. The only technique that preserves efficiency is
to generate object code for the image expression at run-time. In other
words build a simple expression compiler into the application and have
it generate native assembler as needed.

Is there precedent for this type of approach? Comments, ideas and
pointers to existing implementations would be most welcome.
Jul 23 '05 #1
9 1789
Code4u wrote:
UBLAS (http://www.boost.org/libs/numeric/ublas/doc/) I have been able
to avoid temporaries where appropriate and carefully optimize code
(C++) that evaluates expressions, for example

Image A, B, C, D, E
A=((B+C)/D)+E

In this expression A is evaluated directly by looping through the
elements of B,C,D,E.

This all works fine and dandy if the image expressions are known at
compile time, but when it comes to scripting at run-time the
opimizations are lost. The only technique that preserves efficiency
is to generate object code for the image expression at run-time.


Not the only. Your scripting can do the same that the C++ compiler does,
interpret adequately the expression and generate calls to the compose
operator functions. The difference in speed from this approach to generated
machine code will be very small, and the complexity and portability much
better.

--
Salu2
Jul 23 '05 #2
On Sun, 17 Jul 2005 23:57:11 +0200, Julián Albo <JU********@ter ra.es>
wrote:
Code4u wrote:
UBLAS (http://www.boost.org/libs/numeric/ublas/doc/) I have been able
to avoid temporaries where appropriate and carefully optimize code
(C++) that evaluates expressions, for example

Image A, B, C, D, E
A=((B+C)/D)+E

In this expression A is evaluated directly by looping through the
elements of B,C,D,E.

This all works fine and dandy if the image expressions are known at
compile time, but when it comes to scripting at run-time the
opimizations are lost. The only technique that preserves efficiency
is to generate object code for the image expression at run-time.


Not the only. Your scripting can do the same that the C++ compiler does,
interpret adequately the expression and generate calls to the compose
operator functions. The difference in speed from this approach to generated
machine code will be very small, and the complexity and portability much
better.


If I understand you correctly (perhaps not), I don't see how this
would generate efficient code. It's pretty easy to generate a series
of operator calls at run-time but this will not result in the loop
unrolling that maximises efficiency. In the above example the
following operators would be called in sequence:

operator+
operator/
operator+

In each operator call a loop iterates over the scalar values. The
problem is it is much less efficient than a combined operation:

for (size_t i=0; i<scalarCount; ++i)
{
a[i]=((b[i]+c[i])/d[i])+e[i];
}

In case the reason is not apparent- the above code will make much
better use of CPU cache because of the higher locality of reference
and uses a trivial amount of temporary storage. When tested with the
Visual Studio 2003 compiler I measure an average slowdown of x2 for
the version that uses composition of operators.

But how do we generate the equivalent object code at run-time? One
approach is to build an expression compiler into the application,
generate the required assembler, and call it.
Jul 23 '05 #3
Code4u wrote:
This all works fine and dandy if the image expressions are known at
compile time, but when it comes to scripting at run-time the
opimizations are lost. The only technique that preserves efficiency
is to generate object code for the image expression at run-time.
Not the only. Your scripting can do the same that the C++ compiler does,
interpret adequately the expression and generate calls to the compose
operator functions. The difference in speed from this approach to
generated machine code will be very small, and the complexity and
portability much better.


If I understand you correctly (perhaps not), I don't see how this
would generate efficient code. It's pretty easy to generate a series
of operator calls at run-time but this will not result in the loop
unrolling that maximises efficiency. In the above example the
following operators would be called in sequence:

operator+
operator/
operator+

In each operator call a loop iterates over the scalar values. The
problem is it is much less efficient than a combined operation:

for (size_t i=0; i<scalarCount; ++i)
{
a[i]=((b[i]+c[i])/d[i])+e[i];
}


And that is that I say. Your scripting engine can analyze the sequence and
call the combined operation instead of each individual operation in
sequence.

The loop unrolling probably can't be done in the scripting without great
effort. But if the operators are costly (and if you are working with image
data I suppose they are) the benefits of the loop unrolling will be
minimal.

And if you want the maximum efficience at all cost... well, you can write
the entire application in hand-optimized assembler.
But how do we generate the equivalent object code at run-time? One
approach is to build an expression compiler into the application,
generate the required assembler, and call it.


Don't know how your existing scripting engine actually works. If it
generates a stack machine, for example, a simple check os sequences of
operations in the machine can probably do the work. If it is a hand-coded
parser can be harder.

--
Salu2
Jul 23 '05 #4
Me
Code4u wrote:
I have an application that manipulates large arrays of image data of
various types, all the usual arithmetic operations on the data objects
are supported. With careful design and tricks similar to those in
UBLAS (http://www.boost.org/libs/numeric/ublas/doc/) I have been able
to avoid temporaries where appropriate and carefully optimize code
(C++) that evaluates expressions, for example

Image A, B, C, D, E
A=((B+C)/D)+E

In this expression A is evaluated directly by looping through the
elements of B,C,D,E.

This all works fine and dandy if the image expressions are known at
compile time, but when it comes to scripting at run-time the
opimizations are lost. The only technique that preserves efficiency is
to generate object code for the image expression at run-time. In other
words build a simple expression compiler into the application and have
it generate native assembler as needed.

Is there precedent for this type of approach? Comments, ideas and
pointers to existing implementations would be most welcome.


I'd profile this to see if it is really necessary (I'm assuming you're
doing this to eliminate temporaries and not to increase precision). For
example, instead of trying to convert that expression to:

for (size_t i = 0; i < len; ++i)
A[i]=((B[i]+C[i])/D[i])+E[i];

somehow. Try converting it to:

for (size_t i = 0; i < len; ++i)
A[i] = B[i] + C[i];

for (size_t i = 0; i < len; ++i)
A[i] = A[i] / D[i];

for (size_t i = 0; i < len; ++i)
A[i] = A[i] + E[i];

which doesn't involve any runtime code generation at all.

Jul 23 '05 #5
On Mon, 18 Jul 2005 08:34:53 +0200, Julián Albo <JU********@ter ra.es>
wrote:
And that is that I say.
Huh?
Your scripting engine can analyze the sequence and
call the combined operation instead of each individual operation in
sequence.

The loop unrolling probably can't be done in the scripting without great
effort. But if the operators are costly (and if you are working with image
data I suppose they are) the benefits of the loop unrolling will be
minimal.

Actually it's not, the average is a 2x speedup. That's huge.
And if you want the maximum efficience at all cost... well, you can write
the entire application in hand-optimized assembler.


I think there's a disconnect here. You don't seem to understand the
issue at hand.
Jul 23 '05 #6
Code4u wrote:
I think there's a disconnect here. You don't seem to understand the
issue at hand.


Maybe, but it's also possible thay you don't undesrtand what I propose.

--
Salu2
Jul 23 '05 #7
On Mon, 18 Jul 2005 16:15:16 +0200, Julián Albo <JU********@ter ra.es>
wrote:
Code4u wrote:
I think there's a disconnect here. You don't seem to understand the
issue at hand.


Maybe, but it's also possible thay you don't undesrtand what I propose.


You proposed composing the expression at run-time, yes? Such
composition is what I'm trying to avoid because it does not make
efficient use of cache. If I'm wrong, please restate and help me
understand what you propose, simple source code would help.

Thanks.
Jul 23 '05 #8
Code4u wrote:
You proposed composing the expression at run-time, yes? Such
composition is what I'm trying to avoid because it does not make
efficient use of cache.


Then I misunderstand you, I thinked you were worried about the use of
individual operations instead of the composites.

--
Salu2
Jul 23 '05 #9
Code4u wrote:
I have an application that manipulates large arrays of image data of
various types, all the usual arithmetic operations on the data objects
are supported. With careful design and tricks similar to those in
UBLAS (http://www.boost.org/libs/numeric/ublas/doc/) I have been able
to avoid temporaries where appropriate and carefully optimize code
(C++) that evaluates expressions, for example

Image A, B, C, D, E
A=((B+C)/D)+E

In this expression A is evaluated directly by looping through the
elements of B,C,D,E.

This all works fine and dandy if the image expressions are known at
compile time, but when it comes to scripting at run-time the
opimizations are lost. The only technique that preserves efficiency is
to generate object code for the image expression at run-time. In other
words build a simple expression compiler into the application and have
it generate native assembler as needed.

Is there precedent for this type of approach? Comments, ideas and
pointers to existing implementations would be most welcome.


I imagine that Matlab would be quite good at this sort of thing. So try
to find out how it does it.
Jul 23 '05 #10

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

Similar topics

80
5294
by: Bibby | last post by:
Hi, I'm interested in getting started in the programming world. I've dabbled in C, C++ and VB6. Which would be the best language to focus my attention to regarding the following considerations: Hireability Portability Flexibility The likely candidates seem to be Java, VB.Net, C, C++, C#.
79
3066
by: Bibby | last post by:
Hi, I'm interested in getting started in the programming world. I've dabbled in C, C++ and VB6. Which would be the best language to focus my attention to regarding the following considerations: Hireability Portability Flexibility The likely candidates seem to be Java, VB.Net, C, C++, C#.
17
4208
by: Karl Irvin | last post by:
To use the Textstream object, I had to set a Reference to the Microsoft Scripting Runtime. This works good with A2000 Is the Scripting Runtime included with A2002 and A2003 so the Reference won't be broken when my app is opened with those versions. Also is the Scripting Runtime included as part of the A2000 Runtime Engine which some of my customers use.
84
3951
by: Bibby | last post by:
Hi, I'm interested in getting started in the programming world. I've dabbled in C, C++ and VB6. Which would be the best language to focus my attention to regarding the following considerations: Hireability Portability Flexibility The likely candidates seem to be Java, VB.Net, C, C++, C#.
0
4468
ADezii
by: ADezii | last post by:
This is the last in a series of Tips involving the Microsoft Scripting Runtime Library and deals with creating, opening, writing to, reading from, and closing Text Files via this Library. At this time, the Scripting Library cannot deal with the opening and manipulation of Files in Binary Mode, so we will only demonstrate this functionality as it relates to Text Files. The code is fairly straightforward and sparsely commented, so I will not bore...
0
9706
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
9582
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
10335
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10323
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
10082
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
6854
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
5652
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4301
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
3
2993
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.