﻿id	summary	reporter	owner	description	type	status	priority	milestone	component	version	resolution	keywords	cc
3339	ParModelica: Integer division does not work due to generated OpenCL-code not working	Gustaf Thorslund	Mahder Alemseged Gebremedhin	"As an attempt to work around #3335, or to get more threads from a parfor-loop, one may iterate over one range nx*ny and then unpack the x and y component using integer arithmetic. This, however, generates broken OpenCL-code for integer division. In the following code 'foo' is the test, while 'bar' is a not so nice workaround:

{{{#!mo
package DivTest
  constant Integer nx = 10;
  constant Integer ny = 10;
  
  function foo
    input Real a;
    output Real dst[nx,ny];
  protected
    parglobal Real parDst[nx,ny];
    parglobal Integer x;
    parglobal Integer y;
  algorithm
    parfor i in 1:nx*ny loop
      y := div((i-1), nx) + 1;
      x := i - (y-1) * nx;
      parDst[x,y] := x*y;
    end parfor;
    dst := parDst;
  end foo;

  function bar
    input Real a;
    output Real dst[nx,ny];
  protected
    Integer i2x[nx*ny];
    Integer i2y[nx*ny];
    parglobal Integer pi2x[nx*ny];
    parglobal Integer pi2y[nx*ny];
    parglobal Real parDst[nx,ny];
    parglobal Integer x;
    parglobal Integer y;
  algorithm
    for i in 1:nx, j in 1:ny loop
      i2x[i+(j-1)*nx] := i;
      i2y[i+(j-1)*nx] := j;
    end for;
    pi2x := i2x;
    pi2y := i2y;
    parfor i in 1:nx*ny loop
      y := pi2y[i];
      x := pi2x[i];
      parDst[x,y] := x*y;
    end parfor;
    dst := parDst;
  end bar;
end DivTest;
}}}

script:
{{{#!mo
setCommandLineOptions(""+d=noevalfunc +g=ParModelica -v=1"");
getErrorString();
loadFile(""DivTest.mo"");
getErrorString();

y:=DivTest.bar(4);
getErrorString();

x:=DivTest.foo(4);
getErrorString();
}}}

result:
{{{#!bash
.
 .
  .
- Select your device:      1
	 :Using flags -I""/home/gusth/src/OpenModelica/build/include/omc/c/""
Error building program:
CL_BUILD_PROGRAM_FAILURE 
Build failed: Errors detected in compilation of OpenCL code:
CL_PROGRAM_BUILD_LOG:  
/tmp/OCLL5HGyT.cl(41): error: function ""throwStreamPrint"" declared implicitly
      if (tmp1 == 0) {throwStreamPrint(threadData, ""Division by zero %s"", ""div(i + -1, 10)"");}
                      ^

/tmp/OCLL5HGyT.cl(41): error: identifier ""threadData"" is undefined
      if (tmp1 == 0) {throwStreamPrint(threadData, ""Division by zero %s"", ""div(i + -1, 10)"");}
                                       ^

/tmp/OCLL5HGyT.cl(42): error: function ""ldiv"" declared implicitly
      _y = ((modelica_integer) 1 + ldiv(((modelica_integer)_i + (modelica_integer) -1),tmp1).quot);
                                   ^

/tmp/OCLL5HGyT.cl(42): error: expression must have struct or union type
      _y = ((modelica_integer) 1 + ldiv(((modelica_integer)_i + (modelica_integer) -1),tmp1).quot);
                                   ^

4 errors detected in the compilation of ""/tmp/OCLL5HGyT.cl"".

Internal error: clc compiler invocation failed.
}}}"	defect	closed	normal	1.9.4	ParModelica	trunk	fixed		
