Making Jacket Code Run On Plain MATLAB Installations
From Jacket Wiki
One of the most important things when programming in Jacket is the potential lack of compatibility with a plain MATLAB installation. When making for example toolboxes for Jacket, which should also be able to run on a plain MATLAB installation some things must be done. Jacket introduces some new commands and it is at least necessary to ensure that these commands fall back to the corresponding MATLAB functions. To do this a small library, Jacket.lib has been made. A very special function such as Jacket's 'gfor' needs some special consideration, which is shown below.
Contents |
Jacket Library
Since Jacket introduces several new commands otherwise unknown to MATLAB it is necessary to do something about these. An easy way is to define functions for the Jacket reserved commands such as 'gsingle', 'gzeroes' etc. A Jacket library has been made to ensure these functions are known to a plain MATLAB installation. The only thing to do is to unpack the Jacket library to a folder and ensure that this is permanently made a part of the MATLAB installation. An easy way to do this in case the library is installed in d:/home/tl/libraries/Jacket.lib is to open MATLAB and use the two commands:
>> addpath c:/home/tl/libraries/Jacket.lib >> savepath
The savepath command ensures that the folder is permanently included in the MATLAB search path. The functions defined in the library simply uses the corresponding MATLAB function rather than the Jacket function. This ensures that the code will run - also on a plain MATLAB installation. The library can be downloaded here.
GFOR/GEND Function
The GFOR/GEND function poses a special problem as it can't just be replaced by functions. However, there is a way to overcome this obstacle, which is to use the TRY/CATCH functions. Let's put the following example of code into a file named myfunction.m:
function [ y ] = myfunction( x ) %% TEMPORARY VARIABLES N = length(x); y = gzeros(N,N); %% COMPUTATION tmp = grandn(N,N); try % JACKET CASE gfor ii=1:N y(:,ii) = x(ii)*tmp(:,ii); gend fprintf('\nJacket computing ...\n'); catch % MATLAB CASE for ii=1:N y(:,ii) = x(ii)*tmp(:,ii); end fprintf('\nMATLAB computing ...\n'); end end
This use of TRY/CATCH is one way to allow the code to run in a plain MATLAB installation and still give the possibility to use the GFOR/GEND commands. Unfortunately it is not nice coding - but it does work and it is one way to ensure that the developed code can run on different platforms - even with just a plain MATLAB installation.
Example: Jacket Installed
This small example uses the myfunction.m above to show that the library and GFOR/GEND trick does indeed work. If Jacket is available we have the following:
>> ginfo Jacket v1.4.1 (build 6737) by AccelerEyes License Type: Designated Computer (on 32-bit Mac OS X) Licensed Addons: No Addons Detected! Multi-GPU: Licensed for 2 GPUs Detected CUDA-capable GPUs: CUDA driver 19.5.9f02, CUDA toolkit 3.1 GPU0 GeForce GT 330M, 1100 MHz, 512 MB VRAM, Compute 1.2 (single) GPU Memory Usage: 382 MB free (512 MB total) >> x = grandn(10,1,'single'); x' ans = 2.1626 0.7621 0.9274 1.8957 -0.7468 0.9599 0.6924 0.1901 -1.2785 -1.1472 >> y = myfunction( x ) Jacket computing ... y = 3.2042 -1.2283 -0.3375 0.8893 0.5288 0.1733 0.0064 -0.1654 -0.4276 0.8859 5.7504 0.3077 1.0265 -1.9648 0.6711 0.4354 -0.1459 0.2798 -1.2675 0.9759 -3.2549 0.4101 -0.5354 1.0774 -0.0549 0.7183 0.3287 0.0646 2.9600 -0.2708 1.7891 -1.6792 -0.2248 0.9234 -0.7836 0.1993 -0.6060 0.0046 -0.0700 2.0725 -0.6211 0.1747 -1.0443 0.5332 0.1155 -0.7408 -0.0569 0.1452 0.5116 1.3894 1.8639 0.8855 1.9853 -1.2017 0.0775 -0.1741 -0.4515 0.1023 -0.7355 -0.8328 0.8093 0.9957 -0.0933 -0.1231 0.2990 -0.4347 -0.6571 -0.0319 -0.3155 0.6764 -2.7700 -1.0635 0.0093 2.6764 -0.7713 0.2543 -0.2356 -0.0081 -2.6811 -0.4587 -3.6001 1.5515 -0.2667 -0.2351 0.2754 -0.4154 -0.9847 0.1150 -2.5300 -0.6860 1.5661 0.3718 0.0797 0.5676 -0.9290 -1.8011 -0.7388 -0.3927 -0.7950 0.2821 >> whos Name Size Bytes Class Attributes ans 1x10 928 gsingle x 10x1 436 gsingle y 10x10 436 gsingle >>
As seen above the Jacket functions are used and the GFOR/GEND loop is used. Also the variables are of Jacket type.
Example: Plain MATLAB
When using our myfunction on a plain MATLAB installation, the first step is to include the Jacket library in the MATLAB path. After running the addpath command you may use the savepath command to ensure that the Jacket library is permanently a part of the path.
>> addpath /Users/tl/Dropbox/Jacket/Torbens_Corner/Tips_and_Tricks/Jacket' and Matlab Coexistence'/Jacket.lib/ >> savepath >> x = grandn(10,1,'single'); x' ans = -0.5320 1.6821 -0.8757 -0.4838 -0.7120 -1.1742 -0.1922 -0.2741 1.5301 -0.2490 >> y = myfunction( x ) MATLAB computing ... y = 0.5662 0.6592 0.9341 0.3552 -0.7120 -0.3840 -0.1752 0.0067 0.1223 -0.2236 -0.8531 -2.1038 -0.8177 0.0149 1.1849 -1.2712 -0.1143 0.5341 -1.4512 0.0329 -0.6569 -1.5946 -0.3068 -0.1124 0.4201 -1.1813 -0.0673 -0.2797 0.6296 0.0367 0.1222 -1.2466 0.0254 -0.2063 0.1980 0.7643 -0.2403 -0.2362 1.0358 -0.2510 0.8013 -0.8542 -0.1598 0.1804 -0.3010 -0.3018 -0.1787 -0.0003 1.3124 0.5288 0.2365 -0.5392 1.3706 0.1144 1.1892 1.1089 -0.0461 0.0194 -1.0575 0.1257 0.0830 0.0210 0.0740 -0.9791 -0.3358 1.5521 0.1327 0.6814 0.6876 0.3164 -0.1469 -5.0954 -1.4046 1.0926 0.8636 -1.0859 0.1253 -0.1593 0.1540 0.0953 0.1389 -0.7687 -0.0861 -1.0786 -0.0471 -0.0001 -0.2292 0.6009 1.2639 -0.1615 -0.2359 2.0899 -0.0362 -0.1633 -0.4645 0.0645 0.3099 0.6356 0.8204 -0.2056 >> whos Name Size Bytes Class Attributes ans 1x10 40 single x 10x1 40 single y 10x10 800 double >>
As seen we use the Jacket library, and the code still works as it is, which is precisely what we intended.
Conclusions
To have Jacket code which is compatible with a plain MATLAB installation is important to many - not least if toolboxes etc. are made. This page presents a library of Jacket functions, which makes a plain MATLAB installation fall back to just use MATLAB for the computations. Further is it shown how the efficient GFOR/GEND functions of Jacket can be used without violating plain MATLAB functionality.
Go Home: Torben's Corner