Does Numba support for multiple dictionary multiplication in terms of `multi_dot`? - sparse-matrix

I need to multiply three 2-D matrices of size 25x25, 25x60, and 60x60 to get a result of size 25x60 using numpy. For fast multiplication, I wanted to use from numpy.linalg import multi_dot and also tried to parallely execute it in GPU using #jit(nopython=True) instruction.
However, in my program I receive the following numba error.
TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Untyped global name 'multi_dot': cannot determine Numba type of <class 'function'>
This error is not occuring when I am not using the #jit(nopython=True) command above the function definition i.e. sequentially it is working fine for me.

Related

How do I generate a string array in MatLab 2016b? [duplicate]

When trying to run my code, for example
for ii= 1:10
output(ii)=rand(3);
end
I get the error
In an assignment A(:) = B, the number of elements in A and B must be the same
or
In an assignment A(I) = B, the number of elements in B and I must be the same.
What does this error mean? What is the approach to get rid of it?
This error comes because you are trying to fill a variable chunk with more (or less) values than its size. In other words, you have a statement A(:)=B on where size(A(:)) is different to size(B).
In the example in the question, rand(3) returns a 3x3 matrix, however, output(ii) is just a single value (even if output may be bigger, output(ii) is just a single value of output), thus the value returned by rand(3) does not fit inside output.
In order to solve this problem, you need to change the the size of the output variable, so you have space to fit all the result.
There are 2 ways of doing this. One of them is by creating a Matrix that fits the return, e.g. output=zeros(3,3,10).
Then we can change the code to
for ii= 1:10
output(:,:,ii)=rand(3);
end
Alternatively, you can fill the output as a cell array. This is particularly useful when the return of the function changes sizes each time, e.g. rand(ii);
In that case, the following would work
for ii= 1:10
output{ii}=rand(ii);
end
It is probable that unlike in the example in the question, in the real case you do not know the size of what the output returns, thus you do not know which of the two options to use to fix your code.
On possible way of learning that, is activating debugging help when the code errors, by typing dbstop if error in your command line. This will trigger a debugging stop when MATLAB throws an error, and you can type size(rand(ii)) and size(output(ii)) to see the sizes of both.
Often, reading the documentation of the function being used also helps, to see if different sizes are possible.
That said, the second option, cell arrays, will always ensure everything will fit. However matrices are generally a faster and easier to use in MATLAB, thus you should aim for the matrix based solution if you can.

Problem when trying to generate a psuedo-random array/matrix from normally distributed numbers

I am trying to time the generation of psuedo-random arrays in ipython, using random.gauss() and list comprehension in a ubuntu terminal but it kills the environment after pausing for a while the environment is killed and returns to root. I'm doing this to time the difference between a pure Python approach vs using Numpy.
tried on ubuntu VM and Windows.
import random
I = 5000
mat = [[random.gauss(0, 1) for j in range(I)] for i in range(I)]
expected a array with a shape of 5000x5000 instead get killed.
Very large overhead to use standard python for such kind of things (after generation you have to operate on it, right?)
Please, use NumPy
import numpy as np
q = np.random.normal(size=(5000,5000))
print(q.shape)
that was pretty much instant

Using Matlab Coder generated algorithm for Production

I have a fine tuned algorithm in MATLAB that operates on matrices (ofcourse). I've used matlab coder to generate c code for this algorithm and it works as expected.
Here's a function call that I used in Matlab
x = B/A
wherein
B is of size 1*500 (rows * columns)
A is of size 10*500
x, the result is of size 1*10
When this is converted into C source using Matlab Coder. I noticed that the function definition accepts parameters that are same as above sizes.
void myfunction(const double B[500], const double A[5000], double x[10])
For prototype and testing purposes this seems okay. However, in production I prefer to have this function be used for different sizes too. For example 100 instead of 500 in above mentioned variables should also work. How can I remove dependence of matrix dimensions in my algorithm ?
Additionally, there are few lines of code that use hard coded integers. For example, there is code like
if (rankR <= 1.4903363393874656E-8)
// Some internal function calls
else
// Usage of standard sqrt
or
500.0 * fabs(A[0]) * 2.2204460492503131E-16
Could any one explain what are these hard coded integers ? Are these generated from the test data that I've used in MATLAB ?
If the function call you refer to is the entry-point function, you can define the size when setting up Coder. The simplest way to run the Coder is using the GUI from the 'Apps' menu inside MATLAB (or type 'coder' at the console). After specifying the entry-point function, step 2 is to define the type and size for each of the input variables.
For each dimension of your input variable (there can be more than 2 if necessary), you can specify the:
n - dimension is exactly n long
:n - dimension is up to n long
inf - dimension is unbounded
If the function call is not the entry-point function, and is buried inside your code (or if you are running the codegen function from the console), you can explicitly define variables as being of varying size:
coder.varsize('myVariableName');
Bear in mind that some functions can only be used (with Coder) with fixed-sized inputs.
Fuller description here:
http://uk.mathworks.com/help/fixedpoint/ug/defining-variable-size-data-for-code-generation.html#br9t627
Not sure about the random constants unfortunately.

Understand SLU_SC format matrix and convert to normal CSR

The SLU_SC in SuperLU is defined as "Supernode, column-wise". It's generally the result of running SuperLU on a sparse matrix.
I would like to inspect this matrix instead of solving for a vector so the CSR / COO format would be nice. Any pointer on how to do it ?
If you are fine with using a high level language interface to SuperLU you can use Python together with scipy. You can call the spilu method and then access and convert the L and U of the resulting SuperLU object to all the standard sparse matrix formats.

How to identify places in MATLAB where data is stored outside the bounds of an array?

I am trying to use MATLAB Coder to convert code from Matlab to a MEX file. If I have a code snippet of the following form:
x = zeros(a,1)
x(a+1) = 1
then in Matlab this will resize the array to accommodate the new element, while in the MEX file this will give an "Index exceeds matrix dimensions" error. I expect there are lots of places in the code where this is happening.
What I want to do is run the MATLAB version of the code (without using the coder) but have MATLAB generate an error or warning whenever it resizes an array because I assign to something outside the bounds. (I could just use the MEX file and see where the errors pop up, but this requires rebuilding the whole MEX file using MATLAB Coder every time I change the code at all, which takes a while.)
Is there a way to do this? Is there any kind of setting in MATLAB that will turn off the "automatically resize if you assign to an out-of-bounds index", or give a warning if this happens?
EDIT: As of Matlab 2015b, Coder now has runtime error checking as an option (from the Matlab release notes):
In R2015b, generated standalone libraries and executables can detect and report run-time errors such as out-of-bounds array indexing. In
previous releases, only generated MEX detected and reported run-time
errors.
By default, run-time error detection is enabled for MEX. By
default, run-time error detection is disabled for standalone libraries
and executables.
To enable run-time error detection for standalone
libraries and executables:
At the command line, use the code
configuration property RuntimeChecks.
cfg = coder.config('lib'); % or
'dll' or 'exe'
cfg.RuntimeChecks = true;
codegen -config cfg myfunction
Using the MATLAB Coder app, in the project build settings,
on the Debugging tab, select the Generate run-time error checks check
box.
The generated libraries and executables use fprintf to write
error messages to stderr and abort to terminate the application. If
fprintf and abort are not available, you must provide them. Error
messages are in English.
See Run-Time Error Detection and Reporting in Standalone C/C++ Code and Generate Standalone Code That Detects and Reports Run-Time Errors.
Original answer:
The answer in the comments regarding declaring a class subclassed from double wherein the subsref method is overloaded to disallow growing would be one good way to do it.
Another easy way to do it is to sprinkle assert commands throughout the code (in each loop iteration or at the bottom of a function) to assert that the size has not increased over the allocated size.
For instance, if you had the code in a format of:
x = zeros(a,1)
x(a+1) = 1
... lots of other operations
if coder.target('MATLAB')
assert(isequal(size(x), [a,1]), 'x has been indexed out of bounds')
end
this would let you have the assertion fail if any value were assigned that extended the array.
To make this a bit tidier, you might even make a function that bounds checked all the variables you care about, again wrapping the coder.target if statement around it. Then you could sprinkle this throughout your code.
It's not as elegant as overloading the double class, but on the other hand it doesn't add any overhead to the compiled code at all. It also won't give you errors exactly when the overrun happens, but it will give you confidence that the code is working well in a variety of situations.
Another way that you can be more confident of assignments is to do your own bounds checking on the assignment in situations where this may be appropriate. A common problem I've seen in assignment goes something like this. We have an allocated array and are copying in data from another array with a vector assignment. For instance, consider the following situation:
t = char(zeros(5,7)); % Allocate a 5 by 7 char array
tempstring = 'hello to anyone'; % Here's a string we want to put into it.
t(1, 1:numel(tempstring)) = tempstring; % A valid assignment in MATLAB
>> size(t)
ans =
5 15
Uh oh, exactly what you're concerned about in the question has happened: the t array has been automatically resized during the assignment, which works in MATLAB, but in Coder created code will cause a segfault or MEX error. An alternative is to use the power of the end function to keep the assignment tidy (but truncated.) If we change the assignment to:
t(1,1:min(end,numel(tempstring))) = tempstring(1:size(t, 2));
The size of t will remain unchanged, but the assignment will be truncated. The use of end allows bounds checking during the assignment. For some situations, this can be a nice way of handling the issue and will give you confidence that the bounds will never be exceeded, but obviously in some situations this is very undesirable (and won't give you error messages in MATLAB.)
Another helpful tool MATLAB provides is in the editor itself. If you use the %#codegen tag in your code, it will signal the editor's syntax checker to highlight various code generation problems, including places where you are obviously increasing the size of an array by indexing. This can't catch every situation, but it's a good help.
One last note. As mentioned in the question, a MEX file generated by Coder will give you an "Index exceeds matrix dimensions" error right at the time of the assignment, and will gracefully exit and even tell you the original line of code where the error occurred. A C library generated out of Coder has no such nice behavior or bounds checking and will segfault out completely with no diagnostics. The intermediate answer is to do exactly what you're doing, which is to run the code as a MEX. That's not very helpful to your question (as you say, rebuilding the MEX can take time), but for those of us who code for the cold, cruel world of external C code, the intermediate test of being able to run the MEX to find these errors is a lifesaver.
The bottom line is this is a divergence in behavior between MATLAB and Coder generated C code, and it can be a source of significant problems. In my own code, I am very careful with array access and growth for exactly this reason. It's an area where I'd like to see improvement in the Coder tool itself, but there are ways to be very careful when writing MATLAB code targeted for Coder.

Resources