Hot answers tagged

36 votes

How to compile effectively?

Leonid Shifrin has already given an excellent answer for the question but it's so… long and may be frustrating for someone just beginning to learn the usage of ...
xzczd's user avatar
  • 65.4k
32 votes

Strange behavior of Table

This is because Table automatically compiles its argument above a certain length limit. ...
Szabolcs's user avatar
  • 234k
27 votes

Why can Mathematica not use the compiled code?

This is a very subtle issue. It is important to Compile that it has always the same return type, no matter what happens at runtime. Note that ...
Henrik Schumacher's user avatar
27 votes

What does Compile[] do to make code run so much faster?

One of the biggest differences between main kernel evaluation and compiled evaluation in the "Wolfram Virtual Machine" (WVM) is that in the kernel, arbitrary expressions are allowed that are ...
Michael E2's user avatar
  • 234k
21 votes

Compile vs FunctionCompile

On my M1 Max processor and with Mathematica 13.2.1.0, the speed differences are even more severe: ...
Henrik Schumacher's user avatar
20 votes
Accepted

Why CompilationTarget->"C" doesn't improve computation time

Additional to your (maybe a bit too thorough) safety checks, Mathematica injects quite many more bound checks for the calls to Part in order to avoid segfaults. ...
Henrik Schumacher's user avatar
18 votes

Why is CompilationTarget -> C slower than directly writing with C?

Using Compile with CompilationTarget->"C" does generate C-Code to be compiled in a generalized way, the resulting code will ...
Albert Retey's user avatar
  • 23.6k
18 votes
Accepted

Writing compiled functions as fast as Python's Numba

Based on the experience obtained here: ...
xzczd's user avatar
  • 65.4k
17 votes
Accepted

Why can't I inject expressions in Compile using (only) With

Compile is considered a scoping construct by the outer With, and its bindings are protected. This will work: ...
Leonid Shifrin's user avatar
17 votes

What does Compile[] do to make code run so much faster?

Extended comment.. I've never come across specifics of what Compile[] specifically does. That said, a definition re: programming languages in general provides some ...
Jagra's user avatar
  • 14.3k
16 votes

Compile with a list as an argument

I think this is because a in your code represents a rank 10 tensor. If you want to use a as a vector, then I think it would be ...
xslittlegrass's user avatar
16 votes

Why is this compiled function 50x slower?

Here is the original code. logisticMap[x0_, μ_, n_] := Module[{i}, RecurrenceTable[{x[i + 1] == μ x[i] (1 - x[i]), x[1] == x0}, x, {i, 1, n}]] We'll ...
Daniel Lichtblau's user avatar
16 votes

Substitute for Association inside Compile?

For what it's worth, you can work with a HashTable as long as you're willing to put in a bit of effort ...
b3m2a1's user avatar
  • 46.8k
16 votes
Accepted

LLVM compilation in version 12.3

No expert on this, but will share what little I know about this. This is based on my understanding of the video Wolfram Language Version 12.3 Sneak Peek with Jon McLoone Here is a link What ...
Nasser's user avatar
  • 142k
16 votes

How to convert the ListVector into PackedArray in FunctionCompile

Edit Regarding the direct question in OP, i.e. how to flatten a "ragged" array inside FunctionCompile, I have to say there might be ready-to-use method ...
Silvia's user avatar
  • 27.5k
16 votes
Accepted

3D stable fluids algorithm based on FFT

Here's a 3D version of my answer: ...
xzczd's user avatar
  • 65.4k
16 votes
Accepted

When to use WVM instead of C for CompilationTarget?

IMHO, the only feasible usage cases of CompilationTarget -> "WVM" I can image are: You work on a device without a C compiler (e.g., when you just don'...
Henrik Schumacher's user avatar
15 votes
Accepted

Is it possible for LibraryLink function to be Listable like that in Compile?

Simple solution There is an easier solution than the one I gave almost 2 years ago. In principle, you wrap your library function inside another CompiledFunction ...
halirutan's user avatar
  • 113k
15 votes
Accepted

Goto and Compile to C

Almost 3 years later.. The problem lies in the Label instruction. What happens when you have code that contains labels and gotos is that the ...
halirutan's user avatar
  • 113k
15 votes
Accepted

Implementing a compilable Faddeeva function of complex argument

Preface I've put a FaddeevaM package on my GitHub account that will do all that follows automatically. It is a full package that exposes all (and not only the Faddeeva $w$) functions for Mathematica ...
halirutan's user avatar
  • 113k
15 votes
Accepted

How to use CompilerOptions?

These are accepted as valid rules for CompilerOptions -> { rules }: ...
Michael E2's user avatar
  • 234k
14 votes
Accepted

Can one compile the Switch function?

A drawback of the Which command is that it evaluates x repeatedly until one of the conditions of Which is satisfied. This is time consuming when x is a complicated expression or/and when the set of ...
Szabolcs's user avatar
  • 234k
14 votes
Accepted

Why is this compiled function 50x slower?

Your code is slow because You used the symbols t and i in the compiled function, but did not localise them. So they are global ...
Simon Woods's user avatar
  • 84.9k
14 votes

How to define a function inside a Compile?

This is covered by the 3rd rule in this post: Compiled evaluation can't work with patterns… Notice that function definition like f[x_] = … or ...
xzczd's user avatar
  • 65.4k
14 votes

Compile and uncompilable function bug?

If you declare the type of DuplicateFreeQ, it works consistently (in V11.1.1). ...
Michael E2's user avatar
  • 234k
14 votes

Using DataStructures in FunctionCompile

This is a bit too long for a comment, but below is a slightly modified version that uses the "DynamicArray" approach. The basic issue is that all the ...
ilian's user avatar
  • 25.5k
14 votes
Accepted

How to compile matrix product?

Compiling such linear algebra operatations does make some sense because we can give the compiler valuable information, e.g., that we use only $3 \times 3$ matrices. This allows the compiler to ...
Henrik Schumacher's user avatar
14 votes
Accepted

How can CompiledCodeFunctions be optimized and parallelized?

In general, the weak spots of interpreted languages are loop constructs and function calls. So I'd suggest to push also the Table into the compiled code to generate ...
Henrik Schumacher's user avatar
13 votes
Accepted

How to compile this code?

Compile is the toy of experienced Mathematica user. You'd better improve your understanding for the core language first. As to your specific question, your attempt ...
xzczd's user avatar
  • 65.4k
13 votes
Accepted

Efficiency in calculation on graphs: Compile?

The following should be 150 times faster. Too many micro changes to comment on all of them. Major speedup came from using Ordering to find ...
Henrik Schumacher's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible