1
$\begingroup$

Consider some functions

Condition1[func_, 1] = Boole[func > 10^-5.];
Condition1[func_, 0] = Boole[func^2 > 10^-3.];
Condition1[func_, 2] = Boole[func^3 > 10^-3.];
Condition1[func_, 3] = Boole[func > 0.001];

depending on a variable func and an integer. The following test code

idlist = RandomInteger[{0, 3}, 100];
compblock3 = 
 Hold@Compile[{{idlist, _Integer, 1}}, 
     Module[{count, id1}, 
      count = 0.; 
      Do[
       id1 = idlist[[i]];
       count += Condition1[10^-3, id1], {i, 1, Length[idlist], 1}];
      {count}], CompilationTarget -> "C", RuntimeOptions -> "Speed", 
     RuntimeAttributes -> {Listable}, Parallelization -> True] /. 
   DownValues@Condition1 // ReleaseHold

compblock3[idlist]

compiles without errors. However,

<< CompiledFunctionTools`
CompilePrint@compblock3

shows problems:

    1 argument
    9 Integer registers
    4 Real registers
    3 Tensor registers
    Underflow checking off
    Overflow checking off
    Integer overflow checking off
    RuntimeAttributes -> {Listable}

    T(I1)0 = A1
    I3 = 0
    I6 = 10
    I0 = 1
    I7 = 3
    R0 = 0.
    Result = T(R1)2

1   R2 = R0
2   I1 = Length[ T(I1)0]
3   I2 = I3
4   goto 12
5   I4 = Part[ T(I1)0, I2]
6   I8 = Power[ I6, I7]
7   R1 = I8
8   R3 = Reciprocal[ R1]
9   R1 = MainEvaluate[ Hold[Condition1][ R3, I4]]
10  R3 = R2 + R1
11  R2 = R3
12  if[ ++ I2 <= I1] goto 5
13  T(R1)2 = {R2}
14  Return

Namely, Condition1 is not compilable. I do not understand what is the reason for this.

If replacing count += Condition1[10^-3, id1] with e.g. count += Condition1[10^-3, 1], the code compiles well.

In some realistic codes where I use Condition1, this leads to errors like calls ordinary code that can be evaluated on only one thread at a time.

Could you please tell me how to avoid the issue?

$\endgroup$
2
  • $\begingroup$ Remove final // ReleaseHold in compblock3 and observe the output. Your trick with Hold and /. DownValues@Condition1 is not doing anything because there are no DownValues for Condition1. You should precompile Condition1. $\endgroup$
    – Domen
    May 7, 2023 at 20:26
  • $\begingroup$ @Domen : my attempt to compile, Condition1Comp = Compile[{{func, _Real}, {i, _Integer}}, Condition1[func, i], CompilationTarget -> "C", RuntimeOptions -> "Speed"], is also not compilable. [:(]. $\endgroup$ May 7, 2023 at 20:30

1 Answer 1

5
$\begingroup$

You are providing several DownValues for Condition1 which you are trying to inject into your held code. But how do you expect multiple definitions to be used "simultaneously" for one call?

Instead of multiple downvalues for Condition1, use Switch which is compilable. However, note that you have to provide a "default" value with _ or it will not compile.

Clear[Condition1];

Condition1[func_, id_] := 
 Switch[id, 0, Boole[func^2 > 10^-3.], 1, Boole[func > 10^-5.], 2, 
  Boole[func^3 > 10^-3.], 3, Boole[func > 0.001], _, 0]

compblock3 = Hold@Compile[...] /. DownValues@Condition1 // ReleaseHold
$\endgroup$
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.