1 | PORTING THE WINDOWS VERSION OF ACCELERATOR TOOLBOX TO UNIX |
---|
2 | |
---|
3 | I assume you are familiar with mex-files in MATLAB. |
---|
4 | I suggest you also install AT on a Windows |
---|
5 | machine as a reference. |
---|
6 | |
---|
7 | BACKGROUND (See also 'MATLAB API Reference Manual'): |
---|
8 | |
---|
9 | 1. mex-file is a piece of an object code that MATLAB can load |
---|
10 | and execute. |
---|
11 | |
---|
12 | 2. User-written C files must have MATLAB-specific interface: |
---|
13 | They must implement a function 'mexFunction()' which serves |
---|
14 | as an enty point or gateway between the mex-file and MATLAB. |
---|
15 | |
---|
16 | 3. The easiest (and sufficient) way to make mex-files is to |
---|
17 | use MATLAB command 'mex'. |
---|
18 | Example: If myfun.c is a file that contains |
---|
19 | the mexFunction definition, the following |
---|
20 | command builds the mex-file: |
---|
21 | |
---|
22 | >> mex myfun.c |
---|
23 | |
---|
24 | Try also: |
---|
25 | |
---|
26 | >> help mex |
---|
27 | |
---|
28 | to see more options and some platform specific issues |
---|
29 | |
---|
30 | 4. The mex-file produced by 'mex' command is PLATFORM-SPECIFIC |
---|
31 | It even has different extensions: |
---|
32 | .dll on Windows |
---|
33 | .mexglx on Linux |
---|
34 | |
---|
35 | 5. If C file uses only PORTABLE C CODE - porting is trivial: |
---|
36 | Copy the C file to a new platform and let the 'mex' command |
---|
37 | take care of it. |
---|
38 | |
---|
39 | 6. If some portions are not portable, the possible solutions are |
---|
40 | A: modify C file to compile and run only under the |
---|
41 | new platform |
---|
42 | B:(better) Make platform-specific compilation branches |
---|
43 | in the C code using #ifdef compiler directive |
---|
44 | |
---|
45 | |
---|
46 | THE USE OF MEX-FILES IN ACCELERATOR TOOLBOX |
---|
47 | |
---|
48 | 1. There are so called 'pass-methods' |
---|
49 | located in ATROOT/simulator/element |
---|
50 | For example: |
---|
51 | DriftPass.c |
---|
52 | QuadLinearPass.c |
---|
53 | ... |
---|
54 | THESE ARE PLATFORM DEPENDENT but easy to port: |
---|
55 | |
---|
56 | 1.1 Each of these functions has a platform-INdependent interface |
---|
57 | mexFunction() (Examine DriftPass.c for example) |
---|
58 | It allows the function to be called from command line: |
---|
59 | >> D.Length = 1; |
---|
60 | >> DriftPass(D,[0.01 0 0 0 0 0]') |
---|
61 | |
---|
62 | 1.2 A typical mex-file (See MATLAB API reference manual) |
---|
63 | is a shared library that exports only one function - mexFunction(). |
---|
64 | |
---|
65 | ( On Windows NT the list of exported functions of a.dll file |
---|
66 | can be viewed with right-click, QuickView. I think 'nm' |
---|
67 | does the same on linux ) |
---|
68 | |
---|
69 | In Accelerator Toolbox in order to achieve maximum speed |
---|
70 | for multi-turn tracking, pass-methods also export |
---|
71 | a passFuntion(). This allows other mex-files to load and |
---|
72 | execute pass-methods directly. |
---|
73 | ( See the prototype of passFunction |
---|
74 | in ATROOT/simulator/element/elempass.h) |
---|
75 | |
---|
76 | 1.3 Compiling pass-methods on a different platform. |
---|
77 | All I had to do was to modify the declaration |
---|
78 | of passFunction in one header file: elempass.h |
---|
79 | used by all pass-methods |
---|
80 | |
---|
81 | on Windows |
---|
82 | #define ExportMode __declspec(dllexport) |
---|
83 | |
---|
84 | I tried on Linux and True 64 Unix: define ExportMode to nothing |
---|
85 | #define ExportMode |
---|
86 | |
---|
87 | 1.4 File name convention. |
---|
88 | |
---|
89 | I realized after AT1.0 that |
---|
90 | MATLAB on UNIX is case sensitive. It is inconvenient |
---|
91 | to type commands with upper case characters |
---|
92 | from MATLAB command prompt. However, upper case is |
---|
93 | great for naming pass-methods (StrMPoleSymplectic4Pass) |
---|
94 | Beginning in AT1.1, |
---|
95 | ONLY pass-methods mex-file names should contain upper case |
---|
96 | All other filenames (.m and mex-files) should be lowercase |
---|
97 | |
---|
98 | |
---|
99 | 2. There is a group of functions for tracking intended for a |
---|
100 | |
---|
101 | small number of turns. Located in ATROOT/simulator/track |
---|
102 | ringpass.c |
---|
103 | linepass.c |
---|
104 | |
---|
105 | These are trivial to port because they use only PORTABLE C |
---|
106 | on the new platform should do it |
---|
107 | |
---|
108 | |
---|
109 | >>mex ringpass.c |
---|
110 | |
---|
111 | They do NOT call pass-methods directly but |
---|
112 | through MATLAB API function 'mexCallMATLAB' |
---|
113 | Therefore if a pass-method works from command line |
---|
114 | as in the example above: |
---|
115 | |
---|
116 | >> DriftPass(D,[0.01 0 0 0 0 0]') |
---|
117 | |
---|
118 | ringpass should be able to call it. |
---|
119 | |
---|
120 | |
---|
121 | 3. Functions that track particles through the ring |
---|
122 | for LARGE number of turns |
---|
123 | located in ATROOT/simulator/track |
---|
124 | |
---|
125 | ringpassw.c - 'w' for windows |
---|
126 | ringpassl.c - 'l' for linux |
---|
127 | ... |
---|
128 | These are OPTIMIZED for speed on specific platform. |
---|
129 | Unlike 'ringpass' or 'linepass' in 2. they load |
---|
130 | pass-methods as dynamic shared libraries and |
---|
131 | execute passFunction. |
---|
132 | |
---|
133 | These require SOME WORK to port. |
---|
134 | |
---|
135 | 3.1. Calling a function in a shared library is a feature |
---|
136 | provided by the operating system. |
---|
137 | There is almost a one-to-one correspondence |
---|
138 | of C functions in system libraries. |
---|
139 | |
---|
140 | Windows | Linux |
---|
141 | ______________________|____________________ |
---|
142 | #include <windows.h> | #include <dlfcn.h> |
---|
143 | ______________________|____________________ |
---|
144 | LoadLibrary() | dlopen() |
---|
145 | GetProcAddress() | dlsym() |
---|
146 | FreeLibrary() | dlclose() |
---|
147 | |
---|
148 | |
---|
149 | 4. In future, as the number of functions in AT increases, some |
---|
150 | physics functions/tools (other than categories 1,2 above) |
---|
151 | might gain in speed tremendously, if written in C instead of |
---|
152 | MATLAB language. |
---|
153 | |
---|
154 | AT philosophy: |
---|
155 | a. The number of these should be kept to MINIMAL |
---|
156 | b. They shoul be self-contained |
---|
157 | (single C source file for each mex-file) |
---|
158 | c. They should be written in PORTABLE C |
---|
159 | |
---|
160 | For example: |
---|
161 | In AT1.1 (coming in June for PAC 2001 conference) |
---|
162 | there is a findmpoleraddiffmatrix.c |
---|
163 | that calculates Ohmi's radiation diffusion matrix |
---|
164 | for equilibrium beam envelope calculations |
---|