<?xml version="1.0" encoding="utf-8"?><!DOCTYPE article  PUBLIC '-//OASIS//DTD DocBook XML V4.4//EN'  'http://www.docbook.org/xml/4.4/docbookx.dtd'><article><articleinfo><title>SpmWithPentium4</title><revhistory><revision><revnumber>5</revnumber><date>2013-03-07 21:24:02</date><authorinitials>localhost</authorinitials><revremark>converted to 1.6 markup</revremark></revision><revision><revnumber>4</revnumber><date>2006-07-25 14:29:31</date><authorinitials>devel03.mrc-cbu.cam.ac.uk</authorinitials></revision><revision><revnumber>3</revnumber><date>2006-07-25 14:27:19</date><authorinitials>devel03.mrc-cbu.cam.ac.uk</authorinitials></revision><revision><revnumber>2</revnumber><date>2006-07-25 14:26:12</date><authorinitials>devel03.mrc-cbu.cam.ac.uk</authorinitials></revision><revision><revnumber>1</revnumber><date>2006-07-04 15:00:17</date><authorinitials>Scripting Subsystem</authorinitials></revision></revhistory></articleinfo><section><title>Configuring Pentium 4 machines for SPM</title><para>A well-tuned pentium 4 machine is a very <ulink url="https://imaging.mrc-cbu.cam.ac.uk/imaging/SpmWithPentium4/imaging/SpmBenchmarks#">fast platform for running SPM</ulink>. However, there are a couple of flies in this sweet-smelling ointment. The first fly is of moderate size, and I will refer to this as <emphasis>the NaN problem</emphasis>, for reasons that will soon be obvious. The second is a little more obscure; this is <emphasis>the Sparse Matrix problem</emphasis>. The NaN problem means that pentiums can be about 30 times slower than they should be for some aspects of SPM processing, such as writing F contrast images. The problem can be solved by recompiling some SPM and matlab functions. The sparse matrix problem results in moderately slow performance on statistical estimation. The solution is a very small change to an SPM matlab function. </para><para>Lastly, you might consider <link linkend="maxmem">editing the SPM memory setting</link> to match your system. </para><para>The end result of all this work is shown in <link linkend="config_bench">the SPM benchmarks</link> on a Pentium 4 system before and after optimization. </para><para><anchor id="short_simple"/> </para><section><title>The short simple version</title><para>To cut to the chase, you will need a new version of a matlab math library, and the SPM mex files. If you don't want to wade through the rest of this document, you could follow this simple recipe:For Linux: </para><itemizedlist><listitem><para>Download the <ulink url="http://imaging.mrc-cbu.cam.ac.uk/downloads/Intel_p4/atlas_P4SSE2_gcc.so.gz">Linux precompiled library</ulink> to the [matlab] bin/glnx86 directory, and uncompress with gzip. </para></listitem><listitem><para>Download the <ulink url="http://imaging.mrc-cbu.cam.ac.uk/downloads/Intel_p4/mex_spm99_p4_gcc.tgz">SPM99 mex files</ulink> or <ulink url="http://imaging.mrc-cbu.cam.ac.uk/downloads/Intel_p4/mex_spm2_p4_gcc.tgz">SPM2 mex files</ulink> to your spm directory, and uncompress with gzip and tar. Note that these files have been compiled for matlab 6.5 </para></listitem></itemizedlist><para>For Windows (sigh): </para><itemizedlist><listitem><para>Download the <ulink url="http://imaging.mrc-cbu.cam.ac.uk/downloads/Intel_p4/atlas_P4SSE2_win32.zip">Windows precompiled library</ulink> to the [matlab] bin/win32 directory, and uncompress with <ulink url="https://imaging.mrc-cbu.cam.ac.uk/imaging/SpmWithPentium4/imaging/WinZip#">WinZip</ulink> or similar. </para></listitem><listitem><para>Note that SPM2 is distributed with Windows mex files for the Pentium 4, compiled with gcc; these are contained in the file ; to use them you should unzip the contained .dll files into a directory which is earlier on your matlab path than the SPM directory. If you are using SPM99, you may need to do one of the following: </para></listitem><listitem><para>Either: download the <ulink url="http://imaging.mrc-cbu.cam.ac.uk/downloads/Intel_p4/mex_spm99_p4_win32.zip">Windows gcc precompiled mex files</ulink> to your spm directory, and uncompress with <ulink url="https://imaging.mrc-cbu.cam.ac.uk/imaging/SpmWithPentium4/imaging/WinZip#">WinZip</ulink> or similar. Compiled for matlab 6.5 again. </para></listitem><listitem><para>or: download the <ulink url="http://imaging.mrc-cbu.cam.ac.uk/downloads/Intel_p4/icl_spm_dlls.zip">Windows Intel compiler precompiled mex files</ulink>, kindly provided by Chris Rorden. Download to your spm directory, and uncompress with <ulink url="https://imaging.mrc-cbu.cam.ac.uk/imaging/SpmWithPentium4/imaging/WinZip#">WinZip</ulink> or similar. There are versions for spm99 and spm2b, and the files were compiled for matlab 5.3 - so should work for any version &gt;=5.3 </para></listitem></itemizedlist><para>For both: </para><itemizedlist><listitem><para><link linkend="tell_matlab">Tell matlab about the new libraries</link>. </para></listitem><listitem><para>For SPM99, apply the <link linkend="sparse_fix">sparse/full</link> and <link linkend="maxmem">max memory</link> edits to the SPM99 file spm_spm.m. </para></listitem></itemizedlist><para>That's it. Your analyses will be quicker, your love life will be better, and your foreign policy objectives will be achievable by diplomacy. </para><para>Here is the long complex version: </para><para><anchor id="nan_problem"/> </para></section><section><title>The NaN problem</title><para>The default floating point calculations on pentiums 3 and 4 are very slow with not-a-number values (<ulink url="https://imaging.mrc-cbu.cam.ac.uk/imaging/SpmWithPentium4/imaging/NaNs#">NaNs</ulink>). This is important because SPM uses NaN values as markers for voxels for which it has not calculated any statistics. The problem can be solved by reconfiguring SPM and matlab. As a side-effect, the new configuration will run faster than the old across the board. This page collects various messages and links I found useful for this process. As the links will show, the solutions were worked out by Satra Ghosh, Chris Rorden and Tom Womack. <anchor id="nan_problem"/> <link linkend="matlab_nan_test">matlab test program</link>. The <link linkend="nan_test_output">output from the test program</link> shows that matrix multiplication is over seven hundred times slower for a matrix of <ulink url="https://imaging.mrc-cbu.cam.ac.uk/imaging/SpmWithPentium4/imaging/NaNs#">NaNs</ulink> than a matrix of ones. This explains why the pentium can be so slow to write F contrast images. F contrasts involve matrix multiplication on the output from SPM statistics, which are full of <ulink url="https://imaging.mrc-cbu.cam.ac.uk/imaging/SpmWithPentium4/imaging/NaNs#">NaNs</ulink>. </para><para>You can show that this problem is not restricted to matlab, using simple programs written in <link linkend="c_nan_test">C</link> or <ulink url="http://www.cla.sc.edu/psyc/faculty/rorden/simd.html">Delphi</ulink>. Luckilt the problem with <ulink url="https://imaging.mrc-cbu.cam.ac.uk/imaging/SpmWithPentium4/imaging/NaNs#">NaNs</ulink> does not occur with some optimized floating point operations on the pentium 4. Specifically, the <ulink url="http://www.tom.womack.net/x86FAQ/faq_features.html">SSE2</ulink> instructions on the pentium 4 are the same speed for <ulink url="https://imaging.mrc-cbu.cam.ac.uk/imaging/SpmWithPentium4/imaging/NaNs#">NaNs</ulink> as they are for other floating point values. So, the NaN problem can be solved by recompiling the matlab math libraries and SPM C files to use SSE2 instructions. As a pleasant side-effect, the recompiled SPM C files are quicker than the standard versions - see the <link linkend="config_benchmarks">benchmarks for different configurations</link> below. </para></section><section><title>Recompiling the Matlab math routines</title><para>Since version 6, Matlab has used highly optimized external math libraries for most computations. These libraries contain <ulink url="http://www.netlib.org/blas/">BLAS</ulink> and <ulink url="http://www.netlib.org/lapack/">LAPACK</ulink> routines that provide dramatic speed gains. In fact the library that matlab uses are from the open source <ulink url="http://math-atlas.sourceforge.net/">ATLAS</ulink> project. So, we can solve the NaN matrix multiplication problem by providing Matlab with versions of these libraries that use the appropriate pentium instructions to do the multiplication. There are two options: </para><orderedlist numeration="arabic"><listitem><para>Recompile the ATLAS libraries </para></listitem><listitem><para>Use the Intel Math Kernel library </para></listitem></orderedlist><section><title>Recompile the ATLAS libraries</title><para>ATLAS, God bless it, is open-source. On Linux, ATLAS compiles with gcc by default. Using the standard configuration, this gives very fast routines, but leaves the libraries considerably slower for <ulink url="https://imaging.mrc-cbu.cam.ac.uk/imaging/SpmWithPentium4/imaging/NaNs#">NaNs</ulink>. This is because, although the code doing the main computational work uses SSE2 instructions, some of the error checking does not. In any case, you can solve this by recompiling ATLAS to make full use of SSE2. Either recompile with gcc, using appropriate flags for SSE2, or use the Intel C compiler, which is free for academic use. <ulink url="http://www.jiscmail.ac.uk/cgi-bin/wa.exe?A2=ind0302&amp;L=spm&amp;P=R49173&amp;I=-1">Satra Ghosh</ulink> worked out how to do this with the Intel compiler; the following recipe is based on his method, <ulink url="http://www.jiscmail.ac.uk/cgi-bin/wa.exe?A2=ind0303&amp;L=spm&amp;P=R7888&amp;I=-1">Darren Gitelman's helpful update</ulink>, and my own experience. If you are still stuck with Windows, the instructions below will also work, using the gcc within <ulink url="http://cygwin.com">Cygwin</ulink> - see the <ulink url="http://www.kevinsheppard.com/research/MatlabAtlas/MatlabAtlas.htm">ATLAS / Windows page</ulink> </para><para>To recompile with gcc, you will need at least gcc &gt;=3.1, which was (I think) the first to implement the relevant SSE2 flags. I have only used 3.2 and 3.3 myself. </para><para>For the Intel C++ compiler for Linux (icc), <ulink url="http://www.intel.com/software/products/compilers/clin/noncom.htm">download icc from Intel</ulink> and install. I installed into /usr/local/intel. Don't use the Intel Fortran compiler with ATLAS; there is nothing to gain, as the Fortran compiler is only used to provide Fortran linking, and not for the maths. Before you start the compilation, source the relevant script to set up environment variables for the Intel compiler; in my case (using bash) this was with the command &quot;./usr/local/intel/compiler70/bin/iccvars.sh&quot;. </para><para>First download the ATLAS source. The current stable version is 3.6, the development version is 3.7. The intructions below are for ATLAS 3.6. Unpack the source into a handy directory. You should now have directory named &quot;ATLAS&quot;; cd to that directory. </para><section><title>Getting ready to configure ATLAS</title></section><section><title>Configure ATLAS</title><para>To make things a bit easier, I have written a <link linkend="atlas_config">script to configure ATLAS</link> with some good defaults. This script fixes the L1 cache size as described below, as well as setting a flag giving the CPU cycles per second; this is useful when you are the only user of the CPU, and there are timing problems in compiling ATLAS. To run the script for gcc, use &quot;sh atlas_sse_config.sh gcc&quot;; for icc use &quot;sh atlas_sse_config.sh icc&quot;. The scripts set many of the defaults to the values you will need below.If you are not running the script above, just run  from the command line. </para><para>Here is a list the questions and suggested answers for a config run. ATLAS puts the default answer in square brackets at the end of the question and before the colon. The ATLAS defaults here are the standard defaults; the defaults using the script above will contain many of the suggested answers. If there is nothing after the colon, this means you can accept the default. For more information, see the comments printed on screen during the configure process and the <ulink url="http://math-atlas.sourceforge.net/">ATLAS</ulink> website. </para><para>The questions below are from a linux system; there are a few extra questions using <ulink url="http://www.cygwin.com">Cygwin</ulink> and Windows, but the answers to these should be clear. </para><screen><![CDATA[Enter number at top left of screen [0]:
Have you scoped the errata file? [y]:
Are you ready to continue? [y]:
enable Posix threads support? [y]:
use express setup? [y]: n
Enter Architecture name (ARCH) [Linux_P4SSE2]:
Enter Maximum cache size (KB) [512]: 1024]]></screen><para>This last number should be double your actual cache size. In my case the cache was 512MB. </para><screen><![CDATA[Enter File creation delay in seconds [0]:
Enter Top level ATLAS directory [/home/mb312/installs/ATLAS]:
Enter Directory to build libraries in [$(TOPdir)/lib/$(ARCH)]:
Enter Library name for ATLAS primitives [libatlas.a]:
Enter Library name for the C interface to BLAS [libcblas.a]:
Enter Library name for the Fortran77 interface to BLAS [libf77blas.a]:
Enter Library name for the C interface to BLAS [libptcblas.a]:
Enter Library name for the Fortran77 interface to BLAS [libptf77blas.a]:
Enter F77 Linker  [$(F77)]:
Enter F77 Link Flags  [$(F77FLAGS)]:]]></screen><para>Here the configurations for gcc and icc diverge. Here are the next few steps for gcc: </para><screen><![CDATA[Enter ANSI C compiler(CC) [/usr//bin/gcc]:
Enter C Flags (CCFLAGS) [-fomit-frame-pointer -O3 -funroll-all-loops]: -fomit-frame-pointer -O3 -funroll-all-loops -march=pentium4 -mfpmath=sse
Enter C compiler for generated code (MCC) [/usr//bin/gcc]:
Enter C FLAGS (MMFLAGS) [-fomit-frame-pointer -O]: -fomit-frame-pointer -O -march=pentium4 -mfpmath=sse]]></screen><para>For icc these steps would be: </para><screen><![CDATA[Enter ANSI C compiler(CC) [/usr//bin/gcc]: icc
Enter C Flags (CCFLAGS) [-fomit-frame-pointer -O3 -funroll-all-loops]: -xN -O3 -mp1 -static
Enter C compiler for generated code (MCC) [/usr//bin/gcc]: icc
Enter C FLAGS (MMFLAGS) [-fomit-frame-pointer -O]: -xN -O3 -mp1 -static -fno-alias]]></screen><para>After this the configure continues: </para><screen><![CDATA[Enter C Linker  [$(CC)]:
Enter C Link Flags  [$(CCFLAGS)]:
Enter Archiver  [ar]:
Enter Archiver flags  [r]:
Enter Ranlib  [echo]:
Enter BLAS library []:
Enter General and system libs [-lpthread -lm]:]]></screen><para>The next question is. If you choose the default, 'y', you life will be a lot easier, and you will get a compiled version of ATLAS that will probably be well tuned to your hardware. If you choose 'n', the compile will be much longer, may break down (see below), but has the chance of being ideally tuned to your hardware. The supplied defaults are unlikely to be far from the ideal, so the easier choice here is 'n'. If you choose 'y', you get one last question: </para><screen><![CDATA[Tune the Level 1 BLAS? [y]:]]></screen></section><section><title>Compile ATLAS</title><para>If you didn't use my configuration script, there is a tweak to do before starting to ATLAS. During the compile, ATLAS will try to find the size of the processor L1 cache. For the pentium 4s I have tried, <ulink url="http://sourceforge.net/mailarchive/forum.php?forum=math-atlas-devel">ATLAS seems to end up with a random L1 cache size</ulink>. Clint Whaley, the ATLAS maintainer, suggested an L1 cache size of 128MB as optimum for the pentium 4, even though the actual L1 cache is 8MB. The L1 size can be set by hard coding the value; if you use the <link linkend="atlas_config">script to configure ATLAS</link>, explained below, this will happen automatically. Otherwise you can replace the ATLAS file [ATLAS-root] /tune/sysinfo/L1CacheSize.c with the <link linkend="l1cachesize">version in the appendix</link>.When I chose not to accept the architecture defaults, I had a lot of difficulties compiling ATLAS. The problem was that I had many timing outside tolerance errors; see the relevant section in the the <ulink url="http://math-atlas.sourceforge.net/errata.html">ATLAS errata file</ulink>. In fact the <link linkend="atlas_config">script to configure ATLAS</link> mentioned above has a setting in it that tries to deal with this problem, again suggested by Clint Whaley, which is to define a compile variable of  to the actual value of your CPU megahertz; for example, for a 2.6 GHz machine this value would be set to 2600. You can do this via a configuration script like mine, or by editing the  (or whatever) file created by the configuration run. This fix is likely to work well if you are the only user of the machine, so all the CPU load comes from the ATLAS compile itself, when it starts. If not you may not want to set this value; in that case, if you are using my configuration script, you will need to remove the lines setting  from the script. </para><para>Another way to try and reduce the timing errors, if you get them, is suggested in the <ulink url="http://math-atlas.sourceforge.net/errata.html">ATLAS errata file</ulink>. In fact, on occasion I found it useful to preemptively apply a version of a fix suggested in the errata file, and changing line 117 of ATLAS-root] /tune/sysinfo/GetSysSym.c from : </para><screen><![CDATA[fprintf(fpout, "#define ATL_nkflop %d\n", nkflop);]]></screen><para>to: </para><screen><![CDATA[fprintf(fpout, "#define ATL_nkflop %d\n", nkflop*5);]]></screen><para>Now run the compilation as the config output suggests; here the command would be: </para><screen><![CDATA[make install Linux_P4SSE2]]></screen><para>On my 3GHz Pentium, this process took a few hours. </para></section><section><title>Tune ATLAS</title><para>You might consider trying to fine-tune ATLAS. Here are some <ulink url="http://imaging.mrc-cbu.cam.ac.uk/lug/AtlasTips">tips for tuning ATLAS</ulink>. </para></section><section><title>Make into matlab library</title><para>Now you need to make the compiled libraries into a matlab ATLAS library; here are the scripts I use to do this for <link linkend="gcc_atlas">Linux / ATLAS / gcc</link>, and <link linkend="icc_atlas">Linux / ATLAS / icc</link>. For Windows, use the script on <ulink url="http://www.kevinsheppard.com/research/MatlabAtlas/MatlabAtlas.htm">the ATLAS / Windows page</ulink> </para><para>You're done for the ATLAS recompile. If you don't want to recompile ATLAS, the other option is to: </para></section></section><section><title>Use the Intel Math Kernel libraries</title><para>The Intel Math Kernel library is a set of math routines, including BLAS and LAPACK, that are optimised for Intel processors. They are available from Intel, who provide a 30-day evaluation trial, after which you must pay for a license. Intel offer a discount for academic users. To use the MKL, you can either reassemble a new library for matlab (see a <link linkend="mkl_atlas">shell script to do this</link> ), or <ulink url="http://www.jiscmail.ac.uk/cgi-bin/wa.exe?A2=ind0303&amp;L=spm&amp;P=R1100&amp;I=-1">point Matlab to the MKL</ulink> directly. Results of the Matlab bench command and some SPM timings suggest that the MKL runs at a similar speed to the ATLAS library recompiled with gcc or icc. </para><para><anchor id="tell_matlab"/> </para></section><section><title>Telling matlab about the new libraries</title><para>Lastly, you will need to tell matlab to use the newly created ATLAS libraries. You can do this by editing the file &quot;blas.spec&quot; in the &quot;bin/ [arch] &quot; subdirectory in the matlab root directory, where [arch] is &quot;glnx86&quot; for Linux and &quot;win32&quot; for Windows. Change the line like: </para><screen><![CDATA[GenuineIntel Family 15 Model * atlas_P4.so # Pentium IV (Foster)]]></screen><para>to point to the new library, which should be in the same directory. For example, if you used <link linkend="gcc_atlas">the script in the appendix</link> to write the gcc recompiled libraries, you should have a file &quot;atlas_P4SSE2_gcc.so&quot; in the matlab bin/glnx86 directory. In this case you can change the above line to: </para><screen><![CDATA[GenuineIntel Family 15 Model * atlas_P4SSE2_gcc.so # Pentium IV (Foster)]]></screen><para>Don't forget to save the old blas.spec as a backup. </para><para>Otherwise you can point matlab at the relevant library using the BLAS_VERSION environment variable - see the <ulink url="http://www.mathworks.com/access/helpdesk/help/base/install/unix/chap3_10.shtml">Mathworks help on BLAS</ulink>. </para></section></section><section><title>Recompiling the SPM mex routines</title><para>SPM uses compiled C routines for some rate limiting steps like image resampling. As images may contain <ulink url="https://imaging.mrc-cbu.cam.ac.uk/imaging/SpmWithPentium4/imaging/NaNs#">NaNs</ulink>, these need to be recompiled to avoid the Pentium NaN slowdown. Again, this can be done by giving gcc the appropriate flags to use SSE2, or using the Intel compiler. The Intel compiler gives faster times in the benchmarks I have run. Tom Womack and Chris Rorden have long pointed out the benefits of the <ulink url="http://www.psychology.nottingham.ac.uk/staff/cr1/spm99.html">Intel compiler for the SPM mex files</ulink>. </para><section><title>Compiling mex files for SPM99</title><para>To recompile the spm99 mex files you will need to change the spm_MAKE.sh file, and edit / create a mex options file. For gcc or icc, make the <link linkend="spm_make">suggested edits to spm_MAKE.sh</link>, which is in your SPM directory. For icc, copy the <link linkend="iccopts">iccopts.sh appendix</link> to a file &quot;iccopts.sh&quot; in your SPM directory. For gcc on Linux, edit your gccopts.sh option file, find the section for &quot;glnx86)&quot;, and change the COPTIMFLAGS, CXXOPTIMFLAGS variables to be: </para><screen><![CDATA[-O3 -fomit-frame-pointer -funroll-loops -march=pentium4 -mfpmath=sse -DNDEBUG]]></screen><para>For <ulink url="http://gnumex.sourceforge.net">gcc on Windows</ulink>, make and then edit your mexopts.bat file; change the OPTIMFLAGS variable to be: </para><screen><![CDATA[-O3 -fomit-frame-pointer -funroll-loops -march=pentium4 -mfpmath=sse]]></screen><para>Then change directory to the SPM directory, and recompile with: </para><screen><![CDATA[./spm_MAKE.sh gcc]]></screen><para>for Linux gcc, and </para><screen><![CDATA[./spm_MAKE.sh icc]]></screen><para>for icc, and </para><screen><![CDATA[./spm_MAKE.sh windows]]></screen><para>for Windows gcc </para><para>You can also try using the Intel C compiler for Windows (icl), which does give some speed gains over gcc. icl is not free for academic use, and requires Microsoft Visual C++ to be installed. You also need to edit the SPM code to make the compilation work. For those brave souls who want to give it a try, I have packaged up <ulink url="http://imaging.mrc-cbu.cam.ac.uk/downloads/Intel_p4/icl_mex.zip">an icl/SPM archive</ulink>, with edited versions of the SPM code, a mexopts.bat file and a version of the spm_MAKE.bat file that works with icl. </para><para>If you want to avoid recompiling these files, Chris Rorden and I have packaged some precompiled versions - see the <link linkend="short_simple">the short and simple</link> section above for links. </para><para>For reasons that are beyond me, the icc/icl compiled mex file - spm_resels_vol.mexglx / spm_resels_vol.dll - was causing an intermittent matlab crash. This may well have been fixed for more recent versions of the compiler. If you run into this problem, just the gcc version of that file should be used instead of the Intel compiled version. (If you get this problem, and want to try and track it down, the crash only occurs when the SSE2 extensions are used (-xW) to compile the vol_utils library; the file never crashes when running the debugger). </para></section><section><title>Compiling mex files for SPM2</title><section><title>gcc and Linux</title><para>To compile for gcc under Linux, find the following lines the Makefile in the spm2 main directory: </para><screen><![CDATA[Linux:
        make all SUF=mexglx  CC="gcc -O3 -funroll-loops"    MEX="mex COPTIMFLAGS='-O3 -funroll-loops'"]]></screen><para>Just below this, add the following lines: </para><screen><![CDATA[Linux.P4_gcc:
        make all SUF=mexglx  CC="gcc -O3 -funroll-loops -march=pentium4 -mfpmath=sse"    MEX="mex COPTIMFLAGS='-O3 -funroll-loops -march=pentium4 -mfpmath=sse'"]]></screen><para>then recompile the mex files from the command line with. </para></section><section><title>icc and Linux</title><para>First, copy the <link linkend="iccopts">iccopts.sh appendix</link> to a file &quot;iccopts.sh&quot; in your SPM directory. Next, add the following to the Makefile: </para><screen><![CDATA[Linux.P4_icc:
        make all SUF=mexglx  CC="icc -xN -O3 -mp1 -static"    MEX="mex -f iccopts.sh COPTIMFLAGS='icc -xN -O3 -mp1 -static'"]]></screen><para>and compile with. </para></section><section><title>gcc and Windows</title><para>The default compile for windows uses the P4 optimization flags. </para></section></section></section><section><title>Checking mex file execution time</title><para>You can use this <link linkend="mex_test">mex file speed check</link> to see if your current mex files have a problem with <ulink url="https://imaging.mrc-cbu.cam.ac.uk/imaging/SpmWithPentium4/imaging/NaNs#">NaNs</ulink>. If they do, the output will show that an image with <ulink url="https://imaging.mrc-cbu.cam.ac.uk/imaging/SpmWithPentium4/imaging/NaNs#">NaNs</ulink> takes considerably longer to resample than the same image without <ulink url="https://imaging.mrc-cbu.cam.ac.uk/imaging/SpmWithPentium4/imaging/NaNs#">NaNs</ulink>. This is what the output looks like on my laptop, running SPM with the default mex files under Windows on a P3: </para><screen><![CDATA[Simple read:  0.88
     Linear resample:  2.58
       Sinc resample: 19.53
 NaN linear resample:  8.40
              Smooth: 11.60]]></screen><para>Here you see that the NaN resample is taking nearly four times longer than the standard linear resample. </para></section><section><title>The sparse matrix problem</title><para>On my pentium 4 system I found that SPM99 model estimation was not as fast as I expected. After a little searching, this <ulink url="http://www.jiscmail.ac.uk/cgi-bin/wa.exe?A2=ind0303&amp;L=spm&amp;P=R35347&amp;I=1">turned out</ulink> to be due to great slowness during the application of the high and low pass temporal filtering. The filtering involves multiplying a matrix of data by a sparse matrix. A sparse matrix is a matrix which is likely to have many zeros, and matlab implements these differently from other matrices. The multiplication of a sparse matrix and a standard (full) matrix is <ulink url="http://newsreader.mathworks.com/WebX?14@nhxnaCOtegT@.eebba2a">usually slow</ulink>. For some reason, on my Linux systems, it was much slower than for other systems, at more than a hundred times slower than a full by full multiplication. The appendix contains a <link linkend="sparse_demo">sparse/full test script</link> to evaluate the problem on your system. This does not seem to be a Pentium 4 problem in particular; <ulink url="http://www.jiscmail.ac.uk/cgi-bin/wa.exe?A2=ind0303&amp;L=spm&amp;P=R35404&amp;I=-1">others</ulink> have not found the same slowdown.The sparse matrix problem can be solved by converting the sparse matrix format of the SPM99 filters to full matrix format; this gives the same result from the filtering, but is much quicker on all the machines that I have tested. You only need to add a few lines to the SPM99file &quot;spm_spm.m&quot; - <link linkend="sparse_fix">see the appendix</link> for details. I have referred to the use of this edited SPM file as the Optimized model - see below. <anchor id="config_benchmarks"/> </para><para><anchor id="maxmem"/> </para></section><section><title>Telling SPM how much memory to use</title><para>A last possible edit you may want to make is to tell SPM about how much memory it should use. This is nothing to do with Intel machines specifically, and applies to any platform. </para><para>By default, SPM99 is careful, if possible, to use a small amount of memory during model estimation. If you have a large amount of memory, you can speed up SPM by telling it to use more memory. Edit the file spm_spm.m in the SPM99 distribution, and look around line 340 for something like; </para><screen><![CDATA[maxMem   = 2^30;        %-Max data chunking size, in bytes]]></screen><para>Here, I have already edited this line to tell SPM to use (up to) all of my system's 1GB (2^30 bytes) of memory. Edit this line to suit your system. Consider trying a model estimation with different settings; when the setting is too small, you will get not much use of swap space, and rather slow performance. When it is too large, there will be slow performance and lots of swap use. </para></section><section><title>General Linux tuning</title><para>Last but not least, there may be gains from a general Linux tune up: for details try these links: <ulink url="http://www.geocities.com/lunatech3007/how-to/html/howto_17.html">Post-install tuning</ulink>; <ulink url="http://linux.oreillynet.com/pub/a/linux/2000/06/29/hdparm.html">Tuning disks with hdparm</ulink>; <ulink url="http://koushik.fateback.com/tune.html">Linux network tuning</ulink>. <anchor id="config_bench"/> </para></section><section><title>Results - timings</title><para>After all that; here are some sample timings with the various options described here. The benchmarks used a 2.5GHz Pentium 4 machine with 1GB RAM, running SPM99 under Matlab 6.5 and Mandrake Linux 9.0. Values are times in minutes to complete various SPM99 jobs, as described in more detail in <ulink url="https://imaging.mrc-cbu.cam.ac.uk/imaging/SpmWithPentium4/imaging/SpmBenchmarks#">the SPM benchmarks page</ulink>. Note the last column, which gives times for writing a long list of contrast images, including 41 F contrast images. The &quot;default&quot; configuration is the standard SPM mex files, with the atlas library that matlab ships with. &quot;icc atlas&quot; is the same jobs after compiled ATLAS with icc. &quot;icc atlas + mex&quot; uses the recompiled ATLAS and mex files using icc. &quot;gcc SSE atlas + mex&quot; is for the ATLAS and mex files recompiled with gcc as described here. &quot;W2000 gcc SSE atlas + icl mex&quot; refers to timings from the same machine running Windows 2000, and using the ATLAS libraries recompiled using SSE, and SPM mex files recompiled using the Intel C compiler for Windows (icl). This was the Windows configuration that gave the fastest times. </para><informaltable><tgroup cols="6"><colspec colname="col_0"/><colspec colname="col_1"/><colspec colname="col_2"/><colspec colname="col_3"/><colspec colname="col_4"/><colspec colname="col_5"/><tbody><row rowsep="1"><entry colsep="1" rowsep="1"><para>Configuration </para></entry><entry colsep="1" rowsep="1"><para>Realign </para></entry><entry colsep="1" rowsep="1"><para>Smooth </para></entry><entry colsep="1" rowsep="1"><para>Model </para></entry><entry colsep="1" rowsep="1"><para>Optimized model </para></entry><entry colsep="1" rowsep="1"><para>Contrast </para></entry></row><row rowsep="1"><entry colsep="1" rowsep="1"><para>Default </para></entry><entry colsep="1" rowsep="1"><para>22.0 </para></entry><entry colsep="1" rowsep="1"><para>5.9 </para></entry><entry colsep="1" rowsep="1"><para>24.2 </para></entry><entry colsep="1" rowsep="1"><para>6.7 </para></entry><entry colsep="1" rowsep="1"><para>94.0 </para></entry></row><row rowsep="1"><entry colsep="1" rowsep="1"><para>icc atlas </para></entry><entry colsep="1" rowsep="1"><para>22.7 </para></entry><entry colsep="1" rowsep="1"><para>5.8 </para></entry><entry colsep="1" rowsep="1"><para>24.2 </para></entry><entry colsep="1" rowsep="1"><para>5.4 </para></entry><entry colsep="1" rowsep="1"><para>20.1 </para></entry></row><row rowsep="1"><entry colsep="1" rowsep="1"><para>icc atlas, mex </para></entry><entry colsep="1" rowsep="1"><para>16.2 </para></entry><entry colsep="1" rowsep="1"><para>5.4 </para></entry><entry colsep="1" rowsep="1"><para>24.2 </para></entry><entry colsep="1" rowsep="1"><para>5.7 </para></entry><entry colsep="1" rowsep="1"><para>3.2 </para></entry></row><row rowsep="1"><entry colsep="1" rowsep="1"><para>gcc SSE atlas, mex </para></entry><entry colsep="1" rowsep="1"><para>18.2 </para></entry><entry colsep="1" rowsep="1"><para>5.5 </para></entry><entry colsep="1" rowsep="1"><para>23.6 </para></entry><entry colsep="1" rowsep="1"><para>5.8 </para></entry><entry colsep="1" rowsep="1"><para>3.1 </para></entry></row><row rowsep="1"><entry colsep="1" rowsep="1"><para>W2K, gcc SSE atlas, icl mex </para></entry><entry colsep="1" rowsep="1"><para>23.0 </para></entry><entry colsep="1" rowsep="1"><para>5.8 </para></entry><entry colsep="1" rowsep="1"><para>23.4 </para></entry><entry colsep="1" rowsep="1"><para>5.5 </para></entry><entry colsep="1" rowsep="1"><para>3.0 </para></entry></row></tbody></tgroup><!--Width 138px not supported--></informaltable><para>Note the huge speed increase for contrast generation after optimizing for the P4. See <ulink url="https://imaging.mrc-cbu.cam.ac.uk/imaging/SpmWithPentium4/imaging/SpmBenchmarks#">the SPM benchmarks page</ulink> for some comments on Linux and Windows. </para><para>Good luck... </para><para>Matthew Brett 11/4/2003 </para><para><anchor id="matlab_nan_test"/> </para></section><section><title>Appendix - matlab NaN test program</title><para>A test program to show the problem with <ulink url="https://imaging.mrc-cbu.cam.ac.uk/imaging/SpmWithPentium4/imaging/NaNs#">NaNs</ulink> in matlab: </para><screen><![CDATA[sz = 500;
A = ones(sz);
B = ones(sz);
tic
C = A*B;
t1 = toc;
A = A * NaN;
tic
C = A*B;
t2 = toc;
[tmp msg] = unix('uname -a');
fprintf('System: %s\nMatlab: %s\nOnes: %8.4f; NaNs:  %8.4f; Propn: %8.4f\n',...
        msg, version, t1, t2, t2/t1);]]></screen><para><anchor id="nan_test_output"/> with some example output: </para><screen><![CDATA[System: Linux ... 2.4.21pre4-5mdkenterprise #1 SMP
Sat Feb 8
22:27:52 CET 2003 i686 unknown unknown GNU/Linux
Matlab version:
6.5.0.180913a (R13)
Ones:   0.1447; NaNs:  108.7452; Propn:
751.7246]]></screen><para><anchor id="c_nan_test"/> </para></section><section><title>Appendix - C NaN test program</title><para>Here is little C program to show the NaN problem: </para><screen><![CDATA[#include <sys/time.h>
#include <sys/resource.h>
#include <stdio.h>
]]><![CDATA[
#ifndef RUSAGE_SELF
#define RUSAGE_SELF      0     /* calling process */
#endif
]]><![CDATA[
#define SZ 10000000
]]><![CDATA[
#define STIME(X) ((X.ru_utime.tv_sec +     \
                    X.ru_stime.tv_sec) +   \
                   (X.ru_utime.tv_usec +   \
                    X.ru_stime.tv_usec) / 1.0E6)
]]><![CDATA[
int main( int argc, char *argv [ ] ) {
  double i, mval;
  struct rusage t0, t1, t2;
  double a,b,c;
  char op;
]]><![CDATA[
  op = '*';
  getrusage(RUSAGE_SELF,&t0);
  mval = 1.0; 
  for (i=0,c=1;<SZ;i++) c*=mval;
  getrusage(RUSAGE_SELF,&t1);
  mval = 0.0 / 0.0; 
  for (i=0,c=1;i<SZ;i++) c*=mval;
  getrusage(RUSAGE_SELF,&t2);
  a = STIME(t1) - STIME(t0);
  b = STIME(t2) - STIME(t1);
  c = b/a;
  printf("%c time %8.4f, %c NaN time %8.4f, proportion %8.4f\n", 
         op, a, op, b, c);
]]><![CDATA[
  op = '+';
  getrusage(RUSAGE_SELF,&t0);
  mval = 1.0;
  for (i=0,c=1;i<SZ;i++) c+=mval;
  getrusage(RUSAGE_SELF,&t1);
  mval = 0.0 / 0.0; 
  for (i=0,c=1;i<SZ;i++) c+=mval;
  getrusage(RUSAGE_SELF,&t2);
  a = STIME(t1) - STIME(t0);
  b = STIME(t2) - STIME(t1);
  c = b/a;
  printf("%c time %8.4f, %c NaN time %8.4f, proportion %8.4f\n", 
         op, a, op, b, c);
]]><![CDATA[
  return(0);
}]]></screen><para>Compile without optimization (e.g gcc -O0). </para><para><anchor id="atlas_config"/> </para></section><section><title>Appendix - configuration scrip for ATLAS</title><para>Save this as [ATLAS-root] /atlas_sse_config.sh, and cd to the [ATLAS-root] directory and run the script with &quot;sh atlas_sse_config.sh gcc&quot; for gcc, or &quot;sh atlas_sse_config.sh icc&quot; for the Intel C compiler. </para><screen><![CDATA[# Script to configure ATLAS for P4 SSE flags
]]><![CDATA[
case "$1" in
    gcc)
        ;;
    icc)
        ;;
    *)
        echo "Usage: $0 {gcc|icc}"
        exit 1
        ;;
esac
]]><![CDATA[
# reset L1CacheSize
l1file='tune/sysinfo/L1CacheSize.c'
mv $l1file ${l1file}.bak
cat <<EOF > $l1file
#include <stdio.h>
#include <stdlib.h>
]]><![CDATA[
main(int nargs, char *args [] )
{
  int L1Size=128;
   FILE *L1f;
   fprintf(stderr, "Assumed L1 cache size = %dkb\n",
           L1Size);
   L1f = fopen("res/L1CacheSize", "w");
   fprintf(L1f, "%d\n",L1Size);
   fclose(L1f);
   exit(0);
}
EOF
]]><![CDATA[
# System parameters
cpu_mhz=`grep "MHz" /proc/cpuinfo | gawk 'BEGIN { FS = " " } {print $4 }'`;
sys_name=`uname`;
arch_name="${sys_name}_P4SSE2_sse_$1"
]]><![CDATA[
# Make and run config options
make xconfig
case "$1" in
    gcc)
        ./xconfig \
            -F c "-fomit-frame-pointer -O3 -funroll-all-loops -march=pentium4 -mfpmath=sse" \
            -F m "-fomit-frame-pointer -O -march=pentium4 -mfpmath=sse" \
            -a $arch_name \
            -C "-D PentiumCPS=$cpu_mhz"
        ;;
    icc)
        ./xconfig \
            -c icc \
            -m icc \
            -a $arch_name \
            -C "-D PentiumCPS=$cpu_mhz"
        ;;
esac
]]><![CDATA[
# to run a lengthy make; uncomment this next line
# make install arch=$arch_name]]></screen><para><anchor id="l1cachesize"/> </para></section><section><title>Appendix - replacement for L1CacheSize.c</title><para>Save this as a replacement for [ATLAS-root] /tune/sysinfo/L1CacheSize.c; </para><screen><![CDATA[#include <stdio.h>
#include <stdlib.h>
]]><![CDATA[
main(int nargs, char *args [] )
{
  int L1Size=128;
   FILE *L1f;
   fprintf(stderr, "Assumed L1 cache size = %dkb\n",
           L1Size);
   L1f = fopen("res/L1CacheSize", "w");
   fprintf(L1f, "%d\n",L1Size);
   fclose(L1f);
   exit(0);
}]]></screen><para><anchor id="gcc_atlas"/> </para></section><section><title>Appendix - make matlab ATLAS for gcc</title><para>Script to make matlab ATLAS library compiled with gcc. Copy this text as a new file, say &quot;make_atlas.sh&quot;. Edit the file to change your paths, ATLAS architecture name, to match your system. Run with &quot;bash make_atlas.sh&quot; </para><screen><![CDATA[# Script to make matlab ATLAS library for gcc
]]><![CDATA[
# matlab, atlas paths; change these as necessary
MATLAB=/usr/local/matlab6p5
ATLAS=/root/installs/ATLAS
ARCH=P4SSE2_gcc
]]><![CDATA[
ATLAS_LIB=$ATLAS/lib/Linux_$ARC<H
GCC_LIB=/usr/lib/gcc-lib/i586-mandrake-linux-gnu/3.2/
]]><![CDATA[
LIB_NAME=$MATLAB/bin/glnx86/atlas_$ARCH.so
]]><![CDATA[
ld -shared -o $LIB_NAME --whole-archive \
 $ATLAS_LIB/libatlas.a \
 $ATLAS_LIB/libcblas.a \
 $ATLAS_LIB/liblapack.a \
 $ATLAS_LIB/libf77blas.a \
 --no-whole-archive -L $GCC_LIB \
 -lg2c -lpthread -lm]]></screen><para><anchor id="icc_atlas"/> </para></section><section><title>Appendix - make matlab ATLAS for icc</title><para>Script to make matlab ATLAS library compiled with icc. Copy this text as a new file, say &quot;make_atlas.sh&quot;. Edit the file to change your paths, ATLAS architecture name, to match your system. Run with &quot;sh make_atlas.sh&quot; </para><screen><![CDATA[# Script to make matlab ATLAS library for icc
]]><![CDATA[
# matlab, intel, atlas paths: change as necessary
MATLAB=/usr/local/matlab6p5
INTEL_LIB=/usr/local/intel
ATLAS=/root/installs/ATLAS
ARCH=P4SSE2_icc
]]><![CDATA[
ATLAS_LIB=$ATLAS/lib/Linux_$ARCH
GCC_LIB=/usr/lib/gcc-lib/i586-mandrake-linux-gnu/3.2/
]]><![CDATA[
LIB_NAME=$MATLAB/bin/glnx86/atlas_$ARCH.so
ICC_LIB=$INTEL_LIB/compiler70/ia32/lib
]]><![CDATA[
ld -shared -o $LIB_NAME --whole-archive \
 $ATLAS_LIB/libatlas.a \
 $ATLAS_LIB/libcblas.a \
 $ATLAS_LIB/liblapack.a \
 $ATLAS_LIB/libf77blas.a \
 --no-whole-archive -L $GCC_LIB -L $ICC_LIB \
 -lg2c -lpthread -lm \
 -dn -limf -lirc]]></screen><para><anchor id="mkl_atlas"/> </para></section><section><title>Appendix - make matlab ATLAS from MKL</title><para>Script to write Math Kernel Library static library routines into matlab ATLAS-like library (e.g. atlas_mkl_P4.so in the matlab bin/glnx86/ directory. Change the paths for your system. </para><screen><![CDATA[# CPU can be one of "def", "p3", "p4"
CPU=p4
]]><![CDATA[
# matlab and intel paths 
MATLAB=/usr/local/matlab6p5
INTEL_LIB=/usr/local/intel
]]><![CDATA[
LIB_NAME=$MATLAB/bin/glnx86/atlas_mkl_$CPU.so
MKL_LIB=$INTEL_LIB/mkl/lib/32
ICC_LIB=$INTEL_LIB/compiler70/ia32/lib
]]><![CDATA[
cp $MKL_LIB/libmkl_lapack.a tmp_lapack.a
ar -d tmp_lapack.a mkl_lsame.o
]]><![CDATA[
ld -shared -o $LIB_NAME --whole-archive \
 $MKL_LIB/libmkl_$CPU.a tmp_lapack.a  $MKL_LIB/libguide.a \
--no-whole-archive -L $ICC_LIB -L $MKL_LIB \
-dn -lF90 -limf -lirc
]]><![CDATA[
\rm tmp_lapack.a]]></screen><para><anchor id="spm_make"/> </para></section><section><title>Appendix - spm_MAKE.sh for pentium 4 mex files</title><para>Replace the sections for &quot;windows)&quot; through &quot;gcc)&quot; in spm_MAKE.sh with the following: </para><screen><![CDATA[windows)
        echo "Windows compile with Cygwin/MingW gcc"
        echo "see http://www.mrc-cbu.cam.ac.uk/Imaging/gnumex20.shtml"
        echo "for instructions about installing gcc for"
        echo "compiling Mex files."
        echo ""
        deff=-DSPM_WIN32
        CC="gcc -mno-cygwin -march=pentium4 -mfpmath=sse -O3 -fomit-frame-pointer -fno-exceptions -funroll-loops $deff"
        cmex5="mex.bat $deff "
        cmex4="mex.bat $deff -V4 "
        # Windows added utility files
        $CC -c -o win32mmap.o win32mmap.c
        $cmex5 spm_win32utils.c
        added_objs="win32mmap.o spm_mapping.obj";;
    gcc)
        echo "Optimised standard unix compile for gcc"
        echo "This should work on Sun, Linux etc"
        echo "Note that the path to the gccopts.sh file may need"
        echo "changing."
        echo ""
        CC="gcc -O3 -funroll-loops -fomit-frame-pointer -march=pentium4 -mfpmath=sse"
        cmex5="mex     -f gccopts.sh"
        cmex4="mex -V4 -f gccopts.sh";;
    icc)
        echo "Optimised Linux compile using Intel C compiler"
        echo ""
        CC="icc -O3 -tpp7 -xW -unroll"
        cmex5="mex     -f iccopts.sh"
        cmex4="mex -V4 -f iccopts.sh"]]></screen><para><anchor id="iccopts"/> </para></section><section><title>Appendix - mex option file for icc</title><para>Save the following as iccopts.sh: </para><screen><![CDATA[# iccopts.sh    Shell script for configuring MEX-file creation script
# Don't forget to change the root path to the compiler, below
TMW_ROOT="$MATLAB"
MFLAGS=''
if [ "$ENTRYPOINT" = "mexLibrary" ]; then
MLIBS="-L$TMW_ROOT/bin/$Arch -lmx -lmex -lmatlb -lmat -lmwservices -lut -lm"
else  
MLIBS="-L$TMW_ROOT/bin/$Arch -lmx -lmex -lmat -lm"
fi
]]><![CDATA[
# change path to match your system
# The compiler itself will be in $INTEL_PATH/bin
INTEL_PATH="/usr/local/intel"
ILIBS="-L$INTEL_PATH/lib/ -lcprts -lcxa  -lguide -lirc -lircmt -lompstub  -lsvml -lunwind -limf"
RPATH="-Wl,--rpath-link,$TMW_ROOT/extern/lib/$Arch,--rpath-link,$TMW_ROOT/bin/$Arch"
#   icc -V
#   Intel(R) C++ Compiler for 32-bit applications, Version 8.0   Build 20040318Z Package ID: l_cc_pc_8.0.058_pe063.1
CC='icc'
CFLAGS='-fPIC -D_GNU_SOURCE'
CLIBS="$RPATH $ILIBS $MLIBS"
COPTIMFLAGS="-O3 -tpp7 -xW -unroll -DNDEBUG"
CDEBUGFLAGS='-g'
]]><![CDATA[
CXX='icc'
CXXFLAGS="$CFLAGS"
CXXLIBS="$CLIBS"
CXXOPTIMFLAGS='$COPTIMFLAGS'
CXXDEBUGFLAGS='-g'
#   ifc -V
# Intel(R) Fortran Compiler for 32-bit applications, Version 7.1   Build 20030307Z
FC='ifc'
FFLAGS='-fPIC -r8 -w -cm -q'
FLIBS="$RPATH $ILIBS $MLIBS"
FOPTIMFLAGS="$COPTIMFLAGS"
FDEBUGFLAGS='-g'
#
LD="icc"
LDFLAGS="-pthread -shared -static -Wl,--version-script,$TMW_ROOT/extern/lib/$Arch/$MAPFILE"
LDOPTIMFLAGS="$COPTIMFLAGS -fPIC"
LDDEBUGFLAGS='-g'
#
POSTLINK_CMDS=':']]></screen><para><anchor id="mex_test"/> </para></section><section><title>Appendix - mex speed test</title><screen><![CDATA[% Script to test speed of compiled SPM files
]]><![CDATA[
% Tests defined
tests = {'Simple read','Linear resample', 'Sinc resample', ...
         'NaN linear resample', 'Smooth'};
]]><![CDATA[
% Make test image
dim = [128 128 128];
img = randn(dim);
V    = struct(...
    'fname',    'test.img',...
    'dim',      [dim spm_type('float')],...
    'mat',      eye(4),...
    'pinfo',    [1 0 0] ',...
    'descrip',  'test image');
V = spm_write_vol(V, img);
]]><![CDATA[
% Read to load cache
img = spm_read_vols(V);
]]><![CDATA[
% write with NaNs
V2 = V;
V2.fname = 'nan_test.img';
img(img < 3.5) = NaN;
V2 = spm_write_vol(V2, img);
]]><![CDATA[
% Start tests
t = [];
]]><![CDATA[
% simple read
tic
img = spm_read_vols(V);
t = [t toc];
]]><![CDATA[
% resampling
startp = 2;
grain = 1;
dims = V.dim(1:3) - 2;
[X Y Z] = ndgrid(startp:grain:dims(1), ...
                 startp:grain:dims(2), ...
                 startp:grain:dims(3));
X = X+0.5; Y = Y+0.5; Z = Z + 0.5;
]]><![CDATA[
tic
p = spm_sample_vol(V, X, Y, Z, 1); % trilinear
t = [t toc];
]]><![CDATA[
tic 
p = spm_sample_vol(V, X, Y, Z, 8); % sinc
t = [t toc];
]]><![CDATA[
tic 
p = spm_sample_vol(V2, X, Y, Z, 1); % trilinear, NaN
t = [t toc];
]]><![CDATA[
% smoothing
tic
spm_smooth(V, 'sm_test.img', 8);
t = [t toc];
]]><![CDATA[
% report
for i = 1:length(tests)
  fprintf('%20s: %5.2f\n', tests{i}, t(i));
end]]></screen><para><anchor id="sparse_demo"/> </para></section><section><title>Appendix - showing the sparse matrix problem</title><para>Simply run this little program in matlab: </para><screen><![CDATA[Kd = 300;
Vd = 2000;
K = ones(Kd);
y = ones(Kd, Vd);
tic
y = K*y;
t = toc;
K = sparse(K);
tic
y = K*y;
t(2) = toc]]></screen><para>On my system, the second (sparse matrix) timing - t(2), is very considerably longer than the first (full matrix) timing - t(1). <anchor id="sparse_fix"/> </para></section><section><title>Appendix - fixing the sparse matrix problem</title><para>Edit the file spm_spm.m in your SPM99 directory. Around line 420, you will see something like: </para><screen><![CDATA[%-Initialise design space
%=======================================================================
fprintf('%-40s: %30s','Initialising design space','...computing')    %-#]]></screen><para>Just after these lines, add the following: </para><screen><![CDATA[if iscell(xX.K)
  for s = 1:length(xX.K)
    xX.K{s}.KH=full(xX.K{s}.KH);
    xX.K{s}.KL=full(xX.K{s}.KL);
  end
else
  xX.K = full(xX.K);
end]]></screen><para>This converts the filter matrices from sparse to full, and greatly speeds later execution time.Last Refreshed: Fri Apr 11 19:53:43 GMT Daylight Time 2003 </para></section></section></article>