Created
May 29, 2012 12:38
-
-
Save OpenGamma-Blog/2828177 to your computer and use it in GitHub Desktop.
Accessing Native Maths Libraries 5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// implement fortran xerbla | |
#define XERBLA_F77 F77_FUNC(xerbla,XERBLA) | |
// global cache of the VM ptr | |
JavaVM *JVMcache; | |
// Called when the native library is loaded | |
#ifdef __cplusplus | |
extern "C" | |
#endif | |
// The JNI_OnLoad function is called when the native library is loaded | |
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved) | |
{ | |
JVMcache=jvm; // set the global JVM pointer cache to one OnLoad JVM | |
JNIEnv *env=NULL; | |
if ((*jvm)->GetEnv(jvm, (void **)&env, JNI_VERSION_1_2)) { | |
return JNI_ERR; // JNI version not supported | |
} | |
return JNI_VERSION_1_2; | |
} | |
// Normally we'd use a Fortran XERBLA from linked BLAS, but this (often) has a STOP command in it so we override this on the basis that return is called following xerbla | |
// in this way the user has a chance to handle the failure and try again opposed to exit() | |
#ifdef __cplusplus | |
extern "C" | |
#endif | |
void XERBLA_F77(char * SRNAME, int * INFO) | |
{ | |
int i; | |
char name[7]; //LAPACK/BLAS names are always 6 characters or less | |
memset(name,' ',6); | |
for(i=0;i < 6;i++){ | |
name[i]=SRNAME[i]; | |
} | |
name[6]='\0'; | |
printf("Hitting XERBLA overload. Exception state = %d\n",*INFO); | |
printf("Hit called from %s, on arg %d\n",name,*INFO); | |
JNIEnv *envPtr; // local grab of env ptr for this thread | |
(*JVMcache)->AttachCurrentThrea(dJVMcache, (void **)&envPtr, NULL); // attach this thread to the globally cache JVM | |
// find exception class | |
jclass Exception = NULL; | |
Exception = (*envPtr)->FindClass(envPtr,"com/opengamma/maths/nativewrappers/exceptions/OGNativeMathsWrapperInvalidArgumentException"); // find class | |
if(Exception==NULL) | |
{ | |
printf("Cannot find exception class in XERBLA implementation in libOGXERBLAoverride file=__FILE__,line=__LINE__. Hard Exiting.\n"); | |
exit(USER_DEFINED_ERROR_CODE); | |
} | |
char msg[100]; | |
memset(msg,' ',100); | |
sprintf(msg,"Native BLAS: XERBLA: Invalid argument at position %d in routine %s \n",*INFO,name); | |
(*envPtr)->ThrowNew(envPtr,Exception,msg); | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment