Skip to content
Snippets Groups Projects
Commit 5fb5df24 authored by Gary Frost's avatar Gary Frost
Browse files

Tidy JNI code (added some helpers)

parent 35d5e1a3
No related branches found
No related tags found
No related merge requests found
...@@ -288,6 +288,8 @@ jfieldID Range::localIsDerivedFieldID=0; ...@@ -288,6 +288,8 @@ jfieldID Range::localIsDerivedFieldID=0;
class ProfileInfo{ class ProfileInfo{
public: public:
jint type; //0 write, 1 execute, 2 read
char *name;
cl_ulong queued; cl_ulong queued;
cl_ulong submit; cl_ulong submit;
cl_ulong start; cl_ulong start;
...@@ -880,7 +882,7 @@ jint writeProfileInfo(JNIContext* jniContext){ ...@@ -880,7 +882,7 @@ jint writeProfileInfo(JNIContext* jniContext){
} }
// Should failed profiling abort the run and return early? // Should failed profiling abort the run and return early?
cl_int profile(ProfileInfo *profileInfo, cl_event *event){ cl_int profile(ProfileInfo *profileInfo, cl_event *event, jint type, char* name){
cl_int status = CL_SUCCESS; cl_int status = CL_SUCCESS;
status = clGetEventProfilingInfo(*event, CL_PROFILING_COMMAND_QUEUED, sizeof(profileInfo->queued), &(profileInfo->queued), NULL); status = clGetEventProfilingInfo(*event, CL_PROFILING_COMMAND_QUEUED, sizeof(profileInfo->queued), &(profileInfo->queued), NULL);
ASSERT_CL( "clGetEventProfiliningInfo() QUEUED"); ASSERT_CL( "clGetEventProfiliningInfo() QUEUED");
...@@ -890,6 +892,8 @@ cl_int profile(ProfileInfo *profileInfo, cl_event *event){ ...@@ -890,6 +892,8 @@ cl_int profile(ProfileInfo *profileInfo, cl_event *event){
ASSERT_CL( "clGetEventProfiliningInfo() START"); ASSERT_CL( "clGetEventProfiliningInfo() START");
status = clGetEventProfilingInfo(*event, CL_PROFILING_COMMAND_END, sizeof(profileInfo->end), &(profileInfo->end), NULL); status = clGetEventProfilingInfo(*event, CL_PROFILING_COMMAND_END, sizeof(profileInfo->end), &(profileInfo->end), NULL);
ASSERT_CL( "clGetEventProfiliningInfo() END"); ASSERT_CL( "clGetEventProfiliningInfo() END");
profileInfo->type = type;
profileInfo->name = name;
return status; return status;
} }
...@@ -1357,7 +1361,7 @@ JNIEXPORT jint JNICALL Java_com_amd_aparapi_KernelRunner_runKernelJNI(JNIEnv *je ...@@ -1357,7 +1361,7 @@ JNIEXPORT jint JNICALL Java_com_amd_aparapi_KernelRunner_runKernelJNI(JNIEnv *je
for (int i=0; i< readEventCount; i++){ for (int i=0; i< readEventCount; i++){
if (jniContext->isProfilingEnabled()) { if (jniContext->isProfilingEnabled()) {
status = profile(&jniContext->args[jniContext->readEventArgs[i]]->value.ref.read, &jniContext->readEvents[i]); status = profile(&jniContext->args[jniContext->readEventArgs[i]]->value.ref.read, &jniContext->readEvents[i], 0,jniContext->args[jniContext->readEventArgs[i]]->name);
if (status != CL_SUCCESS) { if (status != CL_SUCCESS) {
jniContext->unpinAll(jenv); jniContext->unpinAll(jenv);
return status; return status;
...@@ -1381,7 +1385,7 @@ JNIEXPORT jint JNICALL Java_com_amd_aparapi_KernelRunner_runKernelJNI(JNIEnv *je ...@@ -1381,7 +1385,7 @@ JNIEXPORT jint JNICALL Java_com_amd_aparapi_KernelRunner_runKernelJNI(JNIEnv *je
} }
if (jniContext->isProfilingEnabled()) { if (jniContext->isProfilingEnabled()) {
status = profile(&jniContext->exec, &jniContext->executeEvents[0]); status = profile(&jniContext->exec, &jniContext->executeEvents[0], 1, NULL);
if (status != CL_SUCCESS) { if (status != CL_SUCCESS) {
jniContext->unpinAll(jenv); jniContext->unpinAll(jenv);
return status; return status;
...@@ -1415,7 +1419,7 @@ JNIEXPORT jint JNICALL Java_com_amd_aparapi_KernelRunner_runKernelJNI(JNIEnv *je ...@@ -1415,7 +1419,7 @@ JNIEXPORT jint JNICALL Java_com_amd_aparapi_KernelRunner_runKernelJNI(JNIEnv *je
for (int i=0; i< writeEventCount; i++){ for (int i=0; i< writeEventCount; i++){
if (jniContext->isProfilingEnabled()) { if (jniContext->isProfilingEnabled()) {
profile(&jniContext->args[jniContext->writeEventArgs[i]]->value.ref.write, &jniContext->writeEvents[i]); profile(&jniContext->args[jniContext->writeEventArgs[i]]->value.ref.write, &jniContext->writeEvents[i], 2, jniContext->args[jniContext->writeEventArgs[i]]->name);
} }
status = clReleaseEvent(jniContext->writeEvents[i]); status = clReleaseEvent(jniContext->writeEvents[i]);
if (status != CL_SUCCESS) { if (status != CL_SUCCESS) {
...@@ -1743,135 +1747,103 @@ JNIEXPORT jint JNICALL Java_com_amd_aparapi_KernelRunner_getJNI(JNIEnv *jenv, jo ...@@ -1743,135 +1747,103 @@ JNIEXPORT jint JNICALL Java_com_amd_aparapi_KernelRunner_getJNI(JNIEnv *jenv, jo
return 0; return 0;
} }
jobject createList(JNIEnv *jenv, JNIContext *jniContext){ jobject createInstance(JNIEnv *jenv, char* className, char *signature, ... ){
jclass listClass = jenv->FindClass("java/util/ArrayList"); jclass theClass = jenv->FindClass(className);
if (listClass == NULL || jenv->ExceptionCheck()) { if (theClass == NULL || jenv->ExceptionCheck()) {
jenv->ExceptionDescribe(); /* write to console */\ jenv->ExceptionDescribe();
jenv->ExceptionClear();\ jenv->ExceptionClear();
fprintf(stderr, "bummer! class\n"); fprintf(stderr, "bummer! getting '%s'\n", className);
return(NULL); return(NULL);
} }
jmethodID constructor= jenv->GetMethodID(listClass,"<init>","()V"); jmethodID constructor= jenv->GetMethodID(theClass,"<init>",signature);
if (constructor == NULL || jenv->ExceptionCheck()) { if (constructor == NULL || jenv->ExceptionCheck()) {
jenv->ExceptionDescribe(); /* write to console */\ jenv->ExceptionDescribe();
jenv->ExceptionClear();\ jenv->ExceptionClear();
fprintf(stderr, "bummer getting constructor! \n"); fprintf(stderr, "bummer getting constructor from '%s' with signature! '%s' \n", className, signature);
return(NULL); return(NULL);
} }
jobject listInstance = jenv->NewObject(listClass, constructor); va_list argp;
if (listInstance == NULL || jenv->ExceptionCheck()) { va_start(argp, signature);
jenv->ExceptionDescribe(); /* write to console */\ jobject instance = jenv->NewObjectV(theClass, constructor, argp);
jenv->ExceptionClear();\ if (instance == NULL || jenv->ExceptionCheck()) {
fprintf(stderr, "bummer invoking constructor! \n"); jenv->ExceptionDescribe();
return(NULL); jenv->ExceptionClear();
fprintf(stderr, "bummer invoking constructor from '%s' with signature! '%s' \n", className, signature);
} }
return(listInstance); va_end(argp);
return(instance);
} }
void add(JNIEnv *jenv, JNIContext *jniContext, jobject list, jobject profileItem){ void callVoid(JNIEnv *jenv, jobject instance, char *methodName, char *methodSignature, ...){
jclass listClass = jenv->FindClass("java/util/ArrayList"); jclass theClass = jenv->GetObjectClass(instance);
if (listClass == NULL || jenv->ExceptionCheck()) { if (theClass == NULL || jenv->ExceptionCheck()) {
jenv->ExceptionDescribe(); /* write to console */\ jenv->ExceptionDescribe();
jenv->ExceptionClear();\ jenv->ExceptionClear();
fprintf(stderr, "bummer! list class\n"); fprintf(stderr, "bummer! getting class from instance\n");
return; return;
} }
jmethodID methodId= jenv->GetMethodID(listClass,"add","(Ljava/lang/Object;)Z"); jmethodID methodId= jenv->GetMethodID(theClass,methodName,methodSignature);
if (methodId == NULL || jenv->ExceptionCheck()) { if (methodId == NULL || jenv->ExceptionCheck()) {
jenv->ExceptionDescribe(); /* write to console */\ jenv->ExceptionDescribe();
jenv->ExceptionClear();\ jenv->ExceptionClear();
fprintf(stderr, "bummer getting add method! \n"); fprintf(stderr, "bummer getting method '%s %s' from instance \n", methodName, methodSignature);
return; return;
} }
jenv->CallVoidMethod(list, methodId, profileItem); va_list argp;
va_start(argp, methodSignature);
jenv->CallVoidMethodV(instance, methodId, argp);
if (jenv->ExceptionCheck()) { if (jenv->ExceptionCheck()) {
jenv->ExceptionDescribe(); /* write to console */\ jenv->ExceptionDescribe(); /* write to console */
jenv->ExceptionClear();\ jenv->ExceptionClear();
fprintf(stderr, "bummer callin add\n"); fprintf(stderr, "bummer calling '%s %s'\n", methodName, methodSignature);
return;
} }
va_end(argp);
return;
} }
jobject createProfileInfo(JNIEnv *jenv, jstring label, ProfileInfo &profileInfo, jint type){ jobject createProfileInfo(JNIEnv *jenv, ProfileInfo &profileInfo){
jclass profileInfoClass = jenv->FindClass("com/amd/aparapi/ProfileInfo"); jobject profileInstance = createInstance(jenv, "com/amd/aparapi/ProfileInfo", "(Ljava/lang/String;IJJJJ)V",
if (profileInfoClass == NULL || jenv->ExceptionCheck()) { ((jstring)(profileInfo.name==NULL?NULL:jenv->NewStringUTF(profileInfo.name))),
jenv->ExceptionDescribe(); /* write to console */\ ((jint)profileInfo.type),
jenv->ExceptionClear();\ ((jlong)profileInfo.start),
fprintf(stderr, "bummer! class\n"); ((jlong)profileInfo.end),
return(NULL); ((jlong)profileInfo.queued),
} ((jlong)profileInfo.submit));
jmethodID constructor= jenv->GetMethodID(profileInfoClass,"<init>","(Ljava/lang/String;IJJJJ)V");
if (constructor == NULL || jenv->ExceptionCheck()) {
jenv->ExceptionDescribe(); /* write to console */\
jenv->ExceptionClear();\
fprintf(stderr, "bummer getting constructor! \n");
return(NULL);
}
jlong start=profileInfo.start;
jlong end = profileInfo.end;
jlong queued = profileInfo.queued;
jlong submit= profileInfo.submit;
jobject profileInstance = jenv->NewObject(profileInfoClass, constructor, label, type, start, end, queued, submit);
if (profileInstance == NULL || jenv->ExceptionCheck()) {
jenv->ExceptionDescribe(); /* write to console */\
jenv->ExceptionClear();\
fprintf(stderr, "bummer invoking constructor! \n");
return(NULL);
}
return(profileInstance); return(profileInstance);
} }
JNIEXPORT jobject JNICALL Java_com_amd_aparapi_KernelRunner_getProfileInfoJNI(JNIEnv *jenv, jobject jobj, jlong jniContextHandle) { JNIEXPORT jobject JNICALL Java_com_amd_aparapi_KernelRunner_getProfileInfoJNI(JNIEnv *jenv, jobject jobj, jlong jniContextHandle) {
cl_int status = CL_SUCCESS; cl_int status = CL_SUCCESS;
JNIContext* jniContext = JNIContext::getJNIContext(jniContextHandle); JNIContext* jniContext = JNIContext::getJNIContext(jniContextHandle);
jobject returnList = NULL; jobject returnList = NULL;
if (jniContext != NULL && jniContext->isProfilingEnabled()){ if (jniContext != NULL){
returnList = createList(jenv, jniContext); returnList = createInstance(jenv, "java/util/ArrayList", "()V");
if (jniContext->isProfilingEnabled()){
for (jint i=0; i<jniContext->argc; i++){
KernelArg *arg= jniContext->args[i]; for (jint i=0; i<jniContext->argc; i++){
if (arg->isArray()){ KernelArg *arg= jniContext->args[i];
if (arg->isRead()){ if (arg->isArray()){
jobject writeProfileInfo = createProfileInfo(jenv, jenv->NewStringUTF(arg->name), arg->value.ref.write, 0); if (arg->isRead()){
add(jenv, jniContext, returnList, writeProfileInfo); jobject writeProfileInfo = createProfileInfo(jenv, arg->value.ref.write);
callVoid(jenv, returnList, "add", "(Ljava/lang/Object;)Z", writeProfileInfo);
}
} }
} }
}
jobject executeProfileInfo = createProfileInfo(jenv, NULL, jniContext->exec, 1); jobject executeProfileInfo = createProfileInfo(jenv, jniContext->exec);
add(jenv, jniContext, returnList, executeProfileInfo); callVoid(jenv, returnList, "add", "(Ljava/lang/Object;)Z", executeProfileInfo);
for (jint i=0; i<jniContext->argc; i++){ for (jint i=0; i<jniContext->argc; i++){
KernelArg *arg= jniContext->args[i]; KernelArg *arg= jniContext->args[i];
if (arg->isArray()){ if (arg->isArray()){
if (arg->isWrite()){ if (arg->isWrite()){
jobject readProfileInfo = createProfileInfo(jenv, jenv->NewStringUTF(arg->name), arg->value.ref.read, 2); jobject readProfileInfo = createProfileInfo(jenv, arg->value.ref.read);
add(jenv, jniContext, returnList, readProfileInfo); callVoid(jenv, returnList, "add", "(Ljava/lang/Object;)Z", readProfileInfo);
}
} }
} }
} }
/*
fprintf(stderr, " write end =%lu\n", arg->value.ref.write.end);
fprintf(stderr, " write start =%lu\n", arg->value.ref.write.start);
fprintf(stderr, " write submit=%lu\n", arg->value.ref.write.submit);
fprintf(stderr, " write queued=%lu\n", arg->value.ref.write.queued);
fprintf(stderr, " write =======%lu\n", arg->value.ref.write.end-arg->value.ref.write.start);
fprintf(stderr, " exec end =%lu\n", jniContext->exec.end);
fprintf(stderr, " exec start =%lu\n", jniContext->exec.start);
fprintf(stderr, " exec submit=%lu\n", jniContext->exec.submit);
fprintf(stderr, " exec queued=%lu\n", jniContext->exec.queued);
fprintf(stderr, " exec =======%lu\n", jniContext->exec.end-jniContext->exec.start);
fprintf(stderr, " read end =%lu\n", arg->value.ref.read.end);
fprintf(stderr, " read start =%lu\n", arg->value.ref.read.start);
fprintf(stderr, " read submit=%lu\n", arg->value.ref.read.submit);
fprintf(stderr, " read queued=%lu\n", arg->value.ref.read.queued);
fprintf(stderr, " read =======%lu\n", arg->value.ref.read.end-arg->value.ref.read.start);
*/
} }
return returnList; return returnList;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment