Skip to content

Commit

Permalink
Correct vectorisation errors recently introduced
Browse files Browse the repository at this point in the history
(1) vec_load cannot be used on constants, it can only be used to
access array elements. Replacing UseVectors by False is just a band
aid. vec_load was attached to a routine defining and setting a
variable; really, it should be attached to a routine accessing array
elements, but Kranc doesn't use such an abstraction yet.(2) ToReal is
introduced, but must be removed again for integer expressions such as
the conditions controlling if statements. I added band-aid code to
Conditional[] to remove it. The vectorization routines already handle
IfThen, Pow etc., but Conditional isn't visible to them.
  • Loading branch information
eschnett authored and ianhinder committed May 27, 2011
1 parent ce3f98a commit 431c5d7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Tools/CodeGen/CalculationFunction.m
Expand Up @@ -652,7 +652,7 @@ to do this outside all if(){} statements. "declare2" is a list
{DeclareVariables[localName/@#2, DataType[]],"\n",
Conditional[#1,
Table[AssignVariableInLoop[localName[var], GridName[var], useVectors], {var, #2}],
Sequence@@If[#3 =!= None, {Table[AssignVariableInLoop[localName[var], #3, useVectors], {var, #2}]}, {}]]},
Sequence@@If[#3 =!= None, {Table[AssignVariableInLoop[localName[var], #3, False (*useVectors*)], {var, #2}]}, {}]]},
(* else *)
{}] &,
{Map[#[[2]]&, conds], varsInConds, Map[#[[3]]&, conds]}], "\n"]}};
Expand Down
16 changes: 14 additions & 2 deletions Tools/CodeGen/CodeGen.m
Expand Up @@ -774,12 +774,24 @@
]}
]]]};

(* Remove call to ToReal from condtion. This should really instead be
done much earlier. Sometimes Conditional is called with a
conditional that is an expression, sometimes it is a list of
strings, so we need to handle both cases. *)
(* One approach to remove calls to ToReal is to wrap the condition
into a function call, such as e.g. Cond[x], which is then handled by
the vectorisation code in the same way the IfThen[x,y,z] expression
is handled there. *)
removeToRealFunc[condition_] := Replace[condition, {ToReal[x_] -> x}]
removeToRealString[condition_] := If[StringQ[condition], StringReplace[condition, "ToReal(" ~~ x__ ~~ ")" :> x], condition]
removeToReal[condition_] := Map[removeToRealString, removeToRealFunc[condition]]

Conditional[condition_, block_] :=
{"if (", condition, ")\n",
{"if (", removeToReal[condition], ")\n",
CBlock[block]};

Conditional[condition_, block1_, block2_] :=
{"if (", condition, ")\n",
{"if (", removeToReal[condition], ")\n",
CBlock[block1], "else\n", CBlock[block2]};

onceInGridLoop[block_] :=
Expand Down

0 comments on commit 431c5d7

Please sign in to comment.