pointers in c++ ??

C

Cormac O'Donnell

hi,

I have a piece of code implementing the FFT algorithm in c++ and i
have to convert it into java. i am getting stuck trying to understand
these pointers. Anyone who has experience in java and C++, could you
please explain the code below , esp the lines a1+=nFFT2( i know it
increments the a1 variable/pointer by nFFT2 but how does this give the
methos FBFilter new elements of each array). Anyone who could
translate this to java would be a star!!!

Cormac.

nFrames = 249;
nFFT2 = 128;
nChn =25;

float* s=new float[nFrames*nFFT2]; // FFT
float* u=new float[nFrames*nChn]; // FB

// s and u have operations done on them here

a1=s;
float* a2=u;
for(int i=0;i<nFrames;i++) {
FBFilter(a1,a2);
a1+=nFFT2;
a2+=nChn;
}
 
R

Rolf Magnus

Cormac said:
hi,

I have a piece of code implementing the FFT algorithm in c++ and i
have to convert it into java. i am getting stuck trying to understand
these pointers. Anyone who has experience in java and C++, could you
please explain the code below ,

I don't know Java, but C++, so maybe I can still help.
esp the lines a1+=nFFT2( i know it increments the a1 variable/pointer
by nFFT2 but how does this give the methos FBFilter new elements of
each array).

a1 starts with pointing to the first element of an array of
nFrames*nFFT2 elements. Incrementing a pointer means letting it point
to the next element, adding e.g. 10 to it means letting it point 10
elements further. So in the first iteration, the function gets a
pointer to the first element, in the second one, a pointer to the 128th
element, then to the 256th and so on. I guess that FBFilter will always
take a block of 128 elements.
Anyone who could translate this to java would be a star!!!

Cormac.

nFrames = 249;
nFFT2 = 128;
nChn =25;

float* s=new float[nFrames*nFFT2]; // FFT
float* u=new float[nFrames*nChn]; // FB

// s and u have operations done on them here

a1=s;
float* a2=u;
for(int i=0;i<nFrames;i++) {
FBFilter(a1,a2);
a1+=nFFT2;
a2+=nChn;
}
 
J

John Harrison

Cormac O'Donnell said:
hi,

I have a piece of code implementing the FFT algorithm in c++ and i
have to convert it into java. i am getting stuck trying to understand
these pointers. Anyone who has experience in java and C++, could you
please explain the code below , esp the lines a1+=nFFT2( i know it
increments the a1 variable/pointer by nFFT2 but how does this give the
methos FBFilter new elements of each array). Anyone who could
translate this to java would be a star!!!

Cormac.

nFrames = 249;
nFFT2 = 128;
nChn =25;

float* s=new float[nFrames*nFFT2]; // FFT
float* u=new float[nFrames*nChn]; // FB

// s and u have operations done on them here

a1=s;
float* a2=u;
for(int i=0;i<nFrames;i++) {
FBFilter(a1,a2);
a1+=nFFT2;
a2+=nChn;
}

The way pointers are being used here is pretty pointless (pardon the pun).
Perhaps you'll find this equivalent code easier to understand.

nFrames = 249;
nFFT2 = 128;
nChn =25;

float* s=new float[nFrames*nFFT2]; // FFT
float* u=new float[nFrames*nChn]; // FB

// s and u have operations done on them here

int i1=0;
int i2=0;
for(int i=0;i<nFrames;i++) {
FBFilter(&s[i1],&u[i2]);
i1+=nFFT2;
i2+=nChn;
}

It's really that simple.

john
 
C

Claudio Puviani

Cormac O'Donnell said:
hi,

I have a piece of code implementing the FFT algorithm in c++ and i
have to convert it into java. i am getting stuck trying to understand
these pointers. Anyone who has experience in java and C++, could you
please explain the code below , esp the lines a1+=nFFT2( i know it
increments the a1 variable/pointer by nFFT2 but how does this give the
methos FBFilter new elements of each array). Anyone who could
translate this to java would be a star!!!

From a practical point of view, why don't you just wrap the C++ code with a JNI
interface? That way, you don't need to put effort into translating it AND your
FFT doesn't suddenly become a S L O W Fourier Transform.

Claudio Puviani
 
A

Alf P. Steinbach

* "Claudio Puviani said:
From a practical point of view, why don't you just wrap the C++ code with a JNI
interface? That way, you don't need to put effort into translating it AND your
FFT doesn't suddenly become a S L O W Fourier Transform.

Your suggestion may (or may not, but probably may) have the opposite effect
of what you think.

Wrt. computational tasks Java implementations have many times outperformed
C++ implementations; whatever the causes might be it's a fact.

Otoh. JNI is well-known as introducing extreme overhead, as well as complexity,
so the end result of going that way might be something slow and incorrect...
 
C

Cy Edmunds

Cormac O'Donnell said:
hi,

I have a piece of code implementing the FFT algorithm in c++ and i
have to convert it into java. i am getting stuck trying to understand
these pointers. Anyone who has experience in java and C++, could you
please explain the code below , esp the lines a1+=nFFT2( i know it
increments the a1 variable/pointer by nFFT2 but how does this give the
methos FBFilter new elements of each array). Anyone who could
translate this to java would be a star!!!

Cormac.

nFrames = 249;
nFFT2 = 128;
nChn =25;

float* s=new float[nFrames*nFFT2]; // FFT
float* u=new float[nFrames*nChn]; // FB

// s and u have operations done on them here

a1=s;
float* a2=u;
for(int i=0;i<nFrames;i++) {
FBFilter(a1,a2);
a1+=nFFT2;
a2+=nChn;
}

Perhaps you should consider FFTW. I have never used it in Java, but I have
had very good luck with it in C++. They claim to have a Java wrapper here:

http://www.fftw.org/download.html
 
G

Gianni Mariani

Alf said:
Your suggestion may (or may not, but probably may) have the opposite effect
of what you think.

Wrt. computational tasks Java implementations have many times outperformed
C++ implementations; whatever the causes might be it's a fact.

Can you show a computational application where this is the case ? (i.e.
Java implementation has outperformed a C++ implementation ?)

BTW - I have heard that comment made many times, and I could probably
thing of a case where it may be true, but I have yet to see any evidence
where Java is faster than C++.
 
A

Alf P. Steinbach

* Gianni Mariani said:
Can you show a computational application where this is the case ? (i.e.
Java implementation has outperformed a C++ implementation ?)

Google is your friend. This is several years old news. Just to check it out I
did an initial Googling, just entering "Java outperforms C++ computation", and near
the top of the list came <url: http://www.javagrande.org/leapforward/cacm-ron.pdf>;
I'm sure you'll be able to find more recent results. Also check with Sun. Of
course you'll find evidence also for Java implementations being slow... ;-)

BTW - I have heard that comment made many times, and I could probably
thing of a case where it may be true, but I have yet to see any evidence
where Java is faster than C++.

What has language speed, a non-existent concept, to do with anything?
 
C

Claudio Puviani

Alf P. Steinbach said:
Google is your friend. This is several years old news. Just to check it out I
did an initial Googling, just entering "Java outperforms C++ computation", and near
the top of the list came <url: http://www.javagrande.org/leapforward/cacm-ron.pdf>;
I'm sure you'll be able to find more recent results. Also check with Sun. Of
course you'll find evidence also for Java implementations being slow... ;-)

Unfortunately, that article is vague to the point of being useless. It's easy
to demonstrate that a programmer who is fluent in Java but not in C++ can write
Java code that's faster than his (bad) C++ code. The only thing comparisons like
that demonstrate is the need for people to program in the language they're
familiar with.

I'll second Gianni and say that since the inception of Java, I've yet to see a
credible comparison shows a well-written Java (non-trivial) numerics program
reaching the speed of a well-written C++ (non-trivial) numerics program. I've
seen plenty of toy examples showing that a couple of nested loops performing a
banal computation can be comparable with the two languages, but anyone who
extrapolates that to real-world calculations (which I'm not insinuating that you
do) never so much as sneezed in the general direction of a numerical
application.

As to the overhead of JNI, it's rendered irrelevant if the goal is to call a
function that takes non-negligible time to execute. You wouldn't wrap the sin()
function in a JNI call, but with things like time series analysis, minimax
problems, and so on, the fraction of the time spent in the JNI overhead would be
ridiculously small. In fact, in practice, you often see these types of
calculations offloaded to a remote server because even the network chatter is
trivialized by the size of the problem.

Claudio Puviani
 
J

Jorge Rivera

I have a piece of code implementing the FFT algorithm in c++ and i
have to convert it into java. i am getting stuck trying to understand
these pointers. Anyone who has experience in java and C++, could you
please explain the code below , esp the lines a1+=nFFT2( i know it
increments the a1 variable/pointer by nFFT2 but how does this give the
methos FBFilter new elements of each array). Anyone who could
translate this to java would be a star!!!

I think a Java translation is pretty straight-forward....

int nFrames = 249;
int nFFT2 = 128;
int nChn =25;

float[] s=new float[nFrames*nFFT2]; // FFT
float[] u=new float[nFrames*nChn]; // FB

// s and u have operations done on them here

int index1 = 0;
int index2 = 0;


for(int i=0;i<nFrames;i++) {
// In Java, FBFilter would take a float instead
// of a float*
FBFilter(s[index1],u[index2]);

index1 += nFFT2;
index2 += nChn;

// If the previous lines don't compile, try
// index1 = index1 + nFFT2;
// index2 = index2 + nChn;
}
 
G

Gianni Mariani

Alf said:
Google is your friend. This is several years old news. Just to check it out I
did an initial Googling, just entering "Java outperforms C++ computation", and near
the top of the list came <url: http://www.javagrande.org/leapforward/cacm-ron.pdf>;
I'm sure you'll be able to find more recent results. Also check with Sun. Of
course you'll find evidence also for Java implementations being slow... ;-)

Believe me, I have done that many times and I have yet to find *ANY*
ACTUAL examples of code that runs slower with a compiled language.

The article you cite is yet another example of vapour ware.

Again, I'll say that there *could* be an example, I've heard of so much
research in this are but I have yet to see an example *in-the-wild* so
to speak.

After so many years of vapourware claims, and yet not being able to see
any real advances in this area, my sceptiscism meter has pegged.

It's interesting also that the example describes working on complex
numbers. Complex numbers are supported as part of the C++ standard now
and it is significantly easier to work with compared to Java.
What has language speed, a non-existent concept, to do with anything?

I think you'll need to read everything again in the context of
"computational tasks".
 
J

Jonathan Turkanis

Alf P. Steinbach said:
* Gianni Mariani <[email protected]> schriebt:
Google is your friend. This is several years old news. Just to check it out I
did an initial Googling, just entering "Java outperforms C++ computation", and near
the top of the list came <url: http://www.javagrande.org/leapforward/cacm-ron.pdf>;
I'm sure you'll be able to find more recent results. Also check with Sun. Of
course you'll find evidence also for Java implementations being slow... ;-)



What has language speed, a non-existent concept, to do with anything?

Language speed a non-existent concept? The authors of the article you
cite don't think so:

"For example, a similar comparison on a Sun UltraSPARC, shows that
Java is only 60% the speed of compiled C."

Jonathan
 
M

Martin Stettner

There will be a problem with the line
FBFilter(s[index1],u[index2]);

In the C++ code you pass pointers to two locations in the respective arrays
to the function FBFilter. It seems to me that this function will do some
calculation using all 128 (=nFFT2) resp. 25 (=nChn) Values starting at the
passed location.

So the function would have to be adapted in order to take references to the
arrays as well as the current indices. The call could then look like:

FBFilter(s,index1,u,index2);

greetings
Martin



Jorge Rivera said:
I have a piece of code implementing the FFT algorithm in c++ and i
have to convert it into java. i am getting stuck trying to understand
these pointers. Anyone who has experience in java and C++, could you
please explain the code below , esp the lines a1+=nFFT2( i know it
increments the a1 variable/pointer by nFFT2 but how does this give the
methos FBFilter new elements of each array). Anyone who could
translate this to java would be a star!!!

I think a Java translation is pretty straight-forward....

int nFrames = 249;
int nFFT2 = 128;
int nChn =25;

float[] s=new float[nFrames*nFFT2]; // FFT
float[] u=new float[nFrames*nChn]; // FB

// s and u have operations done on them here

int index1 = 0;
int index2 = 0;


for(int i=0;i<nFrames;i++) {
// In Java, FBFilter would take a float instead
// of a float*
FBFilter(s[index1],u[index2]);

index1 += nFFT2;
index2 += nChn;

// If the previous lines don't compile, try
// index1 = index1 + nFFT2;
// index2 = index2 + nChn;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,431
Latest member
ElyseG3173

Latest Threads

Top