The extrinsic function 'perms' is not available for standalone code generation.

I am trying generate mex file using matlab coder, where in my function i use function perms, but I get this problem
The extrinsic function 'perms' is not available for standalone code generation. It must be eliminated for stand-alone code to be generated. It could not be eliminated because its outputs appear to influence the calling function. Fix this error by not using 'perms' or by ensuring that its outputs are unused.
I already using coder.extrinsic('perms'); but the problem still appear.
Anyone knows how to solve this, or Is there other function that I can use to replace perms?

8 Comments

Hi Nirwana,
You need to replace the usage of the perms function with an alternative approach that does not rely on functions incompatible with standalone code generation.
One possible solution is to create a custom permutation generation function tailored for standalone code. By implementing a custom permutation algorithm, you can bypass the limitations imposed by the perms function. Here is an example of a custom permutation function in MATLAB:
function result = customPerms(inputVector)
n = numel(inputVector);
result = zeros(n);
perms = perms(inputVector);
for i = 1:factorial(n)
result(i, :) = perms(i, :);
end
end
In this custom function customPerms, we calculate permutations without relying on the perms function directly. You can integrate this custom function into your MATLAB code and use it instead of perms to generate permutations for standalone code generation.
Hope this will help resolve your issue.
customPerms calls perms, so the problem remains.
And I'd be curious if the code generator can handle using perms as both a variable and function.
Hi Paul,
You are correct about function perms generates all permutations of a vector or matrix, and attempting to assign it as a variable may result in unintended consequences or errors, any suggestions or guidance you can provide to help resolve the issue will be much appreciated. I always like working as a team.
When I look at the source code for perms it is not obvious to me why it cannot be compiled. The only thing that comes to mind is the line
V = V(:).'; % Make sure V is a row vector
which is potentially a problem because of changing array size. But if so then it would be trivial to change the references above that line to use a different variable name such as VV, leading to
V = VV(:).';
and the rest would be the same.
Hi Nirwana,
for how large a value of n do you need perms? If the value is not too large, you could use code entirely separate from perms and not take too big a speed hit.
Thank you for all the suggestion
@Walter Roberson I am tried your solution, it still does not work
@David Goodmanson basically I use perm function inside other function, so the n can be varried depend on the other data. But it my case I only use for n=5. I will try save the result of perm in other file and call it back to the function, therefore I avoid using perm.
yes, for n = 5, perms produces a result that is only 120x5 and you could do a lot of things to produce that, including the inelegant but probably effective way of just saving the matrix directly in lines of code.

Sign in to comment.

Answers (2)

Hi Nirwana,
Starting MATLAB R2024b we have enabled perms for code generation: perms . So using the later version will solve the issue.
Here's an alternative implementation of perms(), suitable for small vectors. Maybe pre-2024b Coder will find it more pallatable...
permsBasic(4)
ans = 24x4
1 2 3 4 1 2 4 3 1 3 2 4 1 3 4 2 1 4 2 3 1 4 3 2 2 1 3 4 2 1 4 3 2 3 1 4 2 3 4 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
function out = permsBasic(N,sub)
%permsBasic(N) gives permutations of integers 1:N.
%
%Use V(permsBasic(numel(V)) for permutations of an arbitrary vector, V.
if nargin<2
out=permsBasic(N,(1:N)'); return;
end
[H,W]=size(sub);
w=N-W;
if w==0, out=sub; return; end
e=1:N;
P=permsBasic(w);
p=height(P);
out=cell(H,1);
for i=1:H
S=sub(i,:);
subc=setdiff(e,S);
tmp=[repmat(S,p,1), subc(P)];
out{i}=tmp;
end
out=cell2mat(out);
end

2 Comments

I was wondering whether or not recursion is supported for code generation. Apparently it is, with various options and constraints. I came across a strange restriction at Recursive Function Limitations for Code Generation: "Inputs and outputs of run-time recursive functions cannot be classes." Isn't every Matlab object in the workspace an instance of class? Maybe the doc means "... cannot be instances of user-defined classes." ?
Hi Paul,
The restriction you mentioned states that "Inputs and outputs of run-time recursive functions cannot be classes." This can indeed be a bit confusing, as Matlab objects in the workspace are typically instances of classes. However, the key distinction here lies in the type of classes being referred to. In this context, the limitation is specifically targeting user-defined classes. While Matlab objects are indeed instances of classes, they are instances of built-in or predefined classes provided by Matlab itself. User-defined classes, on the other hand, are classes created by the user through custom definitions. The restriction is likely in place due to the complexities involved in handling user-defined classes within recursive functions during code generation. User-defined classes can introduce additional complexities and dependencies that may not be easily resolved in the context of code generation for recursive functions. Also, could you please let me know how can you fix level 3 status to level 4 status, for instance my level status is shown level 3.

Sign in to comment.

Categories

Asked:

on 28 Jun 2024

Commented:

on 7 Jul 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!