uygn / aparapi

Automatically exported from code.google.com/p/aparapi
Other
0 stars 0 forks source link

Call to static method in containing class emits bad OpenCL #74

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Create a kernel as an anonymous inner class with a call to a static method 
in the containing class
2.
3.

What is the expected output? What do you see instead?
I think this should be detected and and exception thrown rather than generate 
bad OpenCL

Please use labels and text to provide additional information.

This is a modification of the squares sample code:

public class Main{

    public static int fooBar(int i) {
        return i;
    }   

   public static void main(String[] _args) {

      final int size = 64; 

      /** Input float array for which square values need to be computed. */
      final float[] values = new float[size];

      /** Initialize input array. */
      for (int i = 0; i < size; i++) {
         values[i] = i;
      }   

      /** Output array which will be populated with square values of corresponding input array elements. */
      final float[] squares = new float[size];

      /** Aparapi Kernel which computes squares of input array elements and populates them in corresponding elements of 
       * output array. 
       **/
      Kernel kernel = new Kernel(){
         @Override public void run() {
            int gid = getGlobalId();
            squares[gid] = values[gid] + fooBar(gid);
         }   
      };  

      // Execute Kernel.

      kernel.execute(Range.create(size));

      // Report target execution mode: GPU or JTP (Java Thread Pool).
      System.out.println("Execution mode=" + kernel.getExecutionMode());

      // Display computed square values.
      for (int i = 0; i < size; i++) {
         System.out.printf("%6.0f %8.0f\n", values[i], squares[i]);
      }   

      // Dispose Kernel resources.
      kernel.dispose();
   }   

}

Results in generating call to non-existant fooBar() with compile failure and 
fallback to JTP...

Oct 29, 2012 9:32:18 AM com.amd.aparapi.KernelRunner execute
INFO: typedef struct This_s{
   __global float *val$squares;
   __global float *val$values;
   int passid;
}This;
int get_pass_id(This *this){
   return this->passid;
}
__kernel void run(
   __global float *val$squares, 
   __global float *val$values, 
   int passid
){
   This thisStruct;
   This* this=&thisStruct;
   this->val$squares = val$squares;
   this->val$values = val$values;
   this->passid = passid;
   {
      int gid = get_global_id(0);
      this->val$squares[gid]  = this->val$values[gid] + (float)fooBar(gid);
      return;
   }
}

clBuildProgram failed
************************************************
"/tmp/OCLDFiZNg.cl", line 21: error: function "fooBar" declared implicitly
        this->val$squares[gid]  = this->val$values[gid] + (float)fooBar(gid);
                                                                 ^

1 error detected in the compilation of "/tmp/OCLDFiZNg.cl".

Internal error: clc compiler invocation failed.

************************************************

Oct 29, 2012 9:32:18 AM com.amd.aparapi.KernelRunner warnFallBackAndExecute
WARNING: Reverting to Java Thread Pool (JTP) for class 
com.amd.aparapi.sample.testStatic.Main$1: OpenCL compile failed
Oct 29, 2012 9:32:18 AM com.amd.aparapi.KernelRunner executeJava
FINE: executeJava: range = global:64 local:(derived)64
null:JTP
Execution mode=JTP
     0        0
     1        2
     2        4
...

Original issue reported on code.google.com by ecasp...@gmail.com on 29 Oct 2012 at 3:32

GoogleCodeExporter commented 9 years ago
Here is a patch to allow static calls to other classes and new tests for it.

Original comment by ecasp...@gmail.com on 31 Oct 2012 at 7:29

Attachments:

GoogleCodeExporter commented 9 years ago
Eric. 

Thanks for the patch. I will try to review it ASAP.  Would you consider adding 
a brief wiki page showing how this can be used and the constraints that the 
user needs to adhere to?

I think this will be real useful for folks wanting to create Util/Helper 
classes for common APIS. 

Gary

Original comment by frost.g...@gmail.com on 31 Oct 2012 at 8:32