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?
// ReleaseHold
incompblock3
and observe the output. Your trick withHold
and/. DownValues@Condition1
is not doing anything because there are noDownValues
forCondition1
. You should precompileCondition1
. $\endgroup$Condition1Comp = Compile[{{func, _Real}, {i, _Integer}}, Condition1[func, i], CompilationTarget -> "C", RuntimeOptions -> "Speed"]
, is also not compilable. [:(]. $\endgroup$