473,625 Members | 3,249 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

creating a wrapper around a vendor api


Assume I have a vendor file called ' vendor.h'. Within the file
there's two methods memalign and cforward. It is my understanding that
the memalign function is a wrapper around malloc. cforward is just a
vendor function for doing forward FFT's. At issue CSL_COMPLEX is
'cumbersome' to work with. As a result I created a wrapper. So now -
given the pseudo code.

#include <iostream>
#include <complex>
#include <vector>

// in vendor file - vendor.h
struct CSL_COMPLEX {
float r;
float i ;
};

void *memalign(size_ t blocksize, size_t bytes)
{
return ( malloc ( blocksize * bytes ) ) ; // not sure if I have this
right but .. for test purposes
}
void cforward (
CSL_COMPLEX* ptr_input,
CSL_COMPLEX* ptr_ouput,
int num_sample
)
{}
// end vendor.h
typedef std::vector < std::complex < float CFLOAT_VEC ;

struct wrapper {
int previous ;
CSL_COMPLEX *ptr_mem_f ;
wrapper ( )
: previous ( INT_MAX )
, ptr_mem_f ( 0 )
{}

void execute_f ( CFLOAT_VEC& vec )
{
CFLOAT_VEC::siz e_type const current_sz = vec.size() ;
if ( !current_sz )
return ;

if ( previous != current_sz )
{
std::cout << current_sz << std::endl;
ptr_mem_f =
( CSL_COMPLEX *) memalign ( 32, 2 * current_sz * sizeof (
CSL_COMPLEX ) ) ;
}

// copy contents from vec to ptr_mem_f
for ( int idx ( 0 ); idx < current_sz; ++idx ) {
ptr_mem_f [ idx ].r = vec[ idx ].real() ;
ptr_mem_f [ idx ].i = vec[ idx ].imag() ;
}

// - at this point we run the forward FFT run the forward fft
//
cforward ( ptr_mem_f, ptr_mem_f, current_sz ) ; // for inplace
operations input and output is the same

// now copy back to vec ..
for ( int idx ( 0 ); idx < current_sz; ++idx ) {
std::complex < float temp (
ptr_mem_f [ idx ].r,
ptr_mem_f [ idx ].i
);
vec[ idx ] = temp ;
}
previous = current_sz;
}
};

int main()
{

CFLOAT_VEC fv ( 5 );
for ( CFLOAT_VEC::siz e_type jdx ( 0 ); jdx < fv.size(); ++jdx )
fv [ jdx ] = std::complex < float ( jdx , jdx ) ;

wrapper w_obj;
w_obj.execute_f ( fv ) ;
}

The fundamental issue today surrounds the copy from vec to ptr_mem_f
and ptr_mem_f to vec - literally - kills my timing. Besides my current
approach, I'm unsure how to put a decent wrapper around the basket
case of a function - cforward? Ideas welcomed, thanks in advance

Oct 9 '06 #1
9 2330
* ma740988:
Assume I have a vendor file called ' vendor.h'. Within the file
there's two methods memalign and cforward. It is my understanding that
the memalign function is a wrapper around malloc. cforward is just a
vendor function for doing forward FFT's. At issue CSL_COMPLEX is
'cumbersome' to work with. As a result I created a wrapper. So now -
given the pseudo code.

#include <iostream>
#include <complex>
#include <vector>

// in vendor file - vendor.h
struct CSL_COMPLEX {
float r;
float i ;
};

void *memalign(size_ t blocksize, size_t bytes)
{
return ( malloc ( blocksize * bytes ) ) ; // not sure if I have this
right but .. for test purposes
}
void cforward (
CSL_COMPLEX* ptr_input,
CSL_COMPLEX* ptr_ouput,
int num_sample
)
{}
// end vendor.h
Are you sure the cforward function has special alignment requirements
above those provided by C++ new? If not, just use a
std::vector<CSL _COMPLEXin your client code. Or rather, two of them.

Client code might look like this (off the cuff, not test-compiled):

typedef CSL_COMPLEX CslComplex; // Get rid of uppercase, principle.
typedef std::vector<Csl ComplexCslValue s;

void transform( CslValues const& data, CslValues& result )
{
if( data.size() == 0 ) { return; }

CslValues resultBuffer( data.size() );
cforward(
const_cast<CslC omplex*>( &data[0] ),
&resultBuffe r[0],
static_cast<int >( data.size() )
);
// Possible error checking here, I don't see that provided? Then:
std::swap( resultBuffer, result ); // Constant time.
}

// Wrapper for notational ease, relying on modern compiler with RVO.
// If measurements show unacceptable time used on copying, one might
// need to rewrite client code to use 'transform' above directly.'
// But that would be a premature optimization without measurements.
inline CslValues transformed( CslValues const& data )
{
CslValues result;
transform( data, result );
return result;
}

struct MakeCslComplex: CslComplex
{
MakeCslComplex( CslComplex const& v ) { r = v.i; i = v.i; }
MakeCslComplex( float re, float im ) { r = re; i = im }
// Etc., eg. conversion from std::complex<do uble>
};

int main()
{
CslValues data;
data.push_back( MakeCslComplex( 1.0, 2.0 ) );
data.push_back( MakeCslComplex( 3.0, 4.0 ) );
// etc.
CslValues result = transformed( data );
}

Hth.,

- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 9 '06 #2
* Alf P. Steinbach:
void transform( CslValues const& data, CslValues& result )
{
if( data.size() == 0 ) { return; }
Argh. Replace the simple 'return' with something like

{ CslValues empty; empty.swap( result ); return; }

Grumble.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 10 '06 #3

Alf P. Steinbach wrote:

Alf, for starters, thanks alot for the suggestions.
Are you sure the cforward function has special alignment requirements
above those provided by C++ new?
Yes, the cforward function has special alignment requirements. You see
under the hood the cforward routine uses hardware tricks to achieve
these 'fast FFT times'.. Meaning they're using an Altivec engine. Now
I'm not too savy on the altivec specifics but my understanding is that
this altivec requires ( dont quote me on this ) 4 byte alignments.
>From what I understand ( sadly I can't recall all this ) the alignments
allow for this engine to do 4 multiplies ( or some such thing) in one
clock cycle. Bottom line. Lots of hardware tricks but your data has
to be 'just right'.

Does this mean because of the alignment your approach doesn't work?

Oct 10 '06 #4

Alf P. Steinbach wrote:
>
Are you sure the cforward function has special alignment requirements
above those provided by C++ new?
Bye the way, what alignment requirements does new require? I'm trying
to find information on this but I'm coming up short.
[OT]
The first link below references the memalign function. The vxworks
manual is in some respects 'silent' on memalign but I think the premise
is the same as that in the second link.

http://www-kryo.desy.de/documents/vx....html#memalign
http://www.mkssoftware.com/docs/man3/memalign.3.asp

Oct 10 '06 #5
* ma740988:
Alf P. Steinbach wrote:

Alf, for starters, thanks alot for the suggestions.
>Are you sure the cforward function has special alignment requirements
above those provided by C++ new?
Yes, the cforward function has special alignment requirements. You see
under the hood the cforward routine uses hardware tricks to achieve
these 'fast FFT times'.. Meaning they're using an Altivec engine. Now
I'm not too savy on the altivec specifics but my understanding is that
this altivec requires ( dont quote me on this ) 4 byte alignments.
I'd just try and see. Also, most compilers let you specify the default
alignment for dynamic allocation, via command line options and via
pragmas. However, the default is unlikely to be less than 4 (if higher
and a power of two the 4 byte alignment is automatically satisfied), so,
disregarding extreme Murphy activity, things should work by default.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 10 '06 #6
ma740988 wrote:
I'm not too savy on the altivec specifics but my understanding is that
this altivec requires ( dont quote me on this ) 4 byte alignments.
Altivec requires data to be 16-byte aligned. On MacOS X, this is the
default for malloc, and I believe for new, as documented by Apple. On
most other systems (e.g. GNU/Linux, and Windows I think), memory
allocation uses only 8-byte alignment by default. Hence the need for
memalign.

Since your routine expects a struct { float real; float imag; } complex
data type, you shouldn't actually need to do any copying. It should be
sufficient to do a reinterpret_cas t<csl_complex*> (x). The reason for
this is that essentially all extant C++ compilers store complex<fooas
real part followed by imaginary part, with no padding, for any floating
point type foo. This is actually slated to become part of the standard
(see http://anubis.dkuug.dk/JTC1/SC22/WG2...2002/1388.pdf).
So, the only problem is to override the memory allocation to fix the
alignment.

Regards,
Steven G. Johnson

Oct 10 '06 #7

st*****@alum.mi t.edu wrote:
Since your routine expects a struct { float real; float imag; } complex
data type, you shouldn't actually need to do any copying. It should be
sufficient to do a reinterpret_cas t<csl_complex*> (x). The reason for
this is that essentially all extant C++ compilers store complex<fooas
real part followed by imaginary part, with no padding, for any floating
point type foo. This is actually slated to become part of the standard
(see http://anubis.dkuug.dk/JTC1/SC22/WG2...2002/1388.pdf).
So, the only problem is to override the memory allocation to fix the
alignment.
I see. I'll check into achieving this override.
Thanks alot

Oct 10 '06 #8
* ma740988:
st*****@alum.mi t.edu wrote:
>Since your routine expects a struct { float real; float imag; } complex
data type, you shouldn't actually need to do any copying. It should be
sufficient to do a reinterpret_cas t<csl_complex*> (x). The reason for
this is that essentially all extant C++ compilers store complex<fooas
real part followed by imaginary part, with no padding, for any floating
point type foo. This is actually slated to become part of the standard
(see http://anubis.dkuug.dk/JTC1/SC22/WG2...2002/1388.pdf).
So, the only problem is to override the memory allocation to fix the
alignment.
I see. I'll check into achieving this override.
Check out the allocator template parameter to std::vector.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 10 '06 #9

Alf P. Steinbach wrote:
Check out the allocator template parameter to std::vector.
Yeah, that's where I'm going through overload. I never thought I'd
ever need to 'write my own allocator' ( thought it was too hi-tech for
me ) as a result I skipped those chapters in Josuttis and the like :)

Oct 10 '06 #10

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

Similar topics

1
2285
by: Mark McEahern | last post by:
I just wrote a very simple wrapper for the PayFlow Pro SDK (see below). A friend of mine did this before, but I didn't have access to his source, so I took it as a learning opportunity for me to write a C wrapper. I did a little searching to see whether anyone had done anything like this for Python. I didn't find anything. I did find that PHP comes with an extension for PayFlow Pro that you can compile into the language:
4
2772
by: Edvard Majakari | last post by:
Hi, I was wondering what would be the most elegant way for creating a Python class wrapper for a command line utility, which takes three types of arguments: 1. options with values (--foo=bar) 2. boolean options (--squibble) 3. data lines (MUNGE:x:y:z:frob)
15
3809
by: Steve Richfield | last post by:
To All, First, some of your replies to me have been posted through DevelopersDex, and these are NOT posted on USENET for the world to see. DevelopersDex appears to be trying to hijack USENET, though there may be some more benign explanation that escapes me. If you want the world to see your replies, then you should post them directly to USENET. However, I'll see them either way. After years of frustration with DAO, it dawned on me...
4
1921
by: Stephen | last post by:
Hi I am currently developing a web application that has a third party component on it. The third party component is a graph component from Xceed that uses a number of dlls. The problems occur when we attempt to deploy the application and the third party component causes a security exception. Description: The application attempted to perform an operation not allowed by the security policy. To grant this application the required...
13
4118
by: Grace | last post by:
Hi, How do I create/access an INI file in VB .NET? I appreciate any help you can give me... Thanks!
8
3263
by: Oenone | last post by:
Is it possible to create an object which can have methods and properties, but which can also be treated as a string? I'm trying to create a wrapper around the IIS Request.Form object which behaves the same as the classic ASP object behaved. This will allow me to quickly get a large amount of code up and running (I can then tweak it to not require this wrapper at a more leisurely pace). When queried directly, this object returns a...
5
4058
by: Bob Bins | last post by:
Is there a way to create a shared lock using the Monitor class or any other synchronization class in .NET? So I have one resource that may have multiple threads using it at once but a second thread that when called must have exclusive access and cause the other threads to wait. I can't figure out how to do this with .Net. Thanks.
16
3429
by: utab | last post by:
Dear all, In programming terminology, what is a wrapper and where is it used? Regards
0
1398
by: mmueller | last post by:
I am new to reporting services 2005 (reporting in Access for years and older versions of Reporting Services from time to time) and this is probably a dumb question... but I have no internal resources since I am the first to use it so... here you go: I have a report I am trying to recreate, the old one is in a PowerBuilder app and the author is no longer with company. The majority of the report is straightforward, but I am really hung up on...
0
8256
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
8694
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...
0
8497
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
7184
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6118
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4089
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4193
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1500
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.