java math performance

O

os2

hi

i tried to improve performance on for some trigonometry calcule

i read
http://www.javaworld.com/javatips/jw-javatip141.html?

i do

tmp@linux:~/bench> echo $CLASSPATH
/usr/lib/j2sdk1.4.2_03:/home/tmp/bench/java


tmp@linux:~/bench> javac MathLib.java

tmp@linux:~/bench> javah -jni -o MathLib.h -classpath . MathLib

i have some error with this command..

tmp@linux:~/bench> gcc -I/usr/lib/j2sdk1.4.2_03
-I/usr/lib/j2sdk1.4.2_03/include/linux Math.c -o MathLib.so
/usr/lib/gcc-lib/i586-suse-linux/3.3.3/../../../crt1.o(.text+0x18): in
function « _start »:
.../sysdeps/i386/elf/start.S:98: undefined reference to `main'
/tmp/cc8K7UyJ.o(.text+0x1c): in function « Java_MathLib_sin »:
: undefined reference to `sin'
/tmp/cc8K7UyJ.o(.text+0x41): in function « Java_MathLib_cos »:
: undefined reference to `cos'
/tmp/cc8K7UyJ.o(.text+0x66): in function « Java_MathLib_sqrt »:
: undefined reference to `sqrt'


how to resolve that?


MathLib.java file

public class MathLib
{
public static native double sin(double radians);
public static native double cos(double radians);
public static native double sqrt(double value);

static
{
System.loadLibrary("MathLib");
}
}


Math.c file


#include <jni.h>
#include <math.h>
#include "MathLib.h"
#include <stdio.h>

JNIEXPORT jdouble JNICALL Java_MathLib_sin(JNIEnv *env, jobject obj,
jdouble value)
{
return(sin(value));
}

JNIEXPORT jdouble JNICALL Java_MathLib_cos(JNIEnv *env, jobject obj,
jdouble value)
{
return(cos(value));
}

JNIEXPORT jdouble JNICALL Java_MathLib_sqrt(JNIEnv *env, jobject obj,
jdouble value)
{
return(sqrt(value));
}




MathLib.h is generate automatically by javah....

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class MathLib */

#ifndef _Included_MathLib
#define _Included_MathLib
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: MathLib
* Method: sin
* Signature: (D)D
*/
JNIEXPORT jdouble JNICALL Java_MathLib_sin
(JNIEnv *, jclass, jdouble);

/*
* Class: MathLib
* Method: cos
* Signature: (D)D
*/
JNIEXPORT jdouble JNICALL Java_MathLib_cos
(JNIEnv *, jclass, jdouble);

/*
* Class: MathLib
* Method: sqrt
* Signature: (D)D
*/
JNIEXPORT jdouble JNICALL Java_MathLib_sqrt
(JNIEnv *, jclass, jdouble);

#ifdef __cplusplus
}
#endif
#endif



any idea?
 
J

John Davison

os2 said:
i have some error with this command..

tmp@linux:~/bench> gcc -I/usr/lib/j2sdk1.4.2_03
-I/usr/lib/j2sdk1.4.2_03/include/linux Math.c -o MathLib.so
/usr/lib/gcc-lib/i586-suse-linux/3.3.3/../../../crt1.o(.text+0x18): in
function « _start »:
../sysdeps/i386/elf/start.S:98: undefined reference to `main'
/tmp/cc8K7UyJ.o(.text+0x1c): in function « Java_MathLib_sin »:
: undefined reference to `sin'
/tmp/cc8K7UyJ.o(.text+0x41): in function « Java_MathLib_cos »:
: undefined reference to `cos'
/tmp/cc8K7UyJ.o(.text+0x66): in function « Java_MathLib_sqrt »:
: undefined reference to `sqrt'


how to resolve that?

This isn't really a Java question - try comp.lang.c
More than likely you are not supplying the correct flags to the gcc
compiler, or better yet, read up on gcc to see how to create .so files.

John Davison
 
C

Carl Howells

os2 said:
tmp@linux:~/bench> gcc -I/usr/lib/j2sdk1.4.2_03
-I/usr/lib/j2sdk1.4.2_03/include/linux Math.c -o MathLib.so
/usr/lib/gcc-lib/i586-suse-linux/3.3.3/../../../crt1.o(.text+0x18): in
function « _start »:
../sysdeps/i386/elf/start.S:98: undefined reference to `main'
/tmp/cc8K7UyJ.o(.text+0x1c): in function « Java_MathLib_sin »:
: undefined reference to `sin'
/tmp/cc8K7UyJ.o(.text+0x41): in function « Java_MathLib_cos »:
: undefined reference to `cos'
/tmp/cc8K7UyJ.o(.text+0x66): in function « Java_MathLib_sqrt »:
: undefined reference to `sqrt'

I tried replying to this yesterday, but my reply was apparently eaten by
some internet monster.

Your problem is that gcc's default set of libraries that it links
against doesn't include the math library. Yeah, I can't understand why
not, either. Add -lm to the gcc compile line, and that should work.
 
A

Ann

Carl Howells said:
I tried replying to this yesterday, but my reply was apparently eaten by
some internet monster.

Your problem is that gcc's default set of libraries that it links
against doesn't include the math library. Yeah, I can't understand why
not, either. Add -lm to the gcc compile line, and that should work.

It has been that way (-lm) forever.
 

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

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top