MAOS
Multithreaded Adaptive Optics Simulator
|
The user is required to have a good understanding of the C fundamentals, like the pointer, to study and improve the code. Here we list a few to remind the user.
If you declare an array: double a[1024];
the symbol actually contains the address of a 1024x8 byte memory region that is automatically allocated in the stack (malloc, calloc allocations memory in the heap). Suppose the address of the memory region is 0xFF00. Assignment a[0]=1;
puts 1 in memory started at 0xFF00. And a[1]=1;
puts 1 in memory started at 0xFF08.
Stack is the memory region allocated dynamically when calling a function. All variables reside in stack automatically disappear when the function returns.
Heap is the memory reservoir that persists during the life time of the executable. The functions malloc
, calloc
, realloc
reserves memory in the heap while free
removes the reservation.
A pointer is a variable that stores the address of a memory region. For example, void *p;
is a basic pointer that just stores the address and can not do any thing about the memory it points to. Its value can be assigned to any other non const vector or from any vector (1d or 2d).
However, a normal pointer like double *p;
also contains information about the memory it points to. More specifically, the compiler know the length of each element so that it can compute the address offset of the elements, just like arrays. For example:
A pointer can be used to create and index an 1-dimensional (1-d) array in the heap:
double *p; //declares a double array pointer. p=calloc(1024,sizeof(double)); //Allocate a memory block of 1024x8 byte and store its address in p. p[0]=1; //will assign 1 to the first number in the array.
Beware about a const pointer and pointer that points to constant memory regions:
A two-dimensional (2-d) pointer is like an array pointer. It stores the address of a memory region. However, unlike the array pointer, the compiler knows the length of the first dimension so that is can compute the memory location given 2-d index like a
[2][3];
Notice that in 2-d indexing a
[2][3], the last index changes the fastest.
MAOS defines and uses a few structs to manage memory for basic math types. Those types are defined in the math
folder.
For example, dmat is used for 1-d or 2-d double array, and cmat is used for complex array. There is a predefined macro P() to help indexing the arrays:
Sparse arrays are managed using dsp for double and csp for complex.
In addition, loc_t is used to describe x and y coordinate of uniformly spaced points. map_t is derived from dmat and used to describe 2-d array of opd or amplitude data with origin and resolution information.
Numerical arrays can be stores in cell arrays like in matlab. Different types have their specific cell definitions. For example, dcell is used to contain dmat pointers, and ccell is used to contain cmat pointers. All cell arrays share the same struct layout and can be allocated using cellnew() and deallocated using cellfree(). However, access elements is better handled with specific cell types to avoid casting. The cell array can contain arbitrary points.
Functions for those fundamental math types are defined using macros that works similarly to the C++ template.
Specific calculation related types are often encapsulated in corresponding data types. Those types are defined in the lib
folder.
The bin
file format is a simple custom binary format developed specifically to save telemetry data for maos
. It can be converted to fits
file using the supplied binary bin/bin2fits
. The data in bin
file are stored with the native endian-ness to facilitate memory mapping.
Data in bin
file are stored as one or multiple consecutive blocks
:
Each block
is composed of a header
and the data
.
The header
contains an optional part followed by a mandatory part. The optional part describes the data:
The mandatory part contains the type and size of data
The data
can be either array of fundamental data type or array of other arrays. For array of the fundamental data type, it is simply a copy of the data in the native endian-ness. For arrays of other arrays, the magic
is set to 0x6420 and the data is composed of nx*ny
blocks
. The data
part is skipped if either nx
or ny
equals to zero.
The bin
file is readable in Matlab or Python using the supplied read
or readbin
routines. In maos
, it can be either read or mapped to memory. For example The atmospheric screens are mapped to memory which enables using extra large atmospheric screens that may not fit in the memory and also enables sharing between concurrent maos runs that uses the same seed.
When modifying the code, please adhere to the following guidelines.
Memory allocation:
Functions:
Others: